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