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