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