blob: 9ba3e81412b33bb76bb6d95c1540510fa57ed181 [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
302 * if we return 0 every time, qsort will assume it's done sorting and exit */
303 if (sort_abort)
304 return 0;
305 fast_breakcheck();
306 if (got_int)
307 sort_abort = TRUE;
308
309 /* When sorting numbers "start_col_nr" is the number, not the column number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000310 if (sort_nr)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000311 result = l1.start_col_nr - l2.start_col_nr;
312 else
313 {
314 /* We need to copy one line into "sortbuf1", because there is no guarantee
315 * that the first pointer becomes invalid when obtaining the second one. */
316 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.start_col_nr, l1.end_col_nr - l1.start_col_nr + 1);
317 sortbuf1[l1.end_col_nr - l1.start_col_nr] = 0;
318 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.start_col_nr, l2.end_col_nr - l2.start_col_nr + 1);
319 sortbuf2[l2.end_col_nr - l2.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000320
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000321 result = sort_ic ? STRICMP(sortbuf1, sortbuf2) : STRCMP(sortbuf1, sortbuf2);
322 }
323 /* If the two lines have the same value, preserve the original line order */
324 if (result == 0)
325 return (int) (l1.lnum - l2.lnum);
326 else
327 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000328}
329
330/*
331 * ":sort".
332 */
333 void
334ex_sort(eap)
335 exarg_T *eap;
336{
337 regmatch_T regmatch;
338 int len;
339 linenr_T lnum;
340 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000341 sorti_T *nrs;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000342 size_t count = eap->line2 - eap->line1 + 1;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000343 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000344 char_u *p;
345 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000346 char_u *s2;
347 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000348 int unique = FALSE;
349 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350 colnr_T start_col;
351 colnr_T end_col;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000352 int sort_oct; /* sort on octal number */
353 int sort_hex; /* sort on hex number */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000354
355 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
356 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000357 sortbuf1 = NULL;
358 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000359 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000360 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000361 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000362 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000363
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000364 sort_abort = sort_ic = sort_rx = sort_nr = sort_oct = sort_hex = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000365
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000366 for (p = eap->arg; *p != NUL; ++p)
367 {
368 if (vim_iswhite(*p))
369 ;
370 else if (*p == 'i')
371 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000372 else if (*p == 'r')
373 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000374 else if (*p == 'n')
375 sort_nr = 2;
376 else if (*p == 'o')
377 sort_oct = 2;
378 else if (*p == 'x')
379 sort_hex = 2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000380 else if (*p == 'u')
381 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000382 else if (*p == '"') /* comment start */
383 break;
384 else if (check_nextcmd(p) != NULL)
385 {
386 eap->nextcmd = check_nextcmd(p);
387 break;
388 }
389 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000390 {
391 s = skip_regexp(p + 1, *p, TRUE, NULL);
392 if (*s != *p)
393 {
394 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000395 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000396 }
397 *s = NUL;
398 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
399 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000400 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000401 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000402 regmatch.rm_ic = p_ic;
403 }
404 else
405 {
406 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000407 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000408 }
409 }
410
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000411 /* Can only have one of 'n', 'o' and 'x'. */
412 if (sort_nr + sort_oct + sort_hex > 2)
413 {
414 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000415 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000416 }
417
418 /* From here on "sort_nr" is used as a flag for any number sorting. */
419 sort_nr += sort_oct + sort_hex;
420
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000421 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000422 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000423 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000424 * When sorting on strings "start_col_nr" is the offset in the line, for
425 * numbers sorting it's the number to sort on. This means the pattern
426 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000427 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000428 */
429 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
430 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000431 s = ml_get(lnum);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000432 len = STRLEN(s);
433 if (maxlen < len)
434 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000435
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000436 start_col = 0;
437 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000438 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000439 {
440 if (sort_rx)
441 {
442 start_col = regmatch.startp[0] - s;
443 end_col = regmatch.endp[0] - s;
444 }
445 else
446 start_col = regmatch.endp[0] - s;
447 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000448 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000449 if (regmatch.regprog != NULL)
450 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000451
452 if (sort_nr)
453 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000454 /* Make sure vim_str2nr doesn't read any digits past the end
455 * of the match, by temporarily terminating the string there */
456 s2 = s + end_col;
457 c = *s2;
458 (*s2) = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000459 /* Sorting on number: Store the number itself. */
460 if (sort_hex)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000461 s = skiptohex(s + start_col);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000462 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000463 s = skiptodigit(s + start_col);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000464 vim_str2nr(s, NULL, NULL, sort_oct, sort_hex,
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000465 &nrs[lnum - eap->line1].start_col_nr, NULL);
466 (*s2) = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000467 }
468 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000469 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000470 /* Store the column to sort at. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000471 nrs[lnum - eap->line1].start_col_nr = start_col;
472 nrs[lnum - eap->line1].end_col_nr = end_col;
473 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000474
475 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000476
477 if (regmatch.regprog != NULL)
478 fast_breakcheck();
479 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000480 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000481 }
482
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000483 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000484 sortbuf1 = alloc((unsigned)maxlen + 1);
485 if (sortbuf1 == NULL)
486 goto sortend;
487 sortbuf2 = alloc((unsigned)maxlen + 1);
488 if (sortbuf2 == NULL)
489 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000490
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000491 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000492 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000493
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000494 if (sort_abort)
495 goto sortend;
496
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000497 /* Insert the lines in the sorted order below the last one. */
498 lnum = eap->line2;
499 for (i = 0; i < count; ++i)
500 {
501 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
502 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000503 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000504 {
505 if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL)
506 break;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000507 if (unique)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000508 STRCPY(sortbuf1, s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000509 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000510 fast_breakcheck();
511 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000512 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000513 }
514
515 /* delete the original lines if appending worked */
516 if (i == count)
517 for (i = 0; i < count; ++i)
518 ml_delete(eap->line1, FALSE);
519 else
520 count = 0;
521
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000522 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000523 deleted = count - (lnum - eap->line2);
524 if (deleted > 0)
525 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
526 else if (deleted < 0)
527 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
528 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000529
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000530 curwin->w_cursor.lnum = eap->line1;
531 beginline(BL_WHITE | BL_FIX);
532
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000533sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000534 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000535 vim_free(sortbuf1);
536 vim_free(sortbuf2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000537 vim_free(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000538 if (got_int)
539 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000540}
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542/*
543 * ":retab".
544 */
545 void
546ex_retab(eap)
547 exarg_T *eap;
548{
549 linenr_T lnum;
550 int got_tab = FALSE;
551 long num_spaces = 0;
552 long num_tabs;
553 long len;
554 long col;
555 long vcol;
556 long start_col = 0; /* For start of white-space string */
557 long start_vcol = 0; /* For start of white-space string */
558 int temp;
559 long old_len;
560 char_u *ptr;
561 char_u *new_line = (char_u *)1; /* init to non-NULL */
562 int did_undo; /* called u_save for current line */
563 int new_ts;
564 int save_list;
565 linenr_T first_line = 0; /* first changed line */
566 linenr_T last_line = 0; /* last changed line */
567
568 save_list = curwin->w_p_list;
569 curwin->w_p_list = 0; /* don't want list mode here */
570
571 new_ts = getdigits(&(eap->arg));
572 if (new_ts < 0)
573 {
574 EMSG(_(e_positive));
575 return;
576 }
577 if (new_ts == 0)
578 new_ts = curbuf->b_p_ts;
579 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
580 {
581 ptr = ml_get(lnum);
582 col = 0;
583 vcol = 0;
584 did_undo = FALSE;
585 for (;;)
586 {
587 if (vim_iswhite(ptr[col]))
588 {
589 if (!got_tab && num_spaces == 0)
590 {
591 /* First consecutive white-space */
592 start_vcol = vcol;
593 start_col = col;
594 }
595 if (ptr[col] == ' ')
596 num_spaces++;
597 else
598 got_tab = TRUE;
599 }
600 else
601 {
602 if (got_tab || (eap->forceit && num_spaces > 1))
603 {
604 /* Retabulate this string of white-space */
605
606 /* len is virtual length of white string */
607 len = num_spaces = vcol - start_vcol;
608 num_tabs = 0;
609 if (!curbuf->b_p_et)
610 {
611 temp = new_ts - (start_vcol % new_ts);
612 if (num_spaces >= temp)
613 {
614 num_spaces -= temp;
615 num_tabs++;
616 }
617 num_tabs += num_spaces / new_ts;
618 num_spaces -= (num_spaces / new_ts) * new_ts;
619 }
620 if (curbuf->b_p_et || got_tab ||
621 (num_spaces + num_tabs < len))
622 {
623 if (did_undo == FALSE)
624 {
625 did_undo = TRUE;
626 if (u_save((linenr_T)(lnum - 1),
627 (linenr_T)(lnum + 1)) == FAIL)
628 {
629 new_line = NULL; /* flag out-of-memory */
630 break;
631 }
632 }
633
634 /* len is actual number of white characters used */
635 len = num_spaces + num_tabs;
636 old_len = (long)STRLEN(ptr);
637 new_line = lalloc(old_len - col + start_col + len + 1,
638 TRUE);
639 if (new_line == NULL)
640 break;
641 if (start_col > 0)
642 mch_memmove(new_line, ptr, (size_t)start_col);
643 mch_memmove(new_line + start_col + len,
644 ptr + col, (size_t)(old_len - col + 1));
645 ptr = new_line + start_col;
646 for (col = 0; col < len; col++)
647 ptr[col] = (col < num_tabs) ? '\t' : ' ';
648 ml_replace(lnum, new_line, FALSE);
649 if (first_line == 0)
650 first_line = lnum;
651 last_line = lnum;
652 ptr = new_line;
653 col = start_col + len;
654 }
655 }
656 got_tab = FALSE;
657 num_spaces = 0;
658 }
659 if (ptr[col] == NUL)
660 break;
661 vcol += chartabsize(ptr + col, (colnr_T)vcol);
662#ifdef FEAT_MBYTE
663 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000664 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 else
666#endif
667 ++col;
668 }
669 if (new_line == NULL) /* out of memory */
670 break;
671 line_breakcheck();
672 }
673 if (got_int)
674 EMSG(_(e_interr));
675
676 if (curbuf->b_p_ts != new_ts)
677 redraw_curbuf_later(NOT_VALID);
678 if (first_line != 0)
679 changed_lines(first_line, 0, last_line + 1, 0L);
680
681 curwin->w_p_list = save_list; /* restore 'list' */
682
683 curbuf->b_p_ts = new_ts;
684 coladvance(curwin->w_curswant);
685
686 u_clearline();
687}
688#endif
689
690/*
691 * :move command - move lines line1-line2 to line dest
692 *
693 * return FAIL for failure, OK otherwise
694 */
695 int
696do_move(line1, line2, dest)
697 linenr_T line1;
698 linenr_T line2;
699 linenr_T dest;
700{
701 char_u *str;
702 linenr_T l;
703 linenr_T extra; /* Num lines added before line1 */
704 linenr_T num_lines; /* Num lines moved */
705 linenr_T last_line; /* Last line in file after adding new text */
706
707 if (dest >= line1 && dest < line2)
708 {
709 EMSG(_("E134: Move lines into themselves"));
710 return FAIL;
711 }
712
713 num_lines = line2 - line1 + 1;
714
715 /*
716 * First we copy the old text to its new location -- webb
717 * Also copy the flag that ":global" command uses.
718 */
719 if (u_save(dest, dest + 1) == FAIL)
720 return FAIL;
721 for (extra = 0, l = line1; l <= line2; l++)
722 {
723 str = vim_strsave(ml_get(l + extra));
724 if (str != NULL)
725 {
726 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
727 vim_free(str);
728 if (dest < line1)
729 extra++;
730 }
731 }
732
733 /*
734 * Now we must be careful adjusting our marks so that we don't overlap our
735 * mark_adjust() calls.
736 *
737 * We adjust the marks within the old text so that they refer to the
738 * last lines of the file (temporarily), because we know no other marks
739 * will be set there since these line numbers did not exist until we added
740 * our new lines.
741 *
742 * Then we adjust the marks on lines between the old and new text positions
743 * (either forwards or backwards).
744 *
745 * And Finally we adjust the marks we put at the end of the file back to
746 * their final destination at the new text position -- webb
747 */
748 last_line = curbuf->b_ml.ml_line_count;
749 mark_adjust(line1, line2, last_line - line2, 0L);
750 if (dest >= line2)
751 {
752 mark_adjust(line2 + 1, dest, -num_lines, 0L);
753 curbuf->b_op_start.lnum = dest - num_lines + 1;
754 curbuf->b_op_end.lnum = dest;
755 }
756 else
757 {
758 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
759 curbuf->b_op_start.lnum = dest + 1;
760 curbuf->b_op_end.lnum = dest + num_lines;
761 }
762 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
763 mark_adjust(last_line - num_lines + 1, last_line,
764 -(last_line - dest - extra), 0L);
765
766 /*
767 * Now we delete the original text -- webb
768 */
769 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
770 return FAIL;
771
772 for (l = line1; l <= line2; l++)
773 ml_delete(line1 + extra, TRUE);
774
775 if (!global_busy && num_lines > p_report)
776 {
777 if (num_lines == 1)
778 MSG(_("1 line moved"));
779 else
780 smsg((char_u *)_("%ld lines moved"), num_lines);
781 }
782
783 /*
784 * Leave the cursor on the last of the moved lines.
785 */
786 if (dest >= line1)
787 curwin->w_cursor.lnum = dest;
788 else
789 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
790
791 if (line1 < dest)
792 changed_lines(line1, 0, dest + num_lines + 1, 0L);
793 else
794 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
795
796 return OK;
797}
798
799/*
800 * ":copy"
801 */
802 void
803ex_copy(line1, line2, n)
804 linenr_T line1;
805 linenr_T line2;
806 linenr_T n;
807{
808 linenr_T count;
809 char_u *p;
810
811 count = line2 - line1 + 1;
812 curbuf->b_op_start.lnum = n + 1;
813 curbuf->b_op_end.lnum = n + count;
814 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
815
816 /*
817 * there are three situations:
818 * 1. destination is above line1
819 * 2. destination is between line1 and line2
820 * 3. destination is below line2
821 *
822 * n = destination (when starting)
823 * curwin->w_cursor.lnum = destination (while copying)
824 * line1 = start of source (while copying)
825 * line2 = end of source (while copying)
826 */
827 if (u_save(n, n + 1) == FAIL)
828 return;
829
830 curwin->w_cursor.lnum = n;
831 while (line1 <= line2)
832 {
833 /* need to use vim_strsave() because the line will be unlocked within
834 * ml_append() */
835 p = vim_strsave(ml_get(line1));
836 if (p != NULL)
837 {
838 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
839 vim_free(p);
840 }
841 /* situation 2: skip already copied lines */
842 if (line1 == n)
843 line1 = curwin->w_cursor.lnum;
844 ++line1;
845 if (curwin->w_cursor.lnum < line1)
846 ++line1;
847 if (curwin->w_cursor.lnum < line2)
848 ++line2;
849 ++curwin->w_cursor.lnum;
850 }
851
852 appended_lines_mark(n, count);
853
854 msgmore((long)count);
855}
856
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000857static char_u *prevcmd = NULL; /* the previous command */
858
859#if defined(EXITFREE) || defined(PROTO)
860 void
861free_prev_shellcmd()
862{
863 vim_free(prevcmd);
864}
865#endif
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867/*
868 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
869 * Bangs in the argument are replaced with the previously entered command.
870 * Remember the argument.
871 *
872 * RISCOS: Bangs only replaced when followed by a space, since many
873 * pathnames contain one.
874 */
875 void
876do_bang(addr_count, eap, forceit, do_in, do_out)
877 int addr_count;
878 exarg_T *eap;
879 int forceit;
880 int do_in, do_out;
881{
882 char_u *arg = eap->arg; /* command */
883 linenr_T line1 = eap->line1; /* start of range */
884 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 char_u *newcmd = NULL; /* the new command */
886 int free_newcmd = FALSE; /* need to free() newcmd */
887 int ins_prevcmd;
888 char_u *t;
889 char_u *p;
890 char_u *trailarg;
891 int len;
892 int scroll_save = msg_scroll;
893
894 /*
895 * Disallow shell commands for "rvim".
896 * Disallow shell commands from .exrc and .vimrc in current directory for
897 * security reasons.
898 */
899 if (check_restricted() || check_secure())
900 return;
901
902 if (addr_count == 0) /* :! */
903 {
904 msg_scroll = FALSE; /* don't scroll here */
905 autowrite_all();
906 msg_scroll = scroll_save;
907 }
908
909 /*
910 * Try to find an embedded bang, like in :!<cmd> ! [args]
911 * (:!! is indicated by the 'forceit' variable)
912 */
913 ins_prevcmd = forceit;
914 trailarg = arg;
915 do
916 {
917 len = (int)STRLEN(trailarg) + 1;
918 if (newcmd != NULL)
919 len += (int)STRLEN(newcmd);
920 if (ins_prevcmd)
921 {
922 if (prevcmd == NULL)
923 {
924 EMSG(_(e_noprev));
925 vim_free(newcmd);
926 return;
927 }
928 len += (int)STRLEN(prevcmd);
929 }
930 if ((t = alloc(len)) == NULL)
931 {
932 vim_free(newcmd);
933 return;
934 }
935 *t = NUL;
936 if (newcmd != NULL)
937 STRCAT(t, newcmd);
938 if (ins_prevcmd)
939 STRCAT(t, prevcmd);
940 p = t + STRLEN(t);
941 STRCAT(t, trailarg);
942 vim_free(newcmd);
943 newcmd = t;
944
945 /*
946 * Scan the rest of the argument for '!', which is replaced by the
947 * previous command. "\!" is replaced by "!" (this is vi compatible).
948 */
949 trailarg = NULL;
950 while (*p)
951 {
952 if (*p == '!'
953#ifdef RISCOS
954 && (p[1] == ' ' || p[1] == NUL)
955#endif
956 )
957 {
958 if (p > newcmd && p[-1] == '\\')
959 mch_memmove(p - 1, p, (size_t)(STRLEN(p) + 1));
960 else
961 {
962 trailarg = p;
963 *trailarg++ = NUL;
964 ins_prevcmd = TRUE;
965 break;
966 }
967 }
968 ++p;
969 }
970 } while (trailarg != NULL);
971
972 vim_free(prevcmd);
973 prevcmd = newcmd;
974
975 if (bangredo) /* put cmd in redo buffer for ! command */
976 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000977 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 AppendToRedobuff((char_u *)"\n");
979 bangredo = FALSE;
980 }
981 /*
982 * Add quotes around the command, for shells that need them.
983 */
984 if (*p_shq != NUL)
985 {
986 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
987 if (newcmd == NULL)
988 return;
989 STRCPY(newcmd, p_shq);
990 STRCAT(newcmd, prevcmd);
991 STRCAT(newcmd, p_shq);
992 free_newcmd = TRUE;
993 }
994 if (addr_count == 0) /* :! */
995 {
996 /* echo the command */
997 msg_start();
998 msg_putchar(':');
999 msg_putchar('!');
1000 msg_outtrans(newcmd);
1001 msg_clr_eos();
1002 windgoto(msg_row, msg_col);
1003
1004 do_shell(newcmd, 0);
1005 }
1006 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 /* Careful: This may recursively call do_bang() again! (because of
1009 * autocommands) */
1010 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001011#ifdef FEAT_AUTOCMD
1012 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1013#endif
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (free_newcmd)
1016 vim_free(newcmd);
1017}
1018
1019/*
1020 * do_filter: filter lines through a command given by the user
1021 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022 * We mostly use temp files and the call_shell() routine here. This would
1023 * normally be done using pipes on a UNIX machine, but this is more portable
1024 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 * to deal with redirection somehow, and should handle things like looking
1026 * at the PATH env. variable, and adding reasonable extensions to the
1027 * command name given by the user. All reasonable versions of call_shell()
1028 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 * Alternatively, if on Unix and redirecting input or output, but not both,
1030 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 * We use input redirection if do_in is TRUE.
1032 * We use output redirection if do_out is TRUE.
1033 */
1034 static void
1035do_filter(line1, line2, eap, cmd, do_in, do_out)
1036 linenr_T line1, line2;
1037 exarg_T *eap; /* for forced 'ff' and 'fenc' */
1038 char_u *cmd;
1039 int do_in, do_out;
1040{
1041 char_u *itmp = NULL;
1042 char_u *otmp = NULL;
1043 linenr_T linecount;
1044 linenr_T read_linecount;
1045 pos_T cursor_save;
1046 char_u *cmd_buf;
1047#ifdef FEAT_AUTOCMD
1048 buf_T *old_curbuf = curbuf;
1049#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
1052 if (*cmd == NUL) /* no filter command */
1053 return;
1054
1055#ifdef WIN3264
1056 /*
1057 * Check if external commands are allowed now.
1058 */
1059 if (can_end_termcap_mode(TRUE) == FALSE)
1060 return;
1061#endif
1062
1063 cursor_save = curwin->w_cursor;
1064 linecount = line2 - line1 + 1;
1065 curwin->w_cursor.lnum = line1;
1066 curwin->w_cursor.col = 0;
1067 changed_line_abv_curs();
1068 invalidate_botline();
1069
1070 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 * When using temp files:
1072 * 1. * Form temp file names
1073 * 2. * Write the lines to a temp file
1074 * 3. Run the filter command on the temp file
1075 * 4. * Read the output of the command into the buffer
1076 * 5. * Delete the original lines to be filtered
1077 * 6. * Remove the temp files
1078 *
1079 * When writing the input with a pipe or when catching the output with a
1080 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
1082
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 if (do_out)
1084 shell_flags |= SHELL_DOOUT;
1085
1086#if !defined(USE_SYSTEM) && defined(UNIX)
1087 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1090 shell_flags |= SHELL_READ;
1091 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 else if (do_in && !do_out && !p_stmp)
1094 {
1095 /* Use a pipe to write stdin of the command, do not use a temp file. */
1096 shell_flags |= SHELL_WRITE;
1097 curbuf->b_op_start.lnum = line1;
1098 curbuf->b_op_end.lnum = line2;
1099 }
1100 else if (do_in && do_out && !p_stmp)
1101 {
1102 /* Use a pipe to write stdin and fetch stdout of the command, do not
1103 * use a temp file. */
1104 shell_flags |= SHELL_READ|SHELL_WRITE;
1105 curbuf->b_op_start.lnum = line1;
1106 curbuf->b_op_end.lnum = line2;
1107 curwin->w_cursor.lnum = line2;
1108 }
1109 else
1110#endif
1111 if ((do_in && (itmp = vim_tempname('i')) == NULL)
1112 || (do_out && (otmp = vim_tempname('o')) == NULL))
1113 {
1114 EMSG(_(e_notmp));
1115 goto filterend;
1116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118/*
1119 * The writing and reading of temp files will not be shown.
1120 * Vi also doesn't do this and the messages are not very informative.
1121 */
1122 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 FALSE, FALSE, FALSE, TRUE) == FAIL)
1125 {
1126 msg_putchar('\n'); /* keep message from buf_write() */
1127 --no_wait_return;
1128#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1129 if (!aborting())
1130#endif
1131 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1132 goto filterend;
1133 }
1134#ifdef FEAT_AUTOCMD
1135 if (curbuf != old_curbuf)
1136 goto filterend;
1137#endif
1138
1139 if (!do_out)
1140 msg_putchar('\n');
1141
1142 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1143 if (cmd_buf == NULL)
1144 goto filterend;
1145
1146 windgoto((int)Rows - 1, 0);
1147 cursor_on();
1148
1149 /*
1150 * When not redirecting the output the command can write anything to the
1151 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1152 * stderr output of external command. Clear the screen later.
1153 * If do_in is FALSE, this could be something like ":r !cat", which may
1154 * also mess up the screen, clear it later.
1155 */
1156 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1157 redraw_later_clear();
1158
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 if (do_out)
1160 {
1161 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
1162 goto error;
1163 redraw_curbuf_later(VALID);
1164 }
1165 read_linecount = curbuf->b_ml.ml_line_count;
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 /*
1168 * When call_shell() fails wait_return() is called to give the user a
1169 * chance to read the error messages. Otherwise errors are ignored, so you
1170 * can see the error messages from the command that appear on stdout; use
1171 * 'u' to fix the text
1172 * Switch to cooked mode when not redirecting stdin, avoids that something
1173 * like ":r !cat" hangs.
1174 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1175 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
1178 redraw_later_clear();
1179 wait_return(FALSE);
1180 }
1181 vim_free(cmd_buf);
1182
1183 did_check_timestamps = FALSE;
1184 need_check_timestamps = TRUE;
1185
1186 /* When interrupting the shell command, it may still have produced some
1187 * useful output. Reset got_int here, so that readfile() won't cancel
1188 * reading. */
1189 ui_breakcheck();
1190 got_int = FALSE;
1191
1192 if (do_out)
1193 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1197 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1200 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 {
1203 msg_putchar('\n');
1204 EMSG2(_(e_notread), otmp);
1205 }
1206 goto error;
1207 }
1208#ifdef FEAT_AUTOCMD
1209 if (curbuf != old_curbuf)
1210 goto filterend;
1211#endif
1212 }
1213
1214 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1215
1216 if (shell_flags & SHELL_READ)
1217 {
1218 curbuf->b_op_start.lnum = line2 + 1;
1219 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1220 appended_lines_mark(line2, read_linecount);
1221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
1223 if (do_in)
1224 {
1225 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (read_linecount >= linecount)
1228 /* move all marks from old lines to new lines */
1229 mark_adjust(line1, line2, linecount, 0L);
1230 else
1231 {
1232 /* move marks from old lines to new lines, delete marks
1233 * that are in deleted lines */
1234 mark_adjust(line1, line1 + read_linecount - 1,
1235 linecount, 0L);
1236 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1237 }
1238 }
1239
1240 /*
1241 * Put cursor on first filtered line for ":range!cmd".
1242 * Adjust '[ and '] (set by buf_write()).
1243 */
1244 curwin->w_cursor.lnum = line1;
1245 del_lines(linecount, TRUE);
1246 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1247 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1248 write_lnum_adjust(-linecount); /* adjust last line
1249 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250#ifdef FEAT_FOLDING
1251 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 else
1255 {
1256 /*
1257 * Put cursor on last new line for ":r !cmd".
1258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001260 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1264 --no_wait_return;
1265
1266 if (linecount > p_report)
1267 {
1268 if (do_in)
1269 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001270 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1271 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001274 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 else
1277 msgmore((long)linecount);
1278 }
1279 }
1280 else
1281 {
1282error:
1283 /* put cursor back in same position for ":w !cmd" */
1284 curwin->w_cursor = cursor_save;
1285 --no_wait_return;
1286 wait_return(FALSE);
1287 }
1288
1289filterend:
1290
1291#ifdef FEAT_AUTOCMD
1292 if (curbuf != old_curbuf)
1293 {
1294 --no_wait_return;
1295 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1296 }
1297#endif
1298 if (itmp != NULL)
1299 mch_remove(itmp);
1300 if (otmp != NULL)
1301 mch_remove(otmp);
1302 vim_free(itmp);
1303 vim_free(otmp);
1304}
1305
1306/*
1307 * Call a shell to execute a command.
1308 * When "cmd" is NULL start an interactive shell.
1309 */
1310 void
1311do_shell(cmd, flags)
1312 char_u *cmd;
1313 int flags; /* may be SHELL_DOOUT when output is redirected */
1314{
1315 buf_T *buf;
1316#ifndef FEAT_GUI_MSWIN
1317 int save_nwr;
1318#endif
1319#ifdef MSWIN
1320 int winstart = FALSE;
1321#endif
1322
1323 /*
1324 * Disallow shell commands for "rvim".
1325 * Disallow shell commands from .exrc and .vimrc in current directory for
1326 * security reasons.
1327 */
1328 if (check_restricted() || check_secure())
1329 {
1330 msg_end();
1331 return;
1332 }
1333
1334#ifdef MSWIN
1335 /*
1336 * Check if external commands are allowed now.
1337 */
1338 if (can_end_termcap_mode(TRUE) == FALSE)
1339 return;
1340
1341 /*
1342 * Check if ":!start" is used.
1343 */
1344 if (cmd != NULL)
1345 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1346#endif
1347
1348 /*
1349 * For autocommands we want to get the output on the current screen, to
1350 * avoid having to type return below.
1351 */
1352 msg_putchar('\r'); /* put cursor at start of line */
1353#ifdef FEAT_AUTOCMD
1354 if (!autocmd_busy)
1355#endif
1356 {
1357#ifdef MSWIN
1358 if (!winstart)
1359#endif
1360 stoptermcap();
1361 }
1362#ifdef MSWIN
1363 if (!winstart)
1364#endif
1365 msg_putchar('\n'); /* may shift screen one line up */
1366
1367 /* warning message before calling the shell */
1368 if (p_warn
1369#ifdef FEAT_AUTOCMD
1370 && !autocmd_busy
1371#endif
1372 && msg_silent == 0)
1373 for (buf = firstbuf; buf; buf = buf->b_next)
1374 if (bufIsChanged(buf))
1375 {
1376#ifdef FEAT_GUI_MSWIN
1377 if (!winstart)
1378 starttermcap(); /* don't want a message box here */
1379#endif
1380 MSG_PUTS(_("[No write since last change]\n"));
1381#ifdef FEAT_GUI_MSWIN
1382 if (!winstart)
1383 stoptermcap();
1384#endif
1385 break;
1386 }
1387
1388 /* This windgoto is required for when the '\n' resulted in a "delete line
1389 * 1" command to the terminal. */
1390 if (!swapping_screen())
1391 windgoto(msg_row, msg_col);
1392 cursor_on();
1393 (void)call_shell(cmd, SHELL_COOKED | flags);
1394 did_check_timestamps = FALSE;
1395 need_check_timestamps = TRUE;
1396
1397 /*
1398 * put the message cursor at the end of the screen, avoids wait_return()
1399 * to overwrite the text that the external command showed
1400 */
1401 if (!swapping_screen())
1402 {
1403 msg_row = Rows - 1;
1404 msg_col = 0;
1405 }
1406
1407#ifdef FEAT_AUTOCMD
1408 if (autocmd_busy)
1409 {
1410 if (msg_silent == 0)
1411 redraw_later_clear();
1412 }
1413 else
1414#endif
1415 {
1416 /*
1417 * For ":sh" there is no need to call wait_return(), just redraw.
1418 * Also for the Win32 GUI (the output is in a console window).
1419 * Otherwise there is probably text on the screen that the user wants
1420 * to read before redrawing, so call wait_return().
1421 */
1422#ifndef FEAT_GUI_MSWIN
1423 if (cmd == NULL
1424# ifdef WIN3264
1425 || (winstart && !need_wait_return)
1426# endif
1427 )
1428 {
1429 if (msg_silent == 0)
1430 redraw_later_clear();
1431 need_wait_return = FALSE;
1432 }
1433 else
1434 {
1435 /*
1436 * If we switch screens when starttermcap() is called, we really
1437 * want to wait for "hit return to continue".
1438 */
1439 save_nwr = no_wait_return;
1440 if (swapping_screen())
1441 no_wait_return = FALSE;
1442# ifdef AMIGA
1443 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1444# else
1445 wait_return(msg_silent == 0);
1446# endif
1447 no_wait_return = save_nwr;
1448 }
1449#endif /* FEAT_GUI_W32 */
1450
1451#ifdef MSWIN
1452 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1453#endif
1454 starttermcap(); /* start termcap if not done by wait_return() */
1455
1456 /*
1457 * In an Amiga window redrawing is caused by asking the window size.
1458 * If we got an interrupt this will not work. The chance that the
1459 * window size is wrong is very small, but we need to redraw the
1460 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1461 * but it saves an extra redraw.
1462 */
1463#ifdef AMIGA
1464 if (skip_redraw) /* ':' hit in wait_return() */
1465 {
1466 if (msg_silent == 0)
1467 redraw_later_clear();
1468 }
1469 else if (term_console)
1470 {
1471 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1472 if (got_int && msg_silent == 0)
1473 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1474 else
1475 must_redraw = 0; /* no extra redraw needed */
1476 }
1477#endif
1478 }
1479
1480 /* display any error messages now */
1481 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001482
1483#ifdef FEAT_AUTOCMD
1484 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486}
1487
1488/*
1489 * Create a shell command from a command string, input redirection file and
1490 * output redirection file.
1491 * Returns an allocated string with the shell command, or NULL for failure.
1492 */
1493 char_u *
1494make_filter_cmd(cmd, itmp, otmp)
1495 char_u *cmd; /* command */
1496 char_u *itmp; /* NULL or name of input file */
1497 char_u *otmp; /* NULL or name of output file */
1498{
1499 char_u *buf;
1500 long_u len;
1501
1502 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
1503 if (itmp != NULL)
1504 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1505 if (otmp != NULL)
1506 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1507 buf = lalloc(len, TRUE);
1508 if (buf == NULL)
1509 return NULL;
1510
1511#if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2)
1512 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001513 * Put braces around the command (for concatenated commands) when
1514 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001516 if (itmp != NULL || otmp != NULL)
1517 sprintf((char *)buf, "(%s)", (char *)cmd);
1518 else
1519 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (itmp != NULL)
1521 {
1522 STRCAT(buf, " < ");
1523 STRCAT(buf, itmp);
1524 }
1525#else
1526 /*
1527 * for shells that don't understand braces around commands, at least allow
1528 * the use of commands in a pipe.
1529 */
1530 STRCPY(buf, cmd);
1531 if (itmp != NULL)
1532 {
1533 char_u *p;
1534
1535 /*
1536 * If there is a pipe, we have to put the '<' in front of it.
1537 * Don't do this when 'shellquote' is not empty, otherwise the
1538 * redirection would be inside the quotes.
1539 */
1540 if (*p_shq == NUL)
1541 {
1542 p = vim_strchr(buf, '|');
1543 if (p != NULL)
1544 *p = NUL;
1545 }
1546# ifdef RISCOS
1547 STRCAT(buf, " { < "); /* Use RISC OS notation for input. */
1548 STRCAT(buf, itmp);
1549 STRCAT(buf, " } ");
1550# else
1551 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1552 STRCAT(buf, itmp);
1553# endif
1554 if (*p_shq == NUL)
1555 {
1556 p = vim_strchr(cmd, '|');
1557 if (p != NULL)
1558 {
1559 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1560 STRCAT(buf, p);
1561 }
1562 }
1563 }
1564#endif
1565 if (otmp != NULL)
1566 append_redir(buf, p_srr, otmp);
1567
1568 return buf;
1569}
1570
1571/*
1572 * Append output redirection for file "fname" to the end of string buffer "buf"
1573 * Works with the 'shellredir' and 'shellpipe' options.
1574 * The caller should make sure that there is enough room:
1575 * STRLEN(opt) + STRLEN(fname) + 3
1576 */
1577 void
1578append_redir(buf, opt, fname)
1579 char_u *buf;
1580 char_u *opt;
1581 char_u *fname;
1582{
1583 char_u *p;
1584
1585 buf += STRLEN(buf);
1586 /* find "%s", skipping "%%" */
1587 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
1588 if (p[1] == 's')
1589 break;
1590 if (p != NULL)
1591 {
1592 *buf = ' '; /* not really needed? Not with sh, ksh or bash */
1593 sprintf((char *)buf + 1, (char *)opt, (char *)fname);
1594 }
1595 else
1596 sprintf((char *)buf,
1597#ifdef FEAT_QUICKFIX
1598# ifndef RISCOS
1599 opt != p_sp ? " %s%s" :
1600# endif
1601 " %s %s",
1602#else
1603# ifndef RISCOS
1604 " %s%s", /* " > %s" causes problems on Amiga */
1605# else
1606 " %s %s", /* But is needed for 'shellpipe' and RISC OS */
1607# endif
1608#endif
1609 (char *)opt, (char *)fname);
1610}
1611
1612#ifdef FEAT_VIMINFO
1613
1614static int no_viminfo __ARGS((void));
1615static int viminfo_errcnt;
1616
1617 static int
1618no_viminfo()
1619{
1620 /* "vim -i NONE" does not read or write a viminfo file */
1621 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1622}
1623
1624/*
1625 * Report an error for reading a viminfo file.
1626 * Count the number of errors. When there are more than 10, return TRUE.
1627 */
1628 int
1629viminfo_error(errnum, message, line)
1630 char *errnum;
1631 char *message;
1632 char_u *line;
1633{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001634 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1635 errnum, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff));
Bram Moolenaar758711c2005-02-02 23:11:38 +00001637 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1638 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 emsg(IObuff);
1640 if (++viminfo_errcnt >= 10)
1641 {
1642 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1643 return TRUE;
1644 }
1645 return FALSE;
1646}
1647
1648/*
1649 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
1650 * set are not over-written unless force is TRUE. -- webb
1651 */
1652 int
1653read_viminfo(file, want_info, want_marks, forceit)
1654 char_u *file;
1655 int want_info;
1656 int want_marks;
1657 int forceit;
1658{
1659 FILE *fp;
1660 char_u *fname;
1661
1662 if (no_viminfo())
1663 return FAIL;
1664
1665 fname = viminfo_filename(file); /* may set to default if NULL */
1666 if (fname == NULL)
1667 return FAIL;
1668 fp = mch_fopen((char *)fname, READBIN);
1669
1670 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001671 {
1672 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001673 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1674 fname,
1675 want_info ? _(" info") : "",
1676 want_marks ? _(" marks") : "",
1677 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001678 verbose_leave();
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 vim_free(fname);
1682 if (fp == NULL)
1683 return FAIL;
1684
1685 viminfo_errcnt = 0;
1686 do_viminfo(fp, NULL, want_info, want_marks, forceit);
1687
1688 fclose(fp);
1689
1690 return OK;
1691}
1692
1693/*
1694 * write_viminfo() -- Write the viminfo file. The old one is read in first so
1695 * that effectively a merge of current info and old info is done. This allows
1696 * multiple vims to run simultaneously, without losing any marks etc. If
1697 * forceit is TRUE, then the old file is not read in, and only internal info is
1698 * written to the file. -- webb
1699 */
1700 void
1701write_viminfo(file, forceit)
1702 char_u *file;
1703 int forceit;
1704{
1705 char_u *fname;
1706 FILE *fp_in = NULL; /* input viminfo file, if any */
1707 FILE *fp_out = NULL; /* output viminfo file */
1708 char_u *tempname = NULL; /* name of temp viminfo file */
1709 struct stat st_new; /* mch_stat() of potential new file */
1710 char_u *wp;
1711#if defined(UNIX) || defined(VMS)
1712 mode_t umask_save;
1713#endif
1714#ifdef UNIX
1715 int shortname = FALSE; /* use 8.3 file name */
1716 struct stat st_old; /* mch_stat() of existing viminfo file */
1717#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001718#ifdef WIN3264
1719 long perm = -1;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 if (no_viminfo())
1723 return;
1724
1725 fname = viminfo_filename(file); /* may set to default if NULL */
1726 if (fname == NULL)
1727 return;
1728
1729 fp_in = mch_fopen((char *)fname, READBIN);
1730 if (fp_in == NULL)
1731 {
1732 /* if it does exist, but we can't read it, don't try writing */
1733 if (mch_stat((char *)fname, &st_new) == 0)
1734 goto end;
1735#if defined(UNIX) || defined(VMS)
1736 /*
1737 * For Unix we create the .viminfo non-accessible for others,
1738 * because it may contain text from non-accessible documents.
1739 */
1740 umask_save = umask(077);
1741#endif
1742 fp_out = mch_fopen((char *)fname, WRITEBIN);
1743#if defined(UNIX) || defined(VMS)
1744 (void)umask(umask_save);
1745#endif
1746 }
1747 else
1748 {
1749 /*
1750 * There is an existing viminfo file. Create a temporary file to
1751 * write the new viminfo into, in the same directory as the
1752 * existing viminfo file, which will be renamed later.
1753 */
1754#ifdef UNIX
1755 /*
1756 * For Unix we check the owner of the file. It's not very nice to
1757 * overwrite a user's viminfo file after a "su root", with a
1758 * viminfo file that the user can't read.
1759 */
1760 st_old.st_dev = st_old.st_ino = 0;
1761 st_old.st_mode = 0600;
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001762 if (mch_stat((char *)fname, &st_old) == 0 && getuid()
1763 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 ? (st_old.st_mode & 0200)
1765 : (st_old.st_gid == getgid()
1766 ? (st_old.st_mode & 0020)
1767 : (st_old.st_mode & 0002))))
1768 {
1769 int tt;
1770
1771 /* avoid a wait_return for this message, it's annoying */
1772 tt = msg_didany;
1773 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1774 msg_didany = tt;
1775 goto end;
1776 }
1777#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001778#ifdef WIN3264
1779 /* Get the file attributes of the existing viminfo file. */
1780 perm = mch_getperm(fname);
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 /*
1784 * Make tempname.
1785 * May try twice: Once normal and once with shortname set, just in
1786 * case somebody puts his viminfo file in an 8.3 filesystem.
1787 */
1788 for (;;)
1789 {
1790 tempname = buf_modname(
1791#ifdef UNIX
1792 shortname,
1793#else
1794# ifdef SHORT_FNAME
1795 TRUE,
1796# else
1797# ifdef FEAT_GUI_W32
1798 gui_is_win32s(),
1799# else
1800 FALSE,
1801# endif
1802# endif
1803#endif
1804 fname,
1805#ifdef VMS
1806 (char_u *)"-tmp",
1807#else
1808# ifdef RISCOS
1809 (char_u *)"/tmp",
1810# else
1811 (char_u *)".tmp",
1812# endif
1813#endif
1814 FALSE);
1815 if (tempname == NULL) /* out of memory */
1816 break;
1817
1818 /*
1819 * Check if tempfile already exists. Never overwrite an
1820 * existing file!
1821 */
1822 if (mch_stat((char *)tempname, &st_new) == 0)
1823 {
1824#ifdef UNIX
1825 /*
1826 * Check if tempfile is same as original file. May happen
1827 * when modname() gave the same file back. E.g. silly
1828 * link, or file name-length reached. Try again with
1829 * shortname set.
1830 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001831 if (!shortname && st_new.st_dev == st_old.st_dev
1832 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 vim_free(tempname);
1835 tempname = NULL;
1836 shortname = TRUE;
1837 continue;
1838 }
1839#endif
1840 /*
1841 * Try another name. Change one character, just before
1842 * the extension. This should also work for an 8.3
1843 * file name, when after adding the extension it still is
1844 * the same file as the original.
1845 */
1846 wp = tempname + STRLEN(tempname) - 5;
1847 if (wp < gettail(tempname)) /* empty file name? */
1848 wp = gettail(tempname);
1849 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1850 --*wp)
1851 {
1852 /*
1853 * They all exist? Must be something wrong! Don't
1854 * write the viminfo file then.
1855 */
1856 if (*wp == 'a')
1857 {
1858 vim_free(tempname);
1859 tempname = NULL;
1860 break;
1861 }
1862 }
1863 }
1864 break;
1865 }
1866
1867 if (tempname != NULL)
1868 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00001869#ifdef VMS
1870 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00001871 umask_save = umask(077);
1872 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1873 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001874#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00001875 int fd;
1876
1877 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001878 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001879 * Unix: same as original file, but strip s-bit. Reset umask to
1880 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001881 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00001882# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001883 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00001884 fd = mch_open((char *)tempname,
1885 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001886 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001887 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001888# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001889 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001890 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001891# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00001892 if (fd < 0)
1893 fp_out = NULL;
1894 else
1895 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001896#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898 /*
1899 * If we can't create in the same directory, try creating a
1900 * "normal" temp file.
1901 */
1902 if (fp_out == NULL)
1903 {
1904 vim_free(tempname);
1905 if ((tempname = vim_tempname('o')) != NULL)
1906 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1907 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00001908
1909#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00001911 * Make sure the owner can read/write it. This only works for
1912 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 */
1914 if (fp_out != NULL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001915 (void)fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916#endif
1917 }
1918 }
1919
1920 /*
1921 * Check if the new viminfo file can be written to.
1922 */
1923 if (fp_out == NULL)
1924 {
1925 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001926 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (fp_in != NULL)
1928 fclose(fp_in);
1929 goto end;
1930 }
1931
1932 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001933 {
1934 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001935 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001936 verbose_leave();
1937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 viminfo_errcnt = 0;
1940 do_viminfo(fp_in, fp_out, !forceit, !forceit, FALSE);
1941
1942 fclose(fp_out); /* errors are ignored !? */
1943 if (fp_in != NULL)
1944 {
1945 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 /*
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001948 * In case of an error keep the original viminfo file.
1949 * Otherwise rename the newly written file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 */
1951 if (viminfo_errcnt || vim_rename(tempname, fname) == -1)
1952 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001953
1954#ifdef WIN3264
1955 /* If the viminfo file was hidden then also hide the new file. */
1956 if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN))
1957 mch_hide(fname);
1958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961end:
1962 vim_free(fname);
1963 vim_free(tempname);
1964}
1965
1966/*
1967 * Get the viminfo file name to use.
1968 * If "file" is given and not empty, use it (has already been expanded by
1969 * cmdline functions).
1970 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
1971 * expand environment variables.
1972 * Returns an allocated string. NULL when out of memory.
1973 */
1974 static char_u *
1975viminfo_filename(file)
1976 char_u *file;
1977{
1978 if (file == NULL || *file == NUL)
1979 {
1980 if (use_viminfo != NULL)
1981 file = use_viminfo;
1982 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
1983 {
1984#ifdef VIMINFO_FILE2
1985 /* don't use $HOME when not defined (turned into "c:/"!). */
1986# ifdef VMS
1987 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
1988# else
1989 if (mch_getenv((char_u *)"HOME") == NULL)
1990# endif
1991 {
1992 /* don't use $VIM when not available. */
1993 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
1994 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
1995 file = (char_u *)VIMINFO_FILE2;
1996 else
1997 file = (char_u *)VIMINFO_FILE;
1998 }
1999 else
2000#endif
2001 file = (char_u *)VIMINFO_FILE;
2002 }
2003 expand_env(file, NameBuff, MAXPATHL);
2004 file = NameBuff;
2005 }
2006 return vim_strsave(file);
2007}
2008
2009/*
2010 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2011 */
2012 static void
2013do_viminfo(fp_in, fp_out, want_info, want_marks, force_read)
2014 FILE *fp_in;
2015 FILE *fp_out;
2016 int want_info;
2017 int want_marks;
2018 int force_read;
2019{
2020 int count = 0;
2021 int eof = FALSE;
2022 vir_T vir;
2023
2024 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2025 return;
2026 vir.vir_fd = fp_in;
2027#ifdef FEAT_MBYTE
2028 vir.vir_conv.vc_type = CONV_NONE;
2029#endif
2030
2031 if (fp_in != NULL)
2032 {
2033 if (want_info)
2034 eof = read_viminfo_up_to_marks(&vir, force_read, fp_out != NULL);
2035 else
2036 /* Skip info, find start of marks */
2037 while (!(eof = viminfo_readline(&vir))
2038 && vir.vir_line[0] != '>')
2039 ;
2040 }
2041 if (fp_out != NULL)
2042 {
2043 /* Write the info: */
2044 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2045 VIM_VERSION_MEDIUM);
2046 fprintf(fp_out, _("# You may edit it if you're careful!\n\n"));
2047#ifdef FEAT_MBYTE
2048 fprintf(fp_out, _("# Value of 'encoding' when this file was written\n"));
2049 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2050#endif
2051 write_viminfo_search_pattern(fp_out);
2052 write_viminfo_sub_string(fp_out);
2053#ifdef FEAT_CMDHIST
2054 write_viminfo_history(fp_out);
2055#endif
2056 write_viminfo_registers(fp_out);
2057#ifdef FEAT_EVAL
2058 write_viminfo_varlist(fp_out);
2059#endif
2060 write_viminfo_filemarks(fp_out);
2061 write_viminfo_bufferlist(fp_out);
2062 count = write_viminfo_marks(fp_out);
2063 }
2064 if (fp_in != NULL && want_marks)
2065 copy_viminfo_marks(&vir, fp_out, count, eof);
2066
2067 vim_free(vir.vir_line);
2068#ifdef FEAT_MBYTE
2069 if (vir.vir_conv.vc_type != CONV_NONE)
2070 convert_setup(&vir.vir_conv, NULL, NULL);
2071#endif
2072}
2073
2074/*
2075 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2076 * first part of the viminfo file which contains everything but the marks that
2077 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2078 */
2079 static int
2080read_viminfo_up_to_marks(virp, forceit, writing)
2081 vir_T *virp;
2082 int forceit;
2083 int writing;
2084{
2085 int eof;
2086 buf_T *buf;
2087
2088#ifdef FEAT_CMDHIST
2089 prepare_viminfo_history(forceit ? 9999 : 0);
2090#endif
2091 eof = viminfo_readline(virp);
2092 while (!eof && virp->vir_line[0] != '>')
2093 {
2094 switch (virp->vir_line[0])
2095 {
2096 /* Characters reserved for future expansion, ignored now */
2097 case '+': /* "+40 /path/dir file", for running vim without args */
2098 case '|': /* to be defined */
2099 case '^': /* to be defined */
2100 case '<': /* long line - ignored */
2101 /* A comment or empty line. */
2102 case NUL:
2103 case '\r':
2104 case '\n':
2105 case '#':
2106 eof = viminfo_readline(virp);
2107 break;
2108 case '*': /* "*encoding=value" */
2109 eof = viminfo_encoding(virp);
2110 break;
2111 case '!': /* global variable */
2112#ifdef FEAT_EVAL
2113 eof = read_viminfo_varlist(virp, writing);
2114#else
2115 eof = viminfo_readline(virp);
2116#endif
2117 break;
2118 case '%': /* entry for buffer list */
2119 eof = read_viminfo_bufferlist(virp, writing);
2120 break;
2121 case '"':
2122 eof = read_viminfo_register(virp, forceit);
2123 break;
2124 case '/': /* Search string */
2125 case '&': /* Substitute search string */
2126 case '~': /* Last search string, followed by '/' or '&' */
2127 eof = read_viminfo_search_pattern(virp, forceit);
2128 break;
2129 case '$':
2130 eof = read_viminfo_sub_string(virp, forceit);
2131 break;
2132 case ':':
2133 case '?':
2134 case '=':
2135 case '@':
2136#ifdef FEAT_CMDHIST
2137 eof = read_viminfo_history(virp);
2138#else
2139 eof = viminfo_readline(virp);
2140#endif
2141 break;
2142 case '-':
2143 case '\'':
2144 eof = read_viminfo_filemark(virp, forceit);
2145 break;
2146 default:
2147 if (viminfo_error("E575: ", _("Illegal starting char"),
2148 virp->vir_line))
2149 eof = TRUE;
2150 else
2151 eof = viminfo_readline(virp);
2152 break;
2153 }
2154 }
2155
2156#ifdef FEAT_CMDHIST
2157 /* Finish reading history items. */
2158 finish_viminfo_history();
2159#endif
2160
2161 /* Change file names to buffer numbers for fmarks. */
2162 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2163 fmarks_check_names(buf);
2164
2165 return eof;
2166}
2167
2168/*
2169 * Compare the 'encoding' value in the viminfo file with the current value of
2170 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2171 * conversion of text with iconv() in viminfo_readstring().
2172 */
2173 static int
2174viminfo_encoding(virp)
2175 vir_T *virp;
2176{
2177#ifdef FEAT_MBYTE
2178 char_u *p;
2179 int i;
2180
2181 if (get_viminfo_parameter('c') != 0)
2182 {
2183 p = vim_strchr(virp->vir_line, '=');
2184 if (p != NULL)
2185 {
2186 /* remove trailing newline */
2187 ++p;
2188 for (i = 0; vim_isprintc(p[i]); ++i)
2189 ;
2190 p[i] = NUL;
2191
2192 convert_setup(&virp->vir_conv, p, p_enc);
2193 }
2194 }
2195#endif
2196 return viminfo_readline(virp);
2197}
2198
2199/*
2200 * Read a line from the viminfo file.
2201 * Returns TRUE for end-of-file;
2202 */
2203 int
2204viminfo_readline(virp)
2205 vir_T *virp;
2206{
2207 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2208}
2209
2210/*
2211 * check string read from viminfo file
2212 * remove '\n' at the end of the line
2213 * - replace CTRL-V CTRL-V with CTRL-V
2214 * - replace CTRL-V 'n' with '\n'
2215 *
2216 * Check for a long line as written by viminfo_writestring().
2217 *
2218 * Return the string in allocated memory (NULL when out of memory).
2219 */
2220/*ARGSUSED*/
2221 char_u *
2222viminfo_readstring(virp, off, convert)
2223 vir_T *virp;
2224 int off; /* offset for virp->vir_line */
2225 int convert; /* convert the string */
2226{
2227 char_u *retval;
2228 char_u *s, *d;
2229 long len;
2230
2231 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2232 {
2233 len = atol((char *)virp->vir_line + off + 1);
2234 retval = lalloc(len, TRUE);
2235 if (retval == NULL)
2236 {
2237 /* Line too long? File messed up? Skip next line. */
2238 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2239 return NULL;
2240 }
2241 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2242 s = retval + 1; /* Skip the leading '<' */
2243 }
2244 else
2245 {
2246 retval = vim_strsave(virp->vir_line + off);
2247 if (retval == NULL)
2248 return NULL;
2249 s = retval;
2250 }
2251
2252 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2253 d = retval;
2254 while (*s != NUL && *s != '\n')
2255 {
2256 if (s[0] == Ctrl_V && s[1] != NUL)
2257 {
2258 if (s[1] == 'n')
2259 *d++ = '\n';
2260 else
2261 *d++ = Ctrl_V;
2262 s += 2;
2263 }
2264 else
2265 *d++ = *s++;
2266 }
2267 *d = NUL;
2268
2269#ifdef FEAT_MBYTE
2270 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2271 {
2272 d = string_convert(&virp->vir_conv, retval, NULL);
2273 if (d != NULL)
2274 {
2275 vim_free(retval);
2276 retval = d;
2277 }
2278 }
2279#endif
2280
2281 return retval;
2282}
2283
2284/*
2285 * write string to viminfo file
2286 * - replace CTRL-V with CTRL-V CTRL-V
2287 * - replace '\n' with CTRL-V 'n'
2288 * - add a '\n' at the end
2289 *
2290 * For a long line:
2291 * - write " CTRL-V <length> \n " in first line
2292 * - write " < <string> \n " in second line
2293 */
2294 void
2295viminfo_writestring(fd, p)
2296 FILE *fd;
2297 char_u *p;
2298{
2299 int c;
2300 char_u *s;
2301 int len = 0;
2302
2303 for (s = p; *s != NUL; ++s)
2304 {
2305 if (*s == Ctrl_V || *s == '\n')
2306 ++len;
2307 ++len;
2308 }
2309
2310 /* If the string will be too long, write its length and put it in the next
2311 * line. Take into account that some room is needed for what comes before
2312 * the string (e.g., variable name). Add something to the length for the
2313 * '<', NL and trailing NUL. */
2314 if (len > LSIZE / 2)
2315 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2316
2317 while ((c = *p++) != NUL)
2318 {
2319 if (c == Ctrl_V || c == '\n')
2320 {
2321 putc(Ctrl_V, fd);
2322 if (c == '\n')
2323 c = 'n';
2324 }
2325 putc(c, fd);
2326 }
2327 putc('\n', fd);
2328}
2329#endif /* FEAT_VIMINFO */
2330
2331/*
2332 * Implementation of ":fixdel", also used by get_stty().
2333 * <BS> resulting <Del>
2334 * ^? ^H
2335 * not ^? ^?
2336 */
2337/*ARGSUSED*/
2338 void
2339do_fixdel(eap)
2340 exarg_T *eap;
2341{
2342 char_u *p;
2343
2344 p = find_termcode((char_u *)"kb");
2345 add_termcode((char_u *)"kD", p != NULL
2346 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2347}
2348
2349 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002350print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 linenr_T lnum;
2352 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002353 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002355 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 if (curwin->w_p_nu || use_number)
2358 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002359 sprintf((char *)numbuf, "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2361 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002362 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363}
2364
2365/*
2366 * Print a text line. Also in silent mode ("ex -s").
2367 */
2368 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002369print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 linenr_T lnum;
2371 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002372 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 int save_silent = silent_mode;
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002377 silent_mode = FALSE;
2378 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2379 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (save_silent)
2381 {
2382 msg_putchar('\n');
2383 cursor_on(); /* msg_start() switches it off */
2384 out_flush();
2385 silent_mode = save_silent;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002386 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388}
2389
2390/*
2391 * ":file[!] [fname]".
2392 */
2393 void
2394ex_file(eap)
2395 exarg_T *eap;
2396{
2397 char_u *fname, *sfname, *xfname;
2398 buf_T *buf;
2399
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002400 /* ":0file" removes the file name. Check for illegal uses ":3file",
2401 * "0file name", etc. */
2402 if (eap->addr_count > 0
2403 && (*eap->arg != NUL
2404 || eap->line2 > 0
2405 || eap->addr_count > 1))
2406 {
2407 EMSG(_(e_invarg));
2408 return;
2409 }
2410
2411 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
2413#ifdef FEAT_AUTOCMD
2414 buf = curbuf;
2415 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
2416 /* buffer changed, don't change name now */
2417 if (buf != curbuf)
2418 return;
2419# ifdef FEAT_EVAL
2420 if (aborting()) /* autocmds may abort script processing */
2421 return;
2422# endif
2423#endif
2424 /*
2425 * The name of the current buffer will be changed.
2426 * A new (unlisted) buffer entry needs to be made to hold the old file
2427 * name, which will become the alternate file name.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002428 * But don't set the alternate file name if the buffer didn't have a
2429 * name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
2431 fname = curbuf->b_ffname;
2432 sfname = curbuf->b_sfname;
2433 xfname = curbuf->b_fname;
2434 curbuf->b_ffname = NULL;
2435 curbuf->b_sfname = NULL;
2436 if (setfname(curbuf, eap->arg, NULL, TRUE) == FAIL)
2437 {
2438 curbuf->b_ffname = fname;
2439 curbuf->b_sfname = sfname;
2440 return;
2441 }
2442 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002443 if (xfname != NULL && *xfname != NUL)
2444 {
2445 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2446 if (buf != NULL && !cmdmod.keepalt)
2447 curwin->w_alt_fnum = buf->b_fnum;
2448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 vim_free(fname);
2450 vim_free(sfname);
2451#ifdef FEAT_AUTOCMD
2452 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
2453#endif
2454 }
2455 /* print full file name if :cd used */
2456 fileinfo(FALSE, FALSE, eap->forceit);
2457}
2458
2459/*
2460 * ":update".
2461 */
2462 void
2463ex_update(eap)
2464 exarg_T *eap;
2465{
2466 if (curbufIsChanged())
2467 (void)do_write(eap);
2468}
2469
2470/*
2471 * ":write" and ":saveas".
2472 */
2473 void
2474ex_write(eap)
2475 exarg_T *eap;
2476{
2477 if (eap->usefilter) /* input lines to shell command */
2478 do_bang(1, eap, FALSE, TRUE, FALSE);
2479 else
2480 (void)do_write(eap);
2481}
2482
2483/*
2484 * write current buffer to file 'eap->arg'
2485 * if 'eap->append' is TRUE, append to the file
2486 *
2487 * if *eap->arg == NUL write to current file
2488 *
2489 * return FAIL for failure, OK otherwise
2490 */
2491 int
2492do_write(eap)
2493 exarg_T *eap;
2494{
2495 int other;
2496 char_u *fname = NULL; /* init to shut up gcc */
2497 char_u *ffname;
2498 int retval = FAIL;
2499 char_u *free_fname = NULL;
2500#ifdef FEAT_BROWSE
2501 char_u *browse_file = NULL;
2502#endif
2503 buf_T *alt_buf = NULL;
2504
2505 if (not_writing()) /* check 'write' option */
2506 return FAIL;
2507
2508 ffname = eap->arg;
2509#ifdef FEAT_BROWSE
2510 if (cmdmod.browse)
2511 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002512 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 NULL, NULL, NULL, curbuf);
2514 if (browse_file == NULL)
2515 goto theend;
2516 ffname = browse_file;
2517 }
2518#endif
2519 if (*ffname == NUL)
2520 {
2521 if (eap->cmdidx == CMD_saveas)
2522 {
2523 EMSG(_(e_argreq));
2524 goto theend;
2525 }
2526 other = FALSE;
2527 }
2528 else
2529 {
2530 fname = ffname;
2531 free_fname = fix_fname(ffname);
2532 /*
2533 * When out-of-memory, keep unexpanded file name, because we MUST be
2534 * able to write the file in this situation.
2535 */
2536 if (free_fname != NULL)
2537 ffname = free_fname;
2538 other = otherfile(ffname);
2539 }
2540
2541 /*
2542 * If we have a new file, put its name in the list of alternate file names.
2543 */
2544 if (other)
2545 {
2546 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2547 || eap->cmdidx == CMD_saveas)
2548 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2549 else
2550 alt_buf = buflist_findname(ffname);
2551 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2552 {
2553 /* Overwriting a file that is loaded in another buffer is not a
2554 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002555 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 goto theend;
2557 }
2558 }
2559
2560 /*
2561 * Writing to the current file is not allowed in readonly mode
2562 * and a file name is required.
2563 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2564 */
2565 if (!other && (
2566#ifdef FEAT_QUICKFIX
2567 bt_dontwrite_msg(curbuf) ||
2568#endif
2569 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2570 goto theend;
2571
2572 if (!other)
2573 {
2574 ffname = curbuf->b_ffname;
2575 fname = curbuf->b_fname;
2576 /*
2577 * Not writing the whole file is only allowed with '!'.
2578 */
2579 if ( (eap->line1 != 1
2580 || eap->line2 != curbuf->b_ml.ml_line_count)
2581 && !eap->forceit
2582 && !eap->append
2583 && !p_wa)
2584 {
2585#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2586 if (p_confirm || cmdmod.confirm)
2587 {
2588 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2589 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2590 goto theend;
2591 eap->forceit = TRUE;
2592 }
2593 else
2594#endif
2595 {
2596 EMSG(_("E140: Use ! to write partial buffer"));
2597 goto theend;
2598 }
2599 }
2600 }
2601
2602 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2603 {
2604 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2605 {
2606#ifdef FEAT_AUTOCMD
2607 buf_T *was_curbuf = curbuf;
2608
2609 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002610 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611# ifdef FEAT_EVAL
2612 if (curbuf != was_curbuf || aborting())
2613# else
2614 if (curbuf != was_curbuf)
2615# endif
2616 {
2617 /* buffer changed, don't change name now */
2618 retval = FAIL;
2619 goto theend;
2620 }
2621#endif
2622 /* Exchange the file names for the current and the alternate
2623 * buffer. This makes it look like we are now editing the buffer
2624 * under the new name. Must be done before buf_write(), because
2625 * if there is no file name and 'cpo' contains 'F', it will set
2626 * the file name. */
2627 fname = alt_buf->b_fname;
2628 alt_buf->b_fname = curbuf->b_fname;
2629 curbuf->b_fname = fname;
2630 fname = alt_buf->b_ffname;
2631 alt_buf->b_ffname = curbuf->b_ffname;
2632 curbuf->b_ffname = fname;
2633 fname = alt_buf->b_sfname;
2634 alt_buf->b_sfname = curbuf->b_sfname;
2635 curbuf->b_sfname = fname;
2636 buf_name_changed(curbuf);
2637#ifdef FEAT_AUTOCMD
2638 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002639 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (!alt_buf->b_p_bl)
2641 {
2642 alt_buf->b_p_bl = TRUE;
2643 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2644 }
2645# ifdef FEAT_EVAL
2646 if (curbuf != was_curbuf || aborting())
2647# else
2648 if (curbuf != was_curbuf)
2649# endif
2650 {
2651 /* buffer changed, don't write the file */
2652 retval = FAIL;
2653 goto theend;
2654 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002655
2656 /* If 'filetype' was empty try detecting it now. */
2657 if (*curbuf->b_p_ft == NUL)
2658 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002659 if (au_has_group((char_u *)"filetypedetect"))
2660 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
2661 TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002662 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665 }
2666
2667 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2668 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002669
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002670 /* After ":saveas fname" reset 'readonly'. */
2671 if (eap->cmdidx == CMD_saveas && retval == OK)
2672 curbuf->b_p_ro = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675theend:
2676#ifdef FEAT_BROWSE
2677 vim_free(browse_file);
2678#endif
2679 vim_free(free_fname);
2680 return retval;
2681}
2682
2683/*
2684 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2685 * BF_NEW or BF_READERR, check for overwriting current file.
2686 * May set eap->forceit if a dialog says it's OK to overwrite.
2687 * Return OK if it's OK, FAIL if it is not.
2688 */
2689/*ARGSUSED*/
2690 static int
2691check_overwrite(eap, buf, fname, ffname, other)
2692 exarg_T *eap;
2693 buf_T *buf;
2694 char_u *fname; /* file name to be used (can differ from
2695 buf->ffname) */
2696 char_u *ffname; /* full path version of fname */
2697 int other; /* writing under other name */
2698{
2699 /*
2700 * write to other file or b_flags set or not writing the whole file:
2701 * overwriting only allowed with '!'
2702 */
2703 if ( (other
2704 || (buf->b_flags & BF_NOTEDITED)
2705 || ((buf->b_flags & BF_NEW)
2706 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2707 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 && !p_wa
2709 && vim_fexists(ffname))
2710 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002711 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002713#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002714 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002715 if (mch_isdir(ffname))
2716 {
2717 EMSG2(_(e_isadir2), ffname);
2718 return FAIL;
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720#endif
2721#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002722 if (p_confirm || cmdmod.confirm)
2723 {
2724 char_u buff[IOSIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002726 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2727 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2728 return FAIL;
2729 eap->forceit = TRUE;
2730 }
2731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002733 {
2734 EMSG(_(e_exists));
2735 return FAIL;
2736 }
2737 }
2738
2739 /* For ":w! filename" check that no swap file exists for "filename". */
2740 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002742 char_u dir[MAXPATHL];
2743 char_u *p;
2744 int r;
2745 char_u *swapname;
2746
2747 /* We only try the first entry in 'directory', without checking if
2748 * it's writable. If the "." directory is not writable the write
2749 * will probably fail anyway.
2750 * Use 'shortname' of the current buffer, since there is no buffer
2751 * for the written file. */
2752 if (*p_dir == NUL)
2753 STRCPY(dir, ".");
2754 else
2755 {
2756 p = p_dir;
2757 copy_option_part(&p, dir, MAXPATHL, ",");
2758 }
2759 swapname = makeswapname(fname, ffname, curbuf, dir);
2760 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002761 if (r)
2762 {
2763#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2764 if (p_confirm || cmdmod.confirm)
2765 {
2766 char_u buff[IOSIZE];
2767
2768 dialog_msg(buff,
2769 _("Swap file \"%s\" exists, overwrite anyway?"),
2770 swapname);
2771 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2772 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002773 {
2774 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002775 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002776 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002777 eap->forceit = TRUE;
2778 }
2779 else
2780#endif
2781 {
2782 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2783 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002784 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002785 return FAIL;
2786 }
2787 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002788 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 }
2791 return OK;
2792}
2793
2794/*
2795 * Handle ":wnext", ":wNext" and ":wprevious" commands.
2796 */
2797 void
2798ex_wnext(eap)
2799 exarg_T *eap;
2800{
2801 int i;
2802
2803 if (eap->cmd[1] == 'n')
2804 i = curwin->w_arg_idx + (int)eap->line2;
2805 else
2806 i = curwin->w_arg_idx - (int)eap->line2;
2807 eap->line1 = 1;
2808 eap->line2 = curbuf->b_ml.ml_line_count;
2809 if (do_write(eap) != FAIL)
2810 do_argfile(eap, i);
2811}
2812
2813/*
2814 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
2815 */
2816 void
2817do_wqall(eap)
2818 exarg_T *eap;
2819{
2820 buf_T *buf;
2821 int error = 0;
2822 int save_forceit = eap->forceit;
2823
2824 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
2825 exiting = TRUE;
2826
2827 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2828 {
2829 if (bufIsChanged(buf))
2830 {
2831 /*
2832 * Check if there is a reason the buffer cannot be written:
2833 * 1. if the 'write' option is set
2834 * 2. if there is no file name (even after browsing)
2835 * 3. if the 'readonly' is set (even after a dialog)
2836 * 4. if overwriting is allowed (even after a dialog)
2837 */
2838 if (not_writing())
2839 {
2840 ++error;
2841 break;
2842 }
2843#ifdef FEAT_BROWSE
2844 /* ":browse wall": ask for file name if there isn't one */
2845 if (buf->b_ffname == NULL && cmdmod.browse)
2846 browse_save_fname(buf);
2847#endif
2848 if (buf->b_ffname == NULL)
2849 {
2850 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
2851 ++error;
2852 }
2853 else if (check_readonly(&eap->forceit, buf)
2854 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
2855 FALSE) == FAIL)
2856 {
2857 ++error;
2858 }
2859 else
2860 {
2861 if (buf_write_all(buf, eap->forceit) == FAIL)
2862 ++error;
2863#ifdef FEAT_AUTOCMD
2864 /* an autocommand may have deleted the buffer */
2865 if (!buf_valid(buf))
2866 buf = firstbuf;
2867#endif
2868 }
2869 eap->forceit = save_forceit; /* check_overwrite() may set it */
2870 }
2871 }
2872 if (exiting)
2873 {
2874 if (!error)
2875 getout(0); /* exit Vim */
2876 not_exiting();
2877 }
2878}
2879
2880/*
2881 * Check the 'write' option.
2882 * Return TRUE and give a message when it's not st.
2883 */
2884 int
2885not_writing()
2886{
2887 if (p_write)
2888 return FALSE;
2889 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
2890 return TRUE;
2891}
2892
2893/*
2894 * Check if a buffer is read-only. Ask for overruling in a dialog.
2895 * Return TRUE and give an error message when the buffer is readonly.
2896 */
2897 static int
2898check_readonly(forceit, buf)
2899 int *forceit;
2900 buf_T *buf;
2901{
2902 if (!*forceit && buf->b_p_ro)
2903 {
2904#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2905 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
2906 {
2907 char_u buff[IOSIZE];
2908
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002909 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 buf->b_fname);
2911
2912 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
2913 {
2914 /* Set forceit, to force the writing of a readonly file */
2915 *forceit = TRUE;
2916 return FALSE;
2917 }
2918 else
2919 return TRUE;
2920 }
2921 else
2922#endif
2923 EMSG(_(e_readonly));
2924 return TRUE;
2925 }
2926 return FALSE;
2927}
2928
2929/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002930 * Try to abandon current file and edit a new or existing file.
2931 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002933 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
2934 * -1 for succesfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 * 'lnum' is the line number for the cursor in the new file (if non-zero).
2936 */
2937 int
2938getfile(fnum, ffname, sfname, setpm, lnum, forceit)
2939 int fnum;
2940 char_u *ffname;
2941 char_u *sfname;
2942 int setpm;
2943 linenr_T lnum;
2944 int forceit;
2945{
2946 int other;
2947 int retval;
2948 char_u *free_me = NULL;
2949
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002950 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002952#ifdef FEAT_AUTOCMD
2953 if (curbuf_locked())
2954 return 1;
2955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 if (fnum == 0)
2958 {
2959 /* make ffname full path, set sfname */
2960 fname_expand(curbuf, &ffname, &sfname);
2961 other = otherfile(ffname);
2962 free_me = ffname; /* has been allocated, free() later */
2963 }
2964 else
2965 other = (fnum != curbuf->b_fnum);
2966
2967 if (other)
2968 ++no_wait_return; /* don't wait for autowrite message */
2969 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
2970 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
2971 {
2972#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2973 if (p_confirm && p_write)
2974 dialog_changed(curbuf, FALSE);
2975 if (curbufIsChanged())
2976#endif
2977 {
2978 if (other)
2979 --no_wait_return;
2980 EMSG(_(e_nowrtmsg));
2981 retval = 2; /* file has been changed */
2982 goto theend;
2983 }
2984 }
2985 if (other)
2986 --no_wait_return;
2987 if (setpm)
2988 setpcmark();
2989 if (!other)
2990 {
2991 if (lnum != 0)
2992 curwin->w_cursor.lnum = lnum;
2993 check_cursor_lnum();
2994 beginline(BL_SOL | BL_FIX);
2995 retval = 0; /* it's in the same file */
2996 }
2997 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
2998 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0)) == OK)
2999 retval = -1; /* opened another file */
3000 else
3001 retval = 1; /* error encountered */
3002
3003theend:
3004 vim_free(free_me);
3005 return retval;
3006}
3007
3008/*
3009 * start editing a new file
3010 *
3011 * fnum: file number; if zero use ffname/sfname
3012 * ffname: the file name
3013 * - full path if sfname used,
3014 * - any file name if sfname is NULL
3015 * - empty string to re-edit with the same file name (but may be
3016 * in a different directory)
3017 * - NULL to start an empty buffer
3018 * sfname: the short file name (or NULL)
3019 * eap: contains the command to be executed after loading the file and
3020 * forced 'ff' and 'fenc'
3021 * newlnum: if > 0: put cursor on this line number (if possible)
3022 * if ECMD_LASTL: use last position in loaded file
3023 * if ECMD_LAST: use last position in all files
3024 * if ECMD_ONE: use first line
3025 * flags:
3026 * ECMD_HIDE: if TRUE don't free the current buffer
3027 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3028 * ECMD_OLDBUF: use existing buffer if it exists
3029 * ECMD_FORCEIT: ! used for Ex command
3030 * ECMD_ADDBUF: don't edit, just add to buffer list
3031 *
3032 * return FAIL for failure, OK otherwise
3033 */
3034 int
3035do_ecmd(fnum, ffname, sfname, eap, newlnum, flags)
3036 int fnum;
3037 char_u *ffname;
3038 char_u *sfname;
3039 exarg_T *eap; /* can be NULL! */
3040 linenr_T newlnum;
3041 int flags;
3042{
3043 int other_file; /* TRUE if editing another file */
3044 int oldbuf; /* TRUE if using existing buffer */
3045#ifdef FEAT_AUTOCMD
3046 int auto_buf = FALSE; /* TRUE if autocommands brought us
3047 into the buffer unexpectedly */
3048 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003049 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#endif
3051 buf_T *buf;
3052#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3053 buf_T *old_curbuf = curbuf;
3054#endif
3055 char_u *free_fname = NULL;
3056#ifdef FEAT_BROWSE
3057 char_u *browse_file = NULL;
3058#endif
3059 int retval = FAIL;
3060 long n;
3061 linenr_T lnum;
3062 linenr_T topline = 0;
3063 int newcol = -1;
3064 int solcol = -1;
3065 pos_T *pos;
3066#ifdef FEAT_SUN_WORKSHOP
3067 char_u *cp;
3068#endif
3069 char_u *command = NULL;
3070
3071 if (eap != NULL)
3072 command = eap->do_ecmd_cmd;
3073
3074 if (fnum != 0)
3075 {
3076 if (fnum == curbuf->b_fnum) /* file is already being edited */
3077 return OK; /* nothing to do */
3078 other_file = TRUE;
3079 }
3080 else
3081 {
3082#ifdef FEAT_BROWSE
3083 if (cmdmod.browse)
3084 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003085 if (
3086# ifdef FEAT_GUI
3087 !gui.in_use &&
3088# endif
3089 au_has_group((char_u *)"FileExplorer"))
3090 {
3091 /* No browsing supported but we do have the file explorer:
3092 * Edit the directory. */
3093 if (ffname == NULL || !mch_isdir(ffname))
3094 ffname = (char_u *)".";
3095 }
3096 else
3097 {
3098 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003100 if (browse_file == NULL)
3101 goto theend;
3102 ffname = browse_file;
3103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105#endif
3106 /* if no short name given, use ffname for short name */
3107 if (sfname == NULL)
3108 sfname = ffname;
3109#ifdef USE_FNAME_CASE
3110# ifdef USE_LONG_FNAME
3111 if (USE_LONG_FNAME)
3112# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003113 if (sfname != NULL)
3114 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116
3117#ifdef FEAT_LISTCMDS
3118 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3119 goto theend;
3120#endif
3121
3122 if (ffname == NULL)
3123 other_file = TRUE;
3124 /* there is no file name */
3125 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3126 other_file = FALSE;
3127 else
3128 {
3129 if (*ffname == NUL) /* re-edit with same file name */
3130 {
3131 ffname = curbuf->b_ffname;
3132 sfname = curbuf->b_fname;
3133 }
3134 free_fname = fix_fname(ffname); /* may expand to full path name */
3135 if (free_fname != NULL)
3136 ffname = free_fname;
3137 other_file = otherfile(ffname);
3138#ifdef FEAT_SUN_WORKSHOP
3139 if (usingSunWorkShop && p_acd
3140 && (cp = vim_strrchr(sfname, '/')) != NULL)
3141 sfname = ++cp;
3142#endif
3143 }
3144 }
3145
3146 /*
3147 * if the file was changed we may not be allowed to abandon it
3148 * - if we are going to re-edit the same file
3149 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3150 */
3151 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3152 || (curbuf->b_nwindows == 1
3153 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
3154 && check_changed(curbuf, p_awa, !other_file,
3155 (flags & ECMD_FORCEIT), FALSE))
3156 {
3157 if (fnum == 0 && other_file && ffname != NULL)
3158 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3159 goto theend;
3160 }
3161
3162#ifdef FEAT_VISUAL
3163 /*
3164 * End Visual mode before switching to another buffer, so the text can be
3165 * copied into the GUI selection buffer.
3166 */
3167 reset_VIsual();
3168#endif
3169
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003170#ifdef FEAT_AUTOCMD
3171 if ((command != NULL || newlnum > (linenr_T)0)
3172 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3173 {
3174 int len;
3175 char_u *p;
3176
3177 /* Set v:swapcommand for the SwapExists autocommands. */
3178 if (command != NULL)
3179 len = STRLEN(command) + 3;
3180 else
3181 len = 30;
3182 p = alloc((unsigned)len);
3183 if (p != NULL)
3184 {
3185 if (command != NULL)
3186 vim_snprintf((char *)p, len, ":%s\r", command);
3187 else
3188 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3189 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3190 did_set_swapcommand = TRUE;
3191 vim_free(p);
3192 }
3193 }
3194#endif
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 /*
3197 * If we are starting to edit another file, open a (new) buffer.
3198 * Otherwise we re-use the current buffer.
3199 */
3200 if (other_file)
3201 {
3202#ifdef FEAT_LISTCMDS
3203 if (!(flags & ECMD_ADDBUF))
3204#endif
3205 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003206 if (!cmdmod.keepalt)
3207 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 buflist_altfpos();
3209 }
3210
3211 if (fnum)
3212 buf = buflist_findnr(fnum);
3213 else
3214 {
3215#ifdef FEAT_LISTCMDS
3216 if (flags & ECMD_ADDBUF)
3217 {
3218 linenr_T tlnum = 1L;
3219
3220 if (command != NULL)
3221 {
3222 tlnum = atol((char *)command);
3223 if (tlnum <= 0)
3224 tlnum = 1L;
3225 }
3226 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3227 goto theend;
3228 }
3229#endif
3230 buf = buflist_new(ffname, sfname, 0L,
3231 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
3232 }
3233 if (buf == NULL)
3234 goto theend;
3235 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3236 {
3237 oldbuf = FALSE;
3238 buf->b_nwindows = 0;
3239 }
3240 else /* existing memfile */
3241 {
3242 oldbuf = TRUE;
3243 (void)buf_check_timestamp(buf, FALSE);
3244 /* Check if autocommands made buffer invalid or changed the current
3245 * buffer. */
3246 if (!buf_valid(buf)
3247#ifdef FEAT_AUTOCMD
3248 || curbuf != old_curbuf
3249#endif
3250 )
3251 goto theend;
3252#ifdef FEAT_EVAL
3253 if (aborting()) /* autocmds may abort script processing */
3254 goto theend;
3255#endif
3256 }
3257
3258 /* May jump to last used line number for a loaded buffer or when asked
3259 * for explicitly */
3260 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3261 {
3262 pos = buflist_findfpos(buf);
3263 newlnum = pos->lnum;
3264 solcol = pos->col;
3265 }
3266
3267 /*
3268 * Make the (new) buffer the one used by the current window.
3269 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3270 * If the current buffer was empty and has no file name, curbuf
3271 * is returned by buflist_new().
3272 */
3273 if (buf != curbuf)
3274 {
3275#ifdef FEAT_AUTOCMD
3276 /*
3277 * Be careful: The autocommands may delete any buffer and change
3278 * the current buffer.
3279 * - If the buffer we are going to edit is deleted, give up.
3280 * - If the current buffer is deleted, prefer to load the new
3281 * buffer when loading a buffer is required. This avoids
3282 * loading another buffer which then must be closed again.
3283 * - If we ended up in the new buffer already, need to skip a few
3284 * things, set auto_buf.
3285 */
3286 if (buf->b_fname != NULL)
3287 new_name = vim_strsave(buf->b_fname);
3288 au_new_curbuf = buf;
3289 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3290 if (!buf_valid(buf)) /* new buffer has been deleted */
3291 {
3292 delbuf_msg(new_name); /* frees new_name */
3293 goto theend;
3294 }
3295# ifdef FEAT_EVAL
3296 if (aborting()) /* autocmds may abort script processing */
3297 {
3298 vim_free(new_name);
3299 goto theend;
3300 }
3301# endif
3302 if (buf == curbuf) /* already in new buffer */
3303 auto_buf = TRUE;
3304 else
3305 {
3306 if (curbuf == old_curbuf)
3307#endif
3308 buf_copy_options(buf, BCO_ENTER);
3309
3310 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003311 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 close_buffer(curwin, curbuf,
3313 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD);
3314
3315#ifdef FEAT_AUTOCMD
3316# ifdef FEAT_EVAL
3317 if (aborting()) /* autocmds may abort script processing */
3318 {
3319 vim_free(new_name);
3320 goto theend;
3321 }
3322# endif
3323 /* Be careful again, like above. */
3324 if (!buf_valid(buf)) /* new buffer has been deleted */
3325 {
3326 delbuf_msg(new_name); /* frees new_name */
3327 goto theend;
3328 }
3329 if (buf == curbuf) /* already in new buffer */
3330 auto_buf = TRUE;
3331 else
3332#endif
3333 {
3334 curwin->w_buffer = buf;
3335 curbuf = buf;
3336 ++curbuf->b_nwindows;
3337 /* set 'fileformat' */
3338 if (*p_ffs && !oldbuf)
3339 set_fileformat(default_fileformat(), OPT_LOCAL);
3340 }
3341
3342 /* May get the window options from the last time this buffer
3343 * was in this window (or another window). If not used
3344 * before, reset the local window options to the global
3345 * values. Also restores old folding stuff. */
3346 get_winopts(buf);
3347
3348#ifdef FEAT_AUTOCMD
3349 }
3350 vim_free(new_name);
3351 au_new_curbuf = NULL;
3352#endif
3353 }
3354 else
3355 ++curbuf->b_nwindows;
3356
3357 curwin->w_pcmark.lnum = 1;
3358 curwin->w_pcmark.col = 0;
3359 }
3360 else /* !other_file */
3361 {
3362 if (
3363#ifdef FEAT_LISTCMDS
3364 (flags & ECMD_ADDBUF) ||
3365#endif
3366 check_fname() == FAIL)
3367 goto theend;
3368 oldbuf = (flags & ECMD_OLDBUF);
3369 }
3370
3371 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3372 {
3373 char_u *p;
3374
3375 curbuf->b_help = TRUE;
3376#ifdef FEAT_QUICKFIX
3377 set_string_option_direct((char_u *)"buftype", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003378 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#endif
3380
3381 /*
3382 * Always set these options after jumping to a help tag, because the
3383 * user may have an autocommand that gets in the way.
3384 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
3385 * latin1 word characters (for translated help files).
3386 * Only set it when needed, buf_init_chartab() is some work.
3387 */
3388 p =
3389#ifdef EBCDIC
3390 (char_u *)"65-255,^*,^|,^\"";
3391#else
3392 (char_u *)"!-~,^*,^|,^\",192-255";
3393#endif
3394 if (STRCMP(curbuf->b_p_isk, p) != 0)
3395 {
3396 set_string_option_direct((char_u *)"isk", -1, p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003397 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 check_buf_options(curbuf);
3399 (void)buf_init_chartab(curbuf, FALSE);
3400 }
3401
3402 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
3403 curwin->w_p_list = FALSE; /* no list mode */
3404
3405 curbuf->b_p_ma = FALSE; /* not modifiable */
3406 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
3407 curwin->w_p_nu = 0; /* no line numbers */
3408#ifdef FEAT_SCROLLBIND
3409 curwin->w_p_scb = FALSE; /* no scroll binding */
3410#endif
3411#ifdef FEAT_ARABIC
3412 curwin->w_p_arab = FALSE; /* no arabic mode */
3413#endif
3414#ifdef FEAT_RIGHTLEFT
3415 curwin->w_p_rl = FALSE; /* help window is left-to-right */
3416#endif
3417#ifdef FEAT_FOLDING
3418 curwin->w_p_fen = FALSE; /* No folding in the help window */
3419#endif
3420#ifdef FEAT_DIFF
3421 curwin->w_p_diff = FALSE; /* No 'diff' */
3422#endif
Bram Moolenaara1956f62006-03-12 22:18:00 +00003423#ifdef FEAT_SPELL
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003424 curwin->w_p_spell = FALSE; /* No spell checking */
3425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427#ifdef FEAT_AUTOCMD
3428 buf = curbuf;
3429#endif
3430 set_buflisted(FALSE);
3431 }
3432 else
3433 {
3434#ifdef FEAT_AUTOCMD
3435 buf = curbuf;
3436#endif
3437 /* Don't make a buffer listed if it's a help buffer. Useful when
3438 * using CTRL-O to go back to a help file. */
3439 if (!curbuf->b_help)
3440 set_buflisted(TRUE);
3441 }
3442
3443#ifdef FEAT_AUTOCMD
3444 /* If autocommands change buffers under our fingers, forget about
3445 * editing the file. */
3446 if (buf != curbuf)
3447 goto theend;
3448# ifdef FEAT_EVAL
3449 if (aborting()) /* autocmds may abort script processing */
3450 goto theend;
3451# endif
3452
3453 /* Since we are starting to edit a file, consider the filetype to be
3454 * unset. Helps for when an autocommand changes files and expects syntax
3455 * highlighting to work in the other file. */
3456 did_filetype = FALSE;
3457#endif
3458
3459/*
3460 * other_file oldbuf
3461 * FALSE FALSE re-edit same file, buffer is re-used
3462 * FALSE TRUE re-edit same file, nothing changes
3463 * TRUE FALSE start editing new file, new buffer
3464 * TRUE TRUE start editing in existing buffer (nothing to do)
3465 */
3466 if (!other_file && !oldbuf) /* re-use the buffer */
3467 {
3468 set_last_cursor(curwin); /* may set b_last_cursor */
3469 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3470 {
3471 newlnum = curwin->w_cursor.lnum;
3472 solcol = curwin->w_cursor.col;
3473 }
3474#ifdef FEAT_AUTOCMD
3475 buf = curbuf;
3476 if (buf->b_fname != NULL)
3477 new_name = vim_strsave(buf->b_fname);
3478 else
3479 new_name = NULL;
3480#endif
3481 buf_freeall(curbuf, FALSE, FALSE); /* free all things for buffer */
3482#ifdef FEAT_AUTOCMD
3483 /* If autocommands deleted the buffer we were going to re-edit, give
3484 * up and jump to the end. */
3485 if (!buf_valid(buf))
3486 {
3487 delbuf_msg(new_name); /* frees new_name */
3488 goto theend;
3489 }
3490 vim_free(new_name);
3491
3492 /* If autocommands change buffers under our fingers, forget about
3493 * re-editing the file. Should do the buf_clear_file(), but perhaps
3494 * the autocommands changed the buffer... */
3495 if (buf != curbuf)
3496 goto theend;
3497# ifdef FEAT_EVAL
3498 if (aborting()) /* autocmds may abort script processing */
3499 goto theend;
3500# endif
3501#endif
3502 buf_clear_file(curbuf);
3503 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3504 curbuf->b_op_end.lnum = 0;
3505 }
3506
3507/*
3508 * If we get here we are sure to start editing
3509 */
3510 /* don't redraw until the cursor is in the right line */
3511 ++RedrawingDisabled;
3512
3513 /* Assume success now */
3514 retval = OK;
3515
3516 /*
3517 * Reset cursor position, could be used by autocommands.
3518 */
3519 check_cursor();
3520
3521 /*
3522 * Check if we are editing the w_arg_idx file in the argument list.
3523 */
3524 check_arg_idx(curwin);
3525
3526#ifdef FEAT_AUTOCMD
3527 if (!auto_buf)
3528#endif
3529 {
3530 /*
3531 * Set cursor and init window before reading the file and executing
3532 * autocommands. This allows for the autocommands to position the
3533 * cursor.
3534 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003535 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537#ifdef FEAT_FOLDING
3538 /* It's like all lines in the buffer changed. Need to update
3539 * automatic folding. */
3540 foldUpdateAll(curwin);
3541#endif
3542
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003543#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (p_acd && curbuf->b_ffname != NULL
3545 && vim_chdirfile(curbuf->b_ffname) == OK)
3546 shorten_fnames(TRUE);
3547#endif
3548 /*
3549 * Careful: open_buffer() and apply_autocmds() may change the current
3550 * buffer and window.
3551 */
3552 lnum = curwin->w_cursor.lnum;
3553 topline = curwin->w_topline;
3554 if (!oldbuf) /* need to read the file */
3555 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003556#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 swap_exists_action = SEA_DIALOG;
3558#endif
3559 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3560
3561 /*
3562 * Open the buffer and read the file.
3563 */
3564#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3565 if (should_abort(open_buffer(FALSE, eap)))
3566 retval = FAIL;
3567#else
3568 (void)open_buffer(FALSE, eap);
3569#endif
3570
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003571#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (swap_exists_action == SEA_QUIT)
3573 retval = FAIL;
3574 handle_swap_exists(old_curbuf);
3575#endif
3576 }
3577#ifdef FEAT_AUTOCMD
3578 else
3579 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003580 /* Read the modelines, but only to set window-local options. Any
3581 * buffer-local options have already been set and may have been
3582 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00003583 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3586 &retval);
3587 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3588 &retval);
3589 }
3590 check_arg_idx(curwin);
3591#endif
3592
3593 /*
3594 * If autocommands change the cursor position or topline, we should
3595 * keep it.
3596 */
3597 if (curwin->w_cursor.lnum != lnum)
3598 {
3599 newlnum = curwin->w_cursor.lnum;
3600 newcol = curwin->w_cursor.col;
3601 }
3602 if (curwin->w_topline == topline)
3603 topline = 0;
3604
3605 /* Even when cursor didn't move we need to recompute topline. */
3606 changed_line_abv_curs();
3607
3608#ifdef FEAT_TITLE
3609 maketitle();
3610#endif
3611 }
3612
3613#ifdef FEAT_DIFF
3614 /* Tell the diff stuff that this buffer is new and/or needs updating.
3615 * Also needed when re-editing the same buffer, because unloading will
3616 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003617 if (curwin->w_p_diff)
3618 {
3619 diff_buf_add(curbuf);
3620 diff_invalidate(curbuf);
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#endif
3623
3624 if (command == NULL)
3625 {
3626 if (newcol >= 0) /* position set by autocommands */
3627 {
3628 curwin->w_cursor.lnum = newlnum;
3629 curwin->w_cursor.col = newcol;
3630 check_cursor();
3631 }
3632 else if (newlnum > 0) /* line number from caller or old position */
3633 {
3634 curwin->w_cursor.lnum = newlnum;
3635 check_cursor_lnum();
3636 if (solcol >= 0 && !p_sol)
3637 {
3638 /* 'sol' is off: Use last known column. */
3639 curwin->w_cursor.col = solcol;
3640 check_cursor_col();
3641#ifdef FEAT_VIRTUALEDIT
3642 curwin->w_cursor.coladd = 0;
3643#endif
3644 curwin->w_set_curswant = TRUE;
3645 }
3646 else
3647 beginline(BL_SOL | BL_FIX);
3648 }
3649 else /* no line number, go to last line in Ex mode */
3650 {
3651 if (exmode_active)
3652 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3653 beginline(BL_WHITE | BL_FIX);
3654 }
3655 }
3656
3657#ifdef FEAT_WINDOWS
3658 /* Check if cursors in other windows on the same buffer are still valid */
3659 check_lnums(FALSE);
3660#endif
3661
3662 /*
3663 * Did not read the file, need to show some info about the file.
3664 * Do this after setting the cursor.
3665 */
3666 if (oldbuf
3667#ifdef FEAT_AUTOCMD
3668 && !auto_buf
3669#endif
3670 )
3671 {
3672 int msg_scroll_save = msg_scroll;
3673
3674 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3675 * message. */
3676 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3677 msg_scroll = FALSE;
3678 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3679 check_for_delay(FALSE);
3680 msg_start();
3681 msg_scroll = msg_scroll_save;
3682 msg_scrolled_ign = TRUE;
3683
3684 fileinfo(FALSE, TRUE, FALSE);
3685
3686 msg_scrolled_ign = FALSE;
3687 }
3688
3689 if (command != NULL)
3690 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3691
3692#ifdef FEAT_KEYMAP
3693 if (curbuf->b_kmap_state & KEYMAP_INIT)
3694 keymap_init();
3695#endif
3696
3697 --RedrawingDisabled;
3698 if (!skip_redraw)
3699 {
3700 n = p_so;
3701 if (topline == 0 && command == NULL)
3702 p_so = 999; /* force cursor halfway the window */
3703 update_topline();
3704#ifdef FEAT_SCROLLBIND
3705 curwin->w_scbind_pos = curwin->w_topline;
3706#endif
3707 p_so = n;
3708 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3709 }
3710
3711 if (p_im)
3712 need_start_insertmode = TRUE;
3713
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003714#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 /* Change directories when the acd option is set on. */
3716 if (p_acd && curbuf->b_ffname != NULL
3717 && vim_chdirfile(curbuf->b_ffname) == OK)
3718 shorten_fnames(TRUE);
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003721#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (gui.in_use && curbuf->b_ffname != NULL)
3723 {
3724# ifdef FEAT_SUN_WORKSHOP
3725 if (usingSunWorkShop)
3726 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3727# endif
3728# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003729 if (usingNetbeans & ((flags & ECMD_SET_HELP) != ECMD_SET_HELP))
3730 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731# endif
3732 }
3733#endif
3734
3735theend:
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003736#ifdef FEAT_AUTOCMD
3737 if (did_set_swapcommand)
3738 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
3739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740#ifdef FEAT_BROWSE
3741 vim_free(browse_file);
3742#endif
3743 vim_free(free_fname);
3744 return retval;
3745}
3746
3747#ifdef FEAT_AUTOCMD
3748 static void
3749delbuf_msg(name)
3750 char_u *name;
3751{
3752 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3753 name == NULL ? (char_u *)"" : name);
3754 vim_free(name);
3755 au_new_curbuf = NULL;
3756}
3757#endif
3758
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003759static int append_indent = 0; /* autoindent for first line */
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761/*
3762 * ":insert" and ":append", also used by ":change"
3763 */
3764 void
3765ex_append(eap)
3766 exarg_T *eap;
3767{
3768 char_u *theline;
3769 int did_undo = FALSE;
3770 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003771 int indent = 0;
3772 char_u *p;
3773 int vcol;
3774 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003776 /* the ! flag toggles autoindent */
3777 if (eap->forceit)
3778 curbuf->b_p_ai = !curbuf->b_p_ai;
3779
3780 /* First autoindent comes from the line we start on */
3781 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
3782 append_indent = get_indent_lnum(lnum);
3783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 if (eap->cmdidx != CMD_append)
3785 --lnum;
3786
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003787 /* when the buffer is empty append to line 0 and delete the dummy line */
3788 if (empty && lnum == 1)
3789 lnum = 0;
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 State = INSERT; /* behave like in Insert mode */
3792 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3793 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003794
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003795 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 msg_scroll = TRUE;
3798 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003799 if (curbuf->b_p_ai)
3800 {
3801 if (append_indent >= 0)
3802 {
3803 indent = append_indent;
3804 append_indent = -1;
3805 }
3806 else if (lnum > 0)
3807 indent = get_indent_lnum(lnum);
3808 }
3809 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003811 {
3812 /* No getline() function, use the lines that follow. This ends
3813 * when there is no more. */
3814 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
3815 break;
3816 p = vim_strchr(eap->nextcmd, NL);
3817 if (p == NULL)
3818 p = eap->nextcmd + STRLEN(eap->nextcmd);
3819 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
3820 if (*p != NUL)
3821 ++p;
3822 eap->nextcmd = p;
3823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 else
3825 theline = eap->getline(
3826#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003827 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003829 NUL, eap->cookie, indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003831 if (theline == NULL)
3832 break;
3833
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003834 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
3835 if (ex_keep_indent)
3836 append_indent = indent;
3837
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003838 /* Look for the "." after automatic indent. */
3839 vcol = 0;
3840 for (p = theline; indent > vcol; ++p)
3841 {
3842 if (*p == ' ')
3843 ++vcol;
3844 else if (*p == TAB)
3845 vcol += 8 - vcol % 8;
3846 else
3847 break;
3848 }
3849 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00003850 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
3851 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
3853 vim_free(theline);
3854 break;
3855 }
3856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003857 /* don't use autoindent if nothing was typed. */
3858 if (p[0] == NUL)
3859 theline[0] = NUL;
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 did_undo = TRUE;
3862 ml_append(lnum, theline, (colnr_T)0, FALSE);
3863 appended_lines_mark(lnum, 1L);
3864
3865 vim_free(theline);
3866 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003867
3868 if (empty)
3869 {
3870 ml_delete(2L, FALSE);
3871 empty = FALSE;
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874 State = NORMAL;
3875
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003876 if (eap->forceit)
3877 curbuf->b_p_ai = !curbuf->b_p_ai;
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 /* "start" is set to eap->line2+1 unless that position is invalid (when
3880 * eap->line2 pointed to the end of the buffer and nothig was appended)
3881 * "end" is set to lnum when something has been appended, otherwise
3882 * it is the same than "start" -- Acevedo */
3883 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
3884 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
3885 if (eap->cmdidx != CMD_append)
3886 --curbuf->b_op_start.lnum;
3887 curbuf->b_op_end.lnum = (eap->line2 < lnum)
3888 ? lnum : curbuf->b_op_start.lnum;
3889 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
3890 curwin->w_cursor.lnum = lnum;
3891 check_cursor_lnum();
3892 beginline(BL_SOL | BL_FIX);
3893
3894 need_wait_return = FALSE; /* don't use wait_return() now */
3895 ex_no_reprint = TRUE;
3896}
3897
3898/*
3899 * ":change"
3900 */
3901 void
3902ex_change(eap)
3903 exarg_T *eap;
3904{
3905 linenr_T lnum;
3906
3907 if (eap->line2 >= eap->line1
3908 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
3909 return;
3910
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003911 /* the ! flag toggles autoindent */
3912 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
3913 append_indent = get_indent_lnum(eap->line1);
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
3916 {
3917 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
3918 break;
3919 ml_delete(eap->line1, FALSE);
3920 }
3921 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
3922
3923 /* ":append" on the line above the deleted lines. */
3924 eap->line2 = eap->line1;
3925 ex_append(eap);
3926}
3927
3928 void
3929ex_z(eap)
3930 exarg_T *eap;
3931{
3932 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003933 int bigness;
3934 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 int minus = 0;
3936 linenr_T start, end, curs, i;
3937 int j;
3938 linenr_T lnum = eap->line2;
3939
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003940 /* Vi compatible: ":z!" uses display height, without a count uses
3941 * 'scroll' */
3942 if (eap->forceit)
3943 bigness = curwin->w_height;
3944 else if (firstwin == lastwin)
3945 bigness = curwin->w_p_scr * 2;
3946 else
3947 bigness = curwin->w_height - 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (bigness < 1)
3949 bigness = 1;
3950
3951 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003952 kind = x;
3953 if (*kind == '-' || *kind == '+' || *kind == '='
3954 || *kind == '^' || *kind == '.')
3955 ++x;
3956 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 ++x;
3958
3959 if (*x != 0)
3960 {
3961 if (!VIM_ISDIGIT(*x))
3962 {
3963 EMSG(_("E144: non-numeric argument to :z"));
3964 return;
3965 }
3966 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003969 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003970 if (*kind == '=')
3971 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003975 /* the number of '-' and '+' multiplies the distance */
3976 if (*kind == '-' || *kind == '+')
3977 for (x = kind + 1; *x == *kind; ++x)
3978 ;
3979
3980 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
3982 case '-':
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003983 start = lnum - bigness * (x - kind);
3984 end = start + bigness;
3985 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 break;
3987
3988 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003989 start = lnum - (bigness + 1) / 2 + 1;
3990 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 curs = lnum;
3992 minus = 1;
3993 break;
3994
3995 case '^':
3996 start = lnum - bigness * 2;
3997 end = lnum - bigness;
3998 curs = lnum - bigness;
3999 break;
4000
4001 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004002 start = lnum - (bigness + 1) / 2 + 1;
4003 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 curs = end;
4005 break;
4006
4007 default: /* '+' */
4008 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004009 if (*kind == '+')
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004010 start += bigness * (x - kind - 1) + 1;
4011 else if (eap->addr_count == 0)
4012 ++start;
4013 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 curs = end;
4015 break;
4016 }
4017
4018 if (start < 1)
4019 start = 1;
4020
4021 if (end > curbuf->b_ml.ml_line_count)
4022 end = curbuf->b_ml.ml_line_count;
4023
4024 if (curs > curbuf->b_ml.ml_line_count)
4025 curs = curbuf->b_ml.ml_line_count;
4026
4027 for (i = start; i <= end; i++)
4028 {
4029 if (minus && i == lnum)
4030 {
4031 msg_putchar('\n');
4032
4033 for (j = 1; j < Columns; j++)
4034 msg_putchar('-');
4035 }
4036
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004037 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 if (minus && i == lnum)
4040 {
4041 msg_putchar('\n');
4042
4043 for (j = 1; j < Columns; j++)
4044 msg_putchar('-');
4045 }
4046 }
4047
4048 curwin->w_cursor.lnum = curs;
4049 ex_no_reprint = TRUE;
4050}
4051
4052/*
4053 * Check if the restricted flag is set.
4054 * If so, give an error message and return TRUE.
4055 * Otherwise, return FALSE.
4056 */
4057 int
4058check_restricted()
4059{
4060 if (restricted)
4061 {
4062 EMSG(_("E145: Shell commands not allowed in rvim"));
4063 return TRUE;
4064 }
4065 return FALSE;
4066}
4067
4068/*
4069 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4070 * If so, give an error message and return TRUE.
4071 * Otherwise, return FALSE.
4072 */
4073 int
4074check_secure()
4075{
4076 if (secure)
4077 {
4078 secure = 2;
4079 EMSG(_(e_curdir));
4080 return TRUE;
4081 }
4082#ifdef HAVE_SANDBOX
4083 /*
4084 * In the sandbox more things are not allowed, including the things
4085 * disallowed in secure mode.
4086 */
4087 if (sandbox != 0)
4088 {
4089 EMSG(_(e_sandbox));
4090 return TRUE;
4091 }
4092#endif
4093 return FALSE;
4094}
4095
4096static char_u *old_sub = NULL; /* previous substitute pattern */
4097static int global_need_beginline; /* call beginline() after ":g" */
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099/* do_sub()
4100 *
4101 * Perform a substitution from line eap->line1 to line eap->line2 using the
4102 * command pointed to by eap->arg which should be of the form:
4103 *
4104 * /pattern/substitution/{flags}
4105 *
4106 * The usual escapes are supported as described in the regexp docs.
4107 */
4108 void
4109do_sub(eap)
4110 exarg_T *eap;
4111{
4112 linenr_T lnum;
4113 long i = 0;
4114 regmmatch_T regmatch;
4115 static int do_all = FALSE; /* do multiple substitutions per line */
4116 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004117 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 static int do_error = TRUE; /* if false, ignore errors */
4119 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004120 static int do_list = FALSE; /* list last line with subs. */
4121 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 static int do_ic = 0; /* ignore case flag */
4123 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4124 int delimiter;
4125 int sublen;
4126 int got_quit = FALSE;
4127 int got_match = FALSE;
4128 int temp;
4129 int which_pat;
4130 char_u *cmd;
4131 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004132 linenr_T first_line = 0; /* first changed line */
4133 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 * change */
4135 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4136 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004137 long nmatch; /* number of lines in match */
4138 linenr_T sub_firstlnum; /* nr of first sub line */
4139 char_u *sub_firstline; /* allocated copy of first sub line */
4140 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004141 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 cmd = eap->arg;
4144 if (!global_busy)
4145 {
4146 sub_nsubs = 0;
4147 sub_nlines = 0;
4148 }
4149
4150#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4151 if (p_altkeymap && curwin->w_p_rl)
4152 lrF_sub(cmd);
4153#endif
4154
4155 if (eap->cmdidx == CMD_tilde)
4156 which_pat = RE_LAST; /* use last used regexp */
4157 else
4158 which_pat = RE_SUBST; /* use last substitute regexp */
4159
4160 /* new pattern and substitution */
4161 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4162 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4163 {
4164 /* don't accept alphanumeric for separator */
4165 if (isalpha(*cmd))
4166 {
4167 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4168 return;
4169 }
4170 /*
4171 * undocumented vi feature:
4172 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4173 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4174 */
4175 if (*cmd == '\\')
4176 {
4177 ++cmd;
4178 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4179 {
4180 EMSG(_(e_backslash));
4181 return;
4182 }
4183 if (*cmd != '&')
4184 which_pat = RE_SEARCH; /* use last '/' pattern */
4185 pat = (char_u *)""; /* empty search pattern */
4186 delimiter = *cmd++; /* remember delimiter character */
4187 }
4188 else /* find the end of the regexp */
4189 {
4190 which_pat = RE_LAST; /* use last used regexp */
4191 delimiter = *cmd++; /* remember delimiter character */
4192 pat = cmd; /* remember start of search pat */
4193 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4194 if (cmd[0] == delimiter) /* end delimiter found */
4195 *cmd++ = NUL; /* replace it with a NUL */
4196 }
4197
4198 /*
4199 * Small incompatibility: vi sees '\n' as end of the command, but in
4200 * Vim we want to use '\n' to find/substitute a NUL.
4201 */
4202 sub = cmd; /* remember the start of the substitution */
4203
4204 while (cmd[0])
4205 {
4206 if (cmd[0] == delimiter) /* end delimiter found */
4207 {
4208 *cmd++ = NUL; /* replace it with a NUL */
4209 break;
4210 }
4211 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4212 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004213 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215
4216 if (!eap->skip)
4217 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004218 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4219 if (STRCMP(sub, "%") == 0
4220 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4221 {
4222 if (old_sub == NULL) /* there is no previous command */
4223 {
4224 EMSG(_(e_nopresub));
4225 return;
4226 }
4227 sub = old_sub;
4228 }
4229 else
4230 {
4231 vim_free(old_sub);
4232 old_sub = vim_strsave(sub);
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 }
4236 else if (!eap->skip) /* use previous pattern and substitution */
4237 {
4238 if (old_sub == NULL) /* there is no previous command */
4239 {
4240 EMSG(_(e_nopresub));
4241 return;
4242 }
4243 pat = NULL; /* search_regcomp() will use previous pattern */
4244 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004245
4246 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4247 * last column after using "$". */
4248 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250
4251 /*
4252 * Find trailing options. When '&' is used, keep old options.
4253 */
4254 if (*cmd == '&')
4255 ++cmd;
4256 else
4257 {
4258 if (!p_ed)
4259 {
4260 if (p_gd) /* default is global on */
4261 do_all = TRUE;
4262 else
4263 do_all = FALSE;
4264 do_ask = FALSE;
4265 }
4266 do_error = TRUE;
4267 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004268 do_count = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 do_ic = 0;
4270 }
4271 while (*cmd)
4272 {
4273 /*
4274 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4275 * 'r' is never inverted.
4276 */
4277 if (*cmd == 'g')
4278 do_all = !do_all;
4279 else if (*cmd == 'c')
4280 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281 else if (*cmd == 'n')
4282 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 else if (*cmd == 'e')
4284 do_error = !do_error;
4285 else if (*cmd == 'r') /* use last used regexp */
4286 which_pat = RE_LAST;
4287 else if (*cmd == 'p')
4288 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004289 else if (*cmd == '#')
4290 {
4291 do_print = TRUE;
4292 do_number = TRUE;
4293 }
4294 else if (*cmd == 'l')
4295 {
4296 do_print = TRUE;
4297 do_list = TRUE;
4298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 else if (*cmd == 'i') /* ignore case */
4300 do_ic = 'i';
4301 else if (*cmd == 'I') /* don't ignore case */
4302 do_ic = 'I';
4303 else
4304 break;
4305 ++cmd;
4306 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004307 if (do_count)
4308 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /*
4311 * check for a trailing count
4312 */
4313 cmd = skipwhite(cmd);
4314 if (VIM_ISDIGIT(*cmd))
4315 {
4316 i = getdigits(&cmd);
4317 if (i <= 0 && !eap->skip && do_error)
4318 {
4319 EMSG(_(e_zerocount));
4320 return;
4321 }
4322 eap->line1 = eap->line2;
4323 eap->line2 += i - 1;
4324 if (eap->line2 > curbuf->b_ml.ml_line_count)
4325 eap->line2 = curbuf->b_ml.ml_line_count;
4326 }
4327
4328 /*
4329 * check for trailing command or garbage
4330 */
4331 cmd = skipwhite(cmd);
4332 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4333 {
4334 eap->nextcmd = check_nextcmd(cmd);
4335 if (eap->nextcmd == NULL)
4336 {
4337 EMSG(_(e_trailing));
4338 return;
4339 }
4340 }
4341
4342 if (eap->skip) /* not executing commands, only parsing */
4343 return;
4344
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004345 if (!do_count && !curbuf->b_p_ma)
4346 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004347 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004348 EMSG(_(e_modifiable));
4349 return;
4350 }
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4353 {
4354 if (do_error)
4355 EMSG(_(e_invcmd));
4356 return;
4357 }
4358
4359 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4360 if (do_ic == 'i')
4361 regmatch.rmm_ic = TRUE;
4362 else if (do_ic == 'I')
4363 regmatch.rmm_ic = FALSE;
4364
4365 sub_firstline = NULL;
4366
4367 /*
4368 * ~ in the substitute pattern is replaced with the old pattern.
4369 * We do it here once to avoid it to be replaced over and over again.
4370 * But don't do it when it starts with "\=", then it's an expression.
4371 */
4372 if (!(sub[0] == '\\' && sub[1] == '='))
4373 sub = regtilde(sub, p_magic);
4374
4375 /*
4376 * Check for a match on each line.
4377 */
4378 line2 = eap->line2;
4379 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4380#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4381 || aborting()
4382#endif
4383 ); ++lnum)
4384 {
4385 sub_firstlnum = lnum;
4386 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
4387 if (nmatch)
4388 {
4389 colnr_T copycol;
4390 colnr_T matchcol;
4391 colnr_T prev_matchcol = MAXCOL;
4392 char_u *new_end, *new_start = NULL;
4393 unsigned new_start_len = 0;
4394 char_u *p1;
4395 int did_sub = FALSE;
4396 int lastone;
4397 unsigned len, needed_len;
4398 long nmatch_tl = 0; /* nr of lines matched below lnum */
4399 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004400 int skip_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /*
4403 * The new text is build up step by step, to avoid too much
4404 * copying. There are these pieces:
4405 * sub_firstline The old text, unmodifed.
4406 * copycol Column in the old text where we started
4407 * looking for a match; from here old text still
4408 * needs to be copied to the new text.
4409 * matchcol Column number of the old text where to look
4410 * for the next match. It's just after the
4411 * previous match or one further.
4412 * prev_matchcol Column just after the previous match (if any).
4413 * Mostly equal to matchcol, except for the first
4414 * match and after skipping an empty match.
4415 * regmatch.*pos Where the pattern matched in the old text.
4416 * new_start The new text, all that has been produced so
4417 * far.
4418 * new_end The new text, where to append new text.
4419 *
4420 * lnum The line number where we were looking for the
4421 * first match in the old line.
4422 * sub_firstlnum The line number in the buffer where to look
4423 * for a match. Can be different from "lnum"
4424 * when the pattern or substitute string contains
4425 * line breaks.
4426 *
4427 * Special situations:
4428 * - When the substitute string contains a line break, the part up
4429 * to the line break is inserted in the text, but the copy of
4430 * the original line is kept. "sub_firstlnum" is adjusted for
4431 * the inserted lines.
4432 * - When the matched pattern contains a line break, the old line
4433 * is taken from the line at the end of the pattern. The lines
4434 * in the match are deleted later, "sub_firstlnum" is adjusted
4435 * accordingly.
4436 *
4437 * The new text is built up in new_start[]. It has some extra
4438 * room to avoid using alloc()/free() too often. new_start_len is
4439 * the lenght of the allocated memory at new_start.
4440 *
4441 * Make a copy of the old line, so it won't be taken away when
4442 * updating the screen or handling a multi-line match. The "old_"
4443 * pointers point into this copy.
4444 */
4445 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4446 if (sub_firstline == NULL)
4447 {
4448 vim_free(new_start);
4449 goto outofmem;
4450 }
4451 copycol = 0;
4452 matchcol = 0;
4453
4454 /* At first match, remember current cursor position. */
4455 if (!got_match)
4456 {
4457 setpcmark();
4458 got_match = TRUE;
4459 }
4460
4461 /*
4462 * Loop until nothing more to replace in this line.
4463 * 1. Handle match with empty string.
4464 * 2. If do_ask is set, ask for confirmation.
4465 * 3. substitute the string.
4466 * 4. if do_all is set, find next match
4467 * 5. break if there isn't another match in this line
4468 */
4469 for (;;)
4470 {
4471 /* Save the line number of the last change for the final
4472 * cursor position (just like Vi). */
4473 curwin->w_cursor.lnum = lnum;
4474 do_again = FALSE;
4475
4476 /*
4477 * 1. Match empty string does not count, except for first
4478 * match. This reproduces the strange vi behaviour.
4479 * This also catches endless loops.
4480 */
4481 if (matchcol == prev_matchcol
4482 && regmatch.endpos[0].lnum == 0
4483 && matchcol == regmatch.endpos[0].col)
4484 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004485 if (sub_firstline[matchcol] == NUL)
4486 /* We already were at the end of the line. Don't look
4487 * for a match in this line again. */
4488 skip_match = TRUE;
4489 else
4490 ++matchcol; /* search for a match at next column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 goto skip;
4492 }
4493
4494 /* Normally we continue searching for a match just after the
4495 * previous match. */
4496 matchcol = regmatch.endpos[0].col;
4497 prev_matchcol = matchcol;
4498
4499 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004500 * 2. If do_count is set only increase the counter.
4501 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004503 if (do_count)
4504 {
4505 /* For a multi-line match, put matchcol at the NUL at
4506 * the end of the line and set nmatch to one, so that
4507 * we continue looking for a match on the next line.
4508 * Avoids that ":s/\nB\@=//gc" get stuck. */
4509 if (nmatch > 1)
4510 {
4511 matchcol = STRLEN(sub_firstline);
4512 nmatch = 1;
4513 }
4514 sub_nsubs++;
4515 did_sub = TRUE;
4516 goto skip;
4517 }
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (do_ask)
4520 {
4521 /* change State to CONFIRM, so that the mouse works
4522 * properly */
4523 save_State = State;
4524 State = CONFIRM;
4525#ifdef FEAT_MOUSE
4526 setmouse(); /* disable mouse in xterm */
4527#endif
4528 curwin->w_cursor.col = regmatch.startpos[0].col;
4529
4530 /* When 'cpoptions' contains "u" don't sync undo when
4531 * asking for confirmation. */
4532 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4533 ++no_u_sync;
4534
4535 /*
4536 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4537 */
4538 while (do_ask)
4539 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004540 if (exmode_active)
4541 {
4542 char_u *resp;
4543 colnr_T sc, ec;
4544
4545 print_line_no_prefix(lnum, FALSE, FALSE);
4546
4547 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4548 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4549 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
4550 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004551 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004552 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004553 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004554 msg_putchar('^');
4555
4556 resp = getexmodeline('?', NULL, 0);
4557 if (resp != NULL)
4558 {
4559 i = *resp;
4560 vim_free(resp);
4561 }
4562 }
4563 else
4564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004566 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004568 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004570 /* Invert the matched string.
4571 * Remove the inversion afterwards. */
4572 temp = RedrawingDisabled;
4573 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004575 search_match_lines = regmatch.endpos[0].lnum;
4576 search_match_endcol = regmatch.endpos[0].col;
4577 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004579 update_topline();
4580 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00004581 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004582 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00004583 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004586 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004588 if (msg_row == Rows - 1)
4589 msg_didout = FALSE; /* avoid a scroll-up */
4590 msg_starthere();
4591 i = msg_scroll;
4592 msg_scroll = 0; /* truncate msg when
4593 needed */
4594 msg_no_more = TRUE;
4595 /* write message same highlighting as for
4596 * wait_return */
4597 smsg_attr(hl_attr(HLF_R),
4598 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4599 msg_no_more = FALSE;
4600 msg_scroll = i;
4601 showruler(TRUE);
4602 windgoto(msg_row, msg_col);
4603 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004606 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004608 ++no_mapping; /* don't map this key */
4609 ++allow_keys; /* allow special keys */
4610 i = safe_vgetc();
4611 --allow_keys;
4612 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004614 /* clear the question */
4615 msg_didout = FALSE; /* don't scroll up */
4616 msg_col = 0;
4617 gotocmdline(TRUE);
4618 }
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 need_wait_return = FALSE; /* no hit-return prompt */
4621 if (i == 'q' || i == ESC || i == Ctrl_C
4622#ifdef UNIX
4623 || i == intr_char
4624#endif
4625 )
4626 {
4627 got_quit = TRUE;
4628 break;
4629 }
4630 if (i == 'n')
4631 break;
4632 if (i == 'y')
4633 break;
4634 if (i == 'l')
4635 {
4636 /* last: replace and then stop */
4637 do_all = FALSE;
4638 line2 = lnum;
4639 break;
4640 }
4641 if (i == 'a')
4642 {
4643 do_ask = FALSE;
4644 break;
4645 }
4646#ifdef FEAT_INS_EXPAND
4647 if (i == Ctrl_E)
4648 scrollup_clamp();
4649 else if (i == Ctrl_Y)
4650 scrolldown_clamp();
4651#endif
4652 }
4653 State = save_State;
4654#ifdef FEAT_MOUSE
4655 setmouse();
4656#endif
4657 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4658 --no_u_sync;
4659
4660 if (i == 'n')
4661 {
4662 /* For a multi-line match, put matchcol at the NUL at
4663 * the end of the line and set nmatch to one, so that
4664 * we continue looking for a match on the next line.
4665 * Avoids that ":s/\nB\@=//gc" get stuck. */
4666 if (nmatch > 1)
4667 {
4668 matchcol = STRLEN(sub_firstline);
4669 nmatch = 1;
4670 }
4671 goto skip;
4672 }
4673 if (got_quit)
4674 break;
4675 }
4676
4677 /* Move the cursor to the start of the match, so that we can
4678 * use "\=col("."). */
4679 curwin->w_cursor.col = regmatch.startpos[0].col;
4680
4681 /*
4682 * 3. substitute the string.
4683 */
4684 /* get length of substitution part */
4685 sublen = vim_regsub_multi(&regmatch, sub_firstlnum,
4686 sub, sub_firstline, FALSE, p_magic, TRUE);
4687
Bram Moolenaar4463f292005-09-25 22:20:24 +00004688 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004689 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00004690 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
4691 {
4692 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
4693 skip_match = TRUE;
4694 }
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 /* Need room for:
4697 * - result so far in new_start (not for first sub in line)
4698 * - original text up to match
4699 * - length of substituted part
4700 * - original text after match
4701 */
4702 if (nmatch == 1)
4703 p1 = sub_firstline;
4704 else
4705 {
4706 p1 = ml_get(sub_firstlnum + nmatch - 1);
4707 nmatch_tl += nmatch - 1;
4708 }
4709 i = regmatch.startpos[0].col - copycol;
4710 needed_len = i + ((unsigned)STRLEN(p1) - regmatch.endpos[0].col)
4711 + sublen + 1;
4712 if (new_start == NULL)
4713 {
4714 /*
4715 * Get some space for a temporary buffer to do the
4716 * substitution into (and some extra space to avoid
4717 * too many calls to alloc()/free()).
4718 */
4719 new_start_len = needed_len + 50;
4720 if ((new_start = alloc_check(new_start_len)) == NULL)
4721 goto outofmem;
4722 *new_start = NUL;
4723 new_end = new_start;
4724 }
4725 else
4726 {
4727 /*
4728 * Check if the temporary buffer is long enough to do the
4729 * substitution into. If not, make it larger (with a bit
4730 * extra to avoid too many calls to alloc()/free()).
4731 */
4732 len = (unsigned)STRLEN(new_start);
4733 needed_len += len;
4734 if (needed_len > new_start_len)
4735 {
4736 new_start_len = needed_len + 50;
4737 if ((p1 = alloc_check(new_start_len)) == NULL)
4738 {
4739 vim_free(new_start);
4740 goto outofmem;
4741 }
4742 mch_memmove(p1, new_start, (size_t)(len + 1));
4743 vim_free(new_start);
4744 new_start = p1;
4745 }
4746 new_end = new_start + len;
4747 }
4748
4749 /*
4750 * copy the text up to the part that matched
4751 */
4752 mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
4753 new_end += i;
4754
4755 (void)vim_regsub_multi(&regmatch, sub_firstlnum,
4756 sub, new_end, TRUE, p_magic, TRUE);
4757 sub_nsubs++;
4758 did_sub = TRUE;
4759
4760 /* Move the cursor to the start of the line, to avoid that it
4761 * is beyond the end of the line after the substitution. */
4762 curwin->w_cursor.col = 0;
4763
4764 /* For a multi-line match, make a copy of the last matched
4765 * line and continue in that one. */
4766 if (nmatch > 1)
4767 {
4768 sub_firstlnum += nmatch - 1;
4769 vim_free(sub_firstline);
4770 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4771 /* When going beyond the last line, stop substituting. */
4772 if (sub_firstlnum <= line2)
4773 do_again = TRUE;
4774 else
4775 do_all = FALSE;
4776 }
4777
4778 /* Remember next character to be copied. */
4779 copycol = regmatch.endpos[0].col;
4780
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004781 if (skip_match)
4782 {
4783 /* Already hit end of the buffer, sub_firstlnum is one
4784 * less than what it ought to be. */
4785 vim_free(sub_firstline);
4786 sub_firstline = vim_strsave((char_u *)"");
4787 copycol = 0;
4788 }
4789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /*
4791 * Now the trick is to replace CTRL-M chars with a real line
4792 * break. This would make it impossible to insert a CTRL-M in
4793 * the text. The line break can be avoided by preceding the
4794 * CTRL-M with a backslash. To be able to insert a backslash,
4795 * they must be doubled in the string and are halved here.
4796 * That is Vi compatible.
4797 */
4798 for (p1 = new_end; *p1; ++p1)
4799 {
4800 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
4801 mch_memmove(p1, p1 + 1, STRLEN(p1));
4802 else if (*p1 == CAR)
4803 {
4804 if (u_inssub(lnum) == OK) /* prepare for undo */
4805 {
4806 *p1 = NUL; /* truncate up to the CR */
4807 ml_append(lnum - 1, new_start,
4808 (colnr_T)(p1 - new_start + 1), FALSE);
4809 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
4810 if (do_ask)
4811 appended_lines(lnum - 1, 1L);
4812 else
4813 {
4814 if (first_line == 0)
4815 first_line = lnum;
4816 last_line = lnum + 1;
4817 }
4818 /* All line numbers increase. */
4819 ++sub_firstlnum;
4820 ++lnum;
4821 ++line2;
4822 /* move the cursor to the new line, like Vi */
4823 ++curwin->w_cursor.lnum;
4824 STRCPY(new_start, p1 + 1); /* copy the rest */
4825 p1 = new_start - 1;
4826 }
4827 }
4828#ifdef FEAT_MBYTE
4829 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004830 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#endif
4832 }
4833
4834 /*
4835 * 4. If do_all is set, find next match.
4836 * Prevent endless loop with patterns that match empty
4837 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
4838 * But ":s/\n/#/" is OK.
4839 */
4840skip:
4841 /* We already know that we did the last subst when we are at
4842 * the end of the line, except that a pattern like
4843 * "bar\|\nfoo" may match at the NUL. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004844 lastone = (skip_match
4845 || got_int
4846 || got_quit
4847 || !(do_all || do_again)
4848 || (sub_firstline[matchcol] == NUL && nmatch <= 1
4849 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 nmatch = -1;
4851
4852 /*
4853 * Replace the line in the buffer when needed. This is
4854 * skipped when there are more matches.
4855 * The check for nmatch_tl is needed for when multi-line
4856 * matching must replace the lines before trying to do another
4857 * match, otherwise "\@<=" won't work.
4858 * When asking the user we like to show the already replaced
4859 * text, but don't do it when "\<@=" or "\<@!" is used, it
4860 * changes what matches.
4861 */
4862 if (lastone
4863 || (do_ask && !re_lookbehind(regmatch.regprog))
4864 || nmatch_tl > 0
4865 || (nmatch = vim_regexec_multi(&regmatch, curwin,
4866 curbuf, sub_firstlnum, matchcol)) == 0)
4867 {
4868 if (new_start != NULL)
4869 {
4870 /*
4871 * Copy the rest of the line, that didn't match.
4872 * "matchcol" has to be adjusted, we use the end of
4873 * the line as reference, because the substitute may
4874 * have changed the number of characters. Same for
4875 * "prev_matchcol".
4876 */
4877 STRCAT(new_start, sub_firstline + copycol);
4878 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4879 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4880 - prev_matchcol;
4881
4882 if (u_savesub(lnum) != OK)
4883 break;
4884 ml_replace(lnum, new_start, TRUE);
4885
4886 if (nmatch_tl > 0)
4887 {
4888 /*
4889 * Matched lines have now been substituted and are
4890 * useless, delete them. The part after the match
4891 * has been appended to new_start, we don't need
4892 * it in the buffer.
4893 */
4894 ++lnum;
4895 if (u_savedel(lnum, nmatch_tl) != OK)
4896 break;
4897 for (i = 0; i < nmatch_tl; ++i)
4898 ml_delete(lnum, (int)FALSE);
4899 mark_adjust(lnum, lnum + nmatch_tl - 1,
4900 (long)MAXLNUM, -nmatch_tl);
4901 if (do_ask)
4902 deleted_lines(lnum, nmatch_tl);
4903 --lnum;
4904 line2 -= nmatch_tl; /* nr of lines decreases */
4905 nmatch_tl = 0;
4906 }
4907
4908 /* When asking, undo is saved each time, must also set
4909 * changed flag each time. */
4910 if (do_ask)
4911 changed_bytes(lnum, 0);
4912 else
4913 {
4914 if (first_line == 0)
4915 first_line = lnum;
4916 last_line = lnum + 1;
4917 }
4918
4919 sub_firstlnum = lnum;
4920 vim_free(sub_firstline); /* free the temp buffer */
4921 sub_firstline = new_start;
4922 new_start = NULL;
4923 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4924 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4925 - prev_matchcol;
4926 copycol = 0;
4927 }
4928 if (nmatch == -1 && !lastone)
4929 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
4930 sub_firstlnum, matchcol);
4931
4932 /*
4933 * 5. break if there isn't another match in this line
4934 */
4935 if (nmatch <= 0)
4936 break;
4937 }
4938
4939 line_breakcheck();
4940 }
4941
4942 if (did_sub)
4943 ++sub_nlines;
4944 vim_free(sub_firstline); /* free the copy of the original line */
4945 sub_firstline = NULL;
4946 }
4947
4948 line_breakcheck();
4949 }
4950
4951 if (first_line != 0)
4952 {
4953 /* Need to subtract the number of added lines from "last_line" to get
4954 * the line number before the change (same as adding the number of
4955 * deleted lines). */
4956 i = curbuf->b_ml.ml_line_count - old_line_count;
4957 changed_lines(first_line, 0, last_line - i, i);
4958 }
4959
4960outofmem:
4961 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004962
4963 /* ":s/pat//n" doesn't move the cursor */
4964 if (do_count)
4965 curwin->w_cursor = old_cursor;
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (sub_nsubs)
4968 {
4969 /* Set the '[ and '] marks. */
4970 curbuf->b_op_start.lnum = eap->line1;
4971 curbuf->b_op_end.lnum = line2;
4972 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4973
4974 if (!global_busy)
4975 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004976 if (endcolumn)
4977 coladvance((colnr_T)MAXCOL);
4978 else
4979 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004980 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 MSG("");
4982 }
4983 else
4984 global_need_beginline = TRUE;
4985 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004986 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else if (!global_busy)
4989 {
4990 if (got_int) /* interrupted */
4991 EMSG(_(e_interr));
4992 else if (got_match) /* did find something but nothing substituted */
4993 MSG("");
4994 else if (do_error) /* nothing found */
4995 EMSG2(_(e_patnotf2), get_search_pat());
4996 }
4997
4998 vim_free(regmatch.regprog);
4999}
5000
5001/*
5002 * Give message for number of substitutions.
5003 * Can also be used after a ":global" command.
5004 * Return TRUE if a message was given.
5005 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005006 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00005007do_sub_msg(count_only)
5008 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
Bram Moolenaar555b2802005-05-19 21:08:39 +00005010 int len = 0;
5011
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 /*
5013 * Only report substitutions when:
5014 * - more than 'report' substitutions
5015 * - command was typed by user, or number of changed lines > 'report'
5016 * - giving messages is not disabled by 'lazyredraw'
5017 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005018 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5019 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 && messaging())
5021 {
5022 if (got_int)
Bram Moolenaar555b2802005-05-19 21:08:39 +00005023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar555b2802005-05-19 21:08:39 +00005025 len = STRLEN(msg_buf);
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (sub_nsubs == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00005028 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
5029 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005031 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
Bram Moolenaar05159a02005-02-26 23:04:13 +00005032 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 sub_nsubs);
Bram Moolenaar555b2802005-05-19 21:08:39 +00005034 len = STRLEN(msg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (sub_nlines == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00005036 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
5037 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005039 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
5040 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005043 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return TRUE;
5045 }
5046 if (got_int)
5047 {
5048 EMSG(_(e_interr));
5049 return TRUE;
5050 }
5051 return FALSE;
5052}
5053
5054/*
5055 * Execute a global command of the form:
5056 *
5057 * g/pattern/X : execute X on all lines where pattern matches
5058 * v/pattern/X : execute X on all lines where pattern does not match
5059 *
5060 * where 'X' is an EX command
5061 *
5062 * The command character (as well as the trailing slash) is optional, and
5063 * is assumed to be 'p' if missing.
5064 *
5065 * This is implemented in two passes: first we scan the file for the pattern and
5066 * set a mark for each line that (not) matches. secondly we execute the command
5067 * for each line that has a mark. This is required because after deleting
5068 * lines we do not know where to search for the next match.
5069 */
5070 void
5071ex_global(eap)
5072 exarg_T *eap;
5073{
5074 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005075 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 int type; /* first char of cmd: 'v' or 'g' */
5077 char_u *cmd; /* command argument */
5078
5079 char_u delim; /* delimiter, normally '/' */
5080 char_u *pat;
5081 regmmatch_T regmatch;
5082 int match;
5083 int which_pat;
5084
5085 if (global_busy)
5086 {
5087 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5088 return;
5089 }
5090
5091 if (eap->forceit) /* ":global!" is like ":vglobal" */
5092 type = 'v';
5093 else
5094 type = *eap->cmd;
5095 cmd = eap->arg;
5096 which_pat = RE_LAST; /* default: use last used regexp */
5097 sub_nsubs = 0;
5098 sub_nlines = 0;
5099
5100 /*
5101 * undocumented vi feature:
5102 * "\/" and "\?": use previous search pattern.
5103 * "\&": use previous substitute pattern.
5104 */
5105 if (*cmd == '\\')
5106 {
5107 ++cmd;
5108 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5109 {
5110 EMSG(_(e_backslash));
5111 return;
5112 }
5113 if (*cmd == '&')
5114 which_pat = RE_SUBST; /* use previous substitute pattern */
5115 else
5116 which_pat = RE_SEARCH; /* use previous search pattern */
5117 ++cmd;
5118 pat = (char_u *)"";
5119 }
5120 else if (*cmd == NUL)
5121 {
5122 EMSG(_("E148: Regular expression missing from global"));
5123 return;
5124 }
5125 else
5126 {
5127 delim = *cmd; /* get the delimiter */
5128 if (delim)
5129 ++cmd; /* skip delimiter if there is one */
5130 pat = cmd; /* remember start of pattern */
5131 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5132 if (cmd[0] == delim) /* end delimiter found */
5133 *cmd++ = NUL; /* replace it with a NUL */
5134 }
5135
5136#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5137 if (p_altkeymap && curwin->w_p_rl)
5138 lrFswap(pat,0);
5139#endif
5140
5141 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5142 {
5143 EMSG(_(e_invcmd));
5144 return;
5145 }
5146
5147 /*
5148 * pass 1: set marks for each (not) matching line
5149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5151 {
5152 /* a match on this line? */
5153 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
5154 if ((type == 'g' && match) || (type == 'v' && !match))
5155 {
5156 ml_setmarked(lnum);
5157 ndone++;
5158 }
5159 line_breakcheck();
5160 }
5161
5162 /*
5163 * pass 2: execute the command for each line that has been marked
5164 */
5165 if (got_int)
5166 MSG(_(e_interr));
5167 else if (ndone == 0)
5168 {
5169 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005170 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005172 smsg((char_u *)_(e_patnotf2), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174 else
5175 global_exe(cmd);
5176
5177 ml_clearmarked(); /* clear rest of the marks */
5178 vim_free(regmatch.regprog);
5179}
5180
5181/*
5182 * Execute "cmd" on lines marked with ml_setmarked().
5183 */
5184 void
5185global_exe(cmd)
5186 char_u *cmd;
5187{
5188 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5189 linenr_T lnum; /* line number according to old situation */
5190
5191 /*
5192 * Set current position only once for a global command.
5193 * If global_busy is set, setpcmark() will not do anything.
5194 * If there is an error, global_busy will be incremented.
5195 */
5196 setpcmark();
5197
5198 /* When the command writes a message, don't overwrite the command. */
5199 msg_didout = TRUE;
5200
5201 global_need_beginline = FALSE;
5202 global_busy = 1;
5203 old_lcount = curbuf->b_ml.ml_line_count;
5204 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5205 {
5206 curwin->w_cursor.lnum = lnum;
5207 curwin->w_cursor.col = 0;
5208 if (*cmd == NUL || *cmd == '\n')
5209 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5210 else
5211 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5212 ui_breakcheck();
5213 }
5214
5215 global_busy = 0;
5216 if (global_need_beginline)
5217 beginline(BL_WHITE | BL_FIX);
5218 else
5219 check_cursor(); /* cursor may be beyond the end of the line */
5220
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005221 /* the cursor may not have moved in the text but a change in a previous
5222 * line may move it on the screen */
5223 changed_line_abv_curs();
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 /* If it looks like no message was written, allow overwriting the
5226 * command with the report for number of changes. */
5227 if (msg_col == 0 && msg_scrolled == 0)
5228 msg_didout = FALSE;
5229
5230 /* If subsitutes done, report number of substitues, otherwise report
5231 * number of extra or deleted lines. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005232 if (!do_sub_msg(FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5234}
5235
5236#ifdef FEAT_VIMINFO
5237 int
5238read_viminfo_sub_string(virp, force)
5239 vir_T *virp;
5240 int force;
5241{
5242 if (old_sub != NULL && force)
5243 vim_free(old_sub);
5244 if (force || old_sub == NULL)
5245 old_sub = viminfo_readstring(virp, 1, TRUE);
5246 return viminfo_readline(virp);
5247}
5248
5249 void
5250write_viminfo_sub_string(fp)
5251 FILE *fp;
5252{
5253 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5254 {
5255 fprintf(fp, _("\n# Last Substitute String:\n$"));
5256 viminfo_writestring(fp, old_sub);
5257 }
5258}
5259#endif /* FEAT_VIMINFO */
5260
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005261#if defined(EXITFREE) || defined(PROTO)
5262 void
5263free_old_sub()
5264{
5265 vim_free(old_sub);
5266}
5267#endif
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5270/*
5271 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005272 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005274 int
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005275prepare_tagpreview(undo_sync)
5276 int undo_sync; /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 win_T *wp;
5279
5280# ifdef FEAT_GUI
5281 need_mouse_correct = TRUE;
5282# endif
5283
5284 /*
5285 * If there is already a preview window open, use that one.
5286 */
5287 if (!curwin->w_p_pvw)
5288 {
5289 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5290 if (wp->w_p_pvw)
5291 break;
5292 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005293 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 else
5295 {
5296 /*
5297 * There is no preview window open yet. Create one.
5298 */
5299 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5300 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005301 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 curwin->w_p_pvw = TRUE;
5303 curwin->w_p_wfh = TRUE;
5304# ifdef FEAT_SCROLLBIND
5305 curwin->w_p_scb = FALSE; /* don't take over 'scrollbind' */
5306# endif
5307# ifdef FEAT_DIFF
5308 curwin->w_p_diff = FALSE; /* no 'diff' */
5309# endif
5310# ifdef FEAT_FOLDING
5311 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5312# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005313 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005316 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317}
5318
5319#endif
5320
5321
5322/*
5323 * ":help": open a read-only window on a help file
5324 */
5325 void
5326ex_help(eap)
5327 exarg_T *eap;
5328{
5329 char_u *arg;
5330 char_u *tag;
5331 FILE *helpfd; /* file descriptor of help file */
5332 int n;
5333 int i;
5334#ifdef FEAT_WINDOWS
5335 win_T *wp;
5336#endif
5337 int num_matches;
5338 char_u **matches;
5339 char_u *p;
5340 int empty_fnum = 0;
5341 int alt_fnum = 0;
5342 buf_T *buf;
5343#ifdef FEAT_MULTI_LANG
5344 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005345 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#endif
5347
5348 if (eap != NULL)
5349 {
5350 /*
5351 * A ":help" command ends at the first LF, or at a '|' that is
5352 * followed by some text. Set nextcmd to the following command.
5353 */
5354 for (arg = eap->arg; *arg; ++arg)
5355 {
5356 if (*arg == '\n' || *arg == '\r'
5357 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5358 {
5359 *arg++ = NUL;
5360 eap->nextcmd = arg;
5361 break;
5362 }
5363 }
5364 arg = eap->arg;
5365
5366 if (eap->forceit && *arg == NUL)
5367 {
5368 EMSG(_("E478: Don't panic!"));
5369 return;
5370 }
5371
5372 if (eap->skip) /* not executing commands */
5373 return;
5374 }
5375 else
5376 arg = (char_u *)"";
5377
5378 /* remove trailing blanks */
5379 p = arg + STRLEN(arg) - 1;
5380 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5381 *p-- = NUL;
5382
5383#ifdef FEAT_MULTI_LANG
5384 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005385 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386#endif
5387
5388 /* When no argument given go to the index. */
5389 if (*arg == NUL)
5390 arg = (char_u *)"help.txt";
5391
5392 /*
5393 * Check if there is a match for the argument.
5394 */
5395 n = find_help_tags(arg, &num_matches, &matches,
5396 eap != NULL && eap->forceit);
5397
5398 i = 0;
5399#ifdef FEAT_MULTI_LANG
5400 if (n != FAIL && lang != NULL)
5401 /* Find first item with the requested language. */
5402 for (i = 0; i < num_matches; ++i)
5403 {
5404 len = STRLEN(matches[i]);
5405 if (len > 3 && matches[i][len - 3] == '@'
5406 && STRICMP(matches[i] + len - 2, lang) == 0)
5407 break;
5408 }
5409#endif
5410 if (i >= num_matches || n == FAIL)
5411 {
5412#ifdef FEAT_MULTI_LANG
5413 if (lang != NULL)
5414 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5415 else
5416#endif
5417 EMSG2(_("E149: Sorry, no help for %s"), arg);
5418 if (n != FAIL)
5419 FreeWild(num_matches, matches);
5420 return;
5421 }
5422
5423 /* The first match (in the requested language) is the best match. */
5424 tag = vim_strsave(matches[i]);
5425 FreeWild(num_matches, matches);
5426
5427#ifdef FEAT_GUI
5428 need_mouse_correct = TRUE;
5429#endif
5430
5431 /*
5432 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005433 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005435 if (!curwin->w_buffer->b_help
5436#ifdef FEAT_WINDOWS
5437 || cmdmod.tab != 0
5438#endif
5439 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
5441#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005442 if (cmdmod.tab != 0)
5443 wp = NULL;
5444 else
5445 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5446 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5449 win_enter(wp, TRUE);
5450 else
5451#endif
5452 {
5453 /*
5454 * There is no help window yet.
5455 * Try to open the file specified by the "helpfile" option.
5456 */
5457 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5458 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005459 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 goto erret;
5461 }
5462 fclose(helpfd);
5463
5464#ifdef FEAT_WINDOWS
5465 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005466 * specified, the current window is vertically split and
5467 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 n = WSP_HELP;
5469# ifdef FEAT_VERTSPLIT
5470 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005471 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 n |= WSP_TOP;
5473# endif
5474 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005475 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476#else
5477 /* use current window */
5478 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482#ifdef FEAT_WINDOWS
5483 if (curwin->w_height < p_hh)
5484 win_setheight((int)p_hh);
5485#endif
5486
5487 /*
5488 * Open help file (do_ecmd() will set b_help flag, readfile() will
5489 * set b_p_ro flag).
5490 * Set the alternate file to the previously edited file.
5491 */
5492 alt_fnum = curbuf->b_fnum;
5493 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
5494 ECMD_HIDE + ECMD_SET_HELP);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005495 if (!cmdmod.keepalt)
5496 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 empty_fnum = curbuf->b_fnum;
5498 }
5499 }
5500
5501 if (!p_im)
5502 restart_edit = 0; /* don't want insert mode in help file */
5503
5504 if (tag != NULL)
5505 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5506
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005507 /* Delete the empty buffer if we're not using it. Careful: autocommands
5508 * may have jumped to another window, check that the buffer is not in a
5509 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5511 {
5512 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005513 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 wipe_buffer(buf, TRUE);
5515 }
5516
5517 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005518 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 curwin->w_alt_fnum = alt_fnum;
5520
5521erret:
5522 vim_free(tag);
5523}
5524
5525
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005526#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5527/*
5528 * In an argument search for a language specifiers in the form "@xx".
5529 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5530 * Returns NULL if not found.
5531 */
5532 char_u *
5533check_help_lang(arg)
5534 char_u *arg;
5535{
5536 int len = STRLEN(arg);
5537
5538 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
5539 && ASCII_ISALPHA(arg[len - 1]))
5540 {
5541 arg[len - 3] = NUL; /* remove the '@' */
5542 return arg + len - 2;
5543 }
5544 return NULL;
5545}
5546#endif
5547
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548/*
5549 * Return a heuristic indicating how well the given string matches. The
5550 * smaller the number, the better the match. This is the order of priorities,
5551 * from best match to worst match:
5552 * - Match with least alpha-numeric characters is better.
5553 * - Match with least total characters is better.
5554 * - Match towards the start is better.
5555 * - Match starting with "+" is worse (feature instead of command)
5556 * Assumption is made that the matched_string passed has already been found to
5557 * match some string for which help is requested. webb.
5558 */
5559 int
5560help_heuristic(matched_string, offset, wrong_case)
5561 char_u *matched_string;
5562 int offset; /* offset for match */
5563 int wrong_case; /* no matching case */
5564{
5565 int num_letters;
5566 char_u *p;
5567
5568 num_letters = 0;
5569 for (p = matched_string; *p; p++)
5570 if (ASCII_ISALNUM(*p))
5571 num_letters++;
5572
5573 /*
5574 * Multiply the number of letters by 100 to give it a much bigger
5575 * weighting than the number of characters.
5576 * If there only is a match while ignoring case, add 5000.
5577 * If the match starts in the middle of a word, add 10000 to put it
5578 * somewhere in the last half.
5579 * If the match is more than 2 chars from the start, multiply by 200 to
5580 * put it after matches at the start.
5581 */
5582 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
5583 && ASCII_ISALNUM(matched_string[offset - 1]))
5584 offset += 10000;
5585 else if (offset > 2)
5586 offset *= 200;
5587 if (wrong_case)
5588 offset += 5000;
5589 /* Features are less interesting than the subjects themselves, but "+"
5590 * alone is not a feature. */
5591 if (matched_string[0] == '+' && matched_string[1] != NUL)
5592 offset += 100;
5593 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
5594}
5595
5596/*
5597 * Compare functions for qsort() below, that checks the help heuristics number
5598 * that has been put after the tagname by find_tags().
5599 */
5600 static int
5601#ifdef __BORLANDC__
5602_RTLENTRYF
5603#endif
5604help_compare(s1, s2)
5605 const void *s1;
5606 const void *s2;
5607{
5608 char *p1;
5609 char *p2;
5610
5611 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
5612 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
5613 return strcmp(p1, p2);
5614}
5615
5616/*
5617 * Find all help tags matching "arg", sort them and return in matches[], with
5618 * the number of matches in num_matches.
5619 * The matches will be sorted with a "best" match algorithm.
5620 * When "keep_lang" is TRUE try keeping the language of the current buffer.
5621 */
5622 int
5623find_help_tags(arg, num_matches, matches, keep_lang)
5624 char_u *arg;
5625 int *num_matches;
5626 char_u ***matches;
5627 int keep_lang;
5628{
5629 char_u *s, *d;
5630 int i;
5631 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005632 "/*", "/\\*", "\"*", "**",
5633 "/\\(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005634 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005635 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 "[count]", "[quotex]", "[range]",
5637 "[pattern]", "\\|", "\\%$"};
5638 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005639 "/star", "/\\\\star", "quotestar", "starstar",
5640 "/\\\\(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005642 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 "\\[count]", "\\[quotex]", "\\[range]",
5644 "\\[pattern]", "\\\\bar", "/\\\\%\\$"};
5645 int flags;
5646
5647 d = IObuff; /* assume IObuff is long enough! */
5648
5649 /*
5650 * Recognize a few exceptions to the rule. Some strings that contain '*'
5651 * with "star". Otherwise '*' is recognized as a wildcard.
5652 */
5653 for (i = sizeof(mtable) / sizeof(char *); --i >= 0; )
5654 if (STRCMP(arg, mtable[i]) == 0)
5655 {
5656 STRCPY(d, rtable[i]);
5657 break;
5658 }
5659
5660 if (i < 0) /* no match in table */
5661 {
5662 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
5663 * Also replace "\%^" and "\%(", they match every tag too.
5664 * Also "\zs", "\z1", etc.
5665 * Also "\@<", "\@=", "\@<=", etc.
5666 * And also "\_$" and "\_^". */
5667 if (arg[0] == '\\'
5668 && ((arg[1] != NUL && arg[2] == NUL)
5669 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
5670 && arg[2] != NUL)))
5671 {
5672 STRCPY(d, "/\\\\");
5673 STRCPY(d + 3, arg + 1);
5674 /* Check for "/\\_$", should be "/\\_\$" */
5675 if (d[3] == '_' && d[4] == '$')
5676 STRCPY(d + 4, "\\$");
5677 }
5678 else
5679 {
5680 /* replace "[:...:]" with "\[:...:]"; "[+...]" with "\[++...]" */
5681 if (arg[0] == '[' && (arg[1] == ':'
5682 || (arg[1] == '+' && arg[2] == '+')))
5683 *d++ = '\\';
5684
5685 for (s = arg; *s; ++s)
5686 {
5687 /*
5688 * Replace "|" with "bar" and '"' with "quote" to match the name of
5689 * the tags for these commands.
5690 * Replace "*" with ".*" and "?" with "." to match command line
5691 * completion.
5692 * Insert a backslash before '~', '$' and '.' to avoid their
5693 * special meaning.
5694 */
5695 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
5696 break;
5697 switch (*s)
5698 {
5699 case '|': STRCPY(d, "bar");
5700 d += 3;
5701 continue;
5702 case '"': STRCPY(d, "quote");
5703 d += 5;
5704 continue;
5705 case '*': *d++ = '.';
5706 break;
5707 case '?': *d++ = '.';
5708 continue;
5709 case '$':
5710 case '.':
5711 case '~': *d++ = '\\';
5712 break;
5713 }
5714
5715 /*
5716 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
5717 * ":help i_^_CTRL-D" work.
5718 * Insert '-' before and after "CTRL-X" when applicable.
5719 */
5720 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
5721 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
5722 {
5723 if (d > IObuff && d[-1] != '_')
5724 *d++ = '_'; /* prepend a '_' */
5725 STRCPY(d, "CTRL-");
5726 d += 5;
5727 if (*s < ' ')
5728 {
5729#ifdef EBCDIC
5730 *d++ = CtrlChar(*s);
5731#else
5732 *d++ = *s + '@';
5733#endif
5734 if (d[-1] == '\\')
5735 *d++ = '\\'; /* double a backslash */
5736 }
5737 else
5738 *d++ = *++s;
5739 if (s[1] != NUL && s[1] != '_')
5740 *d++ = '_'; /* append a '_' */
5741 continue;
5742 }
5743 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
5744 *d++ = '\\';
5745
5746 /*
5747 * Insert a backslash before a backslash after a slash, for search
5748 * pattern tags: "/\|" --> "/\\|".
5749 */
5750 else if (s[0] == '\\' && s[1] != '\\'
5751 && *arg == '/' && s == arg + 1)
5752 *d++ = '\\';
5753
5754 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
5755 * "CTRL-\_CTRL-N" */
5756 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
5757 {
5758 STRCPY(d, "CTRL-\\\\");
5759 d += 7;
5760 s += 6;
5761 }
5762
5763 *d++ = *s;
5764
5765 /*
5766 * If tag starts with ', toss everything after a second '. Fixes
5767 * CTRL-] on 'option'. (would include the trailing '.').
5768 */
5769 if (*s == '\'' && s > arg && *arg == '\'')
5770 break;
5771 }
5772 *d = NUL;
5773 }
5774 }
5775
5776 *matches = (char_u **)"";
5777 *num_matches = 0;
5778 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
5779 if (keep_lang)
5780 flags |= TAG_KEEP_LANG;
5781 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
5782 && *num_matches > 0)
5783 /* Sort the matches found on the heuristic number that is after the
5784 * tag name. */
5785 qsort((void *)*matches, (size_t)*num_matches,
5786 sizeof(char_u *), help_compare);
5787 return OK;
5788}
5789
5790/*
5791 * After reading a help file: May cleanup a help buffer when syntax
5792 * highlighting is not used.
5793 */
5794 void
5795fix_help_buffer()
5796{
5797 linenr_T lnum;
5798 char_u *line;
5799 int in_example = FALSE;
5800 int len;
5801 char_u *p;
5802 char_u *rt;
5803 int mustfree;
5804
5805 /* set filetype to "help". */
5806 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
5807
5808#ifdef FEAT_SYN_HL
5809 if (!syntax_present(curbuf))
5810#endif
5811 {
5812 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5813 {
5814 line = ml_get_buf(curbuf, lnum, FALSE);
5815 len = (int)STRLEN(line);
5816 if (in_example && len > 0 && !vim_iswhite(line[0]))
5817 {
5818 /* End of example: non-white or '<' in first column. */
5819 if (line[0] == '<')
5820 {
5821 /* blank-out a '<' in the first column */
5822 line = ml_get_buf(curbuf, lnum, TRUE);
5823 line[0] = ' ';
5824 }
5825 in_example = FALSE;
5826 }
5827 if (!in_example && len > 0)
5828 {
5829 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
5830 {
5831 /* blank-out a '>' in the last column (start of example) */
5832 line = ml_get_buf(curbuf, lnum, TRUE);
5833 line[len - 1] = ' ';
5834 in_example = TRUE;
5835 }
5836 else if (line[len - 1] == '~')
5837 {
5838 /* blank-out a '~' at the end of line (header marker) */
5839 line = ml_get_buf(curbuf, lnum, TRUE);
5840 line[len - 1] = ' ';
5841 }
5842 }
5843 }
5844 }
5845
5846 /*
5847 * In the "help.txt" file, add the locally added help files.
5848 * This uses the very first line in the help file.
5849 */
5850 if (fnamecmp(gettail(curbuf->b_fname), "help.txt") == 0)
5851 {
5852 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
5853 {
5854 line = ml_get_buf(curbuf, lnum, FALSE);
5855 if (strstr((char *)line, "*local-additions*") != NULL)
5856 {
5857 /* Go through all directories in 'runtimepath', skipping
5858 * $VIMRUNTIME. */
5859 p = p_rtp;
5860 while (*p != NUL)
5861 {
5862 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5863 mustfree = FALSE;
5864 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
5865 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
5866 {
5867 int fcount;
5868 char_u **fnames;
5869 FILE *fd;
5870 char_u *s;
5871 int fi;
5872#ifdef FEAT_MBYTE
5873 vimconv_T vc;
5874 char_u *cp;
5875#endif
5876
5877 /* Find all "doc/ *.txt" files in this directory. */
5878 add_pathsep(NameBuff);
5879 STRCAT(NameBuff, "doc/*.txt");
5880 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5881 &fnames, EW_FILE|EW_SILENT) == OK
5882 && fcount > 0)
5883 {
5884 for (fi = 0; fi < fcount; ++fi)
5885 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005886 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 if (fd != NULL)
5888 {
5889 vim_fgets(IObuff, IOSIZE, fd);
5890 if (IObuff[0] == '*'
5891 && (s = vim_strchr(IObuff + 1, '*'))
5892 != NULL)
5893 {
5894#ifdef FEAT_MBYTE
5895 int this_utf = MAYBE;
5896#endif
5897 /* Change tag definition to a
5898 * reference and remove <CR>/<NL>. */
5899 IObuff[0] = '|';
5900 *s = '|';
5901 while (*s != NUL)
5902 {
5903 if (*s == '\r' || *s == '\n')
5904 *s = NUL;
5905#ifdef FEAT_MBYTE
5906 /* The text is utf-8 when a byte
5907 * above 127 is found and no
5908 * illegal byte sequence is found.
5909 */
5910 if (*s >= 0x80 && this_utf != FALSE)
5911 {
5912 int l;
5913
5914 this_utf = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005915 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (l == 1)
5917 this_utf = FALSE;
5918 s += l - 1;
5919 }
5920#endif
5921 ++s;
5922 }
5923#ifdef FEAT_MBYTE
5924 /* The help file is latin1 or utf-8;
5925 * conversion to the current
5926 * 'encoding' may be required. */
5927 vc.vc_type = CONV_NONE;
5928 convert_setup(&vc, (char_u *)(
5929 this_utf == TRUE ? "utf-8"
5930 : "latin1"), p_enc);
5931 if (vc.vc_type == CONV_NONE)
5932 /* No conversion needed. */
5933 cp = IObuff;
5934 else
5935 {
5936 /* Do the conversion. If it fails
5937 * use the unconverted text. */
5938 cp = string_convert(&vc, IObuff,
5939 NULL);
5940 if (cp == NULL)
5941 cp = IObuff;
5942 }
5943 convert_setup(&vc, NULL, NULL);
5944
5945 ml_append(lnum, cp, (colnr_T)0, FALSE);
5946 if (cp != IObuff)
5947 vim_free(cp);
5948#else
5949 ml_append(lnum, IObuff, (colnr_T)0,
5950 FALSE);
5951#endif
5952 ++lnum;
5953 }
5954 fclose(fd);
5955 }
5956 }
5957 FreeWild(fcount, fnames);
5958 }
5959 }
5960 if (mustfree)
5961 vim_free(rt);
5962 }
5963 break;
5964 }
5965 }
5966 }
5967}
5968
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005969/*
5970 * ":exusage"
5971 */
5972/*ARGSUSED*/
5973 void
5974ex_exusage(eap)
5975 exarg_T *eap;
5976{
5977 do_cmdline_cmd((char_u *)"help ex-cmd-index");
5978}
5979
5980/*
5981 * ":viusage"
5982 */
5983/*ARGSUSED*/
5984 void
5985ex_viusage(eap)
5986 exarg_T *eap;
5987{
5988 do_cmdline_cmd((char_u *)"help normal-index");
5989}
5990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#if defined(FEAT_EX_EXTRA) || defined(PROTO)
5992static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang));
5993
5994/*
5995 * ":helptags"
5996 */
5997 void
5998ex_helptags(eap)
5999 exarg_T *eap;
6000{
6001 garray_T ga;
6002 int i, j;
6003 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006004#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 char_u ext[5];
6008 char_u fname[8];
6009 int filecount;
6010 char_u **files;
6011
6012 if (!mch_isdir(eap->arg))
6013 {
6014 EMSG2(_("E150: Not a directory: %s"), eap->arg);
6015 return;
6016 }
6017
6018#ifdef FEAT_MULTI_LANG
6019 /* Get a list of all files in the directory. */
6020 STRCPY(NameBuff, eap->arg);
6021 add_pathsep(NameBuff);
6022 STRCAT(NameBuff, "*");
6023 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6024 EW_FILE|EW_SILENT) == FAIL
6025 || filecount == 0)
6026 {
6027 EMSG2("E151: No match: %s", NameBuff);
6028 return;
6029 }
6030
6031 /* Go over all files in the directory to find out what languages are
6032 * present. */
6033 ga_init2(&ga, 1, 10);
6034 for (i = 0; i < filecount; ++i)
6035 {
6036 len = STRLEN(files[i]);
6037 if (len > 4)
6038 {
6039 if (STRICMP(files[i] + len - 4, ".txt") == 0)
6040 {
6041 /* ".txt" -> language "en" */
6042 lang[0] = 'e';
6043 lang[1] = 'n';
6044 }
6045 else if (files[i][len - 4] == '.'
6046 && ASCII_ISALPHA(files[i][len - 3])
6047 && ASCII_ISALPHA(files[i][len - 2])
6048 && TOLOWER_ASC(files[i][len - 1]) == 'x')
6049 {
6050 /* ".abx" -> language "ab" */
6051 lang[0] = TOLOWER_ASC(files[i][len - 3]);
6052 lang[1] = TOLOWER_ASC(files[i][len - 2]);
6053 }
6054 else
6055 continue;
6056
6057 /* Did we find this language already? */
6058 for (j = 0; j < ga.ga_len; j += 2)
6059 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
6060 break;
6061 if (j == ga.ga_len)
6062 {
6063 /* New language, add it. */
6064 if (ga_grow(&ga, 2) == FAIL)
6065 break;
6066 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
6067 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
6069 }
6070 }
6071
6072 /*
6073 * Loop over the found languages to generate a tags file for each one.
6074 */
6075 for (j = 0; j < ga.ga_len; j += 2)
6076 {
6077 STRCPY(fname, "tags-xx");
6078 fname[5] = ((char_u *)ga.ga_data)[j];
6079 fname[6] = ((char_u *)ga.ga_data)[j + 1];
6080 if (fname[5] == 'e' && fname[6] == 'n')
6081 {
6082 /* English is an exception: use ".txt" and "tags". */
6083 fname[4] = NUL;
6084 STRCPY(ext, ".txt");
6085 }
6086 else
6087 {
6088 /* Language "ab" uses ".abx" and "tags-ab". */
6089 STRCPY(ext, ".xxx");
6090 ext[1] = fname[5];
6091 ext[2] = fname[6];
6092 }
6093 helptags_one(eap->arg, ext, fname);
6094 }
6095
6096 ga_clear(&ga);
6097 FreeWild(filecount, files);
6098
6099#else
6100 /* No language support, just use "*.txt" and "tags". */
6101 helptags_one(eap->arg, (char_u *)".txt", (char_u *)"tags");
6102#endif
6103}
6104
6105 static void
6106helptags_one(dir, ext, tagfname)
6107 char_u *dir; /* doc directory */
6108 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
6109 char_u *tagfname; /* "tags" for English, "tags-it" for Italian. */
6110{
6111 FILE *fd_tags;
6112 FILE *fd;
6113 garray_T ga;
6114 int filecount;
6115 char_u **files;
6116 char_u *p1, *p2;
6117 int fi;
6118 char_u *s;
6119 int i;
6120 char_u *fname;
6121# ifdef FEAT_MBYTE
6122 int utf8 = MAYBE;
6123 int this_utf8;
6124 int firstline;
6125 int mix = FALSE; /* detected mixed encodings */
6126# endif
6127
6128 /*
6129 * Find all *.txt files.
6130 */
6131 STRCPY(NameBuff, dir);
6132 add_pathsep(NameBuff);
6133 STRCAT(NameBuff, "*");
6134 STRCAT(NameBuff, ext);
6135 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6136 EW_FILE|EW_SILENT) == FAIL
6137 || filecount == 0)
6138 {
6139 if (!got_int)
6140 EMSG2("E151: No match: %s", NameBuff);
6141 return;
6142 }
6143
6144 /*
6145 * Open the tags file for writing.
6146 * Do this before scanning through all the files.
6147 */
6148 STRCPY(NameBuff, dir);
6149 add_pathsep(NameBuff);
6150 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006151 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (fd_tags == NULL)
6153 {
6154 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6155 FreeWild(filecount, files);
6156 return;
6157 }
6158
6159 /*
6160 * If generating tags for "$VIMRUNTIME/doc" add the "help-tags" tag.
6161 */
6162 ga_init2(&ga, (int)sizeof(char_u *), 100);
6163 if (fullpathcmp((char_u *)"$VIMRUNTIME/doc", dir, FALSE) == FPC_SAME)
6164 {
6165 if (ga_grow(&ga, 1) == FAIL)
6166 got_int = TRUE;
6167 else
6168 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006169 s = alloc(18 + STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (s == NULL)
6171 got_int = TRUE;
6172 else
6173 {
6174 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6175 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6176 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178 }
6179 }
6180
6181 /*
6182 * Go over all the files and extract the tags.
6183 */
6184 for (fi = 0; fi < filecount && !got_int; ++fi)
6185 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006186 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (fd == NULL)
6188 {
6189 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6190 continue;
6191 }
6192 fname = gettail(files[fi]);
6193
6194# ifdef FEAT_MBYTE
6195 firstline = TRUE;
6196# endif
6197 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6198 {
6199# ifdef FEAT_MBYTE
6200 if (firstline)
6201 {
6202 /* Detect utf-8 file by a non-ASCII char in the first line. */
6203 this_utf8 = MAYBE;
6204 for (s = IObuff; *s != NUL; ++s)
6205 if (*s >= 0x80)
6206 {
6207 int l;
6208
6209 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006210 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 if (l == 1)
6212 {
6213 /* Illegal UTF-8 byte sequence. */
6214 this_utf8 = FALSE;
6215 break;
6216 }
6217 s += l - 1;
6218 }
6219 if (this_utf8 == MAYBE) /* only ASCII characters found */
6220 this_utf8 = FALSE;
6221 if (utf8 == MAYBE) /* first file */
6222 utf8 = this_utf8;
6223 else if (utf8 != this_utf8)
6224 {
6225 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6226 mix = !got_int;
6227 got_int = TRUE;
6228 }
6229 firstline = FALSE;
6230 }
6231# endif
6232 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6233 while (p1 != NULL)
6234 {
6235 p2 = vim_strchr(p1 + 1, '*'); /* find second '*' */
6236 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6237 {
6238 for (s = p1 + 1; s < p2; ++s)
6239 if (*s == ' ' || *s == '\t' || *s == '|')
6240 break;
6241
6242 /*
6243 * Only accept a *tag* when it consists of valid
6244 * characters, there is white space before it and is
6245 * followed by a white character or end-of-line.
6246 */
6247 if (s == p2
6248 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6249 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6250 || s[1] == '\0'))
6251 {
6252 *p2 = '\0';
6253 ++p1;
6254 if (ga_grow(&ga, 1) == FAIL)
6255 {
6256 got_int = TRUE;
6257 break;
6258 }
6259 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6260 if (s == NULL)
6261 {
6262 got_int = TRUE;
6263 break;
6264 }
6265 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6266 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 sprintf((char *)s, "%s\t%s", p1, fname);
6268
6269 /* find next '*' */
6270 p2 = vim_strchr(p2 + 1, '*');
6271 }
6272 }
6273 p1 = p2;
6274 }
6275 line_breakcheck();
6276 }
6277
6278 fclose(fd);
6279 }
6280
6281 FreeWild(filecount, files);
6282
6283 if (!got_int)
6284 {
6285 /*
6286 * Sort the tags.
6287 */
6288 sort_strings((char_u **)ga.ga_data, ga.ga_len);
6289
6290 /*
6291 * Check for duplicates.
6292 */
6293 for (i = 1; i < ga.ga_len; ++i)
6294 {
6295 p1 = ((char_u **)ga.ga_data)[i - 1];
6296 p2 = ((char_u **)ga.ga_data)[i];
6297 while (*p1 == *p2)
6298 {
6299 if (*p2 == '\t')
6300 {
6301 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006302 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6304 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6305 EMSG(NameBuff);
6306 *p2 = '\t';
6307 break;
6308 }
6309 ++p1;
6310 ++p2;
6311 }
6312 }
6313
6314# ifdef FEAT_MBYTE
6315 if (utf8 == TRUE)
6316 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6317# endif
6318
6319 /*
6320 * Write the tags into the file.
6321 */
6322 for (i = 0; i < ga.ga_len; ++i)
6323 {
6324 s = ((char_u **)ga.ga_data)[i];
6325 if (STRNCMP(s, "help-tags", 9) == 0)
6326 /* help-tags entry was added in formatted form */
6327 fprintf(fd_tags, (char *)s);
6328 else
6329 {
6330 fprintf(fd_tags, "%s\t/*", s);
6331 for (p1 = s; *p1 != '\t'; ++p1)
6332 {
6333 /* insert backslash before '\\' and '/' */
6334 if (*p1 == '\\' || *p1 == '/')
6335 putc('\\', fd_tags);
6336 putc(*p1, fd_tags);
6337 }
6338 fprintf(fd_tags, "*\n");
6339 }
6340 }
6341 }
6342#ifdef FEAT_MBYTE
6343 if (mix)
6344 got_int = FALSE; /* continue with other languages */
6345#endif
6346
6347 for (i = 0; i < ga.ga_len; ++i)
6348 vim_free(((char_u **)ga.ga_data)[i]);
6349 ga_clear(&ga);
6350 fclose(fd_tags); /* there is no check for an error... */
6351}
6352#endif
6353
6354#if defined(FEAT_SIGNS) || defined(PROTO)
6355
6356/*
6357 * Struct to hold the sign properties.
6358 */
6359typedef struct sign sign_T;
6360
6361struct sign
6362{
6363 sign_T *sn_next; /* next sign in list */
6364 int sn_typenr; /* type number of sign (negative if not equal
6365 to name) */
6366 char_u *sn_name; /* name of sign */
6367 char_u *sn_icon; /* name of pixmap */
6368#ifdef FEAT_SIGN_ICONS
6369 void *sn_image; /* icon image */
6370#endif
6371 char_u *sn_text; /* text used instead of pixmap */
6372 int sn_line_hl; /* highlight ID for line */
6373 int sn_text_hl; /* highlight ID for text */
6374};
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376static sign_T *first_sign = NULL;
6377static int last_sign_typenr = MAX_TYPENR; /* is decremented */
6378
6379static void sign_list_defined __ARGS((sign_T *sp));
6380
6381/*
6382 * ":sign" command
6383 */
6384 void
6385ex_sign(eap)
6386 exarg_T *eap;
6387{
6388 char_u *arg = eap->arg;
6389 char_u *p;
6390 int idx;
6391 sign_T *sp;
6392 sign_T *sp_prev;
6393 buf_T *buf;
6394 static char *cmds[] = {
6395 "define",
6396#define SIGNCMD_DEFINE 0
6397 "undefine",
6398#define SIGNCMD_UNDEFINE 1
6399 "list",
6400#define SIGNCMD_LIST 2
6401 "place",
6402#define SIGNCMD_PLACE 3
6403 "unplace",
6404#define SIGNCMD_UNPLACE 4
6405 "jump",
6406#define SIGNCMD_JUMP 5
6407#define SIGNCMD_LAST 6
6408 };
6409
6410 /* Parse the subcommand. */
6411 p = skiptowhite(arg);
6412 if (*p != NUL)
6413 *p++ = NUL;
6414 for (idx = 0; ; ++idx)
6415 {
6416 if (idx == SIGNCMD_LAST)
6417 {
6418 EMSG2(_("E160: Unknown sign command: %s"), arg);
6419 return;
6420 }
6421 if (STRCMP(arg, cmds[idx]) == 0)
6422 break;
6423 }
6424 arg = skipwhite(p);
6425
6426 if (idx <= SIGNCMD_LIST)
6427 {
6428 /*
6429 * Define, undefine or list signs.
6430 */
6431 if (idx == SIGNCMD_LIST && *arg == NUL)
6432 {
6433 /* ":sign list": list all defined signs */
6434 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6435 sign_list_defined(sp);
6436 }
6437 else if (*arg == NUL)
6438 EMSG(_("E156: Missing sign name"));
6439 else
6440 {
6441 p = skiptowhite(arg);
6442 if (*p != NUL)
6443 *p++ = NUL;
6444 sp_prev = NULL;
6445 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6446 {
6447 if (STRCMP(sp->sn_name, arg) == 0)
6448 break;
6449 sp_prev = sp;
6450 }
6451 if (idx == SIGNCMD_DEFINE)
6452 {
6453 /* ":sign define {name} ...": define a sign */
6454 if (sp == NULL)
6455 {
6456 /* Allocate a new sign. */
6457 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
6458 if (sp == NULL)
6459 return;
6460 if (sp_prev == NULL)
6461 first_sign = sp;
6462 else
6463 sp_prev->sn_next = sp;
6464 sp->sn_name = vim_strnsave(arg, (int)(p - arg));
6465
6466 /* If the name is a number use that for the typenr,
6467 * otherwise use a negative number. */
6468 if (VIM_ISDIGIT(*arg))
6469 sp->sn_typenr = atoi((char *)arg);
6470 else
6471 {
6472 sign_T *lp;
6473 int start = last_sign_typenr;
6474
6475 for (lp = first_sign; lp != NULL; lp = lp->sn_next)
6476 {
6477 if (lp->sn_typenr == last_sign_typenr)
6478 {
6479 --last_sign_typenr;
6480 if (last_sign_typenr == 0)
6481 last_sign_typenr = MAX_TYPENR;
6482 if (last_sign_typenr == start)
6483 {
6484 EMSG(_("E612: Too many signs defined"));
6485 return;
6486 }
6487 lp = first_sign;
6488 continue;
6489 }
6490 }
6491
6492 sp->sn_typenr = last_sign_typenr--;
6493 if (last_sign_typenr == 0)
6494 last_sign_typenr = MAX_TYPENR; /* wrap around */
6495 }
6496 }
6497
6498 /* set values for a defined sign. */
6499 for (;;)
6500 {
6501 arg = skipwhite(p);
6502 if (*arg == NUL)
6503 break;
6504 p = skiptowhite_esc(arg);
6505 if (STRNCMP(arg, "icon=", 5) == 0)
6506 {
6507 arg += 5;
6508 vim_free(sp->sn_icon);
6509 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
6510 backslash_halve(sp->sn_icon);
6511#ifdef FEAT_SIGN_ICONS
6512 if (gui.in_use)
6513 {
6514 out_flush();
6515 if (sp->sn_image != NULL)
6516 gui_mch_destroy_sign(sp->sn_image);
6517 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6518 }
6519#endif
6520 }
6521 else if (STRNCMP(arg, "text=", 5) == 0)
6522 {
6523 char_u *s;
6524 int cells;
6525 int len;
6526
6527 arg += 5;
6528#ifdef FEAT_MBYTE
6529 /* Count cells and check for non-printable chars */
6530 if (has_mbyte)
6531 {
6532 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006533 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
6535 if (!vim_isprintc((*mb_ptr2char)(s)))
6536 break;
6537 cells += (*mb_ptr2cells)(s);
6538 }
6539 }
6540 else
6541#endif
6542 {
6543 for (s = arg; s < p; ++s)
6544 if (!vim_isprintc(*s))
6545 break;
6546 cells = s - arg;
6547 }
6548 /* Currently must be one or two display cells */
6549 if (s != p || cells < 1 || cells > 2)
6550 {
6551 *p = NUL;
6552 EMSG2(_("E239: Invalid sign text: %s"), arg);
6553 return;
6554 }
6555
6556 vim_free(sp->sn_text);
6557 /* Allocate one byte more if we need to pad up
6558 * with a space. */
6559 len = p - arg + ((cells == 1) ? 1 : 0);
6560 sp->sn_text = vim_strnsave(arg, len);
6561
6562 if (sp->sn_text != NULL && cells == 1)
6563 STRCPY(sp->sn_text + len - 1, " ");
6564 }
6565 else if (STRNCMP(arg, "linehl=", 7) == 0)
6566 {
6567 arg += 7;
6568 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
6569 }
6570 else if (STRNCMP(arg, "texthl=", 7) == 0)
6571 {
6572 arg += 7;
6573 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
6574 }
6575 else
6576 {
6577 EMSG2(_(e_invarg2), arg);
6578 return;
6579 }
6580 }
6581 }
6582 else if (sp == NULL)
6583 EMSG2(_("E155: Unknown sign: %s"), arg);
6584 else if (idx == SIGNCMD_LIST)
6585 /* ":sign list {name}" */
6586 sign_list_defined(sp);
6587 else
6588 {
6589 /* ":sign undefine {name}" */
6590 vim_free(sp->sn_name);
6591 vim_free(sp->sn_icon);
6592#ifdef FEAT_SIGN_ICONS
6593 if (sp->sn_image != NULL)
6594 {
6595 out_flush();
6596 gui_mch_destroy_sign(sp->sn_image);
6597 }
6598#endif
6599 vim_free(sp->sn_text);
6600 if (sp_prev == NULL)
6601 first_sign = sp->sn_next;
6602 else
6603 sp_prev->sn_next = sp->sn_next;
6604 vim_free(sp);
6605 }
6606 }
6607 }
6608 else
6609 {
6610 int id = -1;
6611 linenr_T lnum = -1;
6612 char_u *sign_name = NULL;
6613 char_u *arg1;
6614
6615 if (*arg == NUL)
6616 {
6617 if (idx == SIGNCMD_PLACE)
6618 {
6619 /* ":sign place": list placed signs in all buffers */
6620 sign_list_placed(NULL);
6621 }
6622 else if (idx == SIGNCMD_UNPLACE)
6623 {
6624 /* ":sign unplace": remove placed sign at cursor */
6625 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
6626 if (id > 0)
6627 {
6628 buf_delsign(curwin->w_buffer, id);
6629 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
6630 }
6631 else
6632 EMSG(_("E159: Missing sign number"));
6633 }
6634 else
6635 EMSG(_(e_argreq));
6636 return;
6637 }
6638
6639 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
6640 {
6641 /* ":sign unplace *": remove all placed signs */
6642 buf_delete_all_signs();
6643 return;
6644 }
6645
6646 /* first arg could be placed sign id */
6647 arg1 = arg;
6648 if (VIM_ISDIGIT(*arg))
6649 {
6650 id = getdigits(&arg);
6651 if (!vim_iswhite(*arg) && *arg != NUL)
6652 {
6653 id = -1;
6654 arg = arg1;
6655 }
6656 else
6657 {
6658 arg = skipwhite(arg);
6659 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
6660 {
6661 /* ":sign unplace {id}": remove placed sign by number */
6662 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6663 if ((lnum = buf_delsign(buf, id)) != 0)
6664 update_debug_sign(buf, lnum);
6665 return;
6666 }
6667 }
6668 }
6669
6670 /*
6671 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
6672 * Leave "arg" pointing to {fname}.
6673 */
6674 for (;;)
6675 {
6676 if (STRNCMP(arg, "line=", 5) == 0)
6677 {
6678 arg += 5;
6679 lnum = atoi((char *)arg);
6680 arg = skiptowhite(arg);
6681 }
6682 else if (STRNCMP(arg, "name=", 5) == 0)
6683 {
6684 arg += 5;
6685 sign_name = arg;
6686 arg = skiptowhite(arg);
6687 if (*arg != NUL)
6688 *arg++ = NUL;
6689 }
6690 else if (STRNCMP(arg, "file=", 5) == 0)
6691 {
6692 arg += 5;
6693 buf = buflist_findname(arg);
6694 break;
6695 }
6696 else if (STRNCMP(arg, "buffer=", 7) == 0)
6697 {
6698 arg += 7;
6699 buf = buflist_findnr((int)getdigits(&arg));
6700 if (*skipwhite(arg) != NUL)
6701 EMSG(_(e_trailing));
6702 break;
6703 }
6704 else
6705 {
6706 EMSG(_(e_invarg));
6707 return;
6708 }
6709 arg = skipwhite(arg);
6710 }
6711
6712 if (buf == NULL)
6713 {
6714 EMSG2(_("E158: Invalid buffer name: %s"), arg);
6715 }
6716 else if (id <= 0)
6717 {
6718 if (lnum >= 0 || sign_name != NULL)
6719 EMSG(_(e_invarg));
6720 else
6721 /* ":sign place file={fname}": list placed signs in one file */
6722 sign_list_placed(buf);
6723 }
6724 else if (idx == SIGNCMD_JUMP)
6725 {
6726 /* ":sign jump {id} file={fname}" */
6727 if (lnum >= 0 || sign_name != NULL)
6728 EMSG(_(e_invarg));
6729 else if ((lnum = buf_findsign(buf, id)) > 0)
6730 { /* goto a sign ... */
6731 if (buf_jump_open_win(buf) != NULL)
6732 { /* ... in a current window */
6733 curwin->w_cursor.lnum = lnum;
6734 check_cursor_lnum();
6735 beginline(BL_WHITE);
6736 }
6737 else
6738 { /* ... not currently in a window */
6739 char_u *cmd;
6740
6741 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
6742 if (cmd == NULL)
6743 return;
6744 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
6745 do_cmdline_cmd(cmd);
6746 vim_free(cmd);
6747 }
6748#ifdef FEAT_FOLDING
6749 foldOpenCursor();
6750#endif
6751 }
6752 else
6753 EMSGN(_("E157: Invalid sign ID: %ld"), id);
6754 }
6755 else if (idx == SIGNCMD_UNPLACE)
6756 {
6757 /* ":sign unplace {id} file={fname}" */
6758 if (lnum >= 0 || sign_name != NULL)
6759 EMSG(_(e_invarg));
6760 else
6761 {
6762 lnum = buf_delsign(buf, id);
6763 update_debug_sign(buf, lnum);
6764 }
6765 }
6766 /* idx == SIGNCMD_PLACE */
6767 else if (sign_name != NULL)
6768 {
6769 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6770 if (STRCMP(sp->sn_name, sign_name) == 0)
6771 break;
6772 if (sp == NULL)
6773 {
6774 EMSG2(_("E155: Unknown sign: %s"), sign_name);
6775 return;
6776 }
6777 if (lnum > 0)
6778 /* ":sign place {id} line={lnum} name={name} file={fname}":
6779 * place a sign */
6780 buf_addsign(buf, id, lnum, sp->sn_typenr);
6781 else
6782 /* ":sign place {id} file={fname}": change sign type */
6783 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
6784 update_debug_sign(buf, lnum);
6785 }
6786 else
6787 EMSG(_(e_invarg));
6788 }
6789}
6790
6791#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6792/*
6793 * Allocate the icons. Called when the GUI has started. Allows defining
6794 * signs before it starts.
6795 */
6796 void
6797sign_gui_started()
6798{
6799 sign_T *sp;
6800
6801 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6802 if (sp->sn_icon != NULL)
6803 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6804}
6805#endif
6806
6807/*
6808 * List one sign.
6809 */
6810 static void
6811sign_list_defined(sp)
6812 sign_T *sp;
6813{
6814 char_u *p;
6815
Bram Moolenaar555b2802005-05-19 21:08:39 +00006816 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (sp->sn_icon != NULL)
6818 {
6819 MSG_PUTS(" icon=");
6820 msg_outtrans(sp->sn_icon);
6821#ifdef FEAT_SIGN_ICONS
6822 if (sp->sn_image == NULL)
6823 MSG_PUTS(_(" (NOT FOUND)"));
6824#else
6825 MSG_PUTS(_(" (not supported)"));
6826#endif
6827 }
6828 if (sp->sn_text != NULL)
6829 {
6830 MSG_PUTS(" text=");
6831 msg_outtrans(sp->sn_text);
6832 }
6833 if (sp->sn_line_hl > 0)
6834 {
6835 MSG_PUTS(" linehl=");
6836 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
6837 if (p == NULL)
6838 MSG_PUTS("NONE");
6839 else
6840 msg_puts(p);
6841 }
6842 if (sp->sn_text_hl > 0)
6843 {
6844 MSG_PUTS(" texthl=");
6845 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
6846 if (p == NULL)
6847 MSG_PUTS("NONE");
6848 else
6849 msg_puts(p);
6850 }
6851}
6852
6853/*
6854 * Get highlighting attribute for sign "typenr".
6855 * If "line" is TRUE: line highl, if FALSE: text highl.
6856 */
6857 int
6858sign_get_attr(typenr, line)
6859 int typenr;
6860 int line;
6861{
6862 sign_T *sp;
6863
6864 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6865 if (sp->sn_typenr == typenr)
6866 {
6867 if (line)
6868 {
6869 if (sp->sn_line_hl > 0)
6870 return syn_id2attr(sp->sn_line_hl);
6871 }
6872 else
6873 {
6874 if (sp->sn_text_hl > 0)
6875 return syn_id2attr(sp->sn_text_hl);
6876 }
6877 break;
6878 }
6879 return 0;
6880}
6881
6882/*
6883 * Get text mark for sign "typenr".
6884 * Returns NULL if there isn't one.
6885 */
6886 char_u *
6887sign_get_text(typenr)
6888 int typenr;
6889{
6890 sign_T *sp;
6891
6892 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6893 if (sp->sn_typenr == typenr)
6894 return sp->sn_text;
6895 return NULL;
6896}
6897
6898#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6899 void *
6900sign_get_image(typenr)
6901 int typenr; /* the attribute which may have a sign */
6902{
6903 sign_T *sp;
6904
6905 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6906 if (sp->sn_typenr == typenr)
6907 return sp->sn_image;
6908 return NULL;
6909}
6910#endif
6911
6912/*
6913 * Get the name of a sign by its typenr.
6914 */
6915 char_u *
6916sign_typenr2name(typenr)
6917 int typenr;
6918{
6919 sign_T *sp;
6920
6921 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6922 if (sp->sn_typenr == typenr)
6923 return sp->sn_name;
6924 return (char_u *)_("[Deleted]");
6925}
6926
6927#endif
6928
6929#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
6930/*
6931 * ":drop"
6932 * Opens the first argument in a window. When there are two or more arguments
6933 * the argument list is redefined.
6934 */
6935 void
6936ex_drop(eap)
6937 exarg_T *eap;
6938{
6939 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 win_T *wp;
6941 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006942# ifdef FEAT_WINDOWS
6943 tabpage_T *tp;
6944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 /*
6947 * Check if the first argument is already being edited in a window. If
6948 * so, jump to that window.
6949 * We would actually need to check all arguments, but that's complicated
6950 * and mostly only one file is dropped.
6951 * This also ignores wildcards, since it is very unlikely the user is
6952 * editing a file name with a wildcard character.
6953 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006954 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006956# ifdef FEAT_WINDOWS
6957 if (cmdmod.tab)
6958 {
6959 /* ":tab drop file ...": open a tab for each argument that isn't
6960 * edited in a window yet. It's like ":tab all" but without closing
6961 * windows or tabs. */
6962 ex_all(eap);
6963 }
6964 else
6965# endif
6966 {
6967 /* ":drop file ...": Edit the first argument. Jump to an existing
6968 * window if possible, edit in current window if the current buffer
6969 * can be abandoned, otherwise open a new window. */
6970 buf = buflist_findnr(ARGLIST[0].ae_fnum);
6971
6972 FOR_ALL_TAB_WINDOWS(tp, wp)
6973 {
6974 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006976# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006977 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 return;
6981 }
6982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006984 /*
6985 * Check whether the current buffer is changed. If so, we will need
6986 * to split the current window or data could be lost.
6987 * Skip the check if the 'hidden' option is set, as in this case the
6988 * buffer won't be lost.
6989 */
6990 if (!P_HID(curbuf))
6991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006993 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006995 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006997 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006999 if (split)
7000 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007004 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7005 if (split)
7006 {
7007 eap->cmdidx = CMD_sfirst;
7008 eap->cmd[0] = 's';
7009 }
7010 else
7011 eap->cmdidx = CMD_first;
7012 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015#endif