blob: 074abfdfb785077d21bc47812a4379a71531ef81 [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
Bram Moolenaar362e1a32006-03-06 23:29:24 +000055 int cc[MAX_MCO];
56 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 int len;
58
59 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000060 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 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
Bram Moolenaar362e1a32006-03-06 23:29:24 +000098 c = cc[ci++];
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
100 }
101
102#ifdef FEAT_MBYTE
103 /* Repeat for combining characters. */
104 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
105 {
106 len = (int)STRLEN(IObuff);
107 /* This assumes every multi-byte char is printable... */
108 if (len > 0)
109 IObuff[len++] = ' ';
110 IObuff[len++] = '<';
111 if (utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000112# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 )
116 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000117 len += (*mb_char2bytes)(c, IObuff + len);
118 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
120 : _("> %d, Hex %08x, Octal %o"), c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000121 if (ci == MAX_MCO)
122 break;
123 c = cc[ci++];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 }
125#endif
126
127 msg(IObuff);
128}
129
130#if defined(FEAT_EX_EXTRA) || defined(PROTO)
131/*
132 * ":left", ":center" and ":right": align text.
133 */
134 void
135ex_align(eap)
136 exarg_T *eap;
137{
138 pos_T save_curpos;
139 int len;
140 int indent = 0;
141 int new_indent;
142 int has_tab;
143 int width;
144
145#ifdef FEAT_RIGHTLEFT
146 if (curwin->w_p_rl)
147 {
148 /* switch left and right aligning */
149 if (eap->cmdidx == CMD_right)
150 eap->cmdidx = CMD_left;
151 else if (eap->cmdidx == CMD_left)
152 eap->cmdidx = CMD_right;
153 }
154#endif
155
156 width = atoi((char *)eap->arg);
157 save_curpos = curwin->w_cursor;
158 if (eap->cmdidx == CMD_left) /* width is used for new indent */
159 {
160 if (width >= 0)
161 indent = width;
162 }
163 else
164 {
165 /*
166 * if 'textwidth' set, use it
167 * else if 'wrapmargin' set, use it
168 * if invalid value, use 80
169 */
170 if (width <= 0)
171 width = curbuf->b_p_tw;
172 if (width == 0 && curbuf->b_p_wm > 0)
173 width = W_WIDTH(curwin) - curbuf->b_p_wm;
174 if (width <= 0)
175 width = 80;
176 }
177
178 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
179 return;
180
181 for (curwin->w_cursor.lnum = eap->line1;
182 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
183 {
184 if (eap->cmdidx == CMD_left) /* left align */
185 new_indent = indent;
186 else
187 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000188 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 len = linelen(eap->cmdidx == CMD_right ? &has_tab
190 : NULL) - get_indent();
191
192 if (len <= 0) /* skip blank lines */
193 continue;
194
195 if (eap->cmdidx == CMD_center)
196 new_indent = (width - len) / 2;
197 else
198 {
199 new_indent = width - len; /* right align */
200
201 /*
202 * Make sure that embedded TABs don't make the text go too far
203 * to the right.
204 */
205 if (has_tab)
206 while (new_indent > 0)
207 {
208 (void)set_indent(new_indent, 0);
209 if (linelen(NULL) <= width)
210 {
211 /*
212 * Now try to move the line as much as possible to
213 * the right. Stop when it moves too far.
214 */
215 do
216 (void)set_indent(++new_indent, 0);
217 while (linelen(NULL) <= width);
218 --new_indent;
219 break;
220 }
221 --new_indent;
222 }
223 }
224 }
225 if (new_indent < 0)
226 new_indent = 0;
227 (void)set_indent(new_indent, 0); /* set indent */
228 }
229 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
230 curwin->w_cursor = save_curpos;
231 beginline(BL_WHITE | BL_FIX);
232}
233
234/*
235 * Get the length of the current line, excluding trailing white space.
236 */
237 static int
238linelen(has_tab)
239 int *has_tab;
240{
241 char_u *line;
242 char_u *first;
243 char_u *last;
244 int save;
245 int len;
246
247 /* find the first non-blank character */
248 line = ml_get_curline();
249 first = skipwhite(line);
250
251 /* find the character after the last non-blank character */
252 for (last = first + STRLEN(first);
253 last > first && vim_iswhite(last[-1]); --last)
254 ;
255 save = *last;
256 *last = NUL;
257 len = linetabsize(line); /* get line length */
258 if (has_tab != NULL) /* check for embedded TAB */
259 *has_tab = (vim_strrchr(first, TAB) != NULL);
260 *last = save;
261
262 return len;
263}
264
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000265/* Buffer for two lines used during sorting. They are allocated to
266 * contain the longest line being sorted. */
267static char_u *sortbuf1;
268static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000269
270static int sort_ic; /* ignore case */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000271static int sort_nr; /* sort on number */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000272static int sort_rx; /* sort on regex instead of skipping it */
273
274static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000275
276/* Struct to store info to be sorted. */
277typedef struct
278{
279 linenr_T lnum; /* line number */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000280 long start_col_nr; /* starting column number or number */
281 long end_col_nr; /* ending column number */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000282} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000283
284static int
285#ifdef __BORLANDC__
286_RTLENTRYF
287#endif
288sort_compare __ARGS((const void *s1, const void *s2));
289
290 static int
291#ifdef __BORLANDC__
292_RTLENTRYF
293#endif
294sort_compare(s1, s2)
295 const void *s1;
296 const void *s2;
297{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000298 sorti_T l1 = *(sorti_T *)s1;
299 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000300 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000301
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000302 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000303 * if we return 0 every time, qsort will assume it's done sorting and
304 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000305 if (sort_abort)
306 return 0;
307 fast_breakcheck();
308 if (got_int)
309 sort_abort = TRUE;
310
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000311 /* When sorting numbers "start_col_nr" is the number, not the column
312 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000313 if (sort_nr)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000314 result = l1.start_col_nr - l2.start_col_nr;
315 else
316 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000317 /* We need to copy one line into "sortbuf1", because there is no
318 * guarantee that the first pointer becomes invalid when obtaining the
319 * second one. */
320 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.start_col_nr,
321 l1.end_col_nr - l1.start_col_nr + 1);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000322 sortbuf1[l1.end_col_nr - l1.start_col_nr] = 0;
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000323 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.start_col_nr,
324 l2.end_col_nr - l2.start_col_nr + 1);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000325 sortbuf2[l2.end_col_nr - l2.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000326
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000327 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
328 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000330
331 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000333 return (int)(l1.lnum - l2.lnum);
334 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000335}
336
337/*
338 * ":sort".
339 */
340 void
341ex_sort(eap)
342 exarg_T *eap;
343{
344 regmatch_T regmatch;
345 int len;
346 linenr_T lnum;
347 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000348 sorti_T *nrs;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000349 size_t count = eap->line2 - eap->line1 + 1;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000350 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000351 char_u *p;
352 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 char_u *s2;
354 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000355 int unique = FALSE;
356 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000357 colnr_T start_col;
358 colnr_T end_col;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000359 int sort_oct; /* sort on octal number */
360 int sort_hex; /* sort on hex number */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000361
362 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
363 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000364 sortbuf1 = NULL;
365 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000366 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000367 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000368 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000369 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000370
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000371 sort_abort = sort_ic = sort_rx = sort_nr = sort_oct = sort_hex = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000372
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000373 for (p = eap->arg; *p != NUL; ++p)
374 {
375 if (vim_iswhite(*p))
376 ;
377 else if (*p == 'i')
378 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000379 else if (*p == 'r')
380 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000381 else if (*p == 'n')
382 sort_nr = 2;
383 else if (*p == 'o')
384 sort_oct = 2;
385 else if (*p == 'x')
386 sort_hex = 2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000387 else if (*p == 'u')
388 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000389 else if (*p == '"') /* comment start */
390 break;
391 else if (check_nextcmd(p) != NULL)
392 {
393 eap->nextcmd = check_nextcmd(p);
394 break;
395 }
396 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000397 {
398 s = skip_regexp(p + 1, *p, TRUE, NULL);
399 if (*s != *p)
400 {
401 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000402 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000403 }
404 *s = NUL;
405 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
406 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000407 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000408 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000409 regmatch.rm_ic = p_ic;
410 }
411 else
412 {
413 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000414 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000415 }
416 }
417
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000418 /* Can only have one of 'n', 'o' and 'x'. */
419 if (sort_nr + sort_oct + sort_hex > 2)
420 {
421 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000422 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000423 }
424
425 /* From here on "sort_nr" is used as a flag for any number sorting. */
426 sort_nr += sort_oct + sort_hex;
427
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000428 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000429 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000430 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000431 * When sorting on strings "start_col_nr" is the offset in the line, for
432 * numbers sorting it's the number to sort on. This means the pattern
433 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000434 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000435 */
436 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
437 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000438 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000439 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000440 if (maxlen < len)
441 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000442
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000443 start_col = 0;
444 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000445 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000446 {
447 if (sort_rx)
448 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000449 start_col = (colnr_T)(regmatch.startp[0] - s);
450 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000451 }
452 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000453 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000454 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000455 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000456 if (regmatch.regprog != NULL)
457 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000458
459 if (sort_nr)
460 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000461 /* Make sure vim_str2nr doesn't read any digits past the end
462 * of the match, by temporarily terminating the string there */
463 s2 = s + end_col;
464 c = *s2;
465 (*s2) = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000466 /* Sorting on number: Store the number itself. */
467 if (sort_hex)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000468 s = skiptohex(s + start_col);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000469 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000470 s = skiptodigit(s + start_col);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000471 vim_str2nr(s, NULL, NULL, sort_oct, sort_hex,
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000472 &nrs[lnum - eap->line1].start_col_nr, NULL);
473 (*s2) = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000474 }
475 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000476 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000477 /* Store the column to sort at. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000478 nrs[lnum - eap->line1].start_col_nr = start_col;
479 nrs[lnum - eap->line1].end_col_nr = end_col;
480 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000481
482 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000483
484 if (regmatch.regprog != NULL)
485 fast_breakcheck();
486 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000487 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000488 }
489
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000490 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000491 sortbuf1 = alloc((unsigned)maxlen + 1);
492 if (sortbuf1 == NULL)
493 goto sortend;
494 sortbuf2 = alloc((unsigned)maxlen + 1);
495 if (sortbuf2 == NULL)
496 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000497
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000498 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000499 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000500
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000501 if (sort_abort)
502 goto sortend;
503
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000504 /* Insert the lines in the sorted order below the last one. */
505 lnum = eap->line2;
506 for (i = 0; i < count; ++i)
507 {
508 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
509 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000510 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000511 {
512 if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL)
513 break;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000514 if (unique)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000515 STRCPY(sortbuf1, s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000516 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000517 fast_breakcheck();
518 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000519 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000520 }
521
522 /* delete the original lines if appending worked */
523 if (i == count)
524 for (i = 0; i < count; ++i)
525 ml_delete(eap->line1, FALSE);
526 else
527 count = 0;
528
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000529 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000530 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000531 if (deleted > 0)
532 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
533 else if (deleted < 0)
534 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
535 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000536
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000537 curwin->w_cursor.lnum = eap->line1;
538 beginline(BL_WHITE | BL_FIX);
539
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000540sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000541 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000542 vim_free(sortbuf1);
543 vim_free(sortbuf2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000544 vim_free(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000545 if (got_int)
546 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000547}
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/*
550 * ":retab".
551 */
552 void
553ex_retab(eap)
554 exarg_T *eap;
555{
556 linenr_T lnum;
557 int got_tab = FALSE;
558 long num_spaces = 0;
559 long num_tabs;
560 long len;
561 long col;
562 long vcol;
563 long start_col = 0; /* For start of white-space string */
564 long start_vcol = 0; /* For start of white-space string */
565 int temp;
566 long old_len;
567 char_u *ptr;
568 char_u *new_line = (char_u *)1; /* init to non-NULL */
569 int did_undo; /* called u_save for current line */
570 int new_ts;
571 int save_list;
572 linenr_T first_line = 0; /* first changed line */
573 linenr_T last_line = 0; /* last changed line */
574
575 save_list = curwin->w_p_list;
576 curwin->w_p_list = 0; /* don't want list mode here */
577
578 new_ts = getdigits(&(eap->arg));
579 if (new_ts < 0)
580 {
581 EMSG(_(e_positive));
582 return;
583 }
584 if (new_ts == 0)
585 new_ts = curbuf->b_p_ts;
586 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
587 {
588 ptr = ml_get(lnum);
589 col = 0;
590 vcol = 0;
591 did_undo = FALSE;
592 for (;;)
593 {
594 if (vim_iswhite(ptr[col]))
595 {
596 if (!got_tab && num_spaces == 0)
597 {
598 /* First consecutive white-space */
599 start_vcol = vcol;
600 start_col = col;
601 }
602 if (ptr[col] == ' ')
603 num_spaces++;
604 else
605 got_tab = TRUE;
606 }
607 else
608 {
609 if (got_tab || (eap->forceit && num_spaces > 1))
610 {
611 /* Retabulate this string of white-space */
612
613 /* len is virtual length of white string */
614 len = num_spaces = vcol - start_vcol;
615 num_tabs = 0;
616 if (!curbuf->b_p_et)
617 {
618 temp = new_ts - (start_vcol % new_ts);
619 if (num_spaces >= temp)
620 {
621 num_spaces -= temp;
622 num_tabs++;
623 }
624 num_tabs += num_spaces / new_ts;
625 num_spaces -= (num_spaces / new_ts) * new_ts;
626 }
627 if (curbuf->b_p_et || got_tab ||
628 (num_spaces + num_tabs < len))
629 {
630 if (did_undo == FALSE)
631 {
632 did_undo = TRUE;
633 if (u_save((linenr_T)(lnum - 1),
634 (linenr_T)(lnum + 1)) == FAIL)
635 {
636 new_line = NULL; /* flag out-of-memory */
637 break;
638 }
639 }
640
641 /* len is actual number of white characters used */
642 len = num_spaces + num_tabs;
643 old_len = (long)STRLEN(ptr);
644 new_line = lalloc(old_len - col + start_col + len + 1,
645 TRUE);
646 if (new_line == NULL)
647 break;
648 if (start_col > 0)
649 mch_memmove(new_line, ptr, (size_t)start_col);
650 mch_memmove(new_line + start_col + len,
651 ptr + col, (size_t)(old_len - col + 1));
652 ptr = new_line + start_col;
653 for (col = 0; col < len; col++)
654 ptr[col] = (col < num_tabs) ? '\t' : ' ';
655 ml_replace(lnum, new_line, FALSE);
656 if (first_line == 0)
657 first_line = lnum;
658 last_line = lnum;
659 ptr = new_line;
660 col = start_col + len;
661 }
662 }
663 got_tab = FALSE;
664 num_spaces = 0;
665 }
666 if (ptr[col] == NUL)
667 break;
668 vcol += chartabsize(ptr + col, (colnr_T)vcol);
669#ifdef FEAT_MBYTE
670 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000671 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 else
673#endif
674 ++col;
675 }
676 if (new_line == NULL) /* out of memory */
677 break;
678 line_breakcheck();
679 }
680 if (got_int)
681 EMSG(_(e_interr));
682
683 if (curbuf->b_p_ts != new_ts)
684 redraw_curbuf_later(NOT_VALID);
685 if (first_line != 0)
686 changed_lines(first_line, 0, last_line + 1, 0L);
687
688 curwin->w_p_list = save_list; /* restore 'list' */
689
690 curbuf->b_p_ts = new_ts;
691 coladvance(curwin->w_curswant);
692
693 u_clearline();
694}
695#endif
696
697/*
698 * :move command - move lines line1-line2 to line dest
699 *
700 * return FAIL for failure, OK otherwise
701 */
702 int
703do_move(line1, line2, dest)
704 linenr_T line1;
705 linenr_T line2;
706 linenr_T dest;
707{
708 char_u *str;
709 linenr_T l;
710 linenr_T extra; /* Num lines added before line1 */
711 linenr_T num_lines; /* Num lines moved */
712 linenr_T last_line; /* Last line in file after adding new text */
713
714 if (dest >= line1 && dest < line2)
715 {
716 EMSG(_("E134: Move lines into themselves"));
717 return FAIL;
718 }
719
720 num_lines = line2 - line1 + 1;
721
722 /*
723 * First we copy the old text to its new location -- webb
724 * Also copy the flag that ":global" command uses.
725 */
726 if (u_save(dest, dest + 1) == FAIL)
727 return FAIL;
728 for (extra = 0, l = line1; l <= line2; l++)
729 {
730 str = vim_strsave(ml_get(l + extra));
731 if (str != NULL)
732 {
733 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
734 vim_free(str);
735 if (dest < line1)
736 extra++;
737 }
738 }
739
740 /*
741 * Now we must be careful adjusting our marks so that we don't overlap our
742 * mark_adjust() calls.
743 *
744 * We adjust the marks within the old text so that they refer to the
745 * last lines of the file (temporarily), because we know no other marks
746 * will be set there since these line numbers did not exist until we added
747 * our new lines.
748 *
749 * Then we adjust the marks on lines between the old and new text positions
750 * (either forwards or backwards).
751 *
752 * And Finally we adjust the marks we put at the end of the file back to
753 * their final destination at the new text position -- webb
754 */
755 last_line = curbuf->b_ml.ml_line_count;
756 mark_adjust(line1, line2, last_line - line2, 0L);
757 if (dest >= line2)
758 {
759 mark_adjust(line2 + 1, dest, -num_lines, 0L);
760 curbuf->b_op_start.lnum = dest - num_lines + 1;
761 curbuf->b_op_end.lnum = dest;
762 }
763 else
764 {
765 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
766 curbuf->b_op_start.lnum = dest + 1;
767 curbuf->b_op_end.lnum = dest + num_lines;
768 }
769 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
770 mark_adjust(last_line - num_lines + 1, last_line,
771 -(last_line - dest - extra), 0L);
772
773 /*
774 * Now we delete the original text -- webb
775 */
776 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
777 return FAIL;
778
779 for (l = line1; l <= line2; l++)
780 ml_delete(line1 + extra, TRUE);
781
782 if (!global_busy && num_lines > p_report)
783 {
784 if (num_lines == 1)
785 MSG(_("1 line moved"));
786 else
787 smsg((char_u *)_("%ld lines moved"), num_lines);
788 }
789
790 /*
791 * Leave the cursor on the last of the moved lines.
792 */
793 if (dest >= line1)
794 curwin->w_cursor.lnum = dest;
795 else
796 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
797
798 if (line1 < dest)
799 changed_lines(line1, 0, dest + num_lines + 1, 0L);
800 else
801 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
802
803 return OK;
804}
805
806/*
807 * ":copy"
808 */
809 void
810ex_copy(line1, line2, n)
811 linenr_T line1;
812 linenr_T line2;
813 linenr_T n;
814{
815 linenr_T count;
816 char_u *p;
817
818 count = line2 - line1 + 1;
819 curbuf->b_op_start.lnum = n + 1;
820 curbuf->b_op_end.lnum = n + count;
821 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
822
823 /*
824 * there are three situations:
825 * 1. destination is above line1
826 * 2. destination is between line1 and line2
827 * 3. destination is below line2
828 *
829 * n = destination (when starting)
830 * curwin->w_cursor.lnum = destination (while copying)
831 * line1 = start of source (while copying)
832 * line2 = end of source (while copying)
833 */
834 if (u_save(n, n + 1) == FAIL)
835 return;
836
837 curwin->w_cursor.lnum = n;
838 while (line1 <= line2)
839 {
840 /* need to use vim_strsave() because the line will be unlocked within
841 * ml_append() */
842 p = vim_strsave(ml_get(line1));
843 if (p != NULL)
844 {
845 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
846 vim_free(p);
847 }
848 /* situation 2: skip already copied lines */
849 if (line1 == n)
850 line1 = curwin->w_cursor.lnum;
851 ++line1;
852 if (curwin->w_cursor.lnum < line1)
853 ++line1;
854 if (curwin->w_cursor.lnum < line2)
855 ++line2;
856 ++curwin->w_cursor.lnum;
857 }
858
859 appended_lines_mark(n, count);
860
861 msgmore((long)count);
862}
863
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000864static char_u *prevcmd = NULL; /* the previous command */
865
866#if defined(EXITFREE) || defined(PROTO)
867 void
868free_prev_shellcmd()
869{
870 vim_free(prevcmd);
871}
872#endif
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874/*
875 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
876 * Bangs in the argument are replaced with the previously entered command.
877 * Remember the argument.
878 *
879 * RISCOS: Bangs only replaced when followed by a space, since many
880 * pathnames contain one.
881 */
882 void
883do_bang(addr_count, eap, forceit, do_in, do_out)
884 int addr_count;
885 exarg_T *eap;
886 int forceit;
887 int do_in, do_out;
888{
889 char_u *arg = eap->arg; /* command */
890 linenr_T line1 = eap->line1; /* start of range */
891 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 char_u *newcmd = NULL; /* the new command */
893 int free_newcmd = FALSE; /* need to free() newcmd */
894 int ins_prevcmd;
895 char_u *t;
896 char_u *p;
897 char_u *trailarg;
898 int len;
899 int scroll_save = msg_scroll;
900
901 /*
902 * Disallow shell commands for "rvim".
903 * Disallow shell commands from .exrc and .vimrc in current directory for
904 * security reasons.
905 */
906 if (check_restricted() || check_secure())
907 return;
908
909 if (addr_count == 0) /* :! */
910 {
911 msg_scroll = FALSE; /* don't scroll here */
912 autowrite_all();
913 msg_scroll = scroll_save;
914 }
915
916 /*
917 * Try to find an embedded bang, like in :!<cmd> ! [args]
918 * (:!! is indicated by the 'forceit' variable)
919 */
920 ins_prevcmd = forceit;
921 trailarg = arg;
922 do
923 {
924 len = (int)STRLEN(trailarg) + 1;
925 if (newcmd != NULL)
926 len += (int)STRLEN(newcmd);
927 if (ins_prevcmd)
928 {
929 if (prevcmd == NULL)
930 {
931 EMSG(_(e_noprev));
932 vim_free(newcmd);
933 return;
934 }
935 len += (int)STRLEN(prevcmd);
936 }
937 if ((t = alloc(len)) == NULL)
938 {
939 vim_free(newcmd);
940 return;
941 }
942 *t = NUL;
943 if (newcmd != NULL)
944 STRCAT(t, newcmd);
945 if (ins_prevcmd)
946 STRCAT(t, prevcmd);
947 p = t + STRLEN(t);
948 STRCAT(t, trailarg);
949 vim_free(newcmd);
950 newcmd = t;
951
952 /*
953 * Scan the rest of the argument for '!', which is replaced by the
954 * previous command. "\!" is replaced by "!" (this is vi compatible).
955 */
956 trailarg = NULL;
957 while (*p)
958 {
959 if (*p == '!'
960#ifdef RISCOS
961 && (p[1] == ' ' || p[1] == NUL)
962#endif
963 )
964 {
965 if (p > newcmd && p[-1] == '\\')
966 mch_memmove(p - 1, p, (size_t)(STRLEN(p) + 1));
967 else
968 {
969 trailarg = p;
970 *trailarg++ = NUL;
971 ins_prevcmd = TRUE;
972 break;
973 }
974 }
975 ++p;
976 }
977 } while (trailarg != NULL);
978
979 vim_free(prevcmd);
980 prevcmd = newcmd;
981
982 if (bangredo) /* put cmd in redo buffer for ! command */
983 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000984 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 AppendToRedobuff((char_u *)"\n");
986 bangredo = FALSE;
987 }
988 /*
989 * Add quotes around the command, for shells that need them.
990 */
991 if (*p_shq != NUL)
992 {
993 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
994 if (newcmd == NULL)
995 return;
996 STRCPY(newcmd, p_shq);
997 STRCAT(newcmd, prevcmd);
998 STRCAT(newcmd, p_shq);
999 free_newcmd = TRUE;
1000 }
1001 if (addr_count == 0) /* :! */
1002 {
1003 /* echo the command */
1004 msg_start();
1005 msg_putchar(':');
1006 msg_putchar('!');
1007 msg_outtrans(newcmd);
1008 msg_clr_eos();
1009 windgoto(msg_row, msg_col);
1010
1011 do_shell(newcmd, 0);
1012 }
1013 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 /* Careful: This may recursively call do_bang() again! (because of
1016 * autocommands) */
1017 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001018#ifdef FEAT_AUTOCMD
1019 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1020#endif
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (free_newcmd)
1023 vim_free(newcmd);
1024}
1025
1026/*
1027 * do_filter: filter lines through a command given by the user
1028 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 * We mostly use temp files and the call_shell() routine here. This would
1030 * normally be done using pipes on a UNIX machine, but this is more portable
1031 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 * to deal with redirection somehow, and should handle things like looking
1033 * at the PATH env. variable, and adding reasonable extensions to the
1034 * command name given by the user. All reasonable versions of call_shell()
1035 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 * Alternatively, if on Unix and redirecting input or output, but not both,
1037 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * We use input redirection if do_in is TRUE.
1039 * We use output redirection if do_out is TRUE.
1040 */
1041 static void
1042do_filter(line1, line2, eap, cmd, do_in, do_out)
1043 linenr_T line1, line2;
1044 exarg_T *eap; /* for forced 'ff' and 'fenc' */
1045 char_u *cmd;
1046 int do_in, do_out;
1047{
1048 char_u *itmp = NULL;
1049 char_u *otmp = NULL;
1050 linenr_T linecount;
1051 linenr_T read_linecount;
1052 pos_T cursor_save;
1053 char_u *cmd_buf;
1054#ifdef FEAT_AUTOCMD
1055 buf_T *old_curbuf = curbuf;
1056#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 if (*cmd == NUL) /* no filter command */
1060 return;
1061
1062#ifdef WIN3264
1063 /*
1064 * Check if external commands are allowed now.
1065 */
1066 if (can_end_termcap_mode(TRUE) == FALSE)
1067 return;
1068#endif
1069
1070 cursor_save = curwin->w_cursor;
1071 linecount = line2 - line1 + 1;
1072 curwin->w_cursor.lnum = line1;
1073 curwin->w_cursor.col = 0;
1074 changed_line_abv_curs();
1075 invalidate_botline();
1076
1077 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 * When using temp files:
1079 * 1. * Form temp file names
1080 * 2. * Write the lines to a temp file
1081 * 3. Run the filter command on the temp file
1082 * 4. * Read the output of the command into the buffer
1083 * 5. * Delete the original lines to be filtered
1084 * 6. * Remove the temp files
1085 *
1086 * When writing the input with a pipe or when catching the output with a
1087 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 */
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (do_out)
1091 shell_flags |= SHELL_DOOUT;
1092
1093#if !defined(USE_SYSTEM) && defined(UNIX)
1094 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1097 shell_flags |= SHELL_READ;
1098 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 else if (do_in && !do_out && !p_stmp)
1101 {
1102 /* Use a pipe to write stdin of the command, do not use a temp file. */
1103 shell_flags |= SHELL_WRITE;
1104 curbuf->b_op_start.lnum = line1;
1105 curbuf->b_op_end.lnum = line2;
1106 }
1107 else if (do_in && do_out && !p_stmp)
1108 {
1109 /* Use a pipe to write stdin and fetch stdout of the command, do not
1110 * use a temp file. */
1111 shell_flags |= SHELL_READ|SHELL_WRITE;
1112 curbuf->b_op_start.lnum = line1;
1113 curbuf->b_op_end.lnum = line2;
1114 curwin->w_cursor.lnum = line2;
1115 }
1116 else
1117#endif
1118 if ((do_in && (itmp = vim_tempname('i')) == NULL)
1119 || (do_out && (otmp = vim_tempname('o')) == NULL))
1120 {
1121 EMSG(_(e_notmp));
1122 goto filterend;
1123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125/*
1126 * The writing and reading of temp files will not be shown.
1127 * Vi also doesn't do this and the messages are not very informative.
1128 */
1129 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 FALSE, FALSE, FALSE, TRUE) == FAIL)
1132 {
1133 msg_putchar('\n'); /* keep message from buf_write() */
1134 --no_wait_return;
1135#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1136 if (!aborting())
1137#endif
1138 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1139 goto filterend;
1140 }
1141#ifdef FEAT_AUTOCMD
1142 if (curbuf != old_curbuf)
1143 goto filterend;
1144#endif
1145
1146 if (!do_out)
1147 msg_putchar('\n');
1148
1149 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1150 if (cmd_buf == NULL)
1151 goto filterend;
1152
1153 windgoto((int)Rows - 1, 0);
1154 cursor_on();
1155
1156 /*
1157 * When not redirecting the output the command can write anything to the
1158 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1159 * stderr output of external command. Clear the screen later.
1160 * If do_in is FALSE, this could be something like ":r !cat", which may
1161 * also mess up the screen, clear it later.
1162 */
1163 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1164 redraw_later_clear();
1165
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (do_out)
1167 {
1168 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
1169 goto error;
1170 redraw_curbuf_later(VALID);
1171 }
1172 read_linecount = curbuf->b_ml.ml_line_count;
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 /*
1175 * When call_shell() fails wait_return() is called to give the user a
1176 * chance to read the error messages. Otherwise errors are ignored, so you
1177 * can see the error messages from the command that appear on stdout; use
1178 * 'u' to fix the text
1179 * Switch to cooked mode when not redirecting stdin, avoids that something
1180 * like ":r !cat" hangs.
1181 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1182 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
1185 redraw_later_clear();
1186 wait_return(FALSE);
1187 }
1188 vim_free(cmd_buf);
1189
1190 did_check_timestamps = FALSE;
1191 need_check_timestamps = TRUE;
1192
1193 /* When interrupting the shell command, it may still have produced some
1194 * useful output. Reset got_int here, so that readfile() won't cancel
1195 * reading. */
1196 ui_breakcheck();
1197 got_int = FALSE;
1198
1199 if (do_out)
1200 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1204 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1207 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209 {
1210 msg_putchar('\n');
1211 EMSG2(_(e_notread), otmp);
1212 }
1213 goto error;
1214 }
1215#ifdef FEAT_AUTOCMD
1216 if (curbuf != old_curbuf)
1217 goto filterend;
1218#endif
1219 }
1220
1221 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1222
1223 if (shell_flags & SHELL_READ)
1224 {
1225 curbuf->b_op_start.lnum = line2 + 1;
1226 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1227 appended_lines_mark(line2, read_linecount);
1228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
1230 if (do_in)
1231 {
1232 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (read_linecount >= linecount)
1235 /* move all marks from old lines to new lines */
1236 mark_adjust(line1, line2, linecount, 0L);
1237 else
1238 {
1239 /* move marks from old lines to new lines, delete marks
1240 * that are in deleted lines */
1241 mark_adjust(line1, line1 + read_linecount - 1,
1242 linecount, 0L);
1243 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1244 }
1245 }
1246
1247 /*
1248 * Put cursor on first filtered line for ":range!cmd".
1249 * Adjust '[ and '] (set by buf_write()).
1250 */
1251 curwin->w_cursor.lnum = line1;
1252 del_lines(linecount, TRUE);
1253 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1254 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1255 write_lnum_adjust(-linecount); /* adjust last line
1256 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257#ifdef FEAT_FOLDING
1258 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 else
1262 {
1263 /*
1264 * Put cursor on last new line for ":r !cmd".
1265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001267 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1271 --no_wait_return;
1272
1273 if (linecount > p_report)
1274 {
1275 if (do_in)
1276 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001277 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1278 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001281 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
1284 msgmore((long)linecount);
1285 }
1286 }
1287 else
1288 {
1289error:
1290 /* put cursor back in same position for ":w !cmd" */
1291 curwin->w_cursor = cursor_save;
1292 --no_wait_return;
1293 wait_return(FALSE);
1294 }
1295
1296filterend:
1297
1298#ifdef FEAT_AUTOCMD
1299 if (curbuf != old_curbuf)
1300 {
1301 --no_wait_return;
1302 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1303 }
1304#endif
1305 if (itmp != NULL)
1306 mch_remove(itmp);
1307 if (otmp != NULL)
1308 mch_remove(otmp);
1309 vim_free(itmp);
1310 vim_free(otmp);
1311}
1312
1313/*
1314 * Call a shell to execute a command.
1315 * When "cmd" is NULL start an interactive shell.
1316 */
1317 void
1318do_shell(cmd, flags)
1319 char_u *cmd;
1320 int flags; /* may be SHELL_DOOUT when output is redirected */
1321{
1322 buf_T *buf;
1323#ifndef FEAT_GUI_MSWIN
1324 int save_nwr;
1325#endif
1326#ifdef MSWIN
1327 int winstart = FALSE;
1328#endif
1329
1330 /*
1331 * Disallow shell commands for "rvim".
1332 * Disallow shell commands from .exrc and .vimrc in current directory for
1333 * security reasons.
1334 */
1335 if (check_restricted() || check_secure())
1336 {
1337 msg_end();
1338 return;
1339 }
1340
1341#ifdef MSWIN
1342 /*
1343 * Check if external commands are allowed now.
1344 */
1345 if (can_end_termcap_mode(TRUE) == FALSE)
1346 return;
1347
1348 /*
1349 * Check if ":!start" is used.
1350 */
1351 if (cmd != NULL)
1352 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1353#endif
1354
1355 /*
1356 * For autocommands we want to get the output on the current screen, to
1357 * avoid having to type return below.
1358 */
1359 msg_putchar('\r'); /* put cursor at start of line */
1360#ifdef FEAT_AUTOCMD
1361 if (!autocmd_busy)
1362#endif
1363 {
1364#ifdef MSWIN
1365 if (!winstart)
1366#endif
1367 stoptermcap();
1368 }
1369#ifdef MSWIN
1370 if (!winstart)
1371#endif
1372 msg_putchar('\n'); /* may shift screen one line up */
1373
1374 /* warning message before calling the shell */
1375 if (p_warn
1376#ifdef FEAT_AUTOCMD
1377 && !autocmd_busy
1378#endif
1379 && msg_silent == 0)
1380 for (buf = firstbuf; buf; buf = buf->b_next)
1381 if (bufIsChanged(buf))
1382 {
1383#ifdef FEAT_GUI_MSWIN
1384 if (!winstart)
1385 starttermcap(); /* don't want a message box here */
1386#endif
1387 MSG_PUTS(_("[No write since last change]\n"));
1388#ifdef FEAT_GUI_MSWIN
1389 if (!winstart)
1390 stoptermcap();
1391#endif
1392 break;
1393 }
1394
1395 /* This windgoto is required for when the '\n' resulted in a "delete line
1396 * 1" command to the terminal. */
1397 if (!swapping_screen())
1398 windgoto(msg_row, msg_col);
1399 cursor_on();
1400 (void)call_shell(cmd, SHELL_COOKED | flags);
1401 did_check_timestamps = FALSE;
1402 need_check_timestamps = TRUE;
1403
1404 /*
1405 * put the message cursor at the end of the screen, avoids wait_return()
1406 * to overwrite the text that the external command showed
1407 */
1408 if (!swapping_screen())
1409 {
1410 msg_row = Rows - 1;
1411 msg_col = 0;
1412 }
1413
1414#ifdef FEAT_AUTOCMD
1415 if (autocmd_busy)
1416 {
1417 if (msg_silent == 0)
1418 redraw_later_clear();
1419 }
1420 else
1421#endif
1422 {
1423 /*
1424 * For ":sh" there is no need to call wait_return(), just redraw.
1425 * Also for the Win32 GUI (the output is in a console window).
1426 * Otherwise there is probably text on the screen that the user wants
1427 * to read before redrawing, so call wait_return().
1428 */
1429#ifndef FEAT_GUI_MSWIN
1430 if (cmd == NULL
1431# ifdef WIN3264
1432 || (winstart && !need_wait_return)
1433# endif
1434 )
1435 {
1436 if (msg_silent == 0)
1437 redraw_later_clear();
1438 need_wait_return = FALSE;
1439 }
1440 else
1441 {
1442 /*
1443 * If we switch screens when starttermcap() is called, we really
1444 * want to wait for "hit return to continue".
1445 */
1446 save_nwr = no_wait_return;
1447 if (swapping_screen())
1448 no_wait_return = FALSE;
1449# ifdef AMIGA
1450 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1451# else
1452 wait_return(msg_silent == 0);
1453# endif
1454 no_wait_return = save_nwr;
1455 }
1456#endif /* FEAT_GUI_W32 */
1457
1458#ifdef MSWIN
1459 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1460#endif
1461 starttermcap(); /* start termcap if not done by wait_return() */
1462
1463 /*
1464 * In an Amiga window redrawing is caused by asking the window size.
1465 * If we got an interrupt this will not work. The chance that the
1466 * window size is wrong is very small, but we need to redraw the
1467 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1468 * but it saves an extra redraw.
1469 */
1470#ifdef AMIGA
1471 if (skip_redraw) /* ':' hit in wait_return() */
1472 {
1473 if (msg_silent == 0)
1474 redraw_later_clear();
1475 }
1476 else if (term_console)
1477 {
1478 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1479 if (got_int && msg_silent == 0)
1480 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1481 else
1482 must_redraw = 0; /* no extra redraw needed */
1483 }
1484#endif
1485 }
1486
1487 /* display any error messages now */
1488 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001489
1490#ifdef FEAT_AUTOCMD
1491 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493}
1494
1495/*
1496 * Create a shell command from a command string, input redirection file and
1497 * output redirection file.
1498 * Returns an allocated string with the shell command, or NULL for failure.
1499 */
1500 char_u *
1501make_filter_cmd(cmd, itmp, otmp)
1502 char_u *cmd; /* command */
1503 char_u *itmp; /* NULL or name of input file */
1504 char_u *otmp; /* NULL or name of output file */
1505{
1506 char_u *buf;
1507 long_u len;
1508
1509 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
1510 if (itmp != NULL)
1511 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1512 if (otmp != NULL)
1513 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1514 buf = lalloc(len, TRUE);
1515 if (buf == NULL)
1516 return NULL;
1517
1518#if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2)
1519 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001520 * Put braces around the command (for concatenated commands) when
1521 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001523 if (itmp != NULL || otmp != NULL)
1524 sprintf((char *)buf, "(%s)", (char *)cmd);
1525 else
1526 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (itmp != NULL)
1528 {
1529 STRCAT(buf, " < ");
1530 STRCAT(buf, itmp);
1531 }
1532#else
1533 /*
1534 * for shells that don't understand braces around commands, at least allow
1535 * the use of commands in a pipe.
1536 */
1537 STRCPY(buf, cmd);
1538 if (itmp != NULL)
1539 {
1540 char_u *p;
1541
1542 /*
1543 * If there is a pipe, we have to put the '<' in front of it.
1544 * Don't do this when 'shellquote' is not empty, otherwise the
1545 * redirection would be inside the quotes.
1546 */
1547 if (*p_shq == NUL)
1548 {
1549 p = vim_strchr(buf, '|');
1550 if (p != NULL)
1551 *p = NUL;
1552 }
1553# ifdef RISCOS
1554 STRCAT(buf, " { < "); /* Use RISC OS notation for input. */
1555 STRCAT(buf, itmp);
1556 STRCAT(buf, " } ");
1557# else
1558 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1559 STRCAT(buf, itmp);
1560# endif
1561 if (*p_shq == NUL)
1562 {
1563 p = vim_strchr(cmd, '|');
1564 if (p != NULL)
1565 {
1566 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1567 STRCAT(buf, p);
1568 }
1569 }
1570 }
1571#endif
1572 if (otmp != NULL)
1573 append_redir(buf, p_srr, otmp);
1574
1575 return buf;
1576}
1577
1578/*
1579 * Append output redirection for file "fname" to the end of string buffer "buf"
1580 * Works with the 'shellredir' and 'shellpipe' options.
1581 * The caller should make sure that there is enough room:
1582 * STRLEN(opt) + STRLEN(fname) + 3
1583 */
1584 void
1585append_redir(buf, opt, fname)
1586 char_u *buf;
1587 char_u *opt;
1588 char_u *fname;
1589{
1590 char_u *p;
1591
1592 buf += STRLEN(buf);
1593 /* find "%s", skipping "%%" */
1594 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
1595 if (p[1] == 's')
1596 break;
1597 if (p != NULL)
1598 {
1599 *buf = ' '; /* not really needed? Not with sh, ksh or bash */
1600 sprintf((char *)buf + 1, (char *)opt, (char *)fname);
1601 }
1602 else
1603 sprintf((char *)buf,
1604#ifdef FEAT_QUICKFIX
1605# ifndef RISCOS
1606 opt != p_sp ? " %s%s" :
1607# endif
1608 " %s %s",
1609#else
1610# ifndef RISCOS
1611 " %s%s", /* " > %s" causes problems on Amiga */
1612# else
1613 " %s %s", /* But is needed for 'shellpipe' and RISC OS */
1614# endif
1615#endif
1616 (char *)opt, (char *)fname);
1617}
1618
1619#ifdef FEAT_VIMINFO
1620
1621static int no_viminfo __ARGS((void));
1622static int viminfo_errcnt;
1623
1624 static int
1625no_viminfo()
1626{
1627 /* "vim -i NONE" does not read or write a viminfo file */
1628 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1629}
1630
1631/*
1632 * Report an error for reading a viminfo file.
1633 * Count the number of errors. When there are more than 10, return TRUE.
1634 */
1635 int
1636viminfo_error(errnum, message, line)
1637 char *errnum;
1638 char *message;
1639 char_u *line;
1640{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001641 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1642 errnum, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff));
Bram Moolenaar758711c2005-02-02 23:11:38 +00001644 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1645 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 emsg(IObuff);
1647 if (++viminfo_errcnt >= 10)
1648 {
1649 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1650 return TRUE;
1651 }
1652 return FALSE;
1653}
1654
1655/*
1656 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
1657 * set are not over-written unless force is TRUE. -- webb
1658 */
1659 int
1660read_viminfo(file, want_info, want_marks, forceit)
1661 char_u *file;
1662 int want_info;
1663 int want_marks;
1664 int forceit;
1665{
1666 FILE *fp;
1667 char_u *fname;
1668
1669 if (no_viminfo())
1670 return FAIL;
1671
1672 fname = viminfo_filename(file); /* may set to default if NULL */
1673 if (fname == NULL)
1674 return FAIL;
1675 fp = mch_fopen((char *)fname, READBIN);
1676
1677 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001678 {
1679 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001680 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1681 fname,
1682 want_info ? _(" info") : "",
1683 want_marks ? _(" marks") : "",
1684 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001685 verbose_leave();
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
1688 vim_free(fname);
1689 if (fp == NULL)
1690 return FAIL;
1691
1692 viminfo_errcnt = 0;
1693 do_viminfo(fp, NULL, want_info, want_marks, forceit);
1694
1695 fclose(fp);
1696
1697 return OK;
1698}
1699
1700/*
1701 * write_viminfo() -- Write the viminfo file. The old one is read in first so
1702 * that effectively a merge of current info and old info is done. This allows
1703 * multiple vims to run simultaneously, without losing any marks etc. If
1704 * forceit is TRUE, then the old file is not read in, and only internal info is
1705 * written to the file. -- webb
1706 */
1707 void
1708write_viminfo(file, forceit)
1709 char_u *file;
1710 int forceit;
1711{
1712 char_u *fname;
1713 FILE *fp_in = NULL; /* input viminfo file, if any */
1714 FILE *fp_out = NULL; /* output viminfo file */
1715 char_u *tempname = NULL; /* name of temp viminfo file */
1716 struct stat st_new; /* mch_stat() of potential new file */
1717 char_u *wp;
1718#if defined(UNIX) || defined(VMS)
1719 mode_t umask_save;
1720#endif
1721#ifdef UNIX
1722 int shortname = FALSE; /* use 8.3 file name */
1723 struct stat st_old; /* mch_stat() of existing viminfo file */
1724#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001725#ifdef WIN3264
1726 long perm = -1;
1727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 if (no_viminfo())
1730 return;
1731
1732 fname = viminfo_filename(file); /* may set to default if NULL */
1733 if (fname == NULL)
1734 return;
1735
1736 fp_in = mch_fopen((char *)fname, READBIN);
1737 if (fp_in == NULL)
1738 {
1739 /* if it does exist, but we can't read it, don't try writing */
1740 if (mch_stat((char *)fname, &st_new) == 0)
1741 goto end;
1742#if defined(UNIX) || defined(VMS)
1743 /*
1744 * For Unix we create the .viminfo non-accessible for others,
1745 * because it may contain text from non-accessible documents.
1746 */
1747 umask_save = umask(077);
1748#endif
1749 fp_out = mch_fopen((char *)fname, WRITEBIN);
1750#if defined(UNIX) || defined(VMS)
1751 (void)umask(umask_save);
1752#endif
1753 }
1754 else
1755 {
1756 /*
1757 * There is an existing viminfo file. Create a temporary file to
1758 * write the new viminfo into, in the same directory as the
1759 * existing viminfo file, which will be renamed later.
1760 */
1761#ifdef UNIX
1762 /*
1763 * For Unix we check the owner of the file. It's not very nice to
1764 * overwrite a user's viminfo file after a "su root", with a
1765 * viminfo file that the user can't read.
1766 */
1767 st_old.st_dev = st_old.st_ino = 0;
1768 st_old.st_mode = 0600;
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001769 if (mch_stat((char *)fname, &st_old) == 0 && getuid()
1770 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 ? (st_old.st_mode & 0200)
1772 : (st_old.st_gid == getgid()
1773 ? (st_old.st_mode & 0020)
1774 : (st_old.st_mode & 0002))))
1775 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001776 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1780 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001781 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 goto end;
1783 }
1784#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001785#ifdef WIN3264
1786 /* Get the file attributes of the existing viminfo file. */
1787 perm = mch_getperm(fname);
1788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790 /*
1791 * Make tempname.
1792 * May try twice: Once normal and once with shortname set, just in
1793 * case somebody puts his viminfo file in an 8.3 filesystem.
1794 */
1795 for (;;)
1796 {
1797 tempname = buf_modname(
1798#ifdef UNIX
1799 shortname,
1800#else
1801# ifdef SHORT_FNAME
1802 TRUE,
1803# else
1804# ifdef FEAT_GUI_W32
1805 gui_is_win32s(),
1806# else
1807 FALSE,
1808# endif
1809# endif
1810#endif
1811 fname,
1812#ifdef VMS
1813 (char_u *)"-tmp",
1814#else
1815# ifdef RISCOS
1816 (char_u *)"/tmp",
1817# else
1818 (char_u *)".tmp",
1819# endif
1820#endif
1821 FALSE);
1822 if (tempname == NULL) /* out of memory */
1823 break;
1824
1825 /*
1826 * Check if tempfile already exists. Never overwrite an
1827 * existing file!
1828 */
1829 if (mch_stat((char *)tempname, &st_new) == 0)
1830 {
1831#ifdef UNIX
1832 /*
1833 * Check if tempfile is same as original file. May happen
1834 * when modname() gave the same file back. E.g. silly
1835 * link, or file name-length reached. Try again with
1836 * shortname set.
1837 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001838 if (!shortname && st_new.st_dev == st_old.st_dev
1839 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 vim_free(tempname);
1842 tempname = NULL;
1843 shortname = TRUE;
1844 continue;
1845 }
1846#endif
1847 /*
1848 * Try another name. Change one character, just before
1849 * the extension. This should also work for an 8.3
1850 * file name, when after adding the extension it still is
1851 * the same file as the original.
1852 */
1853 wp = tempname + STRLEN(tempname) - 5;
1854 if (wp < gettail(tempname)) /* empty file name? */
1855 wp = gettail(tempname);
1856 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1857 --*wp)
1858 {
1859 /*
1860 * They all exist? Must be something wrong! Don't
1861 * write the viminfo file then.
1862 */
1863 if (*wp == 'a')
1864 {
1865 vim_free(tempname);
1866 tempname = NULL;
1867 break;
1868 }
1869 }
1870 }
1871 break;
1872 }
1873
1874 if (tempname != NULL)
1875 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00001876#ifdef VMS
1877 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00001878 umask_save = umask(077);
1879 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1880 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001881#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00001882 int fd;
1883
1884 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001885 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001886 * Unix: same as original file, but strip s-bit. Reset umask to
1887 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001888 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00001889# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001890 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00001891 fd = mch_open((char *)tempname,
1892 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001893 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001894 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001895# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001896 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001897 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001898# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00001899 if (fd < 0)
1900 fp_out = NULL;
1901 else
1902 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001903#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 /*
1906 * If we can't create in the same directory, try creating a
1907 * "normal" temp file.
1908 */
1909 if (fp_out == NULL)
1910 {
1911 vim_free(tempname);
1912 if ((tempname = vim_tempname('o')) != NULL)
1913 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1914 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00001915
1916#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00001918 * Make sure the owner can read/write it. This only works for
1919 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 */
1921 if (fp_out != NULL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001922 (void)fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924 }
1925 }
1926
1927 /*
1928 * Check if the new viminfo file can be written to.
1929 */
1930 if (fp_out == NULL)
1931 {
1932 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001933 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (fp_in != NULL)
1935 fclose(fp_in);
1936 goto end;
1937 }
1938
1939 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001940 {
1941 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001942 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001943 verbose_leave();
1944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 viminfo_errcnt = 0;
1947 do_viminfo(fp_in, fp_out, !forceit, !forceit, FALSE);
1948
1949 fclose(fp_out); /* errors are ignored !? */
1950 if (fp_in != NULL)
1951 {
1952 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 /*
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001955 * In case of an error keep the original viminfo file.
1956 * Otherwise rename the newly written file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 */
1958 if (viminfo_errcnt || vim_rename(tempname, fname) == -1)
1959 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001960
1961#ifdef WIN3264
1962 /* If the viminfo file was hidden then also hide the new file. */
1963 if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN))
1964 mch_hide(fname);
1965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968end:
1969 vim_free(fname);
1970 vim_free(tempname);
1971}
1972
1973/*
1974 * Get the viminfo file name to use.
1975 * If "file" is given and not empty, use it (has already been expanded by
1976 * cmdline functions).
1977 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
1978 * expand environment variables.
1979 * Returns an allocated string. NULL when out of memory.
1980 */
1981 static char_u *
1982viminfo_filename(file)
1983 char_u *file;
1984{
1985 if (file == NULL || *file == NUL)
1986 {
1987 if (use_viminfo != NULL)
1988 file = use_viminfo;
1989 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
1990 {
1991#ifdef VIMINFO_FILE2
1992 /* don't use $HOME when not defined (turned into "c:/"!). */
1993# ifdef VMS
1994 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
1995# else
1996 if (mch_getenv((char_u *)"HOME") == NULL)
1997# endif
1998 {
1999 /* don't use $VIM when not available. */
2000 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2001 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2002 file = (char_u *)VIMINFO_FILE2;
2003 else
2004 file = (char_u *)VIMINFO_FILE;
2005 }
2006 else
2007#endif
2008 file = (char_u *)VIMINFO_FILE;
2009 }
2010 expand_env(file, NameBuff, MAXPATHL);
2011 file = NameBuff;
2012 }
2013 return vim_strsave(file);
2014}
2015
2016/*
2017 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2018 */
2019 static void
2020do_viminfo(fp_in, fp_out, want_info, want_marks, force_read)
2021 FILE *fp_in;
2022 FILE *fp_out;
2023 int want_info;
2024 int want_marks;
2025 int force_read;
2026{
2027 int count = 0;
2028 int eof = FALSE;
2029 vir_T vir;
2030
2031 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2032 return;
2033 vir.vir_fd = fp_in;
2034#ifdef FEAT_MBYTE
2035 vir.vir_conv.vc_type = CONV_NONE;
2036#endif
2037
2038 if (fp_in != NULL)
2039 {
2040 if (want_info)
2041 eof = read_viminfo_up_to_marks(&vir, force_read, fp_out != NULL);
2042 else
2043 /* Skip info, find start of marks */
2044 while (!(eof = viminfo_readline(&vir))
2045 && vir.vir_line[0] != '>')
2046 ;
2047 }
2048 if (fp_out != NULL)
2049 {
2050 /* Write the info: */
2051 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2052 VIM_VERSION_MEDIUM);
2053 fprintf(fp_out, _("# You may edit it if you're careful!\n\n"));
2054#ifdef FEAT_MBYTE
2055 fprintf(fp_out, _("# Value of 'encoding' when this file was written\n"));
2056 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2057#endif
2058 write_viminfo_search_pattern(fp_out);
2059 write_viminfo_sub_string(fp_out);
2060#ifdef FEAT_CMDHIST
2061 write_viminfo_history(fp_out);
2062#endif
2063 write_viminfo_registers(fp_out);
2064#ifdef FEAT_EVAL
2065 write_viminfo_varlist(fp_out);
2066#endif
2067 write_viminfo_filemarks(fp_out);
2068 write_viminfo_bufferlist(fp_out);
2069 count = write_viminfo_marks(fp_out);
2070 }
2071 if (fp_in != NULL && want_marks)
2072 copy_viminfo_marks(&vir, fp_out, count, eof);
2073
2074 vim_free(vir.vir_line);
2075#ifdef FEAT_MBYTE
2076 if (vir.vir_conv.vc_type != CONV_NONE)
2077 convert_setup(&vir.vir_conv, NULL, NULL);
2078#endif
2079}
2080
2081/*
2082 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2083 * first part of the viminfo file which contains everything but the marks that
2084 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2085 */
2086 static int
2087read_viminfo_up_to_marks(virp, forceit, writing)
2088 vir_T *virp;
2089 int forceit;
2090 int writing;
2091{
2092 int eof;
2093 buf_T *buf;
2094
2095#ifdef FEAT_CMDHIST
2096 prepare_viminfo_history(forceit ? 9999 : 0);
2097#endif
2098 eof = viminfo_readline(virp);
2099 while (!eof && virp->vir_line[0] != '>')
2100 {
2101 switch (virp->vir_line[0])
2102 {
2103 /* Characters reserved for future expansion, ignored now */
2104 case '+': /* "+40 /path/dir file", for running vim without args */
2105 case '|': /* to be defined */
2106 case '^': /* to be defined */
2107 case '<': /* long line - ignored */
2108 /* A comment or empty line. */
2109 case NUL:
2110 case '\r':
2111 case '\n':
2112 case '#':
2113 eof = viminfo_readline(virp);
2114 break;
2115 case '*': /* "*encoding=value" */
2116 eof = viminfo_encoding(virp);
2117 break;
2118 case '!': /* global variable */
2119#ifdef FEAT_EVAL
2120 eof = read_viminfo_varlist(virp, writing);
2121#else
2122 eof = viminfo_readline(virp);
2123#endif
2124 break;
2125 case '%': /* entry for buffer list */
2126 eof = read_viminfo_bufferlist(virp, writing);
2127 break;
2128 case '"':
2129 eof = read_viminfo_register(virp, forceit);
2130 break;
2131 case '/': /* Search string */
2132 case '&': /* Substitute search string */
2133 case '~': /* Last search string, followed by '/' or '&' */
2134 eof = read_viminfo_search_pattern(virp, forceit);
2135 break;
2136 case '$':
2137 eof = read_viminfo_sub_string(virp, forceit);
2138 break;
2139 case ':':
2140 case '?':
2141 case '=':
2142 case '@':
2143#ifdef FEAT_CMDHIST
2144 eof = read_viminfo_history(virp);
2145#else
2146 eof = viminfo_readline(virp);
2147#endif
2148 break;
2149 case '-':
2150 case '\'':
2151 eof = read_viminfo_filemark(virp, forceit);
2152 break;
2153 default:
2154 if (viminfo_error("E575: ", _("Illegal starting char"),
2155 virp->vir_line))
2156 eof = TRUE;
2157 else
2158 eof = viminfo_readline(virp);
2159 break;
2160 }
2161 }
2162
2163#ifdef FEAT_CMDHIST
2164 /* Finish reading history items. */
2165 finish_viminfo_history();
2166#endif
2167
2168 /* Change file names to buffer numbers for fmarks. */
2169 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2170 fmarks_check_names(buf);
2171
2172 return eof;
2173}
2174
2175/*
2176 * Compare the 'encoding' value in the viminfo file with the current value of
2177 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2178 * conversion of text with iconv() in viminfo_readstring().
2179 */
2180 static int
2181viminfo_encoding(virp)
2182 vir_T *virp;
2183{
2184#ifdef FEAT_MBYTE
2185 char_u *p;
2186 int i;
2187
2188 if (get_viminfo_parameter('c') != 0)
2189 {
2190 p = vim_strchr(virp->vir_line, '=');
2191 if (p != NULL)
2192 {
2193 /* remove trailing newline */
2194 ++p;
2195 for (i = 0; vim_isprintc(p[i]); ++i)
2196 ;
2197 p[i] = NUL;
2198
2199 convert_setup(&virp->vir_conv, p, p_enc);
2200 }
2201 }
2202#endif
2203 return viminfo_readline(virp);
2204}
2205
2206/*
2207 * Read a line from the viminfo file.
2208 * Returns TRUE for end-of-file;
2209 */
2210 int
2211viminfo_readline(virp)
2212 vir_T *virp;
2213{
2214 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2215}
2216
2217/*
2218 * check string read from viminfo file
2219 * remove '\n' at the end of the line
2220 * - replace CTRL-V CTRL-V with CTRL-V
2221 * - replace CTRL-V 'n' with '\n'
2222 *
2223 * Check for a long line as written by viminfo_writestring().
2224 *
2225 * Return the string in allocated memory (NULL when out of memory).
2226 */
2227/*ARGSUSED*/
2228 char_u *
2229viminfo_readstring(virp, off, convert)
2230 vir_T *virp;
2231 int off; /* offset for virp->vir_line */
2232 int convert; /* convert the string */
2233{
2234 char_u *retval;
2235 char_u *s, *d;
2236 long len;
2237
2238 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2239 {
2240 len = atol((char *)virp->vir_line + off + 1);
2241 retval = lalloc(len, TRUE);
2242 if (retval == NULL)
2243 {
2244 /* Line too long? File messed up? Skip next line. */
2245 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2246 return NULL;
2247 }
2248 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2249 s = retval + 1; /* Skip the leading '<' */
2250 }
2251 else
2252 {
2253 retval = vim_strsave(virp->vir_line + off);
2254 if (retval == NULL)
2255 return NULL;
2256 s = retval;
2257 }
2258
2259 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2260 d = retval;
2261 while (*s != NUL && *s != '\n')
2262 {
2263 if (s[0] == Ctrl_V && s[1] != NUL)
2264 {
2265 if (s[1] == 'n')
2266 *d++ = '\n';
2267 else
2268 *d++ = Ctrl_V;
2269 s += 2;
2270 }
2271 else
2272 *d++ = *s++;
2273 }
2274 *d = NUL;
2275
2276#ifdef FEAT_MBYTE
2277 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2278 {
2279 d = string_convert(&virp->vir_conv, retval, NULL);
2280 if (d != NULL)
2281 {
2282 vim_free(retval);
2283 retval = d;
2284 }
2285 }
2286#endif
2287
2288 return retval;
2289}
2290
2291/*
2292 * write string to viminfo file
2293 * - replace CTRL-V with CTRL-V CTRL-V
2294 * - replace '\n' with CTRL-V 'n'
2295 * - add a '\n' at the end
2296 *
2297 * For a long line:
2298 * - write " CTRL-V <length> \n " in first line
2299 * - write " < <string> \n " in second line
2300 */
2301 void
2302viminfo_writestring(fd, p)
2303 FILE *fd;
2304 char_u *p;
2305{
2306 int c;
2307 char_u *s;
2308 int len = 0;
2309
2310 for (s = p; *s != NUL; ++s)
2311 {
2312 if (*s == Ctrl_V || *s == '\n')
2313 ++len;
2314 ++len;
2315 }
2316
2317 /* If the string will be too long, write its length and put it in the next
2318 * line. Take into account that some room is needed for what comes before
2319 * the string (e.g., variable name). Add something to the length for the
2320 * '<', NL and trailing NUL. */
2321 if (len > LSIZE / 2)
2322 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2323
2324 while ((c = *p++) != NUL)
2325 {
2326 if (c == Ctrl_V || c == '\n')
2327 {
2328 putc(Ctrl_V, fd);
2329 if (c == '\n')
2330 c = 'n';
2331 }
2332 putc(c, fd);
2333 }
2334 putc('\n', fd);
2335}
2336#endif /* FEAT_VIMINFO */
2337
2338/*
2339 * Implementation of ":fixdel", also used by get_stty().
2340 * <BS> resulting <Del>
2341 * ^? ^H
2342 * not ^? ^?
2343 */
2344/*ARGSUSED*/
2345 void
2346do_fixdel(eap)
2347 exarg_T *eap;
2348{
2349 char_u *p;
2350
2351 p = find_termcode((char_u *)"kb");
2352 add_termcode((char_u *)"kD", p != NULL
2353 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2354}
2355
2356 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002357print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 linenr_T lnum;
2359 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002360 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002362 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 if (curwin->w_p_nu || use_number)
2365 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002366 sprintf((char *)numbuf, "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2368 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002369 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370}
2371
2372/*
2373 * Print a text line. Also in silent mode ("ex -s").
2374 */
2375 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002376print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 linenr_T lnum;
2378 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002379 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 int save_silent = silent_mode;
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002384 silent_mode = FALSE;
2385 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2386 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (save_silent)
2388 {
2389 msg_putchar('\n');
2390 cursor_on(); /* msg_start() switches it off */
2391 out_flush();
2392 silent_mode = save_silent;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002393 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395}
2396
2397/*
2398 * ":file[!] [fname]".
2399 */
2400 void
2401ex_file(eap)
2402 exarg_T *eap;
2403{
2404 char_u *fname, *sfname, *xfname;
2405 buf_T *buf;
2406
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002407 /* ":0file" removes the file name. Check for illegal uses ":3file",
2408 * "0file name", etc. */
2409 if (eap->addr_count > 0
2410 && (*eap->arg != NUL
2411 || eap->line2 > 0
2412 || eap->addr_count > 1))
2413 {
2414 EMSG(_(e_invarg));
2415 return;
2416 }
2417
2418 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420#ifdef FEAT_AUTOCMD
2421 buf = curbuf;
2422 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
2423 /* buffer changed, don't change name now */
2424 if (buf != curbuf)
2425 return;
2426# ifdef FEAT_EVAL
2427 if (aborting()) /* autocmds may abort script processing */
2428 return;
2429# endif
2430#endif
2431 /*
2432 * The name of the current buffer will be changed.
2433 * A new (unlisted) buffer entry needs to be made to hold the old file
2434 * name, which will become the alternate file name.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002435 * But don't set the alternate file name if the buffer didn't have a
2436 * name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
2438 fname = curbuf->b_ffname;
2439 sfname = curbuf->b_sfname;
2440 xfname = curbuf->b_fname;
2441 curbuf->b_ffname = NULL;
2442 curbuf->b_sfname = NULL;
2443 if (setfname(curbuf, eap->arg, NULL, TRUE) == FAIL)
2444 {
2445 curbuf->b_ffname = fname;
2446 curbuf->b_sfname = sfname;
2447 return;
2448 }
2449 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002450 if (xfname != NULL && *xfname != NUL)
2451 {
2452 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2453 if (buf != NULL && !cmdmod.keepalt)
2454 curwin->w_alt_fnum = buf->b_fnum;
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 vim_free(fname);
2457 vim_free(sfname);
2458#ifdef FEAT_AUTOCMD
2459 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
2460#endif
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002461 /* Change directories when the 'acd' option is set. */
2462 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 /* print full file name if :cd used */
2465 fileinfo(FALSE, FALSE, eap->forceit);
2466}
2467
2468/*
2469 * ":update".
2470 */
2471 void
2472ex_update(eap)
2473 exarg_T *eap;
2474{
2475 if (curbufIsChanged())
2476 (void)do_write(eap);
2477}
2478
2479/*
2480 * ":write" and ":saveas".
2481 */
2482 void
2483ex_write(eap)
2484 exarg_T *eap;
2485{
2486 if (eap->usefilter) /* input lines to shell command */
2487 do_bang(1, eap, FALSE, TRUE, FALSE);
2488 else
2489 (void)do_write(eap);
2490}
2491
2492/*
2493 * write current buffer to file 'eap->arg'
2494 * if 'eap->append' is TRUE, append to the file
2495 *
2496 * if *eap->arg == NUL write to current file
2497 *
2498 * return FAIL for failure, OK otherwise
2499 */
2500 int
2501do_write(eap)
2502 exarg_T *eap;
2503{
2504 int other;
2505 char_u *fname = NULL; /* init to shut up gcc */
2506 char_u *ffname;
2507 int retval = FAIL;
2508 char_u *free_fname = NULL;
2509#ifdef FEAT_BROWSE
2510 char_u *browse_file = NULL;
2511#endif
2512 buf_T *alt_buf = NULL;
2513
2514 if (not_writing()) /* check 'write' option */
2515 return FAIL;
2516
2517 ffname = eap->arg;
2518#ifdef FEAT_BROWSE
2519 if (cmdmod.browse)
2520 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002521 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 NULL, NULL, NULL, curbuf);
2523 if (browse_file == NULL)
2524 goto theend;
2525 ffname = browse_file;
2526 }
2527#endif
2528 if (*ffname == NUL)
2529 {
2530 if (eap->cmdidx == CMD_saveas)
2531 {
2532 EMSG(_(e_argreq));
2533 goto theend;
2534 }
2535 other = FALSE;
2536 }
2537 else
2538 {
2539 fname = ffname;
2540 free_fname = fix_fname(ffname);
2541 /*
2542 * When out-of-memory, keep unexpanded file name, because we MUST be
2543 * able to write the file in this situation.
2544 */
2545 if (free_fname != NULL)
2546 ffname = free_fname;
2547 other = otherfile(ffname);
2548 }
2549
2550 /*
2551 * If we have a new file, put its name in the list of alternate file names.
2552 */
2553 if (other)
2554 {
2555 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2556 || eap->cmdidx == CMD_saveas)
2557 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2558 else
2559 alt_buf = buflist_findname(ffname);
2560 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2561 {
2562 /* Overwriting a file that is loaded in another buffer is not a
2563 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002564 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 goto theend;
2566 }
2567 }
2568
2569 /*
2570 * Writing to the current file is not allowed in readonly mode
2571 * and a file name is required.
2572 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2573 */
2574 if (!other && (
2575#ifdef FEAT_QUICKFIX
2576 bt_dontwrite_msg(curbuf) ||
2577#endif
2578 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2579 goto theend;
2580
2581 if (!other)
2582 {
2583 ffname = curbuf->b_ffname;
2584 fname = curbuf->b_fname;
2585 /*
2586 * Not writing the whole file is only allowed with '!'.
2587 */
2588 if ( (eap->line1 != 1
2589 || eap->line2 != curbuf->b_ml.ml_line_count)
2590 && !eap->forceit
2591 && !eap->append
2592 && !p_wa)
2593 {
2594#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2595 if (p_confirm || cmdmod.confirm)
2596 {
2597 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2598 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2599 goto theend;
2600 eap->forceit = TRUE;
2601 }
2602 else
2603#endif
2604 {
2605 EMSG(_("E140: Use ! to write partial buffer"));
2606 goto theend;
2607 }
2608 }
2609 }
2610
2611 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2612 {
2613 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2614 {
2615#ifdef FEAT_AUTOCMD
2616 buf_T *was_curbuf = curbuf;
2617
2618 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002619 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620# ifdef FEAT_EVAL
2621 if (curbuf != was_curbuf || aborting())
2622# else
2623 if (curbuf != was_curbuf)
2624# endif
2625 {
2626 /* buffer changed, don't change name now */
2627 retval = FAIL;
2628 goto theend;
2629 }
2630#endif
2631 /* Exchange the file names for the current and the alternate
2632 * buffer. This makes it look like we are now editing the buffer
2633 * under the new name. Must be done before buf_write(), because
2634 * if there is no file name and 'cpo' contains 'F', it will set
2635 * the file name. */
2636 fname = alt_buf->b_fname;
2637 alt_buf->b_fname = curbuf->b_fname;
2638 curbuf->b_fname = fname;
2639 fname = alt_buf->b_ffname;
2640 alt_buf->b_ffname = curbuf->b_ffname;
2641 curbuf->b_ffname = fname;
2642 fname = alt_buf->b_sfname;
2643 alt_buf->b_sfname = curbuf->b_sfname;
2644 curbuf->b_sfname = fname;
2645 buf_name_changed(curbuf);
2646#ifdef FEAT_AUTOCMD
2647 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002648 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (!alt_buf->b_p_bl)
2650 {
2651 alt_buf->b_p_bl = TRUE;
2652 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2653 }
2654# ifdef FEAT_EVAL
2655 if (curbuf != was_curbuf || aborting())
2656# else
2657 if (curbuf != was_curbuf)
2658# endif
2659 {
2660 /* buffer changed, don't write the file */
2661 retval = FAIL;
2662 goto theend;
2663 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002664
2665 /* If 'filetype' was empty try detecting it now. */
2666 if (*curbuf->b_p_ft == NUL)
2667 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002668 if (au_has_group((char_u *)"filetypedetect"))
2669 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
2670 TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002671 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673#endif
2674 }
2675
2676 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2677 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002678
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002679 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002680 if (eap->cmdidx == CMD_saveas)
2681 {
2682 if (retval == OK)
2683 curbuf->b_p_ro = FALSE;
2684 /* Change directories when the 'acd' option is set. */
2685 DO_AUTOCHDIR
2686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688
2689theend:
2690#ifdef FEAT_BROWSE
2691 vim_free(browse_file);
2692#endif
2693 vim_free(free_fname);
2694 return retval;
2695}
2696
2697/*
2698 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2699 * BF_NEW or BF_READERR, check for overwriting current file.
2700 * May set eap->forceit if a dialog says it's OK to overwrite.
2701 * Return OK if it's OK, FAIL if it is not.
2702 */
2703/*ARGSUSED*/
2704 static int
2705check_overwrite(eap, buf, fname, ffname, other)
2706 exarg_T *eap;
2707 buf_T *buf;
2708 char_u *fname; /* file name to be used (can differ from
2709 buf->ffname) */
2710 char_u *ffname; /* full path version of fname */
2711 int other; /* writing under other name */
2712{
2713 /*
2714 * write to other file or b_flags set or not writing the whole file:
2715 * overwriting only allowed with '!'
2716 */
2717 if ( (other
2718 || (buf->b_flags & BF_NOTEDITED)
2719 || ((buf->b_flags & BF_NEW)
2720 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2721 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 && !p_wa
2723 && vim_fexists(ffname))
2724 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002725 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002727#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002728 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002729 if (mch_isdir(ffname))
2730 {
2731 EMSG2(_(e_isadir2), ffname);
2732 return FAIL;
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#endif
2735#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002736 if (p_confirm || cmdmod.confirm)
2737 {
2738 char_u buff[IOSIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002740 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2741 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2742 return FAIL;
2743 eap->forceit = TRUE;
2744 }
2745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002747 {
2748 EMSG(_(e_exists));
2749 return FAIL;
2750 }
2751 }
2752
2753 /* For ":w! filename" check that no swap file exists for "filename". */
2754 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002756 char_u dir[MAXPATHL];
2757 char_u *p;
2758 int r;
2759 char_u *swapname;
2760
2761 /* We only try the first entry in 'directory', without checking if
2762 * it's writable. If the "." directory is not writable the write
2763 * will probably fail anyway.
2764 * Use 'shortname' of the current buffer, since there is no buffer
2765 * for the written file. */
2766 if (*p_dir == NUL)
2767 STRCPY(dir, ".");
2768 else
2769 {
2770 p = p_dir;
2771 copy_option_part(&p, dir, MAXPATHL, ",");
2772 }
2773 swapname = makeswapname(fname, ffname, curbuf, dir);
2774 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002775 if (r)
2776 {
2777#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2778 if (p_confirm || cmdmod.confirm)
2779 {
2780 char_u buff[IOSIZE];
2781
2782 dialog_msg(buff,
2783 _("Swap file \"%s\" exists, overwrite anyway?"),
2784 swapname);
2785 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2786 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002787 {
2788 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002789 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002790 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002791 eap->forceit = TRUE;
2792 }
2793 else
2794#endif
2795 {
2796 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2797 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002798 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002799 return FAIL;
2800 }
2801 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002802 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 }
2805 return OK;
2806}
2807
2808/*
2809 * Handle ":wnext", ":wNext" and ":wprevious" commands.
2810 */
2811 void
2812ex_wnext(eap)
2813 exarg_T *eap;
2814{
2815 int i;
2816
2817 if (eap->cmd[1] == 'n')
2818 i = curwin->w_arg_idx + (int)eap->line2;
2819 else
2820 i = curwin->w_arg_idx - (int)eap->line2;
2821 eap->line1 = 1;
2822 eap->line2 = curbuf->b_ml.ml_line_count;
2823 if (do_write(eap) != FAIL)
2824 do_argfile(eap, i);
2825}
2826
2827/*
2828 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
2829 */
2830 void
2831do_wqall(eap)
2832 exarg_T *eap;
2833{
2834 buf_T *buf;
2835 int error = 0;
2836 int save_forceit = eap->forceit;
2837
2838 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
2839 exiting = TRUE;
2840
2841 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2842 {
2843 if (bufIsChanged(buf))
2844 {
2845 /*
2846 * Check if there is a reason the buffer cannot be written:
2847 * 1. if the 'write' option is set
2848 * 2. if there is no file name (even after browsing)
2849 * 3. if the 'readonly' is set (even after a dialog)
2850 * 4. if overwriting is allowed (even after a dialog)
2851 */
2852 if (not_writing())
2853 {
2854 ++error;
2855 break;
2856 }
2857#ifdef FEAT_BROWSE
2858 /* ":browse wall": ask for file name if there isn't one */
2859 if (buf->b_ffname == NULL && cmdmod.browse)
2860 browse_save_fname(buf);
2861#endif
2862 if (buf->b_ffname == NULL)
2863 {
2864 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
2865 ++error;
2866 }
2867 else if (check_readonly(&eap->forceit, buf)
2868 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
2869 FALSE) == FAIL)
2870 {
2871 ++error;
2872 }
2873 else
2874 {
2875 if (buf_write_all(buf, eap->forceit) == FAIL)
2876 ++error;
2877#ifdef FEAT_AUTOCMD
2878 /* an autocommand may have deleted the buffer */
2879 if (!buf_valid(buf))
2880 buf = firstbuf;
2881#endif
2882 }
2883 eap->forceit = save_forceit; /* check_overwrite() may set it */
2884 }
2885 }
2886 if (exiting)
2887 {
2888 if (!error)
2889 getout(0); /* exit Vim */
2890 not_exiting();
2891 }
2892}
2893
2894/*
2895 * Check the 'write' option.
2896 * Return TRUE and give a message when it's not st.
2897 */
2898 int
2899not_writing()
2900{
2901 if (p_write)
2902 return FALSE;
2903 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
2904 return TRUE;
2905}
2906
2907/*
2908 * Check if a buffer is read-only. Ask for overruling in a dialog.
2909 * Return TRUE and give an error message when the buffer is readonly.
2910 */
2911 static int
2912check_readonly(forceit, buf)
2913 int *forceit;
2914 buf_T *buf;
2915{
2916 if (!*forceit && buf->b_p_ro)
2917 {
2918#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2919 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
2920 {
2921 char_u buff[IOSIZE];
2922
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002923 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 buf->b_fname);
2925
2926 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
2927 {
2928 /* Set forceit, to force the writing of a readonly file */
2929 *forceit = TRUE;
2930 return FALSE;
2931 }
2932 else
2933 return TRUE;
2934 }
2935 else
2936#endif
2937 EMSG(_(e_readonly));
2938 return TRUE;
2939 }
2940 return FALSE;
2941}
2942
2943/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002944 * Try to abandon current file and edit a new or existing file.
2945 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002947 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
2948 * -1 for succesfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 * 'lnum' is the line number for the cursor in the new file (if non-zero).
2950 */
2951 int
2952getfile(fnum, ffname, sfname, setpm, lnum, forceit)
2953 int fnum;
2954 char_u *ffname;
2955 char_u *sfname;
2956 int setpm;
2957 linenr_T lnum;
2958 int forceit;
2959{
2960 int other;
2961 int retval;
2962 char_u *free_me = NULL;
2963
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002964 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002966#ifdef FEAT_AUTOCMD
2967 if (curbuf_locked())
2968 return 1;
2969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971 if (fnum == 0)
2972 {
2973 /* make ffname full path, set sfname */
2974 fname_expand(curbuf, &ffname, &sfname);
2975 other = otherfile(ffname);
2976 free_me = ffname; /* has been allocated, free() later */
2977 }
2978 else
2979 other = (fnum != curbuf->b_fnum);
2980
2981 if (other)
2982 ++no_wait_return; /* don't wait for autowrite message */
2983 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
2984 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
2985 {
2986#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2987 if (p_confirm && p_write)
2988 dialog_changed(curbuf, FALSE);
2989 if (curbufIsChanged())
2990#endif
2991 {
2992 if (other)
2993 --no_wait_return;
2994 EMSG(_(e_nowrtmsg));
2995 retval = 2; /* file has been changed */
2996 goto theend;
2997 }
2998 }
2999 if (other)
3000 --no_wait_return;
3001 if (setpm)
3002 setpcmark();
3003 if (!other)
3004 {
3005 if (lnum != 0)
3006 curwin->w_cursor.lnum = lnum;
3007 check_cursor_lnum();
3008 beginline(BL_SOL | BL_FIX);
3009 retval = 0; /* it's in the same file */
3010 }
3011 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
3012 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0)) == OK)
3013 retval = -1; /* opened another file */
3014 else
3015 retval = 1; /* error encountered */
3016
3017theend:
3018 vim_free(free_me);
3019 return retval;
3020}
3021
3022/*
3023 * start editing a new file
3024 *
3025 * fnum: file number; if zero use ffname/sfname
3026 * ffname: the file name
3027 * - full path if sfname used,
3028 * - any file name if sfname is NULL
3029 * - empty string to re-edit with the same file name (but may be
3030 * in a different directory)
3031 * - NULL to start an empty buffer
3032 * sfname: the short file name (or NULL)
3033 * eap: contains the command to be executed after loading the file and
3034 * forced 'ff' and 'fenc'
3035 * newlnum: if > 0: put cursor on this line number (if possible)
3036 * if ECMD_LASTL: use last position in loaded file
3037 * if ECMD_LAST: use last position in all files
3038 * if ECMD_ONE: use first line
3039 * flags:
3040 * ECMD_HIDE: if TRUE don't free the current buffer
3041 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3042 * ECMD_OLDBUF: use existing buffer if it exists
3043 * ECMD_FORCEIT: ! used for Ex command
3044 * ECMD_ADDBUF: don't edit, just add to buffer list
3045 *
3046 * return FAIL for failure, OK otherwise
3047 */
3048 int
3049do_ecmd(fnum, ffname, sfname, eap, newlnum, flags)
3050 int fnum;
3051 char_u *ffname;
3052 char_u *sfname;
3053 exarg_T *eap; /* can be NULL! */
3054 linenr_T newlnum;
3055 int flags;
3056{
3057 int other_file; /* TRUE if editing another file */
3058 int oldbuf; /* TRUE if using existing buffer */
3059#ifdef FEAT_AUTOCMD
3060 int auto_buf = FALSE; /* TRUE if autocommands brought us
3061 into the buffer unexpectedly */
3062 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003063 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif
3065 buf_T *buf;
3066#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3067 buf_T *old_curbuf = curbuf;
3068#endif
3069 char_u *free_fname = NULL;
3070#ifdef FEAT_BROWSE
3071 char_u *browse_file = NULL;
3072#endif
3073 int retval = FAIL;
3074 long n;
3075 linenr_T lnum;
3076 linenr_T topline = 0;
3077 int newcol = -1;
3078 int solcol = -1;
3079 pos_T *pos;
3080#ifdef FEAT_SUN_WORKSHOP
3081 char_u *cp;
3082#endif
3083 char_u *command = NULL;
3084
3085 if (eap != NULL)
3086 command = eap->do_ecmd_cmd;
3087
3088 if (fnum != 0)
3089 {
3090 if (fnum == curbuf->b_fnum) /* file is already being edited */
3091 return OK; /* nothing to do */
3092 other_file = TRUE;
3093 }
3094 else
3095 {
3096#ifdef FEAT_BROWSE
3097 if (cmdmod.browse)
3098 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003099 if (
3100# ifdef FEAT_GUI
3101 !gui.in_use &&
3102# endif
3103 au_has_group((char_u *)"FileExplorer"))
3104 {
3105 /* No browsing supported but we do have the file explorer:
3106 * Edit the directory. */
3107 if (ffname == NULL || !mch_isdir(ffname))
3108 ffname = (char_u *)".";
3109 }
3110 else
3111 {
3112 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003114 if (browse_file == NULL)
3115 goto theend;
3116 ffname = browse_file;
3117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119#endif
3120 /* if no short name given, use ffname for short name */
3121 if (sfname == NULL)
3122 sfname = ffname;
3123#ifdef USE_FNAME_CASE
3124# ifdef USE_LONG_FNAME
3125 if (USE_LONG_FNAME)
3126# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003127 if (sfname != NULL)
3128 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#endif
3130
3131#ifdef FEAT_LISTCMDS
3132 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3133 goto theend;
3134#endif
3135
3136 if (ffname == NULL)
3137 other_file = TRUE;
3138 /* there is no file name */
3139 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3140 other_file = FALSE;
3141 else
3142 {
3143 if (*ffname == NUL) /* re-edit with same file name */
3144 {
3145 ffname = curbuf->b_ffname;
3146 sfname = curbuf->b_fname;
3147 }
3148 free_fname = fix_fname(ffname); /* may expand to full path name */
3149 if (free_fname != NULL)
3150 ffname = free_fname;
3151 other_file = otherfile(ffname);
3152#ifdef FEAT_SUN_WORKSHOP
3153 if (usingSunWorkShop && p_acd
3154 && (cp = vim_strrchr(sfname, '/')) != NULL)
3155 sfname = ++cp;
3156#endif
3157 }
3158 }
3159
3160 /*
3161 * if the file was changed we may not be allowed to abandon it
3162 * - if we are going to re-edit the same file
3163 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3164 */
3165 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3166 || (curbuf->b_nwindows == 1
3167 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
3168 && check_changed(curbuf, p_awa, !other_file,
3169 (flags & ECMD_FORCEIT), FALSE))
3170 {
3171 if (fnum == 0 && other_file && ffname != NULL)
3172 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3173 goto theend;
3174 }
3175
3176#ifdef FEAT_VISUAL
3177 /*
3178 * End Visual mode before switching to another buffer, so the text can be
3179 * copied into the GUI selection buffer.
3180 */
3181 reset_VIsual();
3182#endif
3183
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003184#ifdef FEAT_AUTOCMD
3185 if ((command != NULL || newlnum > (linenr_T)0)
3186 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3187 {
3188 int len;
3189 char_u *p;
3190
3191 /* Set v:swapcommand for the SwapExists autocommands. */
3192 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003193 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003194 else
3195 len = 30;
3196 p = alloc((unsigned)len);
3197 if (p != NULL)
3198 {
3199 if (command != NULL)
3200 vim_snprintf((char *)p, len, ":%s\r", command);
3201 else
3202 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3203 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3204 did_set_swapcommand = TRUE;
3205 vim_free(p);
3206 }
3207 }
3208#endif
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 /*
3211 * If we are starting to edit another file, open a (new) buffer.
3212 * Otherwise we re-use the current buffer.
3213 */
3214 if (other_file)
3215 {
3216#ifdef FEAT_LISTCMDS
3217 if (!(flags & ECMD_ADDBUF))
3218#endif
3219 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003220 if (!cmdmod.keepalt)
3221 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 buflist_altfpos();
3223 }
3224
3225 if (fnum)
3226 buf = buflist_findnr(fnum);
3227 else
3228 {
3229#ifdef FEAT_LISTCMDS
3230 if (flags & ECMD_ADDBUF)
3231 {
3232 linenr_T tlnum = 1L;
3233
3234 if (command != NULL)
3235 {
3236 tlnum = atol((char *)command);
3237 if (tlnum <= 0)
3238 tlnum = 1L;
3239 }
3240 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3241 goto theend;
3242 }
3243#endif
3244 buf = buflist_new(ffname, sfname, 0L,
3245 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
3246 }
3247 if (buf == NULL)
3248 goto theend;
3249 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3250 {
3251 oldbuf = FALSE;
3252 buf->b_nwindows = 0;
3253 }
3254 else /* existing memfile */
3255 {
3256 oldbuf = TRUE;
3257 (void)buf_check_timestamp(buf, FALSE);
3258 /* Check if autocommands made buffer invalid or changed the current
3259 * buffer. */
3260 if (!buf_valid(buf)
3261#ifdef FEAT_AUTOCMD
3262 || curbuf != old_curbuf
3263#endif
3264 )
3265 goto theend;
3266#ifdef FEAT_EVAL
3267 if (aborting()) /* autocmds may abort script processing */
3268 goto theend;
3269#endif
3270 }
3271
3272 /* May jump to last used line number for a loaded buffer or when asked
3273 * for explicitly */
3274 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3275 {
3276 pos = buflist_findfpos(buf);
3277 newlnum = pos->lnum;
3278 solcol = pos->col;
3279 }
3280
3281 /*
3282 * Make the (new) buffer the one used by the current window.
3283 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3284 * If the current buffer was empty and has no file name, curbuf
3285 * is returned by buflist_new().
3286 */
3287 if (buf != curbuf)
3288 {
3289#ifdef FEAT_AUTOCMD
3290 /*
3291 * Be careful: The autocommands may delete any buffer and change
3292 * the current buffer.
3293 * - If the buffer we are going to edit is deleted, give up.
3294 * - If the current buffer is deleted, prefer to load the new
3295 * buffer when loading a buffer is required. This avoids
3296 * loading another buffer which then must be closed again.
3297 * - If we ended up in the new buffer already, need to skip a few
3298 * things, set auto_buf.
3299 */
3300 if (buf->b_fname != NULL)
3301 new_name = vim_strsave(buf->b_fname);
3302 au_new_curbuf = buf;
3303 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3304 if (!buf_valid(buf)) /* new buffer has been deleted */
3305 {
3306 delbuf_msg(new_name); /* frees new_name */
3307 goto theend;
3308 }
3309# ifdef FEAT_EVAL
3310 if (aborting()) /* autocmds may abort script processing */
3311 {
3312 vim_free(new_name);
3313 goto theend;
3314 }
3315# endif
3316 if (buf == curbuf) /* already in new buffer */
3317 auto_buf = TRUE;
3318 else
3319 {
3320 if (curbuf == old_curbuf)
3321#endif
3322 buf_copy_options(buf, BCO_ENTER);
3323
3324 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003325 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 close_buffer(curwin, curbuf,
3327 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD);
3328
3329#ifdef FEAT_AUTOCMD
3330# ifdef FEAT_EVAL
3331 if (aborting()) /* autocmds may abort script processing */
3332 {
3333 vim_free(new_name);
3334 goto theend;
3335 }
3336# endif
3337 /* Be careful again, like above. */
3338 if (!buf_valid(buf)) /* new buffer has been deleted */
3339 {
3340 delbuf_msg(new_name); /* frees new_name */
3341 goto theend;
3342 }
3343 if (buf == curbuf) /* already in new buffer */
3344 auto_buf = TRUE;
3345 else
3346#endif
3347 {
3348 curwin->w_buffer = buf;
3349 curbuf = buf;
3350 ++curbuf->b_nwindows;
3351 /* set 'fileformat' */
3352 if (*p_ffs && !oldbuf)
3353 set_fileformat(default_fileformat(), OPT_LOCAL);
3354 }
3355
3356 /* May get the window options from the last time this buffer
3357 * was in this window (or another window). If not used
3358 * before, reset the local window options to the global
3359 * values. Also restores old folding stuff. */
3360 get_winopts(buf);
3361
3362#ifdef FEAT_AUTOCMD
3363 }
3364 vim_free(new_name);
3365 au_new_curbuf = NULL;
3366#endif
3367 }
3368 else
3369 ++curbuf->b_nwindows;
3370
3371 curwin->w_pcmark.lnum = 1;
3372 curwin->w_pcmark.col = 0;
3373 }
3374 else /* !other_file */
3375 {
3376 if (
3377#ifdef FEAT_LISTCMDS
3378 (flags & ECMD_ADDBUF) ||
3379#endif
3380 check_fname() == FAIL)
3381 goto theend;
3382 oldbuf = (flags & ECMD_OLDBUF);
3383 }
3384
3385 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3386 {
3387 char_u *p;
3388
3389 curbuf->b_help = TRUE;
3390#ifdef FEAT_QUICKFIX
3391 set_string_option_direct((char_u *)"buftype", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003392 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
3394
3395 /*
3396 * Always set these options after jumping to a help tag, because the
3397 * user may have an autocommand that gets in the way.
3398 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
3399 * latin1 word characters (for translated help files).
3400 * Only set it when needed, buf_init_chartab() is some work.
3401 */
3402 p =
3403#ifdef EBCDIC
3404 (char_u *)"65-255,^*,^|,^\"";
3405#else
3406 (char_u *)"!-~,^*,^|,^\",192-255";
3407#endif
3408 if (STRCMP(curbuf->b_p_isk, p) != 0)
3409 {
3410 set_string_option_direct((char_u *)"isk", -1, p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003411 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 check_buf_options(curbuf);
3413 (void)buf_init_chartab(curbuf, FALSE);
3414 }
3415
3416 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
3417 curwin->w_p_list = FALSE; /* no list mode */
3418
3419 curbuf->b_p_ma = FALSE; /* not modifiable */
3420 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
3421 curwin->w_p_nu = 0; /* no line numbers */
3422#ifdef FEAT_SCROLLBIND
3423 curwin->w_p_scb = FALSE; /* no scroll binding */
3424#endif
3425#ifdef FEAT_ARABIC
3426 curwin->w_p_arab = FALSE; /* no arabic mode */
3427#endif
3428#ifdef FEAT_RIGHTLEFT
3429 curwin->w_p_rl = FALSE; /* help window is left-to-right */
3430#endif
3431#ifdef FEAT_FOLDING
3432 curwin->w_p_fen = FALSE; /* No folding in the help window */
3433#endif
3434#ifdef FEAT_DIFF
3435 curwin->w_p_diff = FALSE; /* No 'diff' */
3436#endif
Bram Moolenaara1956f62006-03-12 22:18:00 +00003437#ifdef FEAT_SPELL
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003438 curwin->w_p_spell = FALSE; /* No spell checking */
3439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441#ifdef FEAT_AUTOCMD
3442 buf = curbuf;
3443#endif
3444 set_buflisted(FALSE);
3445 }
3446 else
3447 {
3448#ifdef FEAT_AUTOCMD
3449 buf = curbuf;
3450#endif
3451 /* Don't make a buffer listed if it's a help buffer. Useful when
3452 * using CTRL-O to go back to a help file. */
3453 if (!curbuf->b_help)
3454 set_buflisted(TRUE);
3455 }
3456
3457#ifdef FEAT_AUTOCMD
3458 /* If autocommands change buffers under our fingers, forget about
3459 * editing the file. */
3460 if (buf != curbuf)
3461 goto theend;
3462# ifdef FEAT_EVAL
3463 if (aborting()) /* autocmds may abort script processing */
3464 goto theend;
3465# endif
3466
3467 /* Since we are starting to edit a file, consider the filetype to be
3468 * unset. Helps for when an autocommand changes files and expects syntax
3469 * highlighting to work in the other file. */
3470 did_filetype = FALSE;
3471#endif
3472
3473/*
3474 * other_file oldbuf
3475 * FALSE FALSE re-edit same file, buffer is re-used
3476 * FALSE TRUE re-edit same file, nothing changes
3477 * TRUE FALSE start editing new file, new buffer
3478 * TRUE TRUE start editing in existing buffer (nothing to do)
3479 */
3480 if (!other_file && !oldbuf) /* re-use the buffer */
3481 {
3482 set_last_cursor(curwin); /* may set b_last_cursor */
3483 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3484 {
3485 newlnum = curwin->w_cursor.lnum;
3486 solcol = curwin->w_cursor.col;
3487 }
3488#ifdef FEAT_AUTOCMD
3489 buf = curbuf;
3490 if (buf->b_fname != NULL)
3491 new_name = vim_strsave(buf->b_fname);
3492 else
3493 new_name = NULL;
3494#endif
3495 buf_freeall(curbuf, FALSE, FALSE); /* free all things for buffer */
3496#ifdef FEAT_AUTOCMD
3497 /* If autocommands deleted the buffer we were going to re-edit, give
3498 * up and jump to the end. */
3499 if (!buf_valid(buf))
3500 {
3501 delbuf_msg(new_name); /* frees new_name */
3502 goto theend;
3503 }
3504 vim_free(new_name);
3505
3506 /* If autocommands change buffers under our fingers, forget about
3507 * re-editing the file. Should do the buf_clear_file(), but perhaps
3508 * the autocommands changed the buffer... */
3509 if (buf != curbuf)
3510 goto theend;
3511# ifdef FEAT_EVAL
3512 if (aborting()) /* autocmds may abort script processing */
3513 goto theend;
3514# endif
3515#endif
3516 buf_clear_file(curbuf);
3517 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3518 curbuf->b_op_end.lnum = 0;
3519 }
3520
3521/*
3522 * If we get here we are sure to start editing
3523 */
3524 /* don't redraw until the cursor is in the right line */
3525 ++RedrawingDisabled;
3526
3527 /* Assume success now */
3528 retval = OK;
3529
3530 /*
3531 * Reset cursor position, could be used by autocommands.
3532 */
3533 check_cursor();
3534
3535 /*
3536 * Check if we are editing the w_arg_idx file in the argument list.
3537 */
3538 check_arg_idx(curwin);
3539
3540#ifdef FEAT_AUTOCMD
3541 if (!auto_buf)
3542#endif
3543 {
3544 /*
3545 * Set cursor and init window before reading the file and executing
3546 * autocommands. This allows for the autocommands to position the
3547 * cursor.
3548 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003549 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551#ifdef FEAT_FOLDING
3552 /* It's like all lines in the buffer changed. Need to update
3553 * automatic folding. */
3554 foldUpdateAll(curwin);
3555#endif
3556
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003557 /* Change directories when the 'acd' option is set. */
3558 DO_AUTOCHDIR
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 /*
3561 * Careful: open_buffer() and apply_autocmds() may change the current
3562 * buffer and window.
3563 */
3564 lnum = curwin->w_cursor.lnum;
3565 topline = curwin->w_topline;
3566 if (!oldbuf) /* need to read the file */
3567 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003568#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 swap_exists_action = SEA_DIALOG;
3570#endif
3571 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3572
3573 /*
3574 * Open the buffer and read the file.
3575 */
3576#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3577 if (should_abort(open_buffer(FALSE, eap)))
3578 retval = FAIL;
3579#else
3580 (void)open_buffer(FALSE, eap);
3581#endif
3582
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003583#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 if (swap_exists_action == SEA_QUIT)
3585 retval = FAIL;
3586 handle_swap_exists(old_curbuf);
3587#endif
3588 }
3589#ifdef FEAT_AUTOCMD
3590 else
3591 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003592 /* Read the modelines, but only to set window-local options. Any
3593 * buffer-local options have already been set and may have been
3594 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00003595 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3598 &retval);
3599 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3600 &retval);
3601 }
3602 check_arg_idx(curwin);
3603#endif
3604
3605 /*
3606 * If autocommands change the cursor position or topline, we should
3607 * keep it.
3608 */
3609 if (curwin->w_cursor.lnum != lnum)
3610 {
3611 newlnum = curwin->w_cursor.lnum;
3612 newcol = curwin->w_cursor.col;
3613 }
3614 if (curwin->w_topline == topline)
3615 topline = 0;
3616
3617 /* Even when cursor didn't move we need to recompute topline. */
3618 changed_line_abv_curs();
3619
3620#ifdef FEAT_TITLE
3621 maketitle();
3622#endif
3623 }
3624
3625#ifdef FEAT_DIFF
3626 /* Tell the diff stuff that this buffer is new and/or needs updating.
3627 * Also needed when re-editing the same buffer, because unloading will
3628 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003629 if (curwin->w_p_diff)
3630 {
3631 diff_buf_add(curbuf);
3632 diff_invalidate(curbuf);
3633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
3635
3636 if (command == NULL)
3637 {
3638 if (newcol >= 0) /* position set by autocommands */
3639 {
3640 curwin->w_cursor.lnum = newlnum;
3641 curwin->w_cursor.col = newcol;
3642 check_cursor();
3643 }
3644 else if (newlnum > 0) /* line number from caller or old position */
3645 {
3646 curwin->w_cursor.lnum = newlnum;
3647 check_cursor_lnum();
3648 if (solcol >= 0 && !p_sol)
3649 {
3650 /* 'sol' is off: Use last known column. */
3651 curwin->w_cursor.col = solcol;
3652 check_cursor_col();
3653#ifdef FEAT_VIRTUALEDIT
3654 curwin->w_cursor.coladd = 0;
3655#endif
3656 curwin->w_set_curswant = TRUE;
3657 }
3658 else
3659 beginline(BL_SOL | BL_FIX);
3660 }
3661 else /* no line number, go to last line in Ex mode */
3662 {
3663 if (exmode_active)
3664 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3665 beginline(BL_WHITE | BL_FIX);
3666 }
3667 }
3668
3669#ifdef FEAT_WINDOWS
3670 /* Check if cursors in other windows on the same buffer are still valid */
3671 check_lnums(FALSE);
3672#endif
3673
3674 /*
3675 * Did not read the file, need to show some info about the file.
3676 * Do this after setting the cursor.
3677 */
3678 if (oldbuf
3679#ifdef FEAT_AUTOCMD
3680 && !auto_buf
3681#endif
3682 )
3683 {
3684 int msg_scroll_save = msg_scroll;
3685
3686 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3687 * message. */
3688 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3689 msg_scroll = FALSE;
3690 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3691 check_for_delay(FALSE);
3692 msg_start();
3693 msg_scroll = msg_scroll_save;
3694 msg_scrolled_ign = TRUE;
3695
3696 fileinfo(FALSE, TRUE, FALSE);
3697
3698 msg_scrolled_ign = FALSE;
3699 }
3700
3701 if (command != NULL)
3702 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3703
3704#ifdef FEAT_KEYMAP
3705 if (curbuf->b_kmap_state & KEYMAP_INIT)
3706 keymap_init();
3707#endif
3708
3709 --RedrawingDisabled;
3710 if (!skip_redraw)
3711 {
3712 n = p_so;
3713 if (topline == 0 && command == NULL)
3714 p_so = 999; /* force cursor halfway the window */
3715 update_topline();
3716#ifdef FEAT_SCROLLBIND
3717 curwin->w_scbind_pos = curwin->w_topline;
3718#endif
3719 p_so = n;
3720 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3721 }
3722
3723 if (p_im)
3724 need_start_insertmode = TRUE;
3725
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003726 /* Change directories when the 'acd' option is set. */
3727 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003729#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (gui.in_use && curbuf->b_ffname != NULL)
3731 {
3732# ifdef FEAT_SUN_WORKSHOP
3733 if (usingSunWorkShop)
3734 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3735# endif
3736# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003737 if (usingNetbeans & ((flags & ECMD_SET_HELP) != ECMD_SET_HELP))
3738 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739# endif
3740 }
3741#endif
3742
3743theend:
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003744#ifdef FEAT_AUTOCMD
3745 if (did_set_swapcommand)
3746 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
3747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748#ifdef FEAT_BROWSE
3749 vim_free(browse_file);
3750#endif
3751 vim_free(free_fname);
3752 return retval;
3753}
3754
3755#ifdef FEAT_AUTOCMD
3756 static void
3757delbuf_msg(name)
3758 char_u *name;
3759{
3760 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3761 name == NULL ? (char_u *)"" : name);
3762 vim_free(name);
3763 au_new_curbuf = NULL;
3764}
3765#endif
3766
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003767static int append_indent = 0; /* autoindent for first line */
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769/*
3770 * ":insert" and ":append", also used by ":change"
3771 */
3772 void
3773ex_append(eap)
3774 exarg_T *eap;
3775{
3776 char_u *theline;
3777 int did_undo = FALSE;
3778 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003779 int indent = 0;
3780 char_u *p;
3781 int vcol;
3782 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003784 /* the ! flag toggles autoindent */
3785 if (eap->forceit)
3786 curbuf->b_p_ai = !curbuf->b_p_ai;
3787
3788 /* First autoindent comes from the line we start on */
3789 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
3790 append_indent = get_indent_lnum(lnum);
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (eap->cmdidx != CMD_append)
3793 --lnum;
3794
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003795 /* when the buffer is empty append to line 0 and delete the dummy line */
3796 if (empty && lnum == 1)
3797 lnum = 0;
3798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 State = INSERT; /* behave like in Insert mode */
3800 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3801 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003802
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003803 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 msg_scroll = TRUE;
3806 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003807 if (curbuf->b_p_ai)
3808 {
3809 if (append_indent >= 0)
3810 {
3811 indent = append_indent;
3812 append_indent = -1;
3813 }
3814 else if (lnum > 0)
3815 indent = get_indent_lnum(lnum);
3816 }
3817 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003819 {
3820 /* No getline() function, use the lines that follow. This ends
3821 * when there is no more. */
3822 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
3823 break;
3824 p = vim_strchr(eap->nextcmd, NL);
3825 if (p == NULL)
3826 p = eap->nextcmd + STRLEN(eap->nextcmd);
3827 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
3828 if (*p != NUL)
3829 ++p;
3830 eap->nextcmd = p;
3831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
3833 theline = eap->getline(
3834#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003835 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003837 NUL, eap->cookie, indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003839 if (theline == NULL)
3840 break;
3841
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003842 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
3843 if (ex_keep_indent)
3844 append_indent = indent;
3845
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003846 /* Look for the "." after automatic indent. */
3847 vcol = 0;
3848 for (p = theline; indent > vcol; ++p)
3849 {
3850 if (*p == ' ')
3851 ++vcol;
3852 else if (*p == TAB)
3853 vcol += 8 - vcol % 8;
3854 else
3855 break;
3856 }
3857 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00003858 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
3859 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 vim_free(theline);
3862 break;
3863 }
3864
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003865 /* don't use autoindent if nothing was typed. */
3866 if (p[0] == NUL)
3867 theline[0] = NUL;
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 did_undo = TRUE;
3870 ml_append(lnum, theline, (colnr_T)0, FALSE);
3871 appended_lines_mark(lnum, 1L);
3872
3873 vim_free(theline);
3874 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003875
3876 if (empty)
3877 {
3878 ml_delete(2L, FALSE);
3879 empty = FALSE;
3880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 State = NORMAL;
3883
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003884 if (eap->forceit)
3885 curbuf->b_p_ai = !curbuf->b_p_ai;
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 /* "start" is set to eap->line2+1 unless that position is invalid (when
3888 * eap->line2 pointed to the end of the buffer and nothig was appended)
3889 * "end" is set to lnum when something has been appended, otherwise
3890 * it is the same than "start" -- Acevedo */
3891 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
3892 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
3893 if (eap->cmdidx != CMD_append)
3894 --curbuf->b_op_start.lnum;
3895 curbuf->b_op_end.lnum = (eap->line2 < lnum)
3896 ? lnum : curbuf->b_op_start.lnum;
3897 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
3898 curwin->w_cursor.lnum = lnum;
3899 check_cursor_lnum();
3900 beginline(BL_SOL | BL_FIX);
3901
3902 need_wait_return = FALSE; /* don't use wait_return() now */
3903 ex_no_reprint = TRUE;
3904}
3905
3906/*
3907 * ":change"
3908 */
3909 void
3910ex_change(eap)
3911 exarg_T *eap;
3912{
3913 linenr_T lnum;
3914
3915 if (eap->line2 >= eap->line1
3916 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
3917 return;
3918
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003919 /* the ! flag toggles autoindent */
3920 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
3921 append_indent = get_indent_lnum(eap->line1);
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
3924 {
3925 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
3926 break;
3927 ml_delete(eap->line1, FALSE);
3928 }
3929 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
3930
3931 /* ":append" on the line above the deleted lines. */
3932 eap->line2 = eap->line1;
3933 ex_append(eap);
3934}
3935
3936 void
3937ex_z(eap)
3938 exarg_T *eap;
3939{
3940 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003941 int bigness;
3942 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 int minus = 0;
3944 linenr_T start, end, curs, i;
3945 int j;
3946 linenr_T lnum = eap->line2;
3947
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003948 /* Vi compatible: ":z!" uses display height, without a count uses
3949 * 'scroll' */
3950 if (eap->forceit)
3951 bigness = curwin->w_height;
3952 else if (firstwin == lastwin)
3953 bigness = curwin->w_p_scr * 2;
3954 else
3955 bigness = curwin->w_height - 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (bigness < 1)
3957 bigness = 1;
3958
3959 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003960 kind = x;
3961 if (*kind == '-' || *kind == '+' || *kind == '='
3962 || *kind == '^' || *kind == '.')
3963 ++x;
3964 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 ++x;
3966
3967 if (*x != 0)
3968 {
3969 if (!VIM_ISDIGIT(*x))
3970 {
3971 EMSG(_("E144: non-numeric argument to :z"));
3972 return;
3973 }
3974 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003977 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003978 if (*kind == '=')
3979 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003983 /* the number of '-' and '+' multiplies the distance */
3984 if (*kind == '-' || *kind == '+')
3985 for (x = kind + 1; *x == *kind; ++x)
3986 ;
3987
3988 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 case '-':
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003991 start = lnum - bigness * (linenr_T)(x - kind);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003992 end = start + bigness;
3993 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 break;
3995
3996 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003997 start = lnum - (bigness + 1) / 2 + 1;
3998 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 curs = lnum;
4000 minus = 1;
4001 break;
4002
4003 case '^':
4004 start = lnum - bigness * 2;
4005 end = lnum - bigness;
4006 curs = lnum - bigness;
4007 break;
4008
4009 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004010 start = lnum - (bigness + 1) / 2 + 1;
4011 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 curs = end;
4013 break;
4014
4015 default: /* '+' */
4016 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004017 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004018 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004019 else if (eap->addr_count == 0)
4020 ++start;
4021 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 curs = end;
4023 break;
4024 }
4025
4026 if (start < 1)
4027 start = 1;
4028
4029 if (end > curbuf->b_ml.ml_line_count)
4030 end = curbuf->b_ml.ml_line_count;
4031
4032 if (curs > curbuf->b_ml.ml_line_count)
4033 curs = curbuf->b_ml.ml_line_count;
4034
4035 for (i = start; i <= end; i++)
4036 {
4037 if (minus && i == lnum)
4038 {
4039 msg_putchar('\n');
4040
4041 for (j = 1; j < Columns; j++)
4042 msg_putchar('-');
4043 }
4044
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004045 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047 if (minus && i == lnum)
4048 {
4049 msg_putchar('\n');
4050
4051 for (j = 1; j < Columns; j++)
4052 msg_putchar('-');
4053 }
4054 }
4055
4056 curwin->w_cursor.lnum = curs;
4057 ex_no_reprint = TRUE;
4058}
4059
4060/*
4061 * Check if the restricted flag is set.
4062 * If so, give an error message and return TRUE.
4063 * Otherwise, return FALSE.
4064 */
4065 int
4066check_restricted()
4067{
4068 if (restricted)
4069 {
4070 EMSG(_("E145: Shell commands not allowed in rvim"));
4071 return TRUE;
4072 }
4073 return FALSE;
4074}
4075
4076/*
4077 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4078 * If so, give an error message and return TRUE.
4079 * Otherwise, return FALSE.
4080 */
4081 int
4082check_secure()
4083{
4084 if (secure)
4085 {
4086 secure = 2;
4087 EMSG(_(e_curdir));
4088 return TRUE;
4089 }
4090#ifdef HAVE_SANDBOX
4091 /*
4092 * In the sandbox more things are not allowed, including the things
4093 * disallowed in secure mode.
4094 */
4095 if (sandbox != 0)
4096 {
4097 EMSG(_(e_sandbox));
4098 return TRUE;
4099 }
4100#endif
4101 return FALSE;
4102}
4103
4104static char_u *old_sub = NULL; /* previous substitute pattern */
4105static int global_need_beginline; /* call beginline() after ":g" */
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107/* do_sub()
4108 *
4109 * Perform a substitution from line eap->line1 to line eap->line2 using the
4110 * command pointed to by eap->arg which should be of the form:
4111 *
4112 * /pattern/substitution/{flags}
4113 *
4114 * The usual escapes are supported as described in the regexp docs.
4115 */
4116 void
4117do_sub(eap)
4118 exarg_T *eap;
4119{
4120 linenr_T lnum;
4121 long i = 0;
4122 regmmatch_T regmatch;
4123 static int do_all = FALSE; /* do multiple substitutions per line */
4124 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004125 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 static int do_error = TRUE; /* if false, ignore errors */
4127 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004128 static int do_list = FALSE; /* list last line with subs. */
4129 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 static int do_ic = 0; /* ignore case flag */
4131 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4132 int delimiter;
4133 int sublen;
4134 int got_quit = FALSE;
4135 int got_match = FALSE;
4136 int temp;
4137 int which_pat;
4138 char_u *cmd;
4139 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004140 linenr_T first_line = 0; /* first changed line */
4141 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * change */
4143 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4144 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004145 long nmatch; /* number of lines in match */
4146 linenr_T sub_firstlnum; /* nr of first sub line */
4147 char_u *sub_firstline; /* allocated copy of first sub line */
4148 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004149 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151 cmd = eap->arg;
4152 if (!global_busy)
4153 {
4154 sub_nsubs = 0;
4155 sub_nlines = 0;
4156 }
4157
4158#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4159 if (p_altkeymap && curwin->w_p_rl)
4160 lrF_sub(cmd);
4161#endif
4162
4163 if (eap->cmdidx == CMD_tilde)
4164 which_pat = RE_LAST; /* use last used regexp */
4165 else
4166 which_pat = RE_SUBST; /* use last substitute regexp */
4167
4168 /* new pattern and substitution */
4169 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4170 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4171 {
4172 /* don't accept alphanumeric for separator */
4173 if (isalpha(*cmd))
4174 {
4175 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4176 return;
4177 }
4178 /*
4179 * undocumented vi feature:
4180 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4181 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4182 */
4183 if (*cmd == '\\')
4184 {
4185 ++cmd;
4186 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4187 {
4188 EMSG(_(e_backslash));
4189 return;
4190 }
4191 if (*cmd != '&')
4192 which_pat = RE_SEARCH; /* use last '/' pattern */
4193 pat = (char_u *)""; /* empty search pattern */
4194 delimiter = *cmd++; /* remember delimiter character */
4195 }
4196 else /* find the end of the regexp */
4197 {
4198 which_pat = RE_LAST; /* use last used regexp */
4199 delimiter = *cmd++; /* remember delimiter character */
4200 pat = cmd; /* remember start of search pat */
4201 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4202 if (cmd[0] == delimiter) /* end delimiter found */
4203 *cmd++ = NUL; /* replace it with a NUL */
4204 }
4205
4206 /*
4207 * Small incompatibility: vi sees '\n' as end of the command, but in
4208 * Vim we want to use '\n' to find/substitute a NUL.
4209 */
4210 sub = cmd; /* remember the start of the substitution */
4211
4212 while (cmd[0])
4213 {
4214 if (cmd[0] == delimiter) /* end delimiter found */
4215 {
4216 *cmd++ = NUL; /* replace it with a NUL */
4217 break;
4218 }
4219 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4220 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004221 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223
4224 if (!eap->skip)
4225 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004226 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4227 if (STRCMP(sub, "%") == 0
4228 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4229 {
4230 if (old_sub == NULL) /* there is no previous command */
4231 {
4232 EMSG(_(e_nopresub));
4233 return;
4234 }
4235 sub = old_sub;
4236 }
4237 else
4238 {
4239 vim_free(old_sub);
4240 old_sub = vim_strsave(sub);
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244 else if (!eap->skip) /* use previous pattern and substitution */
4245 {
4246 if (old_sub == NULL) /* there is no previous command */
4247 {
4248 EMSG(_(e_nopresub));
4249 return;
4250 }
4251 pat = NULL; /* search_regcomp() will use previous pattern */
4252 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004253
4254 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4255 * last column after using "$". */
4256 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258
4259 /*
4260 * Find trailing options. When '&' is used, keep old options.
4261 */
4262 if (*cmd == '&')
4263 ++cmd;
4264 else
4265 {
4266 if (!p_ed)
4267 {
4268 if (p_gd) /* default is global on */
4269 do_all = TRUE;
4270 else
4271 do_all = FALSE;
4272 do_ask = FALSE;
4273 }
4274 do_error = TRUE;
4275 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004276 do_count = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 do_ic = 0;
4278 }
4279 while (*cmd)
4280 {
4281 /*
4282 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4283 * 'r' is never inverted.
4284 */
4285 if (*cmd == 'g')
4286 do_all = !do_all;
4287 else if (*cmd == 'c')
4288 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004289 else if (*cmd == 'n')
4290 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 else if (*cmd == 'e')
4292 do_error = !do_error;
4293 else if (*cmd == 'r') /* use last used regexp */
4294 which_pat = RE_LAST;
4295 else if (*cmd == 'p')
4296 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004297 else if (*cmd == '#')
4298 {
4299 do_print = TRUE;
4300 do_number = TRUE;
4301 }
4302 else if (*cmd == 'l')
4303 {
4304 do_print = TRUE;
4305 do_list = TRUE;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 else if (*cmd == 'i') /* ignore case */
4308 do_ic = 'i';
4309 else if (*cmd == 'I') /* don't ignore case */
4310 do_ic = 'I';
4311 else
4312 break;
4313 ++cmd;
4314 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004315 if (do_count)
4316 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 /*
4319 * check for a trailing count
4320 */
4321 cmd = skipwhite(cmd);
4322 if (VIM_ISDIGIT(*cmd))
4323 {
4324 i = getdigits(&cmd);
4325 if (i <= 0 && !eap->skip && do_error)
4326 {
4327 EMSG(_(e_zerocount));
4328 return;
4329 }
4330 eap->line1 = eap->line2;
4331 eap->line2 += i - 1;
4332 if (eap->line2 > curbuf->b_ml.ml_line_count)
4333 eap->line2 = curbuf->b_ml.ml_line_count;
4334 }
4335
4336 /*
4337 * check for trailing command or garbage
4338 */
4339 cmd = skipwhite(cmd);
4340 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4341 {
4342 eap->nextcmd = check_nextcmd(cmd);
4343 if (eap->nextcmd == NULL)
4344 {
4345 EMSG(_(e_trailing));
4346 return;
4347 }
4348 }
4349
4350 if (eap->skip) /* not executing commands, only parsing */
4351 return;
4352
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004353 if (!do_count && !curbuf->b_p_ma)
4354 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004355 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004356 EMSG(_(e_modifiable));
4357 return;
4358 }
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4361 {
4362 if (do_error)
4363 EMSG(_(e_invcmd));
4364 return;
4365 }
4366
4367 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4368 if (do_ic == 'i')
4369 regmatch.rmm_ic = TRUE;
4370 else if (do_ic == 'I')
4371 regmatch.rmm_ic = FALSE;
4372
4373 sub_firstline = NULL;
4374
4375 /*
4376 * ~ in the substitute pattern is replaced with the old pattern.
4377 * We do it here once to avoid it to be replaced over and over again.
4378 * But don't do it when it starts with "\=", then it's an expression.
4379 */
4380 if (!(sub[0] == '\\' && sub[1] == '='))
4381 sub = regtilde(sub, p_magic);
4382
4383 /*
4384 * Check for a match on each line.
4385 */
4386 line2 = eap->line2;
4387 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4388#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4389 || aborting()
4390#endif
4391 ); ++lnum)
4392 {
4393 sub_firstlnum = lnum;
4394 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
4395 if (nmatch)
4396 {
4397 colnr_T copycol;
4398 colnr_T matchcol;
4399 colnr_T prev_matchcol = MAXCOL;
4400 char_u *new_end, *new_start = NULL;
4401 unsigned new_start_len = 0;
4402 char_u *p1;
4403 int did_sub = FALSE;
4404 int lastone;
4405 unsigned len, needed_len;
4406 long nmatch_tl = 0; /* nr of lines matched below lnum */
4407 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004408 int skip_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 /*
4411 * The new text is build up step by step, to avoid too much
4412 * copying. There are these pieces:
4413 * sub_firstline The old text, unmodifed.
4414 * copycol Column in the old text where we started
4415 * looking for a match; from here old text still
4416 * needs to be copied to the new text.
4417 * matchcol Column number of the old text where to look
4418 * for the next match. It's just after the
4419 * previous match or one further.
4420 * prev_matchcol Column just after the previous match (if any).
4421 * Mostly equal to matchcol, except for the first
4422 * match and after skipping an empty match.
4423 * regmatch.*pos Where the pattern matched in the old text.
4424 * new_start The new text, all that has been produced so
4425 * far.
4426 * new_end The new text, where to append new text.
4427 *
4428 * lnum The line number where we were looking for the
4429 * first match in the old line.
4430 * sub_firstlnum The line number in the buffer where to look
4431 * for a match. Can be different from "lnum"
4432 * when the pattern or substitute string contains
4433 * line breaks.
4434 *
4435 * Special situations:
4436 * - When the substitute string contains a line break, the part up
4437 * to the line break is inserted in the text, but the copy of
4438 * the original line is kept. "sub_firstlnum" is adjusted for
4439 * the inserted lines.
4440 * - When the matched pattern contains a line break, the old line
4441 * is taken from the line at the end of the pattern. The lines
4442 * in the match are deleted later, "sub_firstlnum" is adjusted
4443 * accordingly.
4444 *
4445 * The new text is built up in new_start[]. It has some extra
4446 * room to avoid using alloc()/free() too often. new_start_len is
4447 * the lenght of the allocated memory at new_start.
4448 *
4449 * Make a copy of the old line, so it won't be taken away when
4450 * updating the screen or handling a multi-line match. The "old_"
4451 * pointers point into this copy.
4452 */
4453 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4454 if (sub_firstline == NULL)
4455 {
4456 vim_free(new_start);
4457 goto outofmem;
4458 }
4459 copycol = 0;
4460 matchcol = 0;
4461
4462 /* At first match, remember current cursor position. */
4463 if (!got_match)
4464 {
4465 setpcmark();
4466 got_match = TRUE;
4467 }
4468
4469 /*
4470 * Loop until nothing more to replace in this line.
4471 * 1. Handle match with empty string.
4472 * 2. If do_ask is set, ask for confirmation.
4473 * 3. substitute the string.
4474 * 4. if do_all is set, find next match
4475 * 5. break if there isn't another match in this line
4476 */
4477 for (;;)
4478 {
4479 /* Save the line number of the last change for the final
4480 * cursor position (just like Vi). */
4481 curwin->w_cursor.lnum = lnum;
4482 do_again = FALSE;
4483
4484 /*
4485 * 1. Match empty string does not count, except for first
4486 * match. This reproduces the strange vi behaviour.
4487 * This also catches endless loops.
4488 */
4489 if (matchcol == prev_matchcol
4490 && regmatch.endpos[0].lnum == 0
4491 && matchcol == regmatch.endpos[0].col)
4492 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004493 if (sub_firstline[matchcol] == NUL)
4494 /* We already were at the end of the line. Don't look
4495 * for a match in this line again. */
4496 skip_match = TRUE;
4497 else
4498 ++matchcol; /* search for a match at next column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 goto skip;
4500 }
4501
4502 /* Normally we continue searching for a match just after the
4503 * previous match. */
4504 matchcol = regmatch.endpos[0].col;
4505 prev_matchcol = matchcol;
4506
4507 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004508 * 2. If do_count is set only increase the counter.
4509 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004511 if (do_count)
4512 {
4513 /* For a multi-line match, put matchcol at the NUL at
4514 * the end of the line and set nmatch to one, so that
4515 * we continue looking for a match on the next line.
4516 * Avoids that ":s/\nB\@=//gc" get stuck. */
4517 if (nmatch > 1)
4518 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004519 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004520 nmatch = 1;
4521 }
4522 sub_nsubs++;
4523 did_sub = TRUE;
4524 goto skip;
4525 }
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (do_ask)
4528 {
4529 /* change State to CONFIRM, so that the mouse works
4530 * properly */
4531 save_State = State;
4532 State = CONFIRM;
4533#ifdef FEAT_MOUSE
4534 setmouse(); /* disable mouse in xterm */
4535#endif
4536 curwin->w_cursor.col = regmatch.startpos[0].col;
4537
4538 /* When 'cpoptions' contains "u" don't sync undo when
4539 * asking for confirmation. */
4540 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4541 ++no_u_sync;
4542
4543 /*
4544 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4545 */
4546 while (do_ask)
4547 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004548 if (exmode_active)
4549 {
4550 char_u *resp;
4551 colnr_T sc, ec;
4552
4553 print_line_no_prefix(lnum, FALSE, FALSE);
4554
4555 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4556 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4557 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
4558 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004559 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004560 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004561 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004562 msg_putchar('^');
4563
4564 resp = getexmodeline('?', NULL, 0);
4565 if (resp != NULL)
4566 {
4567 i = *resp;
4568 vim_free(resp);
4569 }
4570 }
4571 else
4572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004574 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004576 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004578 /* Invert the matched string.
4579 * Remove the inversion afterwards. */
4580 temp = RedrawingDisabled;
4581 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004583 search_match_lines = regmatch.endpos[0].lnum;
4584 search_match_endcol = regmatch.endpos[0].col;
4585 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004587 update_topline();
4588 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00004589 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004590 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00004591 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004594 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004596 if (msg_row == Rows - 1)
4597 msg_didout = FALSE; /* avoid a scroll-up */
4598 msg_starthere();
4599 i = msg_scroll;
4600 msg_scroll = 0; /* truncate msg when
4601 needed */
4602 msg_no_more = TRUE;
4603 /* write message same highlighting as for
4604 * wait_return */
4605 smsg_attr(hl_attr(HLF_R),
4606 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4607 msg_no_more = FALSE;
4608 msg_scroll = i;
4609 showruler(TRUE);
4610 windgoto(msg_row, msg_col);
4611 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004614 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004616 ++no_mapping; /* don't map this key */
4617 ++allow_keys; /* allow special keys */
4618 i = safe_vgetc();
4619 --allow_keys;
4620 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004622 /* clear the question */
4623 msg_didout = FALSE; /* don't scroll up */
4624 msg_col = 0;
4625 gotocmdline(TRUE);
4626 }
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 need_wait_return = FALSE; /* no hit-return prompt */
4629 if (i == 'q' || i == ESC || i == Ctrl_C
4630#ifdef UNIX
4631 || i == intr_char
4632#endif
4633 )
4634 {
4635 got_quit = TRUE;
4636 break;
4637 }
4638 if (i == 'n')
4639 break;
4640 if (i == 'y')
4641 break;
4642 if (i == 'l')
4643 {
4644 /* last: replace and then stop */
4645 do_all = FALSE;
4646 line2 = lnum;
4647 break;
4648 }
4649 if (i == 'a')
4650 {
4651 do_ask = FALSE;
4652 break;
4653 }
4654#ifdef FEAT_INS_EXPAND
4655 if (i == Ctrl_E)
4656 scrollup_clamp();
4657 else if (i == Ctrl_Y)
4658 scrolldown_clamp();
4659#endif
4660 }
4661 State = save_State;
4662#ifdef FEAT_MOUSE
4663 setmouse();
4664#endif
4665 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4666 --no_u_sync;
4667
4668 if (i == 'n')
4669 {
4670 /* For a multi-line match, put matchcol at the NUL at
4671 * the end of the line and set nmatch to one, so that
4672 * we continue looking for a match on the next line.
4673 * Avoids that ":s/\nB\@=//gc" get stuck. */
4674 if (nmatch > 1)
4675 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004676 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 nmatch = 1;
4678 }
4679 goto skip;
4680 }
4681 if (got_quit)
4682 break;
4683 }
4684
4685 /* Move the cursor to the start of the match, so that we can
4686 * use "\=col("."). */
4687 curwin->w_cursor.col = regmatch.startpos[0].col;
4688
4689 /*
4690 * 3. substitute the string.
4691 */
4692 /* get length of substitution part */
4693 sublen = vim_regsub_multi(&regmatch, sub_firstlnum,
4694 sub, sub_firstline, FALSE, p_magic, TRUE);
4695
Bram Moolenaar4463f292005-09-25 22:20:24 +00004696 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004697 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00004698 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
4699 {
4700 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
4701 skip_match = TRUE;
4702 }
4703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 /* Need room for:
4705 * - result so far in new_start (not for first sub in line)
4706 * - original text up to match
4707 * - length of substituted part
4708 * - original text after match
4709 */
4710 if (nmatch == 1)
4711 p1 = sub_firstline;
4712 else
4713 {
4714 p1 = ml_get(sub_firstlnum + nmatch - 1);
4715 nmatch_tl += nmatch - 1;
4716 }
4717 i = regmatch.startpos[0].col - copycol;
4718 needed_len = i + ((unsigned)STRLEN(p1) - regmatch.endpos[0].col)
4719 + sublen + 1;
4720 if (new_start == NULL)
4721 {
4722 /*
4723 * Get some space for a temporary buffer to do the
4724 * substitution into (and some extra space to avoid
4725 * too many calls to alloc()/free()).
4726 */
4727 new_start_len = needed_len + 50;
4728 if ((new_start = alloc_check(new_start_len)) == NULL)
4729 goto outofmem;
4730 *new_start = NUL;
4731 new_end = new_start;
4732 }
4733 else
4734 {
4735 /*
4736 * Check if the temporary buffer is long enough to do the
4737 * substitution into. If not, make it larger (with a bit
4738 * extra to avoid too many calls to alloc()/free()).
4739 */
4740 len = (unsigned)STRLEN(new_start);
4741 needed_len += len;
4742 if (needed_len > new_start_len)
4743 {
4744 new_start_len = needed_len + 50;
4745 if ((p1 = alloc_check(new_start_len)) == NULL)
4746 {
4747 vim_free(new_start);
4748 goto outofmem;
4749 }
4750 mch_memmove(p1, new_start, (size_t)(len + 1));
4751 vim_free(new_start);
4752 new_start = p1;
4753 }
4754 new_end = new_start + len;
4755 }
4756
4757 /*
4758 * copy the text up to the part that matched
4759 */
4760 mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
4761 new_end += i;
4762
4763 (void)vim_regsub_multi(&regmatch, sub_firstlnum,
4764 sub, new_end, TRUE, p_magic, TRUE);
4765 sub_nsubs++;
4766 did_sub = TRUE;
4767
4768 /* Move the cursor to the start of the line, to avoid that it
4769 * is beyond the end of the line after the substitution. */
4770 curwin->w_cursor.col = 0;
4771
4772 /* For a multi-line match, make a copy of the last matched
4773 * line and continue in that one. */
4774 if (nmatch > 1)
4775 {
4776 sub_firstlnum += nmatch - 1;
4777 vim_free(sub_firstline);
4778 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4779 /* When going beyond the last line, stop substituting. */
4780 if (sub_firstlnum <= line2)
4781 do_again = TRUE;
4782 else
4783 do_all = FALSE;
4784 }
4785
4786 /* Remember next character to be copied. */
4787 copycol = regmatch.endpos[0].col;
4788
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004789 if (skip_match)
4790 {
4791 /* Already hit end of the buffer, sub_firstlnum is one
4792 * less than what it ought to be. */
4793 vim_free(sub_firstline);
4794 sub_firstline = vim_strsave((char_u *)"");
4795 copycol = 0;
4796 }
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 /*
4799 * Now the trick is to replace CTRL-M chars with a real line
4800 * break. This would make it impossible to insert a CTRL-M in
4801 * the text. The line break can be avoided by preceding the
4802 * CTRL-M with a backslash. To be able to insert a backslash,
4803 * they must be doubled in the string and are halved here.
4804 * That is Vi compatible.
4805 */
4806 for (p1 = new_end; *p1; ++p1)
4807 {
4808 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
4809 mch_memmove(p1, p1 + 1, STRLEN(p1));
4810 else if (*p1 == CAR)
4811 {
4812 if (u_inssub(lnum) == OK) /* prepare for undo */
4813 {
4814 *p1 = NUL; /* truncate up to the CR */
4815 ml_append(lnum - 1, new_start,
4816 (colnr_T)(p1 - new_start + 1), FALSE);
4817 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
4818 if (do_ask)
4819 appended_lines(lnum - 1, 1L);
4820 else
4821 {
4822 if (first_line == 0)
4823 first_line = lnum;
4824 last_line = lnum + 1;
4825 }
4826 /* All line numbers increase. */
4827 ++sub_firstlnum;
4828 ++lnum;
4829 ++line2;
4830 /* move the cursor to the new line, like Vi */
4831 ++curwin->w_cursor.lnum;
4832 STRCPY(new_start, p1 + 1); /* copy the rest */
4833 p1 = new_start - 1;
4834 }
4835 }
4836#ifdef FEAT_MBYTE
4837 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004838 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#endif
4840 }
4841
4842 /*
4843 * 4. If do_all is set, find next match.
4844 * Prevent endless loop with patterns that match empty
4845 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
4846 * But ":s/\n/#/" is OK.
4847 */
4848skip:
4849 /* We already know that we did the last subst when we are at
4850 * the end of the line, except that a pattern like
4851 * "bar\|\nfoo" may match at the NUL. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004852 lastone = (skip_match
4853 || got_int
4854 || got_quit
4855 || !(do_all || do_again)
4856 || (sub_firstline[matchcol] == NUL && nmatch <= 1
4857 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 nmatch = -1;
4859
4860 /*
4861 * Replace the line in the buffer when needed. This is
4862 * skipped when there are more matches.
4863 * The check for nmatch_tl is needed for when multi-line
4864 * matching must replace the lines before trying to do another
4865 * match, otherwise "\@<=" won't work.
4866 * When asking the user we like to show the already replaced
4867 * text, but don't do it when "\<@=" or "\<@!" is used, it
4868 * changes what matches.
4869 */
4870 if (lastone
4871 || (do_ask && !re_lookbehind(regmatch.regprog))
4872 || nmatch_tl > 0
4873 || (nmatch = vim_regexec_multi(&regmatch, curwin,
4874 curbuf, sub_firstlnum, matchcol)) == 0)
4875 {
4876 if (new_start != NULL)
4877 {
4878 /*
4879 * Copy the rest of the line, that didn't match.
4880 * "matchcol" has to be adjusted, we use the end of
4881 * the line as reference, because the substitute may
4882 * have changed the number of characters. Same for
4883 * "prev_matchcol".
4884 */
4885 STRCAT(new_start, sub_firstline + copycol);
4886 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4887 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4888 - prev_matchcol;
4889
4890 if (u_savesub(lnum) != OK)
4891 break;
4892 ml_replace(lnum, new_start, TRUE);
4893
4894 if (nmatch_tl > 0)
4895 {
4896 /*
4897 * Matched lines have now been substituted and are
4898 * useless, delete them. The part after the match
4899 * has been appended to new_start, we don't need
4900 * it in the buffer.
4901 */
4902 ++lnum;
4903 if (u_savedel(lnum, nmatch_tl) != OK)
4904 break;
4905 for (i = 0; i < nmatch_tl; ++i)
4906 ml_delete(lnum, (int)FALSE);
4907 mark_adjust(lnum, lnum + nmatch_tl - 1,
4908 (long)MAXLNUM, -nmatch_tl);
4909 if (do_ask)
4910 deleted_lines(lnum, nmatch_tl);
4911 --lnum;
4912 line2 -= nmatch_tl; /* nr of lines decreases */
4913 nmatch_tl = 0;
4914 }
4915
4916 /* When asking, undo is saved each time, must also set
4917 * changed flag each time. */
4918 if (do_ask)
4919 changed_bytes(lnum, 0);
4920 else
4921 {
4922 if (first_line == 0)
4923 first_line = lnum;
4924 last_line = lnum + 1;
4925 }
4926
4927 sub_firstlnum = lnum;
4928 vim_free(sub_firstline); /* free the temp buffer */
4929 sub_firstline = new_start;
4930 new_start = NULL;
4931 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4932 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4933 - prev_matchcol;
4934 copycol = 0;
4935 }
4936 if (nmatch == -1 && !lastone)
4937 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
4938 sub_firstlnum, matchcol);
4939
4940 /*
4941 * 5. break if there isn't another match in this line
4942 */
4943 if (nmatch <= 0)
4944 break;
4945 }
4946
4947 line_breakcheck();
4948 }
4949
4950 if (did_sub)
4951 ++sub_nlines;
4952 vim_free(sub_firstline); /* free the copy of the original line */
4953 sub_firstline = NULL;
4954 }
4955
4956 line_breakcheck();
4957 }
4958
4959 if (first_line != 0)
4960 {
4961 /* Need to subtract the number of added lines from "last_line" to get
4962 * the line number before the change (same as adding the number of
4963 * deleted lines). */
4964 i = curbuf->b_ml.ml_line_count - old_line_count;
4965 changed_lines(first_line, 0, last_line - i, i);
4966 }
4967
4968outofmem:
4969 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004970
4971 /* ":s/pat//n" doesn't move the cursor */
4972 if (do_count)
4973 curwin->w_cursor = old_cursor;
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (sub_nsubs)
4976 {
4977 /* Set the '[ and '] marks. */
4978 curbuf->b_op_start.lnum = eap->line1;
4979 curbuf->b_op_end.lnum = line2;
4980 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4981
4982 if (!global_busy)
4983 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004984 if (endcolumn)
4985 coladvance((colnr_T)MAXCOL);
4986 else
4987 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004988 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 MSG("");
4990 }
4991 else
4992 global_need_beginline = TRUE;
4993 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004994 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 else if (!global_busy)
4997 {
4998 if (got_int) /* interrupted */
4999 EMSG(_(e_interr));
5000 else if (got_match) /* did find something but nothing substituted */
5001 MSG("");
5002 else if (do_error) /* nothing found */
5003 EMSG2(_(e_patnotf2), get_search_pat());
5004 }
5005
5006 vim_free(regmatch.regprog);
5007}
5008
5009/*
5010 * Give message for number of substitutions.
5011 * Can also be used after a ":global" command.
5012 * Return TRUE if a message was given.
5013 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005014 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00005015do_sub_msg(count_only)
5016 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
Bram Moolenaar555b2802005-05-19 21:08:39 +00005018 int len = 0;
5019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 /*
5021 * Only report substitutions when:
5022 * - more than 'report' substitutions
5023 * - command was typed by user, or number of changed lines > 'report'
5024 * - giving messages is not disabled by 'lazyredraw'
5025 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005026 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5027 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 && messaging())
5029 {
5030 if (got_int)
Bram Moolenaar555b2802005-05-19 21:08:39 +00005031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005033 len = (int)STRLEN(msg_buf);
Bram Moolenaar555b2802005-05-19 21:08:39 +00005034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (sub_nsubs == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00005036 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
5037 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005039 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
Bram Moolenaar05159a02005-02-26 23:04:13 +00005040 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 sub_nsubs);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005042 len = (int)STRLEN(msg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (sub_nlines == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00005044 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
5045 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005047 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
5048 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005051 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 return TRUE;
5053 }
5054 if (got_int)
5055 {
5056 EMSG(_(e_interr));
5057 return TRUE;
5058 }
5059 return FALSE;
5060}
5061
5062/*
5063 * Execute a global command of the form:
5064 *
5065 * g/pattern/X : execute X on all lines where pattern matches
5066 * v/pattern/X : execute X on all lines where pattern does not match
5067 *
5068 * where 'X' is an EX command
5069 *
5070 * The command character (as well as the trailing slash) is optional, and
5071 * is assumed to be 'p' if missing.
5072 *
5073 * This is implemented in two passes: first we scan the file for the pattern and
5074 * set a mark for each line that (not) matches. secondly we execute the command
5075 * for each line that has a mark. This is required because after deleting
5076 * lines we do not know where to search for the next match.
5077 */
5078 void
5079ex_global(eap)
5080 exarg_T *eap;
5081{
5082 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005083 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 int type; /* first char of cmd: 'v' or 'g' */
5085 char_u *cmd; /* command argument */
5086
5087 char_u delim; /* delimiter, normally '/' */
5088 char_u *pat;
5089 regmmatch_T regmatch;
5090 int match;
5091 int which_pat;
5092
5093 if (global_busy)
5094 {
5095 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5096 return;
5097 }
5098
5099 if (eap->forceit) /* ":global!" is like ":vglobal" */
5100 type = 'v';
5101 else
5102 type = *eap->cmd;
5103 cmd = eap->arg;
5104 which_pat = RE_LAST; /* default: use last used regexp */
5105 sub_nsubs = 0;
5106 sub_nlines = 0;
5107
5108 /*
5109 * undocumented vi feature:
5110 * "\/" and "\?": use previous search pattern.
5111 * "\&": use previous substitute pattern.
5112 */
5113 if (*cmd == '\\')
5114 {
5115 ++cmd;
5116 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5117 {
5118 EMSG(_(e_backslash));
5119 return;
5120 }
5121 if (*cmd == '&')
5122 which_pat = RE_SUBST; /* use previous substitute pattern */
5123 else
5124 which_pat = RE_SEARCH; /* use previous search pattern */
5125 ++cmd;
5126 pat = (char_u *)"";
5127 }
5128 else if (*cmd == NUL)
5129 {
5130 EMSG(_("E148: Regular expression missing from global"));
5131 return;
5132 }
5133 else
5134 {
5135 delim = *cmd; /* get the delimiter */
5136 if (delim)
5137 ++cmd; /* skip delimiter if there is one */
5138 pat = cmd; /* remember start of pattern */
5139 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5140 if (cmd[0] == delim) /* end delimiter found */
5141 *cmd++ = NUL; /* replace it with a NUL */
5142 }
5143
5144#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5145 if (p_altkeymap && curwin->w_p_rl)
5146 lrFswap(pat,0);
5147#endif
5148
5149 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5150 {
5151 EMSG(_(e_invcmd));
5152 return;
5153 }
5154
5155 /*
5156 * pass 1: set marks for each (not) matching line
5157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5159 {
5160 /* a match on this line? */
5161 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
5162 if ((type == 'g' && match) || (type == 'v' && !match))
5163 {
5164 ml_setmarked(lnum);
5165 ndone++;
5166 }
5167 line_breakcheck();
5168 }
5169
5170 /*
5171 * pass 2: execute the command for each line that has been marked
5172 */
5173 if (got_int)
5174 MSG(_(e_interr));
5175 else if (ndone == 0)
5176 {
5177 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005178 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005180 smsg((char_u *)_(e_patnotf2), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 else
5183 global_exe(cmd);
5184
5185 ml_clearmarked(); /* clear rest of the marks */
5186 vim_free(regmatch.regprog);
5187}
5188
5189/*
5190 * Execute "cmd" on lines marked with ml_setmarked().
5191 */
5192 void
5193global_exe(cmd)
5194 char_u *cmd;
5195{
5196 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5197 linenr_T lnum; /* line number according to old situation */
5198
5199 /*
5200 * Set current position only once for a global command.
5201 * If global_busy is set, setpcmark() will not do anything.
5202 * If there is an error, global_busy will be incremented.
5203 */
5204 setpcmark();
5205
5206 /* When the command writes a message, don't overwrite the command. */
5207 msg_didout = TRUE;
5208
5209 global_need_beginline = FALSE;
5210 global_busy = 1;
5211 old_lcount = curbuf->b_ml.ml_line_count;
5212 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5213 {
5214 curwin->w_cursor.lnum = lnum;
5215 curwin->w_cursor.col = 0;
5216 if (*cmd == NUL || *cmd == '\n')
5217 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5218 else
5219 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5220 ui_breakcheck();
5221 }
5222
5223 global_busy = 0;
5224 if (global_need_beginline)
5225 beginline(BL_WHITE | BL_FIX);
5226 else
5227 check_cursor(); /* cursor may be beyond the end of the line */
5228
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005229 /* the cursor may not have moved in the text but a change in a previous
5230 * line may move it on the screen */
5231 changed_line_abv_curs();
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 /* If it looks like no message was written, allow overwriting the
5234 * command with the report for number of changes. */
5235 if (msg_col == 0 && msg_scrolled == 0)
5236 msg_didout = FALSE;
5237
5238 /* If subsitutes done, report number of substitues, otherwise report
5239 * number of extra or deleted lines. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005240 if (!do_sub_msg(FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5242}
5243
5244#ifdef FEAT_VIMINFO
5245 int
5246read_viminfo_sub_string(virp, force)
5247 vir_T *virp;
5248 int force;
5249{
5250 if (old_sub != NULL && force)
5251 vim_free(old_sub);
5252 if (force || old_sub == NULL)
5253 old_sub = viminfo_readstring(virp, 1, TRUE);
5254 return viminfo_readline(virp);
5255}
5256
5257 void
5258write_viminfo_sub_string(fp)
5259 FILE *fp;
5260{
5261 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5262 {
5263 fprintf(fp, _("\n# Last Substitute String:\n$"));
5264 viminfo_writestring(fp, old_sub);
5265 }
5266}
5267#endif /* FEAT_VIMINFO */
5268
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005269#if defined(EXITFREE) || defined(PROTO)
5270 void
5271free_old_sub()
5272{
5273 vim_free(old_sub);
5274}
5275#endif
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5278/*
5279 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005280 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005282 int
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005283prepare_tagpreview(undo_sync)
5284 int undo_sync; /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 win_T *wp;
5287
5288# ifdef FEAT_GUI
5289 need_mouse_correct = TRUE;
5290# endif
5291
5292 /*
5293 * If there is already a preview window open, use that one.
5294 */
5295 if (!curwin->w_p_pvw)
5296 {
5297 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5298 if (wp->w_p_pvw)
5299 break;
5300 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005301 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 else
5303 {
5304 /*
5305 * There is no preview window open yet. Create one.
5306 */
5307 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5308 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005309 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 curwin->w_p_pvw = TRUE;
5311 curwin->w_p_wfh = TRUE;
5312# ifdef FEAT_SCROLLBIND
5313 curwin->w_p_scb = FALSE; /* don't take over 'scrollbind' */
5314# endif
5315# ifdef FEAT_DIFF
5316 curwin->w_p_diff = FALSE; /* no 'diff' */
5317# endif
5318# ifdef FEAT_FOLDING
5319 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5320# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005321 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005324 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325}
5326
5327#endif
5328
5329
5330/*
5331 * ":help": open a read-only window on a help file
5332 */
5333 void
5334ex_help(eap)
5335 exarg_T *eap;
5336{
5337 char_u *arg;
5338 char_u *tag;
5339 FILE *helpfd; /* file descriptor of help file */
5340 int n;
5341 int i;
5342#ifdef FEAT_WINDOWS
5343 win_T *wp;
5344#endif
5345 int num_matches;
5346 char_u **matches;
5347 char_u *p;
5348 int empty_fnum = 0;
5349 int alt_fnum = 0;
5350 buf_T *buf;
5351#ifdef FEAT_MULTI_LANG
5352 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005353 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354#endif
5355
5356 if (eap != NULL)
5357 {
5358 /*
5359 * A ":help" command ends at the first LF, or at a '|' that is
5360 * followed by some text. Set nextcmd to the following command.
5361 */
5362 for (arg = eap->arg; *arg; ++arg)
5363 {
5364 if (*arg == '\n' || *arg == '\r'
5365 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5366 {
5367 *arg++ = NUL;
5368 eap->nextcmd = arg;
5369 break;
5370 }
5371 }
5372 arg = eap->arg;
5373
5374 if (eap->forceit && *arg == NUL)
5375 {
5376 EMSG(_("E478: Don't panic!"));
5377 return;
5378 }
5379
5380 if (eap->skip) /* not executing commands */
5381 return;
5382 }
5383 else
5384 arg = (char_u *)"";
5385
5386 /* remove trailing blanks */
5387 p = arg + STRLEN(arg) - 1;
5388 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5389 *p-- = NUL;
5390
5391#ifdef FEAT_MULTI_LANG
5392 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005393 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#endif
5395
5396 /* When no argument given go to the index. */
5397 if (*arg == NUL)
5398 arg = (char_u *)"help.txt";
5399
5400 /*
5401 * Check if there is a match for the argument.
5402 */
5403 n = find_help_tags(arg, &num_matches, &matches,
5404 eap != NULL && eap->forceit);
5405
5406 i = 0;
5407#ifdef FEAT_MULTI_LANG
5408 if (n != FAIL && lang != NULL)
5409 /* Find first item with the requested language. */
5410 for (i = 0; i < num_matches; ++i)
5411 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005412 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (len > 3 && matches[i][len - 3] == '@'
5414 && STRICMP(matches[i] + len - 2, lang) == 0)
5415 break;
5416 }
5417#endif
5418 if (i >= num_matches || n == FAIL)
5419 {
5420#ifdef FEAT_MULTI_LANG
5421 if (lang != NULL)
5422 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5423 else
5424#endif
5425 EMSG2(_("E149: Sorry, no help for %s"), arg);
5426 if (n != FAIL)
5427 FreeWild(num_matches, matches);
5428 return;
5429 }
5430
5431 /* The first match (in the requested language) is the best match. */
5432 tag = vim_strsave(matches[i]);
5433 FreeWild(num_matches, matches);
5434
5435#ifdef FEAT_GUI
5436 need_mouse_correct = TRUE;
5437#endif
5438
5439 /*
5440 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005441 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005443 if (!curwin->w_buffer->b_help
5444#ifdef FEAT_WINDOWS
5445 || cmdmod.tab != 0
5446#endif
5447 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005450 if (cmdmod.tab != 0)
5451 wp = NULL;
5452 else
5453 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5454 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5457 win_enter(wp, TRUE);
5458 else
5459#endif
5460 {
5461 /*
5462 * There is no help window yet.
5463 * Try to open the file specified by the "helpfile" option.
5464 */
5465 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5466 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005467 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 goto erret;
5469 }
5470 fclose(helpfd);
5471
5472#ifdef FEAT_WINDOWS
5473 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005474 * specified, the current window is vertically split and
5475 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 n = WSP_HELP;
5477# ifdef FEAT_VERTSPLIT
5478 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005479 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 n |= WSP_TOP;
5481# endif
5482 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005483 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#else
5485 /* use current window */
5486 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
5490#ifdef FEAT_WINDOWS
5491 if (curwin->w_height < p_hh)
5492 win_setheight((int)p_hh);
5493#endif
5494
5495 /*
5496 * Open help file (do_ecmd() will set b_help flag, readfile() will
5497 * set b_p_ro flag).
5498 * Set the alternate file to the previously edited file.
5499 */
5500 alt_fnum = curbuf->b_fnum;
5501 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
5502 ECMD_HIDE + ECMD_SET_HELP);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005503 if (!cmdmod.keepalt)
5504 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 empty_fnum = curbuf->b_fnum;
5506 }
5507 }
5508
5509 if (!p_im)
5510 restart_edit = 0; /* don't want insert mode in help file */
5511
5512 if (tag != NULL)
5513 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5514
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005515 /* Delete the empty buffer if we're not using it. Careful: autocommands
5516 * may have jumped to another window, check that the buffer is not in a
5517 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5519 {
5520 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005521 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 wipe_buffer(buf, TRUE);
5523 }
5524
5525 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005526 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 curwin->w_alt_fnum = alt_fnum;
5528
5529erret:
5530 vim_free(tag);
5531}
5532
5533
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005534#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5535/*
5536 * In an argument search for a language specifiers in the form "@xx".
5537 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5538 * Returns NULL if not found.
5539 */
5540 char_u *
5541check_help_lang(arg)
5542 char_u *arg;
5543{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005544 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005545
5546 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
5547 && ASCII_ISALPHA(arg[len - 1]))
5548 {
5549 arg[len - 3] = NUL; /* remove the '@' */
5550 return arg + len - 2;
5551 }
5552 return NULL;
5553}
5554#endif
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556/*
5557 * Return a heuristic indicating how well the given string matches. The
5558 * smaller the number, the better the match. This is the order of priorities,
5559 * from best match to worst match:
5560 * - Match with least alpha-numeric characters is better.
5561 * - Match with least total characters is better.
5562 * - Match towards the start is better.
5563 * - Match starting with "+" is worse (feature instead of command)
5564 * Assumption is made that the matched_string passed has already been found to
5565 * match some string for which help is requested. webb.
5566 */
5567 int
5568help_heuristic(matched_string, offset, wrong_case)
5569 char_u *matched_string;
5570 int offset; /* offset for match */
5571 int wrong_case; /* no matching case */
5572{
5573 int num_letters;
5574 char_u *p;
5575
5576 num_letters = 0;
5577 for (p = matched_string; *p; p++)
5578 if (ASCII_ISALNUM(*p))
5579 num_letters++;
5580
5581 /*
5582 * Multiply the number of letters by 100 to give it a much bigger
5583 * weighting than the number of characters.
5584 * If there only is a match while ignoring case, add 5000.
5585 * If the match starts in the middle of a word, add 10000 to put it
5586 * somewhere in the last half.
5587 * If the match is more than 2 chars from the start, multiply by 200 to
5588 * put it after matches at the start.
5589 */
5590 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
5591 && ASCII_ISALNUM(matched_string[offset - 1]))
5592 offset += 10000;
5593 else if (offset > 2)
5594 offset *= 200;
5595 if (wrong_case)
5596 offset += 5000;
5597 /* Features are less interesting than the subjects themselves, but "+"
5598 * alone is not a feature. */
5599 if (matched_string[0] == '+' && matched_string[1] != NUL)
5600 offset += 100;
5601 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
5602}
5603
5604/*
5605 * Compare functions for qsort() below, that checks the help heuristics number
5606 * that has been put after the tagname by find_tags().
5607 */
5608 static int
5609#ifdef __BORLANDC__
5610_RTLENTRYF
5611#endif
5612help_compare(s1, s2)
5613 const void *s1;
5614 const void *s2;
5615{
5616 char *p1;
5617 char *p2;
5618
5619 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
5620 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
5621 return strcmp(p1, p2);
5622}
5623
5624/*
5625 * Find all help tags matching "arg", sort them and return in matches[], with
5626 * the number of matches in num_matches.
5627 * The matches will be sorted with a "best" match algorithm.
5628 * When "keep_lang" is TRUE try keeping the language of the current buffer.
5629 */
5630 int
5631find_help_tags(arg, num_matches, matches, keep_lang)
5632 char_u *arg;
5633 int *num_matches;
5634 char_u ***matches;
5635 int keep_lang;
5636{
5637 char_u *s, *d;
5638 int i;
5639 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005640 "/*", "/\\*", "\"*", "**",
5641 "/\\(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005642 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005643 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 "[count]", "[quotex]", "[range]",
5645 "[pattern]", "\\|", "\\%$"};
5646 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005647 "/star", "/\\\\star", "quotestar", "starstar",
5648 "/\\\\(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005649 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005650 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 "\\[count]", "\\[quotex]", "\\[range]",
5652 "\\[pattern]", "\\\\bar", "/\\\\%\\$"};
5653 int flags;
5654
5655 d = IObuff; /* assume IObuff is long enough! */
5656
5657 /*
5658 * Recognize a few exceptions to the rule. Some strings that contain '*'
5659 * with "star". Otherwise '*' is recognized as a wildcard.
5660 */
5661 for (i = sizeof(mtable) / sizeof(char *); --i >= 0; )
5662 if (STRCMP(arg, mtable[i]) == 0)
5663 {
5664 STRCPY(d, rtable[i]);
5665 break;
5666 }
5667
5668 if (i < 0) /* no match in table */
5669 {
5670 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
5671 * Also replace "\%^" and "\%(", they match every tag too.
5672 * Also "\zs", "\z1", etc.
5673 * Also "\@<", "\@=", "\@<=", etc.
5674 * And also "\_$" and "\_^". */
5675 if (arg[0] == '\\'
5676 && ((arg[1] != NUL && arg[2] == NUL)
5677 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
5678 && arg[2] != NUL)))
5679 {
5680 STRCPY(d, "/\\\\");
5681 STRCPY(d + 3, arg + 1);
5682 /* Check for "/\\_$", should be "/\\_\$" */
5683 if (d[3] == '_' && d[4] == '$')
5684 STRCPY(d + 4, "\\$");
5685 }
5686 else
5687 {
5688 /* replace "[:...:]" with "\[:...:]"; "[+...]" with "\[++...]" */
5689 if (arg[0] == '[' && (arg[1] == ':'
5690 || (arg[1] == '+' && arg[2] == '+')))
5691 *d++ = '\\';
5692
5693 for (s = arg; *s; ++s)
5694 {
5695 /*
5696 * Replace "|" with "bar" and '"' with "quote" to match the name of
5697 * the tags for these commands.
5698 * Replace "*" with ".*" and "?" with "." to match command line
5699 * completion.
5700 * Insert a backslash before '~', '$' and '.' to avoid their
5701 * special meaning.
5702 */
5703 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
5704 break;
5705 switch (*s)
5706 {
5707 case '|': STRCPY(d, "bar");
5708 d += 3;
5709 continue;
5710 case '"': STRCPY(d, "quote");
5711 d += 5;
5712 continue;
5713 case '*': *d++ = '.';
5714 break;
5715 case '?': *d++ = '.';
5716 continue;
5717 case '$':
5718 case '.':
5719 case '~': *d++ = '\\';
5720 break;
5721 }
5722
5723 /*
5724 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
5725 * ":help i_^_CTRL-D" work.
5726 * Insert '-' before and after "CTRL-X" when applicable.
5727 */
5728 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
5729 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
5730 {
5731 if (d > IObuff && d[-1] != '_')
5732 *d++ = '_'; /* prepend a '_' */
5733 STRCPY(d, "CTRL-");
5734 d += 5;
5735 if (*s < ' ')
5736 {
5737#ifdef EBCDIC
5738 *d++ = CtrlChar(*s);
5739#else
5740 *d++ = *s + '@';
5741#endif
5742 if (d[-1] == '\\')
5743 *d++ = '\\'; /* double a backslash */
5744 }
5745 else
5746 *d++ = *++s;
5747 if (s[1] != NUL && s[1] != '_')
5748 *d++ = '_'; /* append a '_' */
5749 continue;
5750 }
5751 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
5752 *d++ = '\\';
5753
5754 /*
5755 * Insert a backslash before a backslash after a slash, for search
5756 * pattern tags: "/\|" --> "/\\|".
5757 */
5758 else if (s[0] == '\\' && s[1] != '\\'
5759 && *arg == '/' && s == arg + 1)
5760 *d++ = '\\';
5761
5762 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
5763 * "CTRL-\_CTRL-N" */
5764 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
5765 {
5766 STRCPY(d, "CTRL-\\\\");
5767 d += 7;
5768 s += 6;
5769 }
5770
5771 *d++ = *s;
5772
5773 /*
5774 * If tag starts with ', toss everything after a second '. Fixes
5775 * CTRL-] on 'option'. (would include the trailing '.').
5776 */
5777 if (*s == '\'' && s > arg && *arg == '\'')
5778 break;
5779 }
5780 *d = NUL;
5781 }
5782 }
5783
5784 *matches = (char_u **)"";
5785 *num_matches = 0;
5786 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
5787 if (keep_lang)
5788 flags |= TAG_KEEP_LANG;
5789 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
5790 && *num_matches > 0)
5791 /* Sort the matches found on the heuristic number that is after the
5792 * tag name. */
5793 qsort((void *)*matches, (size_t)*num_matches,
5794 sizeof(char_u *), help_compare);
5795 return OK;
5796}
5797
5798/*
5799 * After reading a help file: May cleanup a help buffer when syntax
5800 * highlighting is not used.
5801 */
5802 void
5803fix_help_buffer()
5804{
5805 linenr_T lnum;
5806 char_u *line;
5807 int in_example = FALSE;
5808 int len;
5809 char_u *p;
5810 char_u *rt;
5811 int mustfree;
5812
5813 /* set filetype to "help". */
5814 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
5815
5816#ifdef FEAT_SYN_HL
5817 if (!syntax_present(curbuf))
5818#endif
5819 {
5820 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5821 {
5822 line = ml_get_buf(curbuf, lnum, FALSE);
5823 len = (int)STRLEN(line);
5824 if (in_example && len > 0 && !vim_iswhite(line[0]))
5825 {
5826 /* End of example: non-white or '<' in first column. */
5827 if (line[0] == '<')
5828 {
5829 /* blank-out a '<' in the first column */
5830 line = ml_get_buf(curbuf, lnum, TRUE);
5831 line[0] = ' ';
5832 }
5833 in_example = FALSE;
5834 }
5835 if (!in_example && len > 0)
5836 {
5837 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
5838 {
5839 /* blank-out a '>' in the last column (start of example) */
5840 line = ml_get_buf(curbuf, lnum, TRUE);
5841 line[len - 1] = ' ';
5842 in_example = TRUE;
5843 }
5844 else if (line[len - 1] == '~')
5845 {
5846 /* blank-out a '~' at the end of line (header marker) */
5847 line = ml_get_buf(curbuf, lnum, TRUE);
5848 line[len - 1] = ' ';
5849 }
5850 }
5851 }
5852 }
5853
5854 /*
5855 * In the "help.txt" file, add the locally added help files.
5856 * This uses the very first line in the help file.
5857 */
5858 if (fnamecmp(gettail(curbuf->b_fname), "help.txt") == 0)
5859 {
5860 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
5861 {
5862 line = ml_get_buf(curbuf, lnum, FALSE);
5863 if (strstr((char *)line, "*local-additions*") != NULL)
5864 {
5865 /* Go through all directories in 'runtimepath', skipping
5866 * $VIMRUNTIME. */
5867 p = p_rtp;
5868 while (*p != NUL)
5869 {
5870 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5871 mustfree = FALSE;
5872 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
5873 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
5874 {
5875 int fcount;
5876 char_u **fnames;
5877 FILE *fd;
5878 char_u *s;
5879 int fi;
5880#ifdef FEAT_MBYTE
5881 vimconv_T vc;
5882 char_u *cp;
5883#endif
5884
5885 /* Find all "doc/ *.txt" files in this directory. */
5886 add_pathsep(NameBuff);
5887 STRCAT(NameBuff, "doc/*.txt");
5888 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5889 &fnames, EW_FILE|EW_SILENT) == OK
5890 && fcount > 0)
5891 {
5892 for (fi = 0; fi < fcount; ++fi)
5893 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005894 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (fd != NULL)
5896 {
5897 vim_fgets(IObuff, IOSIZE, fd);
5898 if (IObuff[0] == '*'
5899 && (s = vim_strchr(IObuff + 1, '*'))
5900 != NULL)
5901 {
5902#ifdef FEAT_MBYTE
5903 int this_utf = MAYBE;
5904#endif
5905 /* Change tag definition to a
5906 * reference and remove <CR>/<NL>. */
5907 IObuff[0] = '|';
5908 *s = '|';
5909 while (*s != NUL)
5910 {
5911 if (*s == '\r' || *s == '\n')
5912 *s = NUL;
5913#ifdef FEAT_MBYTE
5914 /* The text is utf-8 when a byte
5915 * above 127 is found and no
5916 * illegal byte sequence is found.
5917 */
5918 if (*s >= 0x80 && this_utf != FALSE)
5919 {
5920 int l;
5921
5922 this_utf = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005923 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if (l == 1)
5925 this_utf = FALSE;
5926 s += l - 1;
5927 }
5928#endif
5929 ++s;
5930 }
5931#ifdef FEAT_MBYTE
5932 /* The help file is latin1 or utf-8;
5933 * conversion to the current
5934 * 'encoding' may be required. */
5935 vc.vc_type = CONV_NONE;
5936 convert_setup(&vc, (char_u *)(
5937 this_utf == TRUE ? "utf-8"
5938 : "latin1"), p_enc);
5939 if (vc.vc_type == CONV_NONE)
5940 /* No conversion needed. */
5941 cp = IObuff;
5942 else
5943 {
5944 /* Do the conversion. If it fails
5945 * use the unconverted text. */
5946 cp = string_convert(&vc, IObuff,
5947 NULL);
5948 if (cp == NULL)
5949 cp = IObuff;
5950 }
5951 convert_setup(&vc, NULL, NULL);
5952
5953 ml_append(lnum, cp, (colnr_T)0, FALSE);
5954 if (cp != IObuff)
5955 vim_free(cp);
5956#else
5957 ml_append(lnum, IObuff, (colnr_T)0,
5958 FALSE);
5959#endif
5960 ++lnum;
5961 }
5962 fclose(fd);
5963 }
5964 }
5965 FreeWild(fcount, fnames);
5966 }
5967 }
5968 if (mustfree)
5969 vim_free(rt);
5970 }
5971 break;
5972 }
5973 }
5974 }
5975}
5976
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005977/*
5978 * ":exusage"
5979 */
5980/*ARGSUSED*/
5981 void
5982ex_exusage(eap)
5983 exarg_T *eap;
5984{
5985 do_cmdline_cmd((char_u *)"help ex-cmd-index");
5986}
5987
5988/*
5989 * ":viusage"
5990 */
5991/*ARGSUSED*/
5992 void
5993ex_viusage(eap)
5994 exarg_T *eap;
5995{
5996 do_cmdline_cmd((char_u *)"help normal-index");
5997}
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999#if defined(FEAT_EX_EXTRA) || defined(PROTO)
6000static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang));
6001
6002/*
6003 * ":helptags"
6004 */
6005 void
6006ex_helptags(eap)
6007 exarg_T *eap;
6008{
6009 garray_T ga;
6010 int i, j;
6011 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006012#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 char_u ext[5];
6016 char_u fname[8];
6017 int filecount;
6018 char_u **files;
6019
6020 if (!mch_isdir(eap->arg))
6021 {
6022 EMSG2(_("E150: Not a directory: %s"), eap->arg);
6023 return;
6024 }
6025
6026#ifdef FEAT_MULTI_LANG
6027 /* Get a list of all files in the directory. */
6028 STRCPY(NameBuff, eap->arg);
6029 add_pathsep(NameBuff);
6030 STRCAT(NameBuff, "*");
6031 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6032 EW_FILE|EW_SILENT) == FAIL
6033 || filecount == 0)
6034 {
6035 EMSG2("E151: No match: %s", NameBuff);
6036 return;
6037 }
6038
6039 /* Go over all files in the directory to find out what languages are
6040 * present. */
6041 ga_init2(&ga, 1, 10);
6042 for (i = 0; i < filecount; ++i)
6043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006044 len = (int)STRLEN(files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (len > 4)
6046 {
6047 if (STRICMP(files[i] + len - 4, ".txt") == 0)
6048 {
6049 /* ".txt" -> language "en" */
6050 lang[0] = 'e';
6051 lang[1] = 'n';
6052 }
6053 else if (files[i][len - 4] == '.'
6054 && ASCII_ISALPHA(files[i][len - 3])
6055 && ASCII_ISALPHA(files[i][len - 2])
6056 && TOLOWER_ASC(files[i][len - 1]) == 'x')
6057 {
6058 /* ".abx" -> language "ab" */
6059 lang[0] = TOLOWER_ASC(files[i][len - 3]);
6060 lang[1] = TOLOWER_ASC(files[i][len - 2]);
6061 }
6062 else
6063 continue;
6064
6065 /* Did we find this language already? */
6066 for (j = 0; j < ga.ga_len; j += 2)
6067 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
6068 break;
6069 if (j == ga.ga_len)
6070 {
6071 /* New language, add it. */
6072 if (ga_grow(&ga, 2) == FAIL)
6073 break;
6074 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
6075 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 }
6078 }
6079
6080 /*
6081 * Loop over the found languages to generate a tags file for each one.
6082 */
6083 for (j = 0; j < ga.ga_len; j += 2)
6084 {
6085 STRCPY(fname, "tags-xx");
6086 fname[5] = ((char_u *)ga.ga_data)[j];
6087 fname[6] = ((char_u *)ga.ga_data)[j + 1];
6088 if (fname[5] == 'e' && fname[6] == 'n')
6089 {
6090 /* English is an exception: use ".txt" and "tags". */
6091 fname[4] = NUL;
6092 STRCPY(ext, ".txt");
6093 }
6094 else
6095 {
6096 /* Language "ab" uses ".abx" and "tags-ab". */
6097 STRCPY(ext, ".xxx");
6098 ext[1] = fname[5];
6099 ext[2] = fname[6];
6100 }
6101 helptags_one(eap->arg, ext, fname);
6102 }
6103
6104 ga_clear(&ga);
6105 FreeWild(filecount, files);
6106
6107#else
6108 /* No language support, just use "*.txt" and "tags". */
6109 helptags_one(eap->arg, (char_u *)".txt", (char_u *)"tags");
6110#endif
6111}
6112
6113 static void
6114helptags_one(dir, ext, tagfname)
6115 char_u *dir; /* doc directory */
6116 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
6117 char_u *tagfname; /* "tags" for English, "tags-it" for Italian. */
6118{
6119 FILE *fd_tags;
6120 FILE *fd;
6121 garray_T ga;
6122 int filecount;
6123 char_u **files;
6124 char_u *p1, *p2;
6125 int fi;
6126 char_u *s;
6127 int i;
6128 char_u *fname;
6129# ifdef FEAT_MBYTE
6130 int utf8 = MAYBE;
6131 int this_utf8;
6132 int firstline;
6133 int mix = FALSE; /* detected mixed encodings */
6134# endif
6135
6136 /*
6137 * Find all *.txt files.
6138 */
6139 STRCPY(NameBuff, dir);
6140 add_pathsep(NameBuff);
6141 STRCAT(NameBuff, "*");
6142 STRCAT(NameBuff, ext);
6143 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6144 EW_FILE|EW_SILENT) == FAIL
6145 || filecount == 0)
6146 {
6147 if (!got_int)
6148 EMSG2("E151: No match: %s", NameBuff);
6149 return;
6150 }
6151
6152 /*
6153 * Open the tags file for writing.
6154 * Do this before scanning through all the files.
6155 */
6156 STRCPY(NameBuff, dir);
6157 add_pathsep(NameBuff);
6158 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006159 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (fd_tags == NULL)
6161 {
6162 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6163 FreeWild(filecount, files);
6164 return;
6165 }
6166
6167 /*
6168 * If generating tags for "$VIMRUNTIME/doc" add the "help-tags" tag.
6169 */
6170 ga_init2(&ga, (int)sizeof(char_u *), 100);
6171 if (fullpathcmp((char_u *)"$VIMRUNTIME/doc", dir, FALSE) == FPC_SAME)
6172 {
6173 if (ga_grow(&ga, 1) == FAIL)
6174 got_int = TRUE;
6175 else
6176 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006177 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (s == NULL)
6179 got_int = TRUE;
6180 else
6181 {
6182 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6183 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6184 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
6186 }
6187 }
6188
6189 /*
6190 * Go over all the files and extract the tags.
6191 */
6192 for (fi = 0; fi < filecount && !got_int; ++fi)
6193 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006194 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (fd == NULL)
6196 {
6197 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6198 continue;
6199 }
6200 fname = gettail(files[fi]);
6201
6202# ifdef FEAT_MBYTE
6203 firstline = TRUE;
6204# endif
6205 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6206 {
6207# ifdef FEAT_MBYTE
6208 if (firstline)
6209 {
6210 /* Detect utf-8 file by a non-ASCII char in the first line. */
6211 this_utf8 = MAYBE;
6212 for (s = IObuff; *s != NUL; ++s)
6213 if (*s >= 0x80)
6214 {
6215 int l;
6216
6217 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006218 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (l == 1)
6220 {
6221 /* Illegal UTF-8 byte sequence. */
6222 this_utf8 = FALSE;
6223 break;
6224 }
6225 s += l - 1;
6226 }
6227 if (this_utf8 == MAYBE) /* only ASCII characters found */
6228 this_utf8 = FALSE;
6229 if (utf8 == MAYBE) /* first file */
6230 utf8 = this_utf8;
6231 else if (utf8 != this_utf8)
6232 {
6233 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6234 mix = !got_int;
6235 got_int = TRUE;
6236 }
6237 firstline = FALSE;
6238 }
6239# endif
6240 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6241 while (p1 != NULL)
6242 {
6243 p2 = vim_strchr(p1 + 1, '*'); /* find second '*' */
6244 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6245 {
6246 for (s = p1 + 1; s < p2; ++s)
6247 if (*s == ' ' || *s == '\t' || *s == '|')
6248 break;
6249
6250 /*
6251 * Only accept a *tag* when it consists of valid
6252 * characters, there is white space before it and is
6253 * followed by a white character or end-of-line.
6254 */
6255 if (s == p2
6256 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6257 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6258 || s[1] == '\0'))
6259 {
6260 *p2 = '\0';
6261 ++p1;
6262 if (ga_grow(&ga, 1) == FAIL)
6263 {
6264 got_int = TRUE;
6265 break;
6266 }
6267 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6268 if (s == NULL)
6269 {
6270 got_int = TRUE;
6271 break;
6272 }
6273 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6274 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 sprintf((char *)s, "%s\t%s", p1, fname);
6276
6277 /* find next '*' */
6278 p2 = vim_strchr(p2 + 1, '*');
6279 }
6280 }
6281 p1 = p2;
6282 }
6283 line_breakcheck();
6284 }
6285
6286 fclose(fd);
6287 }
6288
6289 FreeWild(filecount, files);
6290
6291 if (!got_int)
6292 {
6293 /*
6294 * Sort the tags.
6295 */
6296 sort_strings((char_u **)ga.ga_data, ga.ga_len);
6297
6298 /*
6299 * Check for duplicates.
6300 */
6301 for (i = 1; i < ga.ga_len; ++i)
6302 {
6303 p1 = ((char_u **)ga.ga_data)[i - 1];
6304 p2 = ((char_u **)ga.ga_data)[i];
6305 while (*p1 == *p2)
6306 {
6307 if (*p2 == '\t')
6308 {
6309 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006310 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6312 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6313 EMSG(NameBuff);
6314 *p2 = '\t';
6315 break;
6316 }
6317 ++p1;
6318 ++p2;
6319 }
6320 }
6321
6322# ifdef FEAT_MBYTE
6323 if (utf8 == TRUE)
6324 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6325# endif
6326
6327 /*
6328 * Write the tags into the file.
6329 */
6330 for (i = 0; i < ga.ga_len; ++i)
6331 {
6332 s = ((char_u **)ga.ga_data)[i];
6333 if (STRNCMP(s, "help-tags", 9) == 0)
6334 /* help-tags entry was added in formatted form */
6335 fprintf(fd_tags, (char *)s);
6336 else
6337 {
6338 fprintf(fd_tags, "%s\t/*", s);
6339 for (p1 = s; *p1 != '\t'; ++p1)
6340 {
6341 /* insert backslash before '\\' and '/' */
6342 if (*p1 == '\\' || *p1 == '/')
6343 putc('\\', fd_tags);
6344 putc(*p1, fd_tags);
6345 }
6346 fprintf(fd_tags, "*\n");
6347 }
6348 }
6349 }
6350#ifdef FEAT_MBYTE
6351 if (mix)
6352 got_int = FALSE; /* continue with other languages */
6353#endif
6354
6355 for (i = 0; i < ga.ga_len; ++i)
6356 vim_free(((char_u **)ga.ga_data)[i]);
6357 ga_clear(&ga);
6358 fclose(fd_tags); /* there is no check for an error... */
6359}
6360#endif
6361
6362#if defined(FEAT_SIGNS) || defined(PROTO)
6363
6364/*
6365 * Struct to hold the sign properties.
6366 */
6367typedef struct sign sign_T;
6368
6369struct sign
6370{
6371 sign_T *sn_next; /* next sign in list */
6372 int sn_typenr; /* type number of sign (negative if not equal
6373 to name) */
6374 char_u *sn_name; /* name of sign */
6375 char_u *sn_icon; /* name of pixmap */
6376#ifdef FEAT_SIGN_ICONS
6377 void *sn_image; /* icon image */
6378#endif
6379 char_u *sn_text; /* text used instead of pixmap */
6380 int sn_line_hl; /* highlight ID for line */
6381 int sn_text_hl; /* highlight ID for text */
6382};
6383
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384static sign_T *first_sign = NULL;
6385static int last_sign_typenr = MAX_TYPENR; /* is decremented */
6386
6387static void sign_list_defined __ARGS((sign_T *sp));
6388
6389/*
6390 * ":sign" command
6391 */
6392 void
6393ex_sign(eap)
6394 exarg_T *eap;
6395{
6396 char_u *arg = eap->arg;
6397 char_u *p;
6398 int idx;
6399 sign_T *sp;
6400 sign_T *sp_prev;
6401 buf_T *buf;
6402 static char *cmds[] = {
6403 "define",
6404#define SIGNCMD_DEFINE 0
6405 "undefine",
6406#define SIGNCMD_UNDEFINE 1
6407 "list",
6408#define SIGNCMD_LIST 2
6409 "place",
6410#define SIGNCMD_PLACE 3
6411 "unplace",
6412#define SIGNCMD_UNPLACE 4
6413 "jump",
6414#define SIGNCMD_JUMP 5
6415#define SIGNCMD_LAST 6
6416 };
6417
6418 /* Parse the subcommand. */
6419 p = skiptowhite(arg);
6420 if (*p != NUL)
6421 *p++ = NUL;
6422 for (idx = 0; ; ++idx)
6423 {
6424 if (idx == SIGNCMD_LAST)
6425 {
6426 EMSG2(_("E160: Unknown sign command: %s"), arg);
6427 return;
6428 }
6429 if (STRCMP(arg, cmds[idx]) == 0)
6430 break;
6431 }
6432 arg = skipwhite(p);
6433
6434 if (idx <= SIGNCMD_LIST)
6435 {
6436 /*
6437 * Define, undefine or list signs.
6438 */
6439 if (idx == SIGNCMD_LIST && *arg == NUL)
6440 {
6441 /* ":sign list": list all defined signs */
6442 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6443 sign_list_defined(sp);
6444 }
6445 else if (*arg == NUL)
6446 EMSG(_("E156: Missing sign name"));
6447 else
6448 {
6449 p = skiptowhite(arg);
6450 if (*p != NUL)
6451 *p++ = NUL;
6452 sp_prev = NULL;
6453 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6454 {
6455 if (STRCMP(sp->sn_name, arg) == 0)
6456 break;
6457 sp_prev = sp;
6458 }
6459 if (idx == SIGNCMD_DEFINE)
6460 {
6461 /* ":sign define {name} ...": define a sign */
6462 if (sp == NULL)
6463 {
6464 /* Allocate a new sign. */
6465 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
6466 if (sp == NULL)
6467 return;
6468 if (sp_prev == NULL)
6469 first_sign = sp;
6470 else
6471 sp_prev->sn_next = sp;
6472 sp->sn_name = vim_strnsave(arg, (int)(p - arg));
6473
6474 /* If the name is a number use that for the typenr,
6475 * otherwise use a negative number. */
6476 if (VIM_ISDIGIT(*arg))
6477 sp->sn_typenr = atoi((char *)arg);
6478 else
6479 {
6480 sign_T *lp;
6481 int start = last_sign_typenr;
6482
6483 for (lp = first_sign; lp != NULL; lp = lp->sn_next)
6484 {
6485 if (lp->sn_typenr == last_sign_typenr)
6486 {
6487 --last_sign_typenr;
6488 if (last_sign_typenr == 0)
6489 last_sign_typenr = MAX_TYPENR;
6490 if (last_sign_typenr == start)
6491 {
6492 EMSG(_("E612: Too many signs defined"));
6493 return;
6494 }
6495 lp = first_sign;
6496 continue;
6497 }
6498 }
6499
6500 sp->sn_typenr = last_sign_typenr--;
6501 if (last_sign_typenr == 0)
6502 last_sign_typenr = MAX_TYPENR; /* wrap around */
6503 }
6504 }
6505
6506 /* set values for a defined sign. */
6507 for (;;)
6508 {
6509 arg = skipwhite(p);
6510 if (*arg == NUL)
6511 break;
6512 p = skiptowhite_esc(arg);
6513 if (STRNCMP(arg, "icon=", 5) == 0)
6514 {
6515 arg += 5;
6516 vim_free(sp->sn_icon);
6517 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
6518 backslash_halve(sp->sn_icon);
6519#ifdef FEAT_SIGN_ICONS
6520 if (gui.in_use)
6521 {
6522 out_flush();
6523 if (sp->sn_image != NULL)
6524 gui_mch_destroy_sign(sp->sn_image);
6525 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6526 }
6527#endif
6528 }
6529 else if (STRNCMP(arg, "text=", 5) == 0)
6530 {
6531 char_u *s;
6532 int cells;
6533 int len;
6534
6535 arg += 5;
6536#ifdef FEAT_MBYTE
6537 /* Count cells and check for non-printable chars */
6538 if (has_mbyte)
6539 {
6540 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006541 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
6543 if (!vim_isprintc((*mb_ptr2char)(s)))
6544 break;
6545 cells += (*mb_ptr2cells)(s);
6546 }
6547 }
6548 else
6549#endif
6550 {
6551 for (s = arg; s < p; ++s)
6552 if (!vim_isprintc(*s))
6553 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006554 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556 /* Currently must be one or two display cells */
6557 if (s != p || cells < 1 || cells > 2)
6558 {
6559 *p = NUL;
6560 EMSG2(_("E239: Invalid sign text: %s"), arg);
6561 return;
6562 }
6563
6564 vim_free(sp->sn_text);
6565 /* Allocate one byte more if we need to pad up
6566 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006567 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 sp->sn_text = vim_strnsave(arg, len);
6569
6570 if (sp->sn_text != NULL && cells == 1)
6571 STRCPY(sp->sn_text + len - 1, " ");
6572 }
6573 else if (STRNCMP(arg, "linehl=", 7) == 0)
6574 {
6575 arg += 7;
6576 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
6577 }
6578 else if (STRNCMP(arg, "texthl=", 7) == 0)
6579 {
6580 arg += 7;
6581 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
6582 }
6583 else
6584 {
6585 EMSG2(_(e_invarg2), arg);
6586 return;
6587 }
6588 }
6589 }
6590 else if (sp == NULL)
6591 EMSG2(_("E155: Unknown sign: %s"), arg);
6592 else if (idx == SIGNCMD_LIST)
6593 /* ":sign list {name}" */
6594 sign_list_defined(sp);
6595 else
6596 {
6597 /* ":sign undefine {name}" */
6598 vim_free(sp->sn_name);
6599 vim_free(sp->sn_icon);
6600#ifdef FEAT_SIGN_ICONS
6601 if (sp->sn_image != NULL)
6602 {
6603 out_flush();
6604 gui_mch_destroy_sign(sp->sn_image);
6605 }
6606#endif
6607 vim_free(sp->sn_text);
6608 if (sp_prev == NULL)
6609 first_sign = sp->sn_next;
6610 else
6611 sp_prev->sn_next = sp->sn_next;
6612 vim_free(sp);
6613 }
6614 }
6615 }
6616 else
6617 {
6618 int id = -1;
6619 linenr_T lnum = -1;
6620 char_u *sign_name = NULL;
6621 char_u *arg1;
6622
6623 if (*arg == NUL)
6624 {
6625 if (idx == SIGNCMD_PLACE)
6626 {
6627 /* ":sign place": list placed signs in all buffers */
6628 sign_list_placed(NULL);
6629 }
6630 else if (idx == SIGNCMD_UNPLACE)
6631 {
6632 /* ":sign unplace": remove placed sign at cursor */
6633 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
6634 if (id > 0)
6635 {
6636 buf_delsign(curwin->w_buffer, id);
6637 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
6638 }
6639 else
6640 EMSG(_("E159: Missing sign number"));
6641 }
6642 else
6643 EMSG(_(e_argreq));
6644 return;
6645 }
6646
6647 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
6648 {
6649 /* ":sign unplace *": remove all placed signs */
6650 buf_delete_all_signs();
6651 return;
6652 }
6653
6654 /* first arg could be placed sign id */
6655 arg1 = arg;
6656 if (VIM_ISDIGIT(*arg))
6657 {
6658 id = getdigits(&arg);
6659 if (!vim_iswhite(*arg) && *arg != NUL)
6660 {
6661 id = -1;
6662 arg = arg1;
6663 }
6664 else
6665 {
6666 arg = skipwhite(arg);
6667 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
6668 {
6669 /* ":sign unplace {id}": remove placed sign by number */
6670 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6671 if ((lnum = buf_delsign(buf, id)) != 0)
6672 update_debug_sign(buf, lnum);
6673 return;
6674 }
6675 }
6676 }
6677
6678 /*
6679 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
6680 * Leave "arg" pointing to {fname}.
6681 */
6682 for (;;)
6683 {
6684 if (STRNCMP(arg, "line=", 5) == 0)
6685 {
6686 arg += 5;
6687 lnum = atoi((char *)arg);
6688 arg = skiptowhite(arg);
6689 }
6690 else if (STRNCMP(arg, "name=", 5) == 0)
6691 {
6692 arg += 5;
6693 sign_name = arg;
6694 arg = skiptowhite(arg);
6695 if (*arg != NUL)
6696 *arg++ = NUL;
6697 }
6698 else if (STRNCMP(arg, "file=", 5) == 0)
6699 {
6700 arg += 5;
6701 buf = buflist_findname(arg);
6702 break;
6703 }
6704 else if (STRNCMP(arg, "buffer=", 7) == 0)
6705 {
6706 arg += 7;
6707 buf = buflist_findnr((int)getdigits(&arg));
6708 if (*skipwhite(arg) != NUL)
6709 EMSG(_(e_trailing));
6710 break;
6711 }
6712 else
6713 {
6714 EMSG(_(e_invarg));
6715 return;
6716 }
6717 arg = skipwhite(arg);
6718 }
6719
6720 if (buf == NULL)
6721 {
6722 EMSG2(_("E158: Invalid buffer name: %s"), arg);
6723 }
6724 else if (id <= 0)
6725 {
6726 if (lnum >= 0 || sign_name != NULL)
6727 EMSG(_(e_invarg));
6728 else
6729 /* ":sign place file={fname}": list placed signs in one file */
6730 sign_list_placed(buf);
6731 }
6732 else if (idx == SIGNCMD_JUMP)
6733 {
6734 /* ":sign jump {id} file={fname}" */
6735 if (lnum >= 0 || sign_name != NULL)
6736 EMSG(_(e_invarg));
6737 else if ((lnum = buf_findsign(buf, id)) > 0)
6738 { /* goto a sign ... */
6739 if (buf_jump_open_win(buf) != NULL)
6740 { /* ... in a current window */
6741 curwin->w_cursor.lnum = lnum;
6742 check_cursor_lnum();
6743 beginline(BL_WHITE);
6744 }
6745 else
6746 { /* ... not currently in a window */
6747 char_u *cmd;
6748
6749 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
6750 if (cmd == NULL)
6751 return;
6752 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
6753 do_cmdline_cmd(cmd);
6754 vim_free(cmd);
6755 }
6756#ifdef FEAT_FOLDING
6757 foldOpenCursor();
6758#endif
6759 }
6760 else
6761 EMSGN(_("E157: Invalid sign ID: %ld"), id);
6762 }
6763 else if (idx == SIGNCMD_UNPLACE)
6764 {
6765 /* ":sign unplace {id} file={fname}" */
6766 if (lnum >= 0 || sign_name != NULL)
6767 EMSG(_(e_invarg));
6768 else
6769 {
6770 lnum = buf_delsign(buf, id);
6771 update_debug_sign(buf, lnum);
6772 }
6773 }
6774 /* idx == SIGNCMD_PLACE */
6775 else if (sign_name != NULL)
6776 {
6777 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6778 if (STRCMP(sp->sn_name, sign_name) == 0)
6779 break;
6780 if (sp == NULL)
6781 {
6782 EMSG2(_("E155: Unknown sign: %s"), sign_name);
6783 return;
6784 }
6785 if (lnum > 0)
6786 /* ":sign place {id} line={lnum} name={name} file={fname}":
6787 * place a sign */
6788 buf_addsign(buf, id, lnum, sp->sn_typenr);
6789 else
6790 /* ":sign place {id} file={fname}": change sign type */
6791 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
6792 update_debug_sign(buf, lnum);
6793 }
6794 else
6795 EMSG(_(e_invarg));
6796 }
6797}
6798
6799#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6800/*
6801 * Allocate the icons. Called when the GUI has started. Allows defining
6802 * signs before it starts.
6803 */
6804 void
6805sign_gui_started()
6806{
6807 sign_T *sp;
6808
6809 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6810 if (sp->sn_icon != NULL)
6811 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6812}
6813#endif
6814
6815/*
6816 * List one sign.
6817 */
6818 static void
6819sign_list_defined(sp)
6820 sign_T *sp;
6821{
6822 char_u *p;
6823
Bram Moolenaar555b2802005-05-19 21:08:39 +00006824 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (sp->sn_icon != NULL)
6826 {
6827 MSG_PUTS(" icon=");
6828 msg_outtrans(sp->sn_icon);
6829#ifdef FEAT_SIGN_ICONS
6830 if (sp->sn_image == NULL)
6831 MSG_PUTS(_(" (NOT FOUND)"));
6832#else
6833 MSG_PUTS(_(" (not supported)"));
6834#endif
6835 }
6836 if (sp->sn_text != NULL)
6837 {
6838 MSG_PUTS(" text=");
6839 msg_outtrans(sp->sn_text);
6840 }
6841 if (sp->sn_line_hl > 0)
6842 {
6843 MSG_PUTS(" linehl=");
6844 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
6845 if (p == NULL)
6846 MSG_PUTS("NONE");
6847 else
6848 msg_puts(p);
6849 }
6850 if (sp->sn_text_hl > 0)
6851 {
6852 MSG_PUTS(" texthl=");
6853 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
6854 if (p == NULL)
6855 MSG_PUTS("NONE");
6856 else
6857 msg_puts(p);
6858 }
6859}
6860
6861/*
6862 * Get highlighting attribute for sign "typenr".
6863 * If "line" is TRUE: line highl, if FALSE: text highl.
6864 */
6865 int
6866sign_get_attr(typenr, line)
6867 int typenr;
6868 int line;
6869{
6870 sign_T *sp;
6871
6872 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6873 if (sp->sn_typenr == typenr)
6874 {
6875 if (line)
6876 {
6877 if (sp->sn_line_hl > 0)
6878 return syn_id2attr(sp->sn_line_hl);
6879 }
6880 else
6881 {
6882 if (sp->sn_text_hl > 0)
6883 return syn_id2attr(sp->sn_text_hl);
6884 }
6885 break;
6886 }
6887 return 0;
6888}
6889
6890/*
6891 * Get text mark for sign "typenr".
6892 * Returns NULL if there isn't one.
6893 */
6894 char_u *
6895sign_get_text(typenr)
6896 int typenr;
6897{
6898 sign_T *sp;
6899
6900 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6901 if (sp->sn_typenr == typenr)
6902 return sp->sn_text;
6903 return NULL;
6904}
6905
6906#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6907 void *
6908sign_get_image(typenr)
6909 int typenr; /* the attribute which may have a sign */
6910{
6911 sign_T *sp;
6912
6913 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6914 if (sp->sn_typenr == typenr)
6915 return sp->sn_image;
6916 return NULL;
6917}
6918#endif
6919
6920/*
6921 * Get the name of a sign by its typenr.
6922 */
6923 char_u *
6924sign_typenr2name(typenr)
6925 int typenr;
6926{
6927 sign_T *sp;
6928
6929 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6930 if (sp->sn_typenr == typenr)
6931 return sp->sn_name;
6932 return (char_u *)_("[Deleted]");
6933}
6934
6935#endif
6936
6937#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
6938/*
6939 * ":drop"
6940 * Opens the first argument in a window. When there are two or more arguments
6941 * the argument list is redefined.
6942 */
6943 void
6944ex_drop(eap)
6945 exarg_T *eap;
6946{
6947 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 win_T *wp;
6949 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006950# ifdef FEAT_WINDOWS
6951 tabpage_T *tp;
6952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 /*
6955 * Check if the first argument is already being edited in a window. If
6956 * so, jump to that window.
6957 * We would actually need to check all arguments, but that's complicated
6958 * and mostly only one file is dropped.
6959 * This also ignores wildcards, since it is very unlikely the user is
6960 * editing a file name with a wildcard character.
6961 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006962 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006964# ifdef FEAT_WINDOWS
6965 if (cmdmod.tab)
6966 {
6967 /* ":tab drop file ...": open a tab for each argument that isn't
6968 * edited in a window yet. It's like ":tab all" but without closing
6969 * windows or tabs. */
6970 ex_all(eap);
6971 }
6972 else
6973# endif
6974 {
6975 /* ":drop file ...": Edit the first argument. Jump to an existing
6976 * window if possible, edit in current window if the current buffer
6977 * can be abandoned, otherwise open a new window. */
6978 buf = buflist_findnr(ARGLIST[0].ae_fnum);
6979
6980 FOR_ALL_TAB_WINDOWS(tp, wp)
6981 {
6982 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006984# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006985 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 return;
6989 }
6990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006992 /*
6993 * Check whether the current buffer is changed. If so, we will need
6994 * to split the current window or data could be lost.
6995 * Skip the check if the 'hidden' option is set, as in this case the
6996 * buffer won't be lost.
6997 */
6998 if (!P_HID(curbuf))
6999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007001 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007003 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007005 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007007 if (split)
7008 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007012 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7013 if (split)
7014 {
7015 eap->cmdidx = CMD_sfirst;
7016 eap->cmd[0] = 's';
7017 }
7018 else
7019 eap->cmdidx = CMD_first;
7020 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022}
7023#endif