blob: 2118f2691ffcd6621657f3be42c34ea9af595cac [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
Bram Moolenaar342337a2005-07-21 21:11:17 +00002920 if (sfname != NULL)
2921 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#endif
2923
2924#ifdef FEAT_LISTCMDS
2925 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
2926 goto theend;
2927#endif
2928
2929 if (ffname == NULL)
2930 other_file = TRUE;
2931 /* there is no file name */
2932 else if (*ffname == NUL && curbuf->b_ffname == NULL)
2933 other_file = FALSE;
2934 else
2935 {
2936 if (*ffname == NUL) /* re-edit with same file name */
2937 {
2938 ffname = curbuf->b_ffname;
2939 sfname = curbuf->b_fname;
2940 }
2941 free_fname = fix_fname(ffname); /* may expand to full path name */
2942 if (free_fname != NULL)
2943 ffname = free_fname;
2944 other_file = otherfile(ffname);
2945#ifdef FEAT_SUN_WORKSHOP
2946 if (usingSunWorkShop && p_acd
2947 && (cp = vim_strrchr(sfname, '/')) != NULL)
2948 sfname = ++cp;
2949#endif
2950 }
2951 }
2952
2953 /*
2954 * if the file was changed we may not be allowed to abandon it
2955 * - if we are going to re-edit the same file
2956 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
2957 */
2958 if ( ((!other_file && !(flags & ECMD_OLDBUF))
2959 || (curbuf->b_nwindows == 1
2960 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
2961 && check_changed(curbuf, p_awa, !other_file,
2962 (flags & ECMD_FORCEIT), FALSE))
2963 {
2964 if (fnum == 0 && other_file && ffname != NULL)
2965 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
2966 goto theend;
2967 }
2968
2969#ifdef FEAT_VISUAL
2970 /*
2971 * End Visual mode before switching to another buffer, so the text can be
2972 * copied into the GUI selection buffer.
2973 */
2974 reset_VIsual();
2975#endif
2976
2977 /*
2978 * If we are starting to edit another file, open a (new) buffer.
2979 * Otherwise we re-use the current buffer.
2980 */
2981 if (other_file)
2982 {
2983#ifdef FEAT_LISTCMDS
2984 if (!(flags & ECMD_ADDBUF))
2985#endif
2986 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002987 if (!cmdmod.keepalt)
2988 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 buflist_altfpos();
2990 }
2991
2992 if (fnum)
2993 buf = buflist_findnr(fnum);
2994 else
2995 {
2996#ifdef FEAT_LISTCMDS
2997 if (flags & ECMD_ADDBUF)
2998 {
2999 linenr_T tlnum = 1L;
3000
3001 if (command != NULL)
3002 {
3003 tlnum = atol((char *)command);
3004 if (tlnum <= 0)
3005 tlnum = 1L;
3006 }
3007 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3008 goto theend;
3009 }
3010#endif
3011 buf = buflist_new(ffname, sfname, 0L,
3012 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
3013 }
3014 if (buf == NULL)
3015 goto theend;
3016 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3017 {
3018 oldbuf = FALSE;
3019 buf->b_nwindows = 0;
3020 }
3021 else /* existing memfile */
3022 {
3023 oldbuf = TRUE;
3024 (void)buf_check_timestamp(buf, FALSE);
3025 /* Check if autocommands made buffer invalid or changed the current
3026 * buffer. */
3027 if (!buf_valid(buf)
3028#ifdef FEAT_AUTOCMD
3029 || curbuf != old_curbuf
3030#endif
3031 )
3032 goto theend;
3033#ifdef FEAT_EVAL
3034 if (aborting()) /* autocmds may abort script processing */
3035 goto theend;
3036#endif
3037 }
3038
3039 /* May jump to last used line number for a loaded buffer or when asked
3040 * for explicitly */
3041 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3042 {
3043 pos = buflist_findfpos(buf);
3044 newlnum = pos->lnum;
3045 solcol = pos->col;
3046 }
3047
3048 /*
3049 * Make the (new) buffer the one used by the current window.
3050 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3051 * If the current buffer was empty and has no file name, curbuf
3052 * is returned by buflist_new().
3053 */
3054 if (buf != curbuf)
3055 {
3056#ifdef FEAT_AUTOCMD
3057 /*
3058 * Be careful: The autocommands may delete any buffer and change
3059 * the current buffer.
3060 * - If the buffer we are going to edit is deleted, give up.
3061 * - If the current buffer is deleted, prefer to load the new
3062 * buffer when loading a buffer is required. This avoids
3063 * loading another buffer which then must be closed again.
3064 * - If we ended up in the new buffer already, need to skip a few
3065 * things, set auto_buf.
3066 */
3067 if (buf->b_fname != NULL)
3068 new_name = vim_strsave(buf->b_fname);
3069 au_new_curbuf = buf;
3070 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3071 if (!buf_valid(buf)) /* new buffer has been deleted */
3072 {
3073 delbuf_msg(new_name); /* frees new_name */
3074 goto theend;
3075 }
3076# ifdef FEAT_EVAL
3077 if (aborting()) /* autocmds may abort script processing */
3078 {
3079 vim_free(new_name);
3080 goto theend;
3081 }
3082# endif
3083 if (buf == curbuf) /* already in new buffer */
3084 auto_buf = TRUE;
3085 else
3086 {
3087 if (curbuf == old_curbuf)
3088#endif
3089 buf_copy_options(buf, BCO_ENTER);
3090
3091 /* close the link to the current buffer */
3092 close_buffer(curwin, curbuf,
3093 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD);
3094
3095#ifdef FEAT_AUTOCMD
3096# ifdef FEAT_EVAL
3097 if (aborting()) /* autocmds may abort script processing */
3098 {
3099 vim_free(new_name);
3100 goto theend;
3101 }
3102# endif
3103 /* Be careful again, like above. */
3104 if (!buf_valid(buf)) /* new buffer has been deleted */
3105 {
3106 delbuf_msg(new_name); /* frees new_name */
3107 goto theend;
3108 }
3109 if (buf == curbuf) /* already in new buffer */
3110 auto_buf = TRUE;
3111 else
3112#endif
3113 {
3114 curwin->w_buffer = buf;
3115 curbuf = buf;
3116 ++curbuf->b_nwindows;
3117 /* set 'fileformat' */
3118 if (*p_ffs && !oldbuf)
3119 set_fileformat(default_fileformat(), OPT_LOCAL);
3120 }
3121
3122 /* May get the window options from the last time this buffer
3123 * was in this window (or another window). If not used
3124 * before, reset the local window options to the global
3125 * values. Also restores old folding stuff. */
3126 get_winopts(buf);
3127
3128#ifdef FEAT_AUTOCMD
3129 }
3130 vim_free(new_name);
3131 au_new_curbuf = NULL;
3132#endif
3133 }
3134 else
3135 ++curbuf->b_nwindows;
3136
3137 curwin->w_pcmark.lnum = 1;
3138 curwin->w_pcmark.col = 0;
3139 }
3140 else /* !other_file */
3141 {
3142 if (
3143#ifdef FEAT_LISTCMDS
3144 (flags & ECMD_ADDBUF) ||
3145#endif
3146 check_fname() == FAIL)
3147 goto theend;
3148 oldbuf = (flags & ECMD_OLDBUF);
3149 }
3150
3151 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3152 {
3153 char_u *p;
3154
3155 curbuf->b_help = TRUE;
3156#ifdef FEAT_QUICKFIX
3157 set_string_option_direct((char_u *)"buftype", -1,
3158 (char_u *)"help", OPT_FREE|OPT_LOCAL);
3159#endif
3160
3161 /*
3162 * Always set these options after jumping to a help tag, because the
3163 * user may have an autocommand that gets in the way.
3164 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
3165 * latin1 word characters (for translated help files).
3166 * Only set it when needed, buf_init_chartab() is some work.
3167 */
3168 p =
3169#ifdef EBCDIC
3170 (char_u *)"65-255,^*,^|,^\"";
3171#else
3172 (char_u *)"!-~,^*,^|,^\",192-255";
3173#endif
3174 if (STRCMP(curbuf->b_p_isk, p) != 0)
3175 {
3176 set_string_option_direct((char_u *)"isk", -1, p,
3177 OPT_FREE|OPT_LOCAL);
3178 check_buf_options(curbuf);
3179 (void)buf_init_chartab(curbuf, FALSE);
3180 }
3181
3182 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
3183 curwin->w_p_list = FALSE; /* no list mode */
3184
3185 curbuf->b_p_ma = FALSE; /* not modifiable */
3186 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
3187 curwin->w_p_nu = 0; /* no line numbers */
3188#ifdef FEAT_SCROLLBIND
3189 curwin->w_p_scb = FALSE; /* no scroll binding */
3190#endif
3191#ifdef FEAT_ARABIC
3192 curwin->w_p_arab = FALSE; /* no arabic mode */
3193#endif
3194#ifdef FEAT_RIGHTLEFT
3195 curwin->w_p_rl = FALSE; /* help window is left-to-right */
3196#endif
3197#ifdef FEAT_FOLDING
3198 curwin->w_p_fen = FALSE; /* No folding in the help window */
3199#endif
3200#ifdef FEAT_DIFF
3201 curwin->w_p_diff = FALSE; /* No 'diff' */
3202#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003203#ifdef FEAT_SYN_HL
3204 curwin->w_p_spell = FALSE; /* No spell checking */
3205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207#ifdef FEAT_AUTOCMD
3208 buf = curbuf;
3209#endif
3210 set_buflisted(FALSE);
3211 }
3212 else
3213 {
3214#ifdef FEAT_AUTOCMD
3215 buf = curbuf;
3216#endif
3217 /* Don't make a buffer listed if it's a help buffer. Useful when
3218 * using CTRL-O to go back to a help file. */
3219 if (!curbuf->b_help)
3220 set_buflisted(TRUE);
3221 }
3222
3223#ifdef FEAT_AUTOCMD
3224 /* If autocommands change buffers under our fingers, forget about
3225 * editing the file. */
3226 if (buf != curbuf)
3227 goto theend;
3228# ifdef FEAT_EVAL
3229 if (aborting()) /* autocmds may abort script processing */
3230 goto theend;
3231# endif
3232
3233 /* Since we are starting to edit a file, consider the filetype to be
3234 * unset. Helps for when an autocommand changes files and expects syntax
3235 * highlighting to work in the other file. */
3236 did_filetype = FALSE;
3237#endif
3238
3239/*
3240 * other_file oldbuf
3241 * FALSE FALSE re-edit same file, buffer is re-used
3242 * FALSE TRUE re-edit same file, nothing changes
3243 * TRUE FALSE start editing new file, new buffer
3244 * TRUE TRUE start editing in existing buffer (nothing to do)
3245 */
3246 if (!other_file && !oldbuf) /* re-use the buffer */
3247 {
3248 set_last_cursor(curwin); /* may set b_last_cursor */
3249 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3250 {
3251 newlnum = curwin->w_cursor.lnum;
3252 solcol = curwin->w_cursor.col;
3253 }
3254#ifdef FEAT_AUTOCMD
3255 buf = curbuf;
3256 if (buf->b_fname != NULL)
3257 new_name = vim_strsave(buf->b_fname);
3258 else
3259 new_name = NULL;
3260#endif
3261 buf_freeall(curbuf, FALSE, FALSE); /* free all things for buffer */
3262#ifdef FEAT_AUTOCMD
3263 /* If autocommands deleted the buffer we were going to re-edit, give
3264 * up and jump to the end. */
3265 if (!buf_valid(buf))
3266 {
3267 delbuf_msg(new_name); /* frees new_name */
3268 goto theend;
3269 }
3270 vim_free(new_name);
3271
3272 /* If autocommands change buffers under our fingers, forget about
3273 * re-editing the file. Should do the buf_clear_file(), but perhaps
3274 * the autocommands changed the buffer... */
3275 if (buf != curbuf)
3276 goto theend;
3277# ifdef FEAT_EVAL
3278 if (aborting()) /* autocmds may abort script processing */
3279 goto theend;
3280# endif
3281#endif
3282 buf_clear_file(curbuf);
3283 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3284 curbuf->b_op_end.lnum = 0;
3285 }
3286
3287/*
3288 * If we get here we are sure to start editing
3289 */
3290 /* don't redraw until the cursor is in the right line */
3291 ++RedrawingDisabled;
3292
3293 /* Assume success now */
3294 retval = OK;
3295
3296 /*
3297 * Reset cursor position, could be used by autocommands.
3298 */
3299 check_cursor();
3300
3301 /*
3302 * Check if we are editing the w_arg_idx file in the argument list.
3303 */
3304 check_arg_idx(curwin);
3305
3306#ifdef FEAT_AUTOCMD
3307 if (!auto_buf)
3308#endif
3309 {
3310 /*
3311 * Set cursor and init window before reading the file and executing
3312 * autocommands. This allows for the autocommands to position the
3313 * cursor.
3314 */
3315 win_init(curwin);
3316
3317#ifdef FEAT_FOLDING
3318 /* It's like all lines in the buffer changed. Need to update
3319 * automatic folding. */
3320 foldUpdateAll(curwin);
3321#endif
3322
3323#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
3324 if (p_acd && curbuf->b_ffname != NULL
3325 && vim_chdirfile(curbuf->b_ffname) == OK)
3326 shorten_fnames(TRUE);
3327#endif
3328 /*
3329 * Careful: open_buffer() and apply_autocmds() may change the current
3330 * buffer and window.
3331 */
3332 lnum = curwin->w_cursor.lnum;
3333 topline = curwin->w_topline;
3334 if (!oldbuf) /* need to read the file */
3335 {
3336#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3337 swap_exists_action = SEA_DIALOG;
3338#endif
3339 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3340
3341 /*
3342 * Open the buffer and read the file.
3343 */
3344#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3345 if (should_abort(open_buffer(FALSE, eap)))
3346 retval = FAIL;
3347#else
3348 (void)open_buffer(FALSE, eap);
3349#endif
3350
3351#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3352 if (swap_exists_action == SEA_QUIT)
3353 retval = FAIL;
3354 handle_swap_exists(old_curbuf);
3355#endif
3356 }
3357#ifdef FEAT_AUTOCMD
3358 else
3359 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003360 /* Read the modelines, but only to set window-local options. Any
3361 * buffer-local options have already been set and may have been
3362 * changed by the user. */
3363 do_modelines(TRUE);
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3366 &retval);
3367 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3368 &retval);
3369 }
3370 check_arg_idx(curwin);
3371#endif
3372
3373 /*
3374 * If autocommands change the cursor position or topline, we should
3375 * keep it.
3376 */
3377 if (curwin->w_cursor.lnum != lnum)
3378 {
3379 newlnum = curwin->w_cursor.lnum;
3380 newcol = curwin->w_cursor.col;
3381 }
3382 if (curwin->w_topline == topline)
3383 topline = 0;
3384
3385 /* Even when cursor didn't move we need to recompute topline. */
3386 changed_line_abv_curs();
3387
3388#ifdef FEAT_TITLE
3389 maketitle();
3390#endif
3391 }
3392
3393#ifdef FEAT_DIFF
3394 /* Tell the diff stuff that this buffer is new and/or needs updating.
3395 * Also needed when re-editing the same buffer, because unloading will
3396 * have removed it as a diff buffer. */
3397 diff_new_buffer();
3398 diff_invalidate();
3399#endif
3400
3401 if (command == NULL)
3402 {
3403 if (newcol >= 0) /* position set by autocommands */
3404 {
3405 curwin->w_cursor.lnum = newlnum;
3406 curwin->w_cursor.col = newcol;
3407 check_cursor();
3408 }
3409 else if (newlnum > 0) /* line number from caller or old position */
3410 {
3411 curwin->w_cursor.lnum = newlnum;
3412 check_cursor_lnum();
3413 if (solcol >= 0 && !p_sol)
3414 {
3415 /* 'sol' is off: Use last known column. */
3416 curwin->w_cursor.col = solcol;
3417 check_cursor_col();
3418#ifdef FEAT_VIRTUALEDIT
3419 curwin->w_cursor.coladd = 0;
3420#endif
3421 curwin->w_set_curswant = TRUE;
3422 }
3423 else
3424 beginline(BL_SOL | BL_FIX);
3425 }
3426 else /* no line number, go to last line in Ex mode */
3427 {
3428 if (exmode_active)
3429 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3430 beginline(BL_WHITE | BL_FIX);
3431 }
3432 }
3433
3434#ifdef FEAT_WINDOWS
3435 /* Check if cursors in other windows on the same buffer are still valid */
3436 check_lnums(FALSE);
3437#endif
3438
3439 /*
3440 * Did not read the file, need to show some info about the file.
3441 * Do this after setting the cursor.
3442 */
3443 if (oldbuf
3444#ifdef FEAT_AUTOCMD
3445 && !auto_buf
3446#endif
3447 )
3448 {
3449 int msg_scroll_save = msg_scroll;
3450
3451 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3452 * message. */
3453 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3454 msg_scroll = FALSE;
3455 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3456 check_for_delay(FALSE);
3457 msg_start();
3458 msg_scroll = msg_scroll_save;
3459 msg_scrolled_ign = TRUE;
3460
3461 fileinfo(FALSE, TRUE, FALSE);
3462
3463 msg_scrolled_ign = FALSE;
3464 }
3465
3466 if (command != NULL)
3467 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3468
3469#ifdef FEAT_KEYMAP
3470 if (curbuf->b_kmap_state & KEYMAP_INIT)
3471 keymap_init();
3472#endif
3473
3474 --RedrawingDisabled;
3475 if (!skip_redraw)
3476 {
3477 n = p_so;
3478 if (topline == 0 && command == NULL)
3479 p_so = 999; /* force cursor halfway the window */
3480 update_topline();
3481#ifdef FEAT_SCROLLBIND
3482 curwin->w_scbind_pos = curwin->w_topline;
3483#endif
3484 p_so = n;
3485 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3486 }
3487
3488 if (p_im)
3489 need_start_insertmode = TRUE;
3490
3491#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
3492 /* Change directories when the acd option is set on. */
3493 if (p_acd && curbuf->b_ffname != NULL
3494 && vim_chdirfile(curbuf->b_ffname) == OK)
3495 shorten_fnames(TRUE);
3496
3497 if (gui.in_use && curbuf->b_ffname != NULL)
3498 {
3499# ifdef FEAT_SUN_WORKSHOP
3500 if (usingSunWorkShop)
3501 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3502# endif
3503# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003504 if (usingNetbeans & ((flags & ECMD_SET_HELP) != ECMD_SET_HELP))
3505 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506# endif
3507 }
3508#endif
3509
3510theend:
3511#ifdef FEAT_BROWSE
3512 vim_free(browse_file);
3513#endif
3514 vim_free(free_fname);
3515 return retval;
3516}
3517
3518#ifdef FEAT_AUTOCMD
3519 static void
3520delbuf_msg(name)
3521 char_u *name;
3522{
3523 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3524 name == NULL ? (char_u *)"" : name);
3525 vim_free(name);
3526 au_new_curbuf = NULL;
3527}
3528#endif
3529
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003530static int append_indent = 0; /* autoindent for first line */
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532/*
3533 * ":insert" and ":append", also used by ":change"
3534 */
3535 void
3536ex_append(eap)
3537 exarg_T *eap;
3538{
3539 char_u *theline;
3540 int did_undo = FALSE;
3541 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003542 int indent = 0;
3543 char_u *p;
3544 int vcol;
3545 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003547 /* the ! flag toggles autoindent */
3548 if (eap->forceit)
3549 curbuf->b_p_ai = !curbuf->b_p_ai;
3550
3551 /* First autoindent comes from the line we start on */
3552 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
3553 append_indent = get_indent_lnum(lnum);
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (eap->cmdidx != CMD_append)
3556 --lnum;
3557
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003558 /* when the buffer is empty append to line 0 and delete the dummy line */
3559 if (empty && lnum == 1)
3560 lnum = 0;
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 State = INSERT; /* behave like in Insert mode */
3563 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3564 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003565
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003566 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 msg_scroll = TRUE;
3569 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003570 if (curbuf->b_p_ai)
3571 {
3572 if (append_indent >= 0)
3573 {
3574 indent = append_indent;
3575 append_indent = -1;
3576 }
3577 else if (lnum > 0)
3578 indent = get_indent_lnum(lnum);
3579 }
3580 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003582 {
3583 /* No getline() function, use the lines that follow. This ends
3584 * when there is no more. */
3585 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
3586 break;
3587 p = vim_strchr(eap->nextcmd, NL);
3588 if (p == NULL)
3589 p = eap->nextcmd + STRLEN(eap->nextcmd);
3590 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
3591 if (*p != NUL)
3592 ++p;
3593 eap->nextcmd = p;
3594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 else
3596 theline = eap->getline(
3597#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003598 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003600 NUL, eap->cookie, indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003602 if (theline == NULL)
3603 break;
3604
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003605 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
3606 if (ex_keep_indent)
3607 append_indent = indent;
3608
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003609 /* Look for the "." after automatic indent. */
3610 vcol = 0;
3611 for (p = theline; indent > vcol; ++p)
3612 {
3613 if (*p == ' ')
3614 ++vcol;
3615 else if (*p == TAB)
3616 vcol += 8 - vcol % 8;
3617 else
3618 break;
3619 }
3620 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00003621 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
3622 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 vim_free(theline);
3625 break;
3626 }
3627
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003628 /* don't use autoindent if nothing was typed. */
3629 if (p[0] == NUL)
3630 theline[0] = NUL;
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 did_undo = TRUE;
3633 ml_append(lnum, theline, (colnr_T)0, FALSE);
3634 appended_lines_mark(lnum, 1L);
3635
3636 vim_free(theline);
3637 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003638
3639 if (empty)
3640 {
3641 ml_delete(2L, FALSE);
3642 empty = FALSE;
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
3645 State = NORMAL;
3646
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003647 if (eap->forceit)
3648 curbuf->b_p_ai = !curbuf->b_p_ai;
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 /* "start" is set to eap->line2+1 unless that position is invalid (when
3651 * eap->line2 pointed to the end of the buffer and nothig was appended)
3652 * "end" is set to lnum when something has been appended, otherwise
3653 * it is the same than "start" -- Acevedo */
3654 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
3655 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
3656 if (eap->cmdidx != CMD_append)
3657 --curbuf->b_op_start.lnum;
3658 curbuf->b_op_end.lnum = (eap->line2 < lnum)
3659 ? lnum : curbuf->b_op_start.lnum;
3660 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
3661 curwin->w_cursor.lnum = lnum;
3662 check_cursor_lnum();
3663 beginline(BL_SOL | BL_FIX);
3664
3665 need_wait_return = FALSE; /* don't use wait_return() now */
3666 ex_no_reprint = TRUE;
3667}
3668
3669/*
3670 * ":change"
3671 */
3672 void
3673ex_change(eap)
3674 exarg_T *eap;
3675{
3676 linenr_T lnum;
3677
3678 if (eap->line2 >= eap->line1
3679 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
3680 return;
3681
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003682 /* the ! flag toggles autoindent */
3683 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
3684 append_indent = get_indent_lnum(eap->line1);
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
3687 {
3688 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
3689 break;
3690 ml_delete(eap->line1, FALSE);
3691 }
3692 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
3693
3694 /* ":append" on the line above the deleted lines. */
3695 eap->line2 = eap->line1;
3696 ex_append(eap);
3697}
3698
3699 void
3700ex_z(eap)
3701 exarg_T *eap;
3702{
3703 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003704 int bigness;
3705 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 int minus = 0;
3707 linenr_T start, end, curs, i;
3708 int j;
3709 linenr_T lnum = eap->line2;
3710
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003711 /* Vi compatible: ":z!" uses display height, without a count uses
3712 * 'scroll' */
3713 if (eap->forceit)
3714 bigness = curwin->w_height;
3715 else if (firstwin == lastwin)
3716 bigness = curwin->w_p_scr * 2;
3717 else
3718 bigness = curwin->w_height - 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (bigness < 1)
3720 bigness = 1;
3721
3722 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003723 kind = x;
3724 if (*kind == '-' || *kind == '+' || *kind == '='
3725 || *kind == '^' || *kind == '.')
3726 ++x;
3727 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 ++x;
3729
3730 if (*x != 0)
3731 {
3732 if (!VIM_ISDIGIT(*x))
3733 {
3734 EMSG(_("E144: non-numeric argument to :z"));
3735 return;
3736 }
3737 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003740 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003741 if (*kind == '=')
3742 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003746 /* the number of '-' and '+' multiplies the distance */
3747 if (*kind == '-' || *kind == '+')
3748 for (x = kind + 1; *x == *kind; ++x)
3749 ;
3750
3751 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 case '-':
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003754 start = lnum - bigness * (x - kind);
3755 end = start + bigness;
3756 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 break;
3758
3759 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003760 start = lnum - (bigness + 1) / 2 + 1;
3761 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 curs = lnum;
3763 minus = 1;
3764 break;
3765
3766 case '^':
3767 start = lnum - bigness * 2;
3768 end = lnum - bigness;
3769 curs = lnum - bigness;
3770 break;
3771
3772 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003773 start = lnum - (bigness + 1) / 2 + 1;
3774 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 curs = end;
3776 break;
3777
3778 default: /* '+' */
3779 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003780 if (*kind == '+')
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003781 start += bigness * (x - kind - 1) + 1;
3782 else if (eap->addr_count == 0)
3783 ++start;
3784 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 curs = end;
3786 break;
3787 }
3788
3789 if (start < 1)
3790 start = 1;
3791
3792 if (end > curbuf->b_ml.ml_line_count)
3793 end = curbuf->b_ml.ml_line_count;
3794
3795 if (curs > curbuf->b_ml.ml_line_count)
3796 curs = curbuf->b_ml.ml_line_count;
3797
3798 for (i = start; i <= end; i++)
3799 {
3800 if (minus && i == lnum)
3801 {
3802 msg_putchar('\n');
3803
3804 for (j = 1; j < Columns; j++)
3805 msg_putchar('-');
3806 }
3807
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003808 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 if (minus && i == lnum)
3811 {
3812 msg_putchar('\n');
3813
3814 for (j = 1; j < Columns; j++)
3815 msg_putchar('-');
3816 }
3817 }
3818
3819 curwin->w_cursor.lnum = curs;
3820 ex_no_reprint = TRUE;
3821}
3822
3823/*
3824 * Check if the restricted flag is set.
3825 * If so, give an error message and return TRUE.
3826 * Otherwise, return FALSE.
3827 */
3828 int
3829check_restricted()
3830{
3831 if (restricted)
3832 {
3833 EMSG(_("E145: Shell commands not allowed in rvim"));
3834 return TRUE;
3835 }
3836 return FALSE;
3837}
3838
3839/*
3840 * Check if the secure flag is set (.exrc or .vimrc in current directory).
3841 * If so, give an error message and return TRUE.
3842 * Otherwise, return FALSE.
3843 */
3844 int
3845check_secure()
3846{
3847 if (secure)
3848 {
3849 secure = 2;
3850 EMSG(_(e_curdir));
3851 return TRUE;
3852 }
3853#ifdef HAVE_SANDBOX
3854 /*
3855 * In the sandbox more things are not allowed, including the things
3856 * disallowed in secure mode.
3857 */
3858 if (sandbox != 0)
3859 {
3860 EMSG(_(e_sandbox));
3861 return TRUE;
3862 }
3863#endif
3864 return FALSE;
3865}
3866
3867static char_u *old_sub = NULL; /* previous substitute pattern */
3868static int global_need_beginline; /* call beginline() after ":g" */
3869
3870/*
3871 * When ":global" is used to number of substitutions and changed lines is
3872 * accumulated until it's finished.
3873 */
3874static long sub_nsubs; /* total number of substitutions */
3875static linenr_T sub_nlines; /* total number of lines changed */
3876
3877/* do_sub()
3878 *
3879 * Perform a substitution from line eap->line1 to line eap->line2 using the
3880 * command pointed to by eap->arg which should be of the form:
3881 *
3882 * /pattern/substitution/{flags}
3883 *
3884 * The usual escapes are supported as described in the regexp docs.
3885 */
3886 void
3887do_sub(eap)
3888 exarg_T *eap;
3889{
3890 linenr_T lnum;
3891 long i = 0;
3892 regmmatch_T regmatch;
3893 static int do_all = FALSE; /* do multiple substitutions per line */
3894 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003895 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 static int do_error = TRUE; /* if false, ignore errors */
3897 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003898 static int do_list = FALSE; /* list last line with subs. */
3899 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 static int do_ic = 0; /* ignore case flag */
3901 char_u *pat = NULL, *sub = NULL; /* init for GCC */
3902 int delimiter;
3903 int sublen;
3904 int got_quit = FALSE;
3905 int got_match = FALSE;
3906 int temp;
3907 int which_pat;
3908 char_u *cmd;
3909 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003910 linenr_T first_line = 0; /* first changed line */
3911 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 * change */
3913 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
3914 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003915 long nmatch; /* number of lines in match */
3916 linenr_T sub_firstlnum; /* nr of first sub line */
3917 char_u *sub_firstline; /* allocated copy of first sub line */
3918 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003919 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 cmd = eap->arg;
3922 if (!global_busy)
3923 {
3924 sub_nsubs = 0;
3925 sub_nlines = 0;
3926 }
3927
3928#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
3929 if (p_altkeymap && curwin->w_p_rl)
3930 lrF_sub(cmd);
3931#endif
3932
3933 if (eap->cmdidx == CMD_tilde)
3934 which_pat = RE_LAST; /* use last used regexp */
3935 else
3936 which_pat = RE_SUBST; /* use last substitute regexp */
3937
3938 /* new pattern and substitution */
3939 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
3940 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
3941 {
3942 /* don't accept alphanumeric for separator */
3943 if (isalpha(*cmd))
3944 {
3945 EMSG(_("E146: Regular expressions can't be delimited by letters"));
3946 return;
3947 }
3948 /*
3949 * undocumented vi feature:
3950 * "\/sub/" and "\?sub?" use last used search pattern (almost like
3951 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
3952 */
3953 if (*cmd == '\\')
3954 {
3955 ++cmd;
3956 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
3957 {
3958 EMSG(_(e_backslash));
3959 return;
3960 }
3961 if (*cmd != '&')
3962 which_pat = RE_SEARCH; /* use last '/' pattern */
3963 pat = (char_u *)""; /* empty search pattern */
3964 delimiter = *cmd++; /* remember delimiter character */
3965 }
3966 else /* find the end of the regexp */
3967 {
3968 which_pat = RE_LAST; /* use last used regexp */
3969 delimiter = *cmd++; /* remember delimiter character */
3970 pat = cmd; /* remember start of search pat */
3971 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
3972 if (cmd[0] == delimiter) /* end delimiter found */
3973 *cmd++ = NUL; /* replace it with a NUL */
3974 }
3975
3976 /*
3977 * Small incompatibility: vi sees '\n' as end of the command, but in
3978 * Vim we want to use '\n' to find/substitute a NUL.
3979 */
3980 sub = cmd; /* remember the start of the substitution */
3981
3982 while (cmd[0])
3983 {
3984 if (cmd[0] == delimiter) /* end delimiter found */
3985 {
3986 *cmd++ = NUL; /* replace it with a NUL */
3987 break;
3988 }
3989 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
3990 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003991 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993
3994 if (!eap->skip)
3995 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003996 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
3997 if (STRCMP(sub, "%") == 0
3998 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
3999 {
4000 if (old_sub == NULL) /* there is no previous command */
4001 {
4002 EMSG(_(e_nopresub));
4003 return;
4004 }
4005 sub = old_sub;
4006 }
4007 else
4008 {
4009 vim_free(old_sub);
4010 old_sub = vim_strsave(sub);
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 }
4014 else if (!eap->skip) /* use previous pattern and substitution */
4015 {
4016 if (old_sub == NULL) /* there is no previous command */
4017 {
4018 EMSG(_(e_nopresub));
4019 return;
4020 }
4021 pat = NULL; /* search_regcomp() will use previous pattern */
4022 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004023
4024 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4025 * last column after using "$". */
4026 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029 /*
4030 * Find trailing options. When '&' is used, keep old options.
4031 */
4032 if (*cmd == '&')
4033 ++cmd;
4034 else
4035 {
4036 if (!p_ed)
4037 {
4038 if (p_gd) /* default is global on */
4039 do_all = TRUE;
4040 else
4041 do_all = FALSE;
4042 do_ask = FALSE;
4043 }
4044 do_error = TRUE;
4045 do_print = FALSE;
4046 do_ic = 0;
4047 }
4048 while (*cmd)
4049 {
4050 /*
4051 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4052 * 'r' is never inverted.
4053 */
4054 if (*cmd == 'g')
4055 do_all = !do_all;
4056 else if (*cmd == 'c')
4057 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004058 else if (*cmd == 'n')
4059 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else if (*cmd == 'e')
4061 do_error = !do_error;
4062 else if (*cmd == 'r') /* use last used regexp */
4063 which_pat = RE_LAST;
4064 else if (*cmd == 'p')
4065 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004066 else if (*cmd == '#')
4067 {
4068 do_print = TRUE;
4069 do_number = TRUE;
4070 }
4071 else if (*cmd == 'l')
4072 {
4073 do_print = TRUE;
4074 do_list = TRUE;
4075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 else if (*cmd == 'i') /* ignore case */
4077 do_ic = 'i';
4078 else if (*cmd == 'I') /* don't ignore case */
4079 do_ic = 'I';
4080 else
4081 break;
4082 ++cmd;
4083 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004084 if (do_count)
4085 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087 /*
4088 * check for a trailing count
4089 */
4090 cmd = skipwhite(cmd);
4091 if (VIM_ISDIGIT(*cmd))
4092 {
4093 i = getdigits(&cmd);
4094 if (i <= 0 && !eap->skip && do_error)
4095 {
4096 EMSG(_(e_zerocount));
4097 return;
4098 }
4099 eap->line1 = eap->line2;
4100 eap->line2 += i - 1;
4101 if (eap->line2 > curbuf->b_ml.ml_line_count)
4102 eap->line2 = curbuf->b_ml.ml_line_count;
4103 }
4104
4105 /*
4106 * check for trailing command or garbage
4107 */
4108 cmd = skipwhite(cmd);
4109 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4110 {
4111 eap->nextcmd = check_nextcmd(cmd);
4112 if (eap->nextcmd == NULL)
4113 {
4114 EMSG(_(e_trailing));
4115 return;
4116 }
4117 }
4118
4119 if (eap->skip) /* not executing commands, only parsing */
4120 return;
4121
4122 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4123 {
4124 if (do_error)
4125 EMSG(_(e_invcmd));
4126 return;
4127 }
4128
4129 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4130 if (do_ic == 'i')
4131 regmatch.rmm_ic = TRUE;
4132 else if (do_ic == 'I')
4133 regmatch.rmm_ic = FALSE;
4134
4135 sub_firstline = NULL;
4136
4137 /*
4138 * ~ in the substitute pattern is replaced with the old pattern.
4139 * We do it here once to avoid it to be replaced over and over again.
4140 * But don't do it when it starts with "\=", then it's an expression.
4141 */
4142 if (!(sub[0] == '\\' && sub[1] == '='))
4143 sub = regtilde(sub, p_magic);
4144
4145 /*
4146 * Check for a match on each line.
4147 */
4148 line2 = eap->line2;
4149 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4150#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4151 || aborting()
4152#endif
4153 ); ++lnum)
4154 {
4155 sub_firstlnum = lnum;
4156 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
4157 if (nmatch)
4158 {
4159 colnr_T copycol;
4160 colnr_T matchcol;
4161 colnr_T prev_matchcol = MAXCOL;
4162 char_u *new_end, *new_start = NULL;
4163 unsigned new_start_len = 0;
4164 char_u *p1;
4165 int did_sub = FALSE;
4166 int lastone;
4167 unsigned len, needed_len;
4168 long nmatch_tl = 0; /* nr of lines matched below lnum */
4169 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004170 int skip_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 /*
4173 * The new text is build up step by step, to avoid too much
4174 * copying. There are these pieces:
4175 * sub_firstline The old text, unmodifed.
4176 * copycol Column in the old text where we started
4177 * looking for a match; from here old text still
4178 * needs to be copied to the new text.
4179 * matchcol Column number of the old text where to look
4180 * for the next match. It's just after the
4181 * previous match or one further.
4182 * prev_matchcol Column just after the previous match (if any).
4183 * Mostly equal to matchcol, except for the first
4184 * match and after skipping an empty match.
4185 * regmatch.*pos Where the pattern matched in the old text.
4186 * new_start The new text, all that has been produced so
4187 * far.
4188 * new_end The new text, where to append new text.
4189 *
4190 * lnum The line number where we were looking for the
4191 * first match in the old line.
4192 * sub_firstlnum The line number in the buffer where to look
4193 * for a match. Can be different from "lnum"
4194 * when the pattern or substitute string contains
4195 * line breaks.
4196 *
4197 * Special situations:
4198 * - When the substitute string contains a line break, the part up
4199 * to the line break is inserted in the text, but the copy of
4200 * the original line is kept. "sub_firstlnum" is adjusted for
4201 * the inserted lines.
4202 * - When the matched pattern contains a line break, the old line
4203 * is taken from the line at the end of the pattern. The lines
4204 * in the match are deleted later, "sub_firstlnum" is adjusted
4205 * accordingly.
4206 *
4207 * The new text is built up in new_start[]. It has some extra
4208 * room to avoid using alloc()/free() too often. new_start_len is
4209 * the lenght of the allocated memory at new_start.
4210 *
4211 * Make a copy of the old line, so it won't be taken away when
4212 * updating the screen or handling a multi-line match. The "old_"
4213 * pointers point into this copy.
4214 */
4215 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4216 if (sub_firstline == NULL)
4217 {
4218 vim_free(new_start);
4219 goto outofmem;
4220 }
4221 copycol = 0;
4222 matchcol = 0;
4223
4224 /* At first match, remember current cursor position. */
4225 if (!got_match)
4226 {
4227 setpcmark();
4228 got_match = TRUE;
4229 }
4230
4231 /*
4232 * Loop until nothing more to replace in this line.
4233 * 1. Handle match with empty string.
4234 * 2. If do_ask is set, ask for confirmation.
4235 * 3. substitute the string.
4236 * 4. if do_all is set, find next match
4237 * 5. break if there isn't another match in this line
4238 */
4239 for (;;)
4240 {
4241 /* Save the line number of the last change for the final
4242 * cursor position (just like Vi). */
4243 curwin->w_cursor.lnum = lnum;
4244 do_again = FALSE;
4245
4246 /*
4247 * 1. Match empty string does not count, except for first
4248 * match. This reproduces the strange vi behaviour.
4249 * This also catches endless loops.
4250 */
4251 if (matchcol == prev_matchcol
4252 && regmatch.endpos[0].lnum == 0
4253 && matchcol == regmatch.endpos[0].col)
4254 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004255 if (sub_firstline[matchcol] == NUL)
4256 /* We already were at the end of the line. Don't look
4257 * for a match in this line again. */
4258 skip_match = TRUE;
4259 else
4260 ++matchcol; /* search for a match at next column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 goto skip;
4262 }
4263
4264 /* Normally we continue searching for a match just after the
4265 * previous match. */
4266 matchcol = regmatch.endpos[0].col;
4267 prev_matchcol = matchcol;
4268
4269 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270 * 2. If do_count is set only increase the counter.
4271 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004273 if (do_count)
4274 {
4275 /* For a multi-line match, put matchcol at the NUL at
4276 * the end of the line and set nmatch to one, so that
4277 * we continue looking for a match on the next line.
4278 * Avoids that ":s/\nB\@=//gc" get stuck. */
4279 if (nmatch > 1)
4280 {
4281 matchcol = STRLEN(sub_firstline);
4282 nmatch = 1;
4283 }
4284 sub_nsubs++;
4285 did_sub = TRUE;
4286 goto skip;
4287 }
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (do_ask)
4290 {
4291 /* change State to CONFIRM, so that the mouse works
4292 * properly */
4293 save_State = State;
4294 State = CONFIRM;
4295#ifdef FEAT_MOUSE
4296 setmouse(); /* disable mouse in xterm */
4297#endif
4298 curwin->w_cursor.col = regmatch.startpos[0].col;
4299
4300 /* When 'cpoptions' contains "u" don't sync undo when
4301 * asking for confirmation. */
4302 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4303 ++no_u_sync;
4304
4305 /*
4306 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4307 */
4308 while (do_ask)
4309 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004310 if (exmode_active)
4311 {
4312 char_u *resp;
4313 colnr_T sc, ec;
4314
4315 print_line_no_prefix(lnum, FALSE, FALSE);
4316
4317 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4318 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4319 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
4320 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004321 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004322 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004323 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004324 msg_putchar('^');
4325
4326 resp = getexmodeline('?', NULL, 0);
4327 if (resp != NULL)
4328 {
4329 i = *resp;
4330 vim_free(resp);
4331 }
4332 }
4333 else
4334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004336 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004338 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004340 /* Invert the matched string.
4341 * Remove the inversion afterwards. */
4342 temp = RedrawingDisabled;
4343 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004345 search_match_lines = regmatch.endpos[0].lnum;
4346 search_match_endcol = regmatch.endpos[0].col;
4347 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004349 update_topline();
4350 validate_cursor();
4351 update_screen(NOT_VALID);
4352 highlight_match = FALSE;
4353 redraw_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004356 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004358 if (msg_row == Rows - 1)
4359 msg_didout = FALSE; /* avoid a scroll-up */
4360 msg_starthere();
4361 i = msg_scroll;
4362 msg_scroll = 0; /* truncate msg when
4363 needed */
4364 msg_no_more = TRUE;
4365 /* write message same highlighting as for
4366 * wait_return */
4367 smsg_attr(hl_attr(HLF_R),
4368 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4369 msg_no_more = FALSE;
4370 msg_scroll = i;
4371 showruler(TRUE);
4372 windgoto(msg_row, msg_col);
4373 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
4375#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004376 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004378 ++no_mapping; /* don't map this key */
4379 ++allow_keys; /* allow special keys */
4380 i = safe_vgetc();
4381 --allow_keys;
4382 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004384 /* clear the question */
4385 msg_didout = FALSE; /* don't scroll up */
4386 msg_col = 0;
4387 gotocmdline(TRUE);
4388 }
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 need_wait_return = FALSE; /* no hit-return prompt */
4391 if (i == 'q' || i == ESC || i == Ctrl_C
4392#ifdef UNIX
4393 || i == intr_char
4394#endif
4395 )
4396 {
4397 got_quit = TRUE;
4398 break;
4399 }
4400 if (i == 'n')
4401 break;
4402 if (i == 'y')
4403 break;
4404 if (i == 'l')
4405 {
4406 /* last: replace and then stop */
4407 do_all = FALSE;
4408 line2 = lnum;
4409 break;
4410 }
4411 if (i == 'a')
4412 {
4413 do_ask = FALSE;
4414 break;
4415 }
4416#ifdef FEAT_INS_EXPAND
4417 if (i == Ctrl_E)
4418 scrollup_clamp();
4419 else if (i == Ctrl_Y)
4420 scrolldown_clamp();
4421#endif
4422 }
4423 State = save_State;
4424#ifdef FEAT_MOUSE
4425 setmouse();
4426#endif
4427 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4428 --no_u_sync;
4429
4430 if (i == 'n')
4431 {
4432 /* For a multi-line match, put matchcol at the NUL at
4433 * the end of the line and set nmatch to one, so that
4434 * we continue looking for a match on the next line.
4435 * Avoids that ":s/\nB\@=//gc" get stuck. */
4436 if (nmatch > 1)
4437 {
4438 matchcol = STRLEN(sub_firstline);
4439 nmatch = 1;
4440 }
4441 goto skip;
4442 }
4443 if (got_quit)
4444 break;
4445 }
4446
4447 /* Move the cursor to the start of the match, so that we can
4448 * use "\=col("."). */
4449 curwin->w_cursor.col = regmatch.startpos[0].col;
4450
4451 /*
4452 * 3. substitute the string.
4453 */
4454 /* get length of substitution part */
4455 sublen = vim_regsub_multi(&regmatch, sub_firstlnum,
4456 sub, sub_firstline, FALSE, p_magic, TRUE);
4457
4458 /* Need room for:
4459 * - result so far in new_start (not for first sub in line)
4460 * - original text up to match
4461 * - length of substituted part
4462 * - original text after match
4463 */
4464 if (nmatch == 1)
4465 p1 = sub_firstline;
4466 else
4467 {
4468 p1 = ml_get(sub_firstlnum + nmatch - 1);
4469 nmatch_tl += nmatch - 1;
4470 }
4471 i = regmatch.startpos[0].col - copycol;
4472 needed_len = i + ((unsigned)STRLEN(p1) - regmatch.endpos[0].col)
4473 + sublen + 1;
4474 if (new_start == NULL)
4475 {
4476 /*
4477 * Get some space for a temporary buffer to do the
4478 * substitution into (and some extra space to avoid
4479 * too many calls to alloc()/free()).
4480 */
4481 new_start_len = needed_len + 50;
4482 if ((new_start = alloc_check(new_start_len)) == NULL)
4483 goto outofmem;
4484 *new_start = NUL;
4485 new_end = new_start;
4486 }
4487 else
4488 {
4489 /*
4490 * Check if the temporary buffer is long enough to do the
4491 * substitution into. If not, make it larger (with a bit
4492 * extra to avoid too many calls to alloc()/free()).
4493 */
4494 len = (unsigned)STRLEN(new_start);
4495 needed_len += len;
4496 if (needed_len > new_start_len)
4497 {
4498 new_start_len = needed_len + 50;
4499 if ((p1 = alloc_check(new_start_len)) == NULL)
4500 {
4501 vim_free(new_start);
4502 goto outofmem;
4503 }
4504 mch_memmove(p1, new_start, (size_t)(len + 1));
4505 vim_free(new_start);
4506 new_start = p1;
4507 }
4508 new_end = new_start + len;
4509 }
4510
4511 /*
4512 * copy the text up to the part that matched
4513 */
4514 mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
4515 new_end += i;
4516
4517 (void)vim_regsub_multi(&regmatch, sub_firstlnum,
4518 sub, new_end, TRUE, p_magic, TRUE);
4519 sub_nsubs++;
4520 did_sub = TRUE;
4521
4522 /* Move the cursor to the start of the line, to avoid that it
4523 * is beyond the end of the line after the substitution. */
4524 curwin->w_cursor.col = 0;
4525
4526 /* For a multi-line match, make a copy of the last matched
4527 * line and continue in that one. */
4528 if (nmatch > 1)
4529 {
4530 sub_firstlnum += nmatch - 1;
4531 vim_free(sub_firstline);
4532 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4533 /* When going beyond the last line, stop substituting. */
4534 if (sub_firstlnum <= line2)
4535 do_again = TRUE;
4536 else
4537 do_all = FALSE;
4538 }
4539
4540 /* Remember next character to be copied. */
4541 copycol = regmatch.endpos[0].col;
4542
4543 /*
4544 * Now the trick is to replace CTRL-M chars with a real line
4545 * break. This would make it impossible to insert a CTRL-M in
4546 * the text. The line break can be avoided by preceding the
4547 * CTRL-M with a backslash. To be able to insert a backslash,
4548 * they must be doubled in the string and are halved here.
4549 * That is Vi compatible.
4550 */
4551 for (p1 = new_end; *p1; ++p1)
4552 {
4553 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
4554 mch_memmove(p1, p1 + 1, STRLEN(p1));
4555 else if (*p1 == CAR)
4556 {
4557 if (u_inssub(lnum) == OK) /* prepare for undo */
4558 {
4559 *p1 = NUL; /* truncate up to the CR */
4560 ml_append(lnum - 1, new_start,
4561 (colnr_T)(p1 - new_start + 1), FALSE);
4562 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
4563 if (do_ask)
4564 appended_lines(lnum - 1, 1L);
4565 else
4566 {
4567 if (first_line == 0)
4568 first_line = lnum;
4569 last_line = lnum + 1;
4570 }
4571 /* All line numbers increase. */
4572 ++sub_firstlnum;
4573 ++lnum;
4574 ++line2;
4575 /* move the cursor to the new line, like Vi */
4576 ++curwin->w_cursor.lnum;
4577 STRCPY(new_start, p1 + 1); /* copy the rest */
4578 p1 = new_start - 1;
4579 }
4580 }
4581#ifdef FEAT_MBYTE
4582 else if (has_mbyte)
4583 p1 += (*mb_ptr2len_check)(p1) - 1;
4584#endif
4585 }
4586
4587 /*
4588 * 4. If do_all is set, find next match.
4589 * Prevent endless loop with patterns that match empty
4590 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
4591 * But ":s/\n/#/" is OK.
4592 */
4593skip:
4594 /* We already know that we did the last subst when we are at
4595 * the end of the line, except that a pattern like
4596 * "bar\|\nfoo" may match at the NUL. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004597 lastone = (skip_match
4598 || got_int
4599 || got_quit
4600 || !(do_all || do_again)
4601 || (sub_firstline[matchcol] == NUL && nmatch <= 1
4602 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 nmatch = -1;
4604
4605 /*
4606 * Replace the line in the buffer when needed. This is
4607 * skipped when there are more matches.
4608 * The check for nmatch_tl is needed for when multi-line
4609 * matching must replace the lines before trying to do another
4610 * match, otherwise "\@<=" won't work.
4611 * When asking the user we like to show the already replaced
4612 * text, but don't do it when "\<@=" or "\<@!" is used, it
4613 * changes what matches.
4614 */
4615 if (lastone
4616 || (do_ask && !re_lookbehind(regmatch.regprog))
4617 || nmatch_tl > 0
4618 || (nmatch = vim_regexec_multi(&regmatch, curwin,
4619 curbuf, sub_firstlnum, matchcol)) == 0)
4620 {
4621 if (new_start != NULL)
4622 {
4623 /*
4624 * Copy the rest of the line, that didn't match.
4625 * "matchcol" has to be adjusted, we use the end of
4626 * the line as reference, because the substitute may
4627 * have changed the number of characters. Same for
4628 * "prev_matchcol".
4629 */
4630 STRCAT(new_start, sub_firstline + copycol);
4631 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4632 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4633 - prev_matchcol;
4634
4635 if (u_savesub(lnum) != OK)
4636 break;
4637 ml_replace(lnum, new_start, TRUE);
4638
4639 if (nmatch_tl > 0)
4640 {
4641 /*
4642 * Matched lines have now been substituted and are
4643 * useless, delete them. The part after the match
4644 * has been appended to new_start, we don't need
4645 * it in the buffer.
4646 */
4647 ++lnum;
4648 if (u_savedel(lnum, nmatch_tl) != OK)
4649 break;
4650 for (i = 0; i < nmatch_tl; ++i)
4651 ml_delete(lnum, (int)FALSE);
4652 mark_adjust(lnum, lnum + nmatch_tl - 1,
4653 (long)MAXLNUM, -nmatch_tl);
4654 if (do_ask)
4655 deleted_lines(lnum, nmatch_tl);
4656 --lnum;
4657 line2 -= nmatch_tl; /* nr of lines decreases */
4658 nmatch_tl = 0;
4659 }
4660
4661 /* When asking, undo is saved each time, must also set
4662 * changed flag each time. */
4663 if (do_ask)
4664 changed_bytes(lnum, 0);
4665 else
4666 {
4667 if (first_line == 0)
4668 first_line = lnum;
4669 last_line = lnum + 1;
4670 }
4671
4672 sub_firstlnum = lnum;
4673 vim_free(sub_firstline); /* free the temp buffer */
4674 sub_firstline = new_start;
4675 new_start = NULL;
4676 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4677 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4678 - prev_matchcol;
4679 copycol = 0;
4680 }
4681 if (nmatch == -1 && !lastone)
4682 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
4683 sub_firstlnum, matchcol);
4684
4685 /*
4686 * 5. break if there isn't another match in this line
4687 */
4688 if (nmatch <= 0)
4689 break;
4690 }
4691
4692 line_breakcheck();
4693 }
4694
4695 if (did_sub)
4696 ++sub_nlines;
4697 vim_free(sub_firstline); /* free the copy of the original line */
4698 sub_firstline = NULL;
4699 }
4700
4701 line_breakcheck();
4702 }
4703
4704 if (first_line != 0)
4705 {
4706 /* Need to subtract the number of added lines from "last_line" to get
4707 * the line number before the change (same as adding the number of
4708 * deleted lines). */
4709 i = curbuf->b_ml.ml_line_count - old_line_count;
4710 changed_lines(first_line, 0, last_line - i, i);
4711 }
4712
4713outofmem:
4714 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004715
4716 /* ":s/pat//n" doesn't move the cursor */
4717 if (do_count)
4718 curwin->w_cursor = old_cursor;
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (sub_nsubs)
4721 {
4722 /* Set the '[ and '] marks. */
4723 curbuf->b_op_start.lnum = eap->line1;
4724 curbuf->b_op_end.lnum = line2;
4725 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4726
4727 if (!global_busy)
4728 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004729 if (endcolumn)
4730 coladvance((colnr_T)MAXCOL);
4731 else
4732 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004733 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 MSG("");
4735 }
4736 else
4737 global_need_beginline = TRUE;
4738 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004739 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741 else if (!global_busy)
4742 {
4743 if (got_int) /* interrupted */
4744 EMSG(_(e_interr));
4745 else if (got_match) /* did find something but nothing substituted */
4746 MSG("");
4747 else if (do_error) /* nothing found */
4748 EMSG2(_(e_patnotf2), get_search_pat());
4749 }
4750
4751 vim_free(regmatch.regprog);
4752}
4753
4754/*
4755 * Give message for number of substitutions.
4756 * Can also be used after a ":global" command.
4757 * Return TRUE if a message was given.
4758 */
4759 static int
Bram Moolenaar05159a02005-02-26 23:04:13 +00004760do_sub_msg(count_only)
4761 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
Bram Moolenaar555b2802005-05-19 21:08:39 +00004763 int len = 0;
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 /*
4766 * Only report substitutions when:
4767 * - more than 'report' substitutions
4768 * - command was typed by user, or number of changed lines > 'report'
4769 * - giving messages is not disabled by 'lazyredraw'
4770 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004771 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
4772 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 && messaging())
4774 {
4775 if (got_int)
Bram Moolenaar555b2802005-05-19 21:08:39 +00004776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar555b2802005-05-19 21:08:39 +00004778 len = STRLEN(msg_buf);
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 if (sub_nsubs == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00004781 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
4782 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00004784 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004785 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 sub_nsubs);
Bram Moolenaar555b2802005-05-19 21:08:39 +00004787 len = STRLEN(msg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (sub_nlines == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00004789 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
4790 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00004792 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
4793 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 if (msg(msg_buf))
4795 {
4796 /* save message to display it after redraw */
4797 set_keep_msg(msg_buf);
4798 keep_msg_attr = 0;
4799 }
4800 return TRUE;
4801 }
4802 if (got_int)
4803 {
4804 EMSG(_(e_interr));
4805 return TRUE;
4806 }
4807 return FALSE;
4808}
4809
4810/*
4811 * Execute a global command of the form:
4812 *
4813 * g/pattern/X : execute X on all lines where pattern matches
4814 * v/pattern/X : execute X on all lines where pattern does not match
4815 *
4816 * where 'X' is an EX command
4817 *
4818 * The command character (as well as the trailing slash) is optional, and
4819 * is assumed to be 'p' if missing.
4820 *
4821 * This is implemented in two passes: first we scan the file for the pattern and
4822 * set a mark for each line that (not) matches. secondly we execute the command
4823 * for each line that has a mark. This is required because after deleting
4824 * lines we do not know where to search for the next match.
4825 */
4826 void
4827ex_global(eap)
4828 exarg_T *eap;
4829{
4830 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004831 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 int type; /* first char of cmd: 'v' or 'g' */
4833 char_u *cmd; /* command argument */
4834
4835 char_u delim; /* delimiter, normally '/' */
4836 char_u *pat;
4837 regmmatch_T regmatch;
4838 int match;
4839 int which_pat;
4840
4841 if (global_busy)
4842 {
4843 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
4844 return;
4845 }
4846
4847 if (eap->forceit) /* ":global!" is like ":vglobal" */
4848 type = 'v';
4849 else
4850 type = *eap->cmd;
4851 cmd = eap->arg;
4852 which_pat = RE_LAST; /* default: use last used regexp */
4853 sub_nsubs = 0;
4854 sub_nlines = 0;
4855
4856 /*
4857 * undocumented vi feature:
4858 * "\/" and "\?": use previous search pattern.
4859 * "\&": use previous substitute pattern.
4860 */
4861 if (*cmd == '\\')
4862 {
4863 ++cmd;
4864 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4865 {
4866 EMSG(_(e_backslash));
4867 return;
4868 }
4869 if (*cmd == '&')
4870 which_pat = RE_SUBST; /* use previous substitute pattern */
4871 else
4872 which_pat = RE_SEARCH; /* use previous search pattern */
4873 ++cmd;
4874 pat = (char_u *)"";
4875 }
4876 else if (*cmd == NUL)
4877 {
4878 EMSG(_("E148: Regular expression missing from global"));
4879 return;
4880 }
4881 else
4882 {
4883 delim = *cmd; /* get the delimiter */
4884 if (delim)
4885 ++cmd; /* skip delimiter if there is one */
4886 pat = cmd; /* remember start of pattern */
4887 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
4888 if (cmd[0] == delim) /* end delimiter found */
4889 *cmd++ = NUL; /* replace it with a NUL */
4890 }
4891
4892#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
4893 if (p_altkeymap && curwin->w_p_rl)
4894 lrFswap(pat,0);
4895#endif
4896
4897 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4898 {
4899 EMSG(_(e_invcmd));
4900 return;
4901 }
4902
4903 /*
4904 * pass 1: set marks for each (not) matching line
4905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
4907 {
4908 /* a match on this line? */
4909 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
4910 if ((type == 'g' && match) || (type == 'v' && !match))
4911 {
4912 ml_setmarked(lnum);
4913 ndone++;
4914 }
4915 line_breakcheck();
4916 }
4917
4918 /*
4919 * pass 2: execute the command for each line that has been marked
4920 */
4921 if (got_int)
4922 MSG(_(e_interr));
4923 else if (ndone == 0)
4924 {
4925 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00004926 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00004928 smsg((char_u *)_(e_patnotf2), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 else
4931 global_exe(cmd);
4932
4933 ml_clearmarked(); /* clear rest of the marks */
4934 vim_free(regmatch.regprog);
4935}
4936
4937/*
4938 * Execute "cmd" on lines marked with ml_setmarked().
4939 */
4940 void
4941global_exe(cmd)
4942 char_u *cmd;
4943{
4944 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
4945 linenr_T lnum; /* line number according to old situation */
4946
4947 /*
4948 * Set current position only once for a global command.
4949 * If global_busy is set, setpcmark() will not do anything.
4950 * If there is an error, global_busy will be incremented.
4951 */
4952 setpcmark();
4953
4954 /* When the command writes a message, don't overwrite the command. */
4955 msg_didout = TRUE;
4956
4957 global_need_beginline = FALSE;
4958 global_busy = 1;
4959 old_lcount = curbuf->b_ml.ml_line_count;
4960 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
4961 {
4962 curwin->w_cursor.lnum = lnum;
4963 curwin->w_cursor.col = 0;
4964 if (*cmd == NUL || *cmd == '\n')
4965 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
4966 else
4967 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
4968 ui_breakcheck();
4969 }
4970
4971 global_busy = 0;
4972 if (global_need_beginline)
4973 beginline(BL_WHITE | BL_FIX);
4974 else
4975 check_cursor(); /* cursor may be beyond the end of the line */
4976
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004977 /* the cursor may not have moved in the text but a change in a previous
4978 * line may move it on the screen */
4979 changed_line_abv_curs();
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /* If it looks like no message was written, allow overwriting the
4982 * command with the report for number of changes. */
4983 if (msg_col == 0 && msg_scrolled == 0)
4984 msg_didout = FALSE;
4985
4986 /* If subsitutes done, report number of substitues, otherwise report
4987 * number of extra or deleted lines. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004988 if (!do_sub_msg(FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
4990}
4991
4992#ifdef FEAT_VIMINFO
4993 int
4994read_viminfo_sub_string(virp, force)
4995 vir_T *virp;
4996 int force;
4997{
4998 if (old_sub != NULL && force)
4999 vim_free(old_sub);
5000 if (force || old_sub == NULL)
5001 old_sub = viminfo_readstring(virp, 1, TRUE);
5002 return viminfo_readline(virp);
5003}
5004
5005 void
5006write_viminfo_sub_string(fp)
5007 FILE *fp;
5008{
5009 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5010 {
5011 fprintf(fp, _("\n# Last Substitute String:\n$"));
5012 viminfo_writestring(fp, old_sub);
5013 }
5014}
5015#endif /* FEAT_VIMINFO */
5016
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005017#if defined(EXITFREE) || defined(PROTO)
5018 void
5019free_old_sub()
5020{
5021 vim_free(old_sub);
5022}
5023#endif
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5026/*
5027 * Set up for a tagpreview.
5028 */
5029 void
5030prepare_tagpreview()
5031{
5032 win_T *wp;
5033
5034# ifdef FEAT_GUI
5035 need_mouse_correct = TRUE;
5036# endif
5037
5038 /*
5039 * If there is already a preview window open, use that one.
5040 */
5041 if (!curwin->w_p_pvw)
5042 {
5043 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5044 if (wp->w_p_pvw)
5045 break;
5046 if (wp != NULL)
5047 win_enter(wp, TRUE);
5048 else
5049 {
5050 /*
5051 * There is no preview window open yet. Create one.
5052 */
5053 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5054 == FAIL)
5055 return;
5056 curwin->w_p_pvw = TRUE;
5057 curwin->w_p_wfh = TRUE;
5058# ifdef FEAT_SCROLLBIND
5059 curwin->w_p_scb = FALSE; /* don't take over 'scrollbind' */
5060# endif
5061# ifdef FEAT_DIFF
5062 curwin->w_p_diff = FALSE; /* no 'diff' */
5063# endif
5064# ifdef FEAT_FOLDING
5065 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5066# endif
5067 }
5068 }
5069}
5070
5071#endif
5072
5073
5074/*
5075 * ":help": open a read-only window on a help file
5076 */
5077 void
5078ex_help(eap)
5079 exarg_T *eap;
5080{
5081 char_u *arg;
5082 char_u *tag;
5083 FILE *helpfd; /* file descriptor of help file */
5084 int n;
5085 int i;
5086#ifdef FEAT_WINDOWS
5087 win_T *wp;
5088#endif
5089 int num_matches;
5090 char_u **matches;
5091 char_u *p;
5092 int empty_fnum = 0;
5093 int alt_fnum = 0;
5094 buf_T *buf;
5095#ifdef FEAT_MULTI_LANG
5096 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005097 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098#endif
5099
5100 if (eap != NULL)
5101 {
5102 /*
5103 * A ":help" command ends at the first LF, or at a '|' that is
5104 * followed by some text. Set nextcmd to the following command.
5105 */
5106 for (arg = eap->arg; *arg; ++arg)
5107 {
5108 if (*arg == '\n' || *arg == '\r'
5109 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5110 {
5111 *arg++ = NUL;
5112 eap->nextcmd = arg;
5113 break;
5114 }
5115 }
5116 arg = eap->arg;
5117
5118 if (eap->forceit && *arg == NUL)
5119 {
5120 EMSG(_("E478: Don't panic!"));
5121 return;
5122 }
5123
5124 if (eap->skip) /* not executing commands */
5125 return;
5126 }
5127 else
5128 arg = (char_u *)"";
5129
5130 /* remove trailing blanks */
5131 p = arg + STRLEN(arg) - 1;
5132 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5133 *p-- = NUL;
5134
5135#ifdef FEAT_MULTI_LANG
5136 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005137 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#endif
5139
5140 /* When no argument given go to the index. */
5141 if (*arg == NUL)
5142 arg = (char_u *)"help.txt";
5143
5144 /*
5145 * Check if there is a match for the argument.
5146 */
5147 n = find_help_tags(arg, &num_matches, &matches,
5148 eap != NULL && eap->forceit);
5149
5150 i = 0;
5151#ifdef FEAT_MULTI_LANG
5152 if (n != FAIL && lang != NULL)
5153 /* Find first item with the requested language. */
5154 for (i = 0; i < num_matches; ++i)
5155 {
5156 len = STRLEN(matches[i]);
5157 if (len > 3 && matches[i][len - 3] == '@'
5158 && STRICMP(matches[i] + len - 2, lang) == 0)
5159 break;
5160 }
5161#endif
5162 if (i >= num_matches || n == FAIL)
5163 {
5164#ifdef FEAT_MULTI_LANG
5165 if (lang != NULL)
5166 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5167 else
5168#endif
5169 EMSG2(_("E149: Sorry, no help for %s"), arg);
5170 if (n != FAIL)
5171 FreeWild(num_matches, matches);
5172 return;
5173 }
5174
5175 /* The first match (in the requested language) is the best match. */
5176 tag = vim_strsave(matches[i]);
5177 FreeWild(num_matches, matches);
5178
5179#ifdef FEAT_GUI
5180 need_mouse_correct = TRUE;
5181#endif
5182
5183 /*
5184 * Re-use an existing help window or open a new one.
5185 */
5186 if (!curwin->w_buffer->b_help)
5187 {
5188#ifdef FEAT_WINDOWS
5189 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5190 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5191 break;
5192 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5193 win_enter(wp, TRUE);
5194 else
5195#endif
5196 {
5197 /*
5198 * There is no help window yet.
5199 * Try to open the file specified by the "helpfile" option.
5200 */
5201 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5202 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005203 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 goto erret;
5205 }
5206 fclose(helpfd);
5207
5208#ifdef FEAT_WINDOWS
5209 /* Split off help window; put it at far top if no position
5210 * specified, the current window is vertically split and narrow. */
5211 n = WSP_HELP;
5212# ifdef FEAT_VERTSPLIT
5213 if (cmdmod.split == 0 && curwin->w_width != Columns
5214 && curwin->w_width < 80)
5215 n |= WSP_TOP;
5216# endif
5217 if (win_split(0, n) == FAIL)
5218#else
5219 /* use current window */
5220 if (!can_abandon(curbuf, FALSE))
5221#endif
5222 goto erret;
5223
5224#ifdef FEAT_WINDOWS
5225 if (curwin->w_height < p_hh)
5226 win_setheight((int)p_hh);
5227#endif
5228
5229 /*
5230 * Open help file (do_ecmd() will set b_help flag, readfile() will
5231 * set b_p_ro flag).
5232 * Set the alternate file to the previously edited file.
5233 */
5234 alt_fnum = curbuf->b_fnum;
5235 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
5236 ECMD_HIDE + ECMD_SET_HELP);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005237 if (!cmdmod.keepalt)
5238 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 empty_fnum = curbuf->b_fnum;
5240 }
5241 }
5242
5243 if (!p_im)
5244 restart_edit = 0; /* don't want insert mode in help file */
5245
5246 if (tag != NULL)
5247 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5248
5249 /* Delete the empty buffer if we're not using it. */
5250 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5251 {
5252 buf = buflist_findnr(empty_fnum);
5253 if (buf != NULL)
5254 wipe_buffer(buf, TRUE);
5255 }
5256
5257 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005258 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 curwin->w_alt_fnum = alt_fnum;
5260
5261erret:
5262 vim_free(tag);
5263}
5264
5265
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005266#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5267/*
5268 * In an argument search for a language specifiers in the form "@xx".
5269 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5270 * Returns NULL if not found.
5271 */
5272 char_u *
5273check_help_lang(arg)
5274 char_u *arg;
5275{
5276 int len = STRLEN(arg);
5277
5278 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
5279 && ASCII_ISALPHA(arg[len - 1]))
5280 {
5281 arg[len - 3] = NUL; /* remove the '@' */
5282 return arg + len - 2;
5283 }
5284 return NULL;
5285}
5286#endif
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288/*
5289 * Return a heuristic indicating how well the given string matches. The
5290 * smaller the number, the better the match. This is the order of priorities,
5291 * from best match to worst match:
5292 * - Match with least alpha-numeric characters is better.
5293 * - Match with least total characters is better.
5294 * - Match towards the start is better.
5295 * - Match starting with "+" is worse (feature instead of command)
5296 * Assumption is made that the matched_string passed has already been found to
5297 * match some string for which help is requested. webb.
5298 */
5299 int
5300help_heuristic(matched_string, offset, wrong_case)
5301 char_u *matched_string;
5302 int offset; /* offset for match */
5303 int wrong_case; /* no matching case */
5304{
5305 int num_letters;
5306 char_u *p;
5307
5308 num_letters = 0;
5309 for (p = matched_string; *p; p++)
5310 if (ASCII_ISALNUM(*p))
5311 num_letters++;
5312
5313 /*
5314 * Multiply the number of letters by 100 to give it a much bigger
5315 * weighting than the number of characters.
5316 * If there only is a match while ignoring case, add 5000.
5317 * If the match starts in the middle of a word, add 10000 to put it
5318 * somewhere in the last half.
5319 * If the match is more than 2 chars from the start, multiply by 200 to
5320 * put it after matches at the start.
5321 */
5322 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
5323 && ASCII_ISALNUM(matched_string[offset - 1]))
5324 offset += 10000;
5325 else if (offset > 2)
5326 offset *= 200;
5327 if (wrong_case)
5328 offset += 5000;
5329 /* Features are less interesting than the subjects themselves, but "+"
5330 * alone is not a feature. */
5331 if (matched_string[0] == '+' && matched_string[1] != NUL)
5332 offset += 100;
5333 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
5334}
5335
5336/*
5337 * Compare functions for qsort() below, that checks the help heuristics number
5338 * that has been put after the tagname by find_tags().
5339 */
5340 static int
5341#ifdef __BORLANDC__
5342_RTLENTRYF
5343#endif
5344help_compare(s1, s2)
5345 const void *s1;
5346 const void *s2;
5347{
5348 char *p1;
5349 char *p2;
5350
5351 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
5352 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
5353 return strcmp(p1, p2);
5354}
5355
5356/*
5357 * Find all help tags matching "arg", sort them and return in matches[], with
5358 * the number of matches in num_matches.
5359 * The matches will be sorted with a "best" match algorithm.
5360 * When "keep_lang" is TRUE try keeping the language of the current buffer.
5361 */
5362 int
5363find_help_tags(arg, num_matches, matches, keep_lang)
5364 char_u *arg;
5365 int *num_matches;
5366 char_u ***matches;
5367 int keep_lang;
5368{
5369 char_u *s, *d;
5370 int i;
5371 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005372 "/*", "/\\*", "\"*", "**",
5373 "/\\(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005374 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005375 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 "[count]", "[quotex]", "[range]",
5377 "[pattern]", "\\|", "\\%$"};
5378 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005379 "/star", "/\\\\star", "quotestar", "starstar",
5380 "/\\\\(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005382 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 "\\[count]", "\\[quotex]", "\\[range]",
5384 "\\[pattern]", "\\\\bar", "/\\\\%\\$"};
5385 int flags;
5386
5387 d = IObuff; /* assume IObuff is long enough! */
5388
5389 /*
5390 * Recognize a few exceptions to the rule. Some strings that contain '*'
5391 * with "star". Otherwise '*' is recognized as a wildcard.
5392 */
5393 for (i = sizeof(mtable) / sizeof(char *); --i >= 0; )
5394 if (STRCMP(arg, mtable[i]) == 0)
5395 {
5396 STRCPY(d, rtable[i]);
5397 break;
5398 }
5399
5400 if (i < 0) /* no match in table */
5401 {
5402 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
5403 * Also replace "\%^" and "\%(", they match every tag too.
5404 * Also "\zs", "\z1", etc.
5405 * Also "\@<", "\@=", "\@<=", etc.
5406 * And also "\_$" and "\_^". */
5407 if (arg[0] == '\\'
5408 && ((arg[1] != NUL && arg[2] == NUL)
5409 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
5410 && arg[2] != NUL)))
5411 {
5412 STRCPY(d, "/\\\\");
5413 STRCPY(d + 3, arg + 1);
5414 /* Check for "/\\_$", should be "/\\_\$" */
5415 if (d[3] == '_' && d[4] == '$')
5416 STRCPY(d + 4, "\\$");
5417 }
5418 else
5419 {
5420 /* replace "[:...:]" with "\[:...:]"; "[+...]" with "\[++...]" */
5421 if (arg[0] == '[' && (arg[1] == ':'
5422 || (arg[1] == '+' && arg[2] == '+')))
5423 *d++ = '\\';
5424
5425 for (s = arg; *s; ++s)
5426 {
5427 /*
5428 * Replace "|" with "bar" and '"' with "quote" to match the name of
5429 * the tags for these commands.
5430 * Replace "*" with ".*" and "?" with "." to match command line
5431 * completion.
5432 * Insert a backslash before '~', '$' and '.' to avoid their
5433 * special meaning.
5434 */
5435 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
5436 break;
5437 switch (*s)
5438 {
5439 case '|': STRCPY(d, "bar");
5440 d += 3;
5441 continue;
5442 case '"': STRCPY(d, "quote");
5443 d += 5;
5444 continue;
5445 case '*': *d++ = '.';
5446 break;
5447 case '?': *d++ = '.';
5448 continue;
5449 case '$':
5450 case '.':
5451 case '~': *d++ = '\\';
5452 break;
5453 }
5454
5455 /*
5456 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
5457 * ":help i_^_CTRL-D" work.
5458 * Insert '-' before and after "CTRL-X" when applicable.
5459 */
5460 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
5461 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
5462 {
5463 if (d > IObuff && d[-1] != '_')
5464 *d++ = '_'; /* prepend a '_' */
5465 STRCPY(d, "CTRL-");
5466 d += 5;
5467 if (*s < ' ')
5468 {
5469#ifdef EBCDIC
5470 *d++ = CtrlChar(*s);
5471#else
5472 *d++ = *s + '@';
5473#endif
5474 if (d[-1] == '\\')
5475 *d++ = '\\'; /* double a backslash */
5476 }
5477 else
5478 *d++ = *++s;
5479 if (s[1] != NUL && s[1] != '_')
5480 *d++ = '_'; /* append a '_' */
5481 continue;
5482 }
5483 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
5484 *d++ = '\\';
5485
5486 /*
5487 * Insert a backslash before a backslash after a slash, for search
5488 * pattern tags: "/\|" --> "/\\|".
5489 */
5490 else if (s[0] == '\\' && s[1] != '\\'
5491 && *arg == '/' && s == arg + 1)
5492 *d++ = '\\';
5493
5494 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
5495 * "CTRL-\_CTRL-N" */
5496 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
5497 {
5498 STRCPY(d, "CTRL-\\\\");
5499 d += 7;
5500 s += 6;
5501 }
5502
5503 *d++ = *s;
5504
5505 /*
5506 * If tag starts with ', toss everything after a second '. Fixes
5507 * CTRL-] on 'option'. (would include the trailing '.').
5508 */
5509 if (*s == '\'' && s > arg && *arg == '\'')
5510 break;
5511 }
5512 *d = NUL;
5513 }
5514 }
5515
5516 *matches = (char_u **)"";
5517 *num_matches = 0;
5518 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
5519 if (keep_lang)
5520 flags |= TAG_KEEP_LANG;
5521 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
5522 && *num_matches > 0)
5523 /* Sort the matches found on the heuristic number that is after the
5524 * tag name. */
5525 qsort((void *)*matches, (size_t)*num_matches,
5526 sizeof(char_u *), help_compare);
5527 return OK;
5528}
5529
5530/*
5531 * After reading a help file: May cleanup a help buffer when syntax
5532 * highlighting is not used.
5533 */
5534 void
5535fix_help_buffer()
5536{
5537 linenr_T lnum;
5538 char_u *line;
5539 int in_example = FALSE;
5540 int len;
5541 char_u *p;
5542 char_u *rt;
5543 int mustfree;
5544
5545 /* set filetype to "help". */
5546 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
5547
5548#ifdef FEAT_SYN_HL
5549 if (!syntax_present(curbuf))
5550#endif
5551 {
5552 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5553 {
5554 line = ml_get_buf(curbuf, lnum, FALSE);
5555 len = (int)STRLEN(line);
5556 if (in_example && len > 0 && !vim_iswhite(line[0]))
5557 {
5558 /* End of example: non-white or '<' in first column. */
5559 if (line[0] == '<')
5560 {
5561 /* blank-out a '<' in the first column */
5562 line = ml_get_buf(curbuf, lnum, TRUE);
5563 line[0] = ' ';
5564 }
5565 in_example = FALSE;
5566 }
5567 if (!in_example && len > 0)
5568 {
5569 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
5570 {
5571 /* blank-out a '>' in the last column (start of example) */
5572 line = ml_get_buf(curbuf, lnum, TRUE);
5573 line[len - 1] = ' ';
5574 in_example = TRUE;
5575 }
5576 else if (line[len - 1] == '~')
5577 {
5578 /* blank-out a '~' at the end of line (header marker) */
5579 line = ml_get_buf(curbuf, lnum, TRUE);
5580 line[len - 1] = ' ';
5581 }
5582 }
5583 }
5584 }
5585
5586 /*
5587 * In the "help.txt" file, add the locally added help files.
5588 * This uses the very first line in the help file.
5589 */
5590 if (fnamecmp(gettail(curbuf->b_fname), "help.txt") == 0)
5591 {
5592 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
5593 {
5594 line = ml_get_buf(curbuf, lnum, FALSE);
5595 if (strstr((char *)line, "*local-additions*") != NULL)
5596 {
5597 /* Go through all directories in 'runtimepath', skipping
5598 * $VIMRUNTIME. */
5599 p = p_rtp;
5600 while (*p != NUL)
5601 {
5602 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5603 mustfree = FALSE;
5604 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
5605 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
5606 {
5607 int fcount;
5608 char_u **fnames;
5609 FILE *fd;
5610 char_u *s;
5611 int fi;
5612#ifdef FEAT_MBYTE
5613 vimconv_T vc;
5614 char_u *cp;
5615#endif
5616
5617 /* Find all "doc/ *.txt" files in this directory. */
5618 add_pathsep(NameBuff);
5619 STRCAT(NameBuff, "doc/*.txt");
5620 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5621 &fnames, EW_FILE|EW_SILENT) == OK
5622 && fcount > 0)
5623 {
5624 for (fi = 0; fi < fcount; ++fi)
5625 {
5626 fd = fopen((char *)fnames[fi], "r");
5627 if (fd != NULL)
5628 {
5629 vim_fgets(IObuff, IOSIZE, fd);
5630 if (IObuff[0] == '*'
5631 && (s = vim_strchr(IObuff + 1, '*'))
5632 != NULL)
5633 {
5634#ifdef FEAT_MBYTE
5635 int this_utf = MAYBE;
5636#endif
5637 /* Change tag definition to a
5638 * reference and remove <CR>/<NL>. */
5639 IObuff[0] = '|';
5640 *s = '|';
5641 while (*s != NUL)
5642 {
5643 if (*s == '\r' || *s == '\n')
5644 *s = NUL;
5645#ifdef FEAT_MBYTE
5646 /* The text is utf-8 when a byte
5647 * above 127 is found and no
5648 * illegal byte sequence is found.
5649 */
5650 if (*s >= 0x80 && this_utf != FALSE)
5651 {
5652 int l;
5653
5654 this_utf = TRUE;
5655 l = utf_ptr2len_check(s);
5656 if (l == 1)
5657 this_utf = FALSE;
5658 s += l - 1;
5659 }
5660#endif
5661 ++s;
5662 }
5663#ifdef FEAT_MBYTE
5664 /* The help file is latin1 or utf-8;
5665 * conversion to the current
5666 * 'encoding' may be required. */
5667 vc.vc_type = CONV_NONE;
5668 convert_setup(&vc, (char_u *)(
5669 this_utf == TRUE ? "utf-8"
5670 : "latin1"), p_enc);
5671 if (vc.vc_type == CONV_NONE)
5672 /* No conversion needed. */
5673 cp = IObuff;
5674 else
5675 {
5676 /* Do the conversion. If it fails
5677 * use the unconverted text. */
5678 cp = string_convert(&vc, IObuff,
5679 NULL);
5680 if (cp == NULL)
5681 cp = IObuff;
5682 }
5683 convert_setup(&vc, NULL, NULL);
5684
5685 ml_append(lnum, cp, (colnr_T)0, FALSE);
5686 if (cp != IObuff)
5687 vim_free(cp);
5688#else
5689 ml_append(lnum, IObuff, (colnr_T)0,
5690 FALSE);
5691#endif
5692 ++lnum;
5693 }
5694 fclose(fd);
5695 }
5696 }
5697 FreeWild(fcount, fnames);
5698 }
5699 }
5700 if (mustfree)
5701 vim_free(rt);
5702 }
5703 break;
5704 }
5705 }
5706 }
5707}
5708
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005709/*
5710 * ":exusage"
5711 */
5712/*ARGSUSED*/
5713 void
5714ex_exusage(eap)
5715 exarg_T *eap;
5716{
5717 do_cmdline_cmd((char_u *)"help ex-cmd-index");
5718}
5719
5720/*
5721 * ":viusage"
5722 */
5723/*ARGSUSED*/
5724 void
5725ex_viusage(eap)
5726 exarg_T *eap;
5727{
5728 do_cmdline_cmd((char_u *)"help normal-index");
5729}
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#if defined(FEAT_EX_EXTRA) || defined(PROTO)
5732static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang));
5733
5734/*
5735 * ":helptags"
5736 */
5737 void
5738ex_helptags(eap)
5739 exarg_T *eap;
5740{
5741 garray_T ga;
5742 int i, j;
5743 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005744#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 char_u ext[5];
5748 char_u fname[8];
5749 int filecount;
5750 char_u **files;
5751
5752 if (!mch_isdir(eap->arg))
5753 {
5754 EMSG2(_("E150: Not a directory: %s"), eap->arg);
5755 return;
5756 }
5757
5758#ifdef FEAT_MULTI_LANG
5759 /* Get a list of all files in the directory. */
5760 STRCPY(NameBuff, eap->arg);
5761 add_pathsep(NameBuff);
5762 STRCAT(NameBuff, "*");
5763 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
5764 EW_FILE|EW_SILENT) == FAIL
5765 || filecount == 0)
5766 {
5767 EMSG2("E151: No match: %s", NameBuff);
5768 return;
5769 }
5770
5771 /* Go over all files in the directory to find out what languages are
5772 * present. */
5773 ga_init2(&ga, 1, 10);
5774 for (i = 0; i < filecount; ++i)
5775 {
5776 len = STRLEN(files[i]);
5777 if (len > 4)
5778 {
5779 if (STRICMP(files[i] + len - 4, ".txt") == 0)
5780 {
5781 /* ".txt" -> language "en" */
5782 lang[0] = 'e';
5783 lang[1] = 'n';
5784 }
5785 else if (files[i][len - 4] == '.'
5786 && ASCII_ISALPHA(files[i][len - 3])
5787 && ASCII_ISALPHA(files[i][len - 2])
5788 && TOLOWER_ASC(files[i][len - 1]) == 'x')
5789 {
5790 /* ".abx" -> language "ab" */
5791 lang[0] = TOLOWER_ASC(files[i][len - 3]);
5792 lang[1] = TOLOWER_ASC(files[i][len - 2]);
5793 }
5794 else
5795 continue;
5796
5797 /* Did we find this language already? */
5798 for (j = 0; j < ga.ga_len; j += 2)
5799 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
5800 break;
5801 if (j == ga.ga_len)
5802 {
5803 /* New language, add it. */
5804 if (ga_grow(&ga, 2) == FAIL)
5805 break;
5806 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
5807 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 }
5810 }
5811
5812 /*
5813 * Loop over the found languages to generate a tags file for each one.
5814 */
5815 for (j = 0; j < ga.ga_len; j += 2)
5816 {
5817 STRCPY(fname, "tags-xx");
5818 fname[5] = ((char_u *)ga.ga_data)[j];
5819 fname[6] = ((char_u *)ga.ga_data)[j + 1];
5820 if (fname[5] == 'e' && fname[6] == 'n')
5821 {
5822 /* English is an exception: use ".txt" and "tags". */
5823 fname[4] = NUL;
5824 STRCPY(ext, ".txt");
5825 }
5826 else
5827 {
5828 /* Language "ab" uses ".abx" and "tags-ab". */
5829 STRCPY(ext, ".xxx");
5830 ext[1] = fname[5];
5831 ext[2] = fname[6];
5832 }
5833 helptags_one(eap->arg, ext, fname);
5834 }
5835
5836 ga_clear(&ga);
5837 FreeWild(filecount, files);
5838
5839#else
5840 /* No language support, just use "*.txt" and "tags". */
5841 helptags_one(eap->arg, (char_u *)".txt", (char_u *)"tags");
5842#endif
5843}
5844
5845 static void
5846helptags_one(dir, ext, tagfname)
5847 char_u *dir; /* doc directory */
5848 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
5849 char_u *tagfname; /* "tags" for English, "tags-it" for Italian. */
5850{
5851 FILE *fd_tags;
5852 FILE *fd;
5853 garray_T ga;
5854 int filecount;
5855 char_u **files;
5856 char_u *p1, *p2;
5857 int fi;
5858 char_u *s;
5859 int i;
5860 char_u *fname;
5861# ifdef FEAT_MBYTE
5862 int utf8 = MAYBE;
5863 int this_utf8;
5864 int firstline;
5865 int mix = FALSE; /* detected mixed encodings */
5866# endif
5867
5868 /*
5869 * Find all *.txt files.
5870 */
5871 STRCPY(NameBuff, dir);
5872 add_pathsep(NameBuff);
5873 STRCAT(NameBuff, "*");
5874 STRCAT(NameBuff, ext);
5875 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
5876 EW_FILE|EW_SILENT) == FAIL
5877 || filecount == 0)
5878 {
5879 if (!got_int)
5880 EMSG2("E151: No match: %s", NameBuff);
5881 return;
5882 }
5883
5884 /*
5885 * Open the tags file for writing.
5886 * Do this before scanning through all the files.
5887 */
5888 STRCPY(NameBuff, dir);
5889 add_pathsep(NameBuff);
5890 STRCAT(NameBuff, tagfname);
5891 fd_tags = fopen((char *)NameBuff, "w");
5892 if (fd_tags == NULL)
5893 {
5894 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
5895 FreeWild(filecount, files);
5896 return;
5897 }
5898
5899 /*
5900 * If generating tags for "$VIMRUNTIME/doc" add the "help-tags" tag.
5901 */
5902 ga_init2(&ga, (int)sizeof(char_u *), 100);
5903 if (fullpathcmp((char_u *)"$VIMRUNTIME/doc", dir, FALSE) == FPC_SAME)
5904 {
5905 if (ga_grow(&ga, 1) == FAIL)
5906 got_int = TRUE;
5907 else
5908 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005909 s = alloc(18 + STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (s == NULL)
5911 got_int = TRUE;
5912 else
5913 {
5914 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
5915 ((char_u **)ga.ga_data)[ga.ga_len] = s;
5916 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
5918 }
5919 }
5920
5921 /*
5922 * Go over all the files and extract the tags.
5923 */
5924 for (fi = 0; fi < filecount && !got_int; ++fi)
5925 {
5926 fd = fopen((char *)files[fi], "r");
5927 if (fd == NULL)
5928 {
5929 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
5930 continue;
5931 }
5932 fname = gettail(files[fi]);
5933
5934# ifdef FEAT_MBYTE
5935 firstline = TRUE;
5936# endif
5937 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5938 {
5939# ifdef FEAT_MBYTE
5940 if (firstline)
5941 {
5942 /* Detect utf-8 file by a non-ASCII char in the first line. */
5943 this_utf8 = MAYBE;
5944 for (s = IObuff; *s != NUL; ++s)
5945 if (*s >= 0x80)
5946 {
5947 int l;
5948
5949 this_utf8 = TRUE;
5950 l = utf_ptr2len_check(s);
5951 if (l == 1)
5952 {
5953 /* Illegal UTF-8 byte sequence. */
5954 this_utf8 = FALSE;
5955 break;
5956 }
5957 s += l - 1;
5958 }
5959 if (this_utf8 == MAYBE) /* only ASCII characters found */
5960 this_utf8 = FALSE;
5961 if (utf8 == MAYBE) /* first file */
5962 utf8 = this_utf8;
5963 else if (utf8 != this_utf8)
5964 {
5965 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
5966 mix = !got_int;
5967 got_int = TRUE;
5968 }
5969 firstline = FALSE;
5970 }
5971# endif
5972 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
5973 while (p1 != NULL)
5974 {
5975 p2 = vim_strchr(p1 + 1, '*'); /* find second '*' */
5976 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
5977 {
5978 for (s = p1 + 1; s < p2; ++s)
5979 if (*s == ' ' || *s == '\t' || *s == '|')
5980 break;
5981
5982 /*
5983 * Only accept a *tag* when it consists of valid
5984 * characters, there is white space before it and is
5985 * followed by a white character or end-of-line.
5986 */
5987 if (s == p2
5988 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
5989 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
5990 || s[1] == '\0'))
5991 {
5992 *p2 = '\0';
5993 ++p1;
5994 if (ga_grow(&ga, 1) == FAIL)
5995 {
5996 got_int = TRUE;
5997 break;
5998 }
5999 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6000 if (s == NULL)
6001 {
6002 got_int = TRUE;
6003 break;
6004 }
6005 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6006 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 sprintf((char *)s, "%s\t%s", p1, fname);
6008
6009 /* find next '*' */
6010 p2 = vim_strchr(p2 + 1, '*');
6011 }
6012 }
6013 p1 = p2;
6014 }
6015 line_breakcheck();
6016 }
6017
6018 fclose(fd);
6019 }
6020
6021 FreeWild(filecount, files);
6022
6023 if (!got_int)
6024 {
6025 /*
6026 * Sort the tags.
6027 */
6028 sort_strings((char_u **)ga.ga_data, ga.ga_len);
6029
6030 /*
6031 * Check for duplicates.
6032 */
6033 for (i = 1; i < ga.ga_len; ++i)
6034 {
6035 p1 = ((char_u **)ga.ga_data)[i - 1];
6036 p2 = ((char_u **)ga.ga_data)[i];
6037 while (*p1 == *p2)
6038 {
6039 if (*p2 == '\t')
6040 {
6041 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006042 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6044 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6045 EMSG(NameBuff);
6046 *p2 = '\t';
6047 break;
6048 }
6049 ++p1;
6050 ++p2;
6051 }
6052 }
6053
6054# ifdef FEAT_MBYTE
6055 if (utf8 == TRUE)
6056 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6057# endif
6058
6059 /*
6060 * Write the tags into the file.
6061 */
6062 for (i = 0; i < ga.ga_len; ++i)
6063 {
6064 s = ((char_u **)ga.ga_data)[i];
6065 if (STRNCMP(s, "help-tags", 9) == 0)
6066 /* help-tags entry was added in formatted form */
6067 fprintf(fd_tags, (char *)s);
6068 else
6069 {
6070 fprintf(fd_tags, "%s\t/*", s);
6071 for (p1 = s; *p1 != '\t'; ++p1)
6072 {
6073 /* insert backslash before '\\' and '/' */
6074 if (*p1 == '\\' || *p1 == '/')
6075 putc('\\', fd_tags);
6076 putc(*p1, fd_tags);
6077 }
6078 fprintf(fd_tags, "*\n");
6079 }
6080 }
6081 }
6082#ifdef FEAT_MBYTE
6083 if (mix)
6084 got_int = FALSE; /* continue with other languages */
6085#endif
6086
6087 for (i = 0; i < ga.ga_len; ++i)
6088 vim_free(((char_u **)ga.ga_data)[i]);
6089 ga_clear(&ga);
6090 fclose(fd_tags); /* there is no check for an error... */
6091}
6092#endif
6093
6094#if defined(FEAT_SIGNS) || defined(PROTO)
6095
6096/*
6097 * Struct to hold the sign properties.
6098 */
6099typedef struct sign sign_T;
6100
6101struct sign
6102{
6103 sign_T *sn_next; /* next sign in list */
6104 int sn_typenr; /* type number of sign (negative if not equal
6105 to name) */
6106 char_u *sn_name; /* name of sign */
6107 char_u *sn_icon; /* name of pixmap */
6108#ifdef FEAT_SIGN_ICONS
6109 void *sn_image; /* icon image */
6110#endif
6111 char_u *sn_text; /* text used instead of pixmap */
6112 int sn_line_hl; /* highlight ID for line */
6113 int sn_text_hl; /* highlight ID for text */
6114};
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116static sign_T *first_sign = NULL;
6117static int last_sign_typenr = MAX_TYPENR; /* is decremented */
6118
6119static void sign_list_defined __ARGS((sign_T *sp));
6120
6121/*
6122 * ":sign" command
6123 */
6124 void
6125ex_sign(eap)
6126 exarg_T *eap;
6127{
6128 char_u *arg = eap->arg;
6129 char_u *p;
6130 int idx;
6131 sign_T *sp;
6132 sign_T *sp_prev;
6133 buf_T *buf;
6134 static char *cmds[] = {
6135 "define",
6136#define SIGNCMD_DEFINE 0
6137 "undefine",
6138#define SIGNCMD_UNDEFINE 1
6139 "list",
6140#define SIGNCMD_LIST 2
6141 "place",
6142#define SIGNCMD_PLACE 3
6143 "unplace",
6144#define SIGNCMD_UNPLACE 4
6145 "jump",
6146#define SIGNCMD_JUMP 5
6147#define SIGNCMD_LAST 6
6148 };
6149
6150 /* Parse the subcommand. */
6151 p = skiptowhite(arg);
6152 if (*p != NUL)
6153 *p++ = NUL;
6154 for (idx = 0; ; ++idx)
6155 {
6156 if (idx == SIGNCMD_LAST)
6157 {
6158 EMSG2(_("E160: Unknown sign command: %s"), arg);
6159 return;
6160 }
6161 if (STRCMP(arg, cmds[idx]) == 0)
6162 break;
6163 }
6164 arg = skipwhite(p);
6165
6166 if (idx <= SIGNCMD_LIST)
6167 {
6168 /*
6169 * Define, undefine or list signs.
6170 */
6171 if (idx == SIGNCMD_LIST && *arg == NUL)
6172 {
6173 /* ":sign list": list all defined signs */
6174 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6175 sign_list_defined(sp);
6176 }
6177 else if (*arg == NUL)
6178 EMSG(_("E156: Missing sign name"));
6179 else
6180 {
6181 p = skiptowhite(arg);
6182 if (*p != NUL)
6183 *p++ = NUL;
6184 sp_prev = NULL;
6185 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6186 {
6187 if (STRCMP(sp->sn_name, arg) == 0)
6188 break;
6189 sp_prev = sp;
6190 }
6191 if (idx == SIGNCMD_DEFINE)
6192 {
6193 /* ":sign define {name} ...": define a sign */
6194 if (sp == NULL)
6195 {
6196 /* Allocate a new sign. */
6197 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
6198 if (sp == NULL)
6199 return;
6200 if (sp_prev == NULL)
6201 first_sign = sp;
6202 else
6203 sp_prev->sn_next = sp;
6204 sp->sn_name = vim_strnsave(arg, (int)(p - arg));
6205
6206 /* If the name is a number use that for the typenr,
6207 * otherwise use a negative number. */
6208 if (VIM_ISDIGIT(*arg))
6209 sp->sn_typenr = atoi((char *)arg);
6210 else
6211 {
6212 sign_T *lp;
6213 int start = last_sign_typenr;
6214
6215 for (lp = first_sign; lp != NULL; lp = lp->sn_next)
6216 {
6217 if (lp->sn_typenr == last_sign_typenr)
6218 {
6219 --last_sign_typenr;
6220 if (last_sign_typenr == 0)
6221 last_sign_typenr = MAX_TYPENR;
6222 if (last_sign_typenr == start)
6223 {
6224 EMSG(_("E612: Too many signs defined"));
6225 return;
6226 }
6227 lp = first_sign;
6228 continue;
6229 }
6230 }
6231
6232 sp->sn_typenr = last_sign_typenr--;
6233 if (last_sign_typenr == 0)
6234 last_sign_typenr = MAX_TYPENR; /* wrap around */
6235 }
6236 }
6237
6238 /* set values for a defined sign. */
6239 for (;;)
6240 {
6241 arg = skipwhite(p);
6242 if (*arg == NUL)
6243 break;
6244 p = skiptowhite_esc(arg);
6245 if (STRNCMP(arg, "icon=", 5) == 0)
6246 {
6247 arg += 5;
6248 vim_free(sp->sn_icon);
6249 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
6250 backslash_halve(sp->sn_icon);
6251#ifdef FEAT_SIGN_ICONS
6252 if (gui.in_use)
6253 {
6254 out_flush();
6255 if (sp->sn_image != NULL)
6256 gui_mch_destroy_sign(sp->sn_image);
6257 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6258 }
6259#endif
6260 }
6261 else if (STRNCMP(arg, "text=", 5) == 0)
6262 {
6263 char_u *s;
6264 int cells;
6265 int len;
6266
6267 arg += 5;
6268#ifdef FEAT_MBYTE
6269 /* Count cells and check for non-printable chars */
6270 if (has_mbyte)
6271 {
6272 cells = 0;
6273 for (s = arg; s < p; s += (*mb_ptr2len_check)(s))
6274 {
6275 if (!vim_isprintc((*mb_ptr2char)(s)))
6276 break;
6277 cells += (*mb_ptr2cells)(s);
6278 }
6279 }
6280 else
6281#endif
6282 {
6283 for (s = arg; s < p; ++s)
6284 if (!vim_isprintc(*s))
6285 break;
6286 cells = s - arg;
6287 }
6288 /* Currently must be one or two display cells */
6289 if (s != p || cells < 1 || cells > 2)
6290 {
6291 *p = NUL;
6292 EMSG2(_("E239: Invalid sign text: %s"), arg);
6293 return;
6294 }
6295
6296 vim_free(sp->sn_text);
6297 /* Allocate one byte more if we need to pad up
6298 * with a space. */
6299 len = p - arg + ((cells == 1) ? 1 : 0);
6300 sp->sn_text = vim_strnsave(arg, len);
6301
6302 if (sp->sn_text != NULL && cells == 1)
6303 STRCPY(sp->sn_text + len - 1, " ");
6304 }
6305 else if (STRNCMP(arg, "linehl=", 7) == 0)
6306 {
6307 arg += 7;
6308 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
6309 }
6310 else if (STRNCMP(arg, "texthl=", 7) == 0)
6311 {
6312 arg += 7;
6313 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
6314 }
6315 else
6316 {
6317 EMSG2(_(e_invarg2), arg);
6318 return;
6319 }
6320 }
6321 }
6322 else if (sp == NULL)
6323 EMSG2(_("E155: Unknown sign: %s"), arg);
6324 else if (idx == SIGNCMD_LIST)
6325 /* ":sign list {name}" */
6326 sign_list_defined(sp);
6327 else
6328 {
6329 /* ":sign undefine {name}" */
6330 vim_free(sp->sn_name);
6331 vim_free(sp->sn_icon);
6332#ifdef FEAT_SIGN_ICONS
6333 if (sp->sn_image != NULL)
6334 {
6335 out_flush();
6336 gui_mch_destroy_sign(sp->sn_image);
6337 }
6338#endif
6339 vim_free(sp->sn_text);
6340 if (sp_prev == NULL)
6341 first_sign = sp->sn_next;
6342 else
6343 sp_prev->sn_next = sp->sn_next;
6344 vim_free(sp);
6345 }
6346 }
6347 }
6348 else
6349 {
6350 int id = -1;
6351 linenr_T lnum = -1;
6352 char_u *sign_name = NULL;
6353 char_u *arg1;
6354
6355 if (*arg == NUL)
6356 {
6357 if (idx == SIGNCMD_PLACE)
6358 {
6359 /* ":sign place": list placed signs in all buffers */
6360 sign_list_placed(NULL);
6361 }
6362 else if (idx == SIGNCMD_UNPLACE)
6363 {
6364 /* ":sign unplace": remove placed sign at cursor */
6365 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
6366 if (id > 0)
6367 {
6368 buf_delsign(curwin->w_buffer, id);
6369 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
6370 }
6371 else
6372 EMSG(_("E159: Missing sign number"));
6373 }
6374 else
6375 EMSG(_(e_argreq));
6376 return;
6377 }
6378
6379 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
6380 {
6381 /* ":sign unplace *": remove all placed signs */
6382 buf_delete_all_signs();
6383 return;
6384 }
6385
6386 /* first arg could be placed sign id */
6387 arg1 = arg;
6388 if (VIM_ISDIGIT(*arg))
6389 {
6390 id = getdigits(&arg);
6391 if (!vim_iswhite(*arg) && *arg != NUL)
6392 {
6393 id = -1;
6394 arg = arg1;
6395 }
6396 else
6397 {
6398 arg = skipwhite(arg);
6399 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
6400 {
6401 /* ":sign unplace {id}": remove placed sign by number */
6402 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6403 if ((lnum = buf_delsign(buf, id)) != 0)
6404 update_debug_sign(buf, lnum);
6405 return;
6406 }
6407 }
6408 }
6409
6410 /*
6411 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
6412 * Leave "arg" pointing to {fname}.
6413 */
6414 for (;;)
6415 {
6416 if (STRNCMP(arg, "line=", 5) == 0)
6417 {
6418 arg += 5;
6419 lnum = atoi((char *)arg);
6420 arg = skiptowhite(arg);
6421 }
6422 else if (STRNCMP(arg, "name=", 5) == 0)
6423 {
6424 arg += 5;
6425 sign_name = arg;
6426 arg = skiptowhite(arg);
6427 if (*arg != NUL)
6428 *arg++ = NUL;
6429 }
6430 else if (STRNCMP(arg, "file=", 5) == 0)
6431 {
6432 arg += 5;
6433 buf = buflist_findname(arg);
6434 break;
6435 }
6436 else if (STRNCMP(arg, "buffer=", 7) == 0)
6437 {
6438 arg += 7;
6439 buf = buflist_findnr((int)getdigits(&arg));
6440 if (*skipwhite(arg) != NUL)
6441 EMSG(_(e_trailing));
6442 break;
6443 }
6444 else
6445 {
6446 EMSG(_(e_invarg));
6447 return;
6448 }
6449 arg = skipwhite(arg);
6450 }
6451
6452 if (buf == NULL)
6453 {
6454 EMSG2(_("E158: Invalid buffer name: %s"), arg);
6455 }
6456 else if (id <= 0)
6457 {
6458 if (lnum >= 0 || sign_name != NULL)
6459 EMSG(_(e_invarg));
6460 else
6461 /* ":sign place file={fname}": list placed signs in one file */
6462 sign_list_placed(buf);
6463 }
6464 else if (idx == SIGNCMD_JUMP)
6465 {
6466 /* ":sign jump {id} file={fname}" */
6467 if (lnum >= 0 || sign_name != NULL)
6468 EMSG(_(e_invarg));
6469 else if ((lnum = buf_findsign(buf, id)) > 0)
6470 { /* goto a sign ... */
6471 if (buf_jump_open_win(buf) != NULL)
6472 { /* ... in a current window */
6473 curwin->w_cursor.lnum = lnum;
6474 check_cursor_lnum();
6475 beginline(BL_WHITE);
6476 }
6477 else
6478 { /* ... not currently in a window */
6479 char_u *cmd;
6480
6481 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
6482 if (cmd == NULL)
6483 return;
6484 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
6485 do_cmdline_cmd(cmd);
6486 vim_free(cmd);
6487 }
6488#ifdef FEAT_FOLDING
6489 foldOpenCursor();
6490#endif
6491 }
6492 else
6493 EMSGN(_("E157: Invalid sign ID: %ld"), id);
6494 }
6495 else if (idx == SIGNCMD_UNPLACE)
6496 {
6497 /* ":sign unplace {id} file={fname}" */
6498 if (lnum >= 0 || sign_name != NULL)
6499 EMSG(_(e_invarg));
6500 else
6501 {
6502 lnum = buf_delsign(buf, id);
6503 update_debug_sign(buf, lnum);
6504 }
6505 }
6506 /* idx == SIGNCMD_PLACE */
6507 else if (sign_name != NULL)
6508 {
6509 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6510 if (STRCMP(sp->sn_name, sign_name) == 0)
6511 break;
6512 if (sp == NULL)
6513 {
6514 EMSG2(_("E155: Unknown sign: %s"), sign_name);
6515 return;
6516 }
6517 if (lnum > 0)
6518 /* ":sign place {id} line={lnum} name={name} file={fname}":
6519 * place a sign */
6520 buf_addsign(buf, id, lnum, sp->sn_typenr);
6521 else
6522 /* ":sign place {id} file={fname}": change sign type */
6523 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
6524 update_debug_sign(buf, lnum);
6525 }
6526 else
6527 EMSG(_(e_invarg));
6528 }
6529}
6530
6531#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6532/*
6533 * Allocate the icons. Called when the GUI has started. Allows defining
6534 * signs before it starts.
6535 */
6536 void
6537sign_gui_started()
6538{
6539 sign_T *sp;
6540
6541 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6542 if (sp->sn_icon != NULL)
6543 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6544}
6545#endif
6546
6547/*
6548 * List one sign.
6549 */
6550 static void
6551sign_list_defined(sp)
6552 sign_T *sp;
6553{
6554 char_u *p;
6555
Bram Moolenaar555b2802005-05-19 21:08:39 +00006556 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (sp->sn_icon != NULL)
6558 {
6559 MSG_PUTS(" icon=");
6560 msg_outtrans(sp->sn_icon);
6561#ifdef FEAT_SIGN_ICONS
6562 if (sp->sn_image == NULL)
6563 MSG_PUTS(_(" (NOT FOUND)"));
6564#else
6565 MSG_PUTS(_(" (not supported)"));
6566#endif
6567 }
6568 if (sp->sn_text != NULL)
6569 {
6570 MSG_PUTS(" text=");
6571 msg_outtrans(sp->sn_text);
6572 }
6573 if (sp->sn_line_hl > 0)
6574 {
6575 MSG_PUTS(" linehl=");
6576 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
6577 if (p == NULL)
6578 MSG_PUTS("NONE");
6579 else
6580 msg_puts(p);
6581 }
6582 if (sp->sn_text_hl > 0)
6583 {
6584 MSG_PUTS(" texthl=");
6585 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
6586 if (p == NULL)
6587 MSG_PUTS("NONE");
6588 else
6589 msg_puts(p);
6590 }
6591}
6592
6593/*
6594 * Get highlighting attribute for sign "typenr".
6595 * If "line" is TRUE: line highl, if FALSE: text highl.
6596 */
6597 int
6598sign_get_attr(typenr, line)
6599 int typenr;
6600 int line;
6601{
6602 sign_T *sp;
6603
6604 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6605 if (sp->sn_typenr == typenr)
6606 {
6607 if (line)
6608 {
6609 if (sp->sn_line_hl > 0)
6610 return syn_id2attr(sp->sn_line_hl);
6611 }
6612 else
6613 {
6614 if (sp->sn_text_hl > 0)
6615 return syn_id2attr(sp->sn_text_hl);
6616 }
6617 break;
6618 }
6619 return 0;
6620}
6621
6622/*
6623 * Get text mark for sign "typenr".
6624 * Returns NULL if there isn't one.
6625 */
6626 char_u *
6627sign_get_text(typenr)
6628 int typenr;
6629{
6630 sign_T *sp;
6631
6632 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6633 if (sp->sn_typenr == typenr)
6634 return sp->sn_text;
6635 return NULL;
6636}
6637
6638#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6639 void *
6640sign_get_image(typenr)
6641 int typenr; /* the attribute which may have a sign */
6642{
6643 sign_T *sp;
6644
6645 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6646 if (sp->sn_typenr == typenr)
6647 return sp->sn_image;
6648 return NULL;
6649}
6650#endif
6651
6652/*
6653 * Get the name of a sign by its typenr.
6654 */
6655 char_u *
6656sign_typenr2name(typenr)
6657 int typenr;
6658{
6659 sign_T *sp;
6660
6661 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6662 if (sp->sn_typenr == typenr)
6663 return sp->sn_name;
6664 return (char_u *)_("[Deleted]");
6665}
6666
6667#endif
6668
6669#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
6670/*
6671 * ":drop"
6672 * Opens the first argument in a window. When there are two or more arguments
6673 * the argument list is redefined.
6674 */
6675 void
6676ex_drop(eap)
6677 exarg_T *eap;
6678{
6679 int split = FALSE;
6680 int incurwin = FALSE;
6681 char_u *arg;
6682 char_u *first = NULL;
6683 win_T *wp;
6684 buf_T *buf;
6685
6686 /*
6687 * Check if the first argument is already being edited in a window. If
6688 * so, jump to that window.
6689 * We would actually need to check all arguments, but that's complicated
6690 * and mostly only one file is dropped.
6691 * This also ignores wildcards, since it is very unlikely the user is
6692 * editing a file name with a wildcard character.
6693 */
6694 arg = vim_strsave(eap->arg);
6695 if (arg != NULL)
6696 {
6697 /* Get the first argument, remove quotes, make it a full path. */
6698 first = fix_fname(arg);
6699 if (first != NULL)
6700 {
6701 buf = buflist_findname(first);
6702 FOR_ALL_WINDOWS(wp)
6703 {
6704 if (wp->w_buffer == buf)
6705 {
6706 incurwin = TRUE;
6707# ifdef FEAT_WINDOWS
6708 win_enter(wp, TRUE);
6709 break;
6710# endif
6711 }
6712 }
6713 vim_free(first);
6714
6715 if (incurwin)
6716 {
6717 /* Already editing the file. Redefine the argument list. */
6718 set_arglist(eap->arg);
6719 curwin->w_arg_idx = 0;
6720 vim_free(arg);
6721 return;
6722 }
6723 }
6724 vim_free(arg);
6725 }
6726
6727 /*
6728 * Check whether the current buffer is changed. If so, we will need
6729 * to split the current window or data could be lost.
6730 * Skip the check if the 'hidden' option is set, as in this case the
6731 * buffer won't be lost.
6732 */
6733 if (!P_HID(curbuf))
6734 {
6735# ifdef FEAT_WINDOWS
6736 ++emsg_off;
6737# endif
6738 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6739# ifdef FEAT_WINDOWS
6740 --emsg_off;
6741# else
6742 if (split)
6743 return;
6744# endif
6745 }
6746
6747 /* Fake a ":snext" or ":next" command, redefine the arglist. */
6748 if (split)
6749 {
6750 eap->cmdidx = CMD_snext;
6751 eap->cmd[0] = 's';
6752 }
6753 else
6754 eap->cmdidx = CMD_next;
6755 ex_next(eap);
6756}
6757#endif