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