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, this
1035 * needs to be removed again to put it in a register. exec_reg then
1036 * adds the escaping back later.
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 TRUE and store a pointer to its
1424 get_spec_reg(regname
, argp
, allocated
, errmsg
)
1427 int *allocated
; /* return: TRUE when value was allocated */
1428 int errmsg
; /* give error message when failing */
1436 case '%': /* file name */
1438 check_fname(); /* will give emsg if not set */
1439 *argp
= curbuf
->b_fname
;
1442 case '#': /* alternate file name */
1443 *argp
= getaltfname(errmsg
); /* may give emsg if not set */
1447 case '=': /* result of expression */
1448 *argp
= get_expr_line();
1453 case ':': /* last command line */
1454 if (last_cmdline
== NULL
&& errmsg
)
1455 EMSG(_(e_nolastcmd
));
1456 *argp
= last_cmdline
;
1459 case '/': /* last search-pattern */
1460 if (last_search_pat() == NULL
&& errmsg
)
1461 EMSG(_(e_noprevre
));
1462 *argp
= last_search_pat();
1465 case '.': /* last inserted text */
1466 *argp
= get_last_insert_save();
1468 if (*argp
== NULL
&& errmsg
)
1469 EMSG(_(e_noinstext
));
1472 #ifdef FEAT_SEARCHPATH
1473 case Ctrl_F
: /* Filename under cursor */
1474 case Ctrl_P
: /* Path under cursor, expand via "path" */
1477 *argp
= file_name_at_cursor(FNAME_MESS
| FNAME_HYP
1478 | (regname
== Ctrl_P
? FNAME_EXP
: 0), 1L, NULL
);
1483 case Ctrl_W
: /* word under cursor */
1484 case Ctrl_A
: /* WORD (mnemonic All) under cursor */
1487 cnt
= find_ident_under_cursor(argp
, regname
== Ctrl_W
1488 ? (FIND_IDENT
|FIND_STRING
) : FIND_STRING
);
1489 *argp
= cnt
? vim_strnsave(*argp
, cnt
) : NULL
;
1493 case '_': /* black hole: always empty */
1494 *argp
= (char_u
*)"";
1502 * Paste a yank register into the command line.
1503 * Only for non-special registers.
1504 * Used by CTRL-R command in command-line mode
1505 * insert_reg() can't be used here, because special characters from the
1506 * register contents will be interpreted as commands.
1508 * return FAIL for failure, OK otherwise
1511 cmdline_paste_reg(regname
, literally
, remcr
)
1513 int literally
; /* Insert text literally instead of "as typed" */
1514 int remcr
; /* don't add trailing CR */
1518 get_yank_register(regname
, FALSE
);
1519 if (y_current
->y_array
== NULL
)
1522 for (i
= 0; i
< y_current
->y_size
; ++i
)
1524 cmdline_paste_str(y_current
->y_array
[i
], literally
);
1526 /* Insert ^M between lines and after last line if type is MLINE.
1527 * Don't do this when "remcr" is TRUE and the next line is empty. */
1528 if (y_current
->y_type
== MLINE
1529 || (i
< y_current
->y_size
- 1
1531 && i
== y_current
->y_size
- 2
1532 && *y_current
->y_array
[i
+ 1] == NUL
)))
1533 cmdline_paste_str((char_u
*)"\r", literally
);
1535 /* Check for CTRL-C, in case someone tries to paste a few thousand
1536 * lines and gets bored. */
1544 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
1546 * Adjust the register name pointed to with "rp" for the clipboard being
1547 * used always and the clipboard being available.
1553 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1554 if (*rp
== 0 && clip_unnamed
)
1556 if (!clip_star
.available
&& *rp
== '*')
1558 if (!clip_plus
.available
&& *rp
== '+')
1564 * op_delete - handle a delete operation
1566 * return FAIL if undo failed, OK otherwise.
1576 char_u
*newp
, *oldp
;
1577 struct block_def bd
;
1579 linenr_T old_lcount
= curbuf
->b_ml
.ml_line_count
;
1580 int did_yank
= FALSE
;
1582 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
) /* nothing to do */
1585 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1587 return u_save_cursor();
1589 if (!curbuf
->b_p_ma
)
1591 EMSG(_(e_modifiable
));
1595 #ifdef FEAT_CLIPBOARD
1596 adjust_clip_reg(&oap
->regname
);
1601 mb_adjust_opend(oap
);
1605 * Imitate the strange Vi behaviour: If the delete spans more than one line
1606 * and motion_type == MCHAR and the result is a blank line, make the delete
1607 * linewise. Don't do this for the change command or Visual mode.
1609 if ( oap
->motion_type
== MCHAR
1614 && oap
->line_count
> 1
1615 && oap
->op_type
== OP_DELETE
)
1617 ptr
= ml_get(oap
->end
.lnum
) + oap
->end
.col
+ oap
->inclusive
;
1618 ptr
= skipwhite(ptr
);
1619 if (*ptr
== NUL
&& inindent(0))
1620 oap
->motion_type
= MLINE
;
1624 * Check for trying to delete (e.g. "D") in an empty line.
1625 * Note: For the change operator it is ok.
1627 if ( oap
->motion_type
== MCHAR
1628 && oap
->line_count
== 1
1629 && oap
->op_type
== OP_DELETE
1630 && *ml_get(oap
->start
.lnum
) == NUL
)
1633 * It's an error to operate on an empty region, when 'E' included in
1634 * 'cpoptions' (Vi compatible).
1636 #ifdef FEAT_VIRTUALEDIT
1638 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1639 * marks as if it happened. */
1642 if (vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
)
1648 * Do a yank of whatever we're about to delete.
1649 * If a yank register was specified, put the deleted text into that register.
1650 * For the black hole register '_' don't yank anything.
1652 if (oap
->regname
!= '_')
1654 if (oap
->regname
!= 0)
1656 /* check for read-only register */
1657 if (!valid_yank_reg(oap
->regname
, TRUE
))
1662 get_yank_register(oap
->regname
, TRUE
); /* yank into specif'd reg. */
1663 if (op_yank(oap
, TRUE
, FALSE
) == OK
) /* yank without message */
1668 * Put deleted text into register 1 and shift number registers if the
1669 * delete contains a line break, or when a regname has been specified.
1671 if (oap
->regname
!= 0 || oap
->motion_type
== MLINE
1672 || oap
->line_count
> 1 || oap
->use_reg_one
)
1674 y_current
= &y_regs
[9];
1675 free_yank_all(); /* free register nine */
1676 for (n
= 9; n
> 1; --n
)
1677 y_regs
[n
] = y_regs
[n
- 1];
1678 y_previous
= y_current
= &y_regs
[1];
1679 y_regs
[1].y_array
= NULL
; /* set register one to empty */
1680 if (op_yank(oap
, TRUE
, FALSE
) == OK
)
1684 /* Yank into small delete register when no register specified and the
1685 * delete is within one line. */
1686 if (oap
->regname
== 0 && oap
->motion_type
!= MLINE
1687 && oap
->line_count
== 1)
1690 get_yank_register(oap
->regname
, TRUE
);
1691 if (op_yank(oap
, TRUE
, FALSE
) == OK
)
1697 * If there's too much stuff to fit in the yank register, then get a
1698 * confirmation before doing the delete. This is crude, but simple.
1699 * And it avoids doing a delete of something we can't put back if we
1704 int msg_silent_save
= msg_silent
;
1706 msg_silent
= 0; /* must display the prompt */
1707 n
= ask_yesno((char_u
*)_("cannot yank; delete anyway"), TRUE
);
1708 msg_silent
= msg_silent_save
;
1721 if (oap
->block_mode
)
1723 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
1724 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
1727 for (lnum
= curwin
->w_cursor
.lnum
; lnum
<= oap
->end
.lnum
; ++lnum
)
1729 block_prep(oap
, &bd
, lnum
, TRUE
);
1730 if (bd
.textlen
== 0) /* nothing to delete */
1733 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1734 if (lnum
== curwin
->w_cursor
.lnum
)
1736 curwin
->w_cursor
.col
= bd
.textcol
+ bd
.startspaces
;
1737 # ifdef FEAT_VIRTUALEDIT
1738 curwin
->w_cursor
.coladd
= 0;
1742 /* n == number of chars deleted
1743 * If we delete a TAB, it may be replaced by several characters.
1744 * Thus the number of characters may increase!
1746 n
= bd
.textlen
- bd
.startspaces
- bd
.endspaces
;
1747 oldp
= ml_get(lnum
);
1748 newp
= alloc_check((unsigned)STRLEN(oldp
) + 1 - n
);
1751 /* copy up to deleted part */
1752 mch_memmove(newp
, oldp
, (size_t)bd
.textcol
);
1754 copy_spaces(newp
+ bd
.textcol
,
1755 (size_t)(bd
.startspaces
+ bd
.endspaces
));
1756 /* copy the part after the deleted part */
1757 oldp
+= bd
.textcol
+ bd
.textlen
;
1758 mch_memmove(newp
+ bd
.textcol
+ bd
.startspaces
+ bd
.endspaces
,
1759 oldp
, STRLEN(oldp
) + 1);
1760 /* replace the line */
1761 ml_replace(lnum
, newp
, FALSE
);
1765 changed_lines(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
,
1766 oap
->end
.lnum
+ 1, 0L);
1767 oap
->line_count
= 0; /* no lines deleted */
1771 if (oap
->motion_type
== MLINE
)
1773 if (oap
->op_type
== OP_CHANGE
)
1775 /* Delete the lines except the first one. Temporarily move the
1776 * cursor to the next line. Save the current line number, if the
1777 * last line is deleted it may be changed.
1779 if (oap
->line_count
> 1)
1781 lnum
= curwin
->w_cursor
.lnum
;
1782 ++curwin
->w_cursor
.lnum
;
1783 del_lines((long)(oap
->line_count
- 1), TRUE
);
1784 curwin
->w_cursor
.lnum
= lnum
;
1786 if (u_save_cursor() == FAIL
)
1788 if (curbuf
->b_p_ai
) /* don't delete indent */
1790 beginline(BL_WHITE
); /* cursor on first non-white */
1791 did_ai
= TRUE
; /* delete the indent when ESC hit */
1792 ai_col
= curwin
->w_cursor
.col
;
1795 beginline(0); /* cursor in column 0 */
1796 truncate_line(FALSE
); /* delete the rest of the line */
1797 /* leave cursor past last char in line */
1798 if (oap
->line_count
> 1)
1799 u_clearline(); /* "U" command not possible after "2cc" */
1803 del_lines(oap
->line_count
, TRUE
);
1804 beginline(BL_WHITE
| BL_FIX
);
1805 u_clearline(); /* "U" command not possible after "dd" */
1810 #ifdef FEAT_VIRTUALEDIT
1815 /* For virtualedit: break the tabs that are partly included. */
1816 if (gchar_pos(&oap
->start
) == '\t')
1818 if (u_save_cursor() == FAIL
) /* save first line for undo */
1820 if (oap
->line_count
== 1)
1821 endcol
= getviscol2(oap
->end
.col
, oap
->end
.coladd
);
1822 coladvance_force(getviscol2(oap
->start
.col
, oap
->start
.coladd
));
1823 oap
->start
= curwin
->w_cursor
;
1824 if (oap
->line_count
== 1)
1827 oap
->end
.col
= curwin
->w_cursor
.col
;
1828 oap
->end
.coladd
= curwin
->w_cursor
.coladd
;
1829 curwin
->w_cursor
= oap
->start
;
1833 /* Break a tab only when it's included in the area. */
1834 if (gchar_pos(&oap
->end
) == '\t'
1835 && (int)oap
->end
.coladd
< oap
->inclusive
)
1837 /* save last line for undo */
1838 if (u_save((linenr_T
)(oap
->end
.lnum
- 1),
1839 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
1841 curwin
->w_cursor
= oap
->end
;
1842 coladvance_force(getviscol2(oap
->end
.col
, oap
->end
.coladd
));
1843 oap
->end
= curwin
->w_cursor
;
1844 curwin
->w_cursor
= oap
->start
;
1849 if (oap
->line_count
== 1) /* delete characters within one line */
1851 if (u_save_cursor() == FAIL
) /* save line for undo */
1854 /* if 'cpoptions' contains '$', display '$' at end of change */
1855 if ( vim_strchr(p_cpo
, CPO_DOLLAR
) != NULL
1856 && oap
->op_type
== OP_CHANGE
1857 && oap
->end
.lnum
== curwin
->w_cursor
.lnum
1862 display_dollar(oap
->end
.col
- !oap
->inclusive
);
1864 n
= oap
->end
.col
- oap
->start
.col
+ 1 - !oap
->inclusive
;
1866 #ifdef FEAT_VIRTUALEDIT
1869 /* fix up things for virtualedit-delete:
1870 * break the tabs which are going to get in our way
1872 char_u
*curline
= ml_get_curline();
1873 int len
= (int)STRLEN(curline
);
1875 if (oap
->end
.coladd
!= 0
1876 && (int)oap
->end
.col
>= len
- 1
1877 && !(oap
->start
.coladd
&& (int)oap
->end
.col
>= len
- 1))
1879 /* Delete at least one char (e.g, when on a control char). */
1880 if (n
== 0 && oap
->start
.coladd
!= oap
->end
.coladd
)
1883 /* When deleted a char in the line, reset coladd. */
1884 if (gchar_cursor() != NUL
)
1885 curwin
->w_cursor
.coladd
= 0;
1888 (void)del_bytes((long)n
, !virtual_op
, oap
->op_type
== OP_DELETE
1894 else /* delete characters between lines */
1898 /* save deleted and changed lines for undo */
1899 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
1900 (linenr_T
)(curwin
->w_cursor
.lnum
+ oap
->line_count
)) == FAIL
)
1903 truncate_line(TRUE
); /* delete from cursor to end of line */
1905 curpos
= curwin
->w_cursor
; /* remember curwin->w_cursor */
1906 ++curwin
->w_cursor
.lnum
;
1907 del_lines((long)(oap
->line_count
- 2), FALSE
);
1909 /* delete from start of line until op_end */
1910 curwin
->w_cursor
.col
= 0;
1911 (void)del_bytes((long)(oap
->end
.col
+ 1 - !oap
->inclusive
),
1912 !virtual_op
, oap
->op_type
== OP_DELETE
1917 curwin
->w_cursor
= curpos
; /* restore curwin->w_cursor */
1919 (void)do_join(FALSE
);
1923 msgmore(curbuf
->b_ml
.ml_line_count
- old_lcount
);
1925 #ifdef FEAT_VIRTUALEDIT
1929 if (oap
->block_mode
)
1931 curbuf
->b_op_end
.lnum
= oap
->end
.lnum
;
1932 curbuf
->b_op_end
.col
= oap
->start
.col
;
1936 curbuf
->b_op_end
= oap
->start
;
1937 curbuf
->b_op_start
= oap
->start
;
1944 * Adjust end of operating area for ending on a multi-byte character.
1945 * Used for deletion.
1948 mb_adjust_opend(oap
)
1955 p
= ml_get(oap
->end
.lnum
);
1956 oap
->end
.col
+= mb_tail_off(p
, p
+ oap
->end
.col
);
1961 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1963 * Replace a whole area with one character.
1974 char_u
*newp
, *oldp
;
1976 struct block_def bd
;
1978 if ((curbuf
->b_ml
.ml_flags
& ML_EMPTY
) || oap
->empty
)
1979 return OK
; /* nothing to do */
1983 mb_adjust_opend(oap
);
1986 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
1987 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
1991 * block mode replace
1993 if (oap
->block_mode
)
1995 bd
.is_MAX
= (curwin
->w_curswant
== MAXCOL
);
1996 for ( ; curwin
->w_cursor
.lnum
<= oap
->end
.lnum
; ++curwin
->w_cursor
.lnum
)
1998 block_prep(oap
, &bd
, curwin
->w_cursor
.lnum
, TRUE
);
1999 if (bd
.textlen
== 0 && (!virtual_op
|| bd
.is_MAX
))
2000 continue; /* nothing to replace */
2002 /* n == number of extra chars required
2003 * If we split a TAB, it may be replaced by several characters.
2004 * Thus the number of characters may increase!
2006 #ifdef FEAT_VIRTUALEDIT
2007 /* If the range starts in virtual space, count the initial
2008 * coladd offset as part of "startspaces" */
2009 if (virtual_op
&& bd
.is_short
&& *bd
.textstart
== NUL
)
2013 getvpos(&vpos
, oap
->start_vcol
);
2014 bd
.startspaces
+= vpos
.coladd
;
2019 /* allow for pre spaces */
2020 n
= (bd
.startspaces
? bd
.start_char_vcols
- 1 : 0);
2022 /* allow for post spp */
2024 #ifdef FEAT_VIRTUALEDIT
2027 && bd
.end_char_vcols
> 0) ? bd
.end_char_vcols
- 1 : 0;
2028 /* Figure out how many characters to replace. */
2029 numc
= oap
->end_vcol
- oap
->start_vcol
+ 1;
2030 if (bd
.is_short
&& (!virtual_op
|| bd
.is_MAX
))
2031 numc
-= (oap
->end_vcol
- bd
.end_vcol
) + 1;
2034 /* A double-wide character can be replaced only up to half the
2036 if ((*mb_char2cells
)(c
) > 1)
2038 if ((numc
& 1) && !bd
.is_short
)
2046 /* Compute bytes needed, move character count to num_chars. */
2048 numc
*= (*mb_char2len
)(c
);
2050 /* oldlen includes textlen, so don't double count */
2051 n
+= numc
- bd
.textlen
;
2053 oldp
= ml_get_curline();
2054 oldlen
= STRLEN(oldp
);
2055 newp
= alloc_check((unsigned)oldlen
+ 1 + n
);
2058 vim_memset(newp
, NUL
, (size_t)(oldlen
+ 1 + n
));
2059 /* copy up to deleted part */
2060 mch_memmove(newp
, oldp
, (size_t)bd
.textcol
);
2061 oldp
+= bd
.textcol
+ bd
.textlen
;
2062 /* insert pre-spaces */
2063 copy_spaces(newp
+ bd
.textcol
, (size_t)bd
.startspaces
);
2064 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2068 n
= (int)STRLEN(newp
);
2069 while (--num_chars
>= 0)
2070 n
+= (*mb_char2bytes
)(c
, newp
+ n
);
2074 copy_chars(newp
+ STRLEN(newp
), (size_t)numc
, c
);
2077 /* insert post-spaces */
2078 copy_spaces(newp
+ STRLEN(newp
), (size_t)bd
.endspaces
);
2079 /* copy the part after the changed part */
2080 mch_memmove(newp
+ STRLEN(newp
), oldp
, STRLEN(oldp
) + 1);
2082 /* replace the line */
2083 ml_replace(curwin
->w_cursor
.lnum
, newp
, FALSE
);
2089 * MCHAR and MLINE motion replace.
2091 if (oap
->motion_type
== MLINE
)
2094 curwin
->w_cursor
.col
= 0;
2095 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
2099 else if (!oap
->inclusive
)
2102 while (ltoreq(curwin
->w_cursor
, oap
->end
))
2108 if ((*mb_char2len
)(c
) > 1 || (*mb_char2len
)(n
) > 1)
2110 /* This is slow, but it handles replacing a single-byte
2111 * with a multi-byte and the other way around. */
2112 oap
->end
.col
+= (*mb_char2len
)(c
) - (*mb_char2len
)(n
);
2117 /* Backup to the replaced character. */
2123 #ifdef FEAT_VIRTUALEDIT
2128 if (curwin
->w_cursor
.lnum
== oap
->end
.lnum
)
2130 /* oap->end has to be recalculated when
2132 end_vcol
= getviscol2(oap
->end
.col
,
2135 coladvance_force(getviscol());
2136 if (curwin
->w_cursor
.lnum
== oap
->end
.lnum
)
2137 getvpos(&oap
->end
, end_vcol
);
2140 pchar(curwin
->w_cursor
, c
);
2143 #ifdef FEAT_VIRTUALEDIT
2144 else if (virtual_op
&& curwin
->w_cursor
.lnum
== oap
->end
.lnum
)
2146 int virtcols
= oap
->end
.coladd
;
2148 if (curwin
->w_cursor
.lnum
== oap
->start
.lnum
2149 && oap
->start
.col
== oap
->end
.col
&& oap
->start
.coladd
)
2150 virtcols
-= oap
->start
.coladd
;
2152 /* oap->end has been trimmed so it's effectively inclusive;
2153 * as a result an extra +1 must be counted so we don't
2154 * trample the NUL byte. */
2155 coladvance_force(getviscol2(oap
->end
.col
, oap
->end
.coladd
) + 1);
2156 curwin
->w_cursor
.col
-= (virtcols
+ 1);
2157 for (; virtcols
>= 0; virtcols
--)
2159 pchar(curwin
->w_cursor
, c
);
2160 if (inc(&curwin
->w_cursor
) == -1)
2166 /* Advance to next character, stop at the end of the file. */
2167 if (inc_cursor() == -1)
2172 curwin
->w_cursor
= oap
->start
;
2174 changed_lines(oap
->start
.lnum
, oap
->start
.col
, oap
->end
.lnum
+ 1, 0L);
2176 /* Set "'[" and "']" marks. */
2177 curbuf
->b_op_start
= oap
->start
;
2178 curbuf
->b_op_end
= oap
->end
;
2185 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2193 struct block_def bd
;
2198 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
2199 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
2204 if (oap
->block_mode
) /* Visual block mode */
2206 for (; pos
.lnum
<= oap
->end
.lnum
; ++pos
.lnum
)
2208 block_prep(oap
, &bd
, pos
.lnum
, FALSE
);
2209 pos
.col
= bd
.textcol
;
2210 for (todo
= bd
.textlen
; todo
> 0; --todo
)
2214 todo
-= (*mb_ptr2len
)(ml_get_pos(&pos
)) - 1;
2216 did_change
|= swapchar(oap
->op_type
, &pos
);
2217 if (inc(&pos
) == -1) /* at end of file */
2220 # ifdef FEAT_NETBEANS_INTG
2221 if (usingNetbeans
&& did_change
)
2223 char_u
*ptr
= ml_get_buf(curbuf
, pos
.lnum
, FALSE
);
2225 netbeans_removed(curbuf
, pos
.lnum
, bd
.textcol
,
2227 netbeans_inserted(curbuf
, pos
.lnum
, bd
.textcol
,
2228 &ptr
[bd
.textcol
], bd
.textlen
);
2233 changed_lines(oap
->start
.lnum
, 0, oap
->end
.lnum
+ 1, 0L);
2235 else /* not block mode */
2238 if (oap
->motion_type
== MLINE
)
2242 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
2246 else if (!oap
->inclusive
)
2249 while (ltoreq(pos
, oap
->end
))
2251 did_change
|= swapchar(oap
->op_type
, &pos
);
2252 if (inc(&pos
) == -1) /* at end of file */
2258 changed_lines(oap
->start
.lnum
, oap
->start
.col
, oap
->end
.lnum
+ 1,
2260 #ifdef FEAT_NETBEANS_INTG
2261 if (usingNetbeans
&& did_change
)
2267 while (pos
.lnum
< oap
->end
.lnum
)
2269 ptr
= ml_get_buf(curbuf
, pos
.lnum
, FALSE
);
2270 count
= (int)STRLEN(ptr
) - pos
.col
;
2271 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
2272 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
2273 &ptr
[pos
.col
], count
);
2277 ptr
= ml_get_buf(curbuf
, pos
.lnum
, FALSE
);
2278 count
= oap
->end
.col
- pos
.col
+ 1;
2279 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
2280 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
2281 &ptr
[pos
.col
], count
);
2288 if (!did_change
&& oap
->is_VIsual
)
2289 /* No change: need to remove the Visual selection */
2290 redraw_curbuf_later(INVERTED
);
2294 * Set '[ and '] marks.
2296 curbuf
->b_op_start
= oap
->start
;
2297 curbuf
->b_op_end
= oap
->end
;
2299 if (oap
->line_count
> p_report
)
2301 if (oap
->line_count
== 1)
2302 MSG(_("1 line changed"));
2304 smsg((char_u
*)_("%ld lines changed"), oap
->line_count
);
2309 * If op_type == OP_UPPER: make uppercase,
2310 * if op_type == OP_LOWER: make lowercase,
2311 * if op_type == OP_ROT13: do rot13 encoding,
2312 * else swap case of character at 'pos'
2313 * returns TRUE when something actually changed.
2316 swapchar(op_type
, pos
)
2325 /* Only do rot13 encoding for ASCII characters. */
2326 if (c
>= 0x80 && op_type
== OP_ROT13
)
2330 if (op_type
== OP_UPPER
&& enc_latin1like
&& c
== 0xdf)
2332 pos_T sp
= curwin
->w_cursor
;
2334 /* Special handling of German sharp s: change to "SS". */
2335 curwin
->w_cursor
= *pos
;
2339 curwin
->w_cursor
= sp
;
2343 if (enc_dbcs
!= 0 && c
>= 0x100) /* No lower/uppercase letter */
2349 if (op_type
== OP_ROT13
)
2351 else if (op_type
!= OP_LOWER
)
2354 else if (MB_ISUPPER(c
))
2356 if (op_type
== OP_ROT13
)
2358 else if (op_type
!= OP_UPPER
)
2364 if (enc_utf8
&& (c
>= 0x80 || nc
>= 0x80))
2366 pos_T sp
= curwin
->w_cursor
;
2368 curwin
->w_cursor
= *pos
;
2371 curwin
->w_cursor
= sp
;
2381 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2383 * op_insert - Insert and append operators for Visual mode.
2386 op_insert(oap
, count1
)
2390 long ins_len
, pre_textlen
= 0;
2391 char_u
*firstline
, *ins_text
;
2392 struct block_def bd
;
2395 /* edit() changes this - record it for OP_APPEND */
2396 bd
.is_MAX
= (curwin
->w_curswant
== MAXCOL
);
2398 /* vis block is still marked. Get rid of it now. */
2399 curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
2400 update_screen(INVERTED
);
2402 if (oap
->block_mode
)
2404 #ifdef FEAT_VIRTUALEDIT
2405 /* When 'virtualedit' is used, need to insert the extra spaces before
2406 * doing block_prep(). When only "block" is used, virtual edit is
2407 * already disabled, but still need it when calling
2408 * coladvance_force(). */
2409 if (curwin
->w_cursor
.coladd
> 0)
2411 int old_ve_flags
= ve_flags
;
2414 if (u_save_cursor() == FAIL
)
2416 coladvance_force(oap
->op_type
== OP_APPEND
2417 ? oap
->end_vcol
+ 1 : getviscol());
2418 if (oap
->op_type
== OP_APPEND
)
2419 --curwin
->w_cursor
.col
;
2420 ve_flags
= old_ve_flags
;
2423 /* Get the info about the block before entering the text */
2424 block_prep(oap
, &bd
, oap
->start
.lnum
, TRUE
);
2425 firstline
= ml_get(oap
->start
.lnum
) + bd
.textcol
;
2426 if (oap
->op_type
== OP_APPEND
)
2427 firstline
+= bd
.textlen
;
2428 pre_textlen
= (long)STRLEN(firstline
);
2431 if (oap
->op_type
== OP_APPEND
)
2434 #ifdef FEAT_VIRTUALEDIT
2435 && curwin
->w_cursor
.coladd
== 0
2439 /* Move the cursor to the character right of the block. */
2440 curwin
->w_set_curswant
= TRUE
;
2441 while (*ml_get_cursor() != NUL
2442 && (curwin
->w_cursor
.col
< bd
.textcol
+ bd
.textlen
))
2443 ++curwin
->w_cursor
.col
;
2444 if (bd
.is_short
&& !bd
.is_MAX
)
2446 /* First line was too short, make it longer and adjust the
2447 * values in "bd". */
2448 if (u_save_cursor() == FAIL
)
2450 for (i
= 0; i
< bd
.endspaces
; ++i
)
2452 bd
.textlen
+= bd
.endspaces
;
2457 curwin
->w_cursor
= oap
->end
;
2460 /* Works just like an 'i'nsert on the next character. */
2461 if (!lineempty(curwin
->w_cursor
.lnum
)
2462 && oap
->start_vcol
!= oap
->end_vcol
)
2467 edit(NUL
, FALSE
, (linenr_T
)count1
);
2469 /* if user has moved off this line, we don't know what to do, so do
2471 if (curwin
->w_cursor
.lnum
!= oap
->start
.lnum
)
2474 if (oap
->block_mode
)
2476 struct block_def bd2
;
2479 * Spaces and tabs in the indent may have changed to other spaces and
2480 * tabs. Get the starting column again and correct the lenght.
2481 * Don't do this when "$" used, end-of-line will have changed.
2483 block_prep(oap
, &bd2
, oap
->start
.lnum
, TRUE
);
2484 if (!bd
.is_MAX
|| bd2
.textlen
< bd
.textlen
)
2486 if (oap
->op_type
== OP_APPEND
)
2488 pre_textlen
+= bd2
.textlen
- bd
.textlen
;
2492 bd
.textcol
= bd2
.textcol
;
2493 bd
.textlen
= bd2
.textlen
;
2497 * Subsequent calls to ml_get() flush the firstline data - take a
2498 * copy of the required string.
2500 firstline
= ml_get(oap
->start
.lnum
) + bd
.textcol
;
2501 if (oap
->op_type
== OP_APPEND
)
2502 firstline
+= bd
.textlen
;
2503 if ((ins_len
= (long)STRLEN(firstline
) - pre_textlen
) > 0)
2505 ins_text
= vim_strnsave(firstline
, (int)ins_len
);
2506 if (ins_text
!= NULL
)
2508 /* block handled here */
2509 if (u_save(oap
->start
.lnum
,
2510 (linenr_T
)(oap
->end
.lnum
+ 1)) == OK
)
2511 block_insert(oap
, ins_text
, (oap
->op_type
== OP_INSERT
),
2514 curwin
->w_cursor
.col
= oap
->start
.col
;
2524 * op_change - handle a change operation
2526 * return TRUE if edit() returns because of a CTRL-O command
2534 #ifdef FEAT_VISUALEXTRA
2537 long ins_len
, pre_textlen
= 0;
2539 char_u
*ins_text
, *newp
, *oldp
;
2540 struct block_def bd
;
2544 if (oap
->motion_type
== MLINE
)
2547 #ifdef FEAT_SMARTINDENT
2548 if (!p_paste
&& curbuf
->b_p_si
2549 # ifdef FEAT_CINDENT
2553 can_si
= TRUE
; /* It's like opening a new line, do si */
2557 /* First delete the text in the region. In an empty buffer only need to
2559 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
2561 if (u_save_cursor() == FAIL
)
2564 else if (op_delete(oap
) == FAIL
)
2567 if ((l
> curwin
->w_cursor
.col
) && !lineempty(curwin
->w_cursor
.lnum
)
2571 #ifdef FEAT_VISUALEXTRA
2572 /* check for still on same line (<CR> in inserted text meaningless) */
2573 /* skip blank lines too */
2574 if (oap
->block_mode
)
2576 # ifdef FEAT_VIRTUALEDIT
2577 /* Add spaces before getting the current line length. */
2578 if (virtual_op
&& (curwin
->w_cursor
.coladd
> 0
2579 || gchar_cursor() == NUL
))
2580 coladvance_force(getviscol());
2582 pre_textlen
= (long)STRLEN(ml_get(oap
->start
.lnum
));
2583 bd
.textcol
= curwin
->w_cursor
.col
;
2587 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2588 if (oap
->motion_type
== MLINE
)
2592 retval
= edit(NUL
, FALSE
, (linenr_T
)1);
2594 #ifdef FEAT_VISUALEXTRA
2596 * In Visual block mode, handle copying the new text to all lines of the
2599 if (oap
->block_mode
&& oap
->start
.lnum
!= oap
->end
.lnum
)
2601 firstline
= ml_get(oap
->start
.lnum
);
2603 * Subsequent calls to ml_get() flush the firstline data - take a
2604 * copy of the required bit.
2606 if ((ins_len
= (long)STRLEN(firstline
) - pre_textlen
) > 0)
2608 if ((ins_text
= alloc_check((unsigned)(ins_len
+ 1))) != NULL
)
2610 vim_strncpy(ins_text
, firstline
+ bd
.textcol
, (size_t)ins_len
);
2611 for (linenr
= oap
->start
.lnum
+ 1; linenr
<= oap
->end
.lnum
;
2614 block_prep(oap
, &bd
, linenr
, TRUE
);
2615 if (!bd
.is_short
|| virtual_op
)
2617 # ifdef FEAT_VIRTUALEDIT
2620 /* If the block starts in virtual space, count the
2621 * initial coladd offset as part of "startspaces" */
2624 linenr_T lnum
= curwin
->w_cursor
.lnum
;
2626 curwin
->w_cursor
.lnum
= linenr
;
2627 (void)getvpos(&vpos
, oap
->start_vcol
);
2628 curwin
->w_cursor
.lnum
= lnum
;
2633 oldp
= ml_get(linenr
);
2634 newp
= alloc_check((unsigned)(STRLEN(oldp
)
2635 # ifdef FEAT_VIRTUALEDIT
2641 /* copy up to block start */
2642 mch_memmove(newp
, oldp
, (size_t)bd
.textcol
);
2643 offset
= bd
.textcol
;
2644 # ifdef FEAT_VIRTUALEDIT
2645 copy_spaces(newp
+ offset
, (size_t)vpos
.coladd
);
2646 offset
+= vpos
.coladd
;
2648 mch_memmove(newp
+ offset
, ins_text
, (size_t)ins_len
);
2651 mch_memmove(newp
+ offset
, oldp
, STRLEN(oldp
) + 1);
2652 ml_replace(linenr
, newp
, FALSE
);
2657 changed_lines(oap
->start
.lnum
+ 1, 0, oap
->end
.lnum
+ 1, 0L);
2668 * set all the yank registers to empty (called from main())
2675 for (i
= 0; i
< NUM_REGISTERS
; ++i
)
2676 y_regs
[i
].y_array
= NULL
;
2679 #if defined(EXITFREE) || defined(PROTO)
2685 for (i
= 0; i
< NUM_REGISTERS
; ++i
)
2687 y_current
= &y_regs
[i
];
2688 if (y_current
->y_array
!= NULL
)
2695 * Free "n" lines from the current yank register.
2696 * Called for normal freeing and in case of error.
2702 if (y_current
->y_array
!= NULL
)
2706 for (i
= n
; --i
>= 0; )
2708 #ifdef AMIGA /* only for very slow machines */
2709 if ((i
& 1023) == 1023) /* this may take a while */
2712 * This message should never cause a hit-return message.
2713 * Overwrite this message with any next message.
2716 smsg((char_u
*)_("freeing %ld lines"), i
+ 1);
2722 vim_free(y_current
->y_array
[i
]);
2724 vim_free(y_current
->y_array
);
2725 y_current
->y_array
= NULL
;
2736 free_yank(y_current
->y_size
);
2740 * Yank the text between "oap->start" and "oap->end" into a yank register.
2741 * If we are to append (uppercase register), we first yank into a new yank
2742 * register and then concatenate the old and the new one (so we keep the old
2743 * one in case of out-of-memory).
2745 * return FAIL for failure, OK otherwise
2748 op_yank(oap
, deleting
, mess
)
2753 long y_idx
; /* index in y_array[] */
2754 struct yankreg
*curr
; /* copy of y_current */
2755 struct yankreg newreg
; /* new yank register when appending */
2757 linenr_T lnum
; /* current line number */
2759 int yanktype
= oap
->motion_type
;
2760 long yanklines
= oap
->line_count
;
2761 linenr_T yankendlnum
= oap
->end
.lnum
;
2764 struct block_def bd
;
2766 /* check for read-only register */
2767 if (oap
->regname
!= 0 && !valid_yank_reg(oap
->regname
, TRUE
))
2772 if (oap
->regname
== '_') /* black hole: nothing to do */
2775 #ifdef FEAT_CLIPBOARD
2776 if (!clip_star
.available
&& oap
->regname
== '*')
2778 else if (!clip_plus
.available
&& oap
->regname
== '+')
2782 if (!deleting
) /* op_delete() already set y_current */
2783 get_yank_register(oap
->regname
, TRUE
);
2786 /* append to existing contents */
2787 if (y_append
&& y_current
->y_array
!= NULL
)
2788 y_current
= &newreg
;
2790 free_yank_all(); /* free previously yanked lines */
2793 * If the cursor was in column 1 before and after the movement, and the
2794 * operator is not inclusive, the yank is always linewise.
2796 if ( oap
->motion_type
== MCHAR
2797 && oap
->start
.col
== 0
2800 && (!oap
->is_VIsual
|| *p_sel
== 'o')
2803 && oap
->end
.col
== 0
2811 y_current
->y_size
= yanklines
;
2812 y_current
->y_type
= yanktype
; /* set the yank register type */
2814 y_current
->y_width
= 0;
2816 y_current
->y_array
= (char_u
**)lalloc_clear((long_u
)(sizeof(char_u
*) *
2819 if (y_current
->y_array
== NULL
)
2826 lnum
= oap
->start
.lnum
;
2829 if (oap
->block_mode
)
2831 /* Visual block mode */
2832 y_current
->y_type
= MBLOCK
; /* set the yank register type */
2833 y_current
->y_width
= oap
->end_vcol
- oap
->start_vcol
;
2835 if (curwin
->w_curswant
== MAXCOL
&& y_current
->y_width
> 0)
2836 y_current
->y_width
--;
2840 for ( ; lnum
<= yankendlnum
; lnum
++, y_idx
++)
2842 switch (y_current
->y_type
)
2846 block_prep(oap
, &bd
, lnum
, FALSE
);
2847 if (yank_copy_line(&bd
, y_idx
) == FAIL
)
2853 if ((y_current
->y_array
[y_idx
] =
2854 vim_strsave(ml_get(lnum
))) == NULL
)
2860 colnr_T startcol
= 0, endcol
= MAXCOL
;
2861 #ifdef FEAT_VIRTUALEDIT
2862 int is_oneChar
= FALSE
;
2869 if (lnum
== oap
->start
.lnum
)
2871 startcol
= oap
->start
.col
;
2872 #ifdef FEAT_VIRTUALEDIT
2875 getvcol(curwin
, &oap
->start
, &cs
, NULL
, &ce
);
2876 if (ce
!= cs
&& oap
->start
.coladd
> 0)
2878 /* Part of a tab selected -- but don't
2879 * double-count it. */
2880 bd
.startspaces
= (ce
- cs
+ 1)
2881 - oap
->start
.coladd
;
2888 if (lnum
== oap
->end
.lnum
)
2890 endcol
= oap
->end
.col
;
2891 #ifdef FEAT_VIRTUALEDIT
2894 getvcol(curwin
, &oap
->end
, &cs
, NULL
, &ce
);
2895 if (p
[endcol
] == NUL
|| (cs
+ oap
->end
.coladd
< ce
2897 /* Don't add space for double-wide
2898 * char; endcol will be on last byte
2899 * of multi-byte char. */
2900 && (*mb_head_off
)(p
, p
+ endcol
) == 0
2904 if (oap
->start
.lnum
== oap
->end
.lnum
2905 && oap
->start
.col
== oap
->end
.col
)
2907 /* Special case: inside a single char */
2909 bd
.startspaces
= oap
->end
.coladd
2910 - oap
->start
.coladd
+ oap
->inclusive
;
2915 bd
.endspaces
= oap
->end
.coladd
2917 endcol
-= oap
->inclusive
;
2923 if (startcol
> endcol
2924 #ifdef FEAT_VIRTUALEDIT
2931 if (endcol
== MAXCOL
)
2932 endcol
= (colnr_T
)STRLEN(p
);
2933 bd
.textlen
= endcol
- startcol
+ oap
->inclusive
;
2935 bd
.textstart
= p
+ startcol
;
2936 if (yank_copy_line(&bd
, y_idx
) == FAIL
)
2944 if (curr
!= y_current
) /* append the new block to the old block */
2946 new_ptr
= (char_u
**)lalloc((long_u
)(sizeof(char_u
*) *
2947 (curr
->y_size
+ y_current
->y_size
)), TRUE
);
2948 if (new_ptr
== NULL
)
2950 for (j
= 0; j
< curr
->y_size
; ++j
)
2951 new_ptr
[j
] = curr
->y_array
[j
];
2952 vim_free(curr
->y_array
);
2953 curr
->y_array
= new_ptr
;
2955 if (yanktype
== MLINE
) /* MLINE overrides MCHAR and MBLOCK */
2956 curr
->y_type
= MLINE
;
2958 /* Concatenate the last line of the old block with the first line of
2959 * the new block, unless being Vi compatible. */
2960 if (curr
->y_type
== MCHAR
&& vim_strchr(p_cpo
, CPO_REGAPPEND
) == NULL
)
2962 pnew
= lalloc((long_u
)(STRLEN(curr
->y_array
[curr
->y_size
- 1])
2963 + STRLEN(y_current
->y_array
[0]) + 1), TRUE
);
2966 y_idx
= y_current
->y_size
- 1;
2969 STRCPY(pnew
, curr
->y_array
[--j
]);
2970 STRCAT(pnew
, y_current
->y_array
[0]);
2971 vim_free(curr
->y_array
[j
]);
2972 vim_free(y_current
->y_array
[0]);
2973 curr
->y_array
[j
++] = pnew
;
2978 while (y_idx
< y_current
->y_size
)
2979 curr
->y_array
[j
++] = y_current
->y_array
[y_idx
++];
2981 vim_free(y_current
->y_array
);
2984 if (mess
) /* Display message about yank? */
2986 if (yanktype
== MCHAR
2992 /* Some versions of Vi use ">=" here, some don't... */
2993 if (yanklines
> p_report
)
2995 /* redisplay now, so message is not deleted */
2996 update_topline_redraw();
3000 if (oap
->block_mode
)
3001 MSG(_("block of 1 line yanked"));
3004 MSG(_("1 line yanked"));
3007 else if (oap
->block_mode
)
3008 smsg((char_u
*)_("block of %ld lines yanked"), yanklines
);
3011 smsg((char_u
*)_("%ld lines yanked"), yanklines
);
3016 * Set "'[" and "']" marks.
3018 curbuf
->b_op_start
= oap
->start
;
3019 curbuf
->b_op_end
= oap
->end
;
3020 if (yanktype
== MLINE
3026 curbuf
->b_op_start
.col
= 0;
3027 curbuf
->b_op_end
.col
= MAXCOL
;
3030 #ifdef FEAT_CLIPBOARD
3032 * If we were yanking to the '*' register, send result to clipboard.
3033 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3034 * to the '*' register.
3036 if (clip_star
.available
3037 && (curr
== &(y_regs
[STAR_REGISTER
])
3038 || (!deleting
&& oap
->regname
== 0 && clip_unnamed
)))
3040 if (curr
!= &(y_regs
[STAR_REGISTER
]))
3041 /* Copy the text from register 0 to the clipboard register. */
3042 copy_yank_reg(&(y_regs
[STAR_REGISTER
]));
3044 clip_own_selection(&clip_star
);
3045 clip_gen_set_selection(&clip_star
);
3050 * If we were yanking to the '+' register, send result to selection.
3051 * Also copy to the '*' register, in case auto-select is off.
3053 else if (clip_plus
.available
&& curr
== &(y_regs
[PLUS_REGISTER
]))
3055 /* No need to copy to * register upon 'unnamed' now - see below */
3056 clip_own_selection(&clip_plus
);
3057 clip_gen_set_selection(&clip_plus
);
3058 if (!clip_isautosel())
3060 copy_yank_reg(&(y_regs
[STAR_REGISTER
]));
3061 clip_own_selection(&clip_star
);
3062 clip_gen_set_selection(&clip_star
);
3070 fail
: /* free the allocated lines */
3071 free_yank(y_idx
+ 1);
3077 yank_copy_line(bd
, y_idx
)
3078 struct block_def
*bd
;
3083 if ((pnew
= alloc(bd
->startspaces
+ bd
->endspaces
+ bd
->textlen
+ 1))
3086 y_current
->y_array
[y_idx
] = pnew
;
3087 copy_spaces(pnew
, (size_t)bd
->startspaces
);
3088 pnew
+= bd
->startspaces
;
3089 mch_memmove(pnew
, bd
->textstart
, (size_t)bd
->textlen
);
3090 pnew
+= bd
->textlen
;
3091 copy_spaces(pnew
, (size_t)bd
->endspaces
);
3092 pnew
+= bd
->endspaces
;
3097 #ifdef FEAT_CLIPBOARD
3099 * Make a copy of the y_current register to register "reg".
3103 struct yankreg
*reg
;
3105 struct yankreg
*curr
= y_current
;
3111 y_current
->y_array
= (char_u
**)lalloc_clear(
3112 (long_u
)(sizeof(char_u
*) * y_current
->y_size
), TRUE
);
3113 if (y_current
->y_array
== NULL
)
3114 y_current
->y_size
= 0;
3116 for (j
= 0; j
< y_current
->y_size
; ++j
)
3117 if ((y_current
->y_array
[j
] = vim_strsave(curr
->y_array
[j
])) == NULL
)
3120 y_current
->y_size
= 0;
3128 * Put contents of register "regname" into the text.
3129 * Caller must check "regname" to be valid!
3130 * "flags": PUT_FIXINDENT make indent look nice
3131 * PUT_CURSEND leave cursor after end of new text
3132 * PUT_LINE force linewise put (":put")
3135 do_put(regname
, dir
, count
, flags
)
3137 int dir
; /* BACKWARD for 'P', FORWARD for 'p' */
3142 char_u
*newp
, *oldp
;
3144 int totlen
= 0; /* init for gcc */
3147 long i
; /* index in y_array[] */
3157 struct block_def bd
;
3159 char_u
**y_array
= NULL
;
3163 int orig_indent
= 0; /* init for gcc */
3164 int indent_diff
= 0; /* init for gcc */
3165 int first_indent
= TRUE
;
3168 char_u
*insert_string
= NULL
;
3169 int allocated
= FALSE
;
3172 #ifdef FEAT_CLIPBOARD
3173 /* Adjust register name for "unnamed" in 'clipboard'. */
3174 adjust_clip_reg(®name
);
3175 (void)may_get_selection(regname
);
3178 if (flags
& PUT_FIXINDENT
)
3179 orig_indent
= get_indent();
3181 curbuf
->b_op_start
= curwin
->w_cursor
; /* default for '[ mark */
3182 curbuf
->b_op_end
= curwin
->w_cursor
; /* default for '] mark */
3185 * Using inserted text works differently, because the register includes
3186 * special characters (newlines, etc.).
3190 (void)stuff_inserted((dir
== FORWARD
? (count
== -1 ? 'o' : 'a') :
3191 (count
== -1 ? 'O' : 'i')), count
, FALSE
);
3192 /* Putting the text is done later, so can't really move the cursor to
3193 * the next character. Use "l" to simulate it. */
3194 if ((flags
& PUT_CURSEND
) && gchar_cursor() != NUL
)
3195 stuffcharReadbuff('l');
3200 * For special registers '%' (file name), '#' (alternate file name) and
3201 * ':' (last command line), etc. we have to create a fake yank register.
3203 if (get_spec_reg(regname
, &insert_string
, &allocated
, TRUE
))
3205 if (insert_string
== NULL
)
3209 if (insert_string
!= NULL
)
3215 /* For the = register we need to split the string at NL
3217 /* Loop twice: count the number of lines and save them. */
3221 ptr
= insert_string
;
3224 if (y_array
!= NULL
)
3225 y_array
[y_size
] = ptr
;
3227 ptr
= vim_strchr(ptr
, '\n');
3230 if (y_array
!= NULL
)
3233 /* A trailing '\n' makes the string linewise */
3241 if (y_array
!= NULL
)
3243 y_array
= (char_u
**)alloc((unsigned)
3244 (y_size
* sizeof(char_u
*)));
3245 if (y_array
== NULL
)
3252 y_size
= 1; /* use fake one-line yank register */
3253 y_array
= &insert_string
;
3258 get_yank_register(regname
, FALSE
);
3260 y_type
= y_current
->y_type
;
3262 y_width
= y_current
->y_width
;
3264 y_size
= y_current
->y_size
;
3265 y_array
= y_current
->y_array
;
3269 if (y_type
== MLINE
)
3271 if (flags
& PUT_LINE_SPLIT
)
3273 /* "p" or "P" in Visual mode: split the lines to put the text in
3275 if (u_save_cursor() == FAIL
)
3277 ptr
= vim_strsave(ml_get_cursor());
3280 ml_append(curwin
->w_cursor
.lnum
, ptr
, (colnr_T
)0, FALSE
);
3283 ptr
= vim_strnsave(ml_get_curline(), curwin
->w_cursor
.col
);
3286 ml_replace(curwin
->w_cursor
.lnum
, ptr
, FALSE
);
3290 if (flags
& PUT_LINE_FORWARD
)
3292 /* Must be "p" for a Visual block, put lines below the block. */
3293 curwin
->w_cursor
= curbuf
->b_visual
.vi_end
;
3296 curbuf
->b_op_start
= curwin
->w_cursor
; /* default for '[ mark */
3297 curbuf
->b_op_end
= curwin
->w_cursor
; /* default for '] mark */
3301 if (flags
& PUT_LINE
) /* :put command or "p" in Visual line mode. */
3304 if (y_size
== 0 || y_array
== NULL
)
3306 EMSG2(_("E353: Nothing in register %s"),
3307 regname
== 0 ? (char_u
*)"\"" : transchar(regname
));
3312 if (y_type
== MBLOCK
)
3314 lnum
= curwin
->w_cursor
.lnum
+ y_size
+ 1;
3315 if (lnum
> curbuf
->b_ml
.ml_line_count
)
3316 lnum
= curbuf
->b_ml
.ml_line_count
+ 1;
3317 if (u_save(curwin
->w_cursor
.lnum
- 1, lnum
) == FAIL
)
3322 if (y_type
== MLINE
)
3324 lnum
= curwin
->w_cursor
.lnum
;
3326 /* Correct line number for closed fold. Don't move the cursor yet,
3327 * u_save() uses it. */
3328 if (dir
== BACKWARD
)
3329 (void)hasFolding(lnum
, &lnum
, NULL
);
3331 (void)hasFolding(lnum
, NULL
, &lnum
);
3335 if (u_save(lnum
- 1, lnum
) == FAIL
)
3339 curwin
->w_cursor
.lnum
= lnum
- 1;
3341 curwin
->w_cursor
.lnum
= lnum
;
3342 curbuf
->b_op_start
= curwin
->w_cursor
; /* for mark_adjust() */
3345 else if (u_save_cursor() == FAIL
)
3348 yanklen
= (int)STRLEN(y_array
[0]);
3350 #ifdef FEAT_VIRTUALEDIT
3351 if (ve_flags
== VE_ALL
&& y_type
== MCHAR
)
3353 if (gchar_cursor() == TAB
)
3355 /* Don't need to insert spaces when "p" on the last position of a
3356 * tab or "P" on the first position. */
3358 ? (int)curwin
->w_cursor
.coladd
< curbuf
->b_p_ts
- 1
3359 : curwin
->w_cursor
.coladd
> 0)
3360 coladvance_force(getviscol());
3362 curwin
->w_cursor
.coladd
= 0;
3364 else if (curwin
->w_cursor
.coladd
> 0 || gchar_cursor() == NUL
)
3365 coladvance_force(getviscol() + (dir
== FORWARD
));
3369 lnum
= curwin
->w_cursor
.lnum
;
3370 col
= curwin
->w_cursor
.col
;
3376 if (y_type
== MBLOCK
)
3378 char c
= gchar_cursor();
3379 colnr_T endcol2
= 0;
3381 if (dir
== FORWARD
&& c
!= NUL
)
3383 #ifdef FEAT_VIRTUALEDIT
3384 if (ve_flags
== VE_ALL
)
3385 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, &endcol2
);
3388 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
3392 /* move to start of next multi-byte character */
3393 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
3396 #ifdef FEAT_VIRTUALEDIT
3397 if (c
!= TAB
|| ve_flags
!= VE_ALL
)
3399 ++curwin
->w_cursor
.col
;
3403 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, &endcol2
);
3405 #ifdef FEAT_VIRTUALEDIT
3406 col
+= curwin
->w_cursor
.coladd
;
3407 if (ve_flags
== VE_ALL
3408 && (curwin
->w_cursor
.coladd
> 0
3409 || endcol2
== curwin
->w_cursor
.col
))
3411 if (dir
== FORWARD
&& c
== NUL
)
3413 if (dir
!= FORWARD
&& c
!= NUL
)
3414 ++curwin
->w_cursor
.col
;
3417 if (dir
== BACKWARD
&& curwin
->w_cursor
.col
)
3418 curwin
->w_cursor
.col
--;
3419 if (dir
== FORWARD
&& col
- 1 == endcol2
)
3420 curwin
->w_cursor
.col
++;
3423 curwin
->w_cursor
.coladd
= 0;
3426 for (i
= 0; i
< y_size
; ++i
)
3436 /* add a new line */
3437 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
3439 if (ml_append(curbuf
->b_ml
.ml_line_count
, (char_u
*)"",
3440 (colnr_T
)1, FALSE
) == FAIL
)
3444 /* get the old line and advance to the position to insert at */
3445 oldp
= ml_get_curline();
3446 oldlen
= (int)STRLEN(oldp
);
3447 for (ptr
= oldp
; vcol
< col
&& *ptr
; )
3449 /* Count a tab for what it's worth (if list mode not on) */
3450 incr
= lbr_chartabsize_adv(&ptr
, (colnr_T
)vcol
);
3453 bd
.textcol
= (colnr_T
)(ptr
- oldp
);
3455 shortline
= (vcol
< col
) || (vcol
== col
&& !*ptr
) ;
3457 if (vcol
< col
) /* line too short, padd with spaces */
3458 bd
.startspaces
= col
- vcol
;
3459 else if (vcol
> col
)
3461 bd
.endspaces
= vcol
- col
;
3462 bd
.startspaces
= incr
- bd
.endspaces
;
3467 bd
.textcol
-= (*mb_head_off
)(oldp
, oldp
+ bd
.textcol
);
3469 if (oldp
[bd
.textcol
] != TAB
)
3471 /* Only a Tab can be split into spaces. Other
3472 * characters will have to be moved to after the
3473 * block, causing misalignment. */
3479 yanklen
= (int)STRLEN(y_array
[i
]);
3481 /* calculate number of spaces required to fill right side of block*/
3482 spaces
= y_width
+ 1;
3483 for (j
= 0; j
< yanklen
; j
++)
3484 spaces
-= lbr_chartabsize(&y_array
[i
][j
], 0);
3488 /* insert the new text */
3489 totlen
= count
* (yanklen
+ spaces
) + bd
.startspaces
+ bd
.endspaces
;
3490 newp
= alloc_check((unsigned)totlen
+ oldlen
+ 1);
3493 /* copy part up to cursor to new line */
3495 mch_memmove(ptr
, oldp
, (size_t)bd
.textcol
);
3497 /* may insert some spaces before the new text */
3498 copy_spaces(ptr
, (size_t)bd
.startspaces
);
3499 ptr
+= bd
.startspaces
;
3500 /* insert the new text */
3501 for (j
= 0; j
< count
; ++j
)
3503 mch_memmove(ptr
, y_array
[i
], (size_t)yanklen
);
3506 /* insert block's trailing spaces only if there's text behind */
3507 if ((j
< count
- 1 || !shortline
) && spaces
)
3509 copy_spaces(ptr
, (size_t)spaces
);
3513 /* may insert some spaces after the new text */
3514 copy_spaces(ptr
, (size_t)bd
.endspaces
);
3515 ptr
+= bd
.endspaces
;
3516 /* move the text after the cursor to the end of the line. */
3517 mch_memmove(ptr
, oldp
+ bd
.textcol
+ delcount
,
3518 (size_t)(oldlen
- bd
.textcol
- delcount
+ 1));
3519 ml_replace(curwin
->w_cursor
.lnum
, newp
, FALSE
);
3521 ++curwin
->w_cursor
.lnum
;
3523 curwin
->w_cursor
.col
+= bd
.startspaces
;
3526 changed_lines(lnum
, 0, curwin
->w_cursor
.lnum
, nr_lines
);
3529 curbuf
->b_op_start
= curwin
->w_cursor
;
3530 curbuf
->b_op_start
.lnum
= lnum
;
3532 /* adjust '] mark */
3533 curbuf
->b_op_end
.lnum
= curwin
->w_cursor
.lnum
- 1;
3534 curbuf
->b_op_end
.col
= bd
.textcol
+ totlen
- 1;
3535 # ifdef FEAT_VIRTUALEDIT
3536 curbuf
->b_op_end
.coladd
= 0;
3538 if (flags
& PUT_CURSEND
)
3542 curwin
->w_cursor
= curbuf
->b_op_end
;
3543 curwin
->w_cursor
.col
++;
3545 /* in Insert mode we might be after the NUL, correct for that */
3546 len
= (colnr_T
)STRLEN(ml_get_curline());
3547 if (curwin
->w_cursor
.col
> len
)
3548 curwin
->w_cursor
.col
= len
;
3551 curwin
->w_cursor
.lnum
= lnum
;
3557 * Character or Line mode
3559 if (y_type
== MCHAR
)
3561 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3563 if (dir
== FORWARD
&& gchar_cursor() != NUL
)
3568 int bytelen
= (*mb_ptr2len
)(ml_get_cursor());
3570 /* put it on the next of the multi-byte character. */
3574 curwin
->w_cursor
.col
+= bytelen
;
3575 curbuf
->b_op_end
.col
+= bytelen
;
3584 ++curwin
->w_cursor
.col
;
3585 ++curbuf
->b_op_end
.col
;
3589 curbuf
->b_op_start
= curwin
->w_cursor
;
3592 * Line mode: BACKWARD is the same as FORWARD on the previous line
3594 else if (dir
== BACKWARD
)
3596 new_cursor
= curwin
->w_cursor
;
3599 * simple case: insert into current line
3601 if (y_type
== MCHAR
&& y_size
== 1)
3603 totlen
= count
* yanklen
;
3606 oldp
= ml_get(lnum
);
3607 newp
= alloc_check((unsigned)(STRLEN(oldp
) + totlen
+ 1));
3609 goto end
; /* alloc() will give error message */
3610 mch_memmove(newp
, oldp
, (size_t)col
);
3612 for (i
= 0; i
< count
; ++i
)
3614 mch_memmove(ptr
, y_array
[0], (size_t)yanklen
);
3617 mch_memmove(ptr
, oldp
+ col
, STRLEN(oldp
+ col
) + 1);
3618 ml_replace(lnum
, newp
, FALSE
);
3619 /* Put cursor on last putted char. */
3620 curwin
->w_cursor
.col
+= (colnr_T
)(totlen
- 1);
3622 curbuf
->b_op_end
= curwin
->w_cursor
;
3623 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3624 if (totlen
&& (restart_edit
!= 0 || (flags
& PUT_CURSEND
)))
3625 ++curwin
->w_cursor
.col
;
3626 changed_bytes(lnum
, col
);
3631 * Insert at least one line. When y_type is MCHAR, break the first
3634 for (cnt
= 1; cnt
<= count
; ++cnt
)
3637 if (y_type
== MCHAR
)
3640 * Split the current line in two at the insert position.
3641 * First insert y_array[size - 1] in front of second line.
3642 * Then append y_array[0] to first line.
3644 lnum
= new_cursor
.lnum
;
3645 ptr
= ml_get(lnum
) + col
;
3646 totlen
= (int)STRLEN(y_array
[y_size
- 1]);
3647 newp
= alloc_check((unsigned)(STRLEN(ptr
) + totlen
+ 1));
3650 STRCPY(newp
, y_array
[y_size
- 1]);
3652 /* insert second line */
3653 ml_append(lnum
, newp
, (colnr_T
)0, FALSE
);
3656 oldp
= ml_get(lnum
);
3657 newp
= alloc_check((unsigned)(col
+ yanklen
+ 1));
3660 /* copy first part of line */
3661 mch_memmove(newp
, oldp
, (size_t)col
);
3662 /* append to first line */
3663 mch_memmove(newp
+ col
, y_array
[0], (size_t)(yanklen
+ 1));
3664 ml_replace(lnum
, newp
, FALSE
);
3666 curwin
->w_cursor
.lnum
= lnum
;
3670 for (; i
< y_size
; ++i
)
3672 if ((y_type
!= MCHAR
|| i
< y_size
- 1)
3673 && ml_append(lnum
, y_array
[i
], (colnr_T
)0, FALSE
)
3678 if (flags
& PUT_FIXINDENT
)
3680 old_pos
= curwin
->w_cursor
;
3681 curwin
->w_cursor
.lnum
= lnum
;
3683 if (cnt
== count
&& i
== y_size
- 1)
3684 lendiff
= (int)STRLEN(ptr
);
3685 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3686 if (*ptr
== '#' && preprocs_left())
3687 indent
= 0; /* Leave # lines at start */
3691 indent
= 0; /* Ignore empty lines */
3692 else if (first_indent
)
3694 indent_diff
= orig_indent
- get_indent();
3695 indent
= orig_indent
;
3696 first_indent
= FALSE
;
3698 else if ((indent
= get_indent() + indent_diff
) < 0)
3700 (void)set_indent(indent
, 0);
3701 curwin
->w_cursor
= old_pos
;
3702 /* remember how many chars were removed */
3703 if (cnt
== count
&& i
== y_size
- 1)
3704 lendiff
-= (int)STRLEN(ml_get(lnum
));
3711 if (y_type
== MLINE
)
3713 curbuf
->b_op_start
.col
= 0;
3715 curbuf
->b_op_start
.lnum
++;
3717 mark_adjust(curbuf
->b_op_start
.lnum
+ (y_type
== MCHAR
),
3718 (linenr_T
)MAXLNUM
, nr_lines
, 0L);
3720 /* note changed text for displaying and folding */
3721 if (y_type
== MCHAR
)
3722 changed_lines(curwin
->w_cursor
.lnum
, col
,
3723 curwin
->w_cursor
.lnum
+ 1, nr_lines
);
3725 changed_lines(curbuf
->b_op_start
.lnum
, 0,
3726 curbuf
->b_op_start
.lnum
, nr_lines
);
3728 /* put '] mark at last inserted character */
3729 curbuf
->b_op_end
.lnum
= lnum
;
3730 /* correct length for change in indent */
3731 col
= (colnr_T
)STRLEN(y_array
[y_size
- 1]) - lendiff
;
3733 curbuf
->b_op_end
.col
= col
- 1;
3735 curbuf
->b_op_end
.col
= 0;
3737 if (flags
& PUT_CURSLINE
)
3739 /* ":put": put cursor on last inserted line */
3740 curwin
->w_cursor
.lnum
= lnum
;
3741 beginline(BL_WHITE
| BL_FIX
);
3743 else if (flags
& PUT_CURSEND
)
3745 /* put cursor after inserted text */
3746 if (y_type
== MLINE
)
3748 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
3749 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
3751 curwin
->w_cursor
.lnum
= lnum
+ 1;
3752 curwin
->w_cursor
.col
= 0;
3756 curwin
->w_cursor
.lnum
= lnum
;
3757 curwin
->w_cursor
.col
= col
;
3760 else if (y_type
== MLINE
)
3762 /* put cursor on first non-blank in first inserted line */
3763 curwin
->w_cursor
.col
= 0;
3765 ++curwin
->w_cursor
.lnum
;
3766 beginline(BL_WHITE
| BL_FIX
);
3768 else /* put cursor on first inserted character */
3769 curwin
->w_cursor
= new_cursor
;
3774 curwin
->w_set_curswant
= TRUE
;
3778 vim_free(insert_string
);
3782 /* If the cursor is past the end of the line put it at the end. */
3783 adjust_cursor_eol();
3787 * When the cursor is on the NUL past the end of the line and it should not be
3788 * there move it left.
3793 if (curwin
->w_cursor
.col
> 0
3794 && gchar_cursor() == NUL
3795 #ifdef FEAT_VIRTUALEDIT
3796 && (ve_flags
& VE_ONEMORE
) == 0
3798 && !(restart_edit
|| (State
& INSERT
)))
3800 /* Put the cursor on the last character in the line. */
3803 #ifdef FEAT_VIRTUALEDIT
3804 if (ve_flags
== VE_ALL
)
3808 /* Coladd is set to the width of the last character. */
3809 getvcol(curwin
, &curwin
->w_cursor
, &scol
, NULL
, &ecol
);
3810 curwin
->w_cursor
.coladd
= ecol
- scol
+ 1;
3816 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3818 * Return TRUE if lines starting with '#' should be left aligned.
3824 # ifdef FEAT_SMARTINDENT
3825 # ifdef FEAT_CINDENT
3826 (curbuf
->b_p_si
&& !curbuf
->b_p_cin
) ||
3831 # ifdef FEAT_CINDENT
3832 (curbuf
->b_p_cin
&& in_cinkeys('#', ' ', TRUE
))
3838 /* Return the character name of the register with the given number */
3840 get_register_name(num
)
3847 else if (num
== DELETION_REGISTER
)
3849 #ifdef FEAT_CLIPBOARD
3850 else if (num
== STAR_REGISTER
)
3852 else if (num
== PLUS_REGISTER
)
3860 /* EBCDIC is really braindead ... */
3861 i
= 'a' + (num
- 10);
3868 return num
+ 'a' - 10;
3874 * ":dis" and ":registers": Display the contents of the yank registers.
3886 char_u
*arg
= eap
->arg
;
3893 if (arg
!= NULL
&& *arg
== NUL
)
3895 attr
= hl_attr(HLF_8
);
3897 /* Highlight title */
3898 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3899 for (i
= -1; i
< NUM_REGISTERS
&& !got_int
; ++i
)
3901 name
= get_register_name(i
);
3902 if (arg
!= NULL
&& vim_strchr(arg
, name
) == NULL
)
3903 continue; /* did not ask for this register */
3905 #ifdef FEAT_CLIPBOARD
3906 /* Adjust register name for "unnamed" in 'clipboard'.
3907 * When it's a clipboard register, fill it with the current contents
3908 * of the clipboard. */
3909 adjust_clip_reg(&name
);
3910 (void)may_get_selection(name
);
3915 if (y_previous
!= NULL
)
3922 if (yb
->y_array
!= NULL
)
3929 n
= (int)Columns
- 6;
3930 for (j
= 0; j
< yb
->y_size
&& n
> 1; ++j
)
3934 MSG_PUTS_ATTR("^J", attr
);
3937 for (p
= yb
->y_array
[j
]; *p
&& (n
-= ptr2cells(p
)) >= 0; ++p
)
3940 clen
= (*mb_ptr2len
)(p
);
3942 msg_outtrans_len(p
, clen
);
3948 if (n
> 1 && yb
->y_type
== MLINE
)
3949 MSG_PUTS_ATTR("^J", attr
);
3950 out_flush(); /* show one line at a time */
3956 * display last inserted text
3958 if ((p
= get_last_insert()) != NULL
3959 && (arg
== NULL
|| vim_strchr(arg
, '.') != NULL
) && !got_int
)
3966 * display last command line
3968 if (last_cmdline
!= NULL
&& (arg
== NULL
|| vim_strchr(arg
, ':') != NULL
)
3972 dis_msg(last_cmdline
, FALSE
);
3976 * display current file name
3978 if (curbuf
->b_fname
!= NULL
3979 && (arg
== NULL
|| vim_strchr(arg
, '%') != NULL
) && !got_int
)
3982 dis_msg(curbuf
->b_fname
, FALSE
);
3986 * display alternate file name
3988 if ((arg
== NULL
|| vim_strchr(arg
, '%') != NULL
) && !got_int
)
3993 if (buflist_name_nr(0, &fname
, &dummy
) != FAIL
)
3996 dis_msg(fname
, FALSE
);
4001 * display last search pattern
4003 if (last_search_pat() != NULL
4004 && (arg
== NULL
|| vim_strchr(arg
, '/') != NULL
) && !got_int
)
4007 dis_msg(last_search_pat(), FALSE
);
4012 * display last used expression
4014 if (expr_line
!= NULL
&& (arg
== NULL
|| vim_strchr(arg
, '=') != NULL
)
4018 dis_msg(expr_line
, FALSE
);
4024 * display a string for do_dis()
4025 * truncate at end of screen line
4028 dis_msg(p
, skip_esc
)
4030 int skip_esc
; /* if TRUE, ignore trailing ESC */
4037 n
= (int)Columns
- 6;
4039 && !(*p
== ESC
&& skip_esc
&& *(p
+ 1) == NUL
)
4040 && (n
-= ptr2cells(p
)) >= 0)
4043 if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
4045 msg_outtrans_len(p
, l
);
4050 msg_outtrans_len(p
++, 1);
4056 * join 'count' lines (minimal 2), including u_save()
4059 do_do_join(count
, insert_space
)
4063 colnr_T col
= MAXCOL
;
4065 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
- 1),
4066 (linenr_T
)(curwin
->w_cursor
.lnum
+ count
)) == FAIL
)
4072 if (got_int
|| do_join(insert_space
) == FAIL
)
4077 if (col
== MAXCOL
&& vim_strchr(p_cpo
, CPO_JOINCOL
) != NULL
)
4078 col
= curwin
->w_cursor
.col
;
4081 /* Vi compatible: use the column of the first join */
4082 if (col
!= MAXCOL
&& vim_strchr(p_cpo
, CPO_JOINCOL
) != NULL
)
4083 curwin
->w_cursor
.col
= col
;
4087 * Need to update the screen if the line where the cursor is became too
4088 * long to fit on the screen.
4090 update_topline_redraw();
4095 * Join two lines at the cursor position.
4096 * "redraw" is TRUE when the screen should be updated.
4097 * Caller must have setup for undo.
4099 * return FAIL for failure, OK otherwise
4102 do_join(insert_space
)
4106 char_u
*next
, *next_start
;
4108 int endcurr1
, endcurr2
;
4109 int currsize
; /* size of the current line */
4110 int nextsize
; /* size of the next line */
4111 int spaces
; /* number of spaces to insert */
4114 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4115 return FAIL
; /* can't join on last line */
4117 curr
= ml_get_curline();
4118 currsize
= (int)STRLEN(curr
);
4119 endcurr1
= endcurr2
= NUL
;
4120 if (insert_space
&& currsize
> 0)
4125 next
= curr
+ currsize
;
4126 mb_ptr_back(curr
, next
);
4127 endcurr1
= (*mb_ptr2char
)(next
);
4130 mb_ptr_back(curr
, next
);
4131 endcurr2
= (*mb_ptr2char
)(next
);
4137 endcurr1
= *(curr
+ currsize
- 1);
4139 endcurr2
= *(curr
+ currsize
- 2);
4143 next
= next_start
= ml_get((linenr_T
)(curwin
->w_cursor
.lnum
+ 1));
4147 next
= skipwhite(next
);
4148 if (*next
!= ')' && currsize
!= 0 && endcurr1
!= TAB
4150 && (!has_format_option(FO_MBYTE_JOIN
)
4151 || (mb_ptr2char(next
) < 0x100 && endcurr1
< 0x100))
4152 && (!has_format_option(FO_MBYTE_JOIN2
)
4153 || mb_ptr2char(next
) < 0x100 || endcurr1
< 0x100)
4157 /* don't add a space if the line is ending in a space */
4158 if (endcurr1
== ' ')
4159 endcurr1
= endcurr2
;
4162 /* extra space when 'joinspaces' set and line ends in '.' */
4165 || (vim_strchr(p_cpo
, CPO_JOINSP
) == NULL
4166 && (endcurr1
== '?' || endcurr1
== '!'))))
4170 nextsize
= (int)STRLEN(next
);
4172 newp
= alloc_check((unsigned)(currsize
+ nextsize
+ spaces
+ 1));
4177 * Insert the next line first, because we already have that pointer.
4178 * Curr has to be obtained again, because getting next will have
4181 mch_memmove(newp
+ currsize
+ spaces
, next
, (size_t)(nextsize
+ 1));
4183 curr
= ml_get_curline();
4184 mch_memmove(newp
, curr
, (size_t)currsize
);
4186 copy_spaces(newp
+ currsize
, (size_t)spaces
);
4188 ml_replace(curwin
->w_cursor
.lnum
, newp
, FALSE
);
4190 /* Only report the change in the first line here, del_lines() will report
4191 * the deleted line. */
4192 changed_lines(curwin
->w_cursor
.lnum
, currsize
,
4193 curwin
->w_cursor
.lnum
+ 1, 0L);
4196 * Delete the following line. To do this we move the cursor there
4197 * briefly, and then move it back. After del_lines() the cursor may
4198 * have moved up (last line deleted), so the current lnum is kept in t.
4200 * Move marks from the deleted line to the joined line, adjusting the
4201 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4202 * should not really be a problem.
4204 t
= curwin
->w_cursor
.lnum
;
4205 mark_col_adjust(t
+ 1, (colnr_T
)0, (linenr_T
)-1,
4206 (long)(currsize
+ spaces
- (next
- next_start
)));
4207 ++curwin
->w_cursor
.lnum
;
4208 del_lines(1L, FALSE
);
4209 curwin
->w_cursor
.lnum
= t
;
4212 * go to first character of the joined line
4214 curwin
->w_cursor
.col
= currsize
;
4216 #ifdef FEAT_VIRTUALEDIT
4217 curwin
->w_cursor
.coladd
= 0;
4219 curwin
->w_set_curswant
= TRUE
;
4224 #ifdef FEAT_COMMENTS
4226 * Return TRUE if the two comment leaders given are the same. "lnum" is
4227 * the first line. White-space is ignored. Note that the whole of
4228 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4231 same_leader(lnum
, leader1_len
, leader1_flags
, leader2_len
, leader2_flags
)
4234 char_u
*leader1_flags
;
4236 char_u
*leader2_flags
;
4238 int idx1
= 0, idx2
= 0;
4243 if (leader1_len
== 0)
4244 return (leader2_len
== 0);
4247 * If first leader has 'f' flag, the lines can be joined only if the
4248 * second line does not have a leader.
4249 * If first leader has 'e' flag, the lines can never be joined.
4250 * If fist leader has 's' flag, the lines can only be joined if there is
4251 * some text after it and the second line has the 'm' flag.
4253 if (leader1_flags
!= NULL
)
4255 for (p
= leader1_flags
; *p
&& *p
!= ':'; ++p
)
4257 if (*p
== COM_FIRST
)
4258 return (leader2_len
== 0);
4261 if (*p
== COM_START
)
4263 if (*(ml_get(lnum
) + leader1_len
) == NUL
)
4265 if (leader2_flags
== NULL
|| leader2_len
== 0)
4267 for (p
= leader2_flags
; *p
&& *p
!= ':'; ++p
)
4268 if (*p
== COM_MIDDLE
)
4276 * Get current line and next line, compare the leaders.
4277 * The first line has to be saved, only one line can be locked at a time.
4279 line1
= vim_strsave(ml_get(lnum
));
4282 for (idx1
= 0; vim_iswhite(line1
[idx1
]); ++idx1
)
4284 line2
= ml_get(lnum
+ 1);
4285 for (idx2
= 0; idx2
< leader2_len
; ++idx2
)
4287 if (!vim_iswhite(line2
[idx2
]))
4289 if (line1
[idx1
++] != line2
[idx2
])
4293 while (vim_iswhite(line1
[idx1
]))
4298 return (idx2
== leader2_len
&& idx1
== leader1_len
);
4303 * implementation of the format operator 'gq'
4306 op_format(oap
, keep_cursor
)
4308 int keep_cursor
; /* keep cursor on same text char */
4310 long old_line_count
= curbuf
->b_ml
.ml_line_count
;
4312 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4313 * can put it back there. */
4314 curwin
->w_cursor
= oap
->cursor_start
;
4316 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
4317 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
4319 curwin
->w_cursor
= oap
->start
;
4323 /* When there is no change: need to remove the Visual selection */
4324 redraw_curbuf_later(INVERTED
);
4327 /* Set '[ mark at the start of the formatted area */
4328 curbuf
->b_op_start
= oap
->start
;
4330 /* For "gw" remember the cursor position and put it back below (adjusted
4331 * for joined and split lines). */
4333 saved_cursor
= oap
->cursor_start
;
4335 format_lines(oap
->line_count
);
4338 * Leave the cursor at the first non-blank of the last formatted line.
4339 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4340 * line, so "." will do the next lines.
4342 if (oap
->end_adjusted
&& curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
4343 ++curwin
->w_cursor
.lnum
;
4344 beginline(BL_WHITE
| BL_FIX
);
4345 old_line_count
= curbuf
->b_ml
.ml_line_count
- old_line_count
;
4346 msgmore(old_line_count
);
4348 /* put '] mark on the end of the formatted area */
4349 curbuf
->b_op_end
= curwin
->w_cursor
;
4353 curwin
->w_cursor
= saved_cursor
;
4354 saved_cursor
.lnum
= 0;
4364 if (wp
->w_old_cursor_lnum
!= 0)
4366 /* When lines have been inserted or deleted, adjust the end of
4367 * the Visual area to be redrawn. */
4368 if (wp
->w_old_cursor_lnum
> wp
->w_old_visual_lnum
)
4369 wp
->w_old_cursor_lnum
+= old_line_count
;
4371 wp
->w_old_visual_lnum
+= old_line_count
;
4378 #if defined(FEAT_EVAL) || defined(PROTO)
4380 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4388 /* When there is no change: need to remove the Visual selection */
4389 redraw_curbuf_later(INVERTED
);
4392 (void)fex_format(oap
->start
.lnum
, oap
->line_count
, NUL
);
4396 fex_format(lnum
, count
, c
)
4399 int c
; /* character to be inserted */
4401 int use_sandbox
= was_set_insecurely((char_u
*)"formatexpr",
4405 char_u buf
[MB_MAXBYTES
];
4411 * Set v:lnum to the first line number and v:count to the number of lines.
4412 * Set v:char to the character to be inserted (can be NUL).
4414 set_vim_var_nr(VV_LNUM
, lnum
);
4415 set_vim_var_nr(VV_COUNT
, count
);
4419 buf
[(*mb_char2bytes
)(c
, buf
)] = NUL
;
4426 set_vim_var_string(VV_CHAR
, buf
, -1);
4429 * Evaluate the function.
4433 r
= eval_to_number(curbuf
->b_p_fex
);
4437 set_vim_var_string(VV_CHAR
, NULL
, -1);
4444 * Format "line_count" lines, starting at the cursor position.
4445 * When "line_count" is negative, format until the end of the paragraph.
4446 * Lines after the cursor line are saved for undo, caller must have saved the
4450 format_lines(line_count
)
4451 linenr_T line_count
;
4454 int is_not_par
; /* current line not part of parag. */
4455 int next_is_not_par
; /* next line not part of paragraph */
4456 int is_end_par
; /* at end of paragraph */
4457 int prev_is_end_par
= FALSE
;/* prev. line not part of parag. */
4458 int next_is_start_par
= FALSE
;
4459 #ifdef FEAT_COMMENTS
4460 int leader_len
= 0; /* leader len of current line */
4461 int next_leader_len
; /* leader len of next line */
4462 char_u
*leader_flags
= NULL
; /* flags for leader of current line */
4463 char_u
*next_leader_flags
; /* flags for leader of next line */
4464 int do_comments
; /* format comments */
4467 int second_indent
= -1;
4468 int do_second_indent
;
4469 int do_number_indent
;
4471 int first_par_line
= TRUE
;
4474 int need_set_indent
= TRUE
; /* set indent of next paragraph */
4475 int force_format
= FALSE
;
4476 int old_State
= State
;
4478 /* length of a line to force formatting: 3 * 'tw' */
4479 max_len
= comp_textwidth(TRUE
) * 3;
4481 /* check for 'q', '2' and '1' in 'formatoptions' */
4482 #ifdef FEAT_COMMENTS
4483 do_comments
= has_format_option(FO_Q_COMS
);
4485 do_second_indent
= has_format_option(FO_Q_SECOND
);
4486 do_number_indent
= has_format_option(FO_Q_NUMBER
);
4487 do_trail_white
= has_format_option(FO_WHITE_PAR
);
4490 * Get info about the previous and current line.
4492 if (curwin
->w_cursor
.lnum
> 1)
4493 is_not_par
= fmt_check_par(curwin
->w_cursor
.lnum
- 1
4494 #ifdef FEAT_COMMENTS
4495 , &leader_len
, &leader_flags
, do_comments
4500 next_is_not_par
= fmt_check_par(curwin
->w_cursor
.lnum
4501 #ifdef FEAT_COMMENTS
4502 , &next_leader_len
, &next_leader_flags
, do_comments
4505 is_end_par
= (is_not_par
|| next_is_not_par
);
4506 if (!is_end_par
&& do_trail_white
)
4507 is_end_par
= !ends_in_white(curwin
->w_cursor
.lnum
- 1);
4509 curwin
->w_cursor
.lnum
--;
4510 for (count
= line_count
; count
!= 0 && !got_int
; --count
)
4513 * Advance to next paragraph.
4517 curwin
->w_cursor
.lnum
++;
4518 prev_is_end_par
= is_end_par
;
4519 is_not_par
= next_is_not_par
;
4520 #ifdef FEAT_COMMENTS
4521 leader_len
= next_leader_len
;
4522 leader_flags
= next_leader_flags
;
4527 * The last line to be formatted.
4529 if (count
== 1 || curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4531 next_is_not_par
= TRUE
;
4532 #ifdef FEAT_COMMENTS
4533 next_leader_len
= 0;
4534 next_leader_flags
= NULL
;
4539 next_is_not_par
= fmt_check_par(curwin
->w_cursor
.lnum
+ 1
4540 #ifdef FEAT_COMMENTS
4541 , &next_leader_len
, &next_leader_flags
, do_comments
4544 if (do_number_indent
)
4546 (get_number_indent(curwin
->w_cursor
.lnum
+ 1) > 0);
4549 is_end_par
= (is_not_par
|| next_is_not_par
|| next_is_start_par
);
4550 if (!is_end_par
&& do_trail_white
)
4551 is_end_par
= !ends_in_white(curwin
->w_cursor
.lnum
);
4554 * Skip lines that are not in a paragraph.
4564 * For the first line of a paragraph, check indent of second line.
4565 * Don't do this for comments and empty lines.
4568 && (do_second_indent
|| do_number_indent
)
4570 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
4571 #ifdef FEAT_COMMENTS
4573 && next_leader_len
== 0
4577 if (do_second_indent
4578 && !lineempty(curwin
->w_cursor
.lnum
+ 1))
4579 second_indent
= get_indent_lnum(curwin
->w_cursor
.lnum
+ 1);
4580 else if (do_number_indent
)
4581 second_indent
= get_number_indent(curwin
->w_cursor
.lnum
);
4585 * When the comment leader changes, it's the end of the paragraph.
4587 if (curwin
->w_cursor
.lnum
>= curbuf
->b_ml
.ml_line_count
4588 #ifdef FEAT_COMMENTS
4589 || !same_leader(curwin
->w_cursor
.lnum
,
4590 leader_len
, leader_flags
,
4591 next_leader_len
, next_leader_flags
)
4597 * If we have got to the end of a paragraph, or the line is
4598 * getting long, format it.
4600 if (is_end_par
|| force_format
)
4602 if (need_set_indent
)
4603 /* replace indent in first line with minimal number of
4604 * tabs and spaces, according to current options */
4605 (void)set_indent(get_indent(), SIN_CHANGED
);
4607 /* put cursor on last non-space */
4608 State
= NORMAL
; /* don't go past end-of-line */
4609 coladvance((colnr_T
)MAXCOL
);
4610 while (curwin
->w_cursor
.col
&& vim_isspace(gchar_cursor()))
4613 /* do the formatting, without 'showmode' */
4614 State
= INSERT
; /* for open_line() */
4617 insertchar(NUL
, INSCHAR_FORMAT
4618 #ifdef FEAT_COMMENTS
4619 + (do_comments
? INSCHAR_DO_COM
: 0)
4625 /* at end of par.: need to set indent of next par. */
4626 need_set_indent
= is_end_par
;
4629 /* When called with a negative line count, break at the
4630 * end of the paragraph. */
4633 first_par_line
= TRUE
;
4635 force_format
= FALSE
;
4639 * When still in same paragraph, join the lines together. But
4640 * first delete the comment leader from the second line.
4645 curwin
->w_cursor
.lnum
++;
4646 curwin
->w_cursor
.col
= 0;
4647 if (line_count
< 0 && u_save_cursor() == FAIL
)
4649 #ifdef FEAT_COMMENTS
4650 (void)del_bytes((long)next_leader_len
, FALSE
, FALSE
);
4651 if (next_leader_len
> 0)
4652 mark_col_adjust(curwin
->w_cursor
.lnum
, (colnr_T
)0, 0L,
4653 (long)-next_leader_len
);
4655 curwin
->w_cursor
.lnum
--;
4656 if (do_join(TRUE
) == FAIL
)
4661 first_par_line
= FALSE
;
4662 /* If the line is getting long, format it next time */
4663 if (STRLEN(ml_get_curline()) > (size_t)max_len
)
4664 force_format
= TRUE
;
4666 force_format
= FALSE
;
4674 * Return TRUE if line "lnum" ends in a white character.
4680 char_u
*s
= ml_get(lnum
);
4685 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4686 * invocation may call function multiple times". */
4688 return vim_iswhite(s
[l
]);
4692 * Blank lines, and lines containing only the comment leader, are left
4693 * untouched by the formatting. The function returns TRUE in this
4694 * case. It also returns TRUE when a line starts with the end of a comment
4695 * ('e' in comment flags), so that this line is skipped, and not joined to the
4696 * previous line. A new paragraph starts after a blank line, or when the
4697 * comment leader changes -- webb.
4699 #ifdef FEAT_COMMENTS
4701 fmt_check_par(lnum
, leader_len
, leader_flags
, do_comments
)
4704 char_u
**leader_flags
;
4707 char_u
*flags
= NULL
; /* init for GCC */
4712 *leader_len
= get_leader_len(ptr
, leader_flags
, FALSE
);
4716 if (*leader_len
> 0)
4719 * Search for 'e' flag in comment leader flags.
4721 flags
= *leader_flags
;
4722 while (*flags
&& *flags
!= ':' && *flags
!= COM_END
)
4726 return (*skipwhite(ptr
+ *leader_len
) == NUL
4727 || (*leader_len
> 0 && *flags
== COM_END
)
4728 || startPS(lnum
, NUL
, FALSE
));
4735 return (*skipwhite(ml_get(lnum
)) == NUL
|| startPS(lnum
, NUL
, FALSE
));
4740 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4741 * previous line is in the same paragraph. Used for auto-formatting.
4744 paragraph_start(lnum
)
4748 #ifdef FEAT_COMMENTS
4749 int leader_len
= 0; /* leader len of current line */
4750 char_u
*leader_flags
= NULL
; /* flags for leader of current line */
4751 int next_leader_len
; /* leader len of next line */
4752 char_u
*next_leader_flags
; /* flags for leader of next line */
4753 int do_comments
; /* format comments */
4757 return TRUE
; /* start of the file */
4759 p
= ml_get(lnum
- 1);
4761 return TRUE
; /* after empty line */
4763 #ifdef FEAT_COMMENTS
4764 do_comments
= has_format_option(FO_Q_COMS
);
4766 if (fmt_check_par(lnum
- 1
4767 #ifdef FEAT_COMMENTS
4768 , &leader_len
, &leader_flags
, do_comments
4771 return TRUE
; /* after non-paragraph line */
4773 if (fmt_check_par(lnum
4774 #ifdef FEAT_COMMENTS
4775 , &next_leader_len
, &next_leader_flags
, do_comments
4778 return TRUE
; /* "lnum" is not a paragraph line */
4780 if (has_format_option(FO_WHITE_PAR
) && !ends_in_white(lnum
- 1))
4781 return TRUE
; /* missing trailing space in previous line. */
4783 if (has_format_option(FO_Q_NUMBER
) && (get_number_indent(lnum
) > 0))
4784 return TRUE
; /* numbered item starts in "lnum". */
4786 #ifdef FEAT_COMMENTS
4787 if (!same_leader(lnum
- 1, leader_len
, leader_flags
,
4788 next_leader_len
, next_leader_flags
))
4789 return TRUE
; /* change of comment leader. */
4797 * prepare a few things for block mode yank/delete/tilde
4800 * - textlen includes the first/last char to be (partly) deleted
4801 * - start/endspaces is the number of columns that are taken by the
4802 * first/last deleted char minus the number of columns that have to be
4803 * deleted. for yank and tilde:
4804 * - textlen includes the first/last char to be wholly yanked
4805 * - start/endspaces is the number of columns of the first/last yanked char
4806 * that are to be yanked.
4809 block_prep(oap
, bdp
, lnum
, is_del
)
4811 struct block_def
*bdp
;
4819 char_u
*prev_pstart
;
4822 bdp
->startspaces
= 0;
4825 bdp
->start_vcol
= 0;
4827 #ifdef FEAT_VISUALEXTRA
4828 bdp
->is_short
= FALSE
;
4829 bdp
->is_oneChar
= FALSE
;
4830 bdp
->pre_whitesp
= 0;
4831 bdp
->pre_whitesp_c
= 0;
4832 bdp
->end_char_vcols
= 0;
4834 bdp
->start_char_vcols
= 0;
4836 line
= ml_get(lnum
);
4839 while (bdp
->start_vcol
< oap
->start_vcol
&& *pstart
)
4841 /* Count a tab for what it's worth (if list mode not on) */
4842 incr
= lbr_chartabsize(pstart
, (colnr_T
)bdp
->start_vcol
);
4843 bdp
->start_vcol
+= incr
;
4844 #ifdef FEAT_VISUALEXTRA
4845 if (vim_iswhite(*pstart
))
4847 bdp
->pre_whitesp
+= incr
;
4848 bdp
->pre_whitesp_c
++;
4852 bdp
->pre_whitesp
= 0;
4853 bdp
->pre_whitesp_c
= 0;
4856 prev_pstart
= pstart
;
4859 bdp
->start_char_vcols
= incr
;
4860 if (bdp
->start_vcol
< oap
->start_vcol
) /* line too short */
4862 bdp
->end_vcol
= bdp
->start_vcol
;
4863 #ifdef FEAT_VISUALEXTRA
4864 bdp
->is_short
= TRUE
;
4866 if (!is_del
|| oap
->op_type
== OP_APPEND
)
4867 bdp
->endspaces
= oap
->end_vcol
- oap
->start_vcol
+ 1;
4871 /* notice: this converts partly selected Multibyte characters to
4873 bdp
->startspaces
= bdp
->start_vcol
- oap
->start_vcol
;
4874 if (is_del
&& bdp
->startspaces
)
4875 bdp
->startspaces
= bdp
->start_char_vcols
- bdp
->startspaces
;
4877 bdp
->end_vcol
= bdp
->start_vcol
;
4878 if (bdp
->end_vcol
> oap
->end_vcol
) /* it's all in one character */
4880 #ifdef FEAT_VISUALEXTRA
4881 bdp
->is_oneChar
= TRUE
;
4883 if (oap
->op_type
== OP_INSERT
)
4884 bdp
->endspaces
= bdp
->start_char_vcols
- bdp
->startspaces
;
4885 else if (oap
->op_type
== OP_APPEND
)
4887 bdp
->startspaces
+= oap
->end_vcol
- oap
->start_vcol
+ 1;
4888 bdp
->endspaces
= bdp
->start_char_vcols
- bdp
->startspaces
;
4892 bdp
->startspaces
= oap
->end_vcol
- oap
->start_vcol
+ 1;
4893 if (is_del
&& oap
->op_type
!= OP_LSHIFT
)
4895 /* just putting the sum of those two into
4896 * bdp->startspaces doesn't work for Visual replace,
4897 * so we have to split the tab in two */
4898 bdp
->startspaces
= bdp
->start_char_vcols
4899 - (bdp
->start_vcol
- oap
->start_vcol
);
4900 bdp
->endspaces
= bdp
->end_vcol
- oap
->end_vcol
- 1;
4907 while (bdp
->end_vcol
<= oap
->end_vcol
&& *pend
!= NUL
)
4909 /* Count a tab for what it's worth (if list mode not on) */
4911 incr
= lbr_chartabsize_adv(&pend
, (colnr_T
)bdp
->end_vcol
);
4912 bdp
->end_vcol
+= incr
;
4914 if (bdp
->end_vcol
<= oap
->end_vcol
4916 || oap
->op_type
== OP_APPEND
4917 || oap
->op_type
== OP_REPLACE
)) /* line too short */
4919 #ifdef FEAT_VISUALEXTRA
4920 bdp
->is_short
= TRUE
;
4922 /* Alternative: include spaces to fill up the block.
4923 * Disadvantage: can lead to trailing spaces when the line is
4924 * short where the text is put */
4925 /* if (!is_del || oap->op_type == OP_APPEND) */
4926 if (oap
->op_type
== OP_APPEND
|| virtual_op
)
4927 bdp
->endspaces
= oap
->end_vcol
- bdp
->end_vcol
4930 bdp
->endspaces
= 0; /* replace doesn't add characters */
4932 else if (bdp
->end_vcol
> oap
->end_vcol
)
4934 bdp
->endspaces
= bdp
->end_vcol
- oap
->end_vcol
- 1;
4935 if (!is_del
&& bdp
->endspaces
)
4937 bdp
->endspaces
= incr
- bdp
->endspaces
;
4943 #ifdef FEAT_VISUALEXTRA
4944 bdp
->end_char_vcols
= incr
;
4946 if (is_del
&& bdp
->startspaces
)
4947 pstart
= prev_pstart
;
4948 bdp
->textlen
= (int)(pend
- pstart
);
4950 bdp
->textcol
= (colnr_T
) (pstart
- line
);
4951 bdp
->textstart
= pstart
;
4953 #endif /* FEAT_VISUAL */
4955 #ifdef FEAT_RIGHTLEFT
4956 static void reverse_line
__ARGS((char_u
*s
));
4965 if ((i
= (int)STRLEN(s
) - 1) <= 0)
4968 curwin
->w_cursor
.col
= i
- curwin
->w_cursor
.col
;
4969 for (j
= 0; j
< i
; j
++, i
--)
4971 c
= s
[i
]; s
[i
] = s
[j
]; s
[j
] = c
;
4975 # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4977 # define RLADDSUBFIX(ptr)
4981 * add or subtract 'Prenum1' from a number in a line
4982 * 'command' is CTRL-A for add, CTRL-X for subtract
4984 * return FAIL for failure, OK otherwise
4987 do_addsub(command
, Prenum1
)
4993 char_u buf2
[NUMBUFLEN
];
4994 int hex
; /* 'X' or 'x': hex; '0': octal */
4995 static int hexupper
= FALSE
; /* 0xABC */
5000 int length
= 0; /* character length of the number */
5009 dohex
= (vim_strchr(curbuf
->b_p_nf
, 'x') != NULL
); /* "heX" */
5010 dooct
= (vim_strchr(curbuf
->b_p_nf
, 'o') != NULL
); /* "Octal" */
5011 doalp
= (vim_strchr(curbuf
->b_p_nf
, 'p') != NULL
); /* "alPha" */
5013 ptr
= ml_get_curline();
5017 * First check if we are on a hexadecimal number, after the "0x".
5019 col
= curwin
->w_cursor
.col
;
5021 while (col
> 0 && vim_isxdigit(ptr
[col
]))
5027 && ptr
[col
- 1] == '0'
5028 && vim_isxdigit(ptr
[col
+ 1]))
5031 * Found hexadecimal number, move to its start.
5038 * Search forward and then backward to find the start of number.
5040 col
= curwin
->w_cursor
.col
;
5042 while (ptr
[col
] != NUL
5043 && !vim_isdigit(ptr
[col
])
5044 && !(doalp
&& ASCII_ISALPHA(ptr
[col
])))
5048 && vim_isdigit(ptr
[col
- 1])
5049 && !(doalp
&& ASCII_ISALPHA(ptr
[col
])))
5054 * If a number was found, and saving for undo works, replace the number.
5056 firstdigit
= ptr
[col
];
5058 if ((!VIM_ISDIGIT(firstdigit
) && !(doalp
&& ASCII_ISALPHA(firstdigit
)))
5059 || u_save_cursor() != OK
)
5065 /* get ptr again, because u_save() may have changed it */
5066 ptr
= ml_get_curline();
5069 if (doalp
&& ASCII_ISALPHA(firstdigit
))
5071 /* decrement or increment alphabetic character */
5072 if (command
== Ctrl_X
)
5074 if (CharOrd(firstdigit
) < Prenum1
)
5076 if (isupper(firstdigit
))
5083 firstdigit
= EBCDIC_CHAR_ADD(firstdigit
, -Prenum1
);
5085 firstdigit
-= Prenum1
;
5090 if (26 - CharOrd(firstdigit
) - 1 < Prenum1
)
5092 if (isupper(firstdigit
))
5099 firstdigit
= EBCDIC_CHAR_ADD(firstdigit
, Prenum1
);
5101 firstdigit
+= Prenum1
;
5104 curwin
->w_cursor
.col
= col
;
5105 (void)del_char(FALSE
);
5106 ins_char(firstdigit
);
5111 if (col
> 0 && ptr
[col
- 1] == '-') /* negative number */
5117 /* get the number value (unsigned) */
5118 vim_str2nr(ptr
+ col
, &hex
, &length
, dooct
, dohex
, NULL
, &n
);
5120 /* ignore leading '-' for hex and octal numbers */
5121 if (hex
&& negative
)
5128 /* add or subtract */
5130 if (command
== Ctrl_X
)
5137 n
-= (unsigned long)Prenum1
;
5139 n
+= (unsigned long)Prenum1
;
5141 /* handle wraparound for decimal numbers */
5148 n
= 1 + (n
^ (unsigned long)-1);
5156 n
= (n
^ (unsigned long)-1);
5165 * Delete the old number.
5167 curwin
->w_cursor
.col
= col
;
5171 * Don't include the '-' in the length, only the length of the part
5172 * after it is kept the same.
5178 if (c
< 0x100 && isalpha(c
))
5185 /* del_char() will mark line needing displaying */
5186 (void)del_char(FALSE
);
5191 * Prepare the leading characters in buf1[].
5192 * When there are many leading zeros it could be very long. Allocate
5195 buf1
= alloc((unsigned)length
+ NUMBUFLEN
);
5208 if (hex
== 'x' || hex
== 'X')
5215 * Put the number characters in buf2[].
5218 sprintf((char *)buf2
, "%lu", n
);
5219 else if (hex
== '0')
5220 sprintf((char *)buf2
, "%lo", n
);
5221 else if (hex
&& hexupper
)
5222 sprintf((char *)buf2
, "%lX", n
);
5224 sprintf((char *)buf2
, "%lx", n
);
5225 length
-= (int)STRLEN(buf2
);
5228 * Adjust number of zeros to the new number of digits, so the
5229 * total length of the number remains the same.
5230 * Don't do this when
5231 * the result may look like an octal number.
5233 if (firstdigit
== '0' && !(dooct
&& hex
== 0))
5234 while (length
-- > 0)
5238 ins_str(buf1
); /* insert the new number */
5241 --curwin
->w_cursor
.col
;
5242 curwin
->w_set_curswant
= TRUE
;
5243 #ifdef FEAT_RIGHTLEFT
5244 ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
, TRUE
);
5252 read_viminfo_register(virp
, force
)
5261 int set_prev
= FALSE
;
5263 char_u
**array
= NULL
;
5265 /* We only get here (hopefully) if line[0] == '"' */
5266 str
= virp
->vir_line
+ 1;
5272 if (!ASCII_ISALNUM(*str
) && *str
!= '-')
5274 if (viminfo_error("E577: ", _("Illegal register name"), virp
->vir_line
))
5275 return TRUE
; /* too many errors, pretend end-of-file */
5278 get_yank_register(*str
++, FALSE
);
5279 if (!force
&& y_current
->y_array
!= NULL
)
5282 limit
= 100; /* Optimized for registers containing <= 100 lines */
5286 y_previous
= y_current
;
5287 vim_free(y_current
->y_array
);
5288 array
= y_current
->y_array
=
5289 (char_u
**)alloc((unsigned)(limit
* sizeof(char_u
*)));
5290 str
= skipwhite(str
);
5291 if (STRNCMP(str
, "CHAR", 4) == 0)
5292 y_current
->y_type
= MCHAR
;
5294 else if (STRNCMP(str
, "BLOCK", 5) == 0)
5295 y_current
->y_type
= MBLOCK
;
5298 y_current
->y_type
= MLINE
;
5299 /* get the block width; if it's missing we get a zero, which is OK */
5300 str
= skipwhite(skiptowhite(str
));
5302 y_current
->y_width
= getdigits(&str
);
5304 (void)getdigits(&str
);
5308 while (!(eof
= viminfo_readline(virp
))
5309 && (virp
->vir_line
[0] == TAB
|| virp
->vir_line
[0] == '<'))
5315 y_current
->y_array
= (char_u
**)
5316 alloc((unsigned)(limit
* 2 * sizeof(char_u
*)));
5317 for (i
= 0; i
< limit
; i
++)
5318 y_current
->y_array
[i
] = array
[i
];
5321 array
= y_current
->y_array
;
5323 str
= viminfo_readstring(virp
, 1, TRUE
);
5325 array
[size
++] = str
;
5335 y_current
->y_array
= NULL
;
5337 else if (size
< limit
)
5339 y_current
->y_array
=
5340 (char_u
**)alloc((unsigned)(size
* sizeof(char_u
*)));
5341 for (i
= 0; i
< size
; i
++)
5342 y_current
->y_array
[i
] = array
[i
];
5345 y_current
->y_size
= size
;
5351 write_viminfo_registers(fp
)
5362 fprintf(fp
, _("\n# Registers:\n"));
5364 /* Get '<' value, use old '"' value if '<' is not found. */
5365 max_num_lines
= get_viminfo_parameter('<');
5366 if (max_num_lines
< 0)
5367 max_num_lines
= get_viminfo_parameter('"');
5368 if (max_num_lines
== 0)
5370 max_kbyte
= get_viminfo_parameter('s');
5373 for (i
= 0; i
< NUM_REGISTERS
; i
++)
5375 if (y_regs
[i
].y_array
== NULL
)
5377 #ifdef FEAT_CLIPBOARD
5378 /* Skip '*'/'+' register, we don't want them back next time */
5379 if (i
== STAR_REGISTER
|| i
== PLUS_REGISTER
)
5383 /* Neither do we want the '~' register */
5384 if (i
== TILDE_REGISTER
)
5387 /* Skip empty registers. */
5388 num_lines
= y_regs
[i
].y_size
;
5390 || (num_lines
== 1 && y_regs
[i
].y_type
== MCHAR
5391 && STRLEN(y_regs
[i
].y_array
[0]) == 0))
5396 /* Skip register if there is more text than the maximum size. */
5398 for (j
= 0; j
< num_lines
; j
++)
5399 len
+= (long)STRLEN(y_regs
[i
].y_array
[j
]) + 1L;
5400 if (len
> (long)max_kbyte
* 1024L)
5404 switch (y_regs
[i
].y_type
)
5407 type
= (char_u
*)"LINE";
5410 type
= (char_u
*)"CHAR";
5414 type
= (char_u
*)"BLOCK";
5418 sprintf((char *)IObuff
, _("E574: Unknown register type %d"),
5421 type
= (char_u
*)"LINE";
5424 if (y_previous
== &y_regs
[i
])
5426 c
= get_register_name(i
);
5427 fprintf(fp
, "\"%c\t%s\t%d\n", c
, type
,
5429 (int)y_regs
[i
].y_width
5435 /* If max_num_lines < 0, then we save ALL the lines in the register */
5436 if (max_num_lines
> 0 && num_lines
> max_num_lines
)
5437 num_lines
= max_num_lines
;
5438 for (j
= 0; j
< num_lines
; j
++)
5441 viminfo_writestring(fp
, y_regs
[i
].y_array
[j
]);
5445 #endif /* FEAT_VIMINFO */
5447 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
5449 * SELECTION / PRIMARY ('*')
5451 * Text selection stuff that uses the GUI selection register '*'. When using a
5452 * GUI this may be text from another window, otherwise it is the last text we
5453 * had highlighted with VIsual mode. With mouse support, clicking the middle
5454 * button performs the paste, otherwise you will need to do <"*p>. "
5455 * If not under X, it is synonymous with the clipboard register '+'.
5459 * Text selection stuff that uses the GUI clipboard register '+'.
5460 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5461 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5462 * otherwise you will need to do <"+p>. "
5463 * If not under X, it is synonymous with the selection register '*'.
5467 * Routine to export any final X selection we had to the environment
5468 * so that the text is still available after vim has exited. X selections
5469 * only exist while the owning application exists, so we write to the
5470 * permanent (while X runs) store CUT_BUFFER0.
5471 * Dump the CLIPBOARD selection if we own it (it's logically the more
5472 * 'permanent' of the two), otherwise the PRIMARY one.
5473 * For now, use a hard-coded sanity limit of 1Mb of data.
5475 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5477 x11_export_final_selection()
5482 int motion_type
= -1;
5489 # ifdef FEAT_XCLIPBOARD
5495 /* Get selection to export */
5496 if (clip_plus
.owned
)
5497 motion_type
= clip_convert_selection(&str
, &len
, &clip_plus
);
5498 else if (clip_star
.owned
)
5499 motion_type
= clip_convert_selection(&str
, &len
, &clip_star
);
5502 if (dpy
!= NULL
&& str
!= NULL
&& motion_type
>= 0
5503 && len
< 1024*1024 && len
> 0)
5505 XStoreBuffer(dpy
, (char *)str
, (int)len
, 0);
5514 clip_free_selection(cbd
)
5517 struct yankreg
*y_ptr
= y_current
;
5519 if (cbd
== &clip_plus
)
5520 y_current
= &y_regs
[PLUS_REGISTER
];
5522 y_current
= &y_regs
[STAR_REGISTER
];
5524 y_current
->y_size
= 0;
5529 * Get the selected text and put it in the gui selection register '*' or '+'.
5532 clip_get_selection(cbd
)
5535 struct yankreg
*old_y_previous
, *old_y_current
;
5539 int old_visual_mode
;
5541 colnr_T old_curswant
;
5542 int old_set_curswant
;
5543 pos_T old_op_start
, old_op_end
;
5549 if ((cbd
== &clip_plus
&& y_regs
[PLUS_REGISTER
].y_array
!= NULL
)
5550 || (cbd
== &clip_star
&& y_regs
[STAR_REGISTER
].y_array
!= NULL
))
5553 /* Get the text between clip_star.start & clip_star.end */
5554 old_y_previous
= y_previous
;
5555 old_y_current
= y_current
;
5556 old_cursor
= curwin
->w_cursor
;
5557 old_curswant
= curwin
->w_curswant
;
5558 old_set_curswant
= curwin
->w_set_curswant
;
5559 old_op_start
= curbuf
->b_op_start
;
5560 old_op_end
= curbuf
->b_op_end
;
5562 old_visual
= VIsual
;
5563 old_visual_mode
= VIsual_mode
;
5566 oa
.regname
= (cbd
== &clip_plus
? '+' : '*');
5567 oa
.op_type
= OP_YANK
;
5568 vim_memset(&ca
, 0, sizeof(ca
));
5572 ca
.retval
= CA_NO_ADJ_OP_END
;
5573 do_pending_operator(&ca
, 0, TRUE
);
5574 y_previous
= old_y_previous
;
5575 y_current
= old_y_current
;
5576 curwin
->w_cursor
= old_cursor
;
5577 changed_cline_bef_curs(); /* need to update w_virtcol et al */
5578 curwin
->w_curswant
= old_curswant
;
5579 curwin
->w_set_curswant
= old_set_curswant
;
5580 curbuf
->b_op_start
= old_op_start
;
5581 curbuf
->b_op_end
= old_op_end
;
5583 VIsual
= old_visual
;
5584 VIsual_mode
= old_visual_mode
;
5589 clip_free_selection(cbd
);
5591 /* Try to get selected text from another window */
5592 clip_gen_request_selection(cbd
);
5596 /* Convert from the GUI selection string into the '*'/'+' register */
5598 clip_yank_selection(type
, str
, len
, cbd
)
5604 struct yankreg
*y_ptr
;
5606 if (cbd
== &clip_plus
)
5607 y_ptr
= &y_regs
[PLUS_REGISTER
];
5609 y_ptr
= &y_regs
[STAR_REGISTER
];
5611 clip_free_selection(cbd
);
5613 str_to_reg(y_ptr
, type
, str
, len
, 0L);
5617 * Convert the '*'/'+' register into a GUI selection string returned in *str
5619 * Returns the motion type, or -1 for failure.
5622 clip_convert_selection(str
, len
, cbd
)
5631 struct yankreg
*y_ptr
;
5633 if (cbd
== &clip_plus
)
5634 y_ptr
= &y_regs
[PLUS_REGISTER
];
5636 y_ptr
= &y_regs
[STAR_REGISTER
];
5646 if (y_ptr
->y_array
== NULL
)
5649 for (i
= 0; i
< y_ptr
->y_size
; i
++)
5650 *len
+= (long_u
)STRLEN(y_ptr
->y_array
[i
]) + eolsize
;
5653 * Don't want newline character at end of last line if we're in MCHAR mode.
5655 if (y_ptr
->y_type
== MCHAR
&& *len
>= eolsize
)
5658 p
= *str
= lalloc(*len
+ 1, TRUE
); /* add one to avoid zero */
5662 for (i
= 0, j
= 0; i
< (int)*len
; i
++, j
++)
5664 if (y_ptr
->y_array
[lnum
][j
] == '\n')
5666 else if (y_ptr
->y_array
[lnum
][j
] == NUL
)
5680 p
[i
] = y_ptr
->y_array
[lnum
][j
];
5682 return y_ptr
->y_type
;
5686 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5688 * If we have written to a clipboard register, send the text to the clipboard.
5693 if (y_current
== &(y_regs
[STAR_REGISTER
]) && clip_star
.available
)
5695 clip_own_selection(&clip_star
);
5696 clip_gen_set_selection(&clip_star
);
5698 else if (y_current
== &(y_regs
[PLUS_REGISTER
]) && clip_plus
.available
)
5700 clip_own_selection(&clip_plus
);
5701 clip_gen_set_selection(&clip_plus
);
5706 #endif /* FEAT_CLIPBOARD || PROTO */
5709 #if defined(FEAT_DND) || defined(PROTO)
5711 * Replace the contents of the '~' register with str.
5714 dnd_yank_drag_data(str
, len
)
5718 struct yankreg
*curr
;
5721 y_current
= &y_regs
[TILDE_REGISTER
];
5723 str_to_reg(y_current
, MCHAR
, str
, len
, 0L);
5729 #if defined(FEAT_EVAL) || defined(PROTO)
5731 * Return the type of a register.
5732 * Used for getregtype()
5733 * Returns MAUTO for error.
5736 get_reg_type(regname
, reglen
)
5742 case '%': /* file name */
5743 case '#': /* alternate file name */
5744 case '=': /* expression */
5745 case ':': /* last command line */
5746 case '/': /* last search-pattern */
5747 case '.': /* last inserted text */
5748 #ifdef FEAT_SEARCHPATH
5749 case Ctrl_F
: /* Filename under cursor */
5750 case Ctrl_P
: /* Path under cursor, expand via "path" */
5752 case Ctrl_W
: /* word under cursor */
5753 case Ctrl_A
: /* WORD (mnemonic All) under cursor */
5754 case '_': /* black hole: always empty */
5758 #ifdef FEAT_CLIPBOARD
5759 regname
= may_get_selection(regname
);
5762 /* Should we check for a valid name? */
5763 get_yank_register(regname
, FALSE
);
5765 if (y_current
->y_array
!= NULL
)
5768 if (reglen
!= NULL
&& y_current
->y_type
== MBLOCK
)
5769 *reglen
= y_current
->y_width
;
5771 return y_current
->y_type
;
5777 * Return the contents of a register as a single allocated string.
5778 * Used for "@r" in expressions and for getreg().
5779 * Returns NULL for error.
5782 get_reg_contents(regname
, allowexpr
, expr_src
)
5784 int allowexpr
; /* allow "=" register */
5785 int expr_src
; /* get expression for "=" register */
5792 /* Don't allow using an expression register inside an expression */
5798 return get_expr_line_src();
5799 return get_expr_line();
5804 if (regname
== '@') /* "@@" is used for unnamed register */
5807 /* check for valid regname */
5808 if (regname
!= NUL
&& !valid_yank_reg(regname
, FALSE
))
5811 #ifdef FEAT_CLIPBOARD
5812 regname
= may_get_selection(regname
);
5815 if (get_spec_reg(regname
, &retval
, &allocated
, FALSE
))
5820 retval
= vim_strsave(retval
);
5824 get_yank_register(regname
, FALSE
);
5825 if (y_current
->y_array
== NULL
)
5829 * Compute length of resulting string.
5832 for (i
= 0; i
< y_current
->y_size
; ++i
)
5834 len
+= (long)STRLEN(y_current
->y_array
[i
]);
5836 * Insert a newline between lines and after last line if
5839 if (y_current
->y_type
== MLINE
|| i
< y_current
->y_size
- 1)
5843 retval
= lalloc(len
+ 1, TRUE
);
5846 * Copy the lines of the yank register into the string.
5851 for (i
= 0; i
< y_current
->y_size
; ++i
)
5853 STRCPY(retval
+ len
, y_current
->y_array
[i
]);
5854 len
+= (long)STRLEN(retval
+ len
);
5857 * Insert a NL between lines and after the last line if y_type is
5860 if (y_current
->y_type
== MLINE
|| i
< y_current
->y_size
- 1)
5861 retval
[len
++] = '\n';
5870 * Store string "str" in register "name".
5871 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5872 * If "must_append" is TRUE, always append to the register. Otherwise append
5873 * if "name" is an uppercase letter.
5874 * Note: "maxlen" and "must_append" don't work for the "/" register.
5875 * Careful: 'str' is modified, you may have to use a copy!
5876 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5879 write_reg_contents(name
, str
, maxlen
, must_append
)
5885 write_reg_contents_ex(name
, str
, maxlen
, must_append
, MAUTO
, 0L);
5889 write_reg_contents_ex(name
, str
, maxlen
, must_append
, yank_type
, block_len
)
5897 struct yankreg
*old_y_previous
, *old_y_current
;
5903 len
= (long)STRLEN(str
);
5905 /* Special case: '/' search pattern */
5908 set_last_search_pat(str
, RE_SEARCH
, TRUE
, TRUE
);
5917 p
= vim_strnsave(str
, (int)len
);
5922 s
= concat_str(get_expr_line_src(), p
);
5932 if (!valid_yank_reg(name
, TRUE
)) /* check for valid reg name */
5938 if (name
== '_') /* black hole: nothing to do */
5941 /* Don't want to change the current (unnamed) register */
5942 old_y_previous
= y_previous
;
5943 old_y_current
= y_current
;
5945 get_yank_register(name
, TRUE
);
5946 if (!y_append
&& !must_append
)
5949 /* Just in case - make sure we don't use MBLOCK */
5950 if (yank_type
== MBLOCK
)
5953 if (yank_type
== MAUTO
)
5954 yank_type
= ((len
> 0 && (str
[len
- 1] == '\n' || str
[len
- 1] == '\r'))
5956 str_to_reg(y_current
, yank_type
, str
, len
, block_len
);
5958 # ifdef FEAT_CLIPBOARD
5959 /* Send text of clipboard register to the clipboard. */
5960 may_set_selection();
5963 /* ':let @" = "val"' should change the meaning of the "" register */
5965 y_previous
= old_y_previous
;
5966 y_current
= old_y_current
;
5968 #endif /* FEAT_EVAL */
5970 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5972 * Put a string into a register. When the register is not empty, the string
5976 str_to_reg(y_ptr
, type
, str
, len
, blocklen
)
5977 struct yankreg
*y_ptr
; /* pointer to yank register */
5978 int type
; /* MCHAR, MLINE or MBLOCK */
5979 char_u
*str
; /* string to put in register */
5980 long len
; /* length of string */
5981 long blocklen
; /* width of Visual block */
5987 int newlines
; /* number of lines added */
5988 int extraline
= 0; /* extra line at the end */
5989 int append
= FALSE
; /* append to last line in register */
5996 if (y_ptr
->y_array
== NULL
) /* NULL means emtpy register */
6000 * Count the number of lines within the string
6003 for (i
= 0; i
< len
; i
++)
6006 if (type
== MCHAR
|| len
== 0 || str
[len
- 1] != '\n')
6009 ++newlines
; /* count extra newline at the end */
6011 if (y_ptr
->y_size
> 0 && y_ptr
->y_type
== MCHAR
)
6014 --newlines
; /* uncount newline when appending first line */
6018 * Allocate an array to hold the pointers to the new register lines.
6019 * If the register was not empty, move the existing lines to the new array.
6021 pp
= (char_u
**)lalloc_clear((y_ptr
->y_size
+ newlines
)
6022 * sizeof(char_u
*), TRUE
);
6023 if (pp
== NULL
) /* out of memory */
6025 for (lnum
= 0; lnum
< y_ptr
->y_size
; ++lnum
)
6026 pp
[lnum
] = y_ptr
->y_array
[lnum
];
6027 vim_free(y_ptr
->y_array
);
6028 y_ptr
->y_array
= pp
;
6034 * Find the end of each line and save it into the array.
6036 for (start
= 0; start
< len
+ extraline
; start
+= i
+ 1)
6038 for (i
= start
; i
< len
; ++i
) /* find the end of the line */
6041 i
-= start
; /* i is now length of line */
6049 extra
= (int)STRLEN(y_ptr
->y_array
[lnum
]);
6053 s
= alloc((unsigned)(i
+ extra
+ 1));
6057 mch_memmove(s
, y_ptr
->y_array
[lnum
], (size_t)extra
);
6059 vim_free(y_ptr
->y_array
[lnum
]);
6061 mch_memmove(s
+ extra
, str
+ start
, (size_t)i
);
6064 y_ptr
->y_array
[lnum
++] = s
;
6065 while (--extra
>= 0)
6068 *s
= '\n'; /* replace NUL with newline */
6071 append
= FALSE
; /* only first line is appended */
6073 y_ptr
->y_type
= type
;
6074 y_ptr
->y_size
= lnum
;
6077 y_ptr
->y_width
= (blocklen
< 0 ? maxlen
- 1 : blocklen
);
6082 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6088 vim_memset(oap
, 0, sizeof(oparg_T
));
6091 static long line_count_info
__ARGS((char_u
*line
, long *wc
, long *cc
, long limit
, int eol_size
));
6094 * Count the number of bytes, characters and "words" in a line.
6096 * "Words" are counted by looking for boundaries between non-space and
6097 * space characters. (it seems to produce results that match 'wc'.)
6099 * Return value is byte count; word count for the line is added to "*wc".
6100 * Char count is added to "*cc".
6102 * The function will only examine the first "limit" characters in the
6103 * line, stopping if it encounters an end-of-line (NUL byte). In that
6104 * case, eol_size will be added to the character count to account for
6105 * the size of the EOL character.
6108 line_count_info(line
, wc
, cc
, limit
, eol_size
)
6120 for (i
= 0; line
[i
] && i
< limit
; )
6124 if (vim_isspace(line
[i
]))
6130 else if (!vim_isspace(line
[i
]))
6134 i
+= (*mb_ptr2len
)(line
+ i
);
6144 /* Add eol_size if the end of line was reached before hitting limit. */
6145 if (line
[i
] == NUL
&& i
< limit
)
6155 * Give some info about the position of the cursor (for "g CTRL-G").
6156 * In Visual mode, give some info about the selected region. (In this case,
6157 * the *_count_cursor variables store running totals for the selection.)
6166 long byte_count
= 0;
6167 long byte_count_cursor
= 0;
6168 long char_count
= 0;
6169 long char_count_cursor
= 0;
6170 long word_count
= 0;
6171 long word_count_cursor
= 0;
6173 long last_check
= 100000L;
6175 long line_count_selected
= 0;
6176 pos_T min_pos
, max_pos
;
6178 struct block_def bd
;
6182 * Compute the length of the file in characters.
6184 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
6186 MSG(_(no_lines_msg
));
6190 if (get_fileformat(curbuf
) == EOL_DOS
)
6198 if (lt(VIsual
, curwin
->w_cursor
))
6201 max_pos
= curwin
->w_cursor
;
6205 min_pos
= curwin
->w_cursor
;
6208 if (*p_sel
== 'e' && max_pos
.col
> 0)
6211 if (VIsual_mode
== Ctrl_V
)
6213 oparg
.is_VIsual
= 1;
6214 oparg
.block_mode
= TRUE
;
6215 oparg
.op_type
= OP_NOP
;
6216 getvcols(curwin
, &min_pos
, &max_pos
,
6217 &oparg
.start_vcol
, &oparg
.end_vcol
);
6218 if (curwin
->w_curswant
== MAXCOL
)
6219 oparg
.end_vcol
= MAXCOL
;
6220 /* Swap the start, end vcol if needed */
6221 if (oparg
.end_vcol
< oparg
.start_vcol
)
6223 oparg
.end_vcol
+= oparg
.start_vcol
;
6224 oparg
.start_vcol
= oparg
.end_vcol
- oparg
.start_vcol
;
6225 oparg
.end_vcol
-= oparg
.start_vcol
;
6228 line_count_selected
= max_pos
.lnum
- min_pos
.lnum
+ 1;
6232 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
; ++lnum
)
6234 /* Check for a CTRL-C every 100000 characters. */
6235 if (byte_count
> last_check
)
6240 last_check
= byte_count
+ 100000L;
6244 /* Do extra processing for VIsual mode. */
6246 && lnum
>= min_pos
.lnum
&& lnum
<= max_pos
.lnum
)
6251 switch (VIsual_mode
)
6254 # ifdef FEAT_VIRTUALEDIT
6255 virtual_op
= virtual_active();
6257 block_prep(&oparg
, &bd
, lnum
, 0);
6258 # ifdef FEAT_VIRTUALEDIT
6262 len
= (long)bd
.textlen
;
6270 colnr_T start_col
= (lnum
== min_pos
.lnum
)
6272 colnr_T end_col
= (lnum
== max_pos
.lnum
)
6273 ? max_pos
.col
- start_col
+ 1 : MAXCOL
;
6275 s
= ml_get(lnum
) + start_col
;
6282 byte_count_cursor
+= line_count_info(s
, &word_count_cursor
,
6283 &char_count_cursor
, len
, eol_size
);
6284 if (lnum
== curbuf
->b_ml
.ml_line_count
6287 && (long)STRLEN(s
) < len
)
6288 byte_count_cursor
-= eol_size
;
6294 /* In non-visual mode, check for the line the cursor is on */
6295 if (lnum
== curwin
->w_cursor
.lnum
)
6297 word_count_cursor
+= word_count
;
6298 char_count_cursor
+= char_count
;
6299 byte_count_cursor
= byte_count
+
6300 line_count_info(ml_get(lnum
),
6301 &word_count_cursor
, &char_count_cursor
,
6302 (long)(curwin
->w_cursor
.col
+ 1), eol_size
);
6305 /* Add to the running totals */
6306 byte_count
+= line_count_info(ml_get(lnum
), &word_count
,
6307 &char_count
, (long)MAXCOL
, eol_size
);
6310 /* Correction for when last line doesn't have an EOL. */
6311 if (!curbuf
->b_p_eol
&& curbuf
->b_p_bin
)
6312 byte_count
-= eol_size
;
6317 if (VIsual_mode
== Ctrl_V
&& curwin
->w_curswant
< MAXCOL
)
6319 getvcols(curwin
, &min_pos
, &max_pos
, &min_pos
.col
,
6321 sprintf((char *)buf1
, _("%ld Cols; "),
6322 (long)(oparg
.end_vcol
- oparg
.start_vcol
+ 1));
6327 if (char_count_cursor
== byte_count_cursor
6328 && char_count
== byte_count
)
6329 sprintf((char *)IObuff
, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
6330 buf1
, line_count_selected
,
6331 (long)curbuf
->b_ml
.ml_line_count
,
6332 word_count_cursor
, word_count
,
6333 byte_count_cursor
, byte_count
);
6335 sprintf((char *)IObuff
, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6336 buf1
, line_count_selected
,
6337 (long)curbuf
->b_ml
.ml_line_count
,
6338 word_count_cursor
, word_count
,
6339 char_count_cursor
, char_count
,
6340 byte_count_cursor
, byte_count
);
6345 p
= ml_get_curline();
6347 col_print(buf1
, (int)curwin
->w_cursor
.col
+ 1,
6348 (int)curwin
->w_virtcol
+ 1);
6349 col_print(buf2
, (int)STRLEN(p
), linetabsize(p
));
6351 if (char_count_cursor
== byte_count_cursor
6352 && char_count
== byte_count
)
6353 sprintf((char *)IObuff
, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
6354 (char *)buf1
, (char *)buf2
,
6355 (long)curwin
->w_cursor
.lnum
,
6356 (long)curbuf
->b_ml
.ml_line_count
,
6357 word_count_cursor
, word_count
,
6358 byte_count_cursor
, byte_count
);
6360 sprintf((char *)IObuff
, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6361 (char *)buf1
, (char *)buf2
,
6362 (long)curwin
->w_cursor
.lnum
,
6363 (long)curbuf
->b_ml
.ml_line_count
,
6364 word_count_cursor
, word_count
,
6365 char_count_cursor
, char_count
,
6366 byte_count_cursor
, byte_count
);
6370 byte_count
= bomb_size();
6372 sprintf((char *)IObuff
+ STRLEN(IObuff
), _("(+%ld for BOM)"),
6375 /* Don't shorten this message, the user asked for it. */
6377 p_shm
= (char_u
*)"";