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