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