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