Mercurial > vim
annotate src/ex_cmds.c @ 2648:656e652c6579 v7.3.068
updated for version 7.3.068
Problem: Using freed memory when doing ":saveas" and an autocommand sets
'autochdir'. (Kevin Klement)
Solution: Get the value of fname again after executing autocommands.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 24 Nov 2010 17:59:32 +0100 |
parents | 94694351f592 |
children | 1a0d346695fa |
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 * ex_cmds.c: some functions for command line commands | |
12 */ | |
13 | |
1668 | 14 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
1624 | 15 # include "vimio.h" /* for mch_open(), must be before vim.h */ |
16 #endif | |
17 | |
7 | 18 #include "vim.h" |
19 #include "version.h" | |
20 | |
21 #ifdef FEAT_EX_EXTRA | |
22 static int linelen __ARGS((int *has_tab)); | |
23 #endif | |
24 static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out)); | |
25 #ifdef FEAT_VIMINFO | |
26 static char_u *viminfo_filename __ARGS((char_u *)); | |
1733 | 27 static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int flags)); |
7 | 28 static int viminfo_encoding __ARGS((vir_T *virp)); |
29 static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing)); | |
30 #endif | |
31 | |
32 static int check_overwrite __ARGS((exarg_T *eap, buf_T *buf, char_u *fname, char_u *ffname, int other)); | |
33 static int check_readonly __ARGS((int *forceit, buf_T *buf)); | |
34 #ifdef FEAT_AUTOCMD | |
35 static void delbuf_msg __ARGS((char_u *name)); | |
36 #endif | |
37 static int | |
38 #ifdef __BORLANDC__ | |
39 _RTLENTRYF | |
40 #endif | |
41 help_compare __ARGS((const void *s1, const void *s2)); | |
42 | |
43 /* | |
44 * ":ascii" and "ga". | |
45 */ | |
46 void | |
47 do_ascii(eap) | |
1876 | 48 exarg_T *eap UNUSED; |
7 | 49 { |
50 int c; | |
1784 | 51 int cval; |
7 | 52 char buf1[20]; |
53 char buf2[20]; | |
54 char_u buf3[7]; | |
55 #ifdef FEAT_MBYTE | |
714 | 56 int cc[MAX_MCO]; |
57 int ci = 0; | |
7 | 58 int len; |
59 | |
60 if (enc_utf8) | |
714 | 61 c = utfc_ptr2char(ml_get_cursor(), cc); |
7 | 62 else |
63 #endif | |
64 c = gchar_cursor(); | |
65 if (c == NUL) | |
66 { | |
67 MSG("NUL"); | |
68 return; | |
69 } | |
70 | |
71 #ifdef FEAT_MBYTE | |
72 IObuff[0] = NUL; | |
73 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) | |
74 #endif | |
75 { | |
76 if (c == NL) /* NUL is stored as NL */ | |
77 c = NUL; | |
1784 | 78 if (c == CAR && get_fileformat(curbuf) == EOL_MAC) |
79 cval = NL; /* NL is stored as CR */ | |
80 else | |
81 cval = c; | |
7 | 82 if (vim_isprintc_strict(c) && (c < ' ' |
83 #ifndef EBCDIC | |
84 || c > '~' | |
85 #endif | |
86 )) | |
87 { | |
88 transchar_nonprint(buf3, c); | |
1872 | 89 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); |
7 | 90 } |
91 else | |
92 buf1[0] = NUL; | |
93 #ifndef EBCDIC | |
94 if (c >= 0x80) | |
1872 | 95 vim_snprintf(buf2, sizeof(buf2), " <M-%s>", |
96 (char *)transchar(c & 0x7f)); | |
7 | 97 else |
98 #endif | |
99 buf2[0] = NUL; | |
274 | 100 vim_snprintf((char *)IObuff, IOSIZE, |
101 _("<%s>%s%s %d, Hex %02x, Octal %03o"), | |
1784 | 102 transchar(c), buf1, buf2, cval, cval, cval); |
7 | 103 #ifdef FEAT_MBYTE |
963 | 104 if (enc_utf8) |
105 c = cc[ci++]; | |
106 else | |
107 c = 0; | |
7 | 108 #endif |
109 } | |
110 | |
111 #ifdef FEAT_MBYTE | |
112 /* Repeat for combining characters. */ | |
113 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80))) | |
114 { | |
115 len = (int)STRLEN(IObuff); | |
116 /* This assumes every multi-byte char is printable... */ | |
117 if (len > 0) | |
118 IObuff[len++] = ' '; | |
119 IObuff[len++] = '<'; | |
963 | 120 if (enc_utf8 && utf_iscomposing(c) |
625 | 121 # ifdef USE_GUI |
7 | 122 && !gui.in_use |
625 | 123 # endif |
7 | 124 ) |
125 IObuff[len++] = ' '; /* draw composing char on top of a space */ | |
274 | 126 len += (*mb_char2bytes)(c, IObuff + len); |
127 vim_snprintf((char *)IObuff + len, IOSIZE - len, | |
7 | 128 c < 0x10000 ? _("> %d, Hex %04x, Octal %o") |
129 : _("> %d, Hex %08x, Octal %o"), c, c, c); | |
714 | 130 if (ci == MAX_MCO) |
131 break; | |
963 | 132 if (enc_utf8) |
133 c = cc[ci++]; | |
134 else | |
135 c = 0; | |
7 | 136 } |
137 #endif | |
138 | |
139 msg(IObuff); | |
140 } | |
141 | |
142 #if defined(FEAT_EX_EXTRA) || defined(PROTO) | |
143 /* | |
144 * ":left", ":center" and ":right": align text. | |
145 */ | |
146 void | |
147 ex_align(eap) | |
148 exarg_T *eap; | |
149 { | |
150 pos_T save_curpos; | |
151 int len; | |
152 int indent = 0; | |
153 int new_indent; | |
154 int has_tab; | |
155 int width; | |
156 | |
157 #ifdef FEAT_RIGHTLEFT | |
158 if (curwin->w_p_rl) | |
159 { | |
160 /* switch left and right aligning */ | |
161 if (eap->cmdidx == CMD_right) | |
162 eap->cmdidx = CMD_left; | |
163 else if (eap->cmdidx == CMD_left) | |
164 eap->cmdidx = CMD_right; | |
165 } | |
166 #endif | |
167 | |
168 width = atoi((char *)eap->arg); | |
169 save_curpos = curwin->w_cursor; | |
170 if (eap->cmdidx == CMD_left) /* width is used for new indent */ | |
171 { | |
172 if (width >= 0) | |
173 indent = width; | |
174 } | |
175 else | |
176 { | |
177 /* | |
178 * if 'textwidth' set, use it | |
179 * else if 'wrapmargin' set, use it | |
180 * if invalid value, use 80 | |
181 */ | |
182 if (width <= 0) | |
183 width = curbuf->b_p_tw; | |
184 if (width == 0 && curbuf->b_p_wm > 0) | |
185 width = W_WIDTH(curwin) - curbuf->b_p_wm; | |
186 if (width <= 0) | |
187 width = 80; | |
188 } | |
189 | |
190 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) | |
191 return; | |
192 | |
193 for (curwin->w_cursor.lnum = eap->line1; | |
194 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) | |
195 { | |
196 if (eap->cmdidx == CMD_left) /* left align */ | |
197 new_indent = indent; | |
198 else | |
199 { | |
944 | 200 has_tab = FALSE; /* avoid uninit warnings */ |
7 | 201 len = linelen(eap->cmdidx == CMD_right ? &has_tab |
202 : NULL) - get_indent(); | |
203 | |
204 if (len <= 0) /* skip blank lines */ | |
205 continue; | |
206 | |
207 if (eap->cmdidx == CMD_center) | |
208 new_indent = (width - len) / 2; | |
209 else | |
210 { | |
211 new_indent = width - len; /* right align */ | |
212 | |
213 /* | |
214 * Make sure that embedded TABs don't make the text go too far | |
215 * to the right. | |
216 */ | |
217 if (has_tab) | |
218 while (new_indent > 0) | |
219 { | |
220 (void)set_indent(new_indent, 0); | |
221 if (linelen(NULL) <= width) | |
222 { | |
223 /* | |
224 * Now try to move the line as much as possible to | |
225 * the right. Stop when it moves too far. | |
226 */ | |
227 do | |
228 (void)set_indent(++new_indent, 0); | |
229 while (linelen(NULL) <= width); | |
230 --new_indent; | |
231 break; | |
232 } | |
233 --new_indent; | |
234 } | |
235 } | |
236 } | |
237 if (new_indent < 0) | |
238 new_indent = 0; | |
239 (void)set_indent(new_indent, 0); /* set indent */ | |
240 } | |
241 changed_lines(eap->line1, 0, eap->line2 + 1, 0L); | |
242 curwin->w_cursor = save_curpos; | |
243 beginline(BL_WHITE | BL_FIX); | |
244 } | |
245 | |
246 /* | |
247 * Get the length of the current line, excluding trailing white space. | |
248 */ | |
249 static int | |
250 linelen(has_tab) | |
251 int *has_tab; | |
252 { | |
253 char_u *line; | |
254 char_u *first; | |
255 char_u *last; | |
256 int save; | |
257 int len; | |
258 | |
259 /* find the first non-blank character */ | |
260 line = ml_get_curline(); | |
261 first = skipwhite(line); | |
262 | |
263 /* find the character after the last non-blank character */ | |
264 for (last = first + STRLEN(first); | |
265 last > first && vim_iswhite(last[-1]); --last) | |
266 ; | |
267 save = *last; | |
268 *last = NUL; | |
269 len = linetabsize(line); /* get line length */ | |
270 if (has_tab != NULL) /* check for embedded TAB */ | |
271 *has_tab = (vim_strrchr(first, TAB) != NULL); | |
272 *last = save; | |
273 | |
274 return len; | |
275 } | |
276 | |
826 | 277 /* Buffer for two lines used during sorting. They are allocated to |
278 * contain the longest line being sorted. */ | |
279 static char_u *sortbuf1; | |
280 static char_u *sortbuf2; | |
284 | 281 |
282 static int sort_ic; /* ignore case */ | |
294 | 283 static int sort_nr; /* sort on number */ |
826 | 284 static int sort_rx; /* sort on regex instead of skipping it */ |
285 | |
286 static int sort_abort; /* flag to indicate if sorting has been interrupted */ | |
294 | 287 |
288 /* Struct to store info to be sorted. */ | |
289 typedef struct | |
290 { | |
291 linenr_T lnum; /* line number */ | |
826 | 292 long start_col_nr; /* starting column number or number */ |
293 long end_col_nr; /* ending column number */ | |
294 | 294 } sorti_T; |
284 | 295 |
296 static int | |
297 #ifdef __BORLANDC__ | |
298 _RTLENTRYF | |
299 #endif | |
300 sort_compare __ARGS((const void *s1, const void *s2)); | |
301 | |
302 static int | |
303 #ifdef __BORLANDC__ | |
304 _RTLENTRYF | |
305 #endif | |
306 sort_compare(s1, s2) | |
307 const void *s1; | |
308 const void *s2; | |
309 { | |
294 | 310 sorti_T l1 = *(sorti_T *)s1; |
311 sorti_T l2 = *(sorti_T *)s2; | |
826 | 312 int result = 0; |
313 | |
314 /* If the user interrupts, there's no way to stop qsort() immediately, but | |
834 | 315 * if we return 0 every time, qsort will assume it's done sorting and |
316 * exit. */ | |
826 | 317 if (sort_abort) |
318 return 0; | |
319 fast_breakcheck(); | |
320 if (got_int) | |
321 sort_abort = TRUE; | |
322 | |
834 | 323 /* When sorting numbers "start_col_nr" is the number, not the column |
324 * number. */ | |
294 | 325 if (sort_nr) |
2606 | 326 result = l1.start_col_nr == l2.start_col_nr ? 0 |
327 : l1.start_col_nr > l2.start_col_nr ? 1 : -1; | |
826 | 328 else |
329 { | |
834 | 330 /* We need to copy one line into "sortbuf1", because there is no |
331 * guarantee that the first pointer becomes invalid when obtaining the | |
332 * second one. */ | |
333 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.start_col_nr, | |
334 l1.end_col_nr - l1.start_col_nr + 1); | |
826 | 335 sortbuf1[l1.end_col_nr - l1.start_col_nr] = 0; |
834 | 336 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.start_col_nr, |
337 l2.end_col_nr - l2.start_col_nr + 1); | |
826 | 338 sortbuf2[l2.end_col_nr - l2.start_col_nr] = 0; |
339 | |
834 | 340 result = sort_ic ? STRICMP(sortbuf1, sortbuf2) |
341 : STRCMP(sortbuf1, sortbuf2); | |
342 } | |
343 | |
344 /* If two lines have the same value, preserve the original line order. */ | |
826 | 345 if (result == 0) |
834 | 346 return (int)(l1.lnum - l2.lnum); |
347 return result; | |
284 | 348 } |
349 | |
350 /* | |
351 * ":sort". | |
352 */ | |
353 void | |
354 ex_sort(eap) | |
355 exarg_T *eap; | |
356 { | |
357 regmatch_T regmatch; | |
358 int len; | |
359 linenr_T lnum; | |
360 long maxlen = 0; | |
294 | 361 sorti_T *nrs; |
1872 | 362 size_t count = (size_t)(eap->line2 - eap->line1 + 1); |
294 | 363 size_t i; |
284 | 364 char_u *p; |
365 char_u *s; | |
826 | 366 char_u *s2; |
367 char_u c; /* temporary character storage */ | |
284 | 368 int unique = FALSE; |
369 long deleted; | |
826 | 370 colnr_T start_col; |
371 colnr_T end_col; | |
294 | 372 int sort_oct; /* sort on octal number */ |
373 int sort_hex; /* sort on hex number */ | |
284 | 374 |
1538 | 375 /* Sorting one line is really quick! */ |
376 if (count <= 1) | |
377 return; | |
378 | |
284 | 379 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) |
380 return; | |
826 | 381 sortbuf1 = NULL; |
382 sortbuf2 = NULL; | |
284 | 383 regmatch.regprog = NULL; |
294 | 384 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE); |
284 | 385 if (nrs == NULL) |
826 | 386 goto sortend; |
387 | |
388 sort_abort = sort_ic = sort_rx = sort_nr = sort_oct = sort_hex = 0; | |
294 | 389 |
284 | 390 for (p = eap->arg; *p != NUL; ++p) |
391 { | |
392 if (vim_iswhite(*p)) | |
393 ; | |
394 else if (*p == 'i') | |
395 sort_ic = TRUE; | |
826 | 396 else if (*p == 'r') |
397 sort_rx = TRUE; | |
294 | 398 else if (*p == 'n') |
399 sort_nr = 2; | |
400 else if (*p == 'o') | |
401 sort_oct = 2; | |
402 else if (*p == 'x') | |
403 sort_hex = 2; | |
284 | 404 else if (*p == 'u') |
405 unique = TRUE; | |
289 | 406 else if (*p == '"') /* comment start */ |
407 break; | |
408 else if (check_nextcmd(p) != NULL) | |
409 { | |
410 eap->nextcmd = check_nextcmd(p); | |
411 break; | |
412 } | |
413 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) | |
284 | 414 { |
415 s = skip_regexp(p + 1, *p, TRUE, NULL); | |
416 if (*s != *p) | |
417 { | |
418 EMSG(_(e_invalpat)); | |
826 | 419 goto sortend; |
284 | 420 } |
421 *s = NUL; | |
1314 | 422 /* Use last search pattern if sort pattern is empty. */ |
423 if (s == p + 1 && last_search_pat() != NULL) | |
424 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); | |
425 else | |
426 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC); | |
284 | 427 if (regmatch.regprog == NULL) |
826 | 428 goto sortend; |
289 | 429 p = s; /* continue after the regexp */ |
284 | 430 regmatch.rm_ic = p_ic; |
431 } | |
432 else | |
433 { | |
434 EMSG2(_(e_invarg2), p); | |
826 | 435 goto sortend; |
284 | 436 } |
437 } | |
438 | |
294 | 439 /* Can only have one of 'n', 'o' and 'x'. */ |
440 if (sort_nr + sort_oct + sort_hex > 2) | |
441 { | |
442 EMSG(_(e_invarg)); | |
826 | 443 goto sortend; |
294 | 444 } |
445 | |
446 /* From here on "sort_nr" is used as a flag for any number sorting. */ | |
447 sort_nr += sort_oct + sort_hex; | |
448 | |
284 | 449 /* |
294 | 450 * Make an array with all line numbers. This avoids having to copy all |
284 | 451 * the lines into allocated memory. |
826 | 452 * When sorting on strings "start_col_nr" is the offset in the line, for |
453 * numbers sorting it's the number to sort on. This means the pattern | |
454 * matching and number conversion only has to be done once per line. | |
294 | 455 * Also get the longest line length for allocating "sortbuf". |
284 | 456 */ |
457 for (lnum = eap->line1; lnum <= eap->line2; ++lnum) | |
458 { | |
459 s = ml_get(lnum); | |
835 | 460 len = (int)STRLEN(s); |
284 | 461 if (maxlen < len) |
462 maxlen = len; | |
294 | 463 |
826 | 464 start_col = 0; |
465 end_col = len; | |
294 | 466 if (regmatch.regprog != NULL && vim_regexec(®match, s, 0)) |
826 | 467 { |
468 if (sort_rx) | |
469 { | |
835 | 470 start_col = (colnr_T)(regmatch.startp[0] - s); |
471 end_col = (colnr_T)(regmatch.endp[0] - s); | |
826 | 472 } |
473 else | |
835 | 474 start_col = (colnr_T)(regmatch.endp[0] - s); |
826 | 475 } |
294 | 476 else |
826 | 477 if (regmatch.regprog != NULL) |
478 end_col = 0; | |
294 | 479 |
480 if (sort_nr) | |
481 { | |
826 | 482 /* Make sure vim_str2nr doesn't read any digits past the end |
483 * of the match, by temporarily terminating the string there */ | |
484 s2 = s + end_col; | |
485 c = *s2; | |
2606 | 486 *s2 = NUL; |
294 | 487 /* Sorting on number: Store the number itself. */ |
1687 | 488 p = s + start_col; |
294 | 489 if (sort_hex) |
1687 | 490 s = skiptohex(p); |
294 | 491 else |
1687 | 492 s = skiptodigit(p); |
493 if (s > p && s[-1] == '-') | |
494 --s; /* include preceding negative sign */ | |
2606 | 495 if (*s == NUL) |
496 /* empty line should sort before any number */ | |
497 nrs[lnum - eap->line1].start_col_nr = -MAXLNUM; | |
498 else | |
499 vim_str2nr(s, NULL, NULL, sort_oct, sort_hex, | |
500 &nrs[lnum - eap->line1].start_col_nr, NULL); | |
501 *s2 = c; | |
294 | 502 } |
503 else | |
826 | 504 { |
294 | 505 /* Store the column to sort at. */ |
826 | 506 nrs[lnum - eap->line1].start_col_nr = start_col; |
507 nrs[lnum - eap->line1].end_col_nr = end_col; | |
508 } | |
294 | 509 |
510 nrs[lnum - eap->line1].lnum = lnum; | |
481 | 511 |
512 if (regmatch.regprog != NULL) | |
513 fast_breakcheck(); | |
514 if (got_int) | |
826 | 515 goto sortend; |
284 | 516 } |
517 | |
294 | 518 /* Allocate a buffer that can hold the longest line. */ |
826 | 519 sortbuf1 = alloc((unsigned)maxlen + 1); |
520 if (sortbuf1 == NULL) | |
521 goto sortend; | |
522 sortbuf2 = alloc((unsigned)maxlen + 1); | |
523 if (sortbuf2 == NULL) | |
524 goto sortend; | |
284 | 525 |
481 | 526 /* Sort the array of line numbers. Note: can't be interrupted! */ |
294 | 527 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); |
284 | 528 |
826 | 529 if (sort_abort) |
530 goto sortend; | |
531 | |
284 | 532 /* Insert the lines in the sorted order below the last one. */ |
533 lnum = eap->line2; | |
534 for (i = 0; i < count; ++i) | |
535 { | |
536 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum); | |
537 if (!unique || i == 0 | |
826 | 538 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0) |
284 | 539 { |
540 if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL) | |
541 break; | |
294 | 542 if (unique) |
826 | 543 STRCPY(sortbuf1, s); |
284 | 544 } |
481 | 545 fast_breakcheck(); |
546 if (got_int) | |
826 | 547 goto sortend; |
284 | 548 } |
549 | |
550 /* delete the original lines if appending worked */ | |
551 if (i == count) | |
552 for (i = 0; i < count; ++i) | |
553 ml_delete(eap->line1, FALSE); | |
554 else | |
555 count = 0; | |
556 | |
294 | 557 /* Adjust marks for deleted (or added) lines and prepare for displaying. */ |
835 | 558 deleted = (long)(count - (lnum - eap->line2)); |
284 | 559 if (deleted > 0) |
560 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted); | |
561 else if (deleted < 0) | |
562 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L); | |
563 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted); | |
294 | 564 |
284 | 565 curwin->w_cursor.lnum = eap->line1; |
566 beginline(BL_WHITE | BL_FIX); | |
567 | |
826 | 568 sortend: |
284 | 569 vim_free(nrs); |
826 | 570 vim_free(sortbuf1); |
571 vim_free(sortbuf2); | |
284 | 572 vim_free(regmatch.regprog); |
481 | 573 if (got_int) |
574 EMSG(_(e_interr)); | |
284 | 575 } |
576 | |
7 | 577 /* |
578 * ":retab". | |
579 */ | |
580 void | |
581 ex_retab(eap) | |
582 exarg_T *eap; | |
583 { | |
584 linenr_T lnum; | |
585 int got_tab = FALSE; | |
586 long num_spaces = 0; | |
587 long num_tabs; | |
588 long len; | |
589 long col; | |
590 long vcol; | |
591 long start_col = 0; /* For start of white-space string */ | |
592 long start_vcol = 0; /* For start of white-space string */ | |
593 int temp; | |
594 long old_len; | |
595 char_u *ptr; | |
596 char_u *new_line = (char_u *)1; /* init to non-NULL */ | |
597 int did_undo; /* called u_save for current line */ | |
598 int new_ts; | |
599 int save_list; | |
600 linenr_T first_line = 0; /* first changed line */ | |
601 linenr_T last_line = 0; /* last changed line */ | |
602 | |
603 save_list = curwin->w_p_list; | |
604 curwin->w_p_list = 0; /* don't want list mode here */ | |
605 | |
606 new_ts = getdigits(&(eap->arg)); | |
607 if (new_ts < 0) | |
608 { | |
609 EMSG(_(e_positive)); | |
610 return; | |
611 } | |
612 if (new_ts == 0) | |
613 new_ts = curbuf->b_p_ts; | |
614 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) | |
615 { | |
616 ptr = ml_get(lnum); | |
617 col = 0; | |
618 vcol = 0; | |
619 did_undo = FALSE; | |
620 for (;;) | |
621 { | |
622 if (vim_iswhite(ptr[col])) | |
623 { | |
624 if (!got_tab && num_spaces == 0) | |
625 { | |
626 /* First consecutive white-space */ | |
627 start_vcol = vcol; | |
628 start_col = col; | |
629 } | |
630 if (ptr[col] == ' ') | |
631 num_spaces++; | |
632 else | |
633 got_tab = TRUE; | |
634 } | |
635 else | |
636 { | |
637 if (got_tab || (eap->forceit && num_spaces > 1)) | |
638 { | |
639 /* Retabulate this string of white-space */ | |
640 | |
641 /* len is virtual length of white string */ | |
642 len = num_spaces = vcol - start_vcol; | |
643 num_tabs = 0; | |
644 if (!curbuf->b_p_et) | |
645 { | |
646 temp = new_ts - (start_vcol % new_ts); | |
647 if (num_spaces >= temp) | |
648 { | |
649 num_spaces -= temp; | |
650 num_tabs++; | |
651 } | |
652 num_tabs += num_spaces / new_ts; | |
653 num_spaces -= (num_spaces / new_ts) * new_ts; | |
654 } | |
655 if (curbuf->b_p_et || got_tab || | |
656 (num_spaces + num_tabs < len)) | |
657 { | |
658 if (did_undo == FALSE) | |
659 { | |
660 did_undo = TRUE; | |
661 if (u_save((linenr_T)(lnum - 1), | |
662 (linenr_T)(lnum + 1)) == FAIL) | |
663 { | |
664 new_line = NULL; /* flag out-of-memory */ | |
665 break; | |
666 } | |
667 } | |
668 | |
669 /* len is actual number of white characters used */ | |
670 len = num_spaces + num_tabs; | |
671 old_len = (long)STRLEN(ptr); | |
672 new_line = lalloc(old_len - col + start_col + len + 1, | |
673 TRUE); | |
674 if (new_line == NULL) | |
675 break; | |
676 if (start_col > 0) | |
677 mch_memmove(new_line, ptr, (size_t)start_col); | |
678 mch_memmove(new_line + start_col + len, | |
679 ptr + col, (size_t)(old_len - col + 1)); | |
680 ptr = new_line + start_col; | |
681 for (col = 0; col < len; col++) | |
682 ptr[col] = (col < num_tabs) ? '\t' : ' '; | |
683 ml_replace(lnum, new_line, FALSE); | |
684 if (first_line == 0) | |
685 first_line = lnum; | |
686 last_line = lnum; | |
687 ptr = new_line; | |
688 col = start_col + len; | |
689 } | |
690 } | |
691 got_tab = FALSE; | |
692 num_spaces = 0; | |
693 } | |
694 if (ptr[col] == NUL) | |
695 break; | |
696 vcol += chartabsize(ptr + col, (colnr_T)vcol); | |
697 #ifdef FEAT_MBYTE | |
698 if (has_mbyte) | |
474 | 699 col += (*mb_ptr2len)(ptr + col); |
7 | 700 else |
701 #endif | |
702 ++col; | |
703 } | |
704 if (new_line == NULL) /* out of memory */ | |
705 break; | |
706 line_breakcheck(); | |
707 } | |
708 if (got_int) | |
709 EMSG(_(e_interr)); | |
710 | |
711 if (curbuf->b_p_ts != new_ts) | |
712 redraw_curbuf_later(NOT_VALID); | |
713 if (first_line != 0) | |
714 changed_lines(first_line, 0, last_line + 1, 0L); | |
715 | |
716 curwin->w_p_list = save_list; /* restore 'list' */ | |
717 | |
718 curbuf->b_p_ts = new_ts; | |
719 coladvance(curwin->w_curswant); | |
720 | |
721 u_clearline(); | |
722 } | |
723 #endif | |
724 | |
725 /* | |
726 * :move command - move lines line1-line2 to line dest | |
727 * | |
728 * return FAIL for failure, OK otherwise | |
729 */ | |
730 int | |
731 do_move(line1, line2, dest) | |
732 linenr_T line1; | |
733 linenr_T line2; | |
734 linenr_T dest; | |
735 { | |
736 char_u *str; | |
737 linenr_T l; | |
738 linenr_T extra; /* Num lines added before line1 */ | |
739 linenr_T num_lines; /* Num lines moved */ | |
740 linenr_T last_line; /* Last line in file after adding new text */ | |
741 | |
742 if (dest >= line1 && dest < line2) | |
743 { | |
744 EMSG(_("E134: Move lines into themselves")); | |
745 return FAIL; | |
746 } | |
747 | |
748 num_lines = line2 - line1 + 1; | |
749 | |
750 /* | |
751 * First we copy the old text to its new location -- webb | |
752 * Also copy the flag that ":global" command uses. | |
753 */ | |
754 if (u_save(dest, dest + 1) == FAIL) | |
755 return FAIL; | |
756 for (extra = 0, l = line1; l <= line2; l++) | |
757 { | |
758 str = vim_strsave(ml_get(l + extra)); | |
759 if (str != NULL) | |
760 { | |
761 ml_append(dest + l - line1, str, (colnr_T)0, FALSE); | |
762 vim_free(str); | |
763 if (dest < line1) | |
764 extra++; | |
765 } | |
766 } | |
767 | |
768 /* | |
769 * Now we must be careful adjusting our marks so that we don't overlap our | |
770 * mark_adjust() calls. | |
771 * | |
772 * We adjust the marks within the old text so that they refer to the | |
773 * last lines of the file (temporarily), because we know no other marks | |
774 * will be set there since these line numbers did not exist until we added | |
775 * our new lines. | |
776 * | |
777 * Then we adjust the marks on lines between the old and new text positions | |
778 * (either forwards or backwards). | |
779 * | |
780 * And Finally we adjust the marks we put at the end of the file back to | |
781 * their final destination at the new text position -- webb | |
782 */ | |
783 last_line = curbuf->b_ml.ml_line_count; | |
784 mark_adjust(line1, line2, last_line - line2, 0L); | |
785 if (dest >= line2) | |
786 { | |
787 mark_adjust(line2 + 1, dest, -num_lines, 0L); | |
788 curbuf->b_op_start.lnum = dest - num_lines + 1; | |
789 curbuf->b_op_end.lnum = dest; | |
790 } | |
791 else | |
792 { | |
793 mark_adjust(dest + 1, line1 - 1, num_lines, 0L); | |
794 curbuf->b_op_start.lnum = dest + 1; | |
795 curbuf->b_op_end.lnum = dest + num_lines; | |
796 } | |
797 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
798 mark_adjust(last_line - num_lines + 1, last_line, | |
799 -(last_line - dest - extra), 0L); | |
800 | |
801 /* | |
802 * Now we delete the original text -- webb | |
803 */ | |
804 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) | |
805 return FAIL; | |
806 | |
807 for (l = line1; l <= line2; l++) | |
808 ml_delete(line1 + extra, TRUE); | |
809 | |
810 if (!global_busy && num_lines > p_report) | |
811 { | |
812 if (num_lines == 1) | |
813 MSG(_("1 line moved")); | |
814 else | |
815 smsg((char_u *)_("%ld lines moved"), num_lines); | |
816 } | |
817 | |
818 /* | |
819 * Leave the cursor on the last of the moved lines. | |
820 */ | |
821 if (dest >= line1) | |
822 curwin->w_cursor.lnum = dest; | |
823 else | |
824 curwin->w_cursor.lnum = dest + (line2 - line1) + 1; | |
825 | |
826 if (line1 < dest) | |
827 changed_lines(line1, 0, dest + num_lines + 1, 0L); | |
828 else | |
829 changed_lines(dest + 1, 0, line1 + num_lines, 0L); | |
830 | |
831 return OK; | |
832 } | |
833 | |
834 /* | |
835 * ":copy" | |
836 */ | |
837 void | |
838 ex_copy(line1, line2, n) | |
839 linenr_T line1; | |
840 linenr_T line2; | |
841 linenr_T n; | |
842 { | |
843 linenr_T count; | |
844 char_u *p; | |
845 | |
846 count = line2 - line1 + 1; | |
847 curbuf->b_op_start.lnum = n + 1; | |
848 curbuf->b_op_end.lnum = n + count; | |
849 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
850 | |
851 /* | |
852 * there are three situations: | |
853 * 1. destination is above line1 | |
854 * 2. destination is between line1 and line2 | |
855 * 3. destination is below line2 | |
856 * | |
857 * n = destination (when starting) | |
858 * curwin->w_cursor.lnum = destination (while copying) | |
859 * line1 = start of source (while copying) | |
860 * line2 = end of source (while copying) | |
861 */ | |
862 if (u_save(n, n + 1) == FAIL) | |
863 return; | |
864 | |
865 curwin->w_cursor.lnum = n; | |
866 while (line1 <= line2) | |
867 { | |
868 /* need to use vim_strsave() because the line will be unlocked within | |
869 * ml_append() */ | |
870 p = vim_strsave(ml_get(line1)); | |
871 if (p != NULL) | |
872 { | |
873 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE); | |
874 vim_free(p); | |
875 } | |
876 /* situation 2: skip already copied lines */ | |
877 if (line1 == n) | |
878 line1 = curwin->w_cursor.lnum; | |
879 ++line1; | |
880 if (curwin->w_cursor.lnum < line1) | |
881 ++line1; | |
882 if (curwin->w_cursor.lnum < line2) | |
883 ++line2; | |
884 ++curwin->w_cursor.lnum; | |
885 } | |
886 | |
887 appended_lines_mark(n, count); | |
888 | |
889 msgmore((long)count); | |
890 } | |
891 | |
359 | 892 static char_u *prevcmd = NULL; /* the previous command */ |
893 | |
894 #if defined(EXITFREE) || defined(PROTO) | |
895 void | |
896 free_prev_shellcmd() | |
897 { | |
898 vim_free(prevcmd); | |
899 } | |
900 #endif | |
901 | |
7 | 902 /* |
903 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" | |
904 * Bangs in the argument are replaced with the previously entered command. | |
905 * Remember the argument. | |
906 * | |
907 * RISCOS: Bangs only replaced when followed by a space, since many | |
908 * pathnames contain one. | |
909 */ | |
910 void | |
911 do_bang(addr_count, eap, forceit, do_in, do_out) | |
912 int addr_count; | |
913 exarg_T *eap; | |
914 int forceit; | |
915 int do_in, do_out; | |
916 { | |
917 char_u *arg = eap->arg; /* command */ | |
918 linenr_T line1 = eap->line1; /* start of range */ | |
919 linenr_T line2 = eap->line2; /* end of range */ | |
920 char_u *newcmd = NULL; /* the new command */ | |
921 int free_newcmd = FALSE; /* need to free() newcmd */ | |
922 int ins_prevcmd; | |
923 char_u *t; | |
924 char_u *p; | |
925 char_u *trailarg; | |
926 int len; | |
927 int scroll_save = msg_scroll; | |
928 | |
929 /* | |
930 * Disallow shell commands for "rvim". | |
931 * Disallow shell commands from .exrc and .vimrc in current directory for | |
932 * security reasons. | |
933 */ | |
934 if (check_restricted() || check_secure()) | |
935 return; | |
936 | |
937 if (addr_count == 0) /* :! */ | |
938 { | |
939 msg_scroll = FALSE; /* don't scroll here */ | |
940 autowrite_all(); | |
941 msg_scroll = scroll_save; | |
942 } | |
943 | |
944 /* | |
945 * Try to find an embedded bang, like in :!<cmd> ! [args] | |
946 * (:!! is indicated by the 'forceit' variable) | |
947 */ | |
948 ins_prevcmd = forceit; | |
949 trailarg = arg; | |
950 do | |
951 { | |
952 len = (int)STRLEN(trailarg) + 1; | |
953 if (newcmd != NULL) | |
954 len += (int)STRLEN(newcmd); | |
955 if (ins_prevcmd) | |
956 { | |
957 if (prevcmd == NULL) | |
958 { | |
959 EMSG(_(e_noprev)); | |
960 vim_free(newcmd); | |
961 return; | |
962 } | |
963 len += (int)STRLEN(prevcmd); | |
964 } | |
1872 | 965 if ((t = alloc((unsigned)len)) == NULL) |
7 | 966 { |
967 vim_free(newcmd); | |
968 return; | |
969 } | |
970 *t = NUL; | |
971 if (newcmd != NULL) | |
972 STRCAT(t, newcmd); | |
973 if (ins_prevcmd) | |
974 STRCAT(t, prevcmd); | |
975 p = t + STRLEN(t); | |
976 STRCAT(t, trailarg); | |
977 vim_free(newcmd); | |
978 newcmd = t; | |
979 | |
980 /* | |
981 * Scan the rest of the argument for '!', which is replaced by the | |
982 * previous command. "\!" is replaced by "!" (this is vi compatible). | |
983 */ | |
984 trailarg = NULL; | |
985 while (*p) | |
986 { | |
987 if (*p == '!' | |
988 #ifdef RISCOS | |
989 && (p[1] == ' ' || p[1] == NUL) | |
990 #endif | |
991 ) | |
992 { | |
993 if (p > newcmd && p[-1] == '\\') | |
1624 | 994 STRMOVE(p - 1, p); |
7 | 995 else |
996 { | |
997 trailarg = p; | |
998 *trailarg++ = NUL; | |
999 ins_prevcmd = TRUE; | |
1000 break; | |
1001 } | |
1002 } | |
1003 ++p; | |
1004 } | |
1005 } while (trailarg != NULL); | |
1006 | |
1007 vim_free(prevcmd); | |
1008 prevcmd = newcmd; | |
1009 | |
1010 if (bangredo) /* put cmd in redo buffer for ! command */ | |
1011 { | |
620 | 1012 AppendToRedobuffLit(prevcmd, -1); |
7 | 1013 AppendToRedobuff((char_u *)"\n"); |
1014 bangredo = FALSE; | |
1015 } | |
1016 /* | |
1017 * Add quotes around the command, for shells that need them. | |
1018 */ | |
1019 if (*p_shq != NUL) | |
1020 { | |
1021 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1)); | |
1022 if (newcmd == NULL) | |
1023 return; | |
1024 STRCPY(newcmd, p_shq); | |
1025 STRCAT(newcmd, prevcmd); | |
1026 STRCAT(newcmd, p_shq); | |
1027 free_newcmd = TRUE; | |
1028 } | |
1029 if (addr_count == 0) /* :! */ | |
1030 { | |
1031 /* echo the command */ | |
1032 msg_start(); | |
1033 msg_putchar(':'); | |
1034 msg_putchar('!'); | |
1035 msg_outtrans(newcmd); | |
1036 msg_clr_eos(); | |
1037 windgoto(msg_row, msg_col); | |
1038 | |
1039 do_shell(newcmd, 0); | |
1040 } | |
1041 else /* :range! */ | |
725 | 1042 { |
7 | 1043 /* Careful: This may recursively call do_bang() again! (because of |
1044 * autocommands) */ | |
1045 do_filter(line1, line2, eap, newcmd, do_in, do_out); | |
725 | 1046 #ifdef FEAT_AUTOCMD |
1047 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); | |
1048 #endif | |
1049 } | |
7 | 1050 if (free_newcmd) |
1051 vim_free(newcmd); | |
1052 } | |
1053 | |
1054 /* | |
1055 * do_filter: filter lines through a command given by the user | |
1056 * | |
169 | 1057 * We mostly use temp files and the call_shell() routine here. This would |
1058 * normally be done using pipes on a UNIX machine, but this is more portable | |
1059 * to non-unix machines. The call_shell() routine needs to be able | |
7 | 1060 * to deal with redirection somehow, and should handle things like looking |
1061 * at the PATH env. variable, and adding reasonable extensions to the | |
1062 * command name given by the user. All reasonable versions of call_shell() | |
1063 * do this. | |
169 | 1064 * Alternatively, if on Unix and redirecting input or output, but not both, |
1065 * and the 'shelltemp' option isn't set, use pipes. | |
7 | 1066 * We use input redirection if do_in is TRUE. |
1067 * We use output redirection if do_out is TRUE. | |
1068 */ | |
1069 static void | |
1070 do_filter(line1, line2, eap, cmd, do_in, do_out) | |
1071 linenr_T line1, line2; | |
1072 exarg_T *eap; /* for forced 'ff' and 'fenc' */ | |
1073 char_u *cmd; | |
1074 int do_in, do_out; | |
1075 { | |
1076 char_u *itmp = NULL; | |
1077 char_u *otmp = NULL; | |
1078 linenr_T linecount; | |
1079 linenr_T read_linecount; | |
1080 pos_T cursor_save; | |
1081 char_u *cmd_buf; | |
1082 #ifdef FEAT_AUTOCMD | |
1083 buf_T *old_curbuf = curbuf; | |
1084 #endif | |
169 | 1085 int shell_flags = 0; |
7 | 1086 |
1087 if (*cmd == NUL) /* no filter command */ | |
1088 return; | |
1089 | |
1090 #ifdef WIN3264 | |
1091 /* | |
1092 * Check if external commands are allowed now. | |
1093 */ | |
1094 if (can_end_termcap_mode(TRUE) == FALSE) | |
1095 return; | |
1096 #endif | |
1097 | |
1098 cursor_save = curwin->w_cursor; | |
1099 linecount = line2 - line1 + 1; | |
1100 curwin->w_cursor.lnum = line1; | |
1101 curwin->w_cursor.col = 0; | |
1102 changed_line_abv_curs(); | |
1103 invalidate_botline(); | |
1104 | |
1105 /* | |
169 | 1106 * When using temp files: |
1107 * 1. * Form temp file names | |
1108 * 2. * Write the lines to a temp file | |
1109 * 3. Run the filter command on the temp file | |
1110 * 4. * Read the output of the command into the buffer | |
1111 * 5. * Delete the original lines to be filtered | |
1112 * 6. * Remove the temp files | |
1113 * | |
1114 * When writing the input with a pipe or when catching the output with a | |
1115 * pipe only need to do 3. | |
7 | 1116 */ |
1117 | |
169 | 1118 if (do_out) |
1119 shell_flags |= SHELL_DOOUT; | |
1120 | |
1121 #if !defined(USE_SYSTEM) && defined(UNIX) | |
1122 if (!do_in && do_out && !p_stmp) | |
1123 { | |
1124 /* Use a pipe to fetch stdout of the command, do not use a temp file. */ | |
1125 shell_flags |= SHELL_READ; | |
1126 curwin->w_cursor.lnum = line2; | |
1127 } | |
1128 else if (do_in && !do_out && !p_stmp) | |
7 | 1129 { |
169 | 1130 /* Use a pipe to write stdin of the command, do not use a temp file. */ |
1131 shell_flags |= SHELL_WRITE; | |
1132 curbuf->b_op_start.lnum = line1; | |
1133 curbuf->b_op_end.lnum = line2; | |
7 | 1134 } |
169 | 1135 else if (do_in && do_out && !p_stmp) |
1136 { | |
1137 /* Use a pipe to write stdin and fetch stdout of the command, do not | |
1138 * use a temp file. */ | |
1139 shell_flags |= SHELL_READ|SHELL_WRITE; | |
1140 curbuf->b_op_start.lnum = line1; | |
1141 curbuf->b_op_end.lnum = line2; | |
1142 curwin->w_cursor.lnum = line2; | |
1143 } | |
1144 else | |
1145 #endif | |
1146 if ((do_in && (itmp = vim_tempname('i')) == NULL) | |
1147 || (do_out && (otmp = vim_tempname('o')) == NULL)) | |
1148 { | |
1149 EMSG(_(e_notmp)); | |
1150 goto filterend; | |
1151 } | |
7 | 1152 |
1153 /* | |
1154 * The writing and reading of temp files will not be shown. | |
1155 * Vi also doesn't do this and the messages are not very informative. | |
1156 */ | |
1157 ++no_wait_return; /* don't call wait_return() while busy */ | |
169 | 1158 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, |
7 | 1159 FALSE, FALSE, FALSE, TRUE) == FAIL) |
1160 { | |
1161 msg_putchar('\n'); /* keep message from buf_write() */ | |
1162 --no_wait_return; | |
1163 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
1164 if (!aborting()) | |
1165 #endif | |
1166 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ | |
1167 goto filterend; | |
1168 } | |
1169 #ifdef FEAT_AUTOCMD | |
1170 if (curbuf != old_curbuf) | |
1171 goto filterend; | |
1172 #endif | |
1173 | |
1174 if (!do_out) | |
1175 msg_putchar('\n'); | |
1176 | |
1581 | 1177 /* Create the shell command in allocated memory. */ |
7 | 1178 cmd_buf = make_filter_cmd(cmd, itmp, otmp); |
1179 if (cmd_buf == NULL) | |
1180 goto filterend; | |
1181 | |
1182 windgoto((int)Rows - 1, 0); | |
1183 cursor_on(); | |
1184 | |
1185 /* | |
1186 * When not redirecting the output the command can write anything to the | |
1187 * screen. If 'shellredir' is equal to ">", screen may be messed up by | |
1188 * stderr output of external command. Clear the screen later. | |
1189 * If do_in is FALSE, this could be something like ":r !cat", which may | |
1190 * also mess up the screen, clear it later. | |
1191 */ | |
1192 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in) | |
1193 redraw_later_clear(); | |
1194 | |
169 | 1195 if (do_out) |
1196 { | |
1197 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) | |
1581 | 1198 { |
1199 vim_free(cmd_buf); | |
169 | 1200 goto error; |
1581 | 1201 } |
169 | 1202 redraw_curbuf_later(VALID); |
1203 } | |
1204 read_linecount = curbuf->b_ml.ml_line_count; | |
1205 | |
7 | 1206 /* |
1207 * When call_shell() fails wait_return() is called to give the user a | |
1208 * chance to read the error messages. Otherwise errors are ignored, so you | |
1209 * can see the error messages from the command that appear on stdout; use | |
1210 * 'u' to fix the text | |
1211 * Switch to cooked mode when not redirecting stdin, avoids that something | |
1212 * like ":r !cat" hangs. | |
1213 * Pass on the SHELL_DOOUT flag when the output is being redirected. | |
1214 */ | |
169 | 1215 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags)) |
7 | 1216 { |
1217 redraw_later_clear(); | |
1218 wait_return(FALSE); | |
1219 } | |
1220 vim_free(cmd_buf); | |
1221 | |
1222 did_check_timestamps = FALSE; | |
1223 need_check_timestamps = TRUE; | |
1224 | |
1225 /* When interrupting the shell command, it may still have produced some | |
1226 * useful output. Reset got_int here, so that readfile() won't cancel | |
1227 * reading. */ | |
1228 ui_breakcheck(); | |
1229 got_int = FALSE; | |
1230 | |
1231 if (do_out) | |
1232 { | |
169 | 1233 if (otmp != NULL) |
7 | 1234 { |
169 | 1235 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, |
1236 eap, READ_FILTER) == FAIL) | |
1237 { | |
7 | 1238 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
169 | 1239 if (!aborting()) |
1240 #endif | |
1241 { | |
1242 msg_putchar('\n'); | |
1243 EMSG2(_(e_notread), otmp); | |
1244 } | |
1245 goto error; | |
7 | 1246 } |
169 | 1247 #ifdef FEAT_AUTOCMD |
1248 if (curbuf != old_curbuf) | |
1249 goto filterend; | |
1250 #endif | |
7 | 1251 } |
169 | 1252 |
1253 read_linecount = curbuf->b_ml.ml_line_count - read_linecount; | |
1254 | |
1255 if (shell_flags & SHELL_READ) | |
1256 { | |
1257 curbuf->b_op_start.lnum = line2 + 1; | |
1258 curbuf->b_op_end.lnum = curwin->w_cursor.lnum; | |
1259 appended_lines_mark(line2, read_linecount); | |
1260 } | |
7 | 1261 |
1262 if (do_in) | |
1263 { | |
1264 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL) | |
1265 { | |
1266 if (read_linecount >= linecount) | |
1267 /* move all marks from old lines to new lines */ | |
1268 mark_adjust(line1, line2, linecount, 0L); | |
1269 else | |
1270 { | |
1271 /* move marks from old lines to new lines, delete marks | |
1272 * that are in deleted lines */ | |
1273 mark_adjust(line1, line1 + read_linecount - 1, | |
1274 linecount, 0L); | |
1275 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L); | |
1276 } | |
1277 } | |
1278 | |
1279 /* | |
1280 * Put cursor on first filtered line for ":range!cmd". | |
1281 * Adjust '[ and '] (set by buf_write()). | |
1282 */ | |
1283 curwin->w_cursor.lnum = line1; | |
1284 del_lines(linecount, TRUE); | |
1285 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */ | |
1286 curbuf->b_op_end.lnum -= linecount; /* adjust '] */ | |
1287 write_lnum_adjust(-linecount); /* adjust last line | |
1288 for next write */ | |
100 | 1289 #ifdef FEAT_FOLDING |
1290 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); | |
1291 #endif | |
7 | 1292 } |
1293 else | |
1294 { | |
1295 /* | |
1296 * Put cursor on last new line for ":r !cmd". | |
1297 */ | |
169 | 1298 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1; |
7 | 1299 curwin->w_cursor.lnum = curbuf->b_op_end.lnum; |
1300 } | |
100 | 1301 |
7 | 1302 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */ |
1303 --no_wait_return; | |
1304 | |
1305 if (linecount > p_report) | |
1306 { | |
1307 if (do_in) | |
1308 { | |
274 | 1309 vim_snprintf((char *)msg_buf, sizeof(msg_buf), |
1310 _("%ld lines filtered"), (long)linecount); | |
7 | 1311 if (msg(msg_buf) && !msg_scroll) |
1312 /* save message to display it after redraw */ | |
680 | 1313 set_keep_msg(msg_buf, 0); |
7 | 1314 } |
1315 else | |
1316 msgmore((long)linecount); | |
1317 } | |
1318 } | |
1319 else | |
1320 { | |
1321 error: | |
1322 /* put cursor back in same position for ":w !cmd" */ | |
1323 curwin->w_cursor = cursor_save; | |
1324 --no_wait_return; | |
1325 wait_return(FALSE); | |
1326 } | |
1327 | |
1328 filterend: | |
1329 | |
1330 #ifdef FEAT_AUTOCMD | |
1331 if (curbuf != old_curbuf) | |
1332 { | |
1333 --no_wait_return; | |
1334 EMSG(_("E135: *Filter* Autocommands must not change current buffer")); | |
1335 } | |
1336 #endif | |
1337 if (itmp != NULL) | |
1338 mch_remove(itmp); | |
1339 if (otmp != NULL) | |
1340 mch_remove(otmp); | |
1341 vim_free(itmp); | |
1342 vim_free(otmp); | |
1343 } | |
1344 | |
1345 /* | |
1346 * Call a shell to execute a command. | |
1347 * When "cmd" is NULL start an interactive shell. | |
1348 */ | |
1349 void | |
1350 do_shell(cmd, flags) | |
1351 char_u *cmd; | |
1352 int flags; /* may be SHELL_DOOUT when output is redirected */ | |
1353 { | |
1354 buf_T *buf; | |
1355 #ifndef FEAT_GUI_MSWIN | |
1356 int save_nwr; | |
1357 #endif | |
1358 #ifdef MSWIN | |
1359 int winstart = FALSE; | |
1360 #endif | |
1361 | |
1362 /* | |
1363 * Disallow shell commands for "rvim". | |
1364 * Disallow shell commands from .exrc and .vimrc in current directory for | |
1365 * security reasons. | |
1366 */ | |
1367 if (check_restricted() || check_secure()) | |
1368 { | |
1369 msg_end(); | |
1370 return; | |
1371 } | |
1372 | |
1373 #ifdef MSWIN | |
1374 /* | |
1375 * Check if external commands are allowed now. | |
1376 */ | |
1377 if (can_end_termcap_mode(TRUE) == FALSE) | |
1378 return; | |
1379 | |
1380 /* | |
1381 * Check if ":!start" is used. | |
1382 */ | |
1383 if (cmd != NULL) | |
1384 winstart = (STRNICMP(cmd, "start ", 6) == 0); | |
1385 #endif | |
1386 | |
1387 /* | |
1388 * For autocommands we want to get the output on the current screen, to | |
1389 * avoid having to type return below. | |
1390 */ | |
1391 msg_putchar('\r'); /* put cursor at start of line */ | |
1392 #ifdef FEAT_AUTOCMD | |
1393 if (!autocmd_busy) | |
1394 #endif | |
1395 { | |
1396 #ifdef MSWIN | |
1397 if (!winstart) | |
1398 #endif | |
1399 stoptermcap(); | |
1400 } | |
1401 #ifdef MSWIN | |
1402 if (!winstart) | |
1403 #endif | |
1404 msg_putchar('\n'); /* may shift screen one line up */ | |
1405 | |
1406 /* warning message before calling the shell */ | |
1407 if (p_warn | |
1408 #ifdef FEAT_AUTOCMD | |
1409 && !autocmd_busy | |
1410 #endif | |
1411 && msg_silent == 0) | |
1412 for (buf = firstbuf; buf; buf = buf->b_next) | |
1413 if (bufIsChanged(buf)) | |
1414 { | |
1415 #ifdef FEAT_GUI_MSWIN | |
1416 if (!winstart) | |
1417 starttermcap(); /* don't want a message box here */ | |
1418 #endif | |
1419 MSG_PUTS(_("[No write since last change]\n")); | |
1420 #ifdef FEAT_GUI_MSWIN | |
1421 if (!winstart) | |
1422 stoptermcap(); | |
1423 #endif | |
1424 break; | |
1425 } | |
1426 | |
1427 /* This windgoto is required for when the '\n' resulted in a "delete line | |
1428 * 1" command to the terminal. */ | |
1429 if (!swapping_screen()) | |
1430 windgoto(msg_row, msg_col); | |
1431 cursor_on(); | |
1432 (void)call_shell(cmd, SHELL_COOKED | flags); | |
1433 did_check_timestamps = FALSE; | |
1434 need_check_timestamps = TRUE; | |
1435 | |
1436 /* | |
1437 * put the message cursor at the end of the screen, avoids wait_return() | |
1438 * to overwrite the text that the external command showed | |
1439 */ | |
1440 if (!swapping_screen()) | |
1441 { | |
1442 msg_row = Rows - 1; | |
1443 msg_col = 0; | |
1444 } | |
1445 | |
1446 #ifdef FEAT_AUTOCMD | |
1447 if (autocmd_busy) | |
1448 { | |
1449 if (msg_silent == 0) | |
1450 redraw_later_clear(); | |
1451 } | |
1452 else | |
1453 #endif | |
1454 { | |
1455 /* | |
1456 * For ":sh" there is no need to call wait_return(), just redraw. | |
1457 * Also for the Win32 GUI (the output is in a console window). | |
1458 * Otherwise there is probably text on the screen that the user wants | |
1459 * to read before redrawing, so call wait_return(). | |
1460 */ | |
1461 #ifndef FEAT_GUI_MSWIN | |
1462 if (cmd == NULL | |
1463 # ifdef WIN3264 | |
1464 || (winstart && !need_wait_return) | |
1465 # endif | |
1466 ) | |
1467 { | |
1468 if (msg_silent == 0) | |
1469 redraw_later_clear(); | |
1470 need_wait_return = FALSE; | |
1471 } | |
1472 else | |
1473 { | |
1474 /* | |
1475 * If we switch screens when starttermcap() is called, we really | |
1476 * want to wait for "hit return to continue". | |
1477 */ | |
1478 save_nwr = no_wait_return; | |
1479 if (swapping_screen()) | |
1480 no_wait_return = FALSE; | |
1481 # ifdef AMIGA | |
1482 wait_return(term_console ? -1 : msg_silent == 0); /* see below */ | |
1483 # else | |
1484 wait_return(msg_silent == 0); | |
1485 # endif | |
1486 no_wait_return = save_nwr; | |
1487 } | |
1488 #endif /* FEAT_GUI_W32 */ | |
1489 | |
1490 #ifdef MSWIN | |
1491 if (!winstart) /* if winstart==TRUE, never stopped termcap! */ | |
1492 #endif | |
1493 starttermcap(); /* start termcap if not done by wait_return() */ | |
1494 | |
1495 /* | |
1496 * In an Amiga window redrawing is caused by asking the window size. | |
1497 * If we got an interrupt this will not work. The chance that the | |
1498 * window size is wrong is very small, but we need to redraw the | |
1499 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY | |
1500 * but it saves an extra redraw. | |
1501 */ | |
1502 #ifdef AMIGA | |
1503 if (skip_redraw) /* ':' hit in wait_return() */ | |
1504 { | |
1505 if (msg_silent == 0) | |
1506 redraw_later_clear(); | |
1507 } | |
1508 else if (term_console) | |
1509 { | |
1510 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */ | |
1511 if (got_int && msg_silent == 0) | |
1512 redraw_later_clear(); /* if got_int is TRUE, redraw needed */ | |
1513 else | |
1514 must_redraw = 0; /* no extra redraw needed */ | |
1515 } | |
1516 #endif | |
1517 } | |
1518 | |
1519 /* display any error messages now */ | |
1520 display_errors(); | |
725 | 1521 |
1522 #ifdef FEAT_AUTOCMD | |
1523 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); | |
1524 #endif | |
7 | 1525 } |
1526 | |
1527 /* | |
1528 * Create a shell command from a command string, input redirection file and | |
1529 * output redirection file. | |
1530 * Returns an allocated string with the shell command, or NULL for failure. | |
1531 */ | |
1532 char_u * | |
1533 make_filter_cmd(cmd, itmp, otmp) | |
1534 char_u *cmd; /* command */ | |
1535 char_u *itmp; /* NULL or name of input file */ | |
1536 char_u *otmp; /* NULL or name of output file */ | |
1537 { | |
1538 char_u *buf; | |
1539 long_u len; | |
1540 | |
1541 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */ | |
1542 if (itmp != NULL) | |
1543 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */ | |
1544 if (otmp != NULL) | |
1545 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */ | |
1546 buf = lalloc(len, TRUE); | |
1547 if (buf == NULL) | |
1548 return NULL; | |
1549 | |
1550 #if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2) | |
1551 /* | |
169 | 1552 * Put braces around the command (for concatenated commands) when |
1553 * redirecting input and/or output. | |
7 | 1554 */ |
169 | 1555 if (itmp != NULL || otmp != NULL) |
1872 | 1556 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); |
169 | 1557 else |
1558 STRCPY(buf, cmd); | |
7 | 1559 if (itmp != NULL) |
1560 { | |
1561 STRCAT(buf, " < "); | |
1562 STRCAT(buf, itmp); | |
1563 } | |
1564 #else | |
1565 /* | |
1566 * for shells that don't understand braces around commands, at least allow | |
1567 * the use of commands in a pipe. | |
1568 */ | |
1569 STRCPY(buf, cmd); | |
1570 if (itmp != NULL) | |
1571 { | |
1572 char_u *p; | |
1573 | |
1574 /* | |
1575 * If there is a pipe, we have to put the '<' in front of it. | |
1576 * Don't do this when 'shellquote' is not empty, otherwise the | |
1577 * redirection would be inside the quotes. | |
1578 */ | |
1579 if (*p_shq == NUL) | |
1580 { | |
1581 p = vim_strchr(buf, '|'); | |
1582 if (p != NULL) | |
1583 *p = NUL; | |
1584 } | |
1585 # ifdef RISCOS | |
1586 STRCAT(buf, " { < "); /* Use RISC OS notation for input. */ | |
1587 STRCAT(buf, itmp); | |
1588 STRCAT(buf, " } "); | |
1589 # else | |
1590 STRCAT(buf, " <"); /* " < " causes problems on Amiga */ | |
1591 STRCAT(buf, itmp); | |
1592 # endif | |
1593 if (*p_shq == NUL) | |
1594 { | |
1595 p = vim_strchr(cmd, '|'); | |
1596 if (p != NULL) | |
1597 { | |
1598 STRCAT(buf, " "); /* insert a space before the '|' for DOS */ | |
1599 STRCAT(buf, p); | |
1600 } | |
1601 } | |
1602 } | |
1603 #endif | |
1604 if (otmp != NULL) | |
1872 | 1605 append_redir(buf, (int)len, p_srr, otmp); |
7 | 1606 |
1607 return buf; | |
1608 } | |
1609 | |
1610 /* | |
1872 | 1611 * Append output redirection for file "fname" to the end of string buffer |
1612 * "buf[buflen]" | |
7 | 1613 * Works with the 'shellredir' and 'shellpipe' options. |
1614 * The caller should make sure that there is enough room: | |
1615 * STRLEN(opt) + STRLEN(fname) + 3 | |
1616 */ | |
1617 void | |
1872 | 1618 append_redir(buf, buflen, opt, fname) |
7 | 1619 char_u *buf; |
1872 | 1620 int buflen; |
7 | 1621 char_u *opt; |
1622 char_u *fname; | |
1623 { | |
1624 char_u *p; | |
1872 | 1625 char_u *end; |
1626 | |
1627 end = buf + STRLEN(buf); | |
7 | 1628 /* find "%s", skipping "%%" */ |
1629 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p) | |
1630 if (p[1] == 's') | |
1631 break; | |
1632 if (p != NULL) | |
1633 { | |
1872 | 1634 *end = ' '; /* not really needed? Not with sh, ksh or bash */ |
1635 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)), | |
1636 (char *)opt, (char *)fname); | |
7 | 1637 } |
1638 else | |
1872 | 1639 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)), |
7 | 1640 #ifdef FEAT_QUICKFIX |
1641 # ifndef RISCOS | |
1642 opt != p_sp ? " %s%s" : | |
1643 # endif | |
1644 " %s %s", | |
1645 #else | |
1646 # ifndef RISCOS | |
1647 " %s%s", /* " > %s" causes problems on Amiga */ | |
1648 # else | |
1649 " %s %s", /* But is needed for 'shellpipe' and RISC OS */ | |
1650 # endif | |
1651 #endif | |
1652 (char *)opt, (char *)fname); | |
1653 } | |
1654 | |
1655 #ifdef FEAT_VIMINFO | |
1656 | |
1657 static int no_viminfo __ARGS((void)); | |
1658 static int viminfo_errcnt; | |
1659 | |
1660 static int | |
1661 no_viminfo() | |
1662 { | |
1663 /* "vim -i NONE" does not read or write a viminfo file */ | |
1664 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0); | |
1665 } | |
1666 | |
1667 /* | |
1668 * Report an error for reading a viminfo file. | |
1669 * Count the number of errors. When there are more than 10, return TRUE. | |
1670 */ | |
1671 int | |
1672 viminfo_error(errnum, message, line) | |
1673 char *errnum; | |
1674 char *message; | |
1675 char_u *line; | |
1676 { | |
274 | 1677 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "), |
1678 errnum, message); | |
1459 | 1679 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1); |
156 | 1680 if (IObuff[STRLEN(IObuff) - 1] == '\n') |
1681 IObuff[STRLEN(IObuff) - 1] = NUL; | |
7 | 1682 emsg(IObuff); |
1683 if (++viminfo_errcnt >= 10) | |
1684 { | |
1685 EMSG(_("E136: viminfo: Too many errors, skipping rest of file")); | |
1686 return TRUE; | |
1687 } | |
1688 return FALSE; | |
1689 } | |
1690 | |
1691 /* | |
1692 * read_viminfo() -- Read the viminfo file. Registers etc. which are already | |
1733 | 1693 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb |
7 | 1694 */ |
1695 int | |
1733 | 1696 read_viminfo(file, flags) |
1697 char_u *file; /* file name or NULL to use default name */ | |
1698 int flags; /* VIF_WANT_INFO et al. */ | |
7 | 1699 { |
1700 FILE *fp; | |
1701 char_u *fname; | |
1702 | |
1703 if (no_viminfo()) | |
1704 return FAIL; | |
1705 | |
1733 | 1706 fname = viminfo_filename(file); /* get file name in allocated buffer */ |
7 | 1707 if (fname == NULL) |
1708 return FAIL; | |
1709 fp = mch_fopen((char *)fname, READBIN); | |
1710 | |
1711 if (p_verbose > 0) | |
294 | 1712 { |
1713 verbose_enter(); | |
274 | 1714 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"), |
1715 fname, | |
1733 | 1716 (flags & VIF_WANT_INFO) ? _(" info") : "", |
1717 (flags & VIF_WANT_MARKS) ? _(" marks") : "", | |
1718 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "", | |
274 | 1719 fp == NULL ? _(" FAILED") : ""); |
294 | 1720 verbose_leave(); |
1721 } | |
7 | 1722 |
1723 vim_free(fname); | |
1724 if (fp == NULL) | |
1725 return FAIL; | |
1726 | |
1727 viminfo_errcnt = 0; | |
1733 | 1728 do_viminfo(fp, NULL, flags); |
7 | 1729 |
1730 fclose(fp); | |
1731 return OK; | |
1732 } | |
1733 | |
1734 /* | |
1735 * write_viminfo() -- Write the viminfo file. The old one is read in first so | |
1736 * that effectively a merge of current info and old info is done. This allows | |
1737 * multiple vims to run simultaneously, without losing any marks etc. If | |
1738 * forceit is TRUE, then the old file is not read in, and only internal info is | |
1739 * written to the file. -- webb | |
1740 */ | |
1741 void | |
1742 write_viminfo(file, forceit) | |
1743 char_u *file; | |
1744 int forceit; | |
1745 { | |
1746 char_u *fname; | |
1747 FILE *fp_in = NULL; /* input viminfo file, if any */ | |
1748 FILE *fp_out = NULL; /* output viminfo file */ | |
1749 char_u *tempname = NULL; /* name of temp viminfo file */ | |
1750 struct stat st_new; /* mch_stat() of potential new file */ | |
1751 char_u *wp; | |
1752 #if defined(UNIX) || defined(VMS) | |
1753 mode_t umask_save; | |
1754 #endif | |
1755 #ifdef UNIX | |
1756 int shortname = FALSE; /* use 8.3 file name */ | |
1757 struct stat st_old; /* mch_stat() of existing viminfo file */ | |
1758 #endif | |
601 | 1759 #ifdef WIN3264 |
1760 long perm = -1; | |
1761 #endif | |
7 | 1762 |
1763 if (no_viminfo()) | |
1764 return; | |
1765 | |
1766 fname = viminfo_filename(file); /* may set to default if NULL */ | |
1767 if (fname == NULL) | |
1768 return; | |
1769 | |
1770 fp_in = mch_fopen((char *)fname, READBIN); | |
1771 if (fp_in == NULL) | |
1772 { | |
1773 /* if it does exist, but we can't read it, don't try writing */ | |
1774 if (mch_stat((char *)fname, &st_new) == 0) | |
1775 goto end; | |
1776 #if defined(UNIX) || defined(VMS) | |
1777 /* | |
1778 * For Unix we create the .viminfo non-accessible for others, | |
1779 * because it may contain text from non-accessible documents. | |
1780 */ | |
1781 umask_save = umask(077); | |
1782 #endif | |
1783 fp_out = mch_fopen((char *)fname, WRITEBIN); | |
1784 #if defined(UNIX) || defined(VMS) | |
1785 (void)umask(umask_save); | |
1786 #endif | |
1787 } | |
1788 else | |
1789 { | |
1790 /* | |
1791 * There is an existing viminfo file. Create a temporary file to | |
1792 * write the new viminfo into, in the same directory as the | |
1793 * existing viminfo file, which will be renamed later. | |
1794 */ | |
1795 #ifdef UNIX | |
1796 /* | |
1797 * For Unix we check the owner of the file. It's not very nice to | |
1798 * overwrite a user's viminfo file after a "su root", with a | |
1799 * viminfo file that the user can't read. | |
1800 */ | |
1869 | 1801 st_old.st_dev = (dev_t)0; |
1438 | 1802 st_old.st_ino = 0; |
7 | 1803 st_old.st_mode = 0600; |
1076 | 1804 if (mch_stat((char *)fname, &st_old) == 0 |
1805 && getuid() != ROOT_UID | |
560 | 1806 && !(st_old.st_uid == getuid() |
7 | 1807 ? (st_old.st_mode & 0200) |
1808 : (st_old.st_gid == getgid() | |
1809 ? (st_old.st_mode & 0020) | |
1810 : (st_old.st_mode & 0002)))) | |
1811 { | |
944 | 1812 int tt = msg_didany; |
7 | 1813 |
1814 /* avoid a wait_return for this message, it's annoying */ | |
1815 EMSG2(_("E137: Viminfo file is not writable: %s"), fname); | |
1816 msg_didany = tt; | |
840 | 1817 fclose(fp_in); |
7 | 1818 goto end; |
1819 } | |
1820 #endif | |
601 | 1821 #ifdef WIN3264 |
1822 /* Get the file attributes of the existing viminfo file. */ | |
1823 perm = mch_getperm(fname); | |
1824 #endif | |
7 | 1825 |
1826 /* | |
1827 * Make tempname. | |
1828 * May try twice: Once normal and once with shortname set, just in | |
1829 * case somebody puts his viminfo file in an 8.3 filesystem. | |
1830 */ | |
1831 for (;;) | |
1832 { | |
1833 tempname = buf_modname( | |
1834 #ifdef UNIX | |
1835 shortname, | |
1836 #else | |
1837 # ifdef SHORT_FNAME | |
1838 TRUE, | |
1839 # else | |
1840 # ifdef FEAT_GUI_W32 | |
1841 gui_is_win32s(), | |
1842 # else | |
1843 FALSE, | |
1844 # endif | |
1845 # endif | |
1846 #endif | |
1847 fname, | |
1848 #ifdef VMS | |
1849 (char_u *)"-tmp", | |
1850 #else | |
1851 # ifdef RISCOS | |
1852 (char_u *)"/tmp", | |
1853 # else | |
1854 (char_u *)".tmp", | |
1855 # endif | |
1856 #endif | |
1857 FALSE); | |
1858 if (tempname == NULL) /* out of memory */ | |
1859 break; | |
1860 | |
1861 /* | |
1862 * Check if tempfile already exists. Never overwrite an | |
1863 * existing file! | |
1864 */ | |
1865 if (mch_stat((char *)tempname, &st_new) == 0) | |
1866 { | |
1867 #ifdef UNIX | |
1868 /* | |
1869 * Check if tempfile is same as original file. May happen | |
1870 * when modname() gave the same file back. E.g. silly | |
1871 * link, or file name-length reached. Try again with | |
1872 * shortname set. | |
1873 */ | |
560 | 1874 if (!shortname && st_new.st_dev == st_old.st_dev |
1875 && st_new.st_ino == st_old.st_ino) | |
7 | 1876 { |
1877 vim_free(tempname); | |
1878 tempname = NULL; | |
1879 shortname = TRUE; | |
1880 continue; | |
1881 } | |
1882 #endif | |
1883 /* | |
1884 * Try another name. Change one character, just before | |
1885 * the extension. This should also work for an 8.3 | |
1886 * file name, when after adding the extension it still is | |
1887 * the same file as the original. | |
1888 */ | |
1889 wp = tempname + STRLEN(tempname) - 5; | |
1890 if (wp < gettail(tempname)) /* empty file name? */ | |
1891 wp = gettail(tempname); | |
1892 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0; | |
1893 --*wp) | |
1894 { | |
1895 /* | |
1896 * They all exist? Must be something wrong! Don't | |
1897 * write the viminfo file then. | |
1898 */ | |
1899 if (*wp == 'a') | |
1900 { | |
1901 vim_free(tempname); | |
1902 tempname = NULL; | |
1903 break; | |
1904 } | |
1905 } | |
1906 } | |
1907 break; | |
1908 } | |
1909 | |
1910 if (tempname != NULL) | |
1911 { | |
762 | 1912 #ifdef VMS |
1913 /* fdopen() fails for some reason */ | |
770 | 1914 umask_save = umask(077); |
1915 fp_out = mch_fopen((char *)tempname, WRITEBIN); | |
1916 (void)umask(umask_save); | |
762 | 1917 #else |
557 | 1918 int fd; |
1919 | |
1920 /* Use mch_open() to be able to use O_NOFOLLOW and set file | |
601 | 1921 * protection: |
673 | 1922 * Unix: same as original file, but strip s-bit. Reset umask to |
1923 * avoid it getting in the way. | |
601 | 1924 * Others: r&w for user only. */ |
762 | 1925 # ifdef UNIX |
673 | 1926 umask_save = umask(0); |
557 | 1927 fd = mch_open((char *)tempname, |
1928 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, | |
569 | 1929 (int)((st_old.st_mode & 0777) | 0600)); |
673 | 1930 (void)umask(umask_save); |
762 | 1931 # else |
569 | 1932 fd = mch_open((char *)tempname, |
673 | 1933 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600); |
762 | 1934 # endif |
557 | 1935 if (fd < 0) |
1936 fp_out = NULL; | |
1937 else | |
1938 fp_out = fdopen(fd, WRITEBIN); | |
762 | 1939 #endif /* VMS */ |
7 | 1940 |
1941 /* | |
1942 * If we can't create in the same directory, try creating a | |
1943 * "normal" temp file. | |
1944 */ | |
1945 if (fp_out == NULL) | |
1946 { | |
1947 vim_free(tempname); | |
1948 if ((tempname = vim_tempname('o')) != NULL) | |
1949 fp_out = mch_fopen((char *)tempname, WRITEBIN); | |
1950 } | |
557 | 1951 |
1952 #if defined(UNIX) && defined(HAVE_FCHOWN) | |
7 | 1953 /* |
557 | 1954 * Make sure the owner can read/write it. This only works for |
1955 * root. | |
7 | 1956 */ |
1957 if (fp_out != NULL) | |
1757 | 1958 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid); |
7 | 1959 #endif |
1960 } | |
1961 } | |
1962 | |
1963 /* | |
1964 * Check if the new viminfo file can be written to. | |
1965 */ | |
1966 if (fp_out == NULL) | |
1967 { | |
1968 EMSG2(_("E138: Can't write viminfo file %s!"), | |
301 | 1969 (fp_in == NULL || tempname == NULL) ? fname : tempname); |
7 | 1970 if (fp_in != NULL) |
1971 fclose(fp_in); | |
1972 goto end; | |
1973 } | |
1974 | |
1975 if (p_verbose > 0) | |
294 | 1976 { |
1977 verbose_enter(); | |
274 | 1978 smsg((char_u *)_("Writing viminfo file \"%s\""), fname); |
294 | 1979 verbose_leave(); |
1980 } | |
7 | 1981 |
1982 viminfo_errcnt = 0; | |
1733 | 1983 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS)); |
7 | 1984 |
1985 fclose(fp_out); /* errors are ignored !? */ | |
1986 if (fp_in != NULL) | |
1987 { | |
1988 fclose(fp_in); | |
601 | 1989 |
7 | 1990 /* |
601 | 1991 * In case of an error keep the original viminfo file. |
1992 * Otherwise rename the newly written file. | |
7 | 1993 */ |
1994 if (viminfo_errcnt || vim_rename(tempname, fname) == -1) | |
1995 mch_remove(tempname); | |
601 | 1996 |
1997 #ifdef WIN3264 | |
1998 /* If the viminfo file was hidden then also hide the new file. */ | |
1999 if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN)) | |
2000 mch_hide(fname); | |
2001 #endif | |
2002 } | |
2003 | |
7 | 2004 end: |
2005 vim_free(fname); | |
2006 vim_free(tempname); | |
2007 } | |
2008 | |
2009 /* | |
2010 * Get the viminfo file name to use. | |
2011 * If "file" is given and not empty, use it (has already been expanded by | |
2012 * cmdline functions). | |
2013 * Otherwise use "-i file_name", value from 'viminfo' or the default, and | |
2014 * expand environment variables. | |
2015 * Returns an allocated string. NULL when out of memory. | |
2016 */ | |
2017 static char_u * | |
2018 viminfo_filename(file) | |
2019 char_u *file; | |
2020 { | |
2021 if (file == NULL || *file == NUL) | |
2022 { | |
2023 if (use_viminfo != NULL) | |
2024 file = use_viminfo; | |
2025 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL) | |
2026 { | |
2027 #ifdef VIMINFO_FILE2 | |
2028 /* don't use $HOME when not defined (turned into "c:/"!). */ | |
2029 # ifdef VMS | |
2030 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL) | |
2031 # else | |
2032 if (mch_getenv((char_u *)"HOME") == NULL) | |
2033 # endif | |
2034 { | |
2035 /* don't use $VIM when not available. */ | |
2036 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL); | |
2037 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */ | |
2038 file = (char_u *)VIMINFO_FILE2; | |
2039 else | |
2040 file = (char_u *)VIMINFO_FILE; | |
2041 } | |
2042 else | |
2043 #endif | |
2044 file = (char_u *)VIMINFO_FILE; | |
2045 } | |
2046 expand_env(file, NameBuff, MAXPATHL); | |
2047 file = NameBuff; | |
2048 } | |
2049 return vim_strsave(file); | |
2050 } | |
2051 | |
2052 /* | |
2053 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo(). | |
2054 */ | |
2055 static void | |
1733 | 2056 do_viminfo(fp_in, fp_out, flags) |
7 | 2057 FILE *fp_in; |
2058 FILE *fp_out; | |
1733 | 2059 int flags; |
7 | 2060 { |
2061 int count = 0; | |
2062 int eof = FALSE; | |
2063 vir_T vir; | |
2064 | |
2065 if ((vir.vir_line = alloc(LSIZE)) == NULL) | |
2066 return; | |
2067 vir.vir_fd = fp_in; | |
2068 #ifdef FEAT_MBYTE | |
2069 vir.vir_conv.vc_type = CONV_NONE; | |
2070 #endif | |
2071 | |
2072 if (fp_in != NULL) | |
2073 { | |
1733 | 2074 if (flags & VIF_WANT_INFO) |
2075 eof = read_viminfo_up_to_marks(&vir, | |
2076 flags & VIF_FORCEIT, fp_out != NULL); | |
7 | 2077 else |
2078 /* Skip info, find start of marks */ | |
2079 while (!(eof = viminfo_readline(&vir)) | |
2080 && vir.vir_line[0] != '>') | |
2081 ; | |
2082 } | |
2083 if (fp_out != NULL) | |
2084 { | |
2085 /* Write the info: */ | |
2086 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"), | |
2087 VIM_VERSION_MEDIUM); | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
2088 fputs(_("# You may edit it if you're careful!\n\n"), fp_out); |
7 | 2089 #ifdef FEAT_MBYTE |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
2090 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out); |
7 | 2091 fprintf(fp_out, "*encoding=%s\n\n", p_enc); |
2092 #endif | |
2093 write_viminfo_search_pattern(fp_out); | |
2094 write_viminfo_sub_string(fp_out); | |
2095 #ifdef FEAT_CMDHIST | |
2096 write_viminfo_history(fp_out); | |
2097 #endif | |
2098 write_viminfo_registers(fp_out); | |
2099 #ifdef FEAT_EVAL | |
2100 write_viminfo_varlist(fp_out); | |
2101 #endif | |
2102 write_viminfo_filemarks(fp_out); | |
2103 write_viminfo_bufferlist(fp_out); | |
2104 count = write_viminfo_marks(fp_out); | |
2105 } | |
1733 | 2106 if (fp_in != NULL |
2107 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT))) | |
2108 copy_viminfo_marks(&vir, fp_out, count, eof, flags); | |
7 | 2109 |
2110 vim_free(vir.vir_line); | |
2111 #ifdef FEAT_MBYTE | |
2112 if (vir.vir_conv.vc_type != CONV_NONE) | |
2113 convert_setup(&vir.vir_conv, NULL, NULL); | |
2114 #endif | |
2115 } | |
2116 | |
2117 /* | |
2118 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the | |
2119 * first part of the viminfo file which contains everything but the marks that | |
2120 * are local to a file. Returns TRUE when end-of-file is reached. -- webb | |
2121 */ | |
2122 static int | |
2123 read_viminfo_up_to_marks(virp, forceit, writing) | |
2124 vir_T *virp; | |
2125 int forceit; | |
2126 int writing; | |
2127 { | |
2128 int eof; | |
2129 buf_T *buf; | |
2130 | |
2131 #ifdef FEAT_CMDHIST | |
2132 prepare_viminfo_history(forceit ? 9999 : 0); | |
2133 #endif | |
2134 eof = viminfo_readline(virp); | |
2135 while (!eof && virp->vir_line[0] != '>') | |
2136 { | |
2137 switch (virp->vir_line[0]) | |
2138 { | |
2139 /* Characters reserved for future expansion, ignored now */ | |
2140 case '+': /* "+40 /path/dir file", for running vim without args */ | |
2141 case '|': /* to be defined */ | |
2142 case '^': /* to be defined */ | |
2143 case '<': /* long line - ignored */ | |
2144 /* A comment or empty line. */ | |
2145 case NUL: | |
2146 case '\r': | |
2147 case '\n': | |
2148 case '#': | |
2149 eof = viminfo_readline(virp); | |
2150 break; | |
2151 case '*': /* "*encoding=value" */ | |
2152 eof = viminfo_encoding(virp); | |
2153 break; | |
2154 case '!': /* global variable */ | |
2155 #ifdef FEAT_EVAL | |
2156 eof = read_viminfo_varlist(virp, writing); | |
2157 #else | |
2158 eof = viminfo_readline(virp); | |
2159 #endif | |
2160 break; | |
2161 case '%': /* entry for buffer list */ | |
2162 eof = read_viminfo_bufferlist(virp, writing); | |
2163 break; | |
2164 case '"': | |
2165 eof = read_viminfo_register(virp, forceit); | |
2166 break; | |
2167 case '/': /* Search string */ | |
2168 case '&': /* Substitute search string */ | |
2169 case '~': /* Last search string, followed by '/' or '&' */ | |
2170 eof = read_viminfo_search_pattern(virp, forceit); | |
2171 break; | |
2172 case '$': | |
2173 eof = read_viminfo_sub_string(virp, forceit); | |
2174 break; | |
2175 case ':': | |
2176 case '?': | |
2177 case '=': | |
2178 case '@': | |
2179 #ifdef FEAT_CMDHIST | |
2180 eof = read_viminfo_history(virp); | |
2181 #else | |
2182 eof = viminfo_readline(virp); | |
2183 #endif | |
2184 break; | |
2185 case '-': | |
2186 case '\'': | |
2187 eof = read_viminfo_filemark(virp, forceit); | |
2188 break; | |
2189 default: | |
2190 if (viminfo_error("E575: ", _("Illegal starting char"), | |
2191 virp->vir_line)) | |
2192 eof = TRUE; | |
2193 else | |
2194 eof = viminfo_readline(virp); | |
2195 break; | |
2196 } | |
2197 } | |
2198 | |
2199 #ifdef FEAT_CMDHIST | |
2200 /* Finish reading history items. */ | |
2201 finish_viminfo_history(); | |
2202 #endif | |
2203 | |
2204 /* Change file names to buffer numbers for fmarks. */ | |
2205 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2206 fmarks_check_names(buf); | |
2207 | |
2208 return eof; | |
2209 } | |
2210 | |
2211 /* | |
2212 * Compare the 'encoding' value in the viminfo file with the current value of | |
2213 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for | |
2214 * conversion of text with iconv() in viminfo_readstring(). | |
2215 */ | |
2216 static int | |
2217 viminfo_encoding(virp) | |
2218 vir_T *virp; | |
2219 { | |
2220 #ifdef FEAT_MBYTE | |
2221 char_u *p; | |
2222 int i; | |
2223 | |
2224 if (get_viminfo_parameter('c') != 0) | |
2225 { | |
2226 p = vim_strchr(virp->vir_line, '='); | |
2227 if (p != NULL) | |
2228 { | |
2229 /* remove trailing newline */ | |
2230 ++p; | |
2231 for (i = 0; vim_isprintc(p[i]); ++i) | |
2232 ; | |
2233 p[i] = NUL; | |
2234 | |
2235 convert_setup(&virp->vir_conv, p, p_enc); | |
2236 } | |
2237 } | |
2238 #endif | |
2239 return viminfo_readline(virp); | |
2240 } | |
2241 | |
2242 /* | |
2243 * Read a line from the viminfo file. | |
2244 * Returns TRUE for end-of-file; | |
2245 */ | |
2246 int | |
2247 viminfo_readline(virp) | |
2248 vir_T *virp; | |
2249 { | |
2250 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd); | |
2251 } | |
2252 | |
2253 /* | |
2254 * check string read from viminfo file | |
2255 * remove '\n' at the end of the line | |
2256 * - replace CTRL-V CTRL-V with CTRL-V | |
2257 * - replace CTRL-V 'n' with '\n' | |
2258 * | |
2259 * Check for a long line as written by viminfo_writestring(). | |
2260 * | |
2261 * Return the string in allocated memory (NULL when out of memory). | |
2262 */ | |
2263 char_u * | |
2264 viminfo_readstring(virp, off, convert) | |
2265 vir_T *virp; | |
2266 int off; /* offset for virp->vir_line */ | |
1883 | 2267 int convert UNUSED; /* convert the string */ |
7 | 2268 { |
2269 char_u *retval; | |
2270 char_u *s, *d; | |
2271 long len; | |
2272 | |
2273 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1])) | |
2274 { | |
2275 len = atol((char *)virp->vir_line + off + 1); | |
2276 retval = lalloc(len, TRUE); | |
2277 if (retval == NULL) | |
2278 { | |
2279 /* Line too long? File messed up? Skip next line. */ | |
2280 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd); | |
2281 return NULL; | |
2282 } | |
2283 (void)vim_fgets(retval, (int)len, virp->vir_fd); | |
2284 s = retval + 1; /* Skip the leading '<' */ | |
2285 } | |
2286 else | |
2287 { | |
2288 retval = vim_strsave(virp->vir_line + off); | |
2289 if (retval == NULL) | |
2290 return NULL; | |
2291 s = retval; | |
2292 } | |
2293 | |
2294 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */ | |
2295 d = retval; | |
2296 while (*s != NUL && *s != '\n') | |
2297 { | |
2298 if (s[0] == Ctrl_V && s[1] != NUL) | |
2299 { | |
2300 if (s[1] == 'n') | |
2301 *d++ = '\n'; | |
2302 else | |
2303 *d++ = Ctrl_V; | |
2304 s += 2; | |
2305 } | |
2306 else | |
2307 *d++ = *s++; | |
2308 } | |
2309 *d = NUL; | |
2310 | |
2311 #ifdef FEAT_MBYTE | |
2312 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL) | |
2313 { | |
2314 d = string_convert(&virp->vir_conv, retval, NULL); | |
2315 if (d != NULL) | |
2316 { | |
2317 vim_free(retval); | |
2318 retval = d; | |
2319 } | |
2320 } | |
2321 #endif | |
2322 | |
2323 return retval; | |
2324 } | |
2325 | |
2326 /* | |
2327 * write string to viminfo file | |
2328 * - replace CTRL-V with CTRL-V CTRL-V | |
2329 * - replace '\n' with CTRL-V 'n' | |
2330 * - add a '\n' at the end | |
2331 * | |
2332 * For a long line: | |
2333 * - write " CTRL-V <length> \n " in first line | |
2334 * - write " < <string> \n " in second line | |
2335 */ | |
2336 void | |
2337 viminfo_writestring(fd, p) | |
2338 FILE *fd; | |
2339 char_u *p; | |
2340 { | |
2341 int c; | |
2342 char_u *s; | |
2343 int len = 0; | |
2344 | |
2345 for (s = p; *s != NUL; ++s) | |
2346 { | |
2347 if (*s == Ctrl_V || *s == '\n') | |
2348 ++len; | |
2349 ++len; | |
2350 } | |
2351 | |
2352 /* If the string will be too long, write its length and put it in the next | |
2353 * line. Take into account that some room is needed for what comes before | |
2354 * the string (e.g., variable name). Add something to the length for the | |
2355 * '<', NL and trailing NUL. */ | |
2356 if (len > LSIZE / 2) | |
2357 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3); | |
2358 | |
2359 while ((c = *p++) != NUL) | |
2360 { | |
2361 if (c == Ctrl_V || c == '\n') | |
2362 { | |
2363 putc(Ctrl_V, fd); | |
2364 if (c == '\n') | |
2365 c = 'n'; | |
2366 } | |
2367 putc(c, fd); | |
2368 } | |
2369 putc('\n', fd); | |
2370 } | |
2371 #endif /* FEAT_VIMINFO */ | |
2372 | |
2373 /* | |
2374 * Implementation of ":fixdel", also used by get_stty(). | |
2375 * <BS> resulting <Del> | |
2376 * ^? ^H | |
2377 * not ^? ^? | |
2378 */ | |
2379 void | |
2380 do_fixdel(eap) | |
1876 | 2381 exarg_T *eap UNUSED; |
7 | 2382 { |
2383 char_u *p; | |
2384 | |
2385 p = find_termcode((char_u *)"kb"); | |
2386 add_termcode((char_u *)"kD", p != NULL | |
2387 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE); | |
2388 } | |
2389 | |
2390 void | |
169 | 2391 print_line_no_prefix(lnum, use_number, list) |
7 | 2392 linenr_T lnum; |
2393 int use_number; | |
169 | 2394 int list; |
7 | 2395 { |
13 | 2396 char_u numbuf[30]; |
7 | 2397 |
2398 if (curwin->w_p_nu || use_number) | |
2399 { | |
1872 | 2400 vim_snprintf((char *)numbuf, sizeof(numbuf), |
2401 "%*ld ", number_width(curwin), (long)lnum); | |
7 | 2402 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */ |
2403 } | |
169 | 2404 msg_prt_line(ml_get(lnum), list); |
7 | 2405 } |
2406 | |
2407 /* | |
2408 * Print a text line. Also in silent mode ("ex -s"). | |
2409 */ | |
2410 void | |
169 | 2411 print_line(lnum, use_number, list) |
7 | 2412 linenr_T lnum; |
2413 int use_number; | |
169 | 2414 int list; |
7 | 2415 { |
2416 int save_silent = silent_mode; | |
2417 | |
169 | 2418 msg_start(); |
7 | 2419 silent_mode = FALSE; |
169 | 2420 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ |
2421 print_line_no_prefix(lnum, use_number, list); | |
7 | 2422 if (save_silent) |
2423 { | |
2424 msg_putchar('\n'); | |
2425 cursor_on(); /* msg_start() switches it off */ | |
2426 out_flush(); | |
2427 silent_mode = save_silent; | |
1798 | 2428 } |
2429 info_message = FALSE; | |
7 | 2430 } |
2431 | |
2432 /* | |
2433 * ":file[!] [fname]". | |
2434 */ | |
2435 void | |
2436 ex_file(eap) | |
2437 exarg_T *eap; | |
2438 { | |
2439 char_u *fname, *sfname, *xfname; | |
2440 buf_T *buf; | |
2441 | |
14 | 2442 /* ":0file" removes the file name. Check for illegal uses ":3file", |
2443 * "0file name", etc. */ | |
2444 if (eap->addr_count > 0 | |
2445 && (*eap->arg != NUL | |
2446 || eap->line2 > 0 | |
2447 || eap->addr_count > 1)) | |
2448 { | |
2449 EMSG(_(e_invarg)); | |
2450 return; | |
2451 } | |
2452 | |
2453 if (*eap->arg != NUL || eap->addr_count == 1) | |
7 | 2454 { |
2455 #ifdef FEAT_AUTOCMD | |
2456 buf = curbuf; | |
2457 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); | |
2458 /* buffer changed, don't change name now */ | |
2459 if (buf != curbuf) | |
2460 return; | |
2461 # ifdef FEAT_EVAL | |
2462 if (aborting()) /* autocmds may abort script processing */ | |
2463 return; | |
2464 # endif | |
2465 #endif | |
2466 /* | |
2467 * The name of the current buffer will be changed. | |
2468 * A new (unlisted) buffer entry needs to be made to hold the old file | |
2469 * name, which will become the alternate file name. | |
29 | 2470 * But don't set the alternate file name if the buffer didn't have a |
2471 * name. | |
7 | 2472 */ |
2473 fname = curbuf->b_ffname; | |
2474 sfname = curbuf->b_sfname; | |
2475 xfname = curbuf->b_fname; | |
2476 curbuf->b_ffname = NULL; | |
2477 curbuf->b_sfname = NULL; | |
2478 if (setfname(curbuf, eap->arg, NULL, TRUE) == FAIL) | |
2479 { | |
2480 curbuf->b_ffname = fname; | |
2481 curbuf->b_sfname = sfname; | |
2482 return; | |
2483 } | |
2484 curbuf->b_flags |= BF_NOTEDITED; | |
29 | 2485 if (xfname != NULL && *xfname != NUL) |
2486 { | |
2487 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0); | |
2488 if (buf != NULL && !cmdmod.keepalt) | |
2489 curwin->w_alt_fnum = buf->b_fnum; | |
2490 } | |
7 | 2491 vim_free(fname); |
2492 vim_free(sfname); | |
2493 #ifdef FEAT_AUTOCMD | |
2494 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); | |
2495 #endif | |
961 | 2496 /* Change directories when the 'acd' option is set. */ |
2497 DO_AUTOCHDIR | |
7 | 2498 } |
2499 /* print full file name if :cd used */ | |
2500 fileinfo(FALSE, FALSE, eap->forceit); | |
2501 } | |
2502 | |
2503 /* | |
2504 * ":update". | |
2505 */ | |
2506 void | |
2507 ex_update(eap) | |
2508 exarg_T *eap; | |
2509 { | |
2510 if (curbufIsChanged()) | |
2511 (void)do_write(eap); | |
2512 } | |
2513 | |
2514 /* | |
2515 * ":write" and ":saveas". | |
2516 */ | |
2517 void | |
2518 ex_write(eap) | |
2519 exarg_T *eap; | |
2520 { | |
2521 if (eap->usefilter) /* input lines to shell command */ | |
2522 do_bang(1, eap, FALSE, TRUE, FALSE); | |
2523 else | |
2524 (void)do_write(eap); | |
2525 } | |
2526 | |
2527 /* | |
2528 * write current buffer to file 'eap->arg' | |
2529 * if 'eap->append' is TRUE, append to the file | |
2530 * | |
2531 * if *eap->arg == NUL write to current file | |
2532 * | |
2533 * return FAIL for failure, OK otherwise | |
2534 */ | |
2535 int | |
2536 do_write(eap) | |
2537 exarg_T *eap; | |
2538 { | |
2539 int other; | |
2540 char_u *fname = NULL; /* init to shut up gcc */ | |
2541 char_u *ffname; | |
2542 int retval = FAIL; | |
2543 char_u *free_fname = NULL; | |
2544 #ifdef FEAT_BROWSE | |
2545 char_u *browse_file = NULL; | |
2546 #endif | |
2547 buf_T *alt_buf = NULL; | |
2548 | |
2549 if (not_writing()) /* check 'write' option */ | |
2550 return FAIL; | |
2551 | |
2552 ffname = eap->arg; | |
2553 #ifdef FEAT_BROWSE | |
2554 if (cmdmod.browse) | |
2555 { | |
29 | 2556 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname, |
7 | 2557 NULL, NULL, NULL, curbuf); |
2558 if (browse_file == NULL) | |
2559 goto theend; | |
2560 ffname = browse_file; | |
2561 } | |
2562 #endif | |
2563 if (*ffname == NUL) | |
2564 { | |
2565 if (eap->cmdidx == CMD_saveas) | |
2566 { | |
2567 EMSG(_(e_argreq)); | |
2568 goto theend; | |
2569 } | |
2570 other = FALSE; | |
2571 } | |
2572 else | |
2573 { | |
2574 fname = ffname; | |
2575 free_fname = fix_fname(ffname); | |
2576 /* | |
2577 * When out-of-memory, keep unexpanded file name, because we MUST be | |
2578 * able to write the file in this situation. | |
2579 */ | |
2580 if (free_fname != NULL) | |
2581 ffname = free_fname; | |
2582 other = otherfile(ffname); | |
2583 } | |
2584 | |
2585 /* | |
2586 * If we have a new file, put its name in the list of alternate file names. | |
2587 */ | |
2588 if (other) | |
2589 { | |
2590 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL | |
2591 || eap->cmdidx == CMD_saveas) | |
2592 alt_buf = setaltfname(ffname, fname, (linenr_T)1); | |
2593 else | |
2594 alt_buf = buflist_findname(ffname); | |
2595 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) | |
2596 { | |
2597 /* Overwriting a file that is loaded in another buffer is not a | |
2598 * good idea. */ | |
315 | 2599 EMSG(_(e_bufloaded)); |
7 | 2600 goto theend; |
2601 } | |
2602 } | |
2603 | |
2604 /* | |
2605 * Writing to the current file is not allowed in readonly mode | |
2606 * and a file name is required. | |
2607 * "nofile" and "nowrite" buffers cannot be written implicitly either. | |
2608 */ | |
2609 if (!other && ( | |
2610 #ifdef FEAT_QUICKFIX | |
2611 bt_dontwrite_msg(curbuf) || | |
2612 #endif | |
2613 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf))) | |
2614 goto theend; | |
2615 | |
2616 if (!other) | |
2617 { | |
2618 ffname = curbuf->b_ffname; | |
2619 fname = curbuf->b_fname; | |
2620 /* | |
2621 * Not writing the whole file is only allowed with '!'. | |
2622 */ | |
2623 if ( (eap->line1 != 1 | |
2624 || eap->line2 != curbuf->b_ml.ml_line_count) | |
2625 && !eap->forceit | |
2626 && !eap->append | |
2627 && !p_wa) | |
2628 { | |
2629 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
2630 if (p_confirm || cmdmod.confirm) | |
2631 { | |
2632 if (vim_dialog_yesno(VIM_QUESTION, NULL, | |
2633 (char_u *)_("Write partial file?"), 2) != VIM_YES) | |
2634 goto theend; | |
2635 eap->forceit = TRUE; | |
2636 } | |
2637 else | |
2638 #endif | |
2639 { | |
2640 EMSG(_("E140: Use ! to write partial buffer")); | |
2641 goto theend; | |
2642 } | |
2643 } | |
2644 } | |
2645 | |
2646 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) | |
2647 { | |
2648 if (eap->cmdidx == CMD_saveas && alt_buf != NULL) | |
2649 { | |
2650 #ifdef FEAT_AUTOCMD | |
2651 buf_T *was_curbuf = curbuf; | |
2652 | |
2653 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); | |
21 | 2654 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); |
7 | 2655 # ifdef FEAT_EVAL |
2656 if (curbuf != was_curbuf || aborting()) | |
2657 # else | |
2658 if (curbuf != was_curbuf) | |
2659 # endif | |
2660 { | |
2661 /* buffer changed, don't change name now */ | |
2662 retval = FAIL; | |
2663 goto theend; | |
2664 } | |
2665 #endif | |
2666 /* Exchange the file names for the current and the alternate | |
2667 * buffer. This makes it look like we are now editing the buffer | |
2668 * under the new name. Must be done before buf_write(), because | |
2669 * if there is no file name and 'cpo' contains 'F', it will set | |
2670 * the file name. */ | |
2671 fname = alt_buf->b_fname; | |
2672 alt_buf->b_fname = curbuf->b_fname; | |
2673 curbuf->b_fname = fname; | |
2674 fname = alt_buf->b_ffname; | |
2675 alt_buf->b_ffname = curbuf->b_ffname; | |
2676 curbuf->b_ffname = fname; | |
2677 fname = alt_buf->b_sfname; | |
2678 alt_buf->b_sfname = curbuf->b_sfname; | |
2679 curbuf->b_sfname = fname; | |
2680 buf_name_changed(curbuf); | |
2681 #ifdef FEAT_AUTOCMD | |
2682 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); | |
21 | 2683 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); |
7 | 2684 if (!alt_buf->b_p_bl) |
2685 { | |
2686 alt_buf->b_p_bl = TRUE; | |
2687 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); | |
2688 } | |
2689 # ifdef FEAT_EVAL | |
2690 if (curbuf != was_curbuf || aborting()) | |
2691 # else | |
2692 if (curbuf != was_curbuf) | |
2693 # endif | |
2694 { | |
2695 /* buffer changed, don't write the file */ | |
2696 retval = FAIL; | |
2697 goto theend; | |
2698 } | |
634 | 2699 |
2700 /* If 'filetype' was empty try detecting it now. */ | |
2701 if (*curbuf->b_p_ft == NUL) | |
2702 { | |
819 | 2703 if (au_has_group((char_u *)"filetypedetect")) |
2704 (void)do_doautocmd((char_u *)"filetypedetect BufRead", | |
2705 TRUE); | |
717 | 2706 do_modelines(0); |
634 | 2707 } |
2648 | 2708 |
2709 /* Autocommands may have changed buffer names, esp. when | |
2710 * 'autochdir' is set. */ | |
2711 fname = curbuf->b_sfname; | |
7 | 2712 #endif |
2713 } | |
2714 | |
2715 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, | |
2716 eap, eap->append, eap->forceit, TRUE, FALSE); | |
634 | 2717 |
819 | 2718 /* After ":saveas fname" reset 'readonly'. */ |
961 | 2719 if (eap->cmdidx == CMD_saveas) |
2720 { | |
2721 if (retval == OK) | |
1806 | 2722 { |
961 | 2723 curbuf->b_p_ro = FALSE; |
1806 | 2724 #ifdef FEAT_WINDOWS |
2725 redraw_tabline = TRUE; | |
2726 #endif | |
2727 } | |
961 | 2728 /* Change directories when the 'acd' option is set. */ |
2729 DO_AUTOCHDIR | |
2730 } | |
7 | 2731 } |
2732 | |
2733 theend: | |
2734 #ifdef FEAT_BROWSE | |
2735 vim_free(browse_file); | |
2736 #endif | |
2737 vim_free(free_fname); | |
2738 return retval; | |
2739 } | |
2740 | |
2741 /* | |
2742 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, | |
2743 * BF_NEW or BF_READERR, check for overwriting current file. | |
2744 * May set eap->forceit if a dialog says it's OK to overwrite. | |
2745 * Return OK if it's OK, FAIL if it is not. | |
2746 */ | |
2747 static int | |
2748 check_overwrite(eap, buf, fname, ffname, other) | |
2749 exarg_T *eap; | |
2750 buf_T *buf; | |
2751 char_u *fname; /* file name to be used (can differ from | |
2752 buf->ffname) */ | |
2753 char_u *ffname; /* full path version of fname */ | |
2754 int other; /* writing under other name */ | |
2755 { | |
2756 /* | |
2757 * write to other file or b_flags set or not writing the whole file: | |
2758 * overwriting only allowed with '!' | |
2759 */ | |
2760 if ( (other | |
2761 || (buf->b_flags & BF_NOTEDITED) | |
2762 || ((buf->b_flags & BF_NEW) | |
2763 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) | |
2764 || (buf->b_flags & BF_READERR)) | |
2765 && !p_wa | |
1457 | 2766 #ifdef FEAT_QUICKFIX |
2767 && !bt_nofile(buf) | |
2768 #endif | |
7 | 2769 && vim_fexists(ffname)) |
2770 { | |
460 | 2771 if (!eap->forceit && !eap->append) |
7 | 2772 { |
460 | 2773 #ifdef UNIX |
637 | 2774 /* with UNIX it is possible to open a directory */ |
460 | 2775 if (mch_isdir(ffname)) |
2776 { | |
2777 EMSG2(_(e_isadir2), ffname); | |
2778 return FAIL; | |
2779 } | |
7 | 2780 #endif |
2781 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
460 | 2782 if (p_confirm || cmdmod.confirm) |
2783 { | |
2784 char_u buff[IOSIZE]; | |
2785 | |
2786 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); | |
2787 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) | |
2788 return FAIL; | |
2789 eap->forceit = TRUE; | |
2790 } | |
2791 else | |
2792 #endif | |
2793 { | |
2794 EMSG(_(e_exists)); | |
7 | 2795 return FAIL; |
460 | 2796 } |
7 | 2797 } |
460 | 2798 |
2799 /* For ":w! filename" check that no swap file exists for "filename". */ | |
2800 if (other && !emsg_silent) | |
7 | 2801 { |
460 | 2802 char_u dir[MAXPATHL]; |
2803 char_u *p; | |
2804 int r; | |
2805 char_u *swapname; | |
2806 | |
2807 /* We only try the first entry in 'directory', without checking if | |
2808 * it's writable. If the "." directory is not writable the write | |
2809 * will probably fail anyway. | |
2810 * Use 'shortname' of the current buffer, since there is no buffer | |
2811 * for the written file. */ | |
2812 if (*p_dir == NUL) | |
2813 STRCPY(dir, "."); | |
2814 else | |
2815 { | |
2816 p = p_dir; | |
2817 copy_option_part(&p, dir, MAXPATHL, ","); | |
2818 } | |
2819 swapname = makeswapname(fname, ffname, curbuf, dir); | |
2820 r = vim_fexists(swapname); | |
2821 if (r) | |
2822 { | |
2823 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
2824 if (p_confirm || cmdmod.confirm) | |
2825 { | |
2826 char_u buff[IOSIZE]; | |
2827 | |
2828 dialog_msg(buff, | |
2829 _("Swap file \"%s\" exists, overwrite anyway?"), | |
2830 swapname); | |
2831 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) | |
2832 != VIM_YES) | |
819 | 2833 { |
2834 vim_free(swapname); | |
460 | 2835 return FAIL; |
819 | 2836 } |
460 | 2837 eap->forceit = TRUE; |
2838 } | |
2839 else | |
2840 #endif | |
2841 { | |
2842 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), | |
2843 swapname); | |
819 | 2844 vim_free(swapname); |
460 | 2845 return FAIL; |
2846 } | |
2847 } | |
819 | 2848 vim_free(swapname); |
7 | 2849 } |
2850 } | |
2851 return OK; | |
2852 } | |
2853 | |
2854 /* | |
2855 * Handle ":wnext", ":wNext" and ":wprevious" commands. | |
2856 */ | |
2857 void | |
2858 ex_wnext(eap) | |
2859 exarg_T *eap; | |
2860 { | |
2861 int i; | |
2862 | |
2863 if (eap->cmd[1] == 'n') | |
2864 i = curwin->w_arg_idx + (int)eap->line2; | |
2865 else | |
2866 i = curwin->w_arg_idx - (int)eap->line2; | |
2867 eap->line1 = 1; | |
2868 eap->line2 = curbuf->b_ml.ml_line_count; | |
2869 if (do_write(eap) != FAIL) | |
2870 do_argfile(eap, i); | |
2871 } | |
2872 | |
2873 /* | |
2874 * ":wall", ":wqall" and ":xall": Write all changed files (and exit). | |
2875 */ | |
2876 void | |
2877 do_wqall(eap) | |
2878 exarg_T *eap; | |
2879 { | |
2880 buf_T *buf; | |
2881 int error = 0; | |
2882 int save_forceit = eap->forceit; | |
2883 | |
2884 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall) | |
2885 exiting = TRUE; | |
2886 | |
2887 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2888 { | |
2889 if (bufIsChanged(buf)) | |
2890 { | |
2891 /* | |
2892 * Check if there is a reason the buffer cannot be written: | |
2893 * 1. if the 'write' option is set | |
2894 * 2. if there is no file name (even after browsing) | |
2895 * 3. if the 'readonly' is set (even after a dialog) | |
2896 * 4. if overwriting is allowed (even after a dialog) | |
2897 */ | |
2898 if (not_writing()) | |
2899 { | |
2900 ++error; | |
2901 break; | |
2902 } | |
2903 #ifdef FEAT_BROWSE | |
2904 /* ":browse wall": ask for file name if there isn't one */ | |
2905 if (buf->b_ffname == NULL && cmdmod.browse) | |
2906 browse_save_fname(buf); | |
2907 #endif | |
2908 if (buf->b_ffname == NULL) | |
2909 { | |
2910 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum); | |
2911 ++error; | |
2912 } | |
2913 else if (check_readonly(&eap->forceit, buf) | |
2914 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, | |
2915 FALSE) == FAIL) | |
2916 { | |
2917 ++error; | |
2918 } | |
2919 else | |
2920 { | |
2921 if (buf_write_all(buf, eap->forceit) == FAIL) | |
2922 ++error; | |
2923 #ifdef FEAT_AUTOCMD | |
2924 /* an autocommand may have deleted the buffer */ | |
2925 if (!buf_valid(buf)) | |
2926 buf = firstbuf; | |
2927 #endif | |
2928 } | |
2929 eap->forceit = save_forceit; /* check_overwrite() may set it */ | |
2930 } | |
2931 } | |
2932 if (exiting) | |
2933 { | |
2934 if (!error) | |
2935 getout(0); /* exit Vim */ | |
2936 not_exiting(); | |
2937 } | |
2938 } | |
2939 | |
2940 /* | |
2941 * Check the 'write' option. | |
2942 * Return TRUE and give a message when it's not st. | |
2943 */ | |
2944 int | |
2945 not_writing() | |
2946 { | |
2947 if (p_write) | |
2948 return FALSE; | |
2949 EMSG(_("E142: File not written: Writing is disabled by 'write' option")); | |
2950 return TRUE; | |
2951 } | |
2952 | |
2953 /* | |
1303 | 2954 * Check if a buffer is read-only (either 'readonly' option is set or file is |
2955 * read-only). Ask for overruling in a dialog. Return TRUE and give an error | |
2956 * message when the buffer is readonly. | |
7 | 2957 */ |
2958 static int | |
2959 check_readonly(forceit, buf) | |
2960 int *forceit; | |
2961 buf_T *buf; | |
2962 { | |
1303 | 2963 struct stat st; |
2964 | |
2965 /* Handle a file being readonly when the 'readonly' option is set or when | |
2966 * the file exists and permissions are read-only. | |
2967 * We will send 0777 to check_file_readonly(), as the "perm" variable is | |
2968 * important for device checks but not here. */ | |
2969 if (!*forceit && (buf->b_p_ro | |
2970 || (mch_stat((char *)buf->b_ffname, &st) >= 0 | |
2971 && check_file_readonly(buf->b_ffname, 0777)))) | |
7 | 2972 { |
2973 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
2974 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) | |
2975 { | |
2976 char_u buff[IOSIZE]; | |
2977 | |
1303 | 2978 if (buf->b_p_ro) |
2979 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), | |
2980 buf->b_fname); | |
2981 else | |
2982 dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), | |
7 | 2983 buf->b_fname); |
2984 | |
2985 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) | |
2986 { | |
2987 /* Set forceit, to force the writing of a readonly file */ | |
2988 *forceit = TRUE; | |
2989 return FALSE; | |
2990 } | |
2991 else | |
2992 return TRUE; | |
2993 } | |
2994 else | |
2995 #endif | |
1303 | 2996 if (buf->b_p_ro) |
7 | 2997 EMSG(_(e_readonly)); |
1303 | 2998 else |
2999 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"), | |
3000 buf->b_fname); | |
7 | 3001 return TRUE; |
3002 } | |
1303 | 3003 |
7 | 3004 return FALSE; |
3005 } | |
3006 | |
3007 /* | |
632 | 3008 * Try to abandon current file and edit a new or existing file. |
3009 * 'fnum' is the number of the file, if zero use ffname/sfname. | |
7 | 3010 * |
632 | 3011 * Return 1 for "normal" error, 2 for "not written" error, 0 for success |
1370 | 3012 * -1 for successfully opening another file. |
7 | 3013 * 'lnum' is the line number for the cursor in the new file (if non-zero). |
3014 */ | |
3015 int | |
3016 getfile(fnum, ffname, sfname, setpm, lnum, forceit) | |
3017 int fnum; | |
3018 char_u *ffname; | |
3019 char_u *sfname; | |
3020 int setpm; | |
3021 linenr_T lnum; | |
3022 int forceit; | |
3023 { | |
3024 int other; | |
3025 int retval; | |
3026 char_u *free_me = NULL; | |
3027 | |
634 | 3028 if (text_locked()) |
7 | 3029 return 1; |
819 | 3030 #ifdef FEAT_AUTOCMD |
3031 if (curbuf_locked()) | |
3032 return 1; | |
3033 #endif | |
7 | 3034 |
3035 if (fnum == 0) | |
3036 { | |
3037 /* make ffname full path, set sfname */ | |
3038 fname_expand(curbuf, &ffname, &sfname); | |
3039 other = otherfile(ffname); | |
3040 free_me = ffname; /* has been allocated, free() later */ | |
3041 } | |
3042 else | |
3043 other = (fnum != curbuf->b_fnum); | |
3044 | |
3045 if (other) | |
3046 ++no_wait_return; /* don't wait for autowrite message */ | |
3047 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf) | |
3048 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) | |
3049 { | |
3050 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
3051 if (p_confirm && p_write) | |
3052 dialog_changed(curbuf, FALSE); | |
3053 if (curbufIsChanged()) | |
3054 #endif | |
3055 { | |
3056 if (other) | |
3057 --no_wait_return; | |
3058 EMSG(_(e_nowrtmsg)); | |
3059 retval = 2; /* file has been changed */ | |
3060 goto theend; | |
3061 } | |
3062 } | |
3063 if (other) | |
3064 --no_wait_return; | |
3065 if (setpm) | |
3066 setpcmark(); | |
3067 if (!other) | |
3068 { | |
3069 if (lnum != 0) | |
3070 curwin->w_cursor.lnum = lnum; | |
3071 check_cursor_lnum(); | |
3072 beginline(BL_SOL | BL_FIX); | |
3073 retval = 0; /* it's in the same file */ | |
3074 } | |
3075 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum, | |
1743 | 3076 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0), |
3077 curwin) == OK) | |
7 | 3078 retval = -1; /* opened another file */ |
3079 else | |
3080 retval = 1; /* error encountered */ | |
3081 | |
3082 theend: | |
3083 vim_free(free_me); | |
3084 return retval; | |
3085 } | |
3086 | |
3087 /* | |
3088 * start editing a new file | |
3089 * | |
3090 * fnum: file number; if zero use ffname/sfname | |
3091 * ffname: the file name | |
3092 * - full path if sfname used, | |
3093 * - any file name if sfname is NULL | |
3094 * - empty string to re-edit with the same file name (but may be | |
3095 * in a different directory) | |
3096 * - NULL to start an empty buffer | |
3097 * sfname: the short file name (or NULL) | |
3098 * eap: contains the command to be executed after loading the file and | |
3099 * forced 'ff' and 'fenc' | |
3100 * newlnum: if > 0: put cursor on this line number (if possible) | |
3101 * if ECMD_LASTL: use last position in loaded file | |
3102 * if ECMD_LAST: use last position in all files | |
3103 * if ECMD_ONE: use first line | |
3104 * flags: | |
3105 * ECMD_HIDE: if TRUE don't free the current buffer | |
3106 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file | |
3107 * ECMD_OLDBUF: use existing buffer if it exists | |
3108 * ECMD_FORCEIT: ! used for Ex command | |
3109 * ECMD_ADDBUF: don't edit, just add to buffer list | |
1743 | 3110 * oldwin: Should be "curwin" when editing a new buffer in the current |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
3111 * window, NULL when splitting the window first. When not NULL info |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
3112 * of the previous buffer for "oldwin" is stored. |
7 | 3113 * |
3114 * return FAIL for failure, OK otherwise | |
3115 */ | |
3116 int | |
1743 | 3117 do_ecmd(fnum, ffname, sfname, eap, newlnum, flags, oldwin) |
7 | 3118 int fnum; |
3119 char_u *ffname; | |
3120 char_u *sfname; | |
3121 exarg_T *eap; /* can be NULL! */ | |
3122 linenr_T newlnum; | |
3123 int flags; | |
1743 | 3124 win_T *oldwin; |
7 | 3125 { |
3126 int other_file; /* TRUE if editing another file */ | |
3127 int oldbuf; /* TRUE if using existing buffer */ | |
3128 #ifdef FEAT_AUTOCMD | |
3129 int auto_buf = FALSE; /* TRUE if autocommands brought us | |
3130 into the buffer unexpectedly */ | |
3131 char_u *new_name = NULL; | |
716 | 3132 int did_set_swapcommand = FALSE; |
7 | 3133 #endif |
3134 buf_T *buf; | |
3135 #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
3136 buf_T *old_curbuf = curbuf; | |
3137 #endif | |
3138 char_u *free_fname = NULL; | |
3139 #ifdef FEAT_BROWSE | |
3140 char_u *browse_file = NULL; | |
3141 #endif | |
3142 int retval = FAIL; | |
3143 long n; | |
3144 linenr_T lnum; | |
3145 linenr_T topline = 0; | |
3146 int newcol = -1; | |
3147 int solcol = -1; | |
3148 pos_T *pos; | |
3149 #ifdef FEAT_SUN_WORKSHOP | |
3150 char_u *cp; | |
3151 #endif | |
3152 char_u *command = NULL; | |
1185 | 3153 #ifdef FEAT_SPELL |
3154 int did_get_winopts = FALSE; | |
3155 #endif | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3156 int readfile_flags = 0; |
7 | 3157 |
3158 if (eap != NULL) | |
3159 command = eap->do_ecmd_cmd; | |
3160 | |
3161 if (fnum != 0) | |
3162 { | |
3163 if (fnum == curbuf->b_fnum) /* file is already being edited */ | |
3164 return OK; /* nothing to do */ | |
3165 other_file = TRUE; | |
3166 } | |
3167 else | |
3168 { | |
3169 #ifdef FEAT_BROWSE | |
3170 if (cmdmod.browse) | |
3171 { | |
1683 | 3172 # ifdef FEAT_AUTOCMD |
462 | 3173 if ( |
1683 | 3174 # ifdef FEAT_GUI |
462 | 3175 !gui.in_use && |
1683 | 3176 # endif |
462 | 3177 au_has_group((char_u *)"FileExplorer")) |
3178 { | |
3179 /* No browsing supported but we do have the file explorer: | |
3180 * Edit the directory. */ | |
3181 if (ffname == NULL || !mch_isdir(ffname)) | |
3182 ffname = (char_u *)"."; | |
3183 } | |
3184 else | |
1683 | 3185 # endif |
462 | 3186 { |
3187 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, | |
7 | 3188 NULL, NULL, NULL, curbuf); |
462 | 3189 if (browse_file == NULL) |
3190 goto theend; | |
3191 ffname = browse_file; | |
3192 } | |
7 | 3193 } |
3194 #endif | |
3195 /* if no short name given, use ffname for short name */ | |
3196 if (sfname == NULL) | |
3197 sfname = ffname; | |
3198 #ifdef USE_FNAME_CASE | |
3199 # ifdef USE_LONG_FNAME | |
3200 if (USE_LONG_FNAME) | |
3201 # endif | |
436 | 3202 if (sfname != NULL) |
3203 fname_case(sfname, 0); /* set correct case for sfname */ | |
7 | 3204 #endif |
3205 | |
3206 #ifdef FEAT_LISTCMDS | |
3207 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL)) | |
3208 goto theend; | |
3209 #endif | |
3210 | |
3211 if (ffname == NULL) | |
3212 other_file = TRUE; | |
3213 /* there is no file name */ | |
3214 else if (*ffname == NUL && curbuf->b_ffname == NULL) | |
3215 other_file = FALSE; | |
3216 else | |
3217 { | |
3218 if (*ffname == NUL) /* re-edit with same file name */ | |
3219 { | |
3220 ffname = curbuf->b_ffname; | |
3221 sfname = curbuf->b_fname; | |
3222 } | |
3223 free_fname = fix_fname(ffname); /* may expand to full path name */ | |
3224 if (free_fname != NULL) | |
3225 ffname = free_fname; | |
3226 other_file = otherfile(ffname); | |
3227 #ifdef FEAT_SUN_WORKSHOP | |
3228 if (usingSunWorkShop && p_acd | |
3229 && (cp = vim_strrchr(sfname, '/')) != NULL) | |
3230 sfname = ++cp; | |
3231 #endif | |
3232 } | |
3233 } | |
3234 | |
3235 /* | |
3236 * if the file was changed we may not be allowed to abandon it | |
3237 * - if we are going to re-edit the same file | |
3238 * - or if we are the only window on this file and if ECMD_HIDE is FALSE | |
3239 */ | |
3240 if ( ((!other_file && !(flags & ECMD_OLDBUF)) | |
3241 || (curbuf->b_nwindows == 1 | |
3242 && !(flags & (ECMD_HIDE | ECMD_ADDBUF)))) | |
3243 && check_changed(curbuf, p_awa, !other_file, | |
3244 (flags & ECMD_FORCEIT), FALSE)) | |
3245 { | |
3246 if (fnum == 0 && other_file && ffname != NULL) | |
3247 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum); | |
3248 goto theend; | |
3249 } | |
3250 | |
3251 #ifdef FEAT_VISUAL | |
3252 /* | |
3253 * End Visual mode before switching to another buffer, so the text can be | |
3254 * copied into the GUI selection buffer. | |
3255 */ | |
3256 reset_VIsual(); | |
3257 #endif | |
3258 | |
716 | 3259 #ifdef FEAT_AUTOCMD |
3260 if ((command != NULL || newlnum > (linenr_T)0) | |
3261 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) | |
3262 { | |
3263 int len; | |
3264 char_u *p; | |
3265 | |
3266 /* Set v:swapcommand for the SwapExists autocommands. */ | |
3267 if (command != NULL) | |
835 | 3268 len = (int)STRLEN(command) + 3; |
716 | 3269 else |
3270 len = 30; | |
3271 p = alloc((unsigned)len); | |
3272 if (p != NULL) | |
3273 { | |
3274 if (command != NULL) | |
3275 vim_snprintf((char *)p, len, ":%s\r", command); | |
3276 else | |
3277 vim_snprintf((char *)p, len, "%ldG", (long)newlnum); | |
3278 set_vim_var_string(VV_SWAPCOMMAND, p, -1); | |
3279 did_set_swapcommand = TRUE; | |
3280 vim_free(p); | |
3281 } | |
3282 } | |
3283 #endif | |
3284 | |
7 | 3285 /* |
3286 * If we are starting to edit another file, open a (new) buffer. | |
3287 * Otherwise we re-use the current buffer. | |
3288 */ | |
3289 if (other_file) | |
3290 { | |
3291 #ifdef FEAT_LISTCMDS | |
3292 if (!(flags & ECMD_ADDBUF)) | |
3293 #endif | |
3294 { | |
22 | 3295 if (!cmdmod.keepalt) |
3296 curwin->w_alt_fnum = curbuf->b_fnum; | |
1743 | 3297 if (oldwin != NULL) |
3298 buflist_altfpos(oldwin); | |
7 | 3299 } |
3300 | |
3301 if (fnum) | |
3302 buf = buflist_findnr(fnum); | |
3303 else | |
3304 { | |
3305 #ifdef FEAT_LISTCMDS | |
3306 if (flags & ECMD_ADDBUF) | |
3307 { | |
3308 linenr_T tlnum = 1L; | |
3309 | |
3310 if (command != NULL) | |
3311 { | |
3312 tlnum = atol((char *)command); | |
3313 if (tlnum <= 0) | |
3314 tlnum = 1L; | |
3315 } | |
3316 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED); | |
3317 goto theend; | |
3318 } | |
3319 #endif | |
3320 buf = buflist_new(ffname, sfname, 0L, | |
3321 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); | |
3322 } | |
3323 if (buf == NULL) | |
3324 goto theend; | |
3325 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */ | |
3326 { | |
3327 oldbuf = FALSE; | |
3328 buf->b_nwindows = 0; | |
3329 } | |
3330 else /* existing memfile */ | |
3331 { | |
3332 oldbuf = TRUE; | |
3333 (void)buf_check_timestamp(buf, FALSE); | |
3334 /* Check if autocommands made buffer invalid or changed the current | |
3335 * buffer. */ | |
3336 if (!buf_valid(buf) | |
3337 #ifdef FEAT_AUTOCMD | |
3338 || curbuf != old_curbuf | |
3339 #endif | |
3340 ) | |
3341 goto theend; | |
3342 #ifdef FEAT_EVAL | |
3343 if (aborting()) /* autocmds may abort script processing */ | |
3344 goto theend; | |
3345 #endif | |
3346 } | |
3347 | |
3348 /* May jump to last used line number for a loaded buffer or when asked | |
3349 * for explicitly */ | |
3350 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST) | |
3351 { | |
3352 pos = buflist_findfpos(buf); | |
3353 newlnum = pos->lnum; | |
3354 solcol = pos->col; | |
3355 } | |
3356 | |
3357 /* | |
3358 * Make the (new) buffer the one used by the current window. | |
3359 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE. | |
3360 * If the current buffer was empty and has no file name, curbuf | |
3361 * is returned by buflist_new(). | |
3362 */ | |
3363 if (buf != curbuf) | |
3364 { | |
3365 #ifdef FEAT_AUTOCMD | |
3366 /* | |
3367 * Be careful: The autocommands may delete any buffer and change | |
3368 * the current buffer. | |
3369 * - If the buffer we are going to edit is deleted, give up. | |
3370 * - If the current buffer is deleted, prefer to load the new | |
3371 * buffer when loading a buffer is required. This avoids | |
3372 * loading another buffer which then must be closed again. | |
3373 * - If we ended up in the new buffer already, need to skip a few | |
3374 * things, set auto_buf. | |
3375 */ | |
3376 if (buf->b_fname != NULL) | |
3377 new_name = vim_strsave(buf->b_fname); | |
3378 au_new_curbuf = buf; | |
3379 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); | |
3380 if (!buf_valid(buf)) /* new buffer has been deleted */ | |
3381 { | |
3382 delbuf_msg(new_name); /* frees new_name */ | |
3383 goto theend; | |
3384 } | |
3385 # ifdef FEAT_EVAL | |
3386 if (aborting()) /* autocmds may abort script processing */ | |
3387 { | |
3388 vim_free(new_name); | |
3389 goto theend; | |
3390 } | |
3391 # endif | |
3392 if (buf == curbuf) /* already in new buffer */ | |
3393 auto_buf = TRUE; | |
3394 else | |
3395 { | |
3396 if (curbuf == old_curbuf) | |
3397 #endif | |
3398 buf_copy_options(buf, BCO_ENTER); | |
3399 | |
3400 /* close the link to the current buffer */ | |
825 | 3401 u_sync(FALSE); |
1743 | 3402 close_buffer(oldwin, curbuf, |
7 | 3403 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD); |
3404 | |
3405 #ifdef FEAT_AUTOCMD | |
3406 # ifdef FEAT_EVAL | |
3407 if (aborting()) /* autocmds may abort script processing */ | |
3408 { | |
3409 vim_free(new_name); | |
3410 goto theend; | |
3411 } | |
3412 # endif | |
3413 /* Be careful again, like above. */ | |
3414 if (!buf_valid(buf)) /* new buffer has been deleted */ | |
3415 { | |
3416 delbuf_msg(new_name); /* frees new_name */ | |
3417 goto theend; | |
3418 } | |
3419 if (buf == curbuf) /* already in new buffer */ | |
3420 auto_buf = TRUE; | |
3421 else | |
3422 #endif | |
3423 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3424 #ifdef FEAT_SYN_HL |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3425 /* |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3426 * <VN> We could instead free the synblock |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3427 * and re-attach to buffer, perhaps. |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3428 */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3429 if (curwin->w_s == &(curwin->w_buffer->b_s)) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3430 curwin->w_s = &(buf->b_s); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3431 #endif |
7 | 3432 curwin->w_buffer = buf; |
3433 curbuf = buf; | |
3434 ++curbuf->b_nwindows; | |
3435 /* set 'fileformat' */ | |
3436 if (*p_ffs && !oldbuf) | |
3437 set_fileformat(default_fileformat(), OPT_LOCAL); | |
3438 } | |
3439 | |
3440 /* May get the window options from the last time this buffer | |
3441 * was in this window (or another window). If not used | |
3442 * before, reset the local window options to the global | |
3443 * values. Also restores old folding stuff. */ | |
1289 | 3444 get_winopts(curbuf); |
1185 | 3445 #ifdef FEAT_SPELL |
3446 did_get_winopts = TRUE; | |
3447 #endif | |
7 | 3448 |
3449 #ifdef FEAT_AUTOCMD | |
3450 } | |
3451 vim_free(new_name); | |
3452 au_new_curbuf = NULL; | |
3453 #endif | |
3454 } | |
3455 else | |
3456 ++curbuf->b_nwindows; | |
3457 | |
3458 curwin->w_pcmark.lnum = 1; | |
3459 curwin->w_pcmark.col = 0; | |
3460 } | |
3461 else /* !other_file */ | |
3462 { | |
3463 if ( | |
3464 #ifdef FEAT_LISTCMDS | |
3465 (flags & ECMD_ADDBUF) || | |
3466 #endif | |
3467 check_fname() == FAIL) | |
3468 goto theend; | |
3469 oldbuf = (flags & ECMD_OLDBUF); | |
3470 } | |
3471 | |
3472 if ((flags & ECMD_SET_HELP) || keep_help_flag) | |
3473 { | |
3474 char_u *p; | |
3475 | |
3476 curbuf->b_help = TRUE; | |
3477 #ifdef FEAT_QUICKFIX | |
3478 set_string_option_direct((char_u *)"buftype", -1, | |
694 | 3479 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0); |
7 | 3480 #endif |
3481 | |
3482 /* | |
3483 * Always set these options after jumping to a help tag, because the | |
3484 * user may have an autocommand that gets in the way. | |
3485 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and | |
3486 * latin1 word characters (for translated help files). | |
3487 * Only set it when needed, buf_init_chartab() is some work. | |
3488 */ | |
3489 p = | |
3490 #ifdef EBCDIC | |
3491 (char_u *)"65-255,^*,^|,^\""; | |
3492 #else | |
3493 (char_u *)"!-~,^*,^|,^\",192-255"; | |
3494 #endif | |
3495 if (STRCMP(curbuf->b_p_isk, p) != 0) | |
3496 { | |
3497 set_string_option_direct((char_u *)"isk", -1, p, | |
694 | 3498 OPT_FREE|OPT_LOCAL, 0); |
7 | 3499 check_buf_options(curbuf); |
3500 (void)buf_init_chartab(curbuf, FALSE); | |
3501 } | |
3502 | |
3503 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */ | |
3504 curwin->w_p_list = FALSE; /* no list mode */ | |
3505 | |
3506 curbuf->b_p_ma = FALSE; /* not modifiable */ | |
3507 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */ | |
3508 curwin->w_p_nu = 0; /* no line numbers */ | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2127
diff
changeset
|
3509 curwin->w_p_rnu = 0; /* no relative line numbers */ |
2583 | 3510 RESET_BINDING(curwin); /* no scroll or cursor binding */ |
7 | 3511 #ifdef FEAT_ARABIC |
3512 curwin->w_p_arab = FALSE; /* no arabic mode */ | |
3513 #endif | |
3514 #ifdef FEAT_RIGHTLEFT | |
3515 curwin->w_p_rl = FALSE; /* help window is left-to-right */ | |
3516 #endif | |
3517 #ifdef FEAT_FOLDING | |
3518 curwin->w_p_fen = FALSE; /* No folding in the help window */ | |
3519 #endif | |
3520 #ifdef FEAT_DIFF | |
3521 curwin->w_p_diff = FALSE; /* No 'diff' */ | |
3522 #endif | |
748 | 3523 #ifdef FEAT_SPELL |
371 | 3524 curwin->w_p_spell = FALSE; /* No spell checking */ |
3525 #endif | |
7 | 3526 |
3527 #ifdef FEAT_AUTOCMD | |
3528 buf = curbuf; | |
3529 #endif | |
3530 set_buflisted(FALSE); | |
3531 } | |
3532 else | |
3533 { | |
3534 #ifdef FEAT_AUTOCMD | |
3535 buf = curbuf; | |
3536 #endif | |
3537 /* Don't make a buffer listed if it's a help buffer. Useful when | |
3538 * using CTRL-O to go back to a help file. */ | |
3539 if (!curbuf->b_help) | |
3540 set_buflisted(TRUE); | |
3541 } | |
3542 | |
3543 #ifdef FEAT_AUTOCMD | |
3544 /* If autocommands change buffers under our fingers, forget about | |
3545 * editing the file. */ | |
3546 if (buf != curbuf) | |
3547 goto theend; | |
3548 # ifdef FEAT_EVAL | |
3549 if (aborting()) /* autocmds may abort script processing */ | |
3550 goto theend; | |
3551 # endif | |
3552 | |
3553 /* Since we are starting to edit a file, consider the filetype to be | |
3554 * unset. Helps for when an autocommand changes files and expects syntax | |
3555 * highlighting to work in the other file. */ | |
3556 did_filetype = FALSE; | |
3557 #endif | |
3558 | |
3559 /* | |
3560 * other_file oldbuf | |
3561 * FALSE FALSE re-edit same file, buffer is re-used | |
3562 * FALSE TRUE re-edit same file, nothing changes | |
3563 * TRUE FALSE start editing new file, new buffer | |
3564 * TRUE TRUE start editing in existing buffer (nothing to do) | |
3565 */ | |
3566 if (!other_file && !oldbuf) /* re-use the buffer */ | |
3567 { | |
3568 set_last_cursor(curwin); /* may set b_last_cursor */ | |
3569 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) | |
3570 { | |
3571 newlnum = curwin->w_cursor.lnum; | |
3572 solcol = curwin->w_cursor.col; | |
3573 } | |
3574 #ifdef FEAT_AUTOCMD | |
3575 buf = curbuf; | |
3576 if (buf->b_fname != NULL) | |
3577 new_name = vim_strsave(buf->b_fname); | |
3578 else | |
3579 new_name = NULL; | |
3580 #endif | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3581 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3582 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3583 /* Save all the text, so that the reload can be undone. |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3584 * Sync first so that this is a separate undo-able action. */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3585 u_sync(FALSE); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3586 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3587 == FAIL) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3588 goto theend; |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3589 u_unchanged(curbuf); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3590 buf_freeall(curbuf, BFA_KEEP_UNDO); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3591 |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3592 /* tell readfile() not to clear or reload undo info */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3593 readfile_flags = READ_KEEP_UNDO; |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3594 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3595 else |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3596 buf_freeall(curbuf, 0); /* free all things for buffer */ |
7 | 3597 #ifdef FEAT_AUTOCMD |
3598 /* If autocommands deleted the buffer we were going to re-edit, give | |
3599 * up and jump to the end. */ | |
3600 if (!buf_valid(buf)) | |
3601 { | |
3602 delbuf_msg(new_name); /* frees new_name */ | |
3603 goto theend; | |
3604 } | |
3605 vim_free(new_name); | |
3606 | |
3607 /* If autocommands change buffers under our fingers, forget about | |
3608 * re-editing the file. Should do the buf_clear_file(), but perhaps | |
3609 * the autocommands changed the buffer... */ | |
3610 if (buf != curbuf) | |
3611 goto theend; | |
3612 # ifdef FEAT_EVAL | |
3613 if (aborting()) /* autocmds may abort script processing */ | |
3614 goto theend; | |
3615 # endif | |
3616 #endif | |
3617 buf_clear_file(curbuf); | |
3618 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ | |
3619 curbuf->b_op_end.lnum = 0; | |
3620 } | |
3621 | |
3622 /* | |
3623 * If we get here we are sure to start editing | |
3624 */ | |
3625 /* don't redraw until the cursor is in the right line */ | |
3626 ++RedrawingDisabled; | |
3627 | |
3628 /* Assume success now */ | |
3629 retval = OK; | |
3630 | |
3631 /* | |
3632 * Reset cursor position, could be used by autocommands. | |
3633 */ | |
3634 check_cursor(); | |
3635 | |
3636 /* | |
3637 * Check if we are editing the w_arg_idx file in the argument list. | |
3638 */ | |
3639 check_arg_idx(curwin); | |
3640 | |
2254
4620acaf4814
One more fix for conceal patch.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3641 #ifdef FEAT_SYN_HL |
4620acaf4814
One more fix for conceal patch.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3642 reset_synblock(curwin); /* remove any ownsyntax */ |
4620acaf4814
One more fix for conceal patch.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3643 #endif |
4620acaf4814
One more fix for conceal patch.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3644 |
7 | 3645 #ifdef FEAT_AUTOCMD |
3646 if (!auto_buf) | |
3647 #endif | |
3648 { | |
3649 /* | |
3650 * Set cursor and init window before reading the file and executing | |
3651 * autocommands. This allows for the autocommands to position the | |
3652 * cursor. | |
3653 */ | |
677 | 3654 curwin_init(); |
7 | 3655 |
3656 #ifdef FEAT_FOLDING | |
1370 | 3657 /* It's possible that all lines in the buffer changed. Need to update |
3658 * automatic folding for all windows where it's used. */ | |
3659 # ifdef FEAT_WINDOWS | |
3660 { | |
3661 win_T *win; | |
3662 tabpage_T *tp; | |
3663 | |
3664 FOR_ALL_TAB_WINDOWS(tp, win) | |
3665 if (win->w_buffer == curbuf) | |
3666 foldUpdateAll(win); | |
3667 } | |
3668 # else | |
7 | 3669 foldUpdateAll(curwin); |
1370 | 3670 # endif |
7 | 3671 #endif |
3672 | |
961 | 3673 /* Change directories when the 'acd' option is set. */ |
3674 DO_AUTOCHDIR | |
3675 | |
7 | 3676 /* |
3677 * Careful: open_buffer() and apply_autocmds() may change the current | |
3678 * buffer and window. | |
3679 */ | |
3680 lnum = curwin->w_cursor.lnum; | |
3681 topline = curwin->w_topline; | |
3682 if (!oldbuf) /* need to read the file */ | |
3683 { | |
580 | 3684 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 3685 swap_exists_action = SEA_DIALOG; |
3686 #endif | |
3687 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ | |
3688 | |
3689 /* | |
3690 * Open the buffer and read the file. | |
3691 */ | |
3692 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3693 if (should_abort(open_buffer(FALSE, eap, readfile_flags))) |
7 | 3694 retval = FAIL; |
3695 #else | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3696 (void)open_buffer(FALSE, eap, readfile_flags); |
7 | 3697 #endif |
3698 | |
580 | 3699 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 3700 if (swap_exists_action == SEA_QUIT) |
3701 retval = FAIL; | |
3702 handle_swap_exists(old_curbuf); | |
3703 #endif | |
3704 } | |
3705 #ifdef FEAT_AUTOCMD | |
3706 else | |
3707 { | |
23 | 3708 /* Read the modelines, but only to set window-local options. Any |
3709 * buffer-local options have already been set and may have been | |
3710 * changed by the user. */ | |
717 | 3711 do_modelines(OPT_WINONLY); |
23 | 3712 |
7 | 3713 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, |
3714 &retval); | |
3715 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, | |
3716 &retval); | |
3717 } | |
3718 check_arg_idx(curwin); | |
3719 #endif | |
3720 | |
3721 /* | |
3722 * If autocommands change the cursor position or topline, we should | |
3723 * keep it. | |
3724 */ | |
3725 if (curwin->w_cursor.lnum != lnum) | |
3726 { | |
3727 newlnum = curwin->w_cursor.lnum; | |
3728 newcol = curwin->w_cursor.col; | |
3729 } | |
3730 if (curwin->w_topline == topline) | |
3731 topline = 0; | |
3732 | |
3733 /* Even when cursor didn't move we need to recompute topline. */ | |
3734 changed_line_abv_curs(); | |
3735 | |
3736 #ifdef FEAT_TITLE | |
3737 maketitle(); | |
3738 #endif | |
3739 } | |
3740 | |
3741 #ifdef FEAT_DIFF | |
3742 /* Tell the diff stuff that this buffer is new and/or needs updating. | |
3743 * Also needed when re-editing the same buffer, because unloading will | |
3744 * have removed it as a diff buffer. */ | |
673 | 3745 if (curwin->w_p_diff) |
3746 { | |
3747 diff_buf_add(curbuf); | |
3748 diff_invalidate(curbuf); | |
3749 } | |
7 | 3750 #endif |
3751 | |
1185 | 3752 #ifdef FEAT_SPELL |
3753 /* If the window options were changed may need to set the spell language. | |
3754 * Can only do this after the buffer has been properly setup. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3755 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3756 (void)did_set_spelllang(curwin); |
1185 | 3757 #endif |
3758 | |
7 | 3759 if (command == NULL) |
3760 { | |
3761 if (newcol >= 0) /* position set by autocommands */ | |
3762 { | |
3763 curwin->w_cursor.lnum = newlnum; | |
3764 curwin->w_cursor.col = newcol; | |
3765 check_cursor(); | |
3766 } | |
3767 else if (newlnum > 0) /* line number from caller or old position */ | |
3768 { | |
3769 curwin->w_cursor.lnum = newlnum; | |
3770 check_cursor_lnum(); | |
3771 if (solcol >= 0 && !p_sol) | |
3772 { | |
3773 /* 'sol' is off: Use last known column. */ | |
3774 curwin->w_cursor.col = solcol; | |
3775 check_cursor_col(); | |
3776 #ifdef FEAT_VIRTUALEDIT | |
3777 curwin->w_cursor.coladd = 0; | |
3778 #endif | |
3779 curwin->w_set_curswant = TRUE; | |
3780 } | |
3781 else | |
3782 beginline(BL_SOL | BL_FIX); | |
3783 } | |
3784 else /* no line number, go to last line in Ex mode */ | |
3785 { | |
3786 if (exmode_active) | |
3787 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
3788 beginline(BL_WHITE | BL_FIX); | |
3789 } | |
3790 } | |
3791 | |
3792 #ifdef FEAT_WINDOWS | |
3793 /* Check if cursors in other windows on the same buffer are still valid */ | |
3794 check_lnums(FALSE); | |
3795 #endif | |
3796 | |
3797 /* | |
3798 * Did not read the file, need to show some info about the file. | |
3799 * Do this after setting the cursor. | |
3800 */ | |
3801 if (oldbuf | |
3802 #ifdef FEAT_AUTOCMD | |
3803 && !auto_buf | |
3804 #endif | |
3805 ) | |
3806 { | |
3807 int msg_scroll_save = msg_scroll; | |
3808 | |
3809 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file | |
3810 * message. */ | |
3811 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
3812 msg_scroll = FALSE; | |
3813 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
3814 check_for_delay(FALSE); | |
3815 msg_start(); | |
3816 msg_scroll = msg_scroll_save; | |
3817 msg_scrolled_ign = TRUE; | |
3818 | |
3819 fileinfo(FALSE, TRUE, FALSE); | |
3820 | |
3821 msg_scrolled_ign = FALSE; | |
3822 } | |
3823 | |
3824 if (command != NULL) | |
3825 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE); | |
3826 | |
3827 #ifdef FEAT_KEYMAP | |
3828 if (curbuf->b_kmap_state & KEYMAP_INIT) | |
1869 | 3829 (void)keymap_init(); |
7 | 3830 #endif |
3831 | |
3832 --RedrawingDisabled; | |
3833 if (!skip_redraw) | |
3834 { | |
3835 n = p_so; | |
3836 if (topline == 0 && command == NULL) | |
3837 p_so = 999; /* force cursor halfway the window */ | |
3838 update_topline(); | |
3839 #ifdef FEAT_SCROLLBIND | |
3840 curwin->w_scbind_pos = curwin->w_topline; | |
3841 #endif | |
3842 p_so = n; | |
3843 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */ | |
3844 } | |
3845 | |
3846 if (p_im) | |
3847 need_start_insertmode = TRUE; | |
3848 | |
961 | 3849 /* Change directories when the 'acd' option is set. */ |
3850 DO_AUTOCHDIR | |
821 | 3851 |
3852 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2178
diff
changeset
|
3853 if (curbuf->b_ffname != NULL) |
7 | 3854 { |
3855 # ifdef FEAT_SUN_WORKSHOP | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2178
diff
changeset
|
3856 if (gui.in_use && usingSunWorkShop) |
7 | 3857 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro); |
3858 # endif | |
3859 # ifdef FEAT_NETBEANS_INTG | |
2210 | 3860 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP) |
34 | 3861 netbeans_file_opened(curbuf); |
7 | 3862 # endif |
3863 } | |
3864 #endif | |
3865 | |
3866 theend: | |
716 | 3867 #ifdef FEAT_AUTOCMD |
3868 if (did_set_swapcommand) | |
3869 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); | |
3870 #endif | |
7 | 3871 #ifdef FEAT_BROWSE |
3872 vim_free(browse_file); | |
3873 #endif | |
3874 vim_free(free_fname); | |
3875 return retval; | |
3876 } | |
3877 | |
3878 #ifdef FEAT_AUTOCMD | |
3879 static void | |
3880 delbuf_msg(name) | |
3881 char_u *name; | |
3882 { | |
3883 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), | |
3884 name == NULL ? (char_u *)"" : name); | |
3885 vim_free(name); | |
3886 au_new_curbuf = NULL; | |
3887 } | |
3888 #endif | |
3889 | |
169 | 3890 static int append_indent = 0; /* autoindent for first line */ |
3891 | |
7 | 3892 /* |
3893 * ":insert" and ":append", also used by ":change" | |
3894 */ | |
3895 void | |
3896 ex_append(eap) | |
3897 exarg_T *eap; | |
3898 { | |
3899 char_u *theline; | |
3900 int did_undo = FALSE; | |
3901 linenr_T lnum = eap->line2; | |
165 | 3902 int indent = 0; |
3903 char_u *p; | |
3904 int vcol; | |
3905 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
7 | 3906 |
169 | 3907 /* the ! flag toggles autoindent */ |
3908 if (eap->forceit) | |
3909 curbuf->b_p_ai = !curbuf->b_p_ai; | |
3910 | |
3911 /* First autoindent comes from the line we start on */ | |
3912 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) | |
3913 append_indent = get_indent_lnum(lnum); | |
3914 | |
7 | 3915 if (eap->cmdidx != CMD_append) |
3916 --lnum; | |
3917 | |
165 | 3918 /* when the buffer is empty append to line 0 and delete the dummy line */ |
3919 if (empty && lnum == 1) | |
3920 lnum = 0; | |
3921 | |
7 | 3922 State = INSERT; /* behave like in Insert mode */ |
3923 if (curbuf->b_p_iminsert == B_IMODE_LMAP) | |
3924 State |= LANGMAP; | |
165 | 3925 |
408 | 3926 for (;;) |
7 | 3927 { |
3928 msg_scroll = TRUE; | |
3929 need_wait_return = FALSE; | |
169 | 3930 if (curbuf->b_p_ai) |
3931 { | |
3932 if (append_indent >= 0) | |
3933 { | |
3934 indent = append_indent; | |
3935 append_indent = -1; | |
3936 } | |
3937 else if (lnum > 0) | |
3938 indent = get_indent_lnum(lnum); | |
3939 } | |
3940 ex_keep_indent = FALSE; | |
7 | 3941 if (eap->getline == NULL) |
169 | 3942 { |
3943 /* No getline() function, use the lines that follow. This ends | |
3944 * when there is no more. */ | |
3945 if (eap->nextcmd == NULL || *eap->nextcmd == NUL) | |
3946 break; | |
3947 p = vim_strchr(eap->nextcmd, NL); | |
3948 if (p == NULL) | |
3949 p = eap->nextcmd + STRLEN(eap->nextcmd); | |
3950 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd)); | |
3951 if (*p != NUL) | |
3952 ++p; | |
3953 eap->nextcmd = p; | |
3954 } | |
7 | 3955 else |
3956 theline = eap->getline( | |
3957 #ifdef FEAT_EVAL | |
76 | 3958 eap->cstack->cs_looplevel > 0 ? -1 : |
7 | 3959 #endif |
165 | 3960 NUL, eap->cookie, indent); |
7 | 3961 lines_left = Rows - 1; |
165 | 3962 if (theline == NULL) |
3963 break; | |
3964 | |
169 | 3965 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */ |
3966 if (ex_keep_indent) | |
3967 append_indent = indent; | |
3968 | |
165 | 3969 /* Look for the "." after automatic indent. */ |
3970 vcol = 0; | |
3971 for (p = theline; indent > vcol; ++p) | |
3972 { | |
3973 if (*p == ' ') | |
3974 ++vcol; | |
3975 else if (*p == TAB) | |
3976 vcol += 8 - vcol % 8; | |
3977 else | |
3978 break; | |
3979 } | |
3980 if ((p[0] == '.' && p[1] == NUL) | |
321 | 3981 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) |
3982 == FAIL)) | |
7 | 3983 { |
3984 vim_free(theline); | |
3985 break; | |
3986 } | |
3987 | |
169 | 3988 /* don't use autoindent if nothing was typed. */ |
3989 if (p[0] == NUL) | |
3990 theline[0] = NUL; | |
3991 | |
7 | 3992 did_undo = TRUE; |
3993 ml_append(lnum, theline, (colnr_T)0, FALSE); | |
3994 appended_lines_mark(lnum, 1L); | |
3995 | |
3996 vim_free(theline); | |
3997 ++lnum; | |
165 | 3998 |
3999 if (empty) | |
4000 { | |
4001 ml_delete(2L, FALSE); | |
4002 empty = FALSE; | |
4003 } | |
7 | 4004 } |
4005 State = NORMAL; | |
4006 | |
169 | 4007 if (eap->forceit) |
4008 curbuf->b_p_ai = !curbuf->b_p_ai; | |
4009 | |
7 | 4010 /* "start" is set to eap->line2+1 unless that position is invalid (when |
1227 | 4011 * eap->line2 pointed to the end of the buffer and nothing was appended) |
7 | 4012 * "end" is set to lnum when something has been appended, otherwise |
4013 * it is the same than "start" -- Acevedo */ | |
4014 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? | |
4015 eap->line2 + 1 : curbuf->b_ml.ml_line_count; | |
4016 if (eap->cmdidx != CMD_append) | |
4017 --curbuf->b_op_start.lnum; | |
4018 curbuf->b_op_end.lnum = (eap->line2 < lnum) | |
4019 ? lnum : curbuf->b_op_start.lnum; | |
4020 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
4021 curwin->w_cursor.lnum = lnum; | |
4022 check_cursor_lnum(); | |
4023 beginline(BL_SOL | BL_FIX); | |
4024 | |
4025 need_wait_return = FALSE; /* don't use wait_return() now */ | |
4026 ex_no_reprint = TRUE; | |
4027 } | |
4028 | |
4029 /* | |
4030 * ":change" | |
4031 */ | |
4032 void | |
4033 ex_change(eap) | |
4034 exarg_T *eap; | |
4035 { | |
4036 linenr_T lnum; | |
4037 | |
4038 if (eap->line2 >= eap->line1 | |
4039 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) | |
4040 return; | |
4041 | |
169 | 4042 /* the ! flag toggles autoindent */ |
4043 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) | |
4044 append_indent = get_indent_lnum(eap->line1); | |
4045 | |
7 | 4046 for (lnum = eap->line2; lnum >= eap->line1; --lnum) |
4047 { | |
4048 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ | |
4049 break; | |
4050 ml_delete(eap->line1, FALSE); | |
4051 } | |
1929 | 4052 |
4053 /* make sure the cursor is not beyond the end of the file now */ | |
4054 check_cursor_lnum(); | |
7 | 4055 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); |
4056 | |
4057 /* ":append" on the line above the deleted lines. */ | |
4058 eap->line2 = eap->line1; | |
4059 ex_append(eap); | |
4060 } | |
4061 | |
4062 void | |
4063 ex_z(eap) | |
4064 exarg_T *eap; | |
4065 { | |
4066 char_u *x; | |
165 | 4067 int bigness; |
4068 char_u *kind; | |
7 | 4069 int minus = 0; |
4070 linenr_T start, end, curs, i; | |
4071 int j; | |
4072 linenr_T lnum = eap->line2; | |
4073 | |
165 | 4074 /* Vi compatible: ":z!" uses display height, without a count uses |
4075 * 'scroll' */ | |
4076 if (eap->forceit) | |
4077 bigness = curwin->w_height; | |
4078 else if (firstwin == lastwin) | |
4079 bigness = curwin->w_p_scr * 2; | |
1877 | 4080 #ifdef FEAT_WINDOWS |
165 | 4081 else |
4082 bigness = curwin->w_height - 3; | |
1877 | 4083 #endif |
7 | 4084 if (bigness < 1) |
4085 bigness = 1; | |
4086 | |
4087 x = eap->arg; | |
165 | 4088 kind = x; |
4089 if (*kind == '-' || *kind == '+' || *kind == '=' | |
4090 || *kind == '^' || *kind == '.') | |
4091 ++x; | |
4092 while (*x == '-' || *x == '+') | |
7 | 4093 ++x; |
4094 | |
4095 if (*x != 0) | |
4096 { | |
4097 if (!VIM_ISDIGIT(*x)) | |
4098 { | |
4099 EMSG(_("E144: non-numeric argument to :z")); | |
4100 return; | |
4101 } | |
4102 else | |
165 | 4103 { |
7 | 4104 bigness = atoi((char *)x); |
165 | 4105 p_window = bigness; |
169 | 4106 if (*kind == '=') |
4107 bigness += 2; | |
165 | 4108 } |
7 | 4109 } |
4110 | |
165 | 4111 /* the number of '-' and '+' multiplies the distance */ |
4112 if (*kind == '-' || *kind == '+') | |
4113 for (x = kind + 1; *x == *kind; ++x) | |
4114 ; | |
4115 | |
4116 switch (*kind) | |
7 | 4117 { |
4118 case '-': | |
835 | 4119 start = lnum - bigness * (linenr_T)(x - kind); |
165 | 4120 end = start + bigness; |
4121 curs = end; | |
7 | 4122 break; |
4123 | |
4124 case '=': | |
159 | 4125 start = lnum - (bigness + 1) / 2 + 1; |
4126 end = lnum + (bigness + 1) / 2 - 1; | |
7 | 4127 curs = lnum; |
4128 minus = 1; | |
4129 break; | |
4130 | |
4131 case '^': | |
4132 start = lnum - bigness * 2; | |
4133 end = lnum - bigness; | |
4134 curs = lnum - bigness; | |
4135 break; | |
4136 | |
4137 case '.': | |
159 | 4138 start = lnum - (bigness + 1) / 2 + 1; |
4139 end = lnum + (bigness + 1) / 2 - 1; | |
7 | 4140 curs = end; |
4141 break; | |
4142 | |
4143 default: /* '+' */ | |
4144 start = lnum; | |
165 | 4145 if (*kind == '+') |
835 | 4146 start += bigness * (linenr_T)(x - kind - 1) + 1; |
169 | 4147 else if (eap->addr_count == 0) |
4148 ++start; | |
4149 end = start + bigness - 1; | |
7 | 4150 curs = end; |
4151 break; | |
4152 } | |
4153 | |
4154 if (start < 1) | |
4155 start = 1; | |
4156 | |
4157 if (end > curbuf->b_ml.ml_line_count) | |
4158 end = curbuf->b_ml.ml_line_count; | |
4159 | |
4160 if (curs > curbuf->b_ml.ml_line_count) | |
4161 curs = curbuf->b_ml.ml_line_count; | |
4162 | |
4163 for (i = start; i <= end; i++) | |
4164 { | |
4165 if (minus && i == lnum) | |
4166 { | |
4167 msg_putchar('\n'); | |
4168 | |
4169 for (j = 1; j < Columns; j++) | |
4170 msg_putchar('-'); | |
4171 } | |
4172 | |
169 | 4173 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); |
7 | 4174 |
4175 if (minus && i == lnum) | |
4176 { | |
4177 msg_putchar('\n'); | |
4178 | |
4179 for (j = 1; j < Columns; j++) | |
4180 msg_putchar('-'); | |
4181 } | |
4182 } | |
4183 | |
4184 curwin->w_cursor.lnum = curs; | |
4185 ex_no_reprint = TRUE; | |
4186 } | |
4187 | |
4188 /* | |
4189 * Check if the restricted flag is set. | |
4190 * If so, give an error message and return TRUE. | |
4191 * Otherwise, return FALSE. | |
4192 */ | |
4193 int | |
4194 check_restricted() | |
4195 { | |
4196 if (restricted) | |
4197 { | |
4198 EMSG(_("E145: Shell commands not allowed in rvim")); | |
4199 return TRUE; | |
4200 } | |
4201 return FALSE; | |
4202 } | |
4203 | |
4204 /* | |
4205 * Check if the secure flag is set (.exrc or .vimrc in current directory). | |
4206 * If so, give an error message and return TRUE. | |
4207 * Otherwise, return FALSE. | |
4208 */ | |
4209 int | |
4210 check_secure() | |
4211 { | |
4212 if (secure) | |
4213 { | |
4214 secure = 2; | |
4215 EMSG(_(e_curdir)); | |
4216 return TRUE; | |
4217 } | |
4218 #ifdef HAVE_SANDBOX | |
4219 /* | |
4220 * In the sandbox more things are not allowed, including the things | |
4221 * disallowed in secure mode. | |
4222 */ | |
4223 if (sandbox != 0) | |
4224 { | |
4225 EMSG(_(e_sandbox)); | |
4226 return TRUE; | |
4227 } | |
4228 #endif | |
4229 return FALSE; | |
4230 } | |
4231 | |
4232 static char_u *old_sub = NULL; /* previous substitute pattern */ | |
4233 static int global_need_beginline; /* call beginline() after ":g" */ | |
4234 | |
4235 /* do_sub() | |
4236 * | |
4237 * Perform a substitution from line eap->line1 to line eap->line2 using the | |
4238 * command pointed to by eap->arg which should be of the form: | |
4239 * | |
4240 * /pattern/substitution/{flags} | |
4241 * | |
4242 * The usual escapes are supported as described in the regexp docs. | |
4243 */ | |
4244 void | |
4245 do_sub(eap) | |
4246 exarg_T *eap; | |
4247 { | |
4248 linenr_T lnum; | |
4249 long i = 0; | |
4250 regmmatch_T regmatch; | |
4251 static int do_all = FALSE; /* do multiple substitutions per line */ | |
4252 static int do_ask = FALSE; /* ask for confirmation */ | |
170 | 4253 static int do_count = FALSE; /* count only */ |
7 | 4254 static int do_error = TRUE; /* if false, ignore errors */ |
4255 static int do_print = FALSE; /* print last line with subs. */ | |
169 | 4256 static int do_list = FALSE; /* list last line with subs. */ |
4257 static int do_number = FALSE; /* list last line with line nr*/ | |
7 | 4258 static int do_ic = 0; /* ignore case flag */ |
4259 char_u *pat = NULL, *sub = NULL; /* init for GCC */ | |
4260 int delimiter; | |
4261 int sublen; | |
4262 int got_quit = FALSE; | |
4263 int got_match = FALSE; | |
4264 int temp; | |
4265 int which_pat; | |
4266 char_u *cmd; | |
4267 int save_State; | |
165 | 4268 linenr_T first_line = 0; /* first changed line */ |
4269 linenr_T last_line= 0; /* below last changed line AFTER the | |
7 | 4270 * change */ |
4271 linenr_T old_line_count = curbuf->b_ml.ml_line_count; | |
4272 linenr_T line2; | |
165 | 4273 long nmatch; /* number of lines in match */ |
4274 char_u *sub_firstline; /* allocated copy of first sub line */ | |
4275 int endcolumn = FALSE; /* cursor in last column when done */ | |
170 | 4276 pos_T old_cursor = curwin->w_cursor; |
2126
e038754d419a
updated for version 7.2.408
Bram Moolenaar <bram@zimbu.org>
parents:
1929
diff
changeset
|
4277 int start_nsubs; |
7 | 4278 |
4279 cmd = eap->arg; | |
4280 if (!global_busy) | |
4281 { | |
4282 sub_nsubs = 0; | |
4283 sub_nlines = 0; | |
4284 } | |
2126
e038754d419a
updated for version 7.2.408
Bram Moolenaar <bram@zimbu.org>
parents:
1929
diff
changeset
|
4285 start_nsubs = sub_nsubs; |
7 | 4286 |
4287 if (eap->cmdidx == CMD_tilde) | |
4288 which_pat = RE_LAST; /* use last used regexp */ | |
4289 else | |
4290 which_pat = RE_SUBST; /* use last substitute regexp */ | |
4291 | |
4292 /* new pattern and substitution */ | |
4293 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd) | |
4294 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) | |
4295 { | |
4296 /* don't accept alphanumeric for separator */ | |
4297 if (isalpha(*cmd)) | |
4298 { | |
4299 EMSG(_("E146: Regular expressions can't be delimited by letters")); | |
4300 return; | |
4301 } | |
4302 /* | |
4303 * undocumented vi feature: | |
4304 * "\/sub/" and "\?sub?" use last used search pattern (almost like | |
4305 * //sub/r). "\&sub&" use last substitute pattern (like //sub/). | |
4306 */ | |
4307 if (*cmd == '\\') | |
4308 { | |
4309 ++cmd; | |
4310 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) | |
4311 { | |
4312 EMSG(_(e_backslash)); | |
4313 return; | |
4314 } | |
4315 if (*cmd != '&') | |
4316 which_pat = RE_SEARCH; /* use last '/' pattern */ | |
4317 pat = (char_u *)""; /* empty search pattern */ | |
4318 delimiter = *cmd++; /* remember delimiter character */ | |
4319 } | |
4320 else /* find the end of the regexp */ | |
4321 { | |
1466 | 4322 #ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */ |
4323 if (p_altkeymap && curwin->w_p_rl) | |
4324 lrF_sub(cmd); | |
4325 #endif | |
7 | 4326 which_pat = RE_LAST; /* use last used regexp */ |
4327 delimiter = *cmd++; /* remember delimiter character */ | |
4328 pat = cmd; /* remember start of search pat */ | |
4329 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); | |
4330 if (cmd[0] == delimiter) /* end delimiter found */ | |
4331 *cmd++ = NUL; /* replace it with a NUL */ | |
4332 } | |
4333 | |
4334 /* | |
4335 * Small incompatibility: vi sees '\n' as end of the command, but in | |
4336 * Vim we want to use '\n' to find/substitute a NUL. | |
4337 */ | |
4338 sub = cmd; /* remember the start of the substitution */ | |
4339 | |
4340 while (cmd[0]) | |
4341 { | |
4342 if (cmd[0] == delimiter) /* end delimiter found */ | |
4343 { | |
4344 *cmd++ = NUL; /* replace it with a NUL */ | |
4345 break; | |
4346 } | |
4347 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */ | |
4348 ++cmd; | |
39 | 4349 mb_ptr_adv(cmd); |
7 | 4350 } |
4351 | |
4352 if (!eap->skip) | |
4353 { | |
169 | 4354 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */ |
4355 if (STRCMP(sub, "%") == 0 | |
4356 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL) | |
4357 { | |
4358 if (old_sub == NULL) /* there is no previous command */ | |
4359 { | |
4360 EMSG(_(e_nopresub)); | |
4361 return; | |
4362 } | |
4363 sub = old_sub; | |
4364 } | |
4365 else | |
4366 { | |
4367 vim_free(old_sub); | |
4368 old_sub = vim_strsave(sub); | |
4369 } | |
7 | 4370 } |
4371 } | |
4372 else if (!eap->skip) /* use previous pattern and substitution */ | |
4373 { | |
4374 if (old_sub == NULL) /* there is no previous command */ | |
4375 { | |
4376 EMSG(_(e_nopresub)); | |
4377 return; | |
4378 } | |
4379 pat = NULL; /* search_regcomp() will use previous pattern */ | |
4380 sub = old_sub; | |
163 | 4381 |
4382 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the | |
4383 * last column after using "$". */ | |
4384 endcolumn = (curwin->w_curswant == MAXCOL); | |
7 | 4385 } |
4386 | |
4387 /* | |
4388 * Find trailing options. When '&' is used, keep old options. | |
4389 */ | |
4390 if (*cmd == '&') | |
4391 ++cmd; | |
4392 else | |
4393 { | |
4394 if (!p_ed) | |
4395 { | |
4396 if (p_gd) /* default is global on */ | |
4397 do_all = TRUE; | |
4398 else | |
4399 do_all = FALSE; | |
4400 do_ask = FALSE; | |
4401 } | |
4402 do_error = TRUE; | |
4403 do_print = FALSE; | |
587 | 4404 do_count = FALSE; |
1321 | 4405 do_number = FALSE; |
7 | 4406 do_ic = 0; |
4407 } | |
4408 while (*cmd) | |
4409 { | |
4410 /* | |
4411 * Note that 'g' and 'c' are always inverted, also when p_ed is off. | |
4412 * 'r' is never inverted. | |
4413 */ | |
4414 if (*cmd == 'g') | |
4415 do_all = !do_all; | |
4416 else if (*cmd == 'c') | |
4417 do_ask = !do_ask; | |
170 | 4418 else if (*cmd == 'n') |
4419 do_count = TRUE; | |
7 | 4420 else if (*cmd == 'e') |
4421 do_error = !do_error; | |
4422 else if (*cmd == 'r') /* use last used regexp */ | |
4423 which_pat = RE_LAST; | |
4424 else if (*cmd == 'p') | |
4425 do_print = TRUE; | |
169 | 4426 else if (*cmd == '#') |
4427 { | |
4428 do_print = TRUE; | |
4429 do_number = TRUE; | |
4430 } | |
4431 else if (*cmd == 'l') | |
4432 { | |
4433 do_print = TRUE; | |
4434 do_list = TRUE; | |
4435 } | |
7 | 4436 else if (*cmd == 'i') /* ignore case */ |
4437 do_ic = 'i'; | |
4438 else if (*cmd == 'I') /* don't ignore case */ | |
4439 do_ic = 'I'; | |
4440 else | |
4441 break; | |
4442 ++cmd; | |
4443 } | |
170 | 4444 if (do_count) |
4445 do_ask = FALSE; | |
7 | 4446 |
4447 /* | |
4448 * check for a trailing count | |
4449 */ | |
4450 cmd = skipwhite(cmd); | |
4451 if (VIM_ISDIGIT(*cmd)) | |
4452 { | |
4453 i = getdigits(&cmd); | |
4454 if (i <= 0 && !eap->skip && do_error) | |
4455 { | |
4456 EMSG(_(e_zerocount)); | |
4457 return; | |
4458 } | |
4459 eap->line1 = eap->line2; | |
4460 eap->line2 += i - 1; | |
4461 if (eap->line2 > curbuf->b_ml.ml_line_count) | |
4462 eap->line2 = curbuf->b_ml.ml_line_count; | |
4463 } | |
4464 | |
4465 /* | |
4466 * check for trailing command or garbage | |
4467 */ | |
4468 cmd = skipwhite(cmd); | |
4469 if (*cmd && *cmd != '"') /* if not end-of-line or comment */ | |
4470 { | |
4471 eap->nextcmd = check_nextcmd(cmd); | |
4472 if (eap->nextcmd == NULL) | |
4473 { | |
4474 EMSG(_(e_trailing)); | |
4475 return; | |
4476 } | |
4477 } | |
4478 | |
4479 if (eap->skip) /* not executing commands, only parsing */ | |
4480 return; | |
4481 | |
823 | 4482 if (!do_count && !curbuf->b_p_ma) |
4483 { | |
825 | 4484 /* Substitution is not allowed in non-'modifiable' buffer */ |
823 | 4485 EMSG(_(e_modifiable)); |
4486 return; | |
4487 } | |
4488 | |
7 | 4489 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL) |
4490 { | |
4491 if (do_error) | |
4492 EMSG(_(e_invcmd)); | |
4493 return; | |
4494 } | |
4495 | |
4496 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */ | |
4497 if (do_ic == 'i') | |
4498 regmatch.rmm_ic = TRUE; | |
4499 else if (do_ic == 'I') | |
4500 regmatch.rmm_ic = FALSE; | |
4501 | |
4502 sub_firstline = NULL; | |
4503 | |
4504 /* | |
4505 * ~ in the substitute pattern is replaced with the old pattern. | |
4506 * We do it here once to avoid it to be replaced over and over again. | |
4507 * But don't do it when it starts with "\=", then it's an expression. | |
4508 */ | |
4509 if (!(sub[0] == '\\' && sub[1] == '=')) | |
4510 sub = regtilde(sub, p_magic); | |
4511 | |
4512 /* | |
4513 * Check for a match on each line. | |
4514 */ | |
4515 line2 = eap->line2; | |
4516 for (lnum = eap->line1; lnum <= line2 && !(got_quit | |
4517 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) | |
4518 || aborting() | |
4519 #endif | |
4520 ); ++lnum) | |
4521 { | |
1521 | 4522 nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, |
4523 (colnr_T)0, NULL); | |
7 | 4524 if (nmatch) |
4525 { | |
4526 colnr_T copycol; | |
4527 colnr_T matchcol; | |
4528 colnr_T prev_matchcol = MAXCOL; | |
4529 char_u *new_end, *new_start = NULL; | |
4530 unsigned new_start_len = 0; | |
4531 char_u *p1; | |
4532 int did_sub = FALSE; | |
4533 int lastone; | |
1872 | 4534 int len, copy_len, needed_len; |
7 | 4535 long nmatch_tl = 0; /* nr of lines matched below lnum */ |
4536 int do_again; /* do it again after joining lines */ | |
15 | 4537 int skip_match = FALSE; |
1499 | 4538 linenr_T sub_firstlnum; /* nr of first sub line */ |
7 | 4539 |
4540 /* | |
4541 * The new text is build up step by step, to avoid too much | |
4542 * copying. There are these pieces: | |
1581 | 4543 * sub_firstline The old text, unmodified. |
7 | 4544 * copycol Column in the old text where we started |
4545 * looking for a match; from here old text still | |
4546 * needs to be copied to the new text. | |
4547 * matchcol Column number of the old text where to look | |
4548 * for the next match. It's just after the | |
4549 * previous match or one further. | |
4550 * prev_matchcol Column just after the previous match (if any). | |
4551 * Mostly equal to matchcol, except for the first | |
4552 * match and after skipping an empty match. | |
4553 * regmatch.*pos Where the pattern matched in the old text. | |
4554 * new_start The new text, all that has been produced so | |
4555 * far. | |
4556 * new_end The new text, where to append new text. | |
4557 * | |
1499 | 4558 * lnum The line number where we found the start of |
4559 * the match. Can be below the line we searched | |
4560 * when there is a \n before a \zs in the | |
4561 * pattern. | |
7 | 4562 * sub_firstlnum The line number in the buffer where to look |
4563 * for a match. Can be different from "lnum" | |
4564 * when the pattern or substitute string contains | |
4565 * line breaks. | |
4566 * | |
4567 * Special situations: | |
4568 * - When the substitute string contains a line break, the part up | |
4569 * to the line break is inserted in the text, but the copy of | |
4570 * the original line is kept. "sub_firstlnum" is adjusted for | |
4571 * the inserted lines. | |
4572 * - When the matched pattern contains a line break, the old line | |
4573 * is taken from the line at the end of the pattern. The lines | |
4574 * in the match are deleted later, "sub_firstlnum" is adjusted | |
4575 * accordingly. | |
4576 * | |
4577 * The new text is built up in new_start[]. It has some extra | |
4578 * room to avoid using alloc()/free() too often. new_start_len is | |
1389 | 4579 * the length of the allocated memory at new_start. |
7 | 4580 * |
4581 * Make a copy of the old line, so it won't be taken away when | |
4582 * updating the screen or handling a multi-line match. The "old_" | |
4583 * pointers point into this copy. | |
4584 */ | |
1499 | 4585 sub_firstlnum = lnum; |
7 | 4586 copycol = 0; |
4587 matchcol = 0; | |
4588 | |
4589 /* At first match, remember current cursor position. */ | |
4590 if (!got_match) | |
4591 { | |
4592 setpcmark(); | |
4593 got_match = TRUE; | |
4594 } | |
4595 | |
4596 /* | |
4597 * Loop until nothing more to replace in this line. | |
4598 * 1. Handle match with empty string. | |
4599 * 2. If do_ask is set, ask for confirmation. | |
4600 * 3. substitute the string. | |
4601 * 4. if do_all is set, find next match | |
4602 * 5. break if there isn't another match in this line | |
4603 */ | |
4604 for (;;) | |
4605 { | |
1499 | 4606 /* Advance "lnum" to the line where the match starts. The |
4607 * match does not start in the first line when there is a line | |
4608 * break before \zs. */ | |
4609 if (regmatch.startpos[0].lnum > 0) | |
4610 { | |
4611 lnum += regmatch.startpos[0].lnum; | |
4612 sub_firstlnum += regmatch.startpos[0].lnum; | |
4613 nmatch -= regmatch.startpos[0].lnum; | |
4614 vim_free(sub_firstline); | |
4615 sub_firstline = NULL; | |
4616 } | |
4617 | |
4618 if (sub_firstline == NULL) | |
4619 { | |
4620 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); | |
4621 if (sub_firstline == NULL) | |
4622 { | |
4623 vim_free(new_start); | |
4624 goto outofmem; | |
4625 } | |
4626 } | |
4627 | |
7 | 4628 /* Save the line number of the last change for the final |
4629 * cursor position (just like Vi). */ | |
4630 curwin->w_cursor.lnum = lnum; | |
4631 do_again = FALSE; | |
4632 | |
4633 /* | |
4634 * 1. Match empty string does not count, except for first | |
4635 * match. This reproduces the strange vi behaviour. | |
4636 * This also catches endless loops. | |
4637 */ | |
4638 if (matchcol == prev_matchcol | |
4639 && regmatch.endpos[0].lnum == 0 | |
4640 && matchcol == regmatch.endpos[0].col) | |
4641 { | |
15 | 4642 if (sub_firstline[matchcol] == NUL) |
4643 /* We already were at the end of the line. Don't look | |
4644 * for a match in this line again. */ | |
4645 skip_match = TRUE; | |
4646 else | |
4647 ++matchcol; /* search for a match at next column */ | |
7 | 4648 goto skip; |
4649 } | |
4650 | |
4651 /* Normally we continue searching for a match just after the | |
4652 * previous match. */ | |
4653 matchcol = regmatch.endpos[0].col; | |
4654 prev_matchcol = matchcol; | |
4655 | |
4656 /* | |
170 | 4657 * 2. If do_count is set only increase the counter. |
4658 * If do_ask is set, ask for confirmation. | |
7 | 4659 */ |
170 | 4660 if (do_count) |
4661 { | |
4662 /* For a multi-line match, put matchcol at the NUL at | |
4663 * the end of the line and set nmatch to one, so that | |
4664 * we continue looking for a match on the next line. | |
4665 * Avoids that ":s/\nB\@=//gc" get stuck. */ | |
4666 if (nmatch > 1) | |
4667 { | |
835 | 4668 matchcol = (colnr_T)STRLEN(sub_firstline); |
170 | 4669 nmatch = 1; |
1483 | 4670 skip_match = TRUE; |
170 | 4671 } |
4672 sub_nsubs++; | |
4673 did_sub = TRUE; | |
4674 goto skip; | |
4675 } | |
4676 | |
7 | 4677 if (do_ask) |
4678 { | |
1874 | 4679 int typed = 0; |
1872 | 4680 |
7 | 4681 /* change State to CONFIRM, so that the mouse works |
4682 * properly */ | |
4683 save_State = State; | |
4684 State = CONFIRM; | |
4685 #ifdef FEAT_MOUSE | |
4686 setmouse(); /* disable mouse in xterm */ | |
4687 #endif | |
4688 curwin->w_cursor.col = regmatch.startpos[0].col; | |
4689 | |
4690 /* When 'cpoptions' contains "u" don't sync undo when | |
4691 * asking for confirmation. */ | |
4692 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) | |
4693 ++no_u_sync; | |
4694 | |
4695 /* | |
4696 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. | |
4697 */ | |
4698 while (do_ask) | |
4699 { | |
169 | 4700 if (exmode_active) |
4701 { | |
4702 char_u *resp; | |
4703 colnr_T sc, ec; | |
4704 | |
4705 print_line_no_prefix(lnum, FALSE, FALSE); | |
4706 | |
4707 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); | |
4708 curwin->w_cursor.col = regmatch.endpos[0].col - 1; | |
4709 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); | |
4710 msg_start(); | |
170 | 4711 for (i = 0; i < (long)sc; ++i) |
169 | 4712 msg_putchar(' '); |
170 | 4713 for ( ; i <= (long)ec; ++i) |
169 | 4714 msg_putchar('^'); |
4715 | |
4716 resp = getexmodeline('?', NULL, 0); | |
4717 if (resp != NULL) | |
4718 { | |
1872 | 4719 typed = *resp; |
169 | 4720 vim_free(resp); |
4721 } | |
4722 } | |
4723 else | |
4724 { | |
7 | 4725 #ifdef FEAT_FOLDING |
169 | 4726 int save_p_fen = curwin->w_p_fen; |
4727 | |
4728 curwin->w_p_fen = FALSE; | |
4729 #endif | |
4730 /* Invert the matched string. | |
4731 * Remove the inversion afterwards. */ | |
4732 temp = RedrawingDisabled; | |
4733 RedrawingDisabled = 0; | |
4734 | |
1499 | 4735 search_match_lines = regmatch.endpos[0].lnum |
4736 - regmatch.startpos[0].lnum; | |
169 | 4737 search_match_endcol = regmatch.endpos[0].col; |
4738 highlight_match = TRUE; | |
4739 | |
4740 update_topline(); | |
4741 validate_cursor(); | |
748 | 4742 update_screen(SOME_VALID); |
169 | 4743 highlight_match = FALSE; |
748 | 4744 redraw_later(SOME_VALID); |
7 | 4745 |
4746 #ifdef FEAT_FOLDING | |
169 | 4747 curwin->w_p_fen = save_p_fen; |
4748 #endif | |
4749 if (msg_row == Rows - 1) | |
4750 msg_didout = FALSE; /* avoid a scroll-up */ | |
4751 msg_starthere(); | |
4752 i = msg_scroll; | |
4753 msg_scroll = 0; /* truncate msg when | |
4754 needed */ | |
4755 msg_no_more = TRUE; | |
4756 /* write message same highlighting as for | |
4757 * wait_return */ | |
4758 smsg_attr(hl_attr(HLF_R), | |
4759 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); | |
4760 msg_no_more = FALSE; | |
4761 msg_scroll = i; | |
4762 showruler(TRUE); | |
4763 windgoto(msg_row, msg_col); | |
4764 RedrawingDisabled = temp; | |
7 | 4765 |
4766 #ifdef USE_ON_FLY_SCROLL | |
169 | 4767 dont_scroll = FALSE; /* allow scrolling here */ |
4768 #endif | |
4769 ++no_mapping; /* don't map this key */ | |
4770 ++allow_keys; /* allow special keys */ | |
1872 | 4771 typed = plain_vgetc(); |
169 | 4772 --allow_keys; |
4773 --no_mapping; | |
4774 | |
4775 /* clear the question */ | |
4776 msg_didout = FALSE; /* don't scroll up */ | |
4777 msg_col = 0; | |
4778 gotocmdline(TRUE); | |
4779 } | |
4780 | |
7 | 4781 need_wait_return = FALSE; /* no hit-return prompt */ |
1872 | 4782 if (typed == 'q' || typed == ESC || typed == Ctrl_C |
7 | 4783 #ifdef UNIX |
1872 | 4784 || typed == intr_char |
7 | 4785 #endif |
4786 ) | |
4787 { | |
4788 got_quit = TRUE; | |
4789 break; | |
4790 } | |
1872 | 4791 if (typed == 'n') |
7 | 4792 break; |
1872 | 4793 if (typed == 'y') |
7 | 4794 break; |
1872 | 4795 if (typed == 'l') |
7 | 4796 { |
4797 /* last: replace and then stop */ | |
4798 do_all = FALSE; | |
4799 line2 = lnum; | |
4800 break; | |
4801 } | |
1872 | 4802 if (typed == 'a') |
7 | 4803 { |
4804 do_ask = FALSE; | |
4805 break; | |
4806 } | |
4807 #ifdef FEAT_INS_EXPAND | |
1872 | 4808 if (typed == Ctrl_E) |
7 | 4809 scrollup_clamp(); |
1872 | 4810 else if (typed == Ctrl_Y) |
7 | 4811 scrolldown_clamp(); |
4812 #endif | |
4813 } | |
4814 State = save_State; | |
4815 #ifdef FEAT_MOUSE | |
4816 setmouse(); | |
4817 #endif | |
4818 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) | |
4819 --no_u_sync; | |
4820 | |
1872 | 4821 if (typed == 'n') |
7 | 4822 { |
4823 /* For a multi-line match, put matchcol at the NUL at | |
4824 * the end of the line and set nmatch to one, so that | |
4825 * we continue looking for a match on the next line. | |
1091 | 4826 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc" |
4827 * get stuck when pressing 'n'. */ | |
7 | 4828 if (nmatch > 1) |
4829 { | |
835 | 4830 matchcol = (colnr_T)STRLEN(sub_firstline); |
1091 | 4831 skip_match = TRUE; |
7 | 4832 } |
4833 goto skip; | |
4834 } | |
4835 if (got_quit) | |
4836 break; | |
4837 } | |
4838 | |
4839 /* Move the cursor to the start of the match, so that we can | |
4840 * use "\=col("."). */ | |
4841 curwin->w_cursor.col = regmatch.startpos[0].col; | |
4842 | |
4843 /* | |
4844 * 3. substitute the string. | |
4845 */ | |
4846 /* get length of substitution part */ | |
1499 | 4847 sublen = vim_regsub_multi(®match, |
4848 sub_firstlnum - regmatch.startpos[0].lnum, | |
7 | 4849 sub, sub_firstline, FALSE, p_magic, TRUE); |
4850 | |
533 | 4851 /* When the match included the "$" of the last line it may |
819 | 4852 * go beyond the last line of the buffer. */ |
533 | 4853 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) |
4854 { | |
4855 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; | |
4856 skip_match = TRUE; | |
4857 } | |
4858 | |
7 | 4859 /* Need room for: |
4860 * - result so far in new_start (not for first sub in line) | |
4861 * - original text up to match | |
4862 * - length of substituted part | |
4863 * - original text after match | |
4864 */ | |
4865 if (nmatch == 1) | |
4866 p1 = sub_firstline; | |
4867 else | |
4868 { | |
4869 p1 = ml_get(sub_firstlnum + nmatch - 1); | |
4870 nmatch_tl += nmatch - 1; | |
4871 } | |
1872 | 4872 copy_len = regmatch.startpos[0].col - copycol; |
4873 needed_len = copy_len + ((unsigned)STRLEN(p1) | |
4874 - regmatch.endpos[0].col) + sublen + 1; | |
7 | 4875 if (new_start == NULL) |
4876 { | |
4877 /* | |
4878 * Get some space for a temporary buffer to do the | |
4879 * substitution into (and some extra space to avoid | |
4880 * too many calls to alloc()/free()). | |
4881 */ | |
4882 new_start_len = needed_len + 50; | |
4883 if ((new_start = alloc_check(new_start_len)) == NULL) | |
4884 goto outofmem; | |
4885 *new_start = NUL; | |
4886 new_end = new_start; | |
4887 } | |
4888 else | |
4889 { | |
4890 /* | |
4891 * Check if the temporary buffer is long enough to do the | |
4892 * substitution into. If not, make it larger (with a bit | |
4893 * extra to avoid too many calls to alloc()/free()). | |
4894 */ | |
4895 len = (unsigned)STRLEN(new_start); | |
4896 needed_len += len; | |
1872 | 4897 if (needed_len > (int)new_start_len) |
7 | 4898 { |
4899 new_start_len = needed_len + 50; | |
4900 if ((p1 = alloc_check(new_start_len)) == NULL) | |
4901 { | |
4902 vim_free(new_start); | |
4903 goto outofmem; | |
4904 } | |
4905 mch_memmove(p1, new_start, (size_t)(len + 1)); | |
4906 vim_free(new_start); | |
4907 new_start = p1; | |
4908 } | |
4909 new_end = new_start + len; | |
4910 } | |
4911 | |
4912 /* | |
4913 * copy the text up to the part that matched | |
4914 */ | |
1872 | 4915 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len); |
4916 new_end += copy_len; | |
7 | 4917 |
1499 | 4918 (void)vim_regsub_multi(®match, |
4919 sub_firstlnum - regmatch.startpos[0].lnum, | |
7 | 4920 sub, new_end, TRUE, p_magic, TRUE); |
4921 sub_nsubs++; | |
4922 did_sub = TRUE; | |
4923 | |
4924 /* Move the cursor to the start of the line, to avoid that it | |
4925 * is beyond the end of the line after the substitution. */ | |
4926 curwin->w_cursor.col = 0; | |
4927 | |
4928 /* For a multi-line match, make a copy of the last matched | |
4929 * line and continue in that one. */ | |
4930 if (nmatch > 1) | |
4931 { | |
4932 sub_firstlnum += nmatch - 1; | |
4933 vim_free(sub_firstline); | |
4934 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); | |
4935 /* When going beyond the last line, stop substituting. */ | |
4936 if (sub_firstlnum <= line2) | |
4937 do_again = TRUE; | |
4938 else | |
4939 do_all = FALSE; | |
4940 } | |
4941 | |
4942 /* Remember next character to be copied. */ | |
4943 copycol = regmatch.endpos[0].col; | |
4944 | |
819 | 4945 if (skip_match) |
4946 { | |
4947 /* Already hit end of the buffer, sub_firstlnum is one | |
4948 * less than what it ought to be. */ | |
4949 vim_free(sub_firstline); | |
4950 sub_firstline = vim_strsave((char_u *)""); | |
4951 copycol = 0; | |
4952 } | |
4953 | |
7 | 4954 /* |
4955 * Now the trick is to replace CTRL-M chars with a real line | |
4956 * break. This would make it impossible to insert a CTRL-M in | |
4957 * the text. The line break can be avoided by preceding the | |
4958 * CTRL-M with a backslash. To be able to insert a backslash, | |
4959 * they must be doubled in the string and are halved here. | |
4960 * That is Vi compatible. | |
4961 */ | |
4962 for (p1 = new_end; *p1; ++p1) | |
4963 { | |
4964 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */ | |
1624 | 4965 STRMOVE(p1, p1 + 1); |
7 | 4966 else if (*p1 == CAR) |
4967 { | |
4968 if (u_inssub(lnum) == OK) /* prepare for undo */ | |
4969 { | |
4970 *p1 = NUL; /* truncate up to the CR */ | |
4971 ml_append(lnum - 1, new_start, | |
4972 (colnr_T)(p1 - new_start + 1), FALSE); | |
4973 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); | |
4974 if (do_ask) | |
4975 appended_lines(lnum - 1, 1L); | |
4976 else | |
4977 { | |
4978 if (first_line == 0) | |
4979 first_line = lnum; | |
4980 last_line = lnum + 1; | |
4981 } | |
4982 /* All line numbers increase. */ | |
4983 ++sub_firstlnum; | |
4984 ++lnum; | |
4985 ++line2; | |
4986 /* move the cursor to the new line, like Vi */ | |
4987 ++curwin->w_cursor.lnum; | |
1444 | 4988 /* copy the rest */ |
1624 | 4989 STRMOVE(new_start, p1 + 1); |
7 | 4990 p1 = new_start - 1; |
4991 } | |
4992 } | |
4993 #ifdef FEAT_MBYTE | |
4994 else if (has_mbyte) | |
474 | 4995 p1 += (*mb_ptr2len)(p1) - 1; |
7 | 4996 #endif |
4997 } | |
4998 | |
4999 /* | |
5000 * 4. If do_all is set, find next match. | |
5001 * Prevent endless loop with patterns that match empty | |
5002 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g. | |
5003 * But ":s/\n/#/" is OK. | |
5004 */ | |
5005 skip: | |
5006 /* We already know that we did the last subst when we are at | |
5007 * the end of the line, except that a pattern like | |
1499 | 5008 * "bar\|\nfoo" may match at the NUL. "lnum" can be below |
5009 * "line2" when there is a \zs in the pattern after a line | |
5010 * break. */ | |
15 | 5011 lastone = (skip_match |
5012 || got_int | |
5013 || got_quit | |
1499 | 5014 || lnum > line2 |
15 | 5015 || !(do_all || do_again) |
5016 || (sub_firstline[matchcol] == NUL && nmatch <= 1 | |
5017 && !re_multiline(regmatch.regprog))); | |
7 | 5018 nmatch = -1; |
5019 | |
5020 /* | |
5021 * Replace the line in the buffer when needed. This is | |
5022 * skipped when there are more matches. | |
5023 * The check for nmatch_tl is needed for when multi-line | |
5024 * matching must replace the lines before trying to do another | |
5025 * match, otherwise "\@<=" won't work. | |
5026 * When asking the user we like to show the already replaced | |
5027 * text, but don't do it when "\<@=" or "\<@!" is used, it | |
5028 * changes what matches. | |
1499 | 5029 * When the match starts below where we start searching also |
5030 * need to replace the line first (using \zs after \n). | |
7 | 5031 */ |
5032 if (lastone | |
5033 || (do_ask && !re_lookbehind(regmatch.regprog)) | |
5034 || nmatch_tl > 0 | |
5035 || (nmatch = vim_regexec_multi(®match, curwin, | |
1521 | 5036 curbuf, sub_firstlnum, |
5037 matchcol, NULL)) == 0 | |
1499 | 5038 || regmatch.startpos[0].lnum > 0) |
7 | 5039 { |
5040 if (new_start != NULL) | |
5041 { | |
5042 /* | |
5043 * Copy the rest of the line, that didn't match. | |
5044 * "matchcol" has to be adjusted, we use the end of | |
5045 * the line as reference, because the substitute may | |
5046 * have changed the number of characters. Same for | |
5047 * "prev_matchcol". | |
5048 */ | |
5049 STRCAT(new_start, sub_firstline + copycol); | |
5050 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; | |
5051 prev_matchcol = (colnr_T)STRLEN(sub_firstline) | |
5052 - prev_matchcol; | |
5053 | |
5054 if (u_savesub(lnum) != OK) | |
5055 break; | |
5056 ml_replace(lnum, new_start, TRUE); | |
5057 | |
5058 if (nmatch_tl > 0) | |
5059 { | |
5060 /* | |
5061 * Matched lines have now been substituted and are | |
5062 * useless, delete them. The part after the match | |
5063 * has been appended to new_start, we don't need | |
5064 * it in the buffer. | |
5065 */ | |
5066 ++lnum; | |
5067 if (u_savedel(lnum, nmatch_tl) != OK) | |
5068 break; | |
5069 for (i = 0; i < nmatch_tl; ++i) | |
5070 ml_delete(lnum, (int)FALSE); | |
5071 mark_adjust(lnum, lnum + nmatch_tl - 1, | |
5072 (long)MAXLNUM, -nmatch_tl); | |
5073 if (do_ask) | |
5074 deleted_lines(lnum, nmatch_tl); | |
5075 --lnum; | |
5076 line2 -= nmatch_tl; /* nr of lines decreases */ | |
5077 nmatch_tl = 0; | |
5078 } | |
5079 | |
5080 /* When asking, undo is saved each time, must also set | |
5081 * changed flag each time. */ | |
5082 if (do_ask) | |
5083 changed_bytes(lnum, 0); | |
5084 else | |
5085 { | |
5086 if (first_line == 0) | |
5087 first_line = lnum; | |
5088 last_line = lnum + 1; | |
5089 } | |
5090 | |
5091 sub_firstlnum = lnum; | |
5092 vim_free(sub_firstline); /* free the temp buffer */ | |
5093 sub_firstline = new_start; | |
5094 new_start = NULL; | |
5095 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; | |
5096 prev_matchcol = (colnr_T)STRLEN(sub_firstline) | |
5097 - prev_matchcol; | |
5098 copycol = 0; | |
5099 } | |
5100 if (nmatch == -1 && !lastone) | |
5101 nmatch = vim_regexec_multi(®match, curwin, curbuf, | |
1521 | 5102 sub_firstlnum, matchcol, NULL); |
7 | 5103 |
5104 /* | |
5105 * 5. break if there isn't another match in this line | |
5106 */ | |
5107 if (nmatch <= 0) | |
1499 | 5108 { |
5109 /* If the match found didn't start where we were | |
5110 * searching, do the next search in the line where we | |
5111 * found the match. */ | |
5112 if (nmatch == -1) | |
5113 lnum -= regmatch.startpos[0].lnum; | |
7 | 5114 break; |
1499 | 5115 } |
7 | 5116 } |
5117 | |
5118 line_breakcheck(); | |
5119 } | |
5120 | |
5121 if (did_sub) | |
5122 ++sub_nlines; | |
1720 | 5123 vim_free(new_start); /* for when substitute was cancelled */ |
7 | 5124 vim_free(sub_firstline); /* free the copy of the original line */ |
5125 sub_firstline = NULL; | |
5126 } | |
5127 | |
5128 line_breakcheck(); | |
5129 } | |
5130 | |
5131 if (first_line != 0) | |
5132 { | |
5133 /* Need to subtract the number of added lines from "last_line" to get | |
5134 * the line number before the change (same as adding the number of | |
5135 * deleted lines). */ | |
5136 i = curbuf->b_ml.ml_line_count - old_line_count; | |
5137 changed_lines(first_line, 0, last_line - i, i); | |
5138 } | |
5139 | |
5140 outofmem: | |
5141 vim_free(sub_firstline); /* may have to free allocated copy of the line */ | |
170 | 5142 |
5143 /* ":s/pat//n" doesn't move the cursor */ | |
5144 if (do_count) | |
5145 curwin->w_cursor = old_cursor; | |
5146 | |
2126
e038754d419a
updated for version 7.2.408
Bram Moolenaar <bram@zimbu.org>
parents:
1929
diff
changeset
|
5147 if (sub_nsubs > start_nsubs) |
7 | 5148 { |
5149 /* Set the '[ and '] marks. */ | |
5150 curbuf->b_op_start.lnum = eap->line1; | |
5151 curbuf->b_op_end.lnum = line2; | |
5152 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
5153 | |
5154 if (!global_busy) | |
5155 { | |
163 | 5156 if (endcolumn) |
5157 coladvance((colnr_T)MAXCOL); | |
5158 else | |
5159 beginline(BL_WHITE | BL_FIX); | |
170 | 5160 if (!do_sub_msg(do_count) && do_ask) |
7 | 5161 MSG(""); |
5162 } | |
5163 else | |
5164 global_need_beginline = TRUE; | |
5165 if (do_print) | |
169 | 5166 print_line(curwin->w_cursor.lnum, do_number, do_list); |
7 | 5167 } |
5168 else if (!global_busy) | |
5169 { | |
5170 if (got_int) /* interrupted */ | |
5171 EMSG(_(e_interr)); | |
5172 else if (got_match) /* did find something but nothing substituted */ | |
5173 MSG(""); | |
5174 else if (do_error) /* nothing found */ | |
5175 EMSG2(_(e_patnotf2), get_search_pat()); | |
5176 } | |
5177 | |
5178 vim_free(regmatch.regprog); | |
5179 } | |
5180 | |
5181 /* | |
5182 * Give message for number of substitutions. | |
5183 * Can also be used after a ":global" command. | |
5184 * Return TRUE if a message was given. | |
5185 */ | |
484 | 5186 int |
170 | 5187 do_sub_msg(count_only) |
5188 int count_only; /* used 'n' flag for ":s" */ | |
7 | 5189 { |
5190 /* | |
5191 * Only report substitutions when: | |
5192 * - more than 'report' substitutions | |
5193 * - command was typed by user, or number of changed lines > 'report' | |
5194 * - giving messages is not disabled by 'lazyredraw' | |
5195 */ | |
170 | 5196 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) |
5197 || count_only) | |
7 | 5198 && messaging()) |
5199 { | |
5200 if (got_int) | |
5201 STRCPY(msg_buf, _("(Interrupted) ")); | |
2290
22529abcd646
Fixed ":s" message. Docs updates.
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5202 else |
22529abcd646
Fixed ":s" message. Docs updates.
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5203 *msg_buf = NUL; |
7 | 5204 if (sub_nsubs == 1) |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5205 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
274 | 5206 "%s", count_only ? _("1 match") : _("1 substitution")); |
7 | 5207 else |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5208 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
170 | 5209 count_only ? _("%ld matches") : _("%ld substitutions"), |
7 | 5210 sub_nsubs); |
5211 if (sub_nlines == 1) | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5212 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
274 | 5213 "%s", _(" on 1 line")); |
7 | 5214 else |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5215 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
274 | 5216 _(" on %ld lines"), (long)sub_nlines); |
7 | 5217 if (msg(msg_buf)) |
5218 /* save message to display it after redraw */ | |
680 | 5219 set_keep_msg(msg_buf, 0); |
7 | 5220 return TRUE; |
5221 } | |
5222 if (got_int) | |
5223 { | |
5224 EMSG(_(e_interr)); | |
5225 return TRUE; | |
5226 } | |
5227 return FALSE; | |
5228 } | |
5229 | |
5230 /* | |
5231 * Execute a global command of the form: | |
5232 * | |
5233 * g/pattern/X : execute X on all lines where pattern matches | |
5234 * v/pattern/X : execute X on all lines where pattern does not match | |
5235 * | |
5236 * where 'X' is an EX command | |
5237 * | |
5238 * The command character (as well as the trailing slash) is optional, and | |
5239 * is assumed to be 'p' if missing. | |
5240 * | |
5241 * This is implemented in two passes: first we scan the file for the pattern and | |
5242 * set a mark for each line that (not) matches. secondly we execute the command | |
5243 * for each line that has a mark. This is required because after deleting | |
5244 * lines we do not know where to search for the next match. | |
5245 */ | |
5246 void | |
5247 ex_global(eap) | |
5248 exarg_T *eap; | |
5249 { | |
5250 linenr_T lnum; /* line number according to old situation */ | |
170 | 5251 int ndone = 0; |
7 | 5252 int type; /* first char of cmd: 'v' or 'g' */ |
5253 char_u *cmd; /* command argument */ | |
5254 | |
5255 char_u delim; /* delimiter, normally '/' */ | |
5256 char_u *pat; | |
5257 regmmatch_T regmatch; | |
5258 int match; | |
5259 int which_pat; | |
5260 | |
5261 if (global_busy) | |
5262 { | |
5263 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */ | |
5264 return; | |
5265 } | |
5266 | |
5267 if (eap->forceit) /* ":global!" is like ":vglobal" */ | |
5268 type = 'v'; | |
5269 else | |
5270 type = *eap->cmd; | |
5271 cmd = eap->arg; | |
5272 which_pat = RE_LAST; /* default: use last used regexp */ | |
5273 | |
5274 /* | |
5275 * undocumented vi feature: | |
5276 * "\/" and "\?": use previous search pattern. | |
5277 * "\&": use previous substitute pattern. | |
5278 */ | |
5279 if (*cmd == '\\') | |
5280 { | |
5281 ++cmd; | |
5282 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) | |
5283 { | |
5284 EMSG(_(e_backslash)); | |
5285 return; | |
5286 } | |
5287 if (*cmd == '&') | |
5288 which_pat = RE_SUBST; /* use previous substitute pattern */ | |
5289 else | |
5290 which_pat = RE_SEARCH; /* use previous search pattern */ | |
5291 ++cmd; | |
5292 pat = (char_u *)""; | |
5293 } | |
5294 else if (*cmd == NUL) | |
5295 { | |
5296 EMSG(_("E148: Regular expression missing from global")); | |
5297 return; | |
5298 } | |
5299 else | |
5300 { | |
5301 delim = *cmd; /* get the delimiter */ | |
5302 if (delim) | |
5303 ++cmd; /* skip delimiter if there is one */ | |
5304 pat = cmd; /* remember start of pattern */ | |
5305 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); | |
5306 if (cmd[0] == delim) /* end delimiter found */ | |
5307 *cmd++ = NUL; /* replace it with a NUL */ | |
5308 } | |
5309 | |
5310 #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */ | |
5311 if (p_altkeymap && curwin->w_p_rl) | |
5312 lrFswap(pat,0); | |
5313 #endif | |
5314 | |
5315 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) | |
5316 { | |
5317 EMSG(_(e_invcmd)); | |
5318 return; | |
5319 } | |
5320 | |
5321 /* | |
5322 * pass 1: set marks for each (not) matching line | |
5323 */ | |
5324 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum) | |
5325 { | |
5326 /* a match on this line? */ | |
1521 | 5327 match = vim_regexec_multi(®match, curwin, curbuf, lnum, |
5328 (colnr_T)0, NULL); | |
7 | 5329 if ((type == 'g' && match) || (type == 'v' && !match)) |
5330 { | |
5331 ml_setmarked(lnum); | |
5332 ndone++; | |
5333 } | |
5334 line_breakcheck(); | |
5335 } | |
5336 | |
5337 /* | |
5338 * pass 2: execute the command for each line that has been marked | |
5339 */ | |
5340 if (got_int) | |
5341 MSG(_(e_interr)); | |
5342 else if (ndone == 0) | |
5343 { | |
5344 if (type == 'v') | |
274 | 5345 smsg((char_u *)_("Pattern found in every line: %s"), pat); |
7 | 5346 else |
274 | 5347 smsg((char_u *)_(e_patnotf2), pat); |
7 | 5348 } |
5349 else | |
5350 global_exe(cmd); | |
5351 | |
5352 ml_clearmarked(); /* clear rest of the marks */ | |
5353 vim_free(regmatch.regprog); | |
5354 } | |
5355 | |
5356 /* | |
5357 * Execute "cmd" on lines marked with ml_setmarked(). | |
5358 */ | |
5359 void | |
5360 global_exe(cmd) | |
5361 char_u *cmd; | |
5362 { | |
5363 linenr_T old_lcount; /* b_ml.ml_line_count before the command */ | |
5364 linenr_T lnum; /* line number according to old situation */ | |
5365 | |
5366 /* | |
5367 * Set current position only once for a global command. | |
5368 * If global_busy is set, setpcmark() will not do anything. | |
5369 * If there is an error, global_busy will be incremented. | |
5370 */ | |
5371 setpcmark(); | |
5372 | |
5373 /* When the command writes a message, don't overwrite the command. */ | |
5374 msg_didout = TRUE; | |
5375 | |
2127
4e22214f8464
updated for version 7.2.409
Bram Moolenaar <bram@zimbu.org>
parents:
2126
diff
changeset
|
5376 sub_nsubs = 0; |
4e22214f8464
updated for version 7.2.409
Bram Moolenaar <bram@zimbu.org>
parents:
2126
diff
changeset
|
5377 sub_nlines = 0; |
7 | 5378 global_need_beginline = FALSE; |
5379 global_busy = 1; | |
5380 old_lcount = curbuf->b_ml.ml_line_count; | |
5381 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) | |
5382 { | |
5383 curwin->w_cursor.lnum = lnum; | |
5384 curwin->w_cursor.col = 0; | |
5385 if (*cmd == NUL || *cmd == '\n') | |
5386 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT); | |
5387 else | |
5388 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT); | |
5389 ui_breakcheck(); | |
5390 } | |
5391 | |
5392 global_busy = 0; | |
5393 if (global_need_beginline) | |
5394 beginline(BL_WHITE | BL_FIX); | |
5395 else | |
5396 check_cursor(); /* cursor may be beyond the end of the line */ | |
5397 | |
39 | 5398 /* the cursor may not have moved in the text but a change in a previous |
5399 * line may move it on the screen */ | |
5400 changed_line_abv_curs(); | |
5401 | |
7 | 5402 /* If it looks like no message was written, allow overwriting the |
5403 * command with the report for number of changes. */ | |
5404 if (msg_col == 0 && msg_scrolled == 0) | |
5405 msg_didout = FALSE; | |
5406 | |
1227 | 5407 /* If substitutes done, report number of substitutes, otherwise report |
7 | 5408 * number of extra or deleted lines. */ |
170 | 5409 if (!do_sub_msg(FALSE)) |
7 | 5410 msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
5411 } | |
5412 | |
5413 #ifdef FEAT_VIMINFO | |
5414 int | |
5415 read_viminfo_sub_string(virp, force) | |
5416 vir_T *virp; | |
5417 int force; | |
5418 { | |
5419 if (old_sub != NULL && force) | |
5420 vim_free(old_sub); | |
5421 if (force || old_sub == NULL) | |
5422 old_sub = viminfo_readstring(virp, 1, TRUE); | |
5423 return viminfo_readline(virp); | |
5424 } | |
5425 | |
5426 void | |
5427 write_viminfo_sub_string(fp) | |
5428 FILE *fp; | |
5429 { | |
5430 if (get_viminfo_parameter('/') != 0 && old_sub != NULL) | |
5431 { | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
5432 fputs(_("\n# Last Substitute String:\n$"), fp); |
7 | 5433 viminfo_writestring(fp, old_sub); |
5434 } | |
5435 } | |
5436 #endif /* FEAT_VIMINFO */ | |
5437 | |
359 | 5438 #if defined(EXITFREE) || defined(PROTO) |
5439 void | |
5440 free_old_sub() | |
5441 { | |
5442 vim_free(old_sub); | |
5443 } | |
5444 #endif | |
5445 | |
7 | 5446 #if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO) |
5447 /* | |
5448 * Set up for a tagpreview. | |
735 | 5449 * Return TRUE when it was created. |
7 | 5450 */ |
735 | 5451 int |
816 | 5452 prepare_tagpreview(undo_sync) |
5453 int undo_sync; /* sync undo when leaving the window */ | |
7 | 5454 { |
5455 win_T *wp; | |
5456 | |
5457 # ifdef FEAT_GUI | |
5458 need_mouse_correct = TRUE; | |
5459 # endif | |
5460 | |
5461 /* | |
5462 * If there is already a preview window open, use that one. | |
5463 */ | |
5464 if (!curwin->w_p_pvw) | |
5465 { | |
5466 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
5467 if (wp->w_p_pvw) | |
5468 break; | |
5469 if (wp != NULL) | |
816 | 5470 win_enter(wp, undo_sync); |
7 | 5471 else |
5472 { | |
5473 /* | |
5474 * There is no preview window open yet. Create one. | |
5475 */ | |
5476 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) | |
5477 == FAIL) | |
735 | 5478 return FALSE; |
7 | 5479 curwin->w_p_pvw = TRUE; |
5480 curwin->w_p_wfh = TRUE; | |
2583 | 5481 RESET_BINDING(curwin); /* don't take over 'scrollbind' |
5482 and 'cursorbind' */ | |
7 | 5483 # ifdef FEAT_DIFF |
5484 curwin->w_p_diff = FALSE; /* no 'diff' */ | |
5485 # endif | |
5486 # ifdef FEAT_FOLDING | |
5487 curwin->w_p_fdc = 0; /* no 'foldcolumn' */ | |
5488 # endif | |
735 | 5489 return TRUE; |
7 | 5490 } |
5491 } | |
735 | 5492 return FALSE; |
7 | 5493 } |
5494 | |
5495 #endif | |
5496 | |
5497 | |
5498 /* | |
5499 * ":help": open a read-only window on a help file | |
5500 */ | |
5501 void | |
5502 ex_help(eap) | |
5503 exarg_T *eap; | |
5504 { | |
5505 char_u *arg; | |
5506 char_u *tag; | |
5507 FILE *helpfd; /* file descriptor of help file */ | |
5508 int n; | |
5509 int i; | |
5510 #ifdef FEAT_WINDOWS | |
5511 win_T *wp; | |
5512 #endif | |
5513 int num_matches; | |
5514 char_u **matches; | |
5515 char_u *p; | |
5516 int empty_fnum = 0; | |
5517 int alt_fnum = 0; | |
5518 buf_T *buf; | |
5519 #ifdef FEAT_MULTI_LANG | |
5520 int len; | |
9 | 5521 char_u *lang; |
7 | 5522 #endif |
5523 | |
5524 if (eap != NULL) | |
5525 { | |
5526 /* | |
5527 * A ":help" command ends at the first LF, or at a '|' that is | |
5528 * followed by some text. Set nextcmd to the following command. | |
5529 */ | |
5530 for (arg = eap->arg; *arg; ++arg) | |
5531 { | |
5532 if (*arg == '\n' || *arg == '\r' | |
5533 || (*arg == '|' && arg[1] != NUL && arg[1] != '|')) | |
5534 { | |
5535 *arg++ = NUL; | |
5536 eap->nextcmd = arg; | |
5537 break; | |
5538 } | |
5539 } | |
5540 arg = eap->arg; | |
5541 | |
5542 if (eap->forceit && *arg == NUL) | |
5543 { | |
5544 EMSG(_("E478: Don't panic!")); | |
5545 return; | |
5546 } | |
5547 | |
5548 if (eap->skip) /* not executing commands */ | |
5549 return; | |
5550 } | |
5551 else | |
5552 arg = (char_u *)""; | |
5553 | |
5554 /* remove trailing blanks */ | |
5555 p = arg + STRLEN(arg) - 1; | |
5556 while (p > arg && vim_iswhite(*p) && p[-1] != '\\') | |
5557 *p-- = NUL; | |
5558 | |
5559 #ifdef FEAT_MULTI_LANG | |
5560 /* Check for a specified language */ | |
9 | 5561 lang = check_help_lang(arg); |
7 | 5562 #endif |
5563 | |
5564 /* When no argument given go to the index. */ | |
5565 if (*arg == NUL) | |
5566 arg = (char_u *)"help.txt"; | |
5567 | |
5568 /* | |
5569 * Check if there is a match for the argument. | |
5570 */ | |
5571 n = find_help_tags(arg, &num_matches, &matches, | |
5572 eap != NULL && eap->forceit); | |
5573 | |
5574 i = 0; | |
5575 #ifdef FEAT_MULTI_LANG | |
5576 if (n != FAIL && lang != NULL) | |
5577 /* Find first item with the requested language. */ | |
5578 for (i = 0; i < num_matches; ++i) | |
5579 { | |
835 | 5580 len = (int)STRLEN(matches[i]); |
7 | 5581 if (len > 3 && matches[i][len - 3] == '@' |
5582 && STRICMP(matches[i] + len - 2, lang) == 0) | |
5583 break; | |
5584 } | |
5585 #endif | |
5586 if (i >= num_matches || n == FAIL) | |
5587 { | |
5588 #ifdef FEAT_MULTI_LANG | |
5589 if (lang != NULL) | |
5590 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg); | |
5591 else | |
5592 #endif | |
5593 EMSG2(_("E149: Sorry, no help for %s"), arg); | |
5594 if (n != FAIL) | |
5595 FreeWild(num_matches, matches); | |
5596 return; | |
5597 } | |
5598 | |
5599 /* The first match (in the requested language) is the best match. */ | |
5600 tag = vim_strsave(matches[i]); | |
5601 FreeWild(num_matches, matches); | |
5602 | |
5603 #ifdef FEAT_GUI | |
5604 need_mouse_correct = TRUE; | |
5605 #endif | |
5606 | |
5607 /* | |
5608 * Re-use an existing help window or open a new one. | |
684 | 5609 * Always open a new one for ":tab help". |
7 | 5610 */ |
684 | 5611 if (!curwin->w_buffer->b_help |
5612 #ifdef FEAT_WINDOWS | |
5613 || cmdmod.tab != 0 | |
5614 #endif | |
5615 ) | |
7 | 5616 { |
5617 #ifdef FEAT_WINDOWS | |
684 | 5618 if (cmdmod.tab != 0) |
5619 wp = NULL; | |
5620 else | |
5621 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
5622 if (wp->w_buffer != NULL && wp->w_buffer->b_help) | |
5623 break; | |
7 | 5624 if (wp != NULL && wp->w_buffer->b_nwindows > 0) |
5625 win_enter(wp, TRUE); | |
5626 else | |
5627 #endif | |
5628 { | |
5629 /* | |
5630 * There is no help window yet. | |
5631 * Try to open the file specified by the "helpfile" option. | |
5632 */ | |
5633 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL) | |
5634 { | |
274 | 5635 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf); |
7 | 5636 goto erret; |
5637 } | |
5638 fclose(helpfd); | |
5639 | |
5640 #ifdef FEAT_WINDOWS | |
5641 /* Split off help window; put it at far top if no position | |
684 | 5642 * specified, the current window is vertically split and |
5643 * narrow. */ | |
7 | 5644 n = WSP_HELP; |
5645 # ifdef FEAT_VERTSPLIT | |
5646 if (cmdmod.split == 0 && curwin->w_width != Columns | |
684 | 5647 && curwin->w_width < 80) |
7 | 5648 n |= WSP_TOP; |
5649 # endif | |
5650 if (win_split(0, n) == FAIL) | |
684 | 5651 goto erret; |
7 | 5652 #else |
5653 /* use current window */ | |
5654 if (!can_abandon(curbuf, FALSE)) | |
5655 goto erret; | |
684 | 5656 #endif |
7 | 5657 |
5658 #ifdef FEAT_WINDOWS | |
5659 if (curwin->w_height < p_hh) | |
5660 win_setheight((int)p_hh); | |
5661 #endif | |
5662 | |
5663 /* | |
5664 * Open help file (do_ecmd() will set b_help flag, readfile() will | |
5665 * set b_p_ro flag). | |
5666 * Set the alternate file to the previously edited file. | |
5667 */ | |
5668 alt_fnum = curbuf->b_fnum; | |
5669 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, | |
1743 | 5670 ECMD_HIDE + ECMD_SET_HELP, |
5671 #ifdef FEAT_WINDOWS | |
5672 NULL /* buffer is still open, don't store info */ | |
5673 #else | |
5674 curwin | |
5675 #endif | |
5676 ); | |
22 | 5677 if (!cmdmod.keepalt) |
5678 curwin->w_alt_fnum = alt_fnum; | |
7 | 5679 empty_fnum = curbuf->b_fnum; |
5680 } | |
5681 } | |
5682 | |
5683 if (!p_im) | |
5684 restart_edit = 0; /* don't want insert mode in help file */ | |
5685 | |
5686 if (tag != NULL) | |
5687 do_tag(tag, DT_HELP, 1, FALSE, TRUE); | |
5688 | |
819 | 5689 /* Delete the empty buffer if we're not using it. Careful: autocommands |
5690 * may have jumped to another window, check that the buffer is not in a | |
5691 * window. */ | |
7 | 5692 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum) |
5693 { | |
5694 buf = buflist_findnr(empty_fnum); | |
819 | 5695 if (buf != NULL && buf->b_nwindows == 0) |
7 | 5696 wipe_buffer(buf, TRUE); |
5697 } | |
5698 | |
5699 /* keep the previous alternate file */ | |
22 | 5700 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) |
7 | 5701 curwin->w_alt_fnum = alt_fnum; |
5702 | |
5703 erret: | |
5704 vim_free(tag); | |
5705 } | |
5706 | |
5707 | |
9 | 5708 #if defined(FEAT_MULTI_LANG) || defined(PROTO) |
5709 /* | |
5710 * In an argument search for a language specifiers in the form "@xx". | |
5711 * Changes the "@" to NUL if found, and returns a pointer to "xx". | |
5712 * Returns NULL if not found. | |
5713 */ | |
5714 char_u * | |
5715 check_help_lang(arg) | |
5716 char_u *arg; | |
5717 { | |
835 | 5718 int len = (int)STRLEN(arg); |
9 | 5719 |
5720 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) | |
5721 && ASCII_ISALPHA(arg[len - 1])) | |
5722 { | |
5723 arg[len - 3] = NUL; /* remove the '@' */ | |
5724 return arg + len - 2; | |
5725 } | |
5726 return NULL; | |
5727 } | |
5728 #endif | |
5729 | |
7 | 5730 /* |
5731 * Return a heuristic indicating how well the given string matches. The | |
5732 * smaller the number, the better the match. This is the order of priorities, | |
5733 * from best match to worst match: | |
5734 * - Match with least alpha-numeric characters is better. | |
5735 * - Match with least total characters is better. | |
5736 * - Match towards the start is better. | |
5737 * - Match starting with "+" is worse (feature instead of command) | |
5738 * Assumption is made that the matched_string passed has already been found to | |
5739 * match some string for which help is requested. webb. | |
5740 */ | |
5741 int | |
5742 help_heuristic(matched_string, offset, wrong_case) | |
5743 char_u *matched_string; | |
5744 int offset; /* offset for match */ | |
5745 int wrong_case; /* no matching case */ | |
5746 { | |
5747 int num_letters; | |
5748 char_u *p; | |
5749 | |
5750 num_letters = 0; | |
5751 for (p = matched_string; *p; p++) | |
5752 if (ASCII_ISALNUM(*p)) | |
5753 num_letters++; | |
5754 | |
5755 /* | |
5756 * Multiply the number of letters by 100 to give it a much bigger | |
5757 * weighting than the number of characters. | |
5758 * If there only is a match while ignoring case, add 5000. | |
5759 * If the match starts in the middle of a word, add 10000 to put it | |
5760 * somewhere in the last half. | |
5761 * If the match is more than 2 chars from the start, multiply by 200 to | |
5762 * put it after matches at the start. | |
5763 */ | |
5764 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0 | |
5765 && ASCII_ISALNUM(matched_string[offset - 1])) | |
5766 offset += 10000; | |
5767 else if (offset > 2) | |
5768 offset *= 200; | |
5769 if (wrong_case) | |
5770 offset += 5000; | |
5771 /* Features are less interesting than the subjects themselves, but "+" | |
5772 * alone is not a feature. */ | |
5773 if (matched_string[0] == '+' && matched_string[1] != NUL) | |
5774 offset += 100; | |
5775 return (int)(100 * num_letters + STRLEN(matched_string) + offset); | |
5776 } | |
5777 | |
5778 /* | |
5779 * Compare functions for qsort() below, that checks the help heuristics number | |
5780 * that has been put after the tagname by find_tags(). | |
5781 */ | |
5782 static int | |
5783 #ifdef __BORLANDC__ | |
5784 _RTLENTRYF | |
5785 #endif | |
5786 help_compare(s1, s2) | |
5787 const void *s1; | |
5788 const void *s2; | |
5789 { | |
5790 char *p1; | |
5791 char *p2; | |
5792 | |
5793 p1 = *(char **)s1 + strlen(*(char **)s1) + 1; | |
5794 p2 = *(char **)s2 + strlen(*(char **)s2) + 1; | |
5795 return strcmp(p1, p2); | |
5796 } | |
5797 | |
5798 /* | |
5799 * Find all help tags matching "arg", sort them and return in matches[], with | |
5800 * the number of matches in num_matches. | |
5801 * The matches will be sorted with a "best" match algorithm. | |
5802 * When "keep_lang" is TRUE try keeping the language of the current buffer. | |
5803 */ | |
5804 int | |
5805 find_help_tags(arg, num_matches, matches, keep_lang) | |
5806 char_u *arg; | |
5807 int *num_matches; | |
5808 char_u ***matches; | |
5809 int keep_lang; | |
5810 { | |
5811 char_u *s, *d; | |
5812 int i; | |
5813 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*", | |
445 | 5814 "/*", "/\\*", "\"*", "**", |
5815 "/\\(\\)", | |
323 | 5816 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
279 | 5817 "/\\?", "/\\z(\\)", "\\=", ":s\\=", |
7 | 5818 "[count]", "[quotex]", "[range]", |
5819 "[pattern]", "\\|", "\\%$"}; | |
5820 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star", | |
445 | 5821 "/star", "/\\\\star", "quotestar", "starstar", |
5822 "/\\\\(\\\\)", | |
323 | 5823 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
279 | 5824 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", |
7 | 5825 "\\[count]", "\\[quotex]", "\\[range]", |
5826 "\\[pattern]", "\\\\bar", "/\\\\%\\$"}; | |
5827 int flags; | |
5828 | |
5829 d = IObuff; /* assume IObuff is long enough! */ | |
5830 | |
5831 /* | |
5832 * Recognize a few exceptions to the rule. Some strings that contain '*' | |
5833 * with "star". Otherwise '*' is recognized as a wildcard. | |
5834 */ | |
1872 | 5835 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; ) |
7 | 5836 if (STRCMP(arg, mtable[i]) == 0) |
5837 { | |
5838 STRCPY(d, rtable[i]); | |
5839 break; | |
5840 } | |
5841 | |
5842 if (i < 0) /* no match in table */ | |
5843 { | |
5844 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched. | |
5845 * Also replace "\%^" and "\%(", they match every tag too. | |
5846 * Also "\zs", "\z1", etc. | |
5847 * Also "\@<", "\@=", "\@<=", etc. | |
5848 * And also "\_$" and "\_^". */ | |
5849 if (arg[0] == '\\' | |
5850 && ((arg[1] != NUL && arg[2] == NUL) | |
5851 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL | |
5852 && arg[2] != NUL))) | |
5853 { | |
5854 STRCPY(d, "/\\\\"); | |
5855 STRCPY(d + 3, arg + 1); | |
5856 /* Check for "/\\_$", should be "/\\_\$" */ | |
5857 if (d[3] == '_' && d[4] == '$') | |
5858 STRCPY(d + 4, "\\$"); | |
5859 } | |
5860 else | |
5861 { | |
5862 /* replace "[:...:]" with "\[:...:]"; "[+...]" with "\[++...]" */ | |
5863 if (arg[0] == '[' && (arg[1] == ':' | |
5864 || (arg[1] == '+' && arg[2] == '+'))) | |
5865 *d++ = '\\'; | |
5866 | |
5867 for (s = arg; *s; ++s) | |
5868 { | |
5869 /* | |
5870 * Replace "|" with "bar" and '"' with "quote" to match the name of | |
5871 * the tags for these commands. | |
5872 * Replace "*" with ".*" and "?" with "." to match command line | |
5873 * completion. | |
5874 * Insert a backslash before '~', '$' and '.' to avoid their | |
5875 * special meaning. | |
5876 */ | |
5877 if (d - IObuff > IOSIZE - 10) /* getting too long!? */ | |
5878 break; | |
5879 switch (*s) | |
5880 { | |
5881 case '|': STRCPY(d, "bar"); | |
5882 d += 3; | |
5883 continue; | |
5884 case '"': STRCPY(d, "quote"); | |
5885 d += 5; | |
5886 continue; | |
5887 case '*': *d++ = '.'; | |
5888 break; | |
5889 case '?': *d++ = '.'; | |
5890 continue; | |
5891 case '$': | |
5892 case '.': | |
5893 case '~': *d++ = '\\'; | |
5894 break; | |
5895 } | |
5896 | |
5897 /* | |
5898 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make | |
5899 * ":help i_^_CTRL-D" work. | |
5900 * Insert '-' before and after "CTRL-X" when applicable. | |
5901 */ | |
5902 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) | |
5903 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL))) | |
5904 { | |
5905 if (d > IObuff && d[-1] != '_') | |
5906 *d++ = '_'; /* prepend a '_' */ | |
5907 STRCPY(d, "CTRL-"); | |
5908 d += 5; | |
5909 if (*s < ' ') | |
5910 { | |
5911 #ifdef EBCDIC | |
5912 *d++ = CtrlChar(*s); | |
5913 #else | |
5914 *d++ = *s + '@'; | |
5915 #endif | |
5916 if (d[-1] == '\\') | |
5917 *d++ = '\\'; /* double a backslash */ | |
5918 } | |
5919 else | |
5920 *d++ = *++s; | |
5921 if (s[1] != NUL && s[1] != '_') | |
5922 *d++ = '_'; /* append a '_' */ | |
5923 continue; | |
5924 } | |
5925 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */ | |
5926 *d++ = '\\'; | |
5927 | |
5928 /* | |
5929 * Insert a backslash before a backslash after a slash, for search | |
5930 * pattern tags: "/\|" --> "/\\|". | |
5931 */ | |
5932 else if (s[0] == '\\' && s[1] != '\\' | |
5933 && *arg == '/' && s == arg + 1) | |
5934 *d++ = '\\'; | |
5935 | |
5936 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in | |
5937 * "CTRL-\_CTRL-N" */ | |
5938 if (STRNICMP(s, "CTRL-\\_", 7) == 0) | |
5939 { | |
5940 STRCPY(d, "CTRL-\\\\"); | |
5941 d += 7; | |
5942 s += 6; | |
5943 } | |
5944 | |
5945 *d++ = *s; | |
5946 | |
5947 /* | |
5948 * If tag starts with ', toss everything after a second '. Fixes | |
5949 * CTRL-] on 'option'. (would include the trailing '.'). | |
5950 */ | |
5951 if (*s == '\'' && s > arg && *arg == '\'') | |
5952 break; | |
5953 } | |
5954 *d = NUL; | |
5955 } | |
5956 } | |
5957 | |
5958 *matches = (char_u **)""; | |
5959 *num_matches = 0; | |
5960 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE; | |
5961 if (keep_lang) | |
5962 flags |= TAG_KEEP_LANG; | |
1696 | 5963 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK |
7 | 5964 && *num_matches > 0) |
1696 | 5965 { |
7 | 5966 /* Sort the matches found on the heuristic number that is after the |
5967 * tag name. */ | |
5968 qsort((void *)*matches, (size_t)*num_matches, | |
5969 sizeof(char_u *), help_compare); | |
1696 | 5970 /* Delete more than TAG_MANY to reduce the size of the listing. */ |
5971 while (*num_matches > TAG_MANY) | |
5972 vim_free((*matches)[--*num_matches]); | |
5973 } | |
7 | 5974 return OK; |
5975 } | |
5976 | |
5977 /* | |
5978 * After reading a help file: May cleanup a help buffer when syntax | |
5979 * highlighting is not used. | |
5980 */ | |
5981 void | |
5982 fix_help_buffer() | |
5983 { | |
5984 linenr_T lnum; | |
5985 char_u *line; | |
5986 int in_example = FALSE; | |
5987 int len; | |
5988 char_u *p; | |
5989 char_u *rt; | |
5990 int mustfree; | |
5991 | |
5992 /* set filetype to "help". */ | |
5993 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); | |
5994 | |
5995 #ifdef FEAT_SYN_HL | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5996 if (!syntax_present(curwin)) |
7 | 5997 #endif |
5998 { | |
5999 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) | |
6000 { | |
6001 line = ml_get_buf(curbuf, lnum, FALSE); | |
6002 len = (int)STRLEN(line); | |
6003 if (in_example && len > 0 && !vim_iswhite(line[0])) | |
6004 { | |
6005 /* End of example: non-white or '<' in first column. */ | |
6006 if (line[0] == '<') | |
6007 { | |
6008 /* blank-out a '<' in the first column */ | |
6009 line = ml_get_buf(curbuf, lnum, TRUE); | |
6010 line[0] = ' '; | |
6011 } | |
6012 in_example = FALSE; | |
6013 } | |
6014 if (!in_example && len > 0) | |
6015 { | |
6016 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) | |
6017 { | |
6018 /* blank-out a '>' in the last column (start of example) */ | |
6019 line = ml_get_buf(curbuf, lnum, TRUE); | |
6020 line[len - 1] = ' '; | |
6021 in_example = TRUE; | |
6022 } | |
6023 else if (line[len - 1] == '~') | |
6024 { | |
6025 /* blank-out a '~' at the end of line (header marker) */ | |
6026 line = ml_get_buf(curbuf, lnum, TRUE); | |
6027 line[len - 1] = ' '; | |
6028 } | |
6029 } | |
6030 } | |
6031 } | |
6032 | |
6033 /* | |
6034 * In the "help.txt" file, add the locally added help files. | |
6035 * This uses the very first line in the help file. | |
6036 */ | |
6037 if (fnamecmp(gettail(curbuf->b_fname), "help.txt") == 0) | |
6038 { | |
6039 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) | |
6040 { | |
6041 line = ml_get_buf(curbuf, lnum, FALSE); | |
6042 if (strstr((char *)line, "*local-additions*") != NULL) | |
6043 { | |
6044 /* Go through all directories in 'runtimepath', skipping | |
6045 * $VIMRUNTIME. */ | |
6046 p = p_rtp; | |
6047 while (*p != NUL) | |
6048 { | |
6049 copy_option_part(&p, NameBuff, MAXPATHL, ","); | |
6050 mustfree = FALSE; | |
6051 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); | |
6052 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME) | |
6053 { | |
6054 int fcount; | |
6055 char_u **fnames; | |
6056 FILE *fd; | |
6057 char_u *s; | |
6058 int fi; | |
6059 #ifdef FEAT_MBYTE | |
6060 vimconv_T vc; | |
6061 char_u *cp; | |
6062 #endif | |
6063 | |
6064 /* Find all "doc/ *.txt" files in this directory. */ | |
6065 add_pathsep(NameBuff); | |
6066 STRCAT(NameBuff, "doc/*.txt"); | |
6067 if (gen_expand_wildcards(1, &NameBuff, &fcount, | |
6068 &fnames, EW_FILE|EW_SILENT) == OK | |
6069 && fcount > 0) | |
6070 { | |
6071 for (fi = 0; fi < fcount; ++fi) | |
6072 { | |
531 | 6073 fd = mch_fopen((char *)fnames[fi], "r"); |
7 | 6074 if (fd != NULL) |
6075 { | |
6076 vim_fgets(IObuff, IOSIZE, fd); | |
6077 if (IObuff[0] == '*' | |
6078 && (s = vim_strchr(IObuff + 1, '*')) | |
6079 != NULL) | |
6080 { | |
6081 #ifdef FEAT_MBYTE | |
6082 int this_utf = MAYBE; | |
6083 #endif | |
6084 /* Change tag definition to a | |
6085 * reference and remove <CR>/<NL>. */ | |
6086 IObuff[0] = '|'; | |
6087 *s = '|'; | |
6088 while (*s != NUL) | |
6089 { | |
6090 if (*s == '\r' || *s == '\n') | |
6091 *s = NUL; | |
6092 #ifdef FEAT_MBYTE | |
6093 /* The text is utf-8 when a byte | |
6094 * above 127 is found and no | |
6095 * illegal byte sequence is found. | |
6096 */ | |
6097 if (*s >= 0x80 && this_utf != FALSE) | |
6098 { | |
6099 int l; | |
6100 | |
6101 this_utf = TRUE; | |
474 | 6102 l = utf_ptr2len(s); |
7 | 6103 if (l == 1) |
6104 this_utf = FALSE; | |
6105 s += l - 1; | |
6106 } | |
6107 #endif | |
6108 ++s; | |
6109 } | |
6110 #ifdef FEAT_MBYTE | |
6111 /* The help file is latin1 or utf-8; | |
6112 * conversion to the current | |
6113 * 'encoding' may be required. */ | |
6114 vc.vc_type = CONV_NONE; | |
6115 convert_setup(&vc, (char_u *)( | |
6116 this_utf == TRUE ? "utf-8" | |
6117 : "latin1"), p_enc); | |
6118 if (vc.vc_type == CONV_NONE) | |
6119 /* No conversion needed. */ | |
6120 cp = IObuff; | |
6121 else | |
6122 { | |
6123 /* Do the conversion. If it fails | |
6124 * use the unconverted text. */ | |
6125 cp = string_convert(&vc, IObuff, | |
6126 NULL); | |
6127 if (cp == NULL) | |
6128 cp = IObuff; | |
6129 } | |
6130 convert_setup(&vc, NULL, NULL); | |
6131 | |
6132 ml_append(lnum, cp, (colnr_T)0, FALSE); | |
6133 if (cp != IObuff) | |
6134 vim_free(cp); | |
6135 #else | |
6136 ml_append(lnum, IObuff, (colnr_T)0, | |
6137 FALSE); | |
6138 #endif | |
6139 ++lnum; | |
6140 } | |
6141 fclose(fd); | |
6142 } | |
6143 } | |
6144 FreeWild(fcount, fnames); | |
6145 } | |
6146 } | |
6147 if (mustfree) | |
6148 vim_free(rt); | |
6149 } | |
6150 break; | |
6151 } | |
6152 } | |
6153 } | |
6154 } | |
6155 | |
40 | 6156 /* |
6157 * ":exusage" | |
6158 */ | |
6159 void | |
6160 ex_exusage(eap) | |
1876 | 6161 exarg_T *eap UNUSED; |
40 | 6162 { |
6163 do_cmdline_cmd((char_u *)"help ex-cmd-index"); | |
6164 } | |
6165 | |
6166 /* | |
6167 * ":viusage" | |
6168 */ | |
6169 void | |
6170 ex_viusage(eap) | |
1876 | 6171 exarg_T *eap UNUSED; |
40 | 6172 { |
6173 do_cmdline_cmd((char_u *)"help normal-index"); | |
6174 } | |
6175 | |
7 | 6176 #if defined(FEAT_EX_EXTRA) || defined(PROTO) |
1502 | 6177 static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang, int add_help_tags)); |
7 | 6178 |
6179 /* | |
6180 * ":helptags" | |
6181 */ | |
6182 void | |
6183 ex_helptags(eap) | |
6184 exarg_T *eap; | |
6185 { | |
6186 garray_T ga; | |
6187 int i, j; | |
6188 int len; | |
9 | 6189 #ifdef FEAT_MULTI_LANG |
7 | 6190 char_u lang[2]; |
9 | 6191 #endif |
1507 | 6192 expand_T xpc; |
6193 char_u *dirname; | |
7 | 6194 char_u ext[5]; |
6195 char_u fname[8]; | |
6196 int filecount; | |
6197 char_u **files; | |
1502 | 6198 int add_help_tags = FALSE; |
6199 | |
6200 /* Check for ":helptags ++t {dir}". */ | |
6201 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3])) | |
6202 { | |
6203 add_help_tags = TRUE; | |
6204 eap->arg = skipwhite(eap->arg + 3); | |
6205 } | |
7 | 6206 |
1507 | 6207 ExpandInit(&xpc); |
6208 xpc.xp_context = EXPAND_DIRECTORIES; | |
6209 dirname = ExpandOne(&xpc, eap->arg, NULL, | |
6210 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE); | |
6211 if (dirname == NULL || !mch_isdir(dirname)) | |
7 | 6212 { |
6213 EMSG2(_("E150: Not a directory: %s"), eap->arg); | |
6214 return; | |
6215 } | |
6216 | |
6217 #ifdef FEAT_MULTI_LANG | |
6218 /* Get a list of all files in the directory. */ | |
1507 | 6219 STRCPY(NameBuff, dirname); |
7 | 6220 add_pathsep(NameBuff); |
6221 STRCAT(NameBuff, "*"); | |
6222 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, | |
6223 EW_FILE|EW_SILENT) == FAIL | |
6224 || filecount == 0) | |
6225 { | |
6226 EMSG2("E151: No match: %s", NameBuff); | |
1507 | 6227 vim_free(dirname); |
7 | 6228 return; |
6229 } | |
6230 | |
6231 /* Go over all files in the directory to find out what languages are | |
6232 * present. */ | |
6233 ga_init2(&ga, 1, 10); | |
6234 for (i = 0; i < filecount; ++i) | |
6235 { | |
835 | 6236 len = (int)STRLEN(files[i]); |
7 | 6237 if (len > 4) |
6238 { | |
6239 if (STRICMP(files[i] + len - 4, ".txt") == 0) | |
6240 { | |
6241 /* ".txt" -> language "en" */ | |
6242 lang[0] = 'e'; | |
6243 lang[1] = 'n'; | |
6244 } | |
6245 else if (files[i][len - 4] == '.' | |
6246 && ASCII_ISALPHA(files[i][len - 3]) | |
6247 && ASCII_ISALPHA(files[i][len - 2]) | |
6248 && TOLOWER_ASC(files[i][len - 1]) == 'x') | |
6249 { | |
6250 /* ".abx" -> language "ab" */ | |
6251 lang[0] = TOLOWER_ASC(files[i][len - 3]); | |
6252 lang[1] = TOLOWER_ASC(files[i][len - 2]); | |
6253 } | |
6254 else | |
6255 continue; | |
6256 | |
6257 /* Did we find this language already? */ | |
6258 for (j = 0; j < ga.ga_len; j += 2) | |
6259 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) | |
6260 break; | |
6261 if (j == ga.ga_len) | |
6262 { | |
6263 /* New language, add it. */ | |
6264 if (ga_grow(&ga, 2) == FAIL) | |
6265 break; | |
6266 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0]; | |
6267 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1]; | |
6268 } | |
6269 } | |
6270 } | |
6271 | |
6272 /* | |
6273 * Loop over the found languages to generate a tags file for each one. | |
6274 */ | |
6275 for (j = 0; j < ga.ga_len; j += 2) | |
6276 { | |
6277 STRCPY(fname, "tags-xx"); | |
6278 fname[5] = ((char_u *)ga.ga_data)[j]; | |
6279 fname[6] = ((char_u *)ga.ga_data)[j + 1]; | |
6280 if (fname[5] == 'e' && fname[6] == 'n') | |
6281 { | |
6282 /* English is an exception: use ".txt" and "tags". */ | |
6283 fname[4] = NUL; | |
6284 STRCPY(ext, ".txt"); | |
6285 } | |
6286 else | |
6287 { | |
6288 /* Language "ab" uses ".abx" and "tags-ab". */ | |
6289 STRCPY(ext, ".xxx"); | |
6290 ext[1] = fname[5]; | |
6291 ext[2] = fname[6]; | |
6292 } | |
1507 | 6293 helptags_one(dirname, ext, fname, add_help_tags); |
7 | 6294 } |
6295 | |
6296 ga_clear(&ga); | |
6297 FreeWild(filecount, files); | |
6298 | |
6299 #else | |
6300 /* No language support, just use "*.txt" and "tags". */ | |
1507 | 6301 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags); |
6302 #endif | |
6303 vim_free(dirname); | |
7 | 6304 } |
6305 | |
6306 static void | |
1502 | 6307 helptags_one(dir, ext, tagfname, add_help_tags) |
6308 char_u *dir; /* doc directory */ | |
6309 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */ | |
6310 char_u *tagfname; /* "tags" for English, "tags-fr" for French. */ | |
6311 int add_help_tags; /* add "help-tags" tag */ | |
7 | 6312 { |
6313 FILE *fd_tags; | |
6314 FILE *fd; | |
6315 garray_T ga; | |
6316 int filecount; | |
6317 char_u **files; | |
6318 char_u *p1, *p2; | |
6319 int fi; | |
6320 char_u *s; | |
6321 int i; | |
6322 char_u *fname; | |
6323 # ifdef FEAT_MBYTE | |
6324 int utf8 = MAYBE; | |
6325 int this_utf8; | |
6326 int firstline; | |
6327 int mix = FALSE; /* detected mixed encodings */ | |
6328 # endif | |
6329 | |
6330 /* | |
6331 * Find all *.txt files. | |
6332 */ | |
6333 STRCPY(NameBuff, dir); | |
6334 add_pathsep(NameBuff); | |
6335 STRCAT(NameBuff, "*"); | |
6336 STRCAT(NameBuff, ext); | |
6337 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, | |
6338 EW_FILE|EW_SILENT) == FAIL | |
6339 || filecount == 0) | |
6340 { | |
6341 if (!got_int) | |
6342 EMSG2("E151: No match: %s", NameBuff); | |
6343 return; | |
6344 } | |
6345 | |
6346 /* | |
6347 * Open the tags file for writing. | |
6348 * Do this before scanning through all the files. | |
6349 */ | |
6350 STRCPY(NameBuff, dir); | |
6351 add_pathsep(NameBuff); | |
6352 STRCAT(NameBuff, tagfname); | |
531 | 6353 fd_tags = mch_fopen((char *)NameBuff, "w"); |
7 | 6354 if (fd_tags == NULL) |
6355 { | |
6356 EMSG2(_("E152: Cannot open %s for writing"), NameBuff); | |
6357 FreeWild(filecount, files); | |
6358 return; | |
6359 } | |
6360 | |
6361 /* | |
1502 | 6362 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc" |
6363 * add the "help-tags" tag. | |
7 | 6364 */ |
6365 ga_init2(&ga, (int)sizeof(char_u *), 100); | |
1502 | 6366 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc", |
6367 dir, FALSE) == FPC_SAME) | |
7 | 6368 { |
6369 if (ga_grow(&ga, 1) == FAIL) | |
6370 got_int = TRUE; | |
6371 else | |
6372 { | |
835 | 6373 s = alloc(18 + (unsigned)STRLEN(tagfname)); |
7 | 6374 if (s == NULL) |
6375 got_int = TRUE; | |
6376 else | |
6377 { | |
6378 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); | |
6379 ((char_u **)ga.ga_data)[ga.ga_len] = s; | |
6380 ++ga.ga_len; | |
6381 } | |
6382 } | |
6383 } | |
6384 | |
6385 /* | |
6386 * Go over all the files and extract the tags. | |
6387 */ | |
6388 for (fi = 0; fi < filecount && !got_int; ++fi) | |
6389 { | |
531 | 6390 fd = mch_fopen((char *)files[fi], "r"); |
7 | 6391 if (fd == NULL) |
6392 { | |
6393 EMSG2(_("E153: Unable to open %s for reading"), files[fi]); | |
6394 continue; | |
6395 } | |
6396 fname = gettail(files[fi]); | |
6397 | |
6398 # ifdef FEAT_MBYTE | |
6399 firstline = TRUE; | |
6400 # endif | |
6401 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) | |
6402 { | |
6403 # ifdef FEAT_MBYTE | |
6404 if (firstline) | |
6405 { | |
6406 /* Detect utf-8 file by a non-ASCII char in the first line. */ | |
6407 this_utf8 = MAYBE; | |
6408 for (s = IObuff; *s != NUL; ++s) | |
6409 if (*s >= 0x80) | |
6410 { | |
6411 int l; | |
6412 | |
6413 this_utf8 = TRUE; | |
474 | 6414 l = utf_ptr2len(s); |
7 | 6415 if (l == 1) |
6416 { | |
6417 /* Illegal UTF-8 byte sequence. */ | |
6418 this_utf8 = FALSE; | |
6419 break; | |
6420 } | |
6421 s += l - 1; | |
6422 } | |
6423 if (this_utf8 == MAYBE) /* only ASCII characters found */ | |
6424 this_utf8 = FALSE; | |
6425 if (utf8 == MAYBE) /* first file */ | |
6426 utf8 = this_utf8; | |
6427 else if (utf8 != this_utf8) | |
6428 { | |
6429 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]); | |
6430 mix = !got_int; | |
6431 got_int = TRUE; | |
6432 } | |
6433 firstline = FALSE; | |
6434 } | |
6435 # endif | |
6436 p1 = vim_strchr(IObuff, '*'); /* find first '*' */ | |
6437 while (p1 != NULL) | |
6438 { | |
6439 p2 = vim_strchr(p1 + 1, '*'); /* find second '*' */ | |
6440 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */ | |
6441 { | |
6442 for (s = p1 + 1; s < p2; ++s) | |
6443 if (*s == ' ' || *s == '\t' || *s == '|') | |
6444 break; | |
6445 | |
6446 /* | |
6447 * Only accept a *tag* when it consists of valid | |
6448 * characters, there is white space before it and is | |
6449 * followed by a white character or end-of-line. | |
6450 */ | |
6451 if (s == p2 | |
6452 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t') | |
6453 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL | |
6454 || s[1] == '\0')) | |
6455 { | |
6456 *p2 = '\0'; | |
6457 ++p1; | |
6458 if (ga_grow(&ga, 1) == FAIL) | |
6459 { | |
6460 got_int = TRUE; | |
6461 break; | |
6462 } | |
6463 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2)); | |
6464 if (s == NULL) | |
6465 { | |
6466 got_int = TRUE; | |
6467 break; | |
6468 } | |
6469 ((char_u **)ga.ga_data)[ga.ga_len] = s; | |
6470 ++ga.ga_len; | |
6471 sprintf((char *)s, "%s\t%s", p1, fname); | |
6472 | |
6473 /* find next '*' */ | |
6474 p2 = vim_strchr(p2 + 1, '*'); | |
6475 } | |
6476 } | |
6477 p1 = p2; | |
6478 } | |
6479 line_breakcheck(); | |
6480 } | |
6481 | |
6482 fclose(fd); | |
6483 } | |
6484 | |
6485 FreeWild(filecount, files); | |
6486 | |
6487 if (!got_int) | |
6488 { | |
6489 /* | |
6490 * Sort the tags. | |
6491 */ | |
6492 sort_strings((char_u **)ga.ga_data, ga.ga_len); | |
6493 | |
6494 /* | |
6495 * Check for duplicates. | |
6496 */ | |
6497 for (i = 1; i < ga.ga_len; ++i) | |
6498 { | |
6499 p1 = ((char_u **)ga.ga_data)[i - 1]; | |
6500 p2 = ((char_u **)ga.ga_data)[i]; | |
6501 while (*p1 == *p2) | |
6502 { | |
6503 if (*p2 == '\t') | |
6504 { | |
6505 *p2 = NUL; | |
274 | 6506 vim_snprintf((char *)NameBuff, MAXPATHL, |
7 | 6507 _("E154: Duplicate tag \"%s\" in file %s/%s"), |
6508 ((char_u **)ga.ga_data)[i], dir, p2 + 1); | |
6509 EMSG(NameBuff); | |
6510 *p2 = '\t'; | |
6511 break; | |
6512 } | |
6513 ++p1; | |
6514 ++p2; | |
6515 } | |
6516 } | |
6517 | |
6518 # ifdef FEAT_MBYTE | |
6519 if (utf8 == TRUE) | |
6520 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); | |
6521 # endif | |
6522 | |
6523 /* | |
6524 * Write the tags into the file. | |
6525 */ | |
6526 for (i = 0; i < ga.ga_len; ++i) | |
6527 { | |
6528 s = ((char_u **)ga.ga_data)[i]; | |
1325 | 6529 if (STRNCMP(s, "help-tags\t", 10) == 0) |
7 | 6530 /* help-tags entry was added in formatted form */ |
1325 | 6531 fputs((char *)s, fd_tags); |
7 | 6532 else |
6533 { | |
6534 fprintf(fd_tags, "%s\t/*", s); | |
6535 for (p1 = s; *p1 != '\t'; ++p1) | |
6536 { | |
6537 /* insert backslash before '\\' and '/' */ | |
6538 if (*p1 == '\\' || *p1 == '/') | |
6539 putc('\\', fd_tags); | |
6540 putc(*p1, fd_tags); | |
6541 } | |
6542 fprintf(fd_tags, "*\n"); | |
6543 } | |
6544 } | |
6545 } | |
6546 #ifdef FEAT_MBYTE | |
6547 if (mix) | |
6548 got_int = FALSE; /* continue with other languages */ | |
6549 #endif | |
6550 | |
6551 for (i = 0; i < ga.ga_len; ++i) | |
6552 vim_free(((char_u **)ga.ga_data)[i]); | |
6553 ga_clear(&ga); | |
6554 fclose(fd_tags); /* there is no check for an error... */ | |
6555 } | |
6556 #endif | |
6557 | |
6558 #if defined(FEAT_SIGNS) || defined(PROTO) | |
6559 | |
6560 /* | |
6561 * Struct to hold the sign properties. | |
6562 */ | |
6563 typedef struct sign sign_T; | |
6564 | |
6565 struct sign | |
6566 { | |
6567 sign_T *sn_next; /* next sign in list */ | |
2606 | 6568 int sn_typenr; /* type number of sign */ |
7 | 6569 char_u *sn_name; /* name of sign */ |
6570 char_u *sn_icon; /* name of pixmap */ | |
6571 #ifdef FEAT_SIGN_ICONS | |
6572 void *sn_image; /* icon image */ | |
6573 #endif | |
6574 char_u *sn_text; /* text used instead of pixmap */ | |
6575 int sn_line_hl; /* highlight ID for line */ | |
6576 int sn_text_hl; /* highlight ID for text */ | |
6577 }; | |
6578 | |
6579 static sign_T *first_sign = NULL; | |
2605 | 6580 static int next_sign_typenr = 1; |
7 | 6581 |
1874 | 6582 static int sign_cmd_idx __ARGS((char_u *begin_cmd, char_u *end_cmd)); |
7 | 6583 static void sign_list_defined __ARGS((sign_T *sp)); |
1828 | 6584 static void sign_undefine __ARGS((sign_T *sp, sign_T *sp_prev)); |
7 | 6585 |
1868 | 6586 static char *cmds[] = { |
6587 "define", | |
6588 #define SIGNCMD_DEFINE 0 | |
6589 "undefine", | |
6590 #define SIGNCMD_UNDEFINE 1 | |
6591 "list", | |
6592 #define SIGNCMD_LIST 2 | |
6593 "place", | |
6594 #define SIGNCMD_PLACE 3 | |
6595 "unplace", | |
6596 #define SIGNCMD_UNPLACE 4 | |
6597 "jump", | |
6598 #define SIGNCMD_JUMP 5 | |
6599 NULL | |
6600 #define SIGNCMD_LAST 6 | |
6601 }; | |
6602 | |
6603 /* | |
6604 * Find index of a ":sign" subcmd from its name. | |
6605 * "*end_cmd" must be writable. | |
6606 */ | |
6607 static int | |
6608 sign_cmd_idx(begin_cmd, end_cmd) | |
1874 | 6609 char_u *begin_cmd; /* begin of sign subcmd */ |
6610 char_u *end_cmd; /* just after sign subcmd */ | |
1868 | 6611 { |
6612 int idx; | |
6613 char save = *end_cmd; | |
6614 | |
6615 *end_cmd = NUL; | |
6616 for (idx = 0; ; ++idx) | |
6617 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0) | |
6618 break; | |
6619 *end_cmd = save; | |
6620 return idx; | |
6621 } | |
6622 | |
7 | 6623 /* |
6624 * ":sign" command | |
6625 */ | |
6626 void | |
6627 ex_sign(eap) | |
6628 exarg_T *eap; | |
6629 { | |
6630 char_u *arg = eap->arg; | |
6631 char_u *p; | |
6632 int idx; | |
6633 sign_T *sp; | |
6634 sign_T *sp_prev; | |
6635 buf_T *buf; | |
6636 | |
6637 /* Parse the subcommand. */ | |
6638 p = skiptowhite(arg); | |
1868 | 6639 idx = sign_cmd_idx(arg, p); |
6640 if (idx == SIGNCMD_LAST) | |
6641 { | |
6642 EMSG2(_("E160: Unknown sign command: %s"), arg); | |
6643 return; | |
7 | 6644 } |
6645 arg = skipwhite(p); | |
6646 | |
6647 if (idx <= SIGNCMD_LIST) | |
6648 { | |
6649 /* | |
6650 * Define, undefine or list signs. | |
6651 */ | |
6652 if (idx == SIGNCMD_LIST && *arg == NUL) | |
6653 { | |
6654 /* ":sign list": list all defined signs */ | |
6655 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
6656 sign_list_defined(sp); | |
6657 } | |
6658 else if (*arg == NUL) | |
6659 EMSG(_("E156: Missing sign name")); | |
6660 else | |
6661 { | |
2605 | 6662 /* Isolate the sign name. If it's a number skip leading zeroes, |
6663 * so that "099" and "99" are the same sign. But keep "0". */ | |
7 | 6664 p = skiptowhite(arg); |
6665 if (*p != NUL) | |
6666 *p++ = NUL; | |
2605 | 6667 while (arg[0] == '0' && arg[1] != NUL) |
6668 ++arg; | |
6669 | |
7 | 6670 sp_prev = NULL; |
6671 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
6672 { | |
6673 if (STRCMP(sp->sn_name, arg) == 0) | |
6674 break; | |
6675 sp_prev = sp; | |
6676 } | |
6677 if (idx == SIGNCMD_DEFINE) | |
6678 { | |
6679 /* ":sign define {name} ...": define a sign */ | |
6680 if (sp == NULL) | |
6681 { | |
2605 | 6682 sign_T *lp; |
6683 int start = next_sign_typenr; | |
6684 | |
7 | 6685 /* Allocate a new sign. */ |
6686 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T)); | |
6687 if (sp == NULL) | |
6688 return; | |
6689 | |
2605 | 6690 /* Check that next_sign_typenr is not already being used. |
6691 * This only happens after wrapping around. Hopefully | |
6692 * another one got deleted and we can use its number. */ | |
6693 for (lp = first_sign; lp != NULL; ) | |
7 | 6694 { |
2605 | 6695 if (lp->sn_typenr == next_sign_typenr) |
7 | 6696 { |
2605 | 6697 ++next_sign_typenr; |
6698 if (next_sign_typenr == MAX_TYPENR) | |
6699 next_sign_typenr = 1; | |
6700 if (next_sign_typenr == start) | |
7 | 6701 { |
2605 | 6702 vim_free(sp); |
6703 EMSG(_("E612: Too many signs defined")); | |
6704 return; | |
7 | 6705 } |
2605 | 6706 lp = first_sign; /* start all over */ |
6707 continue; | |
7 | 6708 } |
2605 | 6709 lp = lp->sn_next; |
6710 } | |
6711 | |
6712 sp->sn_typenr = next_sign_typenr; | |
6713 if (++next_sign_typenr == MAX_TYPENR) | |
6714 next_sign_typenr = 1; /* wrap around */ | |
6715 | |
6716 sp->sn_name = vim_strsave(arg); | |
6717 if (sp->sn_name == NULL) /* out of memory */ | |
6718 { | |
6719 vim_free(sp); | |
6720 return; | |
7 | 6721 } |
2601 | 6722 |
6723 /* add the new sign to the list of signs */ | |
6724 if (sp_prev == NULL) | |
6725 first_sign = sp; | |
6726 else | |
6727 sp_prev->sn_next = sp; | |
7 | 6728 } |
6729 | |
6730 /* set values for a defined sign. */ | |
6731 for (;;) | |
6732 { | |
6733 arg = skipwhite(p); | |
6734 if (*arg == NUL) | |
6735 break; | |
6736 p = skiptowhite_esc(arg); | |
6737 if (STRNCMP(arg, "icon=", 5) == 0) | |
6738 { | |
6739 arg += 5; | |
6740 vim_free(sp->sn_icon); | |
6741 sp->sn_icon = vim_strnsave(arg, (int)(p - arg)); | |
6742 backslash_halve(sp->sn_icon); | |
6743 #ifdef FEAT_SIGN_ICONS | |
6744 if (gui.in_use) | |
6745 { | |
6746 out_flush(); | |
6747 if (sp->sn_image != NULL) | |
6748 gui_mch_destroy_sign(sp->sn_image); | |
6749 sp->sn_image = gui_mch_register_sign(sp->sn_icon); | |
6750 } | |
6751 #endif | |
6752 } | |
6753 else if (STRNCMP(arg, "text=", 5) == 0) | |
6754 { | |
6755 char_u *s; | |
6756 int cells; | |
6757 int len; | |
6758 | |
6759 arg += 5; | |
6760 #ifdef FEAT_MBYTE | |
6761 /* Count cells and check for non-printable chars */ | |
6762 if (has_mbyte) | |
6763 { | |
6764 cells = 0; | |
474 | 6765 for (s = arg; s < p; s += (*mb_ptr2len)(s)) |
7 | 6766 { |
6767 if (!vim_isprintc((*mb_ptr2char)(s))) | |
6768 break; | |
6769 cells += (*mb_ptr2cells)(s); | |
6770 } | |
6771 } | |
6772 else | |
6773 #endif | |
6774 { | |
6775 for (s = arg; s < p; ++s) | |
6776 if (!vim_isprintc(*s)) | |
6777 break; | |
835 | 6778 cells = (int)(s - arg); |
7 | 6779 } |
6780 /* Currently must be one or two display cells */ | |
6781 if (s != p || cells < 1 || cells > 2) | |
6782 { | |
6783 *p = NUL; | |
6784 EMSG2(_("E239: Invalid sign text: %s"), arg); | |
6785 return; | |
6786 } | |
6787 | |
6788 vim_free(sp->sn_text); | |
6789 /* Allocate one byte more if we need to pad up | |
6790 * with a space. */ | |
835 | 6791 len = (int)(p - arg + ((cells == 1) ? 1 : 0)); |
7 | 6792 sp->sn_text = vim_strnsave(arg, len); |
6793 | |
6794 if (sp->sn_text != NULL && cells == 1) | |
6795 STRCPY(sp->sn_text + len - 1, " "); | |
6796 } | |
6797 else if (STRNCMP(arg, "linehl=", 7) == 0) | |
6798 { | |
6799 arg += 7; | |
6800 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg)); | |
6801 } | |
6802 else if (STRNCMP(arg, "texthl=", 7) == 0) | |
6803 { | |
6804 arg += 7; | |
6805 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg)); | |
6806 } | |
6807 else | |
6808 { | |
6809 EMSG2(_(e_invarg2), arg); | |
6810 return; | |
6811 } | |
6812 } | |
6813 } | |
6814 else if (sp == NULL) | |
6815 EMSG2(_("E155: Unknown sign: %s"), arg); | |
6816 else if (idx == SIGNCMD_LIST) | |
6817 /* ":sign list {name}" */ | |
6818 sign_list_defined(sp); | |
6819 else | |
6820 /* ":sign undefine {name}" */ | |
1828 | 6821 sign_undefine(sp, sp_prev); |
7 | 6822 } |
6823 } | |
6824 else | |
6825 { | |
6826 int id = -1; | |
6827 linenr_T lnum = -1; | |
6828 char_u *sign_name = NULL; | |
6829 char_u *arg1; | |
6830 | |
6831 if (*arg == NUL) | |
6832 { | |
6833 if (idx == SIGNCMD_PLACE) | |
6834 { | |
6835 /* ":sign place": list placed signs in all buffers */ | |
6836 sign_list_placed(NULL); | |
6837 } | |
6838 else if (idx == SIGNCMD_UNPLACE) | |
6839 { | |
6840 /* ":sign unplace": remove placed sign at cursor */ | |
6841 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum); | |
6842 if (id > 0) | |
6843 { | |
6844 buf_delsign(curwin->w_buffer, id); | |
6845 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum); | |
6846 } | |
6847 else | |
6848 EMSG(_("E159: Missing sign number")); | |
6849 } | |
6850 else | |
6851 EMSG(_(e_argreq)); | |
6852 return; | |
6853 } | |
6854 | |
6855 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL) | |
6856 { | |
6857 /* ":sign unplace *": remove all placed signs */ | |
6858 buf_delete_all_signs(); | |
6859 return; | |
6860 } | |
6861 | |
6862 /* first arg could be placed sign id */ | |
6863 arg1 = arg; | |
6864 if (VIM_ISDIGIT(*arg)) | |
6865 { | |
6866 id = getdigits(&arg); | |
6867 if (!vim_iswhite(*arg) && *arg != NUL) | |
6868 { | |
6869 id = -1; | |
6870 arg = arg1; | |
6871 } | |
6872 else | |
6873 { | |
6874 arg = skipwhite(arg); | |
6875 if (idx == SIGNCMD_UNPLACE && *arg == NUL) | |
6876 { | |
6877 /* ":sign unplace {id}": remove placed sign by number */ | |
6878 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
6879 if ((lnum = buf_delsign(buf, id)) != 0) | |
6880 update_debug_sign(buf, lnum); | |
6881 return; | |
6882 } | |
6883 } | |
6884 } | |
6885 | |
6886 /* | |
6887 * Check for line={lnum} name={name} and file={fname} or buffer={nr}. | |
6888 * Leave "arg" pointing to {fname}. | |
6889 */ | |
6890 for (;;) | |
6891 { | |
6892 if (STRNCMP(arg, "line=", 5) == 0) | |
6893 { | |
6894 arg += 5; | |
6895 lnum = atoi((char *)arg); | |
6896 arg = skiptowhite(arg); | |
6897 } | |
6898 else if (STRNCMP(arg, "name=", 5) == 0) | |
6899 { | |
6900 arg += 5; | |
6901 sign_name = arg; | |
6902 arg = skiptowhite(arg); | |
6903 if (*arg != NUL) | |
6904 *arg++ = NUL; | |
2605 | 6905 while (sign_name[0] == '0' && sign_name[1] != NUL) |
6906 ++sign_name; | |
7 | 6907 } |
6908 else if (STRNCMP(arg, "file=", 5) == 0) | |
6909 { | |
6910 arg += 5; | |
6911 buf = buflist_findname(arg); | |
6912 break; | |
6913 } | |
6914 else if (STRNCMP(arg, "buffer=", 7) == 0) | |
6915 { | |
6916 arg += 7; | |
6917 buf = buflist_findnr((int)getdigits(&arg)); | |
6918 if (*skipwhite(arg) != NUL) | |
6919 EMSG(_(e_trailing)); | |
6920 break; | |
6921 } | |
6922 else | |
6923 { | |
6924 EMSG(_(e_invarg)); | |
6925 return; | |
6926 } | |
6927 arg = skipwhite(arg); | |
6928 } | |
6929 | |
6930 if (buf == NULL) | |
6931 { | |
6932 EMSG2(_("E158: Invalid buffer name: %s"), arg); | |
6933 } | |
6934 else if (id <= 0) | |
6935 { | |
6936 if (lnum >= 0 || sign_name != NULL) | |
6937 EMSG(_(e_invarg)); | |
6938 else | |
6939 /* ":sign place file={fname}": list placed signs in one file */ | |
6940 sign_list_placed(buf); | |
6941 } | |
6942 else if (idx == SIGNCMD_JUMP) | |
6943 { | |
6944 /* ":sign jump {id} file={fname}" */ | |
6945 if (lnum >= 0 || sign_name != NULL) | |
6946 EMSG(_(e_invarg)); | |
6947 else if ((lnum = buf_findsign(buf, id)) > 0) | |
6948 { /* goto a sign ... */ | |
6949 if (buf_jump_open_win(buf) != NULL) | |
6950 { /* ... in a current window */ | |
6951 curwin->w_cursor.lnum = lnum; | |
6952 check_cursor_lnum(); | |
6953 beginline(BL_WHITE); | |
6954 } | |
6955 else | |
6956 { /* ... not currently in a window */ | |
6957 char_u *cmd; | |
6958 | |
6959 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25); | |
6960 if (cmd == NULL) | |
6961 return; | |
6962 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname); | |
6963 do_cmdline_cmd(cmd); | |
6964 vim_free(cmd); | |
6965 } | |
6966 #ifdef FEAT_FOLDING | |
6967 foldOpenCursor(); | |
6968 #endif | |
6969 } | |
6970 else | |
6971 EMSGN(_("E157: Invalid sign ID: %ld"), id); | |
6972 } | |
6973 else if (idx == SIGNCMD_UNPLACE) | |
6974 { | |
6975 /* ":sign unplace {id} file={fname}" */ | |
6976 if (lnum >= 0 || sign_name != NULL) | |
6977 EMSG(_(e_invarg)); | |
6978 else | |
6979 { | |
6980 lnum = buf_delsign(buf, id); | |
6981 update_debug_sign(buf, lnum); | |
6982 } | |
6983 } | |
6984 /* idx == SIGNCMD_PLACE */ | |
6985 else if (sign_name != NULL) | |
6986 { | |
6987 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
6988 if (STRCMP(sp->sn_name, sign_name) == 0) | |
6989 break; | |
6990 if (sp == NULL) | |
6991 { | |
6992 EMSG2(_("E155: Unknown sign: %s"), sign_name); | |
6993 return; | |
6994 } | |
6995 if (lnum > 0) | |
6996 /* ":sign place {id} line={lnum} name={name} file={fname}": | |
6997 * place a sign */ | |
6998 buf_addsign(buf, id, lnum, sp->sn_typenr); | |
6999 else | |
7000 /* ":sign place {id} file={fname}": change sign type */ | |
7001 lnum = buf_change_sign_type(buf, id, sp->sn_typenr); | |
7002 update_debug_sign(buf, lnum); | |
7003 } | |
7004 else | |
7005 EMSG(_(e_invarg)); | |
7006 } | |
7007 } | |
7008 | |
7009 #if defined(FEAT_SIGN_ICONS) || defined(PROTO) | |
7010 /* | |
7011 * Allocate the icons. Called when the GUI has started. Allows defining | |
7012 * signs before it starts. | |
7013 */ | |
7014 void | |
7015 sign_gui_started() | |
7016 { | |
7017 sign_T *sp; | |
7018 | |
7019 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7020 if (sp->sn_icon != NULL) | |
7021 sp->sn_image = gui_mch_register_sign(sp->sn_icon); | |
7022 } | |
7023 #endif | |
7024 | |
7025 /* | |
7026 * List one sign. | |
7027 */ | |
7028 static void | |
7029 sign_list_defined(sp) | |
7030 sign_T *sp; | |
7031 { | |
7032 char_u *p; | |
7033 | |
274 | 7034 smsg((char_u *)"sign %s", sp->sn_name); |
7 | 7035 if (sp->sn_icon != NULL) |
7036 { | |
7037 MSG_PUTS(" icon="); | |
7038 msg_outtrans(sp->sn_icon); | |
7039 #ifdef FEAT_SIGN_ICONS | |
7040 if (sp->sn_image == NULL) | |
7041 MSG_PUTS(_(" (NOT FOUND)")); | |
7042 #else | |
7043 MSG_PUTS(_(" (not supported)")); | |
7044 #endif | |
7045 } | |
7046 if (sp->sn_text != NULL) | |
7047 { | |
7048 MSG_PUTS(" text="); | |
7049 msg_outtrans(sp->sn_text); | |
7050 } | |
7051 if (sp->sn_line_hl > 0) | |
7052 { | |
7053 MSG_PUTS(" linehl="); | |
7054 p = get_highlight_name(NULL, sp->sn_line_hl - 1); | |
7055 if (p == NULL) | |
7056 MSG_PUTS("NONE"); | |
7057 else | |
7058 msg_puts(p); | |
7059 } | |
7060 if (sp->sn_text_hl > 0) | |
7061 { | |
7062 MSG_PUTS(" texthl="); | |
7063 p = get_highlight_name(NULL, sp->sn_text_hl - 1); | |
7064 if (p == NULL) | |
7065 MSG_PUTS("NONE"); | |
7066 else | |
7067 msg_puts(p); | |
7068 } | |
7069 } | |
7070 | |
7071 /* | |
1828 | 7072 * Undefine a sign and free its memory. |
7073 */ | |
7074 static void | |
7075 sign_undefine(sp, sp_prev) | |
7076 sign_T *sp; | |
7077 sign_T *sp_prev; | |
7078 { | |
7079 vim_free(sp->sn_name); | |
7080 vim_free(sp->sn_icon); | |
7081 #ifdef FEAT_SIGN_ICONS | |
7082 if (sp->sn_image != NULL) | |
7083 { | |
7084 out_flush(); | |
7085 gui_mch_destroy_sign(sp->sn_image); | |
7086 } | |
7087 #endif | |
7088 vim_free(sp->sn_text); | |
7089 if (sp_prev == NULL) | |
7090 first_sign = sp->sn_next; | |
7091 else | |
7092 sp_prev->sn_next = sp->sn_next; | |
7093 vim_free(sp); | |
7094 } | |
7095 | |
7096 /* | |
7 | 7097 * Get highlighting attribute for sign "typenr". |
7098 * If "line" is TRUE: line highl, if FALSE: text highl. | |
7099 */ | |
7100 int | |
7101 sign_get_attr(typenr, line) | |
7102 int typenr; | |
7103 int line; | |
7104 { | |
7105 sign_T *sp; | |
7106 | |
7107 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7108 if (sp->sn_typenr == typenr) | |
7109 { | |
7110 if (line) | |
7111 { | |
7112 if (sp->sn_line_hl > 0) | |
7113 return syn_id2attr(sp->sn_line_hl); | |
7114 } | |
7115 else | |
7116 { | |
7117 if (sp->sn_text_hl > 0) | |
7118 return syn_id2attr(sp->sn_text_hl); | |
7119 } | |
7120 break; | |
7121 } | |
7122 return 0; | |
7123 } | |
7124 | |
7125 /* | |
7126 * Get text mark for sign "typenr". | |
7127 * Returns NULL if there isn't one. | |
7128 */ | |
7129 char_u * | |
7130 sign_get_text(typenr) | |
7131 int typenr; | |
7132 { | |
7133 sign_T *sp; | |
7134 | |
7135 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7136 if (sp->sn_typenr == typenr) | |
7137 return sp->sn_text; | |
7138 return NULL; | |
7139 } | |
7140 | |
7141 #if defined(FEAT_SIGN_ICONS) || defined(PROTO) | |
7142 void * | |
7143 sign_get_image(typenr) | |
7144 int typenr; /* the attribute which may have a sign */ | |
7145 { | |
7146 sign_T *sp; | |
7147 | |
7148 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7149 if (sp->sn_typenr == typenr) | |
7150 return sp->sn_image; | |
7151 return NULL; | |
7152 } | |
7153 #endif | |
7154 | |
7155 /* | |
7156 * Get the name of a sign by its typenr. | |
7157 */ | |
7158 char_u * | |
7159 sign_typenr2name(typenr) | |
7160 int typenr; | |
7161 { | |
7162 sign_T *sp; | |
7163 | |
7164 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7165 if (sp->sn_typenr == typenr) | |
7166 return sp->sn_name; | |
7167 return (char_u *)_("[Deleted]"); | |
7168 } | |
7169 | |
1828 | 7170 #if defined(EXITFREE) || defined(PROTO) |
7171 /* | |
7172 * Undefine/free all signs. | |
7173 */ | |
7174 void | |
7175 free_signs() | |
7176 { | |
7177 while (first_sign != NULL) | |
7178 sign_undefine(first_sign, NULL); | |
7179 } | |
7180 #endif | |
7181 | |
1868 | 7182 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
7183 static enum | |
7184 { | |
7185 EXP_SUBCMD, /* expand :sign sub-commands */ | |
7186 EXP_DEFINE, /* expand :sign define {name} args */ | |
7187 EXP_PLACE, /* expand :sign place {id} args */ | |
7188 EXP_UNPLACE, /* expand :sign unplace" */ | |
7189 EXP_SIGN_NAMES /* expand with name of placed signs */ | |
7190 } expand_what; | |
7191 | |
7192 /* | |
7193 * Function given to ExpandGeneric() to obtain the sign command | |
7194 * expansion. | |
7195 */ | |
7196 char_u * | |
7197 get_sign_name(xp, idx) | |
1876 | 7198 expand_T *xp UNUSED; |
1868 | 7199 int idx; |
7200 { | |
7201 sign_T *sp; | |
7202 int current_idx; | |
7203 | |
7204 switch (expand_what) | |
7205 { | |
7206 case EXP_SUBCMD: | |
7207 return (char_u *)cmds[idx]; | |
7208 case EXP_DEFINE: | |
7209 { | |
7210 char *define_arg[] = | |
7211 { | |
7212 "icon=", "linehl=", "text=", "texthl=", NULL | |
7213 }; | |
7214 return (char_u *)define_arg[idx]; | |
7215 } | |
7216 case EXP_PLACE: | |
7217 { | |
7218 char *place_arg[] = | |
7219 { | |
7220 "line=", "name=", "file=", "buffer=", NULL | |
7221 }; | |
7222 return (char_u *)place_arg[idx]; | |
7223 } | |
7224 case EXP_UNPLACE: | |
7225 { | |
7226 char *unplace_arg[] = { "file=", "buffer=", NULL }; | |
7227 return (char_u *)unplace_arg[idx]; | |
7228 } | |
7229 case EXP_SIGN_NAMES: | |
7230 /* Complete with name of signs already defined */ | |
7231 current_idx = 0; | |
7232 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7233 if (current_idx++ == idx) | |
7234 return sp->sn_name; | |
7235 return NULL; | |
7236 default: | |
7237 return NULL; | |
7238 } | |
7239 } | |
7240 | |
7241 /* | |
7242 * Handle command line completion for :sign command. | |
7243 */ | |
7244 void | |
7245 set_context_in_sign_cmd(xp, arg) | |
7246 expand_T *xp; | |
7247 char_u *arg; | |
7248 { | |
7249 char_u *p; | |
7250 char_u *end_subcmd; | |
7251 char_u *last; | |
7252 int cmd_idx; | |
7253 char_u *begin_subcmd_args; | |
7254 | |
7255 /* Default: expand subcommands. */ | |
7256 xp->xp_context = EXPAND_SIGN; | |
7257 expand_what = EXP_SUBCMD; | |
7258 xp->xp_pattern = arg; | |
7259 | |
7260 end_subcmd = skiptowhite(arg); | |
7261 if (*end_subcmd == NUL) | |
7262 /* expand subcmd name | |
7263 * :sign {subcmd}<CTRL-D>*/ | |
7264 return; | |
7265 | |
7266 cmd_idx = sign_cmd_idx(arg, end_subcmd); | |
7267 | |
7268 /* :sign {subcmd} {subcmd_args} | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7269 * | |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7270 * begin_subcmd_args */ |
1868 | 7271 begin_subcmd_args = skipwhite(end_subcmd); |
7272 p = skiptowhite(begin_subcmd_args); | |
7273 if (*p == NUL) | |
7274 { | |
7275 /* | |
7276 * Expand first argument of subcmd when possible. | |
7277 * For ":jump {id}" and ":unplace {id}", we could | |
7278 * possibly expand the ids of all signs already placed. | |
7279 */ | |
7280 xp->xp_pattern = begin_subcmd_args; | |
7281 switch (cmd_idx) | |
7282 { | |
7283 case SIGNCMD_LIST: | |
7284 case SIGNCMD_UNDEFINE: | |
7285 /* :sign list <CTRL-D> | |
7286 * :sign undefine <CTRL-D> */ | |
7287 expand_what = EXP_SIGN_NAMES; | |
7288 break; | |
7289 default: | |
7290 xp->xp_context = EXPAND_NOTHING; | |
7291 } | |
7292 return; | |
7293 } | |
7294 | |
7295 /* expand last argument of subcmd */ | |
7296 | |
7297 /* :sign define {name} {args}... | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7298 * | |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7299 * p */ |
1868 | 7300 |
7301 /* Loop until reaching last argument. */ | |
7302 do | |
7303 { | |
7304 p = skipwhite(p); | |
7305 last = p; | |
7306 p = skiptowhite(p); | |
7307 } while (*p != NUL); | |
7308 | |
7309 p = vim_strchr(last, '='); | |
7310 | |
7311 /* :sign define {name} {args}... {last}= | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7312 * | | |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7313 * last p */ |
1868 | 7314 if (p == NUL) |
7315 { | |
7316 /* Expand last argument name (before equal sign). */ | |
7317 xp->xp_pattern = last; | |
7318 switch (cmd_idx) | |
7319 { | |
7320 case SIGNCMD_DEFINE: | |
7321 expand_what = EXP_DEFINE; | |
7322 break; | |
7323 case SIGNCMD_PLACE: | |
7324 expand_what = EXP_PLACE; | |
7325 break; | |
7326 case SIGNCMD_JUMP: | |
7327 case SIGNCMD_UNPLACE: | |
7328 expand_what = EXP_UNPLACE; | |
7329 break; | |
7330 default: | |
7331 xp->xp_context = EXPAND_NOTHING; | |
7332 } | |
7333 } | |
7334 else | |
7335 { | |
7336 /* Expand last argument value (after equal sign). */ | |
7337 xp->xp_pattern = p + 1; | |
7338 switch (cmd_idx) | |
7339 { | |
7340 case SIGNCMD_DEFINE: | |
7341 if (STRNCMP(last, "texthl", p - last) == 0 || | |
7342 STRNCMP(last, "linehl", p - last) == 0) | |
7343 xp->xp_context = EXPAND_HIGHLIGHT; | |
7344 else if (STRNCMP(last, "icon", p - last) == 0) | |
7345 xp->xp_context = EXPAND_FILES; | |
7346 else | |
7347 xp->xp_context = EXPAND_NOTHING; | |
7348 break; | |
7349 case SIGNCMD_PLACE: | |
7350 if (STRNCMP(last, "name", p - last) == 0) | |
7351 expand_what = EXP_SIGN_NAMES; | |
7352 else | |
7353 xp->xp_context = EXPAND_NOTHING; | |
7354 break; | |
7355 default: | |
7356 xp->xp_context = EXPAND_NOTHING; | |
7357 } | |
7358 } | |
7359 } | |
7360 #endif | |
7 | 7361 #endif |
7362 | |
7363 #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO) | |
7364 /* | |
7365 * ":drop" | |
7366 * Opens the first argument in a window. When there are two or more arguments | |
7367 * the argument list is redefined. | |
7368 */ | |
7369 void | |
7370 ex_drop(eap) | |
7371 exarg_T *eap; | |
7372 { | |
7373 int split = FALSE; | |
7374 win_T *wp; | |
7375 buf_T *buf; | |
735 | 7376 # ifdef FEAT_WINDOWS |
7377 tabpage_T *tp; | |
7378 # endif | |
7 | 7379 |
7380 /* | |
7381 * Check if the first argument is already being edited in a window. If | |
7382 * so, jump to that window. | |
7383 * We would actually need to check all arguments, but that's complicated | |
7384 * and mostly only one file is dropped. | |
7385 * This also ignores wildcards, since it is very unlikely the user is | |
7386 * editing a file name with a wildcard character. | |
7387 */ | |
735 | 7388 set_arglist(eap->arg); |
7389 | |
1067 | 7390 /* |
7391 * Expanding wildcards may result in an empty argument list. E.g. when | |
7392 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we | |
7393 * already did an error message for this. | |
7394 */ | |
7395 if (ARGCOUNT == 0) | |
7396 return; | |
7397 | |
7 | 7398 # ifdef FEAT_WINDOWS |
735 | 7399 if (cmdmod.tab) |
7400 { | |
7401 /* ":tab drop file ...": open a tab for each argument that isn't | |
7402 * edited in a window yet. It's like ":tab all" but without closing | |
7403 * windows or tabs. */ | |
7404 ex_all(eap); | |
7405 } | |
7406 else | |
7 | 7407 # endif |
735 | 7408 { |
7409 /* ":drop file ...": Edit the first argument. Jump to an existing | |
7410 * window if possible, edit in current window if the current buffer | |
7411 * can be abandoned, otherwise open a new window. */ | |
7412 buf = buflist_findnr(ARGLIST[0].ae_fnum); | |
7413 | |
7414 FOR_ALL_TAB_WINDOWS(tp, wp) | |
7415 { | |
7416 if (wp->w_buffer == buf) | |
7 | 7417 { |
735 | 7418 # ifdef FEAT_WINDOWS |
825 | 7419 goto_tabpage_win(tp, wp); |
735 | 7420 # endif |
7 | 7421 curwin->w_arg_idx = 0; |
7422 return; | |
7423 } | |
7424 } | |
735 | 7425 |
7426 /* | |
7427 * Check whether the current buffer is changed. If so, we will need | |
7428 * to split the current window or data could be lost. | |
7429 * Skip the check if the 'hidden' option is set, as in this case the | |
7430 * buffer won't be lost. | |
7431 */ | |
7432 if (!P_HID(curbuf)) | |
7433 { | |
7 | 7434 # ifdef FEAT_WINDOWS |
735 | 7435 ++emsg_off; |
7 | 7436 # endif |
735 | 7437 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE); |
7 | 7438 # ifdef FEAT_WINDOWS |
735 | 7439 --emsg_off; |
7 | 7440 # else |
735 | 7441 if (split) |
7442 return; | |
7 | 7443 # endif |
735 | 7444 } |
7445 | |
7446 /* Fake a ":sfirst" or ":first" command edit the first argument. */ | |
7447 if (split) | |
7448 { | |
7449 eap->cmdidx = CMD_sfirst; | |
7450 eap->cmd[0] = 's'; | |
7451 } | |
7452 else | |
7453 eap->cmdidx = CMD_first; | |
7454 ex_rewind(eap); | |
7455 } | |
7 | 7456 } |
7457 #endif |