blob: 2f9ff00c47c083dd723df347d922f4ff8d4d1f72 [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 Moolenaar2c7a29c2005-12-12 22:02:31 +0000162 {'g', 'y', 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
1846 (void)del_bytes((long)n, restart_edit == NUL && !virtual_op);
1847 }
1848 else /* delete characters between lines */
1849 {
1850 pos_T curpos;
1851
1852 /* save deleted and changed lines for undo */
1853 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1854 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1855 return FAIL;
1856
1857 truncate_line(TRUE); /* delete from cursor to end of line */
1858
1859 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1860 ++curwin->w_cursor.lnum;
1861 del_lines((long)(oap->line_count - 2), FALSE);
1862
1863 /* delete from start of line until op_end */
1864 curwin->w_cursor.col = 0;
1865 (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
1866 restart_edit == NUL && !virtual_op);
1867 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1868
1869 (void)do_join(FALSE);
1870 }
1871 }
1872
1873 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1874
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001875#ifdef FEAT_VIRTUALEDIT
1876setmarks:
1877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878#ifdef FEAT_VISUAL
1879 if (oap->block_mode)
1880 {
1881 curbuf->b_op_end.lnum = oap->end.lnum;
1882 curbuf->b_op_end.col = oap->start.col;
1883 }
1884 else
1885#endif
1886 curbuf->b_op_end = oap->start;
1887 curbuf->b_op_start = oap->start;
1888
1889 return OK;
1890}
1891
1892#ifdef FEAT_MBYTE
1893/*
1894 * Adjust end of operating area for ending on a multi-byte character.
1895 * Used for deletion.
1896 */
1897 static void
1898mb_adjust_opend(oap)
1899 oparg_T *oap;
1900{
1901 char_u *p;
1902
1903 if (oap->inclusive)
1904 {
1905 p = ml_get(oap->end.lnum);
1906 oap->end.col += mb_tail_off(p, p + oap->end.col);
1907 }
1908}
1909#endif
1910
1911#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1912/*
1913 * Replace a whole area with one character.
1914 */
1915 int
1916op_replace(oap, c)
1917 oparg_T *oap;
1918 int c;
1919{
1920 int n, numc;
1921#ifdef FEAT_MBYTE
1922 int num_chars;
1923#endif
1924 char_u *newp, *oldp;
1925 size_t oldlen;
1926 struct block_def bd;
1927
1928 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1929 return OK; /* nothing to do */
1930
1931#ifdef FEAT_MBYTE
1932 if (has_mbyte)
1933 mb_adjust_opend(oap);
1934#endif
1935
1936 if (u_save((linenr_T)(oap->start.lnum - 1),
1937 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1938 return FAIL;
1939
1940 /*
1941 * block mode replace
1942 */
1943 if (oap->block_mode)
1944 {
1945 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1946 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1947 {
1948 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1949 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1950 continue; /* nothing to replace */
1951
1952 /* n == number of extra chars required
1953 * If we split a TAB, it may be replaced by several characters.
1954 * Thus the number of characters may increase!
1955 */
1956#ifdef FEAT_VIRTUALEDIT
1957 /* If the range starts in virtual space, count the initial
1958 * coladd offset as part of "startspaces" */
1959 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1960 {
1961 pos_T vpos;
1962
1963 getvpos(&vpos, oap->start_vcol);
1964 bd.startspaces += vpos.coladd;
1965 n = bd.startspaces;
1966 }
1967 else
1968#endif
1969 /* allow for pre spaces */
1970 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1971
1972 /* allow for post spp */
1973 n += (bd.endspaces
1974#ifdef FEAT_VIRTUALEDIT
1975 && !bd.is_oneChar
1976#endif
1977 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1978 /* Figure out how many characters to replace. */
1979 numc = oap->end_vcol - oap->start_vcol + 1;
1980 if (bd.is_short && (!virtual_op || bd.is_MAX))
1981 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1982
1983#ifdef FEAT_MBYTE
1984 /* A double-wide character can be replaced only up to half the
1985 * times. */
1986 if ((*mb_char2cells)(c) > 1)
1987 {
1988 if ((numc & 1) && !bd.is_short)
1989 {
1990 ++bd.endspaces;
1991 ++n;
1992 }
1993 numc = numc / 2;
1994 }
1995
1996 /* Compute bytes needed, move character count to num_chars. */
1997 num_chars = numc;
1998 numc *= (*mb_char2len)(c);
1999#endif
2000 /* oldlen includes textlen, so don't double count */
2001 n += numc - bd.textlen;
2002
2003 oldp = ml_get_curline();
2004 oldlen = STRLEN(oldp);
2005 newp = alloc_check((unsigned)oldlen + 1 + n);
2006 if (newp == NULL)
2007 continue;
2008 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2009 /* copy up to deleted part */
2010 mch_memmove(newp, oldp, (size_t)bd.textcol);
2011 oldp += bd.textcol + bd.textlen;
2012 /* insert pre-spaces */
2013 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2014 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2015#ifdef FEAT_MBYTE
2016 if (has_mbyte)
2017 {
2018 n = STRLEN(newp);
2019 while (--num_chars >= 0)
2020 n += (*mb_char2bytes)(c, newp + n);
2021 }
2022 else
2023#endif
2024 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2025 if (!bd.is_short)
2026 {
2027 /* insert post-spaces */
2028 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2029 /* copy the part after the changed part */
2030 mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2031 }
2032 /* replace the line */
2033 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2034 }
2035 }
2036 else
2037 {
2038 /*
2039 * MCHAR and MLINE motion replace.
2040 */
2041 if (oap->motion_type == MLINE)
2042 {
2043 oap->start.col = 0;
2044 curwin->w_cursor.col = 0;
2045 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2046 if (oap->end.col)
2047 --oap->end.col;
2048 }
2049 else if (!oap->inclusive)
2050 dec(&(oap->end));
2051
2052 while (ltoreq(curwin->w_cursor, oap->end))
2053 {
2054 n = gchar_cursor();
2055 if (n != NUL)
2056 {
2057#ifdef FEAT_MBYTE
2058 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2059 {
2060 /* This is slow, but it handles replacing a single-byte
2061 * with a multi-byte and the other way around. */
2062 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2063 n = State;
2064 State = REPLACE;
2065 ins_char(c);
2066 State = n;
2067 /* Backup to the replaced character. */
2068 dec_cursor();
2069 }
2070 else
2071#endif
2072 {
2073#ifdef FEAT_VIRTUALEDIT
2074 if (n == TAB)
2075 {
2076 int end_vcol = 0;
2077
2078 if (curwin->w_cursor.lnum == oap->end.lnum)
2079 {
2080 /* oap->end has to be recalculated when
2081 * the tab breaks */
2082 end_vcol = getviscol2(oap->end.col,
2083 oap->end.coladd);
2084 }
2085 coladvance_force(getviscol());
2086 if (curwin->w_cursor.lnum == oap->end.lnum)
2087 getvpos(&oap->end, end_vcol);
2088 }
2089#endif
2090 pchar(curwin->w_cursor, c);
2091 }
2092 }
2093#ifdef FEAT_VIRTUALEDIT
2094 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2095 {
2096 int virtcols = oap->end.coladd;
2097
2098 if (curwin->w_cursor.lnum == oap->start.lnum
2099 && oap->start.col == oap->end.col && oap->start.coladd)
2100 virtcols -= oap->start.coladd;
2101
2102 /* oap->end has been trimmed so it's effectively inclusive;
2103 * as a result an extra +1 must be counted so we don't
2104 * trample the NUL byte. */
2105 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2106 curwin->w_cursor.col -= (virtcols + 1);
2107 for (; virtcols >= 0; virtcols--)
2108 {
2109 pchar(curwin->w_cursor, c);
2110 if (inc(&curwin->w_cursor) == -1)
2111 break;
2112 }
2113 }
2114#endif
2115
2116 /* Advance to next character, stop at the end of the file. */
2117 if (inc_cursor() == -1)
2118 break;
2119 }
2120 }
2121
2122 curwin->w_cursor = oap->start;
2123 check_cursor();
2124 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2125
2126 /* Set "'[" and "']" marks. */
2127 curbuf->b_op_start = oap->start;
2128 curbuf->b_op_end = oap->end;
2129
2130 return OK;
2131}
2132#endif
2133
2134/*
2135 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2136 */
2137 void
2138op_tilde(oap)
2139 oparg_T *oap;
2140{
2141 pos_T pos;
2142#ifdef FEAT_VISUAL
2143 struct block_def bd;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002144 int todo;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 int did_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148 if (u_save((linenr_T)(oap->start.lnum - 1),
2149 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2150 return;
2151
2152 pos = oap->start;
2153#ifdef FEAT_VISUAL
2154 if (oap->block_mode) /* Visual block mode */
2155 {
2156 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2157 {
2158 block_prep(oap, &bd, pos.lnum, FALSE);
2159 pos.col = bd.textcol;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002160 for (todo = bd.textlen; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002162# ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002163 if (has_mbyte)
2164 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 did_change |= swapchar(oap->op_type, &pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (inc(&pos) == -1) /* at end of file */
2168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170# ifdef FEAT_NETBEANS_INTG
2171 if (usingNetbeans && did_change)
2172 {
2173 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2174
Bram Moolenaar009b2592004-10-24 19:18:58 +00002175 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2176 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002178 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180# endif
2181 }
2182 if (did_change)
2183 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2184 }
2185 else /* not block mode */
2186#endif
2187 {
2188 if (oap->motion_type == MLINE)
2189 {
2190 oap->start.col = 0;
2191 pos.col = 0;
2192 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2193 if (oap->end.col)
2194 --oap->end.col;
2195 }
2196 else if (!oap->inclusive)
2197 dec(&(oap->end));
2198
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002199 while (ltoreq(pos, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 did_change |= swapchar(oap->op_type, &pos);
2202 if (inc(&pos) == -1) /* at end of file */
2203 break;
2204 }
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (did_change)
2207 {
2208 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2209 0L);
2210#ifdef FEAT_NETBEANS_INTG
2211 if (usingNetbeans && did_change)
2212 {
2213 char_u *ptr;
2214 int count;
2215
2216 pos = oap->start;
2217 while (pos.lnum < oap->end.lnum)
2218 {
2219 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2220 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002221 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002223 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 pos.col = 0;
2225 pos.lnum++;
2226 }
2227 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2228 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002229 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002231 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233#endif
2234 }
2235 }
2236
2237#ifdef FEAT_VISUAL
2238 if (!did_change && oap->is_VIsual)
2239 /* No change: need to remove the Visual selection */
2240 redraw_curbuf_later(INVERTED);
2241#endif
2242
2243 /*
2244 * Set '[ and '] marks.
2245 */
2246 curbuf->b_op_start = oap->start;
2247 curbuf->b_op_end = oap->end;
2248
2249 if (oap->line_count > p_report)
2250 {
2251 if (oap->line_count == 1)
2252 MSG(_("1 line changed"));
2253 else
2254 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2255 }
2256}
2257
2258/*
2259 * If op_type == OP_UPPER: make uppercase,
2260 * if op_type == OP_LOWER: make lowercase,
2261 * if op_type == OP_ROT13: do rot13 encoding,
2262 * else swap case of character at 'pos'
2263 * returns TRUE when something actually changed.
2264 */
2265 int
2266swapchar(op_type, pos)
2267 int op_type;
2268 pos_T *pos;
2269{
2270 int c;
2271 int nc;
2272
2273 c = gchar_pos(pos);
2274
2275 /* Only do rot13 encoding for ASCII characters. */
2276 if (c >= 0x80 && op_type == OP_ROT13)
2277 return FALSE;
2278
2279#ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002280 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2281 {
2282 pos_T sp = curwin->w_cursor;
2283
2284 /* Special handling of German sharp s: change to "SS". */
2285 curwin->w_cursor = *pos;
2286 del_char(FALSE);
2287 ins_char('S');
2288 ins_char('S');
2289 curwin->w_cursor = sp;
2290 inc(pos);
2291 }
2292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2294 return FALSE;
2295#endif
2296 nc = c;
2297 if (MB_ISLOWER(c))
2298 {
2299 if (op_type == OP_ROT13)
2300 nc = ROT13(c, 'a');
2301 else if (op_type != OP_LOWER)
2302 nc = MB_TOUPPER(c);
2303 }
2304 else if (MB_ISUPPER(c))
2305 {
2306 if (op_type == OP_ROT13)
2307 nc = ROT13(c, 'A');
2308 else if (op_type != OP_UPPER)
2309 nc = MB_TOLOWER(c);
2310 }
2311 if (nc != c)
2312 {
2313#ifdef FEAT_MBYTE
2314 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2315 {
2316 pos_T sp = curwin->w_cursor;
2317
2318 curwin->w_cursor = *pos;
2319 del_char(FALSE);
2320 ins_char(nc);
2321 curwin->w_cursor = sp;
2322 }
2323 else
2324#endif
2325 pchar(*pos, nc);
2326 return TRUE;
2327 }
2328 return FALSE;
2329}
2330
2331#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2332/*
2333 * op_insert - Insert and append operators for Visual mode.
2334 */
2335 void
2336op_insert(oap, count1)
2337 oparg_T *oap;
2338 long count1;
2339{
2340 long ins_len, pre_textlen = 0;
2341 char_u *firstline, *ins_text;
2342 struct block_def bd;
2343 int i;
2344
2345 /* edit() changes this - record it for OP_APPEND */
2346 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2347
2348 /* vis block is still marked. Get rid of it now. */
2349 curwin->w_cursor.lnum = oap->start.lnum;
2350 update_screen(INVERTED);
2351
2352 if (oap->block_mode)
2353 {
2354#ifdef FEAT_VIRTUALEDIT
2355 /* When 'virtualedit' is used, need to insert the extra spaces before
2356 * doing block_prep(). When only "block" is used, virtual edit is
2357 * already disabled, but still need it when calling
2358 * coladvance_force(). */
2359 if (curwin->w_cursor.coladd > 0)
2360 {
2361 int old_ve_flags = ve_flags;
2362
2363 ve_flags = VE_ALL;
2364 if (u_save_cursor() == FAIL)
2365 return;
2366 coladvance_force(oap->op_type == OP_APPEND
2367 ? oap->end_vcol + 1 : getviscol());
2368 if (oap->op_type == OP_APPEND)
2369 --curwin->w_cursor.col;
2370 ve_flags = old_ve_flags;
2371 }
2372#endif
2373 /* Get the info about the block before entering the text */
2374 block_prep(oap, &bd, oap->start.lnum, TRUE);
2375 firstline = ml_get(oap->start.lnum) + bd.textcol;
2376 if (oap->op_type == OP_APPEND)
2377 firstline += bd.textlen;
2378 pre_textlen = (long)STRLEN(firstline);
2379 }
2380
2381 if (oap->op_type == OP_APPEND)
2382 {
2383 if (oap->block_mode
2384#ifdef FEAT_VIRTUALEDIT
2385 && curwin->w_cursor.coladd == 0
2386#endif
2387 )
2388 {
2389 /* Move the cursor to the character right of the block. */
2390 curwin->w_set_curswant = TRUE;
2391 while (*ml_get_cursor() != NUL
2392 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2393 ++curwin->w_cursor.col;
2394 if (bd.is_short && !bd.is_MAX)
2395 {
2396 /* First line was too short, make it longer and adjust the
2397 * values in "bd". */
2398 if (u_save_cursor() == FAIL)
2399 return;
2400 for (i = 0; i < bd.endspaces; ++i)
2401 ins_char(' ');
2402 bd.textlen += bd.endspaces;
2403 }
2404 }
2405 else
2406 {
2407 curwin->w_cursor = oap->end;
2408
2409 /* Works just like an 'i'nsert on the next character. */
2410 if (!lineempty(curwin->w_cursor.lnum)
2411 && oap->start_vcol != oap->end_vcol)
2412 inc_cursor();
2413 }
2414 }
2415
2416 edit(NUL, FALSE, (linenr_T)count1);
2417
2418 /* if user has moved off this line, we don't know what to do, so do
2419 * nothing */
2420 if (curwin->w_cursor.lnum != oap->start.lnum)
2421 return;
2422
2423 if (oap->block_mode)
2424 {
2425 struct block_def bd2;
2426
2427 /*
2428 * Spaces and tabs in the indent may have changed to other spaces and
2429 * tabs. Get the starting column again and correct the lenght.
2430 * Don't do this when "$" used, end-of-line will have changed.
2431 */
2432 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2433 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2434 {
2435 if (oap->op_type == OP_APPEND)
2436 {
2437 pre_textlen += bd2.textlen - bd.textlen;
2438 if (bd2.endspaces)
2439 --bd2.textlen;
2440 }
2441 bd.textcol = bd2.textcol;
2442 bd.textlen = bd2.textlen;
2443 }
2444
2445 /*
2446 * Subsequent calls to ml_get() flush the firstline data - take a
2447 * copy of the required string.
2448 */
2449 firstline = ml_get(oap->start.lnum) + bd.textcol;
2450 if (oap->op_type == OP_APPEND)
2451 firstline += bd.textlen;
2452 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2453 {
2454 ins_text = vim_strnsave(firstline, (int)ins_len);
2455 if (ins_text != NULL)
2456 {
2457 /* block handled here */
2458 if (u_save(oap->start.lnum,
2459 (linenr_T)(oap->end.lnum + 1)) == OK)
2460 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2461 &bd);
2462
2463 curwin->w_cursor.col = oap->start.col;
2464 check_cursor();
2465 vim_free(ins_text);
2466 }
2467 }
2468 }
2469}
2470#endif
2471
2472/*
2473 * op_change - handle a change operation
2474 *
2475 * return TRUE if edit() returns because of a CTRL-O command
2476 */
2477 int
2478op_change(oap)
2479 oparg_T *oap;
2480{
2481 colnr_T l;
2482 int retval;
2483#ifdef FEAT_VISUALEXTRA
2484 long offset;
2485 linenr_T linenr;
2486 long ins_len, pre_textlen = 0;
2487 char_u *firstline;
2488 char_u *ins_text, *newp, *oldp;
2489 struct block_def bd;
2490#endif
2491
2492 l = oap->start.col;
2493 if (oap->motion_type == MLINE)
2494 {
2495 l = 0;
2496#ifdef FEAT_SMARTINDENT
2497 if (!p_paste && curbuf->b_p_si
2498# ifdef FEAT_CINDENT
2499 && !curbuf->b_p_cin
2500# endif
2501 )
2502 can_si = TRUE; /* It's like opening a new line, do si */
2503#endif
2504 }
2505
2506 /* First delete the text in the region. In an empty buffer only need to
2507 * save for undo */
2508 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2509 {
2510 if (u_save_cursor() == FAIL)
2511 return FALSE;
2512 }
2513 else if (op_delete(oap) == FAIL)
2514 return FALSE;
2515
2516 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2517 && !virtual_op)
2518 inc_cursor();
2519
2520#ifdef FEAT_VISUALEXTRA
2521 /* check for still on same line (<CR> in inserted text meaningless) */
2522 /* skip blank lines too */
2523 if (oap->block_mode)
2524 {
2525# ifdef FEAT_VIRTUALEDIT
2526 /* Add spaces before getting the current line length. */
2527 if (virtual_op && (curwin->w_cursor.coladd > 0
2528 || gchar_cursor() == NUL))
2529 coladvance_force(getviscol());
2530# endif
2531 pre_textlen = (long)STRLEN(ml_get(oap->start.lnum));
2532 bd.textcol = curwin->w_cursor.col;
2533 }
2534#endif
2535
2536#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2537 if (oap->motion_type == MLINE)
2538 fix_indent();
2539#endif
2540
2541 retval = edit(NUL, FALSE, (linenr_T)1);
2542
2543#ifdef FEAT_VISUALEXTRA
2544 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002545 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * block.
2547 */
2548 if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2549 {
2550 firstline = ml_get(oap->start.lnum);
2551 /*
2552 * Subsequent calls to ml_get() flush the firstline data - take a
2553 * copy of the required bit.
2554 */
2555 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2556 {
2557 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2558 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002559 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2561 linenr++)
2562 {
2563 block_prep(oap, &bd, linenr, TRUE);
2564 if (!bd.is_short || virtual_op)
2565 {
2566# ifdef FEAT_VIRTUALEDIT
2567 pos_T vpos;
2568
2569 /* If the block starts in virtual space, count the
2570 * initial coladd offset as part of "startspaces" */
2571 if (bd.is_short)
2572 {
2573 linenr_T lnum = curwin->w_cursor.lnum;
2574
2575 curwin->w_cursor.lnum = linenr;
2576 (void)getvpos(&vpos, oap->start_vcol);
2577 curwin->w_cursor.lnum = lnum;
2578 }
2579 else
2580 vpos.coladd = 0;
2581# endif
2582 oldp = ml_get(linenr);
2583 newp = alloc_check((unsigned)(STRLEN(oldp)
2584# ifdef FEAT_VIRTUALEDIT
2585 + vpos.coladd
2586# endif
2587 + ins_len + 1));
2588 if (newp == NULL)
2589 continue;
2590 /* copy up to block start */
2591 mch_memmove(newp, oldp, (size_t)bd.textcol);
2592 offset = bd.textcol;
2593# ifdef FEAT_VIRTUALEDIT
2594 copy_spaces(newp + offset, (size_t)vpos.coladd);
2595 offset += vpos.coladd;
2596# endif
2597 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2598 offset += ins_len;
2599 oldp += bd.textcol;
2600 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2601 ml_replace(linenr, newp, FALSE);
2602 }
2603 }
2604 check_cursor();
2605
2606 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2607 }
2608 vim_free(ins_text);
2609 }
2610 }
2611#endif
2612
2613 return retval;
2614}
2615
2616/*
2617 * set all the yank registers to empty (called from main())
2618 */
2619 void
2620init_yank()
2621{
2622 int i;
2623
2624 for (i = 0; i < NUM_REGISTERS; ++i)
2625 y_regs[i].y_array = NULL;
2626}
2627
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002628#if defined(EXITFREE) || defined(PROTO)
2629 void
2630clear_registers()
2631{
2632 int i;
2633
2634 for (i = 0; i < NUM_REGISTERS; ++i)
2635 {
2636 y_current = &y_regs[i];
2637 if (y_current->y_array != NULL)
2638 free_yank_all();
2639 }
2640}
2641#endif
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643/*
2644 * Free "n" lines from the current yank register.
2645 * Called for normal freeing and in case of error.
2646 */
2647 static void
2648free_yank(n)
2649 long n;
2650{
2651 if (y_current->y_array != NULL)
2652 {
2653 long i;
2654
2655 for (i = n; --i >= 0; )
2656 {
2657#ifdef AMIGA /* only for very slow machines */
2658 if ((i & 1023) == 1023) /* this may take a while */
2659 {
2660 /*
2661 * This message should never cause a hit-return message.
2662 * Overwrite this message with any next message.
2663 */
2664 ++no_wait_return;
2665 smsg((char_u *)_("freeing %ld lines"), i + 1);
2666 --no_wait_return;
2667 msg_didout = FALSE;
2668 msg_col = 0;
2669 }
2670#endif
2671 vim_free(y_current->y_array[i]);
2672 }
2673 vim_free(y_current->y_array);
2674 y_current->y_array = NULL;
2675#ifdef AMIGA
2676 if (n >= 1000)
2677 MSG("");
2678#endif
2679 }
2680}
2681
2682 static void
2683free_yank_all()
2684{
2685 free_yank(y_current->y_size);
2686}
2687
2688/*
2689 * Yank the text between "oap->start" and "oap->end" into a yank register.
2690 * If we are to append (uppercase register), we first yank into a new yank
2691 * register and then concatenate the old and the new one (so we keep the old
2692 * one in case of out-of-memory).
2693 *
2694 * return FAIL for failure, OK otherwise
2695 */
2696 int
2697op_yank(oap, deleting, mess)
2698 oparg_T *oap;
2699 int deleting;
2700 int mess;
2701{
2702 long y_idx; /* index in y_array[] */
2703 struct yankreg *curr; /* copy of y_current */
2704 struct yankreg newreg; /* new yank register when appending */
2705 char_u **new_ptr;
2706 linenr_T lnum; /* current line number */
2707 long j;
2708 int yanktype = oap->motion_type;
2709 long yanklines = oap->line_count;
2710 linenr_T yankendlnum = oap->end.lnum;
2711 char_u *p;
2712 char_u *pnew;
2713 struct block_def bd;
2714
2715 /* check for read-only register */
2716 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2717 {
2718 beep_flush();
2719 return FAIL;
2720 }
2721 if (oap->regname == '_') /* black hole: nothing to do */
2722 return OK;
2723
2724#ifdef FEAT_CLIPBOARD
2725 if (!clip_star.available && oap->regname == '*')
2726 oap->regname = 0;
2727 else if (!clip_plus.available && oap->regname == '+')
2728 oap->regname = 0;
2729#endif
2730
2731 if (!deleting) /* op_delete() already set y_current */
2732 get_yank_register(oap->regname, TRUE);
2733
2734 curr = y_current;
2735 /* append to existing contents */
2736 if (y_append && y_current->y_array != NULL)
2737 y_current = &newreg;
2738 else
2739 free_yank_all(); /* free previously yanked lines */
2740
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002741 /*
2742 * If the cursor was in column 1 before and after the movement, and the
2743 * operator is not inclusive, the yank is always linewise.
2744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if ( oap->motion_type == MCHAR
2746 && oap->start.col == 0
2747 && !oap->inclusive
2748#ifdef FEAT_VISUAL
2749 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002750 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752 && oap->end.col == 0
2753 && yanklines > 1)
2754 {
2755 yanktype = MLINE;
2756 --yankendlnum;
2757 --yanklines;
2758 }
2759
2760 y_current->y_size = yanklines;
2761 y_current->y_type = yanktype; /* set the yank register type */
2762#ifdef FEAT_VISUAL
2763 y_current->y_width = 0;
2764#endif
2765 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2766 yanklines), TRUE);
2767
2768 if (y_current->y_array == NULL)
2769 {
2770 y_current = curr;
2771 return FAIL;
2772 }
2773
2774 y_idx = 0;
2775 lnum = oap->start.lnum;
2776
2777#ifdef FEAT_VISUAL
2778 if (oap->block_mode)
2779 {
2780 /* Visual block mode */
2781 y_current->y_type = MBLOCK; /* set the yank register type */
2782 y_current->y_width = oap->end_vcol - oap->start_vcol;
2783
2784 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2785 y_current->y_width--;
2786 }
2787#endif
2788
2789 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2790 {
2791 switch (y_current->y_type)
2792 {
2793#ifdef FEAT_VISUAL
2794 case MBLOCK:
2795 block_prep(oap, &bd, lnum, FALSE);
2796 if (yank_copy_line(&bd, y_idx) == FAIL)
2797 goto fail;
2798 break;
2799#endif
2800
2801 case MLINE:
2802 if ((y_current->y_array[y_idx] =
2803 vim_strsave(ml_get(lnum))) == NULL)
2804 goto fail;
2805 break;
2806
2807 case MCHAR:
2808 {
2809 colnr_T startcol = 0, endcol = MAXCOL;
2810#ifdef FEAT_VIRTUALEDIT
2811 int is_oneChar = FALSE;
2812 colnr_T cs, ce;
2813#endif
2814 p = ml_get(lnum);
2815 bd.startspaces = 0;
2816 bd.endspaces = 0;
2817
2818 if (lnum == oap->start.lnum)
2819 {
2820 startcol = oap->start.col;
2821#ifdef FEAT_VIRTUALEDIT
2822 if (virtual_op)
2823 {
2824 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2825 if (ce != cs && oap->start.coladd > 0)
2826 {
2827 /* Part of a tab selected -- but don't
2828 * double-count it. */
2829 bd.startspaces = (ce - cs + 1)
2830 - oap->start.coladd;
2831 startcol++;
2832 }
2833 }
2834#endif
2835 }
2836
2837 if (lnum == oap->end.lnum)
2838 {
2839 endcol = oap->end.col;
2840#ifdef FEAT_VIRTUALEDIT
2841 if (virtual_op)
2842 {
2843 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2844 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2845# ifdef FEAT_MBYTE
2846 /* Don't add space for double-wide
2847 * char; endcol will be on last byte
2848 * of multi-byte char. */
2849 && (*mb_head_off)(p, p + endcol) == 0
2850# endif
2851 ))
2852 {
2853 if (oap->start.lnum == oap->end.lnum
2854 && oap->start.col == oap->end.col)
2855 {
2856 /* Special case: inside a single char */
2857 is_oneChar = TRUE;
2858 bd.startspaces = oap->end.coladd
2859 - oap->start.coladd + oap->inclusive;
2860 endcol = startcol;
2861 }
2862 else
2863 {
2864 bd.endspaces = oap->end.coladd
2865 + oap->inclusive;
2866 endcol -= oap->inclusive;
2867 }
2868 }
2869 }
2870#endif
2871 }
2872 if (startcol > endcol
2873#ifdef FEAT_VIRTUALEDIT
2874 || is_oneChar
2875#endif
2876 )
2877 bd.textlen = 0;
2878 else
2879 {
2880 if (endcol == MAXCOL)
2881 endcol = STRLEN(p);
2882 bd.textlen = endcol - startcol + oap->inclusive;
2883 }
2884 bd.textstart = p + startcol;
2885 if (yank_copy_line(&bd, y_idx) == FAIL)
2886 goto fail;
2887 break;
2888 }
2889 /* NOTREACHED */
2890 }
2891 }
2892
2893 if (curr != y_current) /* append the new block to the old block */
2894 {
2895 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2896 (curr->y_size + y_current->y_size)), TRUE);
2897 if (new_ptr == NULL)
2898 goto fail;
2899 for (j = 0; j < curr->y_size; ++j)
2900 new_ptr[j] = curr->y_array[j];
2901 vim_free(curr->y_array);
2902 curr->y_array = new_ptr;
2903
2904 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2905 curr->y_type = MLINE;
2906
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002907 /* Concatenate the last line of the old block with the first line of
2908 * the new block, unless being Vi compatible. */
2909 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2912 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2913 if (pnew == NULL)
2914 {
2915 y_idx = y_current->y_size - 1;
2916 goto fail;
2917 }
2918 STRCPY(pnew, curr->y_array[--j]);
2919 STRCAT(pnew, y_current->y_array[0]);
2920 vim_free(curr->y_array[j]);
2921 vim_free(y_current->y_array[0]);
2922 curr->y_array[j++] = pnew;
2923 y_idx = 1;
2924 }
2925 else
2926 y_idx = 0;
2927 while (y_idx < y_current->y_size)
2928 curr->y_array[j++] = y_current->y_array[y_idx++];
2929 curr->y_size = j;
2930 vim_free(y_current->y_array);
2931 y_current = curr;
2932 }
2933 if (mess) /* Display message about yank? */
2934 {
2935 if (yanktype == MCHAR
2936#ifdef FEAT_VISUAL
2937 && !oap->block_mode
2938#endif
2939 && yanklines == 1)
2940 yanklines = 0;
2941 /* Some versions of Vi use ">=" here, some don't... */
2942 if (yanklines > p_report)
2943 {
2944 /* redisplay now, so message is not deleted */
2945 update_topline_redraw();
2946 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002947 {
2948#ifdef FEAT_VISUAL
2949 if (oap->block_mode)
2950 MSG(_("block of 1 line yanked"));
2951 else
2952#endif
2953 MSG(_("1 line yanked"));
2954 }
2955#ifdef FEAT_VISUAL
2956 else if (oap->block_mode)
2957 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
2958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else
2960 smsg((char_u *)_("%ld lines yanked"), yanklines);
2961 }
2962 }
2963
2964 /*
2965 * Set "'[" and "']" marks.
2966 */
2967 curbuf->b_op_start = oap->start;
2968 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002969 if (yanktype == MLINE
2970#ifdef FEAT_VISUAL
2971 && !oap->block_mode
2972#endif
2973 )
2974 {
2975 curbuf->b_op_start.col = 0;
2976 curbuf->b_op_end.col = MAXCOL;
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979#ifdef FEAT_CLIPBOARD
2980 /*
2981 * If we were yanking to the '*' register, send result to clipboard.
2982 * If no register was specified, and "unnamed" in 'clipboard', make a copy
2983 * to the '*' register.
2984 */
2985 if (clip_star.available
2986 && (curr == &(y_regs[STAR_REGISTER])
2987 || (!deleting && oap->regname == 0 && clip_unnamed)))
2988 {
2989 if (curr != &(y_regs[STAR_REGISTER]))
2990 /* Copy the text from register 0 to the clipboard register. */
2991 copy_yank_reg(&(y_regs[STAR_REGISTER]));
2992
2993 clip_own_selection(&clip_star);
2994 clip_gen_set_selection(&clip_star);
2995 }
2996
2997# ifdef FEAT_X11
2998 /*
2999 * If we were yanking to the '+' register, send result to selection.
3000 * Also copy to the '*' register, in case auto-select is off.
3001 */
3002 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3003 {
3004 /* No need to copy to * register upon 'unnamed' now - see below */
3005 clip_own_selection(&clip_plus);
3006 clip_gen_set_selection(&clip_plus);
3007 if (!clip_isautosel())
3008 {
3009 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3010 clip_own_selection(&clip_star);
3011 clip_gen_set_selection(&clip_star);
3012 }
3013 }
3014# endif
3015#endif
3016
3017 return OK;
3018
3019fail: /* free the allocated lines */
3020 free_yank(y_idx + 1);
3021 y_current = curr;
3022 return FAIL;
3023}
3024
3025 static int
3026yank_copy_line(bd, y_idx)
3027 struct block_def *bd;
3028 long y_idx;
3029{
3030 char_u *pnew;
3031
3032 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3033 == NULL)
3034 return FAIL;
3035 y_current->y_array[y_idx] = pnew;
3036 copy_spaces(pnew, (size_t)bd->startspaces);
3037 pnew += bd->startspaces;
3038 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3039 pnew += bd->textlen;
3040 copy_spaces(pnew, (size_t)bd->endspaces);
3041 pnew += bd->endspaces;
3042 *pnew = NUL;
3043 return OK;
3044}
3045
3046#ifdef FEAT_CLIPBOARD
3047/*
3048 * Make a copy of the y_current register to register "reg".
3049 */
3050 static void
3051copy_yank_reg(reg)
3052 struct yankreg *reg;
3053{
3054 struct yankreg *curr = y_current;
3055 long j;
3056
3057 y_current = reg;
3058 free_yank_all();
3059 *y_current = *curr;
3060 y_current->y_array = (char_u **)lalloc_clear(
3061 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3062 if (y_current->y_array == NULL)
3063 y_current->y_size = 0;
3064 else
3065 for (j = 0; j < y_current->y_size; ++j)
3066 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3067 {
3068 free_yank(j);
3069 y_current->y_size = 0;
3070 break;
3071 }
3072 y_current = curr;
3073}
3074#endif
3075
3076/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003077 * Put contents of register "regname" into the text.
3078 * Caller must check "regname" to be valid!
3079 * "flags": PUT_FIXINDENT make indent look nice
3080 * PUT_CURSEND leave cursor after end of new text
3081 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 */
3083 void
3084do_put(regname, dir, count, flags)
3085 int regname;
3086 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3087 long count;
3088 int flags;
3089{
3090 char_u *ptr;
3091 char_u *newp, *oldp;
3092 int yanklen;
3093 int totlen = 0; /* init for gcc */
3094 linenr_T lnum;
3095 colnr_T col;
3096 long i; /* index in y_array[] */
3097 int y_type;
3098 long y_size;
3099#ifdef FEAT_VISUAL
3100 int oldlen;
3101 long y_width = 0;
3102 colnr_T vcol;
3103 int delcount;
3104 int incr = 0;
3105 long j;
3106 struct block_def bd;
3107#endif
3108 char_u **y_array = NULL;
3109 long nr_lines = 0;
3110 pos_T new_cursor;
3111 int indent;
3112 int orig_indent = 0; /* init for gcc */
3113 int indent_diff = 0; /* init for gcc */
3114 int first_indent = TRUE;
3115 int lendiff = 0;
3116 pos_T old_pos;
3117 char_u *insert_string = NULL;
3118 int allocated = FALSE;
3119 long cnt;
3120
3121#ifdef FEAT_CLIPBOARD
3122 /* Adjust register name for "unnamed" in 'clipboard'. */
3123 adjust_clip_reg(&regname);
3124 (void)may_get_selection(regname);
3125#endif
3126
3127 if (flags & PUT_FIXINDENT)
3128 orig_indent = get_indent();
3129
3130 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3131 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3132
3133 /*
3134 * Using inserted text works differently, because the register includes
3135 * special characters (newlines, etc.).
3136 */
3137 if (regname == '.')
3138 {
3139 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3140 (count == -1 ? 'O' : 'i')), count, FALSE);
3141 /* Putting the text is done later, so can't really move the cursor to
3142 * the next character. Use "l" to simulate it. */
3143 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3144 stuffcharReadbuff('l');
3145 return;
3146 }
3147
3148 /*
3149 * For special registers '%' (file name), '#' (alternate file name) and
3150 * ':' (last command line), etc. we have to create a fake yank register.
3151 */
3152 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3153 {
3154 if (insert_string == NULL)
3155 return;
3156 }
3157
3158 if (insert_string != NULL)
3159 {
3160 y_type = MCHAR;
3161#ifdef FEAT_EVAL
3162 if (regname == '=')
3163 {
3164 /* For the = register we need to split the string at NL
3165 * characters. */
3166 /* Loop twice: count the number of lines and save them. */
3167 for (;;)
3168 {
3169 y_size = 0;
3170 ptr = insert_string;
3171 while (ptr != NULL)
3172 {
3173 if (y_array != NULL)
3174 y_array[y_size] = ptr;
3175 ++y_size;
3176 ptr = vim_strchr(ptr, '\n');
3177 if (ptr != NULL)
3178 {
3179 if (y_array != NULL)
3180 *ptr = NUL;
3181 ++ptr;
3182 /* A trailing '\n' makes the string linewise */
3183 if (*ptr == NUL)
3184 {
3185 y_type = MLINE;
3186 break;
3187 }
3188 }
3189 }
3190 if (y_array != NULL)
3191 break;
3192 y_array = (char_u **)alloc((unsigned)
3193 (y_size * sizeof(char_u *)));
3194 if (y_array == NULL)
3195 goto end;
3196 }
3197 }
3198 else
3199#endif
3200 {
3201 y_size = 1; /* use fake one-line yank register */
3202 y_array = &insert_string;
3203 }
3204 }
3205 else
3206 {
3207 get_yank_register(regname, FALSE);
3208
3209 y_type = y_current->y_type;
3210#ifdef FEAT_VISUAL
3211 y_width = y_current->y_width;
3212#endif
3213 y_size = y_current->y_size;
3214 y_array = y_current->y_array;
3215 }
3216
3217#ifdef FEAT_VISUAL
3218 if (y_type == MLINE)
3219 {
3220 if (flags & PUT_LINE_SPLIT)
3221 {
3222 /* "p" or "P" in Visual mode: split the lines to put the text in
3223 * between. */
3224 if (u_save_cursor() == FAIL)
3225 goto end;
3226 ptr = vim_strsave(ml_get_cursor());
3227 if (ptr == NULL)
3228 goto end;
3229 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3230 vim_free(ptr);
3231
3232 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3233 if (ptr == NULL)
3234 goto end;
3235 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3236 ++nr_lines;
3237 dir = FORWARD;
3238 }
3239 if (flags & PUT_LINE_FORWARD)
3240 {
3241 /* Must be "p" for a Visual block, put lines below the block. */
3242 curwin->w_cursor = curbuf->b_visual_end;
3243 dir = FORWARD;
3244 }
3245 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3246 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3247 }
3248#endif
3249
3250 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3251 y_type = MLINE;
3252
3253 if (y_size == 0 || y_array == NULL)
3254 {
3255 EMSG2(_("E353: Nothing in register %s"),
3256 regname == 0 ? (char_u *)"\"" : transchar(regname));
3257 goto end;
3258 }
3259
3260#ifdef FEAT_VISUAL
3261 if (y_type == MBLOCK)
3262 {
3263 lnum = curwin->w_cursor.lnum + y_size + 1;
3264 if (lnum > curbuf->b_ml.ml_line_count)
3265 lnum = curbuf->b_ml.ml_line_count + 1;
3266 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3267 goto end;
3268 }
3269 else
3270#endif
3271 if (y_type == MLINE)
3272 {
3273 lnum = curwin->w_cursor.lnum;
3274#ifdef FEAT_FOLDING
3275 /* Correct line number for closed fold. Don't move the cursor yet,
3276 * u_save() uses it. */
3277 if (dir == BACKWARD)
3278 (void)hasFolding(lnum, &lnum, NULL);
3279 else
3280 (void)hasFolding(lnum, NULL, &lnum);
3281#endif
3282 if (dir == FORWARD)
3283 ++lnum;
3284 if (u_save(lnum - 1, lnum) == FAIL)
3285 goto end;
3286#ifdef FEAT_FOLDING
3287 if (dir == FORWARD)
3288 curwin->w_cursor.lnum = lnum - 1;
3289 else
3290 curwin->w_cursor.lnum = lnum;
3291 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3292#endif
3293 }
3294 else if (u_save_cursor() == FAIL)
3295 goto end;
3296
3297 yanklen = (int)STRLEN(y_array[0]);
3298
3299#ifdef FEAT_VIRTUALEDIT
3300 if (ve_flags == VE_ALL && y_type == MCHAR)
3301 {
3302 if (gchar_cursor() == TAB)
3303 {
3304 /* Don't need to insert spaces when "p" on the last position of a
3305 * tab or "P" on the first position. */
3306 if (dir == FORWARD
3307 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3308 : curwin->w_cursor.coladd > 0)
3309 coladvance_force(getviscol());
3310 else
3311 curwin->w_cursor.coladd = 0;
3312 }
3313 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3314 coladvance_force(getviscol() + (dir == FORWARD));
3315 }
3316#endif
3317
3318 lnum = curwin->w_cursor.lnum;
3319 col = curwin->w_cursor.col;
3320
3321#ifdef FEAT_VISUAL
3322 /*
3323 * Block mode
3324 */
3325 if (y_type == MBLOCK)
3326 {
3327 char c = gchar_cursor();
3328 colnr_T endcol2 = 0;
3329
3330 if (dir == FORWARD && c != NUL)
3331 {
3332#ifdef FEAT_VIRTUALEDIT
3333 if (ve_flags == VE_ALL)
3334 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3335 else
3336#endif
3337 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3338
3339#ifdef FEAT_MBYTE
3340 if (has_mbyte)
3341 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003342 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
3344#endif
3345#ifdef FEAT_VIRTUALEDIT
3346 if (c != TAB || ve_flags != VE_ALL)
3347#endif
3348 ++curwin->w_cursor.col;
3349 ++col;
3350 }
3351 else
3352 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3353
3354#ifdef FEAT_VIRTUALEDIT
3355 col += curwin->w_cursor.coladd;
3356 if (ve_flags == VE_ALL && curwin->w_cursor.coladd > 0)
3357 {
3358 if (dir == FORWARD && c == NUL)
3359 ++col;
3360 if (dir != FORWARD && c != NUL)
3361 ++curwin->w_cursor.col;
3362 if (c == TAB)
3363 {
3364 if (dir == BACKWARD && curwin->w_cursor.col)
3365 curwin->w_cursor.col--;
3366 if (dir == FORWARD && col - 1 == endcol2)
3367 curwin->w_cursor.col++;
3368 }
3369 }
3370 curwin->w_cursor.coladd = 0;
3371#endif
3372 for (i = 0; i < y_size; ++i)
3373 {
3374 int spaces;
3375 char shortline;
3376
3377 bd.startspaces = 0;
3378 bd.endspaces = 0;
3379 bd.textcol = 0;
3380 vcol = 0;
3381 delcount = 0;
3382
3383 /* add a new line */
3384 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3385 {
3386 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3387 (colnr_T)1, FALSE) == FAIL)
3388 break;
3389 ++nr_lines;
3390 }
3391 /* get the old line and advance to the position to insert at */
3392 oldp = ml_get_curline();
3393 oldlen = (int)STRLEN(oldp);
3394 for (ptr = oldp; vcol < col && *ptr; )
3395 {
3396 /* Count a tab for what it's worth (if list mode not on) */
3397 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3398 vcol += incr;
3399 }
3400 bd.textcol = (colnr_T)(ptr - oldp);
3401
3402 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3403
3404 if (vcol < col) /* line too short, padd with spaces */
3405 bd.startspaces = col - vcol;
3406 else if (vcol > col)
3407 {
3408 bd.endspaces = vcol - col;
3409 bd.startspaces = incr - bd.endspaces;
3410 --bd.textcol;
3411 delcount = 1;
3412#ifdef FEAT_MBYTE
3413 if (has_mbyte)
3414 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3415#endif
3416 if (oldp[bd.textcol] != TAB)
3417 {
3418 /* Only a Tab can be split into spaces. Other
3419 * characters will have to be moved to after the
3420 * block, causing misalignment. */
3421 delcount = 0;
3422 bd.endspaces = 0;
3423 }
3424 }
3425
3426 yanklen = (int)STRLEN(y_array[i]);
3427
3428 /* calculate number of spaces required to fill right side of block*/
3429 spaces = y_width + 1;
3430 for (j = 0; j < yanklen; j++)
3431 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3432 if (spaces < 0)
3433 spaces = 0;
3434
3435 /* insert the new text */
3436 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3437 newp = alloc_check((unsigned)totlen + oldlen + 1);
3438 if (newp == NULL)
3439 break;
3440 /* copy part up to cursor to new line */
3441 ptr = newp;
3442 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3443 ptr += bd.textcol;
3444 /* may insert some spaces before the new text */
3445 copy_spaces(ptr, (size_t)bd.startspaces);
3446 ptr += bd.startspaces;
3447 /* insert the new text */
3448 for (j = 0; j < count; ++j)
3449 {
3450 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3451 ptr += yanklen;
3452
3453 /* insert block's trailing spaces only if there's text behind */
3454 if ((j < count - 1 || !shortline) && spaces)
3455 {
3456 copy_spaces(ptr, (size_t)spaces);
3457 ptr += spaces;
3458 }
3459 }
3460 /* may insert some spaces after the new text */
3461 copy_spaces(ptr, (size_t)bd.endspaces);
3462 ptr += bd.endspaces;
3463 /* move the text after the cursor to the end of the line. */
3464 mch_memmove(ptr, oldp + bd.textcol + delcount,
3465 (size_t)(oldlen - bd.textcol - delcount + 1));
3466 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3467
3468 ++curwin->w_cursor.lnum;
3469 if (i == 0)
3470 curwin->w_cursor.col += bd.startspaces;
3471 }
3472
3473 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3474
3475 /* Set '[ mark. */
3476 curbuf->b_op_start = curwin->w_cursor;
3477 curbuf->b_op_start.lnum = lnum;
3478
3479 /* adjust '] mark */
3480 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3481 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003482# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (flags & PUT_CURSEND)
3486 {
3487 curwin->w_cursor = curbuf->b_op_end;
3488 curwin->w_cursor.col++;
3489 }
3490 else
3491 curwin->w_cursor.lnum = lnum;
3492 }
3493 else
3494#endif
3495 {
3496 /*
3497 * Character or Line mode
3498 */
3499 if (y_type == MCHAR)
3500 {
3501 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3502 * char */
3503 if (dir == FORWARD && gchar_cursor() != NUL)
3504 {
3505#ifdef FEAT_MBYTE
3506 if (has_mbyte)
3507 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003508 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 /* put it on the next of the multi-byte character. */
3511 col += bytelen;
3512 if (yanklen)
3513 {
3514 curwin->w_cursor.col += bytelen;
3515 curbuf->b_op_end.col += bytelen;
3516 }
3517 }
3518 else
3519#endif
3520 {
3521 ++col;
3522 if (yanklen)
3523 {
3524 ++curwin->w_cursor.col;
3525 ++curbuf->b_op_end.col;
3526 }
3527 }
3528 }
3529 new_cursor = curwin->w_cursor;
3530 curbuf->b_op_start = curwin->w_cursor;
3531 }
3532 /*
3533 * Line mode: BACKWARD is the same as FORWARD on the previous line
3534 */
3535 else if (dir == BACKWARD)
3536 --lnum;
3537
3538 /*
3539 * simple case: insert into current line
3540 */
3541 if (y_type == MCHAR && y_size == 1)
3542 {
3543 totlen = count * yanklen;
3544 if (totlen)
3545 {
3546 oldp = ml_get(lnum);
3547 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3548 if (newp == NULL)
3549 goto end; /* alloc() will give error message */
3550 mch_memmove(newp, oldp, (size_t)col);
3551 ptr = newp + col;
3552 for (i = 0; i < count; ++i)
3553 {
3554 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3555 ptr += yanklen;
3556 }
3557 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3558 ml_replace(lnum, newp, FALSE);
3559 /* Put cursor on last putted char. */
3560 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3561 }
3562 curbuf->b_op_end = curwin->w_cursor;
3563 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3564 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3565 ++curwin->w_cursor.col;
3566 changed_bytes(lnum, col);
3567 }
3568 else
3569 {
3570 /*
3571 * Insert at least one line. When y_type is MCHAR, break the first
3572 * line in two.
3573 */
3574 for (cnt = 1; cnt <= count; ++cnt)
3575 {
3576 i = 0;
3577 if (y_type == MCHAR)
3578 {
3579 /*
3580 * Split the current line in two at the insert position.
3581 * First insert y_array[size - 1] in front of second line.
3582 * Then append y_array[0] to first line.
3583 */
3584 lnum = new_cursor.lnum;
3585 ptr = ml_get(lnum) + col;
3586 totlen = (int)STRLEN(y_array[y_size - 1]);
3587 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3588 if (newp == NULL)
3589 goto error;
3590 STRCPY(newp, y_array[y_size - 1]);
3591 STRCAT(newp, ptr);
3592 /* insert second line */
3593 ml_append(lnum, newp, (colnr_T)0, FALSE);
3594 vim_free(newp);
3595
3596 oldp = ml_get(lnum);
3597 newp = alloc_check((unsigned)(col + yanklen + 1));
3598 if (newp == NULL)
3599 goto error;
3600 /* copy first part of line */
3601 mch_memmove(newp, oldp, (size_t)col);
3602 /* append to first line */
3603 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3604 ml_replace(lnum, newp, FALSE);
3605
3606 curwin->w_cursor.lnum = lnum;
3607 i = 1;
3608 }
3609
3610 for (; i < y_size; ++i)
3611 {
3612 if ((y_type != MCHAR || i < y_size - 1)
3613 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3614 == FAIL)
3615 goto error;
3616 lnum++;
3617 ++nr_lines;
3618 if (flags & PUT_FIXINDENT)
3619 {
3620 old_pos = curwin->w_cursor;
3621 curwin->w_cursor.lnum = lnum;
3622 ptr = ml_get(lnum);
3623 if (cnt == count && i == y_size - 1)
3624 lendiff = (int)STRLEN(ptr);
3625#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3626 if (*ptr == '#' && preprocs_left())
3627 indent = 0; /* Leave # lines at start */
3628 else
3629#endif
3630 if (*ptr == NUL)
3631 indent = 0; /* Ignore empty lines */
3632 else if (first_indent)
3633 {
3634 indent_diff = orig_indent - get_indent();
3635 indent = orig_indent;
3636 first_indent = FALSE;
3637 }
3638 else if ((indent = get_indent() + indent_diff) < 0)
3639 indent = 0;
3640 (void)set_indent(indent, 0);
3641 curwin->w_cursor = old_pos;
3642 /* remember how many chars were removed */
3643 if (cnt == count && i == y_size - 1)
3644 lendiff -= (int)STRLEN(ml_get(lnum));
3645 }
3646 }
3647 }
3648
3649error:
3650 /* Adjust marks. */
3651 if (y_type == MLINE)
3652 {
3653 curbuf->b_op_start.col = 0;
3654 if (dir == FORWARD)
3655 curbuf->b_op_start.lnum++;
3656 }
3657 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3658 (linenr_T)MAXLNUM, nr_lines, 0L);
3659
3660 /* note changed text for displaying and folding */
3661 if (y_type == MCHAR)
3662 changed_lines(curwin->w_cursor.lnum, col,
3663 curwin->w_cursor.lnum + 1, nr_lines);
3664 else
3665 changed_lines(curbuf->b_op_start.lnum, 0,
3666 curbuf->b_op_start.lnum, nr_lines);
3667
3668 /* put '] mark at last inserted character */
3669 curbuf->b_op_end.lnum = lnum;
3670 /* correct length for change in indent */
3671 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3672 if (col > 1)
3673 curbuf->b_op_end.col = col - 1;
3674 else
3675 curbuf->b_op_end.col = 0;
3676
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003677 if (flags & PUT_CURSLINE)
3678 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003679 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003680 curwin->w_cursor.lnum = lnum;
3681 beginline(BL_WHITE | BL_FIX);
3682 }
3683 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
3685 /* put cursor after inserted text */
3686 if (y_type == MLINE)
3687 {
3688 if (lnum >= curbuf->b_ml.ml_line_count)
3689 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3690 else
3691 curwin->w_cursor.lnum = lnum + 1;
3692 curwin->w_cursor.col = 0;
3693 }
3694 else
3695 {
3696 curwin->w_cursor.lnum = lnum;
3697 curwin->w_cursor.col = col;
3698 }
3699 }
3700 else if (y_type == MLINE)
3701 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003702 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 curwin->w_cursor.col = 0;
3704 if (dir == FORWARD)
3705 ++curwin->w_cursor.lnum;
3706 beginline(BL_WHITE | BL_FIX);
3707 }
3708 else /* put cursor on first inserted character */
3709 curwin->w_cursor = new_cursor;
3710 }
3711 }
3712
3713 msgmore(nr_lines);
3714 curwin->w_set_curswant = TRUE;
3715
3716end:
3717 if (allocated)
3718 {
3719 vim_free(insert_string);
3720 if (regname == '=')
3721 vim_free(y_array);
3722 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003723 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (gchar_cursor() == NUL
3725 && curwin->w_cursor.col > 0
3726 && !(restart_edit || (State & INSERT)))
3727 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003728 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003729 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730#ifdef FEAT_VIRTUALEDIT
3731 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003732 {
3733 colnr_T scol, ecol;
3734
3735 /* Coladd is set to the width of the last character. */
3736 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3737 curwin->w_cursor.coladd = ecol - scol + 1;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739#endif
3740 }
3741}
3742
3743#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3744/*
3745 * Return TRUE if lines starting with '#' should be left aligned.
3746 */
3747 int
3748preprocs_left()
3749{
3750 return
3751# ifdef FEAT_SMARTINDENT
3752# ifdef FEAT_CINDENT
3753 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3754# else
3755 curbuf->b_p_si
3756# endif
3757# endif
3758# ifdef FEAT_CINDENT
3759 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3760# endif
3761 ;
3762}
3763#endif
3764
3765/* Return the character name of the register with the given number */
3766 int
3767get_register_name(num)
3768 int num;
3769{
3770 if (num == -1)
3771 return '"';
3772 else if (num < 10)
3773 return num + '0';
3774 else if (num == DELETION_REGISTER)
3775 return '-';
3776#ifdef FEAT_CLIPBOARD
3777 else if (num == STAR_REGISTER)
3778 return '*';
3779 else if (num == PLUS_REGISTER)
3780 return '+';
3781#endif
3782 else
3783 {
3784#ifdef EBCDIC
3785 int i;
3786
3787 /* EBCDIC is really braindead ... */
3788 i = 'a' + (num - 10);
3789 if (i > 'i')
3790 i += 7;
3791 if (i > 'r')
3792 i += 8;
3793 return i;
3794#else
3795 return num + 'a' - 10;
3796#endif
3797 }
3798}
3799
3800/*
3801 * ":dis" and ":registers": Display the contents of the yank registers.
3802 */
3803 void
3804ex_display(eap)
3805 exarg_T *eap;
3806{
3807 int i, n;
3808 long j;
3809 char_u *p;
3810 struct yankreg *yb;
3811 int name;
3812 int attr;
3813 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003814#ifdef FEAT_MBYTE
3815 int clen;
3816#else
3817# define clen 1
3818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 if (arg != NULL && *arg == NUL)
3821 arg = NULL;
3822 attr = hl_attr(HLF_8);
3823
3824 /* Highlight title */
3825 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3826 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3827 {
3828 name = get_register_name(i);
3829 if (arg != NULL && vim_strchr(arg, name) == NULL)
3830 continue; /* did not ask for this register */
3831
3832#ifdef FEAT_CLIPBOARD
3833 /* Adjust register name for "unnamed" in 'clipboard'.
3834 * When it's a clipboard register, fill it with the current contents
3835 * of the clipboard. */
3836 adjust_clip_reg(&name);
3837 (void)may_get_selection(name);
3838#endif
3839
3840 if (i == -1)
3841 {
3842 if (y_previous != NULL)
3843 yb = y_previous;
3844 else
3845 yb = &(y_regs[0]);
3846 }
3847 else
3848 yb = &(y_regs[i]);
3849 if (yb->y_array != NULL)
3850 {
3851 msg_putchar('\n');
3852 msg_putchar('"');
3853 msg_putchar(name);
3854 MSG_PUTS(" ");
3855
3856 n = (int)Columns - 6;
3857 for (j = 0; j < yb->y_size && n > 1; ++j)
3858 {
3859 if (j)
3860 {
3861 MSG_PUTS_ATTR("^J", attr);
3862 n -= 2;
3863 }
3864 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003867 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003868#endif
3869 msg_outtrans_len(p, clen);
3870#ifdef FEAT_MBYTE
3871 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#endif
3873 }
3874 }
3875 if (n > 1 && yb->y_type == MLINE)
3876 MSG_PUTS_ATTR("^J", attr);
3877 out_flush(); /* show one line at a time */
3878 }
3879 ui_breakcheck();
3880 }
3881
3882 /*
3883 * display last inserted text
3884 */
3885 if ((p = get_last_insert()) != NULL
3886 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3887 {
3888 MSG_PUTS("\n\". ");
3889 dis_msg(p, TRUE);
3890 }
3891
3892 /*
3893 * display last command line
3894 */
3895 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3896 && !got_int)
3897 {
3898 MSG_PUTS("\n\": ");
3899 dis_msg(last_cmdline, FALSE);
3900 }
3901
3902 /*
3903 * display current file name
3904 */
3905 if (curbuf->b_fname != NULL
3906 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3907 {
3908 MSG_PUTS("\n\"% ");
3909 dis_msg(curbuf->b_fname, FALSE);
3910 }
3911
3912 /*
3913 * display alternate file name
3914 */
3915 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3916 {
3917 char_u *fname;
3918 linenr_T dummy;
3919
3920 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
3921 {
3922 MSG_PUTS("\n\"# ");
3923 dis_msg(fname, FALSE);
3924 }
3925 }
3926
3927 /*
3928 * display last search pattern
3929 */
3930 if (last_search_pat() != NULL
3931 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
3932 {
3933 MSG_PUTS("\n\"/ ");
3934 dis_msg(last_search_pat(), FALSE);
3935 }
3936
3937#ifdef FEAT_EVAL
3938 /*
3939 * display last used expression
3940 */
3941 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
3942 && !got_int)
3943 {
3944 MSG_PUTS("\n\"= ");
3945 dis_msg(expr_line, FALSE);
3946 }
3947#endif
3948}
3949
3950/*
3951 * display a string for do_dis()
3952 * truncate at end of screen line
3953 */
3954 static void
3955dis_msg(p, skip_esc)
3956 char_u *p;
3957 int skip_esc; /* if TRUE, ignore trailing ESC */
3958{
3959 int n;
3960#ifdef FEAT_MBYTE
3961 int l;
3962#endif
3963
3964 n = (int)Columns - 6;
3965 while (*p != NUL
3966 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
3967 && (n -= ptr2cells(p)) >= 0)
3968 {
3969#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003970 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
3972 msg_outtrans_len(p, l);
3973 p += l;
3974 }
3975 else
3976#endif
3977 msg_outtrans_len(p++, 1);
3978 }
3979 ui_breakcheck();
3980}
3981
3982/*
3983 * join 'count' lines (minimal 2), including u_save()
3984 */
3985 void
3986do_do_join(count, insert_space)
3987 long count;
3988 int insert_space;
3989{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003990 colnr_T col = MAXCOL;
3991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
3993 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
3994 return;
3995
3996 while (--count > 0)
3997 {
3998 line_breakcheck();
3999 if (got_int || do_join(insert_space) == FAIL)
4000 {
4001 beep_flush();
4002 break;
4003 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004004 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4005 col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004008 /* Vi compatible: use the column of the first join */
4009 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4010 curwin->w_cursor.col = col;
4011
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#if 0
4013 /*
4014 * Need to update the screen if the line where the cursor is became too
4015 * long to fit on the screen.
4016 */
4017 update_topline_redraw();
4018#endif
4019}
4020
4021/*
4022 * Join two lines at the cursor position.
4023 * "redraw" is TRUE when the screen should be updated.
4024 * Caller must have setup for undo.
4025 *
4026 * return FAIL for failure, OK ohterwise
4027 */
4028 int
4029do_join(insert_space)
4030 int insert_space;
4031{
4032 char_u *curr;
4033 char_u *next, *next_start;
4034 char_u *newp;
4035 int endcurr1, endcurr2;
4036 int currsize; /* size of the current line */
4037 int nextsize; /* size of the next line */
4038 int spaces; /* number of spaces to insert */
4039 linenr_T t;
4040
4041 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4042 return FAIL; /* can't join on last line */
4043
4044 curr = ml_get_curline();
4045 currsize = (int)STRLEN(curr);
4046 endcurr1 = endcurr2 = NUL;
4047 if (insert_space && currsize > 0)
4048 {
4049#ifdef FEAT_MBYTE
4050 if (has_mbyte)
4051 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004052 next = curr + currsize;
4053 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 endcurr1 = (*mb_ptr2char)(next);
4055 if (next > curr)
4056 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004057 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 endcurr2 = (*mb_ptr2char)(next);
4059 }
4060 }
4061 else
4062#endif
4063 {
4064 endcurr1 = *(curr + currsize - 1);
4065 if (currsize > 1)
4066 endcurr2 = *(curr + currsize - 2);
4067 }
4068 }
4069
4070 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4071 spaces = 0;
4072 if (insert_space)
4073 {
4074 next = skipwhite(next);
4075 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4076#ifdef FEAT_MBYTE
4077 && (!has_format_option(FO_MBYTE_JOIN)
4078 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4079 && (!has_format_option(FO_MBYTE_JOIN2)
4080 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4081#endif
4082 )
4083 {
4084 /* don't add a space if the line is ending in a space */
4085 if (endcurr1 == ' ')
4086 endcurr1 = endcurr2;
4087 else
4088 ++spaces;
4089 /* extra space when 'joinspaces' set and line ends in '.' */
4090 if ( p_js
4091 && (endcurr1 == '.'
4092 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4093 && (endcurr1 == '?' || endcurr1 == '!'))))
4094 ++spaces;
4095 }
4096 }
4097 nextsize = (int)STRLEN(next);
4098
4099 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4100 if (newp == NULL)
4101 return FAIL;
4102
4103 /*
4104 * Insert the next line first, because we already have that pointer.
4105 * Curr has to be obtained again, because getting next will have
4106 * invalidated it.
4107 */
4108 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4109
4110 curr = ml_get_curline();
4111 mch_memmove(newp, curr, (size_t)currsize);
4112
4113 copy_spaces(newp + currsize, (size_t)spaces);
4114
4115 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4116
4117 /* Only report the change in the first line here, del_lines() will report
4118 * the deleted line. */
4119 changed_lines(curwin->w_cursor.lnum, currsize,
4120 curwin->w_cursor.lnum + 1, 0L);
4121
4122 /*
4123 * Delete the following line. To do this we move the cursor there
4124 * briefly, and then move it back. After del_lines() the cursor may
4125 * have moved up (last line deleted), so the current lnum is kept in t.
4126 *
4127 * Move marks from the deleted line to the joined line, adjusting the
4128 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4129 * should not really be a problem.
4130 */
4131 t = curwin->w_cursor.lnum;
4132 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4133 (long)(currsize + spaces - (next - next_start)));
4134 ++curwin->w_cursor.lnum;
4135 del_lines(1L, FALSE);
4136 curwin->w_cursor.lnum = t;
4137
4138 /*
4139 * go to first character of the joined line
4140 */
4141 curwin->w_cursor.col = currsize;
4142 check_cursor_col();
4143#ifdef FEAT_VIRTUALEDIT
4144 curwin->w_cursor.coladd = 0;
4145#endif
4146 curwin->w_set_curswant = TRUE;
4147
4148 return OK;
4149}
4150
4151#ifdef FEAT_COMMENTS
4152/*
4153 * Return TRUE if the two comment leaders given are the same. "lnum" is
4154 * the first line. White-space is ignored. Note that the whole of
4155 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4156 */
4157 static int
4158same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4159 linenr_T lnum;
4160 int leader1_len;
4161 char_u *leader1_flags;
4162 int leader2_len;
4163 char_u *leader2_flags;
4164{
4165 int idx1 = 0, idx2 = 0;
4166 char_u *p;
4167 char_u *line1;
4168 char_u *line2;
4169
4170 if (leader1_len == 0)
4171 return (leader2_len == 0);
4172
4173 /*
4174 * If first leader has 'f' flag, the lines can be joined only if the
4175 * second line does not have a leader.
4176 * If first leader has 'e' flag, the lines can never be joined.
4177 * If fist leader has 's' flag, the lines can only be joined if there is
4178 * some text after it and the second line has the 'm' flag.
4179 */
4180 if (leader1_flags != NULL)
4181 {
4182 for (p = leader1_flags; *p && *p != ':'; ++p)
4183 {
4184 if (*p == COM_FIRST)
4185 return (leader2_len == 0);
4186 if (*p == COM_END)
4187 return FALSE;
4188 if (*p == COM_START)
4189 {
4190 if (*(ml_get(lnum) + leader1_len) == NUL)
4191 return FALSE;
4192 if (leader2_flags == NULL || leader2_len == 0)
4193 return FALSE;
4194 for (p = leader2_flags; *p && *p != ':'; ++p)
4195 if (*p == COM_MIDDLE)
4196 return TRUE;
4197 return FALSE;
4198 }
4199 }
4200 }
4201
4202 /*
4203 * Get current line and next line, compare the leaders.
4204 * The first line has to be saved, only one line can be locked at a time.
4205 */
4206 line1 = vim_strsave(ml_get(lnum));
4207 if (line1 != NULL)
4208 {
4209 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4210 ;
4211 line2 = ml_get(lnum + 1);
4212 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4213 {
4214 if (!vim_iswhite(line2[idx2]))
4215 {
4216 if (line1[idx1++] != line2[idx2])
4217 break;
4218 }
4219 else
4220 while (vim_iswhite(line1[idx1]))
4221 ++idx1;
4222 }
4223 vim_free(line1);
4224 }
4225 return (idx2 == leader2_len && idx1 == leader1_len);
4226}
4227#endif
4228
4229/*
4230 * implementation of the format operator 'gq'
4231 */
4232 void
4233op_format(oap, keep_cursor)
4234 oparg_T *oap;
4235 int keep_cursor; /* keep cursor on same text char */
4236{
4237 long old_line_count = curbuf->b_ml.ml_line_count;
4238
4239 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4240 * can put it back there. */
4241 curwin->w_cursor = oap->cursor_start;
4242
4243 if (u_save((linenr_T)(oap->start.lnum - 1),
4244 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4245 return;
4246 curwin->w_cursor = oap->start;
4247
4248#ifdef FEAT_VISUAL
4249 if (oap->is_VIsual)
4250 /* When there is no change: need to remove the Visual selection */
4251 redraw_curbuf_later(INVERTED);
4252#endif
4253
4254 /* Set '[ mark at the start of the formatted area */
4255 curbuf->b_op_start = oap->start;
4256
4257 /* For "gw" remember the cursor position and put it back below (adjusted
4258 * for joined and split lines). */
4259 if (keep_cursor)
4260 saved_cursor = oap->cursor_start;
4261
4262 format_lines(oap->line_count);
4263
4264 /*
4265 * Leave the cursor at the first non-blank of the last formatted line.
4266 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4267 * line, so "." will do the next lines.
4268 */
4269 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4270 ++curwin->w_cursor.lnum;
4271 beginline(BL_WHITE | BL_FIX);
4272 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4273 msgmore(old_line_count);
4274
4275 /* put '] mark on the end of the formatted area */
4276 curbuf->b_op_end = curwin->w_cursor;
4277
4278 if (keep_cursor)
4279 {
4280 curwin->w_cursor = saved_cursor;
4281 saved_cursor.lnum = 0;
4282 }
4283
4284#ifdef FEAT_VISUAL
4285 if (oap->is_VIsual)
4286 {
4287 win_T *wp;
4288
4289 FOR_ALL_WINDOWS(wp)
4290 {
4291 if (wp->w_old_cursor_lnum != 0)
4292 {
4293 /* When lines have been inserted or deleted, adjust the end of
4294 * the Visual area to be redrawn. */
4295 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4296 wp->w_old_cursor_lnum += old_line_count;
4297 else
4298 wp->w_old_visual_lnum += old_line_count;
4299 }
4300 }
4301 }
4302#endif
4303}
4304
4305/*
4306 * Format "line_count" lines, starting at the cursor position.
4307 * When "line_count" is negative, format until the end of the paragraph.
4308 * Lines after the cursor line are saved for undo, caller must have saved the
4309 * first line.
4310 */
4311 void
4312format_lines(line_count)
4313 linenr_T line_count;
4314{
4315 int max_len;
4316 int is_not_par; /* current line not part of parag. */
4317 int next_is_not_par; /* next line not part of paragraph */
4318 int is_end_par; /* at end of paragraph */
4319 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4320 int next_is_start_par = FALSE;
4321#ifdef FEAT_COMMENTS
4322 int leader_len = 0; /* leader len of current line */
4323 int next_leader_len; /* leader len of next line */
4324 char_u *leader_flags = NULL; /* flags for leader of current line */
4325 char_u *next_leader_flags; /* flags for leader of next line */
4326 int do_comments; /* format comments */
4327#endif
4328 int advance = TRUE;
4329 int second_indent = -1;
4330 int do_second_indent;
4331 int do_number_indent;
4332 int do_trail_white;
4333 int first_par_line = TRUE;
4334 int smd_save;
4335 long count;
4336 int need_set_indent = TRUE; /* set indent of next paragraph */
4337 int force_format = FALSE;
4338 int old_State = State;
4339
4340 /* length of a line to force formatting: 3 * 'tw' */
4341 max_len = comp_textwidth(TRUE) * 3;
4342
4343 /* check for 'q', '2' and '1' in 'formatoptions' */
4344#ifdef FEAT_COMMENTS
4345 do_comments = has_format_option(FO_Q_COMS);
4346#endif
4347 do_second_indent = has_format_option(FO_Q_SECOND);
4348 do_number_indent = has_format_option(FO_Q_NUMBER);
4349 do_trail_white = has_format_option(FO_WHITE_PAR);
4350
4351 /*
4352 * Get info about the previous and current line.
4353 */
4354 if (curwin->w_cursor.lnum > 1)
4355 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4356#ifdef FEAT_COMMENTS
4357 , &leader_len, &leader_flags, do_comments
4358#endif
4359 );
4360 else
4361 is_not_par = TRUE;
4362 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4363#ifdef FEAT_COMMENTS
4364 , &next_leader_len, &next_leader_flags, do_comments
4365#endif
4366 );
4367 is_end_par = (is_not_par || next_is_not_par);
4368 if (!is_end_par && do_trail_white)
4369 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4370
4371 curwin->w_cursor.lnum--;
4372 for (count = line_count; count != 0 && !got_int; --count)
4373 {
4374 /*
4375 * Advance to next paragraph.
4376 */
4377 if (advance)
4378 {
4379 curwin->w_cursor.lnum++;
4380 prev_is_end_par = is_end_par;
4381 is_not_par = next_is_not_par;
4382#ifdef FEAT_COMMENTS
4383 leader_len = next_leader_len;
4384 leader_flags = next_leader_flags;
4385#endif
4386 }
4387
4388 /*
4389 * The last line to be formatted.
4390 */
4391 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4392 {
4393 next_is_not_par = TRUE;
4394#ifdef FEAT_COMMENTS
4395 next_leader_len = 0;
4396 next_leader_flags = NULL;
4397#endif
4398 }
4399 else
4400 {
4401 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4402#ifdef FEAT_COMMENTS
4403 , &next_leader_len, &next_leader_flags, do_comments
4404#endif
4405 );
4406 if (do_number_indent)
4407 next_is_start_par =
4408 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4409 }
4410 advance = TRUE;
4411 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4412 if (!is_end_par && do_trail_white)
4413 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4414
4415 /*
4416 * Skip lines that are not in a paragraph.
4417 */
4418 if (is_not_par)
4419 {
4420 if (line_count < 0)
4421 break;
4422 }
4423 else
4424 {
4425 /*
4426 * For the first line of a paragraph, check indent of second line.
4427 * Don't do this for comments and empty lines.
4428 */
4429 if (first_par_line
4430 && (do_second_indent || do_number_indent)
4431 && prev_is_end_par
4432 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4433#ifdef FEAT_COMMENTS
4434 && leader_len == 0
4435 && next_leader_len == 0
4436#endif
4437 )
4438 {
4439 if (do_second_indent
4440 && !lineempty(curwin->w_cursor.lnum + 1))
4441 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4442 else if (do_number_indent)
4443 second_indent = get_number_indent(curwin->w_cursor.lnum);
4444 }
4445
4446 /*
4447 * When the comment leader changes, it's the end of the paragraph.
4448 */
4449 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4450#ifdef FEAT_COMMENTS
4451 || !same_leader(curwin->w_cursor.lnum,
4452 leader_len, leader_flags,
4453 next_leader_len, next_leader_flags)
4454#endif
4455 )
4456 is_end_par = TRUE;
4457
4458 /*
4459 * If we have got to the end of a paragraph, or the line is
4460 * getting long, format it.
4461 */
4462 if (is_end_par || force_format)
4463 {
4464 if (need_set_indent)
4465 /* replace indent in first line with minimal number of
4466 * tabs and spaces, according to current options */
4467 (void)set_indent(get_indent(), SIN_CHANGED);
4468
4469 /* put cursor on last non-space */
4470 State = NORMAL; /* don't go past end-of-line */
4471 coladvance((colnr_T)MAXCOL);
4472 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4473 dec_cursor();
4474
4475 /* do the formatting, without 'showmode' */
4476 State = INSERT; /* for open_line() */
4477 smd_save = p_smd;
4478 p_smd = FALSE;
4479 insertchar(NUL, INSCHAR_FORMAT
4480#ifdef FEAT_COMMENTS
4481 + (do_comments ? INSCHAR_DO_COM : 0)
4482#endif
4483 , second_indent);
4484 State = old_State;
4485 p_smd = smd_save;
4486 second_indent = -1;
4487 /* at end of par.: need to set indent of next par. */
4488 need_set_indent = is_end_par;
4489 if (is_end_par)
4490 {
4491 /* When called with a negative line count, break at the
4492 * end of the paragraph. */
4493 if (line_count < 0)
4494 break;
4495 first_par_line = TRUE;
4496 }
4497 force_format = FALSE;
4498 }
4499
4500 /*
4501 * When still in same paragraph, join the lines together. But
4502 * first delete the comment leader from the second line.
4503 */
4504 if (!is_end_par)
4505 {
4506 advance = FALSE;
4507 curwin->w_cursor.lnum++;
4508 curwin->w_cursor.col = 0;
4509 if (line_count < 0 && u_save_cursor() == FAIL)
4510 break;
4511#ifdef FEAT_COMMENTS
4512 (void)del_bytes((long)next_leader_len, FALSE);
4513 if (next_leader_len > 0)
4514 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4515 (long)-next_leader_len);
4516#endif
4517 curwin->w_cursor.lnum--;
4518 if (do_join(TRUE) == FAIL)
4519 {
4520 beep_flush();
4521 break;
4522 }
4523 first_par_line = FALSE;
4524 /* If the line is getting long, format it next time */
4525 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4526 force_format = TRUE;
4527 else
4528 force_format = FALSE;
4529 }
4530 }
4531 line_breakcheck();
4532 }
4533}
4534
4535/*
4536 * Return TRUE if line "lnum" ends in a white character.
4537 */
4538 static int
4539ends_in_white(lnum)
4540 linenr_T lnum;
4541{
4542 char_u *s = ml_get(lnum);
4543 size_t l;
4544
4545 if (*s == NUL)
4546 return FALSE;
4547 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4548 * invocation may call function multiple times". */
4549 l = STRLEN(s) - 1;
4550 return vim_iswhite(s[l]);
4551}
4552
4553/*
4554 * Blank lines, and lines containing only the comment leader, are left
4555 * untouched by the formatting. The function returns TRUE in this
4556 * case. It also returns TRUE when a line starts with the end of a comment
4557 * ('e' in comment flags), so that this line is skipped, and not joined to the
4558 * previous line. A new paragraph starts after a blank line, or when the
4559 * comment leader changes -- webb.
4560 */
4561#ifdef FEAT_COMMENTS
4562 static int
4563fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4564 linenr_T lnum;
4565 int *leader_len;
4566 char_u **leader_flags;
4567 int do_comments;
4568{
4569 char_u *flags = NULL; /* init for GCC */
4570 char_u *ptr;
4571
4572 ptr = ml_get(lnum);
4573 if (do_comments)
4574 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4575 else
4576 *leader_len = 0;
4577
4578 if (*leader_len > 0)
4579 {
4580 /*
4581 * Search for 'e' flag in comment leader flags.
4582 */
4583 flags = *leader_flags;
4584 while (*flags && *flags != ':' && *flags != COM_END)
4585 ++flags;
4586 }
4587
4588 return (*skipwhite(ptr + *leader_len) == NUL
4589 || (*leader_len > 0 && *flags == COM_END)
4590 || startPS(lnum, NUL, FALSE));
4591}
4592#else
4593 static int
4594fmt_check_par(lnum)
4595 linenr_T lnum;
4596{
4597 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4598}
4599#endif
4600
4601/*
4602 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4603 * previous line is in the same paragraph. Used for auto-formatting.
4604 */
4605 int
4606paragraph_start(lnum)
4607 linenr_T lnum;
4608{
4609 char_u *p;
4610#ifdef FEAT_COMMENTS
4611 int leader_len = 0; /* leader len of current line */
4612 char_u *leader_flags = NULL; /* flags for leader of current line */
4613 int next_leader_len; /* leader len of next line */
4614 char_u *next_leader_flags; /* flags for leader of next line */
4615 int do_comments; /* format comments */
4616#endif
4617
4618 if (lnum <= 1)
4619 return TRUE; /* start of the file */
4620
4621 p = ml_get(lnum - 1);
4622 if (*p == NUL)
4623 return TRUE; /* after empty line */
4624
4625#ifdef FEAT_COMMENTS
4626 do_comments = has_format_option(FO_Q_COMS);
4627#endif
4628 if (fmt_check_par(lnum - 1
4629#ifdef FEAT_COMMENTS
4630 , &leader_len, &leader_flags, do_comments
4631#endif
4632 ))
4633 return TRUE; /* after non-paragraph line */
4634
4635 if (fmt_check_par(lnum
4636#ifdef FEAT_COMMENTS
4637 , &next_leader_len, &next_leader_flags, do_comments
4638#endif
4639 ))
4640 return TRUE; /* "lnum" is not a paragraph line */
4641
4642 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4643 return TRUE; /* missing trailing space in previous line. */
4644
4645 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4646 return TRUE; /* numbered item starts in "lnum". */
4647
4648#ifdef FEAT_COMMENTS
4649 if (!same_leader(lnum - 1, leader_len, leader_flags,
4650 next_leader_len, next_leader_flags))
4651 return TRUE; /* change of comment leader. */
4652#endif
4653
4654 return FALSE;
4655}
4656
4657#ifdef FEAT_VISUAL
4658/*
4659 * prepare a few things for block mode yank/delete/tilde
4660 *
4661 * for delete:
4662 * - textlen includes the first/last char to be (partly) deleted
4663 * - start/endspaces is the number of columns that are taken by the
4664 * first/last deleted char minus the number of columns that have to be
4665 * deleted. for yank and tilde:
4666 * - textlen includes the first/last char to be wholly yanked
4667 * - start/endspaces is the number of columns of the first/last yanked char
4668 * that are to be yanked.
4669 */
4670 static void
4671block_prep(oap, bdp, lnum, is_del)
4672 oparg_T *oap;
4673 struct block_def *bdp;
4674 linenr_T lnum;
4675 int is_del;
4676{
4677 int incr = 0;
4678 char_u *pend;
4679 char_u *pstart;
4680 char_u *line;
4681 char_u *prev_pstart;
4682 char_u *prev_pend;
4683
4684 bdp->startspaces = 0;
4685 bdp->endspaces = 0;
4686 bdp->textlen = 0;
4687 bdp->start_vcol = 0;
4688 bdp->end_vcol = 0;
4689#ifdef FEAT_VISUALEXTRA
4690 bdp->is_short = FALSE;
4691 bdp->is_oneChar = FALSE;
4692 bdp->pre_whitesp = 0;
4693 bdp->pre_whitesp_c = 0;
4694 bdp->end_char_vcols = 0;
4695#endif
4696 bdp->start_char_vcols = 0;
4697
4698 line = ml_get(lnum);
4699 pstart = line;
4700 prev_pstart = line;
4701 while (bdp->start_vcol < oap->start_vcol && *pstart)
4702 {
4703 /* Count a tab for what it's worth (if list mode not on) */
4704 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4705 bdp->start_vcol += incr;
4706#ifdef FEAT_VISUALEXTRA
4707 if (vim_iswhite(*pstart))
4708 {
4709 bdp->pre_whitesp += incr;
4710 bdp->pre_whitesp_c++;
4711 }
4712 else
4713 {
4714 bdp->pre_whitesp = 0;
4715 bdp->pre_whitesp_c = 0;
4716 }
4717#endif
4718 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004719 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721 bdp->start_char_vcols = incr;
4722 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4723 {
4724 bdp->end_vcol = bdp->start_vcol;
4725#ifdef FEAT_VISUALEXTRA
4726 bdp->is_short = TRUE;
4727#endif
4728 if (!is_del || oap->op_type == OP_APPEND)
4729 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4730 }
4731 else
4732 {
4733 /* notice: this converts partly selected Multibyte characters to
4734 * spaces, too. */
4735 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4736 if (is_del && bdp->startspaces)
4737 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4738 pend = pstart;
4739 bdp->end_vcol = bdp->start_vcol;
4740 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4741 {
4742#ifdef FEAT_VISUALEXTRA
4743 bdp->is_oneChar = TRUE;
4744#endif
4745 if (oap->op_type == OP_INSERT)
4746 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4747 else if (oap->op_type == OP_APPEND)
4748 {
4749 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4750 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4751 }
4752 else
4753 {
4754 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4755 if (is_del && oap->op_type != OP_LSHIFT)
4756 {
4757 /* just putting the sum of those two into
4758 * bdp->startspaces doesn't work for Visual replace,
4759 * so we have to split the tab in two */
4760 bdp->startspaces = bdp->start_char_vcols
4761 - (bdp->start_vcol - oap->start_vcol);
4762 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4763 }
4764 }
4765 }
4766 else
4767 {
4768 prev_pend = pend;
4769 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4770 {
4771 /* Count a tab for what it's worth (if list mode not on) */
4772 prev_pend = pend;
4773 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4774 bdp->end_vcol += incr;
4775 }
4776 if (bdp->end_vcol <= oap->end_vcol
4777 && (!is_del
4778 || oap->op_type == OP_APPEND
4779 || oap->op_type == OP_REPLACE)) /* line too short */
4780 {
4781#ifdef FEAT_VISUALEXTRA
4782 bdp->is_short = TRUE;
4783#endif
4784 /* Alternative: include spaces to fill up the block.
4785 * Disadvantage: can lead to trailing spaces when the line is
4786 * short where the text is put */
4787 /* if (!is_del || oap->op_type == OP_APPEND) */
4788 if (oap->op_type == OP_APPEND || virtual_op)
4789 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004790 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
4792 bdp->endspaces = 0; /* replace doesn't add characters */
4793 }
4794 else if (bdp->end_vcol > oap->end_vcol)
4795 {
4796 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4797 if (!is_del && bdp->endspaces)
4798 {
4799 bdp->endspaces = incr - bdp->endspaces;
4800 if (pend != pstart)
4801 pend = prev_pend;
4802 }
4803 }
4804 }
4805#ifdef FEAT_VISUALEXTRA
4806 bdp->end_char_vcols = incr;
4807#endif
4808 if (is_del && bdp->startspaces)
4809 pstart = prev_pstart;
4810 bdp->textlen = (int)(pend - pstart);
4811 }
4812 bdp->textcol = (colnr_T) (pstart - line);
4813 bdp->textstart = pstart;
4814}
4815#endif /* FEAT_VISUAL */
4816
4817#ifdef FEAT_RIGHTLEFT
4818static void reverse_line __ARGS((char_u *s));
4819
4820 static void
4821reverse_line(s)
4822 char_u *s;
4823{
4824 int i, j;
4825 char_u c;
4826
4827 if ((i = (int)STRLEN(s) - 1) <= 0)
4828 return;
4829
4830 curwin->w_cursor.col = i - curwin->w_cursor.col;
4831 for (j = 0; j < i; j++, i--)
4832 {
4833 c = s[i]; s[i] = s[j]; s[j] = c;
4834 }
4835}
4836
4837# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4838#else
4839# define RLADDSUBFIX(ptr)
4840#endif
4841
4842/*
4843 * add or subtract 'Prenum1' from a number in a line
4844 * 'command' is CTRL-A for add, CTRL-X for subtract
4845 *
4846 * return FAIL for failure, OK otherwise
4847 */
4848 int
4849do_addsub(command, Prenum1)
4850 int command;
4851 linenr_T Prenum1;
4852{
4853 int col;
4854 char_u *buf1;
4855 char_u buf2[NUMBUFLEN];
4856 int hex; /* 'X' or 'x': hex; '0': octal */
4857 static int hexupper = FALSE; /* 0xABC */
4858 long_u n;
4859 long_u oldn;
4860 char_u *ptr;
4861 int c;
4862 int length = 0; /* character length of the number */
4863 int todel;
4864 int dohex;
4865 int dooct;
4866 int doalp;
4867 int firstdigit;
4868 int negative;
4869 int subtract;
4870
4871 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
4872 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
4873 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
4874
4875 ptr = ml_get_curline();
4876 RLADDSUBFIX(ptr);
4877
4878 /*
4879 * First check if we are on a hexadecimal number, after the "0x".
4880 */
4881 col = curwin->w_cursor.col;
4882 if (dohex)
4883 while (col > 0 && vim_isxdigit(ptr[col]))
4884 --col;
4885 if ( dohex
4886 && col > 0
4887 && (ptr[col] == 'X'
4888 || ptr[col] == 'x')
4889 && ptr[col - 1] == '0'
4890 && vim_isxdigit(ptr[col + 1]))
4891 {
4892 /*
4893 * Found hexadecimal number, move to its start.
4894 */
4895 --col;
4896 }
4897 else
4898 {
4899 /*
4900 * Search forward and then backward to find the start of number.
4901 */
4902 col = curwin->w_cursor.col;
4903
4904 while (ptr[col] != NUL
4905 && !vim_isdigit(ptr[col])
4906 && !(doalp && ASCII_ISALPHA(ptr[col])))
4907 ++col;
4908
4909 while (col > 0
4910 && vim_isdigit(ptr[col - 1])
4911 && !(doalp && ASCII_ISALPHA(ptr[col])))
4912 --col;
4913 }
4914
4915 /* truncate to max length of a number */
4916 if (length >= NUMBUFLEN - 1)
4917 length = NUMBUFLEN - 2;
4918
4919 /*
4920 * If a number was found, and saving for undo works, replace the number.
4921 */
4922 firstdigit = ptr[col];
4923 RLADDSUBFIX(ptr);
4924 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
4925 || u_save_cursor() != OK)
4926 {
4927 beep_flush();
4928 return FAIL;
4929 }
4930
4931 /* get ptr again, because u_save() may have changed it */
4932 ptr = ml_get_curline();
4933 RLADDSUBFIX(ptr);
4934
4935 if (doalp && ASCII_ISALPHA(firstdigit))
4936 {
4937 /* decrement or increment alphabetic character */
4938 if (command == Ctrl_X)
4939 {
4940 if (CharOrd(firstdigit) < Prenum1)
4941 {
4942 if (isupper(firstdigit))
4943 firstdigit = 'A';
4944 else
4945 firstdigit = 'a';
4946 }
4947 else
4948#ifdef EBCDIC
4949 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
4950#else
4951 firstdigit -= Prenum1;
4952#endif
4953 }
4954 else
4955 {
4956 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
4957 {
4958 if (isupper(firstdigit))
4959 firstdigit = 'Z';
4960 else
4961 firstdigit = 'z';
4962 }
4963 else
4964#ifdef EBCDIC
4965 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
4966#else
4967 firstdigit += Prenum1;
4968#endif
4969 }
4970 curwin->w_cursor.col = col;
4971 (void)del_char(FALSE);
4972 ins_char(firstdigit);
4973 }
4974 else
4975 {
4976 negative = FALSE;
4977 if (col > 0 && ptr[col - 1] == '-') /* negative number */
4978 {
4979 --col;
4980 negative = TRUE;
4981 }
4982
4983 /* get the number value (unsigned) */
4984 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
4985
4986 /* ignore leading '-' for hex and octal numbers */
4987 if (hex && negative)
4988 {
4989 ++col;
4990 --length;
4991 negative = FALSE;
4992 }
4993
4994 /* add or subtract */
4995 subtract = FALSE;
4996 if (command == Ctrl_X)
4997 subtract ^= TRUE;
4998 if (negative)
4999 subtract ^= TRUE;
5000
5001 oldn = n;
5002 if (subtract)
5003 n -= (unsigned long)Prenum1;
5004 else
5005 n += (unsigned long)Prenum1;
5006
5007 /* handle wraparound for decimal numbers */
5008 if (!hex)
5009 {
5010 if (subtract)
5011 {
5012 if (n > oldn)
5013 {
5014 n = 1 + (n ^ (unsigned long)-1);
5015 negative ^= TRUE;
5016 }
5017 }
5018 else /* add */
5019 {
5020 if (n < oldn)
5021 {
5022 n = (n ^ (unsigned long)-1);
5023 negative ^= TRUE;
5024 }
5025 }
5026 if (n == 0)
5027 negative = FALSE;
5028 }
5029
5030 /*
5031 * Delete the old number.
5032 */
5033 curwin->w_cursor.col = col;
5034 todel = length;
5035 c = gchar_cursor();
5036 /*
5037 * Don't include the '-' in the length, only the length of the part
5038 * after it is kept the same.
5039 */
5040 if (c == '-')
5041 --length;
5042 while (todel-- > 0)
5043 {
5044 if (c < 0x100 && isalpha(c))
5045 {
5046 if (isupper(c))
5047 hexupper = TRUE;
5048 else
5049 hexupper = FALSE;
5050 }
5051 /* del_char() will mark line needing displaying */
5052 (void)del_char(FALSE);
5053 c = gchar_cursor();
5054 }
5055
5056 /*
5057 * Prepare the leading characters in buf1[].
5058 * When there are many leading zeros it could be very long. Allocate
5059 * a bit too much.
5060 */
5061 buf1 = alloc((unsigned)length + NUMBUFLEN);
5062 if (buf1 == NULL)
5063 return FAIL;
5064 ptr = buf1;
5065 if (negative)
5066 {
5067 *ptr++ = '-';
5068 }
5069 if (hex)
5070 {
5071 *ptr++ = '0';
5072 --length;
5073 }
5074 if (hex == 'x' || hex == 'X')
5075 {
5076 *ptr++ = hex;
5077 --length;
5078 }
5079
5080 /*
5081 * Put the number characters in buf2[].
5082 */
5083 if (hex == 0)
5084 sprintf((char *)buf2, "%lu", n);
5085 else if (hex == '0')
5086 sprintf((char *)buf2, "%lo", n);
5087 else if (hex && hexupper)
5088 sprintf((char *)buf2, "%lX", n);
5089 else
5090 sprintf((char *)buf2, "%lx", n);
5091 length -= (int)STRLEN(buf2);
5092
5093 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005094 * Adjust number of zeros to the new number of digits, so the
5095 * total length of the number remains the same.
5096 * Don't do this when
5097 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005099 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 while (length-- > 0)
5101 *ptr++ = '0';
5102 *ptr = NUL;
5103 STRCAT(buf1, buf2);
5104 ins_str(buf1); /* insert the new number */
5105 vim_free(buf1);
5106 }
5107 --curwin->w_cursor.col;
5108 curwin->w_set_curswant = TRUE;
5109#ifdef FEAT_RIGHTLEFT
5110 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5111 RLADDSUBFIX(ptr);
5112#endif
5113 return OK;
5114}
5115
5116#ifdef FEAT_VIMINFO
5117 int
5118read_viminfo_register(virp, force)
5119 vir_T *virp;
5120 int force;
5121{
5122 int eof;
5123 int do_it = TRUE;
5124 int size;
5125 int limit;
5126 int i;
5127 int set_prev = FALSE;
5128 char_u *str;
5129 char_u **array = NULL;
5130
5131 /* We only get here (hopefully) if line[0] == '"' */
5132 str = virp->vir_line + 1;
5133 if (*str == '"')
5134 {
5135 set_prev = TRUE;
5136 str++;
5137 }
5138 if (!ASCII_ISALNUM(*str) && *str != '-')
5139 {
5140 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5141 return TRUE; /* too many errors, pretend end-of-file */
5142 do_it = FALSE;
5143 }
5144 get_yank_register(*str++, FALSE);
5145 if (!force && y_current->y_array != NULL)
5146 do_it = FALSE;
5147 size = 0;
5148 limit = 100; /* Optimized for registers containing <= 100 lines */
5149 if (do_it)
5150 {
5151 if (set_prev)
5152 y_previous = y_current;
5153 vim_free(y_current->y_array);
5154 array = y_current->y_array =
5155 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5156 str = skipwhite(str);
5157 if (STRNCMP(str, "CHAR", 4) == 0)
5158 y_current->y_type = MCHAR;
5159#ifdef FEAT_VISUAL
5160 else if (STRNCMP(str, "BLOCK", 5) == 0)
5161 y_current->y_type = MBLOCK;
5162#endif
5163 else
5164 y_current->y_type = MLINE;
5165 /* get the block width; if it's missing we get a zero, which is OK */
5166 str = skipwhite(skiptowhite(str));
5167#ifdef FEAT_VISUAL
5168 y_current->y_width = getdigits(&str);
5169#else
5170 (void)getdigits(&str);
5171#endif
5172 }
5173
5174 while (!(eof = viminfo_readline(virp))
5175 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5176 {
5177 if (do_it)
5178 {
5179 if (size >= limit)
5180 {
5181 y_current->y_array = (char_u **)
5182 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5183 for (i = 0; i < limit; i++)
5184 y_current->y_array[i] = array[i];
5185 vim_free(array);
5186 limit *= 2;
5187 array = y_current->y_array;
5188 }
5189 str = viminfo_readstring(virp, 1, TRUE);
5190 if (str != NULL)
5191 array[size++] = str;
5192 else
5193 do_it = FALSE;
5194 }
5195 }
5196 if (do_it)
5197 {
5198 if (size == 0)
5199 {
5200 vim_free(array);
5201 y_current->y_array = NULL;
5202 }
5203 else if (size < limit)
5204 {
5205 y_current->y_array =
5206 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5207 for (i = 0; i < size; i++)
5208 y_current->y_array[i] = array[i];
5209 vim_free(array);
5210 }
5211 y_current->y_size = size;
5212 }
5213 return eof;
5214}
5215
5216 void
5217write_viminfo_registers(fp)
5218 FILE *fp;
5219{
5220 int i, j;
5221 char_u *type;
5222 char_u c;
5223 int num_lines;
5224 int max_num_lines;
5225 int max_kbyte;
5226 long len;
5227
5228 fprintf(fp, _("\n# Registers:\n"));
5229
5230 /* Get '<' value, use old '"' value if '<' is not found. */
5231 max_num_lines = get_viminfo_parameter('<');
5232 if (max_num_lines < 0)
5233 max_num_lines = get_viminfo_parameter('"');
5234 if (max_num_lines == 0)
5235 return;
5236 max_kbyte = get_viminfo_parameter('s');
5237 if (max_kbyte == 0)
5238 return;
5239 for (i = 0; i < NUM_REGISTERS; i++)
5240 {
5241 if (y_regs[i].y_array == NULL)
5242 continue;
5243#ifdef FEAT_CLIPBOARD
5244 /* Skip '*'/'+' register, we don't want them back next time */
5245 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5246 continue;
5247#endif
5248#ifdef FEAT_DND
5249 /* Neither do we want the '~' register */
5250 if (i == TILDE_REGISTER)
5251 continue;
5252#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005253 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005255 if (num_lines == 0
5256 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5257 && STRLEN(y_regs[i].y_array[0]) == 0))
5258 continue;
5259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (max_kbyte > 0)
5261 {
5262 /* Skip register if there is more text than the maximum size. */
5263 len = 0;
5264 for (j = 0; j < num_lines; j++)
5265 len += STRLEN(y_regs[i].y_array[j]) + 1L;
5266 if (len > (long)max_kbyte * 1024L)
5267 continue;
5268 }
5269
5270 switch (y_regs[i].y_type)
5271 {
5272 case MLINE:
5273 type = (char_u *)"LINE";
5274 break;
5275 case MCHAR:
5276 type = (char_u *)"CHAR";
5277 break;
5278#ifdef FEAT_VISUAL
5279 case MBLOCK:
5280 type = (char_u *)"BLOCK";
5281 break;
5282#endif
5283 default:
5284 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005285 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 emsg(IObuff);
5287 type = (char_u *)"LINE";
5288 break;
5289 }
5290 if (y_previous == &y_regs[i])
5291 fprintf(fp, "\"");
5292 c = get_register_name(i);
5293 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5294#ifdef FEAT_VISUAL
5295 (int)y_regs[i].y_width
5296#else
5297 0
5298#endif
5299 );
5300
5301 /* If max_num_lines < 0, then we save ALL the lines in the register */
5302 if (max_num_lines > 0 && num_lines > max_num_lines)
5303 num_lines = max_num_lines;
5304 for (j = 0; j < num_lines; j++)
5305 {
5306 putc('\t', fp);
5307 viminfo_writestring(fp, y_regs[i].y_array[j]);
5308 }
5309 }
5310}
5311#endif /* FEAT_VIMINFO */
5312
5313#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5314/*
5315 * SELECTION / PRIMARY ('*')
5316 *
5317 * Text selection stuff that uses the GUI selection register '*'. When using a
5318 * GUI this may be text from another window, otherwise it is the last text we
5319 * had highlighted with VIsual mode. With mouse support, clicking the middle
5320 * button performs the paste, otherwise you will need to do <"*p>. "
5321 * If not under X, it is synonymous with the clipboard register '+'.
5322 *
5323 * X CLIPBOARD ('+')
5324 *
5325 * Text selection stuff that uses the GUI clipboard register '+'.
5326 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5327 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5328 * otherwise you will need to do <"+p>. "
5329 * If not under X, it is synonymous with the selection register '*'.
5330 */
5331
5332/*
5333 * Routine to export any final X selection we had to the environment
5334 * so that the text is still available after vim has exited. X selections
5335 * only exist while the owning application exists, so we write to the
5336 * permanent (while X runs) store CUT_BUFFER0.
5337 * Dump the CLIPBOARD selection if we own it (it's logically the more
5338 * 'permanent' of the two), otherwise the PRIMARY one.
5339 * For now, use a hard-coded sanity limit of 1Mb of data.
5340 */
5341#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5342 void
5343x11_export_final_selection()
5344{
5345 Display *dpy;
5346 char_u *str = NULL;
5347 long_u len = 0;
5348 int motion_type = -1;
5349
5350# ifdef FEAT_GUI
5351 if (gui.in_use)
5352 dpy = X_DISPLAY;
5353 else
5354# endif
5355# ifdef FEAT_XCLIPBOARD
5356 dpy = xterm_dpy;
5357# else
5358 return;
5359# endif
5360
5361 /* Get selection to export */
5362 if (clip_plus.owned)
5363 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5364 else if (clip_star.owned)
5365 motion_type = clip_convert_selection(&str, &len, &clip_star);
5366
5367 /* Check it's OK */
5368 if (dpy != NULL && str != NULL && motion_type >= 0
5369 && len < 1024*1024 && len > 0)
5370 {
5371 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5372 XFlush(dpy);
5373 }
5374
5375 vim_free(str);
5376}
5377#endif
5378
5379 void
5380clip_free_selection(cbd)
5381 VimClipboard *cbd;
5382{
5383 struct yankreg *y_ptr = y_current;
5384
5385 if (cbd == &clip_plus)
5386 y_current = &y_regs[PLUS_REGISTER];
5387 else
5388 y_current = &y_regs[STAR_REGISTER];
5389 free_yank_all();
5390 y_current->y_size = 0;
5391 y_current = y_ptr;
5392}
5393
5394/*
5395 * Get the selected text and put it in the gui selection register '*' or '+'.
5396 */
5397 void
5398clip_get_selection(cbd)
5399 VimClipboard *cbd;
5400{
5401 struct yankreg *old_y_previous, *old_y_current;
5402 pos_T old_cursor;
5403#ifdef FEAT_VISUAL
5404 pos_T old_visual;
5405 int old_visual_mode;
5406#endif
5407 colnr_T old_curswant;
5408 int old_set_curswant;
5409 pos_T old_op_start, old_op_end;
5410 oparg_T oa;
5411 cmdarg_T ca;
5412
5413 if (cbd->owned)
5414 {
5415 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5416 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5417 return;
5418
5419 /* Get the text between clip_star.start & clip_star.end */
5420 old_y_previous = y_previous;
5421 old_y_current = y_current;
5422 old_cursor = curwin->w_cursor;
5423 old_curswant = curwin->w_curswant;
5424 old_set_curswant = curwin->w_set_curswant;
5425 old_op_start = curbuf->b_op_start;
5426 old_op_end = curbuf->b_op_end;
5427#ifdef FEAT_VISUAL
5428 old_visual = VIsual;
5429 old_visual_mode = VIsual_mode;
5430#endif
5431 clear_oparg(&oa);
5432 oa.regname = (cbd == &clip_plus ? '+' : '*');
5433 oa.op_type = OP_YANK;
5434 vim_memset(&ca, 0, sizeof(ca));
5435 ca.oap = &oa;
5436 ca.cmdchar = 'y';
5437 ca.count1 = 1;
5438 ca.retval = CA_NO_ADJ_OP_END;
5439 do_pending_operator(&ca, 0, TRUE);
5440 y_previous = old_y_previous;
5441 y_current = old_y_current;
5442 curwin->w_cursor = old_cursor;
5443 curwin->w_curswant = old_curswant;
5444 curwin->w_set_curswant = old_set_curswant;
5445 curbuf->b_op_start = old_op_start;
5446 curbuf->b_op_end = old_op_end;
5447#ifdef FEAT_VISUAL
5448 VIsual = old_visual;
5449 VIsual_mode = old_visual_mode;
5450#endif
5451 }
5452 else
5453 {
5454 clip_free_selection(cbd);
5455
5456 /* Try to get selected text from another window */
5457 clip_gen_request_selection(cbd);
5458 }
5459}
5460
5461/* Convert from the GUI selection string into the '*'/'+' register */
5462 void
5463clip_yank_selection(type, str, len, cbd)
5464 int type;
5465 char_u *str;
5466 long len;
5467 VimClipboard *cbd;
5468{
5469 struct yankreg *y_ptr;
5470
5471 if (cbd == &clip_plus)
5472 y_ptr = &y_regs[PLUS_REGISTER];
5473 else
5474 y_ptr = &y_regs[STAR_REGISTER];
5475
5476 clip_free_selection(cbd);
5477
5478 str_to_reg(y_ptr, type, str, len, 0L);
5479}
5480
5481/*
5482 * Convert the '*'/'+' register into a GUI selection string returned in *str
5483 * with length *len.
5484 * Returns the motion type, or -1 for failure.
5485 */
5486 int
5487clip_convert_selection(str, len, cbd)
5488 char_u **str;
5489 long_u *len;
5490 VimClipboard *cbd;
5491{
5492 char_u *p;
5493 int lnum;
5494 int i, j;
5495 int_u eolsize;
5496 struct yankreg *y_ptr;
5497
5498 if (cbd == &clip_plus)
5499 y_ptr = &y_regs[PLUS_REGISTER];
5500 else
5501 y_ptr = &y_regs[STAR_REGISTER];
5502
5503#ifdef USE_CRNL
5504 eolsize = 2;
5505#else
5506 eolsize = 1;
5507#endif
5508
5509 *str = NULL;
5510 *len = 0;
5511 if (y_ptr->y_array == NULL)
5512 return -1;
5513
5514 for (i = 0; i < y_ptr->y_size; i++)
5515 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5516
5517 /*
5518 * Don't want newline character at end of last line if we're in MCHAR mode.
5519 */
5520 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5521 *len -= eolsize;
5522
5523 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5524 if (p == NULL)
5525 return -1;
5526 lnum = 0;
5527 for (i = 0, j = 0; i < (int)*len; i++, j++)
5528 {
5529 if (y_ptr->y_array[lnum][j] == '\n')
5530 p[i] = NUL;
5531 else if (y_ptr->y_array[lnum][j] == NUL)
5532 {
5533#ifdef USE_CRNL
5534 p[i++] = '\r';
5535#endif
5536#ifdef USE_CR
5537 p[i] = '\r';
5538#else
5539 p[i] = '\n';
5540#endif
5541 lnum++;
5542 j = -1;
5543 }
5544 else
5545 p[i] = y_ptr->y_array[lnum][j];
5546 }
5547 return y_ptr->y_type;
5548}
5549
5550
5551# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5552/*
5553 * If we have written to a clipboard register, send the text to the clipboard.
5554 */
5555 static void
5556may_set_selection()
5557{
5558 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5559 {
5560 clip_own_selection(&clip_star);
5561 clip_gen_set_selection(&clip_star);
5562 }
5563 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5564 {
5565 clip_own_selection(&clip_plus);
5566 clip_gen_set_selection(&clip_plus);
5567 }
5568}
5569# endif
5570
5571#endif /* FEAT_CLIPBOARD || PROTO */
5572
5573
5574#if defined(FEAT_DND) || defined(PROTO)
5575/*
5576 * Replace the contents of the '~' register with str.
5577 */
5578 void
5579dnd_yank_drag_data(str, len)
5580 char_u *str;
5581 long len;
5582{
5583 struct yankreg *curr;
5584
5585 curr = y_current;
5586 y_current = &y_regs[TILDE_REGISTER];
5587 free_yank_all();
5588 str_to_reg(y_current, MCHAR, str, len, 0L);
5589 y_current = curr;
5590}
5591#endif
5592
5593
5594#if defined(FEAT_EVAL) || defined(PROTO)
5595/*
5596 * Return the type of a register.
5597 * Used for getregtype()
5598 * Returns MAUTO for error.
5599 */
5600 char_u
5601get_reg_type(regname, reglen)
5602 int regname;
5603 long *reglen;
5604{
5605 switch (regname)
5606 {
5607 case '%': /* file name */
5608 case '#': /* alternate file name */
5609 case '=': /* expression */
5610 case ':': /* last command line */
5611 case '/': /* last search-pattern */
5612 case '.': /* last inserted text */
5613#ifdef FEAT_SEARCHPATH
5614 case Ctrl_F: /* Filename under cursor */
5615 case Ctrl_P: /* Path under cursor, expand via "path" */
5616#endif
5617 case Ctrl_W: /* word under cursor */
5618 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5619 case '_': /* black hole: always empty */
5620 return MCHAR;
5621 }
5622
5623#ifdef FEAT_CLIPBOARD
5624 regname = may_get_selection(regname);
5625#endif
5626
5627 /* Should we check for a valid name? */
5628 get_yank_register(regname, FALSE);
5629
5630 if (y_current->y_array != NULL)
5631 {
5632#ifdef FEAT_VISUAL
5633 if (reglen != NULL && y_current->y_type == MBLOCK)
5634 *reglen = y_current->y_width;
5635#endif
5636 return y_current->y_type;
5637 }
5638 return MAUTO;
5639}
5640
5641/*
5642 * Return the contents of a register as a single allocated string.
5643 * Used for "@r" in expressions and for getreg().
5644 * Returns NULL for error.
5645 */
5646 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00005647get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00005649 int allowexpr; /* allow "=" register */
5650 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 long i;
5653 char_u *retval;
5654 int allocated;
5655 long len;
5656
5657 /* Don't allow using an expression register inside an expression */
5658 if (regname == '=')
5659 {
5660 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00005661 {
5662 if (expr_src)
5663 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00005665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 return NULL;
5667 }
5668
5669 if (regname == '@') /* "@@" is used for unnamed register */
5670 regname = '"';
5671
5672 /* check for valid regname */
5673 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5674 return NULL;
5675
5676#ifdef FEAT_CLIPBOARD
5677 regname = may_get_selection(regname);
5678#endif
5679
5680 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5681 {
5682 if (retval == NULL)
5683 return NULL;
5684 if (!allocated)
5685 retval = vim_strsave(retval);
5686 return retval;
5687 }
5688
5689 get_yank_register(regname, FALSE);
5690 if (y_current->y_array == NULL)
5691 return NULL;
5692
5693 /*
5694 * Compute length of resulting string.
5695 */
5696 len = 0;
5697 for (i = 0; i < y_current->y_size; ++i)
5698 {
5699 len += (long)STRLEN(y_current->y_array[i]);
5700 /*
5701 * Insert a newline between lines and after last line if
5702 * y_type is MLINE.
5703 */
5704 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5705 ++len;
5706 }
5707
5708 retval = lalloc(len + 1, TRUE);
5709
5710 /*
5711 * Copy the lines of the yank register into the string.
5712 */
5713 if (retval != NULL)
5714 {
5715 len = 0;
5716 for (i = 0; i < y_current->y_size; ++i)
5717 {
5718 STRCPY(retval + len, y_current->y_array[i]);
5719 len += (long)STRLEN(retval + len);
5720
5721 /*
5722 * Insert a NL between lines and after the last line if y_type is
5723 * MLINE.
5724 */
5725 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5726 retval[len++] = '\n';
5727 }
5728 retval[len] = NUL;
5729 }
5730
5731 return retval;
5732}
5733
5734/*
5735 * Store string "str" in register "name".
5736 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5737 * If "must_append" is TRUE, always append to the register. Otherwise append
5738 * if "name" is an uppercase letter.
5739 * Note: "maxlen" and "must_append" don't work for the "/" register.
5740 * Careful: 'str' is modified, you may have to use a copy!
5741 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5742 */
5743 void
5744write_reg_contents(name, str, maxlen, must_append)
5745 int name;
5746 char_u *str;
5747 int maxlen;
5748 int must_append;
5749{
5750 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5751}
5752
5753 void
5754write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5755 int name;
5756 char_u *str;
5757 int maxlen;
5758 int must_append;
5759 int yank_type;
5760 long block_len;
5761{
5762 struct yankreg *old_y_previous, *old_y_current;
5763 long len;
5764
Bram Moolenaare7566042005-06-17 22:00:15 +00005765 if (maxlen >= 0)
5766 len = maxlen;
5767 else
5768 len = (long)STRLEN(str);
5769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 /* Special case: '/' search pattern */
5771 if (name == '/')
5772 {
5773 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5774 return;
5775 }
5776
Bram Moolenaare7566042005-06-17 22:00:15 +00005777#ifdef FEAT_EVAL
5778 if (name == '=')
5779 {
5780 char_u *p, *s;
5781
5782 p = vim_strnsave(str, (int)len);
5783 if (p == NULL)
5784 return;
5785 if (must_append)
5786 {
5787 s = concat_str(get_expr_line_src(), p);
5788 vim_free(p);
5789 p = s;
5790
5791 }
5792 set_expr_line(p);
5793 return;
5794 }
5795#endif
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5798 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005799 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 return;
5801 }
5802
5803 if (name == '_') /* black hole: nothing to do */
5804 return;
5805
5806 /* Don't want to change the current (unnamed) register */
5807 old_y_previous = y_previous;
5808 old_y_current = y_current;
5809
5810 get_yank_register(name, TRUE);
5811 if (!y_append && !must_append)
5812 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#ifndef FEAT_VISUAL
5814 /* Just in case - make sure we don't use MBLOCK */
5815 if (yank_type == MBLOCK)
5816 yank_type = MAUTO;
5817#endif
5818 if (yank_type == MAUTO)
5819 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5820 ? MLINE : MCHAR);
5821 str_to_reg(y_current, yank_type, str, len, block_len);
5822
5823# ifdef FEAT_CLIPBOARD
5824 /* Send text of clipboard register to the clipboard. */
5825 may_set_selection();
5826# endif
5827
5828 /* ':let @" = "val"' should change the meaning of the "" register */
5829 if (name != '"')
5830 y_previous = old_y_previous;
5831 y_current = old_y_current;
5832}
5833#endif /* FEAT_EVAL */
5834
5835#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5836/*
5837 * Put a string into a register. When the register is not empty, the string
5838 * is appended.
5839 */
5840 static void
5841str_to_reg(y_ptr, type, str, len, blocklen)
5842 struct yankreg *y_ptr; /* pointer to yank register */
5843 int type; /* MCHAR, MLINE or MBLOCK */
5844 char_u *str; /* string to put in register */
5845 long len; /* length of string */
5846 long blocklen; /* width of Visual block */
5847{
5848 int lnum;
5849 long start;
5850 long i;
5851 int extra;
5852 int newlines; /* number of lines added */
5853 int extraline = 0; /* extra line at the end */
5854 int append = FALSE; /* append to last line in register */
5855 char_u *s;
5856 char_u **pp;
5857#ifdef FEAT_VISUAL
5858 long maxlen;
5859#endif
5860
5861 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
5862 y_ptr->y_size = 0;
5863
5864 /*
5865 * Count the number of lines within the string
5866 */
5867 newlines = 0;
5868 for (i = 0; i < len; i++)
5869 if (str[i] == '\n')
5870 ++newlines;
5871 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
5872 {
5873 extraline = 1;
5874 ++newlines; /* count extra newline at the end */
5875 }
5876 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
5877 {
5878 append = TRUE;
5879 --newlines; /* uncount newline when appending first line */
5880 }
5881
5882 /*
5883 * Allocate an array to hold the pointers to the new register lines.
5884 * If the register was not empty, move the existing lines to the new array.
5885 */
5886 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
5887 * sizeof(char_u *), TRUE);
5888 if (pp == NULL) /* out of memory */
5889 return;
5890 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
5891 pp[lnum] = y_ptr->y_array[lnum];
5892 vim_free(y_ptr->y_array);
5893 y_ptr->y_array = pp;
5894#ifdef FEAT_VISUAL
5895 maxlen = 0;
5896#endif
5897
5898 /*
5899 * Find the end of each line and save it into the array.
5900 */
5901 for (start = 0; start < len + extraline; start += i + 1)
5902 {
5903 for (i = start; i < len; ++i) /* find the end of the line */
5904 if (str[i] == '\n')
5905 break;
5906 i -= start; /* i is now length of line */
5907#ifdef FEAT_VISUAL
5908 if (i > maxlen)
5909 maxlen = i;
5910#endif
5911 if (append)
5912 {
5913 --lnum;
5914 extra = (int)STRLEN(y_ptr->y_array[lnum]);
5915 }
5916 else
5917 extra = 0;
5918 s = alloc((unsigned)(i + extra + 1));
5919 if (s == NULL)
5920 break;
5921 if (extra)
5922 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
5923 if (append)
5924 vim_free(y_ptr->y_array[lnum]);
5925 if (i)
5926 mch_memmove(s + extra, str + start, (size_t)i);
5927 extra += i;
5928 s[extra] = NUL;
5929 y_ptr->y_array[lnum++] = s;
5930 while (--extra >= 0)
5931 {
5932 if (*s == NUL)
5933 *s = '\n'; /* replace NUL with newline */
5934 ++s;
5935 }
5936 append = FALSE; /* only first line is appended */
5937 }
5938 y_ptr->y_type = type;
5939 y_ptr->y_size = lnum;
5940# ifdef FEAT_VISUAL
5941 if (type == MBLOCK)
5942 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
5943 else
5944 y_ptr->y_width = 0;
5945# endif
5946}
5947#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
5948
5949 void
5950clear_oparg(oap)
5951 oparg_T *oap;
5952{
5953 vim_memset(oap, 0, sizeof(oparg_T));
5954}
5955
Bram Moolenaar7c626922005-02-07 22:01:03 +00005956static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
5958/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00005959 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 *
5961 * "Words" are counted by looking for boundaries between non-space and
5962 * space characters. (it seems to produce results that match 'wc'.)
5963 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00005964 * Return value is byte count; word count for the line is added to "*wc".
5965 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 *
5967 * The function will only examine the first "limit" characters in the
5968 * line, stopping if it encounters an end-of-line (NUL byte). In that
5969 * case, eol_size will be added to the character count to account for
5970 * the size of the EOL character.
5971 */
5972 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00005973line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 char_u *line;
5975 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005976 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 long limit;
5978 int eol_size;
5979{
Bram Moolenaar7c626922005-02-07 22:01:03 +00005980 long i;
5981 long words = 0;
5982 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 int is_word = 0;
5984
Bram Moolenaar7c626922005-02-07 22:01:03 +00005985 for (i = 0; line[i] && i < limit; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 if (is_word)
5988 {
5989 if (vim_isspace(line[i]))
5990 {
5991 words++;
5992 is_word = 0;
5993 }
5994 }
5995 else if (!vim_isspace(line[i]))
5996 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005997 ++chars;
5998#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005999 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006000#else
6001 ++i;
6002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 }
6004
6005 if (is_word)
6006 words++;
6007 *wc += words;
6008
6009 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006010 if (line[i] == NUL && i < limit)
6011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006013 chars += eol_size;
6014 }
6015 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 return i;
6017}
6018
6019/*
6020 * Give some info about the position of the cursor (for "g CTRL-G").
6021 * In Visual mode, give some info about the selected region. (In this case,
6022 * the *_count_cursor variables store running totals for the selection.)
6023 */
6024 void
6025cursor_pos_info()
6026{
6027 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006028 char_u buf1[50];
6029 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006031 long byte_count = 0;
6032 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 long char_count = 0;
6034 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 long word_count = 0;
6036 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006037 int eol_size;
6038 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039#ifdef FEAT_VISUAL
6040 long line_count_selected = 0;
6041 pos_T min_pos, max_pos;
6042 oparg_T oparg;
6043 struct block_def bd;
6044#endif
6045
6046 /*
6047 * Compute the length of the file in characters.
6048 */
6049 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6050 {
6051 MSG(_(no_lines_msg));
6052 }
6053 else
6054 {
6055 if (get_fileformat(curbuf) == EOL_DOS)
6056 eol_size = 2;
6057 else
6058 eol_size = 1;
6059
6060#ifdef FEAT_VISUAL
6061 if (VIsual_active)
6062 {
6063 if (lt(VIsual, curwin->w_cursor))
6064 {
6065 min_pos = VIsual;
6066 max_pos = curwin->w_cursor;
6067 }
6068 else
6069 {
6070 min_pos = curwin->w_cursor;
6071 max_pos = VIsual;
6072 }
6073 if (*p_sel == 'e' && max_pos.col > 0)
6074 --max_pos.col;
6075
6076 if (VIsual_mode == Ctrl_V)
6077 {
6078 oparg.is_VIsual = 1;
6079 oparg.block_mode = TRUE;
6080 oparg.op_type = OP_NOP;
6081 getvcols(curwin, &min_pos, &max_pos,
6082 &oparg.start_vcol, &oparg.end_vcol);
6083 /* Swap the start, end vcol if needed */
6084 if (oparg.end_vcol < oparg.start_vcol)
6085 {
6086 oparg.end_vcol += oparg.start_vcol;
6087 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6088 oparg.end_vcol -= oparg.start_vcol;
6089 }
6090 }
6091 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6092 }
6093#endif
6094
6095 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6096 {
6097 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006098 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {
6100 ui_breakcheck();
6101 if (got_int)
6102 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006103 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105
6106#ifdef FEAT_VISUAL
6107 /* Do extra processing for VIsual mode. */
6108 if (VIsual_active
6109 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6110 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006111 char_u *s = NULL;
6112 long len = 0L;
6113
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 switch (VIsual_mode)
6115 {
6116 case Ctrl_V:
6117# ifdef FEAT_VIRTUALEDIT
6118 virtual_op = virtual_active();
6119# endif
6120 block_prep(&oparg, &bd, lnum, 0);
6121# ifdef FEAT_VIRTUALEDIT
6122 virtual_op = MAYBE;
6123# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006124 s = bd.textstart;
6125 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 break;
6127 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006128 s = ml_get(lnum);
6129 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 break;
6131 case 'v':
6132 {
6133 colnr_T start_col = (lnum == min_pos.lnum)
6134 ? min_pos.col : 0;
6135 colnr_T end_col = (lnum == max_pos.lnum)
6136 ? max_pos.col - start_col + 1 : MAXCOL;
6137
Bram Moolenaardef9e822004-12-31 20:58:58 +00006138 s = ml_get(lnum) + start_col;
6139 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
6141 break;
6142 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006143 if (s != NULL)
6144 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006145 byte_count_cursor += line_count_info(s, &word_count_cursor,
6146 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006147 if (lnum == curbuf->b_ml.ml_line_count
6148 && !curbuf->b_p_eol
6149 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006150 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006151 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 }
6154 else
6155#endif
6156 {
6157 /* In non-visual mode, check for the line the cursor is on */
6158 if (lnum == curwin->w_cursor.lnum)
6159 {
6160 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006161 char_count_cursor += char_count;
6162 byte_count_cursor = byte_count +
6163 line_count_info(ml_get(lnum),
6164 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 (long)(curwin->w_cursor.col + 1), eol_size);
6166 }
6167 }
6168 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006169 byte_count += line_count_info(ml_get(lnum), &word_count,
6170 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
6172
6173 /* Correction for when last line doesn't have an EOL. */
6174 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006175 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177#ifdef FEAT_VISUAL
6178 if (VIsual_active)
6179 {
6180 if (VIsual_mode == Ctrl_V)
6181 {
6182 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
6183 &max_pos.col);
6184 sprintf((char *)buf1, _("%ld Cols; "),
6185 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6186 }
6187 else
6188 buf1[0] = NUL;
6189
Bram Moolenaar7c626922005-02-07 22:01:03 +00006190 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006191 && char_count == byte_count)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006192 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 buf1, line_count_selected,
6194 (long)curbuf->b_ml.ml_line_count,
6195 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006196 byte_count_cursor, byte_count);
6197 else
6198 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6199 buf1, line_count_selected,
6200 (long)curbuf->b_ml.ml_line_count,
6201 word_count_cursor, word_count,
6202 char_count_cursor, char_count,
6203 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 }
6205 else
6206#endif
6207 {
6208 p = ml_get_curline();
6209 validate_virtcol();
6210 col_print(buf1, (int)curwin->w_cursor.col + 1,
6211 (int)curwin->w_virtcol + 1);
6212 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6213
Bram Moolenaar7c626922005-02-07 22:01:03 +00006214 if (char_count_cursor == byte_count_cursor
6215 && char_count == byte_count)
6216 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 +00006217 (char *)buf1, (char *)buf2,
6218 (long)curwin->w_cursor.lnum,
6219 (long)curbuf->b_ml.ml_line_count,
6220 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006221 byte_count_cursor, byte_count);
6222 else
6223 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6224 (char *)buf1, (char *)buf2,
6225 (long)curwin->w_cursor.lnum,
6226 (long)curbuf->b_ml.ml_line_count,
6227 word_count_cursor, word_count,
6228 char_count_cursor, char_count,
6229 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231
6232#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006233 byte_count = bomb_size();
6234 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006236 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#endif
6238 /* Don't shorten this message, the user asked for it. */
6239 p = p_shm;
6240 p_shm = (char_u *)"";
6241 msg(IObuff);
6242 p_shm = p;
6243 }
6244}