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