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