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