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