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