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