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