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