blob: e9630ee0783eecefbc0e76a8445aabb0b9b3499c [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
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014#include "vim.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#include "version.h"
16
Bram Moolenaar17576a12016-01-20 20:05:44 +010017#ifdef FEAT_FLOAT
18# include <float.h>
19#endif
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef FEAT_EX_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010022static int linelen(int *has_tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010024static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010026static char_u *viminfo_filename(char_u *);
27static void do_viminfo(FILE *fp_in, FILE *fp_out, int flags);
28static int viminfo_encoding(vir_T *virp);
29static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#endif
31
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static int check_readonly(int *forceit, buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010034static void delbuf_msg(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static int
37#ifdef __BORLANDC__
38 _RTLENTRYF
39#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040 help_compare(const void *s1, const void *s2);
41static void prepare_help_buffer(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43/*
44 * ":ascii" and "ga".
45 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 void
47do_ascii(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +000048 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000049{
50 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000051 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 char buf1[20];
53 char buf2[20];
54 char_u buf3[7];
55#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000056 int cc[MAX_MCO];
57 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 int len;
59
60 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000061 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 else
63#endif
64 c = gchar_cursor();
65 if (c == NUL)
66 {
67 MSG("NUL");
68 return;
69 }
70
71#ifdef FEAT_MBYTE
72 IObuff[0] = NUL;
73 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
74#endif
75 {
76 if (c == NL) /* NUL is stored as NL */
77 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000078 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
79 cval = NL; /* NL is stored as CR */
80 else
81 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 if (vim_isprintc_strict(c) && (c < ' '
83#ifndef EBCDIC
84 || c > '~'
85#endif
86 ))
87 {
88 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000089 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 }
91 else
92 buf1[0] = NUL;
93#ifndef EBCDIC
94 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000095 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
96 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 else
98#endif
99 buf2[0] = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +0000100 vim_snprintf((char *)IObuff, IOSIZE,
101 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +0000102 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#ifdef FEAT_MBYTE
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000104 if (enc_utf8)
105 c = cc[ci++];
106 else
107 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109 }
110
111#ifdef FEAT_MBYTE
112 /* Repeat for combining characters. */
113 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
114 {
115 len = (int)STRLEN(IObuff);
116 /* This assumes every multi-byte char is printable... */
117 if (len > 0)
118 IObuff[len++] = ' ';
119 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000120 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000121# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 )
125 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000126 len += (*mb_char2bytes)(c, IObuff + len);
127 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
129 : _("> %d, Hex %08x, Octal %o"), c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000130 if (ci == MAX_MCO)
131 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000132 if (enc_utf8)
133 c = cc[ci++];
134 else
135 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 }
137#endif
138
139 msg(IObuff);
140}
141
142#if defined(FEAT_EX_EXTRA) || defined(PROTO)
143/*
144 * ":left", ":center" and ":right": align text.
145 */
146 void
147ex_align(eap)
148 exarg_T *eap;
149{
150 pos_T save_curpos;
151 int len;
152 int indent = 0;
153 int new_indent;
154 int has_tab;
155 int width;
156
157#ifdef FEAT_RIGHTLEFT
158 if (curwin->w_p_rl)
159 {
160 /* switch left and right aligning */
161 if (eap->cmdidx == CMD_right)
162 eap->cmdidx = CMD_left;
163 else if (eap->cmdidx == CMD_left)
164 eap->cmdidx = CMD_right;
165 }
166#endif
167
168 width = atoi((char *)eap->arg);
169 save_curpos = curwin->w_cursor;
170 if (eap->cmdidx == CMD_left) /* width is used for new indent */
171 {
172 if (width >= 0)
173 indent = width;
174 }
175 else
176 {
177 /*
178 * if 'textwidth' set, use it
179 * else if 'wrapmargin' set, use it
180 * if invalid value, use 80
181 */
182 if (width <= 0)
183 width = curbuf->b_p_tw;
184 if (width == 0 && curbuf->b_p_wm > 0)
185 width = W_WIDTH(curwin) - curbuf->b_p_wm;
186 if (width <= 0)
187 width = 80;
188 }
189
190 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
191 return;
192
193 for (curwin->w_cursor.lnum = eap->line1;
194 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
195 {
196 if (eap->cmdidx == CMD_left) /* left align */
197 new_indent = indent;
198 else
199 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000200 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 len = linelen(eap->cmdidx == CMD_right ? &has_tab
202 : NULL) - get_indent();
203
204 if (len <= 0) /* skip blank lines */
205 continue;
206
207 if (eap->cmdidx == CMD_center)
208 new_indent = (width - len) / 2;
209 else
210 {
211 new_indent = width - len; /* right align */
212
213 /*
214 * Make sure that embedded TABs don't make the text go too far
215 * to the right.
216 */
217 if (has_tab)
218 while (new_indent > 0)
219 {
220 (void)set_indent(new_indent, 0);
221 if (linelen(NULL) <= width)
222 {
223 /*
224 * Now try to move the line as much as possible to
225 * the right. Stop when it moves too far.
226 */
227 do
228 (void)set_indent(++new_indent, 0);
229 while (linelen(NULL) <= width);
230 --new_indent;
231 break;
232 }
233 --new_indent;
234 }
235 }
236 }
237 if (new_indent < 0)
238 new_indent = 0;
239 (void)set_indent(new_indent, 0); /* set indent */
240 }
241 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
242 curwin->w_cursor = save_curpos;
243 beginline(BL_WHITE | BL_FIX);
244}
245
246/*
247 * Get the length of the current line, excluding trailing white space.
248 */
249 static int
250linelen(has_tab)
251 int *has_tab;
252{
253 char_u *line;
254 char_u *first;
255 char_u *last;
256 int save;
257 int len;
258
259 /* find the first non-blank character */
260 line = ml_get_curline();
261 first = skipwhite(line);
262
263 /* find the character after the last non-blank character */
264 for (last = first + STRLEN(first);
265 last > first && vim_iswhite(last[-1]); --last)
266 ;
267 save = *last;
268 *last = NUL;
269 len = linetabsize(line); /* get line length */
270 if (has_tab != NULL) /* check for embedded TAB */
271 *has_tab = (vim_strrchr(first, TAB) != NULL);
272 *last = save;
273
274 return len;
275}
276
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000277/* Buffer for two lines used during sorting. They are allocated to
278 * contain the longest line being sorted. */
279static char_u *sortbuf1;
280static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000281
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100282static int sort_ic; /* ignore case */
283static int sort_nr; /* sort on number */
284static int sort_rx; /* sort on regex instead of skipping it */
285#ifdef FEAT_FLOAT
286static int sort_flt; /* sort on floating number */
287#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000288
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100289static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000290
291/* Struct to store info to be sorted. */
292typedef struct
293{
294 linenr_T lnum; /* line number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100295 union {
296 struct
297 {
298 long start_col_nr; /* starting column number */
299 long end_col_nr; /* ending column number */
300 } line;
301 long value; /* value if sorting by integer */
302#ifdef FEAT_FLOAT
303 float_T value_flt; /* value if sorting by float */
304#endif
305 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000306} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000307
308static int
309#ifdef __BORLANDC__
310_RTLENTRYF
311#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100312sort_compare(const void *s1, const void *s2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000313
314 static int
315#ifdef __BORLANDC__
316_RTLENTRYF
317#endif
318sort_compare(s1, s2)
319 const void *s1;
320 const void *s2;
321{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000322 sorti_T l1 = *(sorti_T *)s1;
323 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000324 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000325
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000326 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000327 * if we return 0 every time, qsort will assume it's done sorting and
328 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329 if (sort_abort)
330 return 0;
331 fast_breakcheck();
332 if (got_int)
333 sort_abort = TRUE;
334
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000335 /* When sorting numbers "start_col_nr" is the number, not the column
336 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000337 if (sort_nr)
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100338 result = l1.st_u.value == l2.st_u.value ? 0
339 : l1.st_u.value > l2.st_u.value ? 1 : -1;
340#ifdef FEAT_FLOAT
341 else if (sort_flt)
342 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
343 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
344#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345 else
346 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000347 /* We need to copy one line into "sortbuf1", because there is no
348 * guarantee that the first pointer becomes invalid when obtaining the
349 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100350 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
351 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
352 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
353 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
354 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
355 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000356
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000357 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
358 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000359 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000360
361 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000362 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000363 return (int)(l1.lnum - l2.lnum);
364 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000365}
366
367/*
368 * ":sort".
369 */
370 void
371ex_sort(eap)
372 exarg_T *eap;
373{
374 regmatch_T regmatch;
375 int len;
376 linenr_T lnum;
377 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000378 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000379 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000380 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000381 char_u *p;
382 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000383 char_u *s2;
384 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000385 int unique = FALSE;
386 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000387 colnr_T start_col;
388 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100389 int sort_what = 0;
390 int format_found = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000391
Bram Moolenaar48c99162008-02-18 18:42:52 +0000392 /* Sorting one line is really quick! */
393 if (count <= 1)
394 return;
395
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000396 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
397 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000398 sortbuf1 = NULL;
399 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000400 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000401 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000402 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000403 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000404
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100405 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100406#ifdef FEAT_FLOAT
407 sort_flt = 0;
408#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000409
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000410 for (p = eap->arg; *p != NUL; ++p)
411 {
412 if (vim_iswhite(*p))
413 ;
414 else if (*p == 'i')
415 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000416 else if (*p == 'r')
417 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000418 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100419 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100420 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100421 ++format_found;
422 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100423#ifdef FEAT_FLOAT
424 else if (*p == 'f')
425 {
426 sort_flt = 1;
427 ++format_found;
428 }
429#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100430 else if (*p == 'b')
431 {
432 sort_what = STR2NR_BIN + STR2NR_FORCE;
433 ++format_found;
434 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000435 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100436 {
437 sort_what = STR2NR_OCT + STR2NR_FORCE;
438 ++format_found;
439 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000440 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100441 {
442 sort_what = STR2NR_HEX + STR2NR_FORCE;
443 ++format_found;
444 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000445 else if (*p == 'u')
446 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000447 else if (*p == '"') /* comment start */
448 break;
449 else if (check_nextcmd(p) != NULL)
450 {
451 eap->nextcmd = check_nextcmd(p);
452 break;
453 }
454 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000455 {
456 s = skip_regexp(p + 1, *p, TRUE, NULL);
457 if (*s != *p)
458 {
459 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000460 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000461 }
462 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000463 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100464 if (s == p + 1)
465 {
466 if (last_search_pat() == NULL)
467 {
468 EMSG(_(e_noprevre));
469 goto sortend;
470 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000471 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100472 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000473 else
474 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000475 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000476 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000477 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000478 regmatch.rm_ic = p_ic;
479 }
480 else
481 {
482 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000483 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000484 }
485 }
486
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100487 /* Can only have one of 'n', 'b', 'o' and 'x'. */
488 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000489 {
490 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000491 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000492 }
493
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100494 /* From here on "sort_nr" is used as a flag for any integer number
495 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100496 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000497
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000498 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000499 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000500 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000501 * When sorting on strings "start_col_nr" is the offset in the line, for
502 * numbers sorting it's the number to sort on. This means the pattern
503 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000504 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000505 */
506 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
507 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000508 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000509 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000510 if (maxlen < len)
511 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000512
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000513 start_col = 0;
514 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000515 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000516 {
517 if (sort_rx)
518 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000519 start_col = (colnr_T)(regmatch.startp[0] - s);
520 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000521 }
522 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000523 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000524 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000525 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000526 if (regmatch.regprog != NULL)
527 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000528
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100529 if (sort_nr || sort_flt)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000530 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000531 /* Make sure vim_str2nr doesn't read any digits past the end
532 * of the match, by temporarily terminating the string there */
533 s2 = s + end_col;
534 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200535 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000536 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000537 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100538 if (sort_nr)
539 {
540 if (sort_what & STR2NR_HEX)
541 s = skiptohex(p);
542 else if (sort_what & STR2NR_BIN)
543 s = skiptobin(p);
544 else
545 s = skiptodigit(p);
546 if (s > p && s[-1] == '-')
547 --s; /* include preceding negative sign */
548 if (*s == NUL)
549 /* empty line should sort before any number */
550 nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
551 else
552 vim_str2nr(s, NULL, NULL, sort_what,
553 &nrs[lnum - eap->line1].st_u.value, NULL, 0);
554 }
555#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000556 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100557 {
558 s = skipwhite(p);
559 if (*s == '+')
560 s = skipwhite(s + 1);
561
562 if (*s == NUL)
563 /* empty line should sort before any number */
564 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
565 else
566 nrs[lnum - eap->line1].st_u.value_flt =
567 strtod((char *)s, NULL);
568 }
569#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200570 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000571 }
572 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000573 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000574 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100575 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
576 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000577 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000578
579 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000580
581 if (regmatch.regprog != NULL)
582 fast_breakcheck();
583 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000584 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000585 }
586
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000587 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000588 sortbuf1 = alloc((unsigned)maxlen + 1);
589 if (sortbuf1 == NULL)
590 goto sortend;
591 sortbuf2 = alloc((unsigned)maxlen + 1);
592 if (sortbuf2 == NULL)
593 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000594
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000595 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000596 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000597
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000598 if (sort_abort)
599 goto sortend;
600
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000601 /* Insert the lines in the sorted order below the last one. */
602 lnum = eap->line2;
603 for (i = 0; i < count; ++i)
604 {
605 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
606 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000607 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000608 {
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100609 /* Copy the line into a buffer, it may become invalid in
610 * ml_append(). And it's needed for "unique". */
611 STRCPY(sortbuf1, s);
612 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000613 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000614 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000615 fast_breakcheck();
616 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000617 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000618 }
619
620 /* delete the original lines if appending worked */
621 if (i == count)
622 for (i = 0; i < count; ++i)
623 ml_delete(eap->line1, FALSE);
624 else
625 count = 0;
626
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000627 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000628 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000629 if (deleted > 0)
630 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
631 else if (deleted < 0)
632 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
633 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000634
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000635 curwin->w_cursor.lnum = eap->line1;
636 beginline(BL_WHITE | BL_FIX);
637
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000638sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000639 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000640 vim_free(sortbuf1);
641 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200642 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000643 if (got_int)
644 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
648 * ":retab".
649 */
650 void
651ex_retab(eap)
652 exarg_T *eap;
653{
654 linenr_T lnum;
655 int got_tab = FALSE;
656 long num_spaces = 0;
657 long num_tabs;
658 long len;
659 long col;
660 long vcol;
661 long start_col = 0; /* For start of white-space string */
662 long start_vcol = 0; /* For start of white-space string */
663 int temp;
664 long old_len;
665 char_u *ptr;
666 char_u *new_line = (char_u *)1; /* init to non-NULL */
667 int did_undo; /* called u_save for current line */
668 int new_ts;
669 int save_list;
670 linenr_T first_line = 0; /* first changed line */
671 linenr_T last_line = 0; /* last changed line */
672
673 save_list = curwin->w_p_list;
674 curwin->w_p_list = 0; /* don't want list mode here */
675
676 new_ts = getdigits(&(eap->arg));
677 if (new_ts < 0)
678 {
679 EMSG(_(e_positive));
680 return;
681 }
682 if (new_ts == 0)
683 new_ts = curbuf->b_p_ts;
684 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
685 {
686 ptr = ml_get(lnum);
687 col = 0;
688 vcol = 0;
689 did_undo = FALSE;
690 for (;;)
691 {
692 if (vim_iswhite(ptr[col]))
693 {
694 if (!got_tab && num_spaces == 0)
695 {
696 /* First consecutive white-space */
697 start_vcol = vcol;
698 start_col = col;
699 }
700 if (ptr[col] == ' ')
701 num_spaces++;
702 else
703 got_tab = TRUE;
704 }
705 else
706 {
707 if (got_tab || (eap->forceit && num_spaces > 1))
708 {
709 /* Retabulate this string of white-space */
710
711 /* len is virtual length of white string */
712 len = num_spaces = vcol - start_vcol;
713 num_tabs = 0;
714 if (!curbuf->b_p_et)
715 {
716 temp = new_ts - (start_vcol % new_ts);
717 if (num_spaces >= temp)
718 {
719 num_spaces -= temp;
720 num_tabs++;
721 }
722 num_tabs += num_spaces / new_ts;
723 num_spaces -= (num_spaces / new_ts) * new_ts;
724 }
725 if (curbuf->b_p_et || got_tab ||
726 (num_spaces + num_tabs < len))
727 {
728 if (did_undo == FALSE)
729 {
730 did_undo = TRUE;
731 if (u_save((linenr_T)(lnum - 1),
732 (linenr_T)(lnum + 1)) == FAIL)
733 {
734 new_line = NULL; /* flag out-of-memory */
735 break;
736 }
737 }
738
739 /* len is actual number of white characters used */
740 len = num_spaces + num_tabs;
741 old_len = (long)STRLEN(ptr);
742 new_line = lalloc(old_len - col + start_col + len + 1,
743 TRUE);
744 if (new_line == NULL)
745 break;
746 if (start_col > 0)
747 mch_memmove(new_line, ptr, (size_t)start_col);
748 mch_memmove(new_line + start_col + len,
749 ptr + col, (size_t)(old_len - col + 1));
750 ptr = new_line + start_col;
751 for (col = 0; col < len; col++)
752 ptr[col] = (col < num_tabs) ? '\t' : ' ';
753 ml_replace(lnum, new_line, FALSE);
754 if (first_line == 0)
755 first_line = lnum;
756 last_line = lnum;
757 ptr = new_line;
758 col = start_col + len;
759 }
760 }
761 got_tab = FALSE;
762 num_spaces = 0;
763 }
764 if (ptr[col] == NUL)
765 break;
766 vcol += chartabsize(ptr + col, (colnr_T)vcol);
767#ifdef FEAT_MBYTE
768 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000769 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 else
771#endif
772 ++col;
773 }
774 if (new_line == NULL) /* out of memory */
775 break;
776 line_breakcheck();
777 }
778 if (got_int)
779 EMSG(_(e_interr));
780
781 if (curbuf->b_p_ts != new_ts)
782 redraw_curbuf_later(NOT_VALID);
783 if (first_line != 0)
784 changed_lines(first_line, 0, last_line + 1, 0L);
785
786 curwin->w_p_list = save_list; /* restore 'list' */
787
788 curbuf->b_p_ts = new_ts;
789 coladvance(curwin->w_curswant);
790
791 u_clearline();
792}
793#endif
794
795/*
796 * :move command - move lines line1-line2 to line dest
797 *
798 * return FAIL for failure, OK otherwise
799 */
800 int
801do_move(line1, line2, dest)
802 linenr_T line1;
803 linenr_T line2;
804 linenr_T dest;
805{
806 char_u *str;
807 linenr_T l;
808 linenr_T extra; /* Num lines added before line1 */
809 linenr_T num_lines; /* Num lines moved */
810 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200811#ifdef FEAT_FOLDING
812 int isFolded;
813
814 /* Moving lines seems to corrupt the folds, delete folding info now
815 * and recreate it when finished. Don't do this for manual folding, it
816 * would delete all folds. */
817 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin);
818 if (isFolded)
819 deleteFoldRecurse(&curwin->w_folds);
820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822 if (dest >= line1 && dest < line2)
823 {
824 EMSG(_("E134: Move lines into themselves"));
825 return FAIL;
826 }
827
828 num_lines = line2 - line1 + 1;
829
830 /*
831 * First we copy the old text to its new location -- webb
832 * Also copy the flag that ":global" command uses.
833 */
834 if (u_save(dest, dest + 1) == FAIL)
835 return FAIL;
836 for (extra = 0, l = line1; l <= line2; l++)
837 {
838 str = vim_strsave(ml_get(l + extra));
839 if (str != NULL)
840 {
841 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
842 vim_free(str);
843 if (dest < line1)
844 extra++;
845 }
846 }
847
848 /*
849 * Now we must be careful adjusting our marks so that we don't overlap our
850 * mark_adjust() calls.
851 *
852 * We adjust the marks within the old text so that they refer to the
853 * last lines of the file (temporarily), because we know no other marks
854 * will be set there since these line numbers did not exist until we added
855 * our new lines.
856 *
857 * Then we adjust the marks on lines between the old and new text positions
858 * (either forwards or backwards).
859 *
860 * And Finally we adjust the marks we put at the end of the file back to
861 * their final destination at the new text position -- webb
862 */
863 last_line = curbuf->b_ml.ml_line_count;
864 mark_adjust(line1, line2, last_line - line2, 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200865 changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (dest >= line2)
867 {
868 mark_adjust(line2 + 1, dest, -num_lines, 0L);
869 curbuf->b_op_start.lnum = dest - num_lines + 1;
870 curbuf->b_op_end.lnum = dest;
871 }
872 else
873 {
874 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
875 curbuf->b_op_start.lnum = dest + 1;
876 curbuf->b_op_end.lnum = dest + num_lines;
877 }
878 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
879 mark_adjust(last_line - num_lines + 1, last_line,
880 -(last_line - dest - extra), 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200881 changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883 /*
884 * Now we delete the original text -- webb
885 */
886 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
887 return FAIL;
888
889 for (l = line1; l <= line2; l++)
890 ml_delete(line1 + extra, TRUE);
891
892 if (!global_busy && num_lines > p_report)
893 {
894 if (num_lines == 1)
895 MSG(_("1 line moved"));
896 else
897 smsg((char_u *)_("%ld lines moved"), num_lines);
898 }
899
900 /*
901 * Leave the cursor on the last of the moved lines.
902 */
903 if (dest >= line1)
904 curwin->w_cursor.lnum = dest;
905 else
906 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
907
908 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +0100909 {
910 dest += num_lines + 1;
911 last_line = curbuf->b_ml.ml_line_count;
912 if (dest > last_line + 1)
913 dest = last_line + 1;
914 changed_lines(line1, 0, dest, 0L);
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else
917 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
918
Bram Moolenaard5f69332015-04-15 12:43:50 +0200919#ifdef FEAT_FOLDING
920 /* recreate folds */
921 if (isFolded)
922 foldUpdateAll(curwin);
923#endif
924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 return OK;
926}
927
928/*
929 * ":copy"
930 */
931 void
932ex_copy(line1, line2, n)
933 linenr_T line1;
934 linenr_T line2;
935 linenr_T n;
936{
937 linenr_T count;
938 char_u *p;
939
940 count = line2 - line1 + 1;
941 curbuf->b_op_start.lnum = n + 1;
942 curbuf->b_op_end.lnum = n + count;
943 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
944
945 /*
946 * there are three situations:
947 * 1. destination is above line1
948 * 2. destination is between line1 and line2
949 * 3. destination is below line2
950 *
951 * n = destination (when starting)
952 * curwin->w_cursor.lnum = destination (while copying)
953 * line1 = start of source (while copying)
954 * line2 = end of source (while copying)
955 */
956 if (u_save(n, n + 1) == FAIL)
957 return;
958
959 curwin->w_cursor.lnum = n;
960 while (line1 <= line2)
961 {
962 /* need to use vim_strsave() because the line will be unlocked within
963 * ml_append() */
964 p = vim_strsave(ml_get(line1));
965 if (p != NULL)
966 {
967 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
968 vim_free(p);
969 }
970 /* situation 2: skip already copied lines */
971 if (line1 == n)
972 line1 = curwin->w_cursor.lnum;
973 ++line1;
974 if (curwin->w_cursor.lnum < line1)
975 ++line1;
976 if (curwin->w_cursor.lnum < line2)
977 ++line2;
978 ++curwin->w_cursor.lnum;
979 }
980
981 appended_lines_mark(n, count);
982
983 msgmore((long)count);
984}
985
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000986static char_u *prevcmd = NULL; /* the previous command */
987
988#if defined(EXITFREE) || defined(PROTO)
989 void
990free_prev_shellcmd()
991{
992 vim_free(prevcmd);
993}
994#endif
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/*
997 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
998 * Bangs in the argument are replaced with the previously entered command.
999 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 */
1001 void
1002do_bang(addr_count, eap, forceit, do_in, do_out)
1003 int addr_count;
1004 exarg_T *eap;
1005 int forceit;
1006 int do_in, do_out;
1007{
1008 char_u *arg = eap->arg; /* command */
1009 linenr_T line1 = eap->line1; /* start of range */
1010 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 char_u *newcmd = NULL; /* the new command */
1012 int free_newcmd = FALSE; /* need to free() newcmd */
1013 int ins_prevcmd;
1014 char_u *t;
1015 char_u *p;
1016 char_u *trailarg;
1017 int len;
1018 int scroll_save = msg_scroll;
1019
1020 /*
1021 * Disallow shell commands for "rvim".
1022 * Disallow shell commands from .exrc and .vimrc in current directory for
1023 * security reasons.
1024 */
1025 if (check_restricted() || check_secure())
1026 return;
1027
1028 if (addr_count == 0) /* :! */
1029 {
1030 msg_scroll = FALSE; /* don't scroll here */
1031 autowrite_all();
1032 msg_scroll = scroll_save;
1033 }
1034
1035 /*
1036 * Try to find an embedded bang, like in :!<cmd> ! [args]
1037 * (:!! is indicated by the 'forceit' variable)
1038 */
1039 ins_prevcmd = forceit;
1040 trailarg = arg;
1041 do
1042 {
1043 len = (int)STRLEN(trailarg) + 1;
1044 if (newcmd != NULL)
1045 len += (int)STRLEN(newcmd);
1046 if (ins_prevcmd)
1047 {
1048 if (prevcmd == NULL)
1049 {
1050 EMSG(_(e_noprev));
1051 vim_free(newcmd);
1052 return;
1053 }
1054 len += (int)STRLEN(prevcmd);
1055 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001056 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 {
1058 vim_free(newcmd);
1059 return;
1060 }
1061 *t = NUL;
1062 if (newcmd != NULL)
1063 STRCAT(t, newcmd);
1064 if (ins_prevcmd)
1065 STRCAT(t, prevcmd);
1066 p = t + STRLEN(t);
1067 STRCAT(t, trailarg);
1068 vim_free(newcmd);
1069 newcmd = t;
1070
1071 /*
1072 * Scan the rest of the argument for '!', which is replaced by the
1073 * previous command. "\!" is replaced by "!" (this is vi compatible).
1074 */
1075 trailarg = NULL;
1076 while (*p)
1077 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001078 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
1080 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001081 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 {
1084 trailarg = p;
1085 *trailarg++ = NUL;
1086 ins_prevcmd = TRUE;
1087 break;
1088 }
1089 }
1090 ++p;
1091 }
1092 } while (trailarg != NULL);
1093
1094 vim_free(prevcmd);
1095 prevcmd = newcmd;
1096
1097 if (bangredo) /* put cmd in redo buffer for ! command */
1098 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001099 /* If % or # appears in the command, it must have been escaped.
1100 * Reescape them, so that redoing them does not substitute them by the
1101 * buffername. */
1102 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1103
1104 if (cmd != NULL)
1105 {
1106 AppendToRedobuffLit(cmd, -1);
1107 vim_free(cmd);
1108 }
1109 else
1110 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 AppendToRedobuff((char_u *)"\n");
1112 bangredo = FALSE;
1113 }
1114 /*
1115 * Add quotes around the command, for shells that need them.
1116 */
1117 if (*p_shq != NUL)
1118 {
1119 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1120 if (newcmd == NULL)
1121 return;
1122 STRCPY(newcmd, p_shq);
1123 STRCAT(newcmd, prevcmd);
1124 STRCAT(newcmd, p_shq);
1125 free_newcmd = TRUE;
1126 }
1127 if (addr_count == 0) /* :! */
1128 {
1129 /* echo the command */
1130 msg_start();
1131 msg_putchar(':');
1132 msg_putchar('!');
1133 msg_outtrans(newcmd);
1134 msg_clr_eos();
1135 windgoto(msg_row, msg_col);
1136
1137 do_shell(newcmd, 0);
1138 }
1139 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 /* Careful: This may recursively call do_bang() again! (because of
1142 * autocommands) */
1143 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001144#ifdef FEAT_AUTOCMD
1145 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1146#endif
1147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (free_newcmd)
1149 vim_free(newcmd);
1150}
1151
1152/*
1153 * do_filter: filter lines through a command given by the user
1154 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 * We mostly use temp files and the call_shell() routine here. This would
1156 * normally be done using pipes on a UNIX machine, but this is more portable
1157 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 * to deal with redirection somehow, and should handle things like looking
1159 * at the PATH env. variable, and adding reasonable extensions to the
1160 * command name given by the user. All reasonable versions of call_shell()
1161 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 * Alternatively, if on Unix and redirecting input or output, but not both,
1163 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 * We use input redirection if do_in is TRUE.
1165 * We use output redirection if do_out is TRUE.
1166 */
1167 static void
1168do_filter(line1, line2, eap, cmd, do_in, do_out)
1169 linenr_T line1, line2;
1170 exarg_T *eap; /* for forced 'ff' and 'fenc' */
1171 char_u *cmd;
1172 int do_in, do_out;
1173{
1174 char_u *itmp = NULL;
1175 char_u *otmp = NULL;
1176 linenr_T linecount;
1177 linenr_T read_linecount;
1178 pos_T cursor_save;
1179 char_u *cmd_buf;
1180#ifdef FEAT_AUTOCMD
1181 buf_T *old_curbuf = curbuf;
1182#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 if (*cmd == NUL) /* no filter command */
1186 return;
1187
1188#ifdef WIN3264
1189 /*
1190 * Check if external commands are allowed now.
1191 */
1192 if (can_end_termcap_mode(TRUE) == FALSE)
1193 return;
1194#endif
1195
1196 cursor_save = curwin->w_cursor;
1197 linecount = line2 - line1 + 1;
1198 curwin->w_cursor.lnum = line1;
1199 curwin->w_cursor.col = 0;
1200 changed_line_abv_curs();
1201 invalidate_botline();
1202
1203 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 * When using temp files:
1205 * 1. * Form temp file names
1206 * 2. * Write the lines to a temp file
1207 * 3. Run the filter command on the temp file
1208 * 4. * Read the output of the command into the buffer
1209 * 5. * Delete the original lines to be filtered
1210 * 6. * Remove the temp files
1211 *
1212 * When writing the input with a pipe or when catching the output with a
1213 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
1215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 if (do_out)
1217 shell_flags |= SHELL_DOOUT;
1218
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001219#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1223 shell_flags |= SHELL_READ;
1224 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226 else if (do_in && !do_out && !p_stmp)
1227 {
1228 /* Use a pipe to write stdin of the command, do not use a temp file. */
1229 shell_flags |= SHELL_WRITE;
1230 curbuf->b_op_start.lnum = line1;
1231 curbuf->b_op_end.lnum = line2;
1232 }
1233 else if (do_in && do_out && !p_stmp)
1234 {
1235 /* Use a pipe to write stdin and fetch stdout of the command, do not
1236 * use a temp file. */
1237 shell_flags |= SHELL_READ|SHELL_WRITE;
1238 curbuf->b_op_start.lnum = line1;
1239 curbuf->b_op_end.lnum = line2;
1240 curwin->w_cursor.lnum = line2;
1241 }
1242 else
1243#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001244 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1245 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246 {
1247 EMSG(_(e_notmp));
1248 goto filterend;
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251/*
1252 * The writing and reading of temp files will not be shown.
1253 * Vi also doesn't do this and the messages are not very informative.
1254 */
1255 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001256 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 FALSE, FALSE, FALSE, TRUE) == FAIL)
1258 {
1259 msg_putchar('\n'); /* keep message from buf_write() */
1260 --no_wait_return;
1261#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1262 if (!aborting())
1263#endif
1264 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1265 goto filterend;
1266 }
1267#ifdef FEAT_AUTOCMD
1268 if (curbuf != old_curbuf)
1269 goto filterend;
1270#endif
1271
1272 if (!do_out)
1273 msg_putchar('\n');
1274
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001275 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1277 if (cmd_buf == NULL)
1278 goto filterend;
1279
1280 windgoto((int)Rows - 1, 0);
1281 cursor_on();
1282
1283 /*
1284 * When not redirecting the output the command can write anything to the
1285 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1286 * stderr output of external command. Clear the screen later.
1287 * If do_in is FALSE, this could be something like ":r !cat", which may
1288 * also mess up the screen, clear it later.
1289 */
1290 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1291 redraw_later_clear();
1292
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001293 if (do_out)
1294 {
1295 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001296 {
1297 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001298 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001299 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001300 redraw_curbuf_later(VALID);
1301 }
1302 read_linecount = curbuf->b_ml.ml_line_count;
1303
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 /*
1305 * When call_shell() fails wait_return() is called to give the user a
1306 * chance to read the error messages. Otherwise errors are ignored, so you
1307 * can see the error messages from the command that appear on stdout; use
1308 * 'u' to fix the text
1309 * Switch to cooked mode when not redirecting stdin, avoids that something
1310 * like ":r !cat" hangs.
1311 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1312 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001313 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 redraw_later_clear();
1316 wait_return(FALSE);
1317 }
1318 vim_free(cmd_buf);
1319
1320 did_check_timestamps = FALSE;
1321 need_check_timestamps = TRUE;
1322
1323 /* When interrupting the shell command, it may still have produced some
1324 * useful output. Reset got_int here, so that readfile() won't cancel
1325 * reading. */
1326 ui_breakcheck();
1327 got_int = FALSE;
1328
1329 if (do_out)
1330 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001331 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001333 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1334 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001336#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1337 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001339 {
1340 msg_putchar('\n');
1341 EMSG2(_(e_notread), otmp);
1342 }
1343 goto error;
1344 }
1345#ifdef FEAT_AUTOCMD
1346 if (curbuf != old_curbuf)
1347 goto filterend;
1348#endif
1349 }
1350
1351 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1352
1353 if (shell_flags & SHELL_READ)
1354 {
1355 curbuf->b_op_start.lnum = line2 + 1;
1356 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1357 appended_lines_mark(line2, read_linecount);
1358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 if (do_in)
1361 {
1362 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (read_linecount >= linecount)
1365 /* move all marks from old lines to new lines */
1366 mark_adjust(line1, line2, linecount, 0L);
1367 else
1368 {
1369 /* move marks from old lines to new lines, delete marks
1370 * that are in deleted lines */
1371 mark_adjust(line1, line1 + read_linecount - 1,
1372 linecount, 0L);
1373 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1374 }
1375 }
1376
1377 /*
1378 * Put cursor on first filtered line for ":range!cmd".
1379 * Adjust '[ and '] (set by buf_write()).
1380 */
1381 curwin->w_cursor.lnum = line1;
1382 del_lines(linecount, TRUE);
1383 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1384 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1385 write_lnum_adjust(-linecount); /* adjust last line
1386 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001387#ifdef FEAT_FOLDING
1388 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else
1392 {
1393 /*
1394 * Put cursor on last new line for ":r !cmd".
1395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001397 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1401 --no_wait_return;
1402
1403 if (linecount > p_report)
1404 {
1405 if (do_in)
1406 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001407 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1408 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001411 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
1413 else
1414 msgmore((long)linecount);
1415 }
1416 }
1417 else
1418 {
1419error:
1420 /* put cursor back in same position for ":w !cmd" */
1421 curwin->w_cursor = cursor_save;
1422 --no_wait_return;
1423 wait_return(FALSE);
1424 }
1425
1426filterend:
1427
1428#ifdef FEAT_AUTOCMD
1429 if (curbuf != old_curbuf)
1430 {
1431 --no_wait_return;
1432 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1433 }
1434#endif
1435 if (itmp != NULL)
1436 mch_remove(itmp);
1437 if (otmp != NULL)
1438 mch_remove(otmp);
1439 vim_free(itmp);
1440 vim_free(otmp);
1441}
1442
1443/*
1444 * Call a shell to execute a command.
1445 * When "cmd" is NULL start an interactive shell.
1446 */
1447 void
1448do_shell(cmd, flags)
1449 char_u *cmd;
1450 int flags; /* may be SHELL_DOOUT when output is redirected */
1451{
1452 buf_T *buf;
1453#ifndef FEAT_GUI_MSWIN
1454 int save_nwr;
1455#endif
1456#ifdef MSWIN
1457 int winstart = FALSE;
1458#endif
1459
1460 /*
1461 * Disallow shell commands for "rvim".
1462 * Disallow shell commands from .exrc and .vimrc in current directory for
1463 * security reasons.
1464 */
1465 if (check_restricted() || check_secure())
1466 {
1467 msg_end();
1468 return;
1469 }
1470
1471#ifdef MSWIN
1472 /*
1473 * Check if external commands are allowed now.
1474 */
1475 if (can_end_termcap_mode(TRUE) == FALSE)
1476 return;
1477
1478 /*
1479 * Check if ":!start" is used.
1480 */
1481 if (cmd != NULL)
1482 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1483#endif
1484
1485 /*
1486 * For autocommands we want to get the output on the current screen, to
1487 * avoid having to type return below.
1488 */
1489 msg_putchar('\r'); /* put cursor at start of line */
1490#ifdef FEAT_AUTOCMD
1491 if (!autocmd_busy)
1492#endif
1493 {
1494#ifdef MSWIN
1495 if (!winstart)
1496#endif
1497 stoptermcap();
1498 }
1499#ifdef MSWIN
1500 if (!winstart)
1501#endif
1502 msg_putchar('\n'); /* may shift screen one line up */
1503
1504 /* warning message before calling the shell */
1505 if (p_warn
1506#ifdef FEAT_AUTOCMD
1507 && !autocmd_busy
1508#endif
1509 && msg_silent == 0)
1510 for (buf = firstbuf; buf; buf = buf->b_next)
1511 if (bufIsChanged(buf))
1512 {
1513#ifdef FEAT_GUI_MSWIN
1514 if (!winstart)
1515 starttermcap(); /* don't want a message box here */
1516#endif
1517 MSG_PUTS(_("[No write since last change]\n"));
1518#ifdef FEAT_GUI_MSWIN
1519 if (!winstart)
1520 stoptermcap();
1521#endif
1522 break;
1523 }
1524
1525 /* This windgoto is required for when the '\n' resulted in a "delete line
1526 * 1" command to the terminal. */
1527 if (!swapping_screen())
1528 windgoto(msg_row, msg_col);
1529 cursor_on();
1530 (void)call_shell(cmd, SHELL_COOKED | flags);
1531 did_check_timestamps = FALSE;
1532 need_check_timestamps = TRUE;
1533
1534 /*
1535 * put the message cursor at the end of the screen, avoids wait_return()
1536 * to overwrite the text that the external command showed
1537 */
1538 if (!swapping_screen())
1539 {
1540 msg_row = Rows - 1;
1541 msg_col = 0;
1542 }
1543
1544#ifdef FEAT_AUTOCMD
1545 if (autocmd_busy)
1546 {
1547 if (msg_silent == 0)
1548 redraw_later_clear();
1549 }
1550 else
1551#endif
1552 {
1553 /*
1554 * For ":sh" there is no need to call wait_return(), just redraw.
1555 * Also for the Win32 GUI (the output is in a console window).
1556 * Otherwise there is probably text on the screen that the user wants
1557 * to read before redrawing, so call wait_return().
1558 */
1559#ifndef FEAT_GUI_MSWIN
1560 if (cmd == NULL
1561# ifdef WIN3264
1562 || (winstart && !need_wait_return)
1563# endif
1564 )
1565 {
1566 if (msg_silent == 0)
1567 redraw_later_clear();
1568 need_wait_return = FALSE;
1569 }
1570 else
1571 {
1572 /*
1573 * If we switch screens when starttermcap() is called, we really
1574 * want to wait for "hit return to continue".
1575 */
1576 save_nwr = no_wait_return;
1577 if (swapping_screen())
1578 no_wait_return = FALSE;
1579# ifdef AMIGA
1580 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1581# else
1582 wait_return(msg_silent == 0);
1583# endif
1584 no_wait_return = save_nwr;
1585 }
1586#endif /* FEAT_GUI_W32 */
1587
1588#ifdef MSWIN
1589 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1590#endif
1591 starttermcap(); /* start termcap if not done by wait_return() */
1592
1593 /*
1594 * In an Amiga window redrawing is caused by asking the window size.
1595 * If we got an interrupt this will not work. The chance that the
1596 * window size is wrong is very small, but we need to redraw the
1597 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1598 * but it saves an extra redraw.
1599 */
1600#ifdef AMIGA
1601 if (skip_redraw) /* ':' hit in wait_return() */
1602 {
1603 if (msg_silent == 0)
1604 redraw_later_clear();
1605 }
1606 else if (term_console)
1607 {
1608 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1609 if (got_int && msg_silent == 0)
1610 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1611 else
1612 must_redraw = 0; /* no extra redraw needed */
1613 }
1614#endif
1615 }
1616
1617 /* display any error messages now */
1618 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001619
1620#ifdef FEAT_AUTOCMD
1621 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623}
1624
1625/*
1626 * Create a shell command from a command string, input redirection file and
1627 * output redirection file.
1628 * Returns an allocated string with the shell command, or NULL for failure.
1629 */
1630 char_u *
1631make_filter_cmd(cmd, itmp, otmp)
1632 char_u *cmd; /* command */
1633 char_u *itmp; /* NULL or name of input file */
1634 char_u *otmp; /* NULL or name of output file */
1635{
1636 char_u *buf;
1637 long_u len;
1638
Bram Moolenaar53076832015-12-31 19:53:21 +01001639#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001640 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001641 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001642
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001643 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001644 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1645 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001646 if (is_fish_shell)
1647 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1648 else
1649#endif
1650 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (itmp != NULL)
1652 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1653 if (otmp != NULL)
1654 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1655 buf = lalloc(len, TRUE);
1656 if (buf == NULL)
1657 return NULL;
1658
Bram Moolenaar53076832015-12-31 19:53:21 +01001659#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001661 * Put braces around the command (for concatenated commands) when
1662 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001664 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001665 {
1666 if (is_fish_shell)
1667 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1668 else
1669 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1670 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001671 else
1672 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (itmp != NULL)
1674 {
1675 STRCAT(buf, " < ");
1676 STRCAT(buf, itmp);
1677 }
1678#else
1679 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001680 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 * the use of commands in a pipe.
1682 */
1683 STRCPY(buf, cmd);
1684 if (itmp != NULL)
1685 {
1686 char_u *p;
1687
1688 /*
1689 * If there is a pipe, we have to put the '<' in front of it.
1690 * Don't do this when 'shellquote' is not empty, otherwise the
1691 * redirection would be inside the quotes.
1692 */
1693 if (*p_shq == NUL)
1694 {
1695 p = vim_strchr(buf, '|');
1696 if (p != NULL)
1697 *p = NUL;
1698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1700 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (*p_shq == NUL)
1702 {
1703 p = vim_strchr(cmd, '|');
1704 if (p != NULL)
1705 {
1706 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1707 STRCAT(buf, p);
1708 }
1709 }
1710 }
1711#endif
1712 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001713 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 return buf;
1716}
1717
1718/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001719 * Append output redirection for file "fname" to the end of string buffer
1720 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * Works with the 'shellredir' and 'shellpipe' options.
1722 * The caller should make sure that there is enough room:
1723 * STRLEN(opt) + STRLEN(fname) + 3
1724 */
1725 void
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001726append_redir(buf, buflen, opt, fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 char_u *buf;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001728 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 char_u *opt;
1730 char_u *fname;
1731{
1732 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001733 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001735 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001736 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001738 {
1739 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001741 if (p[1] == '%') /* skip %% */
1742 ++p;
1743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (p != NULL)
1745 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001746 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1747 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1748 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001751 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 " %s %s",
1754#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
1757 (char *)opt, (char *)fname);
1758}
1759
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001760#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001762static int no_viminfo(void);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001763static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764static int viminfo_errcnt;
1765
1766 static int
1767no_viminfo()
1768{
1769 /* "vim -i NONE" does not read or write a viminfo file */
1770 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1771}
1772
1773/*
1774 * Report an error for reading a viminfo file.
1775 * Count the number of errors. When there are more than 10, return TRUE.
1776 */
1777 int
1778viminfo_error(errnum, message, line)
1779 char *errnum;
1780 char *message;
1781 char_u *line;
1782{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001783 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1784 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001785 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001786 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1787 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 emsg(IObuff);
1789 if (++viminfo_errcnt >= 10)
1790 {
1791 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1792 return TRUE;
1793 }
1794 return FALSE;
1795}
1796
1797/*
1798 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001799 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 */
1801 int
Bram Moolenaard812df62008-11-09 12:46:09 +00001802read_viminfo(file, flags)
1803 char_u *file; /* file name or NULL to use default name */
1804 int flags; /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805{
1806 FILE *fp;
1807 char_u *fname;
1808
1809 if (no_viminfo())
1810 return FAIL;
1811
Bram Moolenaard812df62008-11-09 12:46:09 +00001812 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (fname == NULL)
1814 return FAIL;
1815 fp = mch_fopen((char *)fname, READBIN);
1816
1817 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001818 {
1819 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001820 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1821 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001822 (flags & VIF_WANT_INFO) ? _(" info") : "",
1823 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1824 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001825 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001826 verbose_leave();
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 vim_free(fname);
1830 if (fp == NULL)
1831 return FAIL;
1832
1833 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001834 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 return OK;
1838}
1839
1840/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001841 * Write the viminfo file. The old one is read in first so that effectively a
1842 * merge of current info and old info is done. This allows multiple vims to
1843 * run simultaneously, without losing any marks etc.
1844 * If "forceit" is TRUE, then the old file is not read in, and only internal
1845 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
1847 void
1848write_viminfo(file, forceit)
1849 char_u *file;
1850 int forceit;
1851{
1852 char_u *fname;
1853 FILE *fp_in = NULL; /* input viminfo file, if any */
1854 FILE *fp_out = NULL; /* output viminfo file */
1855 char_u *tempname = NULL; /* name of temp viminfo file */
1856 struct stat st_new; /* mch_stat() of potential new file */
1857 char_u *wp;
1858#if defined(UNIX) || defined(VMS)
1859 mode_t umask_save;
1860#endif
1861#ifdef UNIX
1862 int shortname = FALSE; /* use 8.3 file name */
1863 struct stat st_old; /* mch_stat() of existing viminfo file */
1864#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001865#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001866 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 if (no_viminfo())
1870 return;
1871
1872 fname = viminfo_filename(file); /* may set to default if NULL */
1873 if (fname == NULL)
1874 return;
1875
1876 fp_in = mch_fopen((char *)fname, READBIN);
1877 if (fp_in == NULL)
1878 {
1879 /* if it does exist, but we can't read it, don't try writing */
1880 if (mch_stat((char *)fname, &st_new) == 0)
1881 goto end;
1882#if defined(UNIX) || defined(VMS)
1883 /*
1884 * For Unix we create the .viminfo non-accessible for others,
1885 * because it may contain text from non-accessible documents.
1886 */
1887 umask_save = umask(077);
1888#endif
1889 fp_out = mch_fopen((char *)fname, WRITEBIN);
1890#if defined(UNIX) || defined(VMS)
1891 (void)umask(umask_save);
1892#endif
1893 }
1894 else
1895 {
1896 /*
1897 * There is an existing viminfo file. Create a temporary file to
1898 * write the new viminfo into, in the same directory as the
1899 * existing viminfo file, which will be renamed later.
1900 */
1901#ifdef UNIX
1902 /*
1903 * For Unix we check the owner of the file. It's not very nice to
1904 * overwrite a user's viminfo file after a "su root", with a
1905 * viminfo file that the user can't read.
1906 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001907 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001908 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001910 if (mch_stat((char *)fname, &st_old) == 0
1911 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001912 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 ? (st_old.st_mode & 0200)
1914 : (st_old.st_gid == getgid()
1915 ? (st_old.st_mode & 0020)
1916 : (st_old.st_mode & 0002))))
1917 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001918 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1922 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001923 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 goto end;
1925 }
1926#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001927#ifdef WIN3264
1928 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001929 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932 /*
1933 * Make tempname.
1934 * May try twice: Once normal and once with shortname set, just in
1935 * case somebody puts his viminfo file in an 8.3 filesystem.
1936 */
1937 for (;;)
1938 {
1939 tempname = buf_modname(
1940#ifdef UNIX
1941 shortname,
1942#else
1943# ifdef SHORT_FNAME
1944 TRUE,
1945# else
1946# ifdef FEAT_GUI_W32
1947 gui_is_win32s(),
1948# else
1949 FALSE,
1950# endif
1951# endif
1952#endif
1953 fname,
1954#ifdef VMS
1955 (char_u *)"-tmp",
1956#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958#endif
1959 FALSE);
1960 if (tempname == NULL) /* out of memory */
1961 break;
1962
1963 /*
1964 * Check if tempfile already exists. Never overwrite an
1965 * existing file!
1966 */
1967 if (mch_stat((char *)tempname, &st_new) == 0)
1968 {
1969#ifdef UNIX
1970 /*
1971 * Check if tempfile is same as original file. May happen
1972 * when modname() gave the same file back. E.g. silly
1973 * link, or file name-length reached. Try again with
1974 * shortname set.
1975 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001976 if (!shortname && st_new.st_dev == st_old.st_dev
1977 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 vim_free(tempname);
1980 tempname = NULL;
1981 shortname = TRUE;
1982 continue;
1983 }
1984#endif
1985 /*
1986 * Try another name. Change one character, just before
1987 * the extension. This should also work for an 8.3
1988 * file name, when after adding the extension it still is
1989 * the same file as the original.
1990 */
1991 wp = tempname + STRLEN(tempname) - 5;
1992 if (wp < gettail(tempname)) /* empty file name? */
1993 wp = gettail(tempname);
1994 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1995 --*wp)
1996 {
1997 /*
1998 * They all exist? Must be something wrong! Don't
1999 * write the viminfo file then.
2000 */
2001 if (*wp == 'a')
2002 {
2003 vim_free(tempname);
2004 tempname = NULL;
2005 break;
2006 }
2007 }
2008 }
2009 break;
2010 }
2011
2012 if (tempname != NULL)
2013 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00002014#ifdef VMS
2015 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00002016 umask_save = umask(077);
2017 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2018 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002019#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00002020 int fd;
2021
2022 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002023 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002024 * Unix: same as original file, but strip s-bit. Reset umask to
2025 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002026 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00002027# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002028 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002029 fd = mch_open((char *)tempname,
2030 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002031 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002032 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002033# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002034 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002035 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002036# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 if (fd < 0)
2038 fp_out = NULL;
2039 else
2040 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002041#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 /*
2044 * If we can't create in the same directory, try creating a
2045 * "normal" temp file.
2046 */
2047 if (fp_out == NULL)
2048 {
2049 vim_free(tempname);
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002050 if ((tempname = vim_tempname('o', TRUE)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2052 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00002053
2054#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00002056 * Make sure the owner can read/write it. This only works for
2057 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 */
2059 if (fp_out != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002060 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061#endif
2062 }
2063 }
2064
2065 /*
2066 * Check if the new viminfo file can be written to.
2067 */
2068 if (fp_out == NULL)
2069 {
2070 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002071 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (fp_in != NULL)
2073 fclose(fp_in);
2074 goto end;
2075 }
2076
2077 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002078 {
2079 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002080 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002081 verbose_leave();
2082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002085 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 fclose(fp_out); /* errors are ignored !? */
2088 if (fp_in != NULL)
2089 {
2090 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002091
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002092 /* In case of an error keep the original viminfo file. Otherwise
2093 * rename the newly written file. Give an error if that fails. */
2094 if (viminfo_errcnt == 0 && vim_rename(tempname, fname) == -1)
2095 {
2096 ++viminfo_errcnt;
2097 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2098 }
2099 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002101
2102#ifdef WIN3264
2103 /* If the viminfo file was hidden then also hide the new file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002104 if (hidden)
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002105 mch_hide(fname);
2106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109end:
2110 vim_free(fname);
2111 vim_free(tempname);
2112}
2113
2114/*
2115 * Get the viminfo file name to use.
2116 * If "file" is given and not empty, use it (has already been expanded by
2117 * cmdline functions).
2118 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2119 * expand environment variables.
2120 * Returns an allocated string. NULL when out of memory.
2121 */
2122 static char_u *
2123viminfo_filename(file)
2124 char_u *file;
2125{
2126 if (file == NULL || *file == NUL)
2127 {
2128 if (use_viminfo != NULL)
2129 file = use_viminfo;
2130 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2131 {
2132#ifdef VIMINFO_FILE2
2133 /* don't use $HOME when not defined (turned into "c:/"!). */
2134# ifdef VMS
2135 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2136# else
2137 if (mch_getenv((char_u *)"HOME") == NULL)
2138# endif
2139 {
2140 /* don't use $VIM when not available. */
2141 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2142 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2143 file = (char_u *)VIMINFO_FILE2;
2144 else
2145 file = (char_u *)VIMINFO_FILE;
2146 }
2147 else
2148#endif
2149 file = (char_u *)VIMINFO_FILE;
2150 }
2151 expand_env(file, NameBuff, MAXPATHL);
2152 file = NameBuff;
2153 }
2154 return vim_strsave(file);
2155}
2156
2157/*
2158 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2159 */
2160 static void
Bram Moolenaard812df62008-11-09 12:46:09 +00002161do_viminfo(fp_in, fp_out, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 FILE *fp_in;
2163 FILE *fp_out;
Bram Moolenaard812df62008-11-09 12:46:09 +00002164 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 int count = 0;
2167 int eof = FALSE;
2168 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002169 int merge = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2172 return;
2173 vir.vir_fd = fp_in;
2174#ifdef FEAT_MBYTE
2175 vir.vir_conv.vc_type = CONV_NONE;
2176#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002177 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 if (fp_in != NULL)
2180 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002181 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002182 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002183 eof = read_viminfo_up_to_marks(&vir,
2184 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002185 merge = TRUE;
2186 }
2187 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /* Skip info, find start of marks */
2189 while (!(eof = viminfo_readline(&vir))
2190 && vir.vir_line[0] != '>')
2191 ;
2192 }
2193 if (fp_out != NULL)
2194 {
2195 /* Write the info: */
2196 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2197 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002198 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002200 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2202#endif
2203 write_viminfo_search_pattern(fp_out);
2204 write_viminfo_sub_string(fp_out);
2205#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002206 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#endif
2208 write_viminfo_registers(fp_out);
2209#ifdef FEAT_EVAL
2210 write_viminfo_varlist(fp_out);
2211#endif
2212 write_viminfo_filemarks(fp_out);
2213 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002214 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 count = write_viminfo_marks(fp_out);
2216 }
Bram Moolenaard812df62008-11-09 12:46:09 +00002217 if (fp_in != NULL
2218 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT)))
2219 copy_viminfo_marks(&vir, fp_out, count, eof, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 vim_free(vir.vir_line);
2222#ifdef FEAT_MBYTE
2223 if (vir.vir_conv.vc_type != CONV_NONE)
2224 convert_setup(&vir.vir_conv, NULL, NULL);
2225#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002226 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227}
2228
2229/*
2230 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2231 * first part of the viminfo file which contains everything but the marks that
2232 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2233 */
2234 static int
2235read_viminfo_up_to_marks(virp, forceit, writing)
2236 vir_T *virp;
2237 int forceit;
2238 int writing;
2239{
2240 int eof;
2241 buf_T *buf;
2242
2243#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002244 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#endif
2246 eof = viminfo_readline(virp);
2247 while (!eof && virp->vir_line[0] != '>')
2248 {
2249 switch (virp->vir_line[0])
2250 {
2251 /* Characters reserved for future expansion, ignored now */
2252 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 case '^': /* to be defined */
2254 case '<': /* long line - ignored */
2255 /* A comment or empty line. */
2256 case NUL:
2257 case '\r':
2258 case '\n':
2259 case '#':
2260 eof = viminfo_readline(virp);
2261 break;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002262 case '|': /* copy line (for future use) */
2263 if (writing)
2264 ga_add_string(&virp->vir_barlines, virp->vir_line);
2265 eof = viminfo_readline(virp);
2266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 case '*': /* "*encoding=value" */
2268 eof = viminfo_encoding(virp);
2269 break;
2270 case '!': /* global variable */
2271#ifdef FEAT_EVAL
2272 eof = read_viminfo_varlist(virp, writing);
2273#else
2274 eof = viminfo_readline(virp);
2275#endif
2276 break;
2277 case '%': /* entry for buffer list */
2278 eof = read_viminfo_bufferlist(virp, writing);
2279 break;
2280 case '"':
2281 eof = read_viminfo_register(virp, forceit);
2282 break;
2283 case '/': /* Search string */
2284 case '&': /* Substitute search string */
2285 case '~': /* Last search string, followed by '/' or '&' */
2286 eof = read_viminfo_search_pattern(virp, forceit);
2287 break;
2288 case '$':
2289 eof = read_viminfo_sub_string(virp, forceit);
2290 break;
2291 case ':':
2292 case '?':
2293 case '=':
2294 case '@':
2295#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002296 eof = read_viminfo_history(virp, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297#else
2298 eof = viminfo_readline(virp);
2299#endif
2300 break;
2301 case '-':
2302 case '\'':
2303 eof = read_viminfo_filemark(virp, forceit);
2304 break;
2305 default:
2306 if (viminfo_error("E575: ", _("Illegal starting char"),
2307 virp->vir_line))
2308 eof = TRUE;
2309 else
2310 eof = viminfo_readline(virp);
2311 break;
2312 }
2313 }
2314
2315#ifdef FEAT_CMDHIST
2316 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002317 if (!writing)
2318 finish_viminfo_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
2320
2321 /* Change file names to buffer numbers for fmarks. */
2322 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2323 fmarks_check_names(buf);
2324
2325 return eof;
2326}
2327
2328/*
2329 * Compare the 'encoding' value in the viminfo file with the current value of
2330 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2331 * conversion of text with iconv() in viminfo_readstring().
2332 */
2333 static int
2334viminfo_encoding(virp)
2335 vir_T *virp;
2336{
2337#ifdef FEAT_MBYTE
2338 char_u *p;
2339 int i;
2340
2341 if (get_viminfo_parameter('c') != 0)
2342 {
2343 p = vim_strchr(virp->vir_line, '=');
2344 if (p != NULL)
2345 {
2346 /* remove trailing newline */
2347 ++p;
2348 for (i = 0; vim_isprintc(p[i]); ++i)
2349 ;
2350 p[i] = NUL;
2351
2352 convert_setup(&virp->vir_conv, p, p_enc);
2353 }
2354 }
2355#endif
2356 return viminfo_readline(virp);
2357}
2358
2359/*
2360 * Read a line from the viminfo file.
2361 * Returns TRUE for end-of-file;
2362 */
2363 int
2364viminfo_readline(virp)
2365 vir_T *virp;
2366{
2367 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2368}
2369
2370/*
2371 * check string read from viminfo file
2372 * remove '\n' at the end of the line
2373 * - replace CTRL-V CTRL-V with CTRL-V
2374 * - replace CTRL-V 'n' with '\n'
2375 *
2376 * Check for a long line as written by viminfo_writestring().
2377 *
2378 * Return the string in allocated memory (NULL when out of memory).
2379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 char_u *
2381viminfo_readstring(virp, off, convert)
2382 vir_T *virp;
2383 int off; /* offset for virp->vir_line */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002384 int convert UNUSED; /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 char_u *retval;
2387 char_u *s, *d;
2388 long len;
2389
2390 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2391 {
2392 len = atol((char *)virp->vir_line + off + 1);
2393 retval = lalloc(len, TRUE);
2394 if (retval == NULL)
2395 {
2396 /* Line too long? File messed up? Skip next line. */
2397 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2398 return NULL;
2399 }
2400 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2401 s = retval + 1; /* Skip the leading '<' */
2402 }
2403 else
2404 {
2405 retval = vim_strsave(virp->vir_line + off);
2406 if (retval == NULL)
2407 return NULL;
2408 s = retval;
2409 }
2410
2411 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2412 d = retval;
2413 while (*s != NUL && *s != '\n')
2414 {
2415 if (s[0] == Ctrl_V && s[1] != NUL)
2416 {
2417 if (s[1] == 'n')
2418 *d++ = '\n';
2419 else
2420 *d++ = Ctrl_V;
2421 s += 2;
2422 }
2423 else
2424 *d++ = *s++;
2425 }
2426 *d = NUL;
2427
2428#ifdef FEAT_MBYTE
2429 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2430 {
2431 d = string_convert(&virp->vir_conv, retval, NULL);
2432 if (d != NULL)
2433 {
2434 vim_free(retval);
2435 retval = d;
2436 }
2437 }
2438#endif
2439
2440 return retval;
2441}
2442
2443/*
2444 * write string to viminfo file
2445 * - replace CTRL-V with CTRL-V CTRL-V
2446 * - replace '\n' with CTRL-V 'n'
2447 * - add a '\n' at the end
2448 *
2449 * For a long line:
2450 * - write " CTRL-V <length> \n " in first line
2451 * - write " < <string> \n " in second line
2452 */
2453 void
2454viminfo_writestring(fd, p)
2455 FILE *fd;
2456 char_u *p;
2457{
2458 int c;
2459 char_u *s;
2460 int len = 0;
2461
2462 for (s = p; *s != NUL; ++s)
2463 {
2464 if (*s == Ctrl_V || *s == '\n')
2465 ++len;
2466 ++len;
2467 }
2468
2469 /* If the string will be too long, write its length and put it in the next
2470 * line. Take into account that some room is needed for what comes before
2471 * the string (e.g., variable name). Add something to the length for the
2472 * '<', NL and trailing NUL. */
2473 if (len > LSIZE / 2)
2474 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2475
2476 while ((c = *p++) != NUL)
2477 {
2478 if (c == Ctrl_V || c == '\n')
2479 {
2480 putc(Ctrl_V, fd);
2481 if (c == '\n')
2482 c = 'n';
2483 }
2484 putc(c, fd);
2485 }
2486 putc('\n', fd);
2487}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002488
2489 static void
2490write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2491{
2492 int i;
2493 garray_T *gap = &virp->vir_barlines;
2494
2495 if (gap->ga_len > 0)
2496 {
2497 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2498
2499 for (i = 0; i < gap->ga_len; ++i)
2500 fputs(((char **)(gap->ga_data))[i], fp_out);
2501 }
2502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#endif /* FEAT_VIMINFO */
2504
2505/*
2506 * Implementation of ":fixdel", also used by get_stty().
2507 * <BS> resulting <Del>
2508 * ^? ^H
2509 * not ^? ^?
2510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 void
2512do_fixdel(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002513 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 char_u *p;
2516
2517 p = find_termcode((char_u *)"kb");
2518 add_termcode((char_u *)"kD", p != NULL
2519 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2520}
2521
2522 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002523print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 linenr_T lnum;
2525 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002526 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002528 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 if (curwin->w_p_nu || use_number)
2531 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002532 vim_snprintf((char *)numbuf, sizeof(numbuf),
2533 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2535 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002536 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537}
2538
2539/*
2540 * Print a text line. Also in silent mode ("ex -s").
2541 */
2542 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002543print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 linenr_T lnum;
2545 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002546 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 int save_silent = silent_mode;
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002551 silent_mode = FALSE;
2552 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2553 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (save_silent)
2555 {
2556 msg_putchar('\n');
2557 cursor_on(); /* msg_start() switches it off */
2558 out_flush();
2559 silent_mode = save_silent;
2560 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00002561 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002564 int
2565rename_buffer(new_fname)
2566 char_u *new_fname;
2567{
2568 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02002569 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002570
Bram Moolenaar7e283842013-05-30 11:43:15 +02002571#ifdef FEAT_AUTOCMD
2572 buf = curbuf;
2573 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002574 /* buffer changed, don't change name now */
2575 if (buf != curbuf)
2576 return FAIL;
2577# ifdef FEAT_EVAL
2578 if (aborting()) /* autocmds may abort script processing */
2579 return FAIL;
2580# endif
2581#endif
2582 /*
2583 * The name of the current buffer will be changed.
2584 * A new (unlisted) buffer entry needs to be made to hold the old file
2585 * name, which will become the alternate file name.
2586 * But don't set the alternate file name if the buffer didn't have a
2587 * name.
2588 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02002589 fname = curbuf->b_ffname;
2590 sfname = curbuf->b_sfname;
2591 xfname = curbuf->b_fname;
2592 curbuf->b_ffname = NULL;
2593 curbuf->b_sfname = NULL;
2594 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002595 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02002596 curbuf->b_ffname = fname;
2597 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002598 return FAIL;
2599 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02002600 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002601 if (xfname != NULL && *xfname != NUL)
2602 {
2603 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2604 if (buf != NULL && !cmdmod.keepalt)
2605 curwin->w_alt_fnum = buf->b_fnum;
2606 }
2607 vim_free(fname);
2608 vim_free(sfname);
2609#ifdef FEAT_AUTOCMD
Bram Moolenaar7e283842013-05-30 11:43:15 +02002610 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002611#endif
2612 /* Change directories when the 'acd' option is set. */
2613 DO_AUTOCHDIR
2614 return OK;
2615}
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617/*
2618 * ":file[!] [fname]".
2619 */
2620 void
2621ex_file(eap)
2622 exarg_T *eap;
2623{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002624 /* ":0file" removes the file name. Check for illegal uses ":3file",
2625 * "0file name", etc. */
2626 if (eap->addr_count > 0
2627 && (*eap->arg != NUL
2628 || eap->line2 > 0
2629 || eap->addr_count > 1))
2630 {
2631 EMSG(_(e_invarg));
2632 return;
2633 }
2634
2635 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002637 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 /* print full file name if :cd used */
2641 fileinfo(FALSE, FALSE, eap->forceit);
2642}
2643
2644/*
2645 * ":update".
2646 */
2647 void
2648ex_update(eap)
2649 exarg_T *eap;
2650{
2651 if (curbufIsChanged())
2652 (void)do_write(eap);
2653}
2654
2655/*
2656 * ":write" and ":saveas".
2657 */
2658 void
2659ex_write(eap)
2660 exarg_T *eap;
2661{
2662 if (eap->usefilter) /* input lines to shell command */
2663 do_bang(1, eap, FALSE, TRUE, FALSE);
2664 else
2665 (void)do_write(eap);
2666}
2667
2668/*
2669 * write current buffer to file 'eap->arg'
2670 * if 'eap->append' is TRUE, append to the file
2671 *
2672 * if *eap->arg == NUL write to current file
2673 *
2674 * return FAIL for failure, OK otherwise
2675 */
2676 int
2677do_write(eap)
2678 exarg_T *eap;
2679{
2680 int other;
2681 char_u *fname = NULL; /* init to shut up gcc */
2682 char_u *ffname;
2683 int retval = FAIL;
2684 char_u *free_fname = NULL;
2685#ifdef FEAT_BROWSE
2686 char_u *browse_file = NULL;
2687#endif
2688 buf_T *alt_buf = NULL;
2689
2690 if (not_writing()) /* check 'write' option */
2691 return FAIL;
2692
2693 ffname = eap->arg;
2694#ifdef FEAT_BROWSE
2695 if (cmdmod.browse)
2696 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002697 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 NULL, NULL, NULL, curbuf);
2699 if (browse_file == NULL)
2700 goto theend;
2701 ffname = browse_file;
2702 }
2703#endif
2704 if (*ffname == NUL)
2705 {
2706 if (eap->cmdidx == CMD_saveas)
2707 {
2708 EMSG(_(e_argreq));
2709 goto theend;
2710 }
2711 other = FALSE;
2712 }
2713 else
2714 {
2715 fname = ffname;
2716 free_fname = fix_fname(ffname);
2717 /*
2718 * When out-of-memory, keep unexpanded file name, because we MUST be
2719 * able to write the file in this situation.
2720 */
2721 if (free_fname != NULL)
2722 ffname = free_fname;
2723 other = otherfile(ffname);
2724 }
2725
2726 /*
2727 * If we have a new file, put its name in the list of alternate file names.
2728 */
2729 if (other)
2730 {
2731 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2732 || eap->cmdidx == CMD_saveas)
2733 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2734 else
2735 alt_buf = buflist_findname(ffname);
2736 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2737 {
2738 /* Overwriting a file that is loaded in another buffer is not a
2739 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002740 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 goto theend;
2742 }
2743 }
2744
2745 /*
2746 * Writing to the current file is not allowed in readonly mode
2747 * and a file name is required.
2748 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2749 */
2750 if (!other && (
2751#ifdef FEAT_QUICKFIX
2752 bt_dontwrite_msg(curbuf) ||
2753#endif
2754 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2755 goto theend;
2756
2757 if (!other)
2758 {
2759 ffname = curbuf->b_ffname;
2760 fname = curbuf->b_fname;
2761 /*
2762 * Not writing the whole file is only allowed with '!'.
2763 */
2764 if ( (eap->line1 != 1
2765 || eap->line2 != curbuf->b_ml.ml_line_count)
2766 && !eap->forceit
2767 && !eap->append
2768 && !p_wa)
2769 {
2770#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2771 if (p_confirm || cmdmod.confirm)
2772 {
2773 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2774 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2775 goto theend;
2776 eap->forceit = TRUE;
2777 }
2778 else
2779#endif
2780 {
2781 EMSG(_("E140: Use ! to write partial buffer"));
2782 goto theend;
2783 }
2784 }
2785 }
2786
2787 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2788 {
2789 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2790 {
2791#ifdef FEAT_AUTOCMD
2792 buf_T *was_curbuf = curbuf;
2793
2794 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002795 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796# ifdef FEAT_EVAL
2797 if (curbuf != was_curbuf || aborting())
2798# else
2799 if (curbuf != was_curbuf)
2800# endif
2801 {
2802 /* buffer changed, don't change name now */
2803 retval = FAIL;
2804 goto theend;
2805 }
2806#endif
2807 /* Exchange the file names for the current and the alternate
2808 * buffer. This makes it look like we are now editing the buffer
2809 * under the new name. Must be done before buf_write(), because
2810 * if there is no file name and 'cpo' contains 'F', it will set
2811 * the file name. */
2812 fname = alt_buf->b_fname;
2813 alt_buf->b_fname = curbuf->b_fname;
2814 curbuf->b_fname = fname;
2815 fname = alt_buf->b_ffname;
2816 alt_buf->b_ffname = curbuf->b_ffname;
2817 curbuf->b_ffname = fname;
2818 fname = alt_buf->b_sfname;
2819 alt_buf->b_sfname = curbuf->b_sfname;
2820 curbuf->b_sfname = fname;
2821 buf_name_changed(curbuf);
2822#ifdef FEAT_AUTOCMD
2823 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002824 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (!alt_buf->b_p_bl)
2826 {
2827 alt_buf->b_p_bl = TRUE;
2828 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2829 }
2830# ifdef FEAT_EVAL
2831 if (curbuf != was_curbuf || aborting())
2832# else
2833 if (curbuf != was_curbuf)
2834# endif
2835 {
2836 /* buffer changed, don't write the file */
2837 retval = FAIL;
2838 goto theend;
2839 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002840
2841 /* If 'filetype' was empty try detecting it now. */
2842 if (*curbuf->b_p_ft == NUL)
2843 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002844 if (au_has_group((char_u *)"filetypedetect"))
2845 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
2846 TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002847 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002848 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01002849
2850 /* Autocommands may have changed buffer names, esp. when
2851 * 'autochdir' is set. */
2852 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853#endif
2854 }
2855
2856 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2857 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002858
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002859 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002860 if (eap->cmdidx == CMD_saveas)
2861 {
2862 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00002863 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002864 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00002865#ifdef FEAT_WINDOWS
2866 redraw_tabline = TRUE;
2867#endif
2868 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002869 /* Change directories when the 'acd' option is set. */
2870 DO_AUTOCHDIR
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873
2874theend:
2875#ifdef FEAT_BROWSE
2876 vim_free(browse_file);
2877#endif
2878 vim_free(free_fname);
2879 return retval;
2880}
2881
2882/*
2883 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2884 * BF_NEW or BF_READERR, check for overwriting current file.
2885 * May set eap->forceit if a dialog says it's OK to overwrite.
2886 * Return OK if it's OK, FAIL if it is not.
2887 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02002888 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889check_overwrite(eap, buf, fname, ffname, other)
2890 exarg_T *eap;
2891 buf_T *buf;
2892 char_u *fname; /* file name to be used (can differ from
2893 buf->ffname) */
2894 char_u *ffname; /* full path version of fname */
2895 int other; /* writing under other name */
2896{
2897 /*
2898 * write to other file or b_flags set or not writing the whole file:
2899 * overwriting only allowed with '!'
2900 */
2901 if ( (other
2902 || (buf->b_flags & BF_NOTEDITED)
2903 || ((buf->b_flags & BF_NEW)
2904 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2905 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00002907#ifdef FEAT_QUICKFIX
2908 && !bt_nofile(buf)
2909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 && vim_fexists(ffname))
2911 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002912 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002914#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002915 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002916 if (mch_isdir(ffname))
2917 {
2918 EMSG2(_(e_isadir2), ffname);
2919 return FAIL;
2920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#endif
2922#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002923 if (p_confirm || cmdmod.confirm)
2924 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002925 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002927 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2928 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2929 return FAIL;
2930 eap->forceit = TRUE;
2931 }
2932 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002934 {
2935 EMSG(_(e_exists));
2936 return FAIL;
2937 }
2938 }
2939
2940 /* For ":w! filename" check that no swap file exists for "filename". */
2941 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002943 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002944 char_u *p;
2945 int r;
2946 char_u *swapname;
2947
2948 /* We only try the first entry in 'directory', without checking if
2949 * it's writable. If the "." directory is not writable the write
2950 * will probably fail anyway.
2951 * Use 'shortname' of the current buffer, since there is no buffer
2952 * for the written file. */
2953 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002954 {
2955 dir = alloc(5);
2956 if (dir == NULL)
2957 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002958 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002959 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002960 else
2961 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002962 dir = alloc(MAXPATHL);
2963 if (dir == NULL)
2964 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002965 p = p_dir;
2966 copy_option_part(&p, dir, MAXPATHL, ",");
2967 }
2968 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02002969 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002970 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002971 if (r)
2972 {
2973#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2974 if (p_confirm || cmdmod.confirm)
2975 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002976 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002977
2978 dialog_msg(buff,
2979 _("Swap file \"%s\" exists, overwrite anyway?"),
2980 swapname);
2981 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2982 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002983 {
2984 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002985 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002986 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002987 eap->forceit = TRUE;
2988 }
2989 else
2990#endif
2991 {
2992 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2993 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002994 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002995 return FAIL;
2996 }
2997 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002998 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000 }
3001 return OK;
3002}
3003
3004/*
3005 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3006 */
3007 void
3008ex_wnext(eap)
3009 exarg_T *eap;
3010{
3011 int i;
3012
3013 if (eap->cmd[1] == 'n')
3014 i = curwin->w_arg_idx + (int)eap->line2;
3015 else
3016 i = curwin->w_arg_idx - (int)eap->line2;
3017 eap->line1 = 1;
3018 eap->line2 = curbuf->b_ml.ml_line_count;
3019 if (do_write(eap) != FAIL)
3020 do_argfile(eap, i);
3021}
3022
3023/*
3024 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3025 */
3026 void
3027do_wqall(eap)
3028 exarg_T *eap;
3029{
3030 buf_T *buf;
3031 int error = 0;
3032 int save_forceit = eap->forceit;
3033
3034 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3035 exiting = TRUE;
3036
3037 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3038 {
3039 if (bufIsChanged(buf))
3040 {
3041 /*
3042 * Check if there is a reason the buffer cannot be written:
3043 * 1. if the 'write' option is set
3044 * 2. if there is no file name (even after browsing)
3045 * 3. if the 'readonly' is set (even after a dialog)
3046 * 4. if overwriting is allowed (even after a dialog)
3047 */
3048 if (not_writing())
3049 {
3050 ++error;
3051 break;
3052 }
3053#ifdef FEAT_BROWSE
3054 /* ":browse wall": ask for file name if there isn't one */
3055 if (buf->b_ffname == NULL && cmdmod.browse)
3056 browse_save_fname(buf);
3057#endif
3058 if (buf->b_ffname == NULL)
3059 {
3060 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3061 ++error;
3062 }
3063 else if (check_readonly(&eap->forceit, buf)
3064 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3065 FALSE) == FAIL)
3066 {
3067 ++error;
3068 }
3069 else
3070 {
3071 if (buf_write_all(buf, eap->forceit) == FAIL)
3072 ++error;
3073#ifdef FEAT_AUTOCMD
3074 /* an autocommand may have deleted the buffer */
3075 if (!buf_valid(buf))
3076 buf = firstbuf;
3077#endif
3078 }
3079 eap->forceit = save_forceit; /* check_overwrite() may set it */
3080 }
3081 }
3082 if (exiting)
3083 {
3084 if (!error)
3085 getout(0); /* exit Vim */
3086 not_exiting();
3087 }
3088}
3089
3090/*
3091 * Check the 'write' option.
3092 * Return TRUE and give a message when it's not st.
3093 */
3094 int
3095not_writing()
3096{
3097 if (p_write)
3098 return FALSE;
3099 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3100 return TRUE;
3101}
3102
3103/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003104 * Check if a buffer is read-only (either 'readonly' option is set or file is
3105 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3106 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
3108 static int
3109check_readonly(forceit, buf)
3110 int *forceit;
3111 buf_T *buf;
3112{
Bram Moolenaar5386a122007-06-28 20:02:32 +00003113 struct stat st;
3114
3115 /* Handle a file being readonly when the 'readonly' option is set or when
3116 * the file exists and permissions are read-only.
3117 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3118 * important for device checks but not here. */
3119 if (!*forceit && (buf->b_p_ro
3120 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3121 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
3123#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3124 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3125 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003126 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaar5386a122007-06-28 20:02:32 +00003128 if (buf->b_p_ro)
3129 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3130 buf->b_fname);
3131 else
3132 dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 buf->b_fname);
3134
3135 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3136 {
3137 /* Set forceit, to force the writing of a readonly file */
3138 *forceit = TRUE;
3139 return FALSE;
3140 }
3141 else
3142 return TRUE;
3143 }
3144 else
3145#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003146 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003148 else
3149 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3150 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 return TRUE;
3152 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return FALSE;
3155}
3156
3157/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003158 * Try to abandon current file and edit a new or existing file.
3159 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003161 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003162 * -1 for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 * 'lnum' is the line number for the cursor in the new file (if non-zero).
3164 */
3165 int
3166getfile(fnum, ffname, sfname, setpm, lnum, forceit)
3167 int fnum;
3168 char_u *ffname;
3169 char_u *sfname;
3170 int setpm;
3171 linenr_T lnum;
3172 int forceit;
3173{
3174 int other;
3175 int retval;
3176 char_u *free_me = NULL;
3177
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003178 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003180#ifdef FEAT_AUTOCMD
3181 if (curbuf_locked())
3182 return 1;
3183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 if (fnum == 0)
3186 {
3187 /* make ffname full path, set sfname */
3188 fname_expand(curbuf, &ffname, &sfname);
3189 other = otherfile(ffname);
3190 free_me = ffname; /* has been allocated, free() later */
3191 }
3192 else
3193 other = (fnum != curbuf->b_fnum);
3194
3195 if (other)
3196 ++no_wait_return; /* don't wait for autowrite message */
3197 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
3198 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3199 {
3200#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3201 if (p_confirm && p_write)
3202 dialog_changed(curbuf, FALSE);
3203 if (curbufIsChanged())
3204#endif
3205 {
3206 if (other)
3207 --no_wait_return;
3208 EMSG(_(e_nowrtmsg));
3209 retval = 2; /* file has been changed */
3210 goto theend;
3211 }
3212 }
3213 if (other)
3214 --no_wait_return;
3215 if (setpm)
3216 setpcmark();
3217 if (!other)
3218 {
3219 if (lnum != 0)
3220 curwin->w_cursor.lnum = lnum;
3221 check_cursor_lnum();
3222 beginline(BL_SOL | BL_FIX);
3223 retval = 0; /* it's in the same file */
3224 }
3225 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003226 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
3227 curwin) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 retval = -1; /* opened another file */
3229 else
3230 retval = 1; /* error encountered */
3231
3232theend:
3233 vim_free(free_me);
3234 return retval;
3235}
3236
3237/*
3238 * start editing a new file
3239 *
3240 * fnum: file number; if zero use ffname/sfname
3241 * ffname: the file name
3242 * - full path if sfname used,
3243 * - any file name if sfname is NULL
3244 * - empty string to re-edit with the same file name (but may be
3245 * in a different directory)
3246 * - NULL to start an empty buffer
3247 * sfname: the short file name (or NULL)
3248 * eap: contains the command to be executed after loading the file and
3249 * forced 'ff' and 'fenc'
3250 * newlnum: if > 0: put cursor on this line number (if possible)
3251 * if ECMD_LASTL: use last position in loaded file
3252 * if ECMD_LAST: use last position in all files
3253 * if ECMD_ONE: use first line
3254 * flags:
3255 * ECMD_HIDE: if TRUE don't free the current buffer
3256 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3257 * ECMD_OLDBUF: use existing buffer if it exists
3258 * ECMD_FORCEIT: ! used for Ex command
3259 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003261 * window, NULL when splitting the window first. When not NULL info
3262 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 *
3264 * return FAIL for failure, OK otherwise
3265 */
3266 int
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003267do_ecmd(fnum, ffname, sfname, eap, newlnum, flags, oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 int fnum;
3269 char_u *ffname;
3270 char_u *sfname;
3271 exarg_T *eap; /* can be NULL! */
3272 linenr_T newlnum;
3273 int flags;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003274 win_T *oldwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 int other_file; /* TRUE if editing another file */
3277 int oldbuf; /* TRUE if using existing buffer */
3278#ifdef FEAT_AUTOCMD
3279 int auto_buf = FALSE; /* TRUE if autocommands brought us
3280 into the buffer unexpectedly */
3281 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003282 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#endif
3284 buf_T *buf;
3285#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3286 buf_T *old_curbuf = curbuf;
3287#endif
3288 char_u *free_fname = NULL;
3289#ifdef FEAT_BROWSE
3290 char_u *browse_file = NULL;
3291#endif
3292 int retval = FAIL;
3293 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003294 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 linenr_T topline = 0;
3296 int newcol = -1;
3297 int solcol = -1;
3298 pos_T *pos;
3299#ifdef FEAT_SUN_WORKSHOP
3300 char_u *cp;
3301#endif
3302 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003303#ifdef FEAT_SPELL
3304 int did_get_winopts = FALSE;
3305#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003306 int readfile_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
3308 if (eap != NULL)
3309 command = eap->do_ecmd_cmd;
3310
3311 if (fnum != 0)
3312 {
3313 if (fnum == curbuf->b_fnum) /* file is already being edited */
3314 return OK; /* nothing to do */
3315 other_file = TRUE;
3316 }
3317 else
3318 {
3319#ifdef FEAT_BROWSE
3320 if (cmdmod.browse)
3321 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003322# ifdef FEAT_AUTOCMD
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003323 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003324# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003325 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003326# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003327 au_has_group((char_u *)"FileExplorer"))
3328 {
3329 /* No browsing supported but we do have the file explorer:
3330 * Edit the directory. */
3331 if (ffname == NULL || !mch_isdir(ffname))
3332 ffname = (char_u *)".";
3333 }
3334 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003335# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003336 {
3337 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003339 if (browse_file == NULL)
3340 goto theend;
3341 ffname = browse_file;
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344#endif
3345 /* if no short name given, use ffname for short name */
3346 if (sfname == NULL)
3347 sfname = ffname;
3348#ifdef USE_FNAME_CASE
3349# ifdef USE_LONG_FNAME
3350 if (USE_LONG_FNAME)
3351# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003352 if (sfname != NULL)
3353 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355
3356#ifdef FEAT_LISTCMDS
3357 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3358 goto theend;
3359#endif
3360
3361 if (ffname == NULL)
3362 other_file = TRUE;
3363 /* there is no file name */
3364 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3365 other_file = FALSE;
3366 else
3367 {
3368 if (*ffname == NUL) /* re-edit with same file name */
3369 {
3370 ffname = curbuf->b_ffname;
3371 sfname = curbuf->b_fname;
3372 }
3373 free_fname = fix_fname(ffname); /* may expand to full path name */
3374 if (free_fname != NULL)
3375 ffname = free_fname;
3376 other_file = otherfile(ffname);
3377#ifdef FEAT_SUN_WORKSHOP
3378 if (usingSunWorkShop && p_acd
3379 && (cp = vim_strrchr(sfname, '/')) != NULL)
3380 sfname = ++cp;
3381#endif
3382 }
3383 }
3384
3385 /*
3386 * if the file was changed we may not be allowed to abandon it
3387 * - if we are going to re-edit the same file
3388 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3389 */
3390 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3391 || (curbuf->b_nwindows == 1
3392 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003393 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3394 | (other_file ? 0 : CCGD_MULTWIN)
3395 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3396 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
3398 if (fnum == 0 && other_file && ffname != NULL)
3399 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3400 goto theend;
3401 }
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /*
3404 * End Visual mode before switching to another buffer, so the text can be
3405 * copied into the GUI selection buffer.
3406 */
3407 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003409#ifdef FEAT_AUTOCMD
3410 if ((command != NULL || newlnum > (linenr_T)0)
3411 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3412 {
3413 int len;
3414 char_u *p;
3415
3416 /* Set v:swapcommand for the SwapExists autocommands. */
3417 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003418 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003419 else
3420 len = 30;
3421 p = alloc((unsigned)len);
3422 if (p != NULL)
3423 {
3424 if (command != NULL)
3425 vim_snprintf((char *)p, len, ":%s\r", command);
3426 else
3427 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3428 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3429 did_set_swapcommand = TRUE;
3430 vim_free(p);
3431 }
3432 }
3433#endif
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 /*
3436 * If we are starting to edit another file, open a (new) buffer.
3437 * Otherwise we re-use the current buffer.
3438 */
3439 if (other_file)
3440 {
3441#ifdef FEAT_LISTCMDS
3442 if (!(flags & ECMD_ADDBUF))
3443#endif
3444 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003445 if (!cmdmod.keepalt)
3446 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003447 if (oldwin != NULL)
3448 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450
3451 if (fnum)
3452 buf = buflist_findnr(fnum);
3453 else
3454 {
3455#ifdef FEAT_LISTCMDS
3456 if (flags & ECMD_ADDBUF)
3457 {
3458 linenr_T tlnum = 1L;
3459
3460 if (command != NULL)
3461 {
3462 tlnum = atol((char *)command);
3463 if (tlnum <= 0)
3464 tlnum = 1L;
3465 }
3466 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3467 goto theend;
3468 }
3469#endif
3470 buf = buflist_new(ffname, sfname, 0L,
3471 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003472#ifdef FEAT_AUTOCMD
3473 /* autocommands may change curwin and curbuf */
3474 if (oldwin != NULL)
3475 oldwin = curwin;
3476 old_curbuf = curbuf;
3477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 if (buf == NULL)
3480 goto theend;
3481 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3482 {
3483 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485 else /* existing memfile */
3486 {
3487 oldbuf = TRUE;
3488 (void)buf_check_timestamp(buf, FALSE);
3489 /* Check if autocommands made buffer invalid or changed the current
3490 * buffer. */
3491 if (!buf_valid(buf)
3492#ifdef FEAT_AUTOCMD
3493 || curbuf != old_curbuf
3494#endif
3495 )
3496 goto theend;
3497#ifdef FEAT_EVAL
3498 if (aborting()) /* autocmds may abort script processing */
3499 goto theend;
3500#endif
3501 }
3502
3503 /* May jump to last used line number for a loaded buffer or when asked
3504 * for explicitly */
3505 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3506 {
3507 pos = buflist_findfpos(buf);
3508 newlnum = pos->lnum;
3509 solcol = pos->col;
3510 }
3511
3512 /*
3513 * Make the (new) buffer the one used by the current window.
3514 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3515 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003516 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
3518 if (buf != curbuf)
3519 {
3520#ifdef FEAT_AUTOCMD
3521 /*
3522 * Be careful: The autocommands may delete any buffer and change
3523 * the current buffer.
3524 * - If the buffer we are going to edit is deleted, give up.
3525 * - If the current buffer is deleted, prefer to load the new
3526 * buffer when loading a buffer is required. This avoids
3527 * loading another buffer which then must be closed again.
3528 * - If we ended up in the new buffer already, need to skip a few
3529 * things, set auto_buf.
3530 */
3531 if (buf->b_fname != NULL)
3532 new_name = vim_strsave(buf->b_fname);
3533 au_new_curbuf = buf;
3534 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3535 if (!buf_valid(buf)) /* new buffer has been deleted */
3536 {
3537 delbuf_msg(new_name); /* frees new_name */
3538 goto theend;
3539 }
3540# ifdef FEAT_EVAL
3541 if (aborting()) /* autocmds may abort script processing */
3542 {
3543 vim_free(new_name);
3544 goto theend;
3545 }
3546# endif
3547 if (buf == curbuf) /* already in new buffer */
3548 auto_buf = TRUE;
3549 else
3550 {
3551 if (curbuf == old_curbuf)
3552#endif
3553 buf_copy_options(buf, BCO_ENTER);
3554
3555 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003556 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003557 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003558 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560#ifdef FEAT_AUTOCMD
Bram Moolenaarfc573802011-12-30 15:01:59 +01003561 /* Autocommands may open a new window and leave oldwin open
3562 * which leads to crashes since the above call sets
3563 * oldwin->w_buffer to NULL. */
3564 if (curwin != oldwin && oldwin != aucmd_win
3565 && win_valid(oldwin) && oldwin->w_buffer == NULL)
3566 win_close(oldwin, FALSE);
3567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568# ifdef FEAT_EVAL
3569 if (aborting()) /* autocmds may abort script processing */
3570 {
3571 vim_free(new_name);
3572 goto theend;
3573 }
3574# endif
3575 /* Be careful again, like above. */
3576 if (!buf_valid(buf)) /* new buffer has been deleted */
3577 {
3578 delbuf_msg(new_name); /* frees new_name */
3579 goto theend;
3580 }
3581 if (buf == curbuf) /* already in new buffer */
3582 auto_buf = TRUE;
3583 else
3584#endif
3585 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003586#ifdef FEAT_SYN_HL
3587 /*
3588 * <VN> We could instead free the synblock
3589 * and re-attach to buffer, perhaps.
3590 */
3591 if (curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02003592 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 curwin->w_buffer = buf;
3595 curbuf = buf;
3596 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003597
3598 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
3599 if (!oldbuf && eap != NULL)
3600 {
3601 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003602#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003603 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003604#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607
3608 /* May get the window options from the last time this buffer
3609 * was in this window (or another window). If not used
3610 * before, reset the local window options to the global
3611 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00003612 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003613#ifdef FEAT_SPELL
3614 did_get_winopts = TRUE;
3615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617#ifdef FEAT_AUTOCMD
3618 }
3619 vim_free(new_name);
3620 au_new_curbuf = NULL;
3621#endif
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 curwin->w_pcmark.lnum = 1;
3625 curwin->w_pcmark.col = 0;
3626 }
3627 else /* !other_file */
3628 {
3629 if (
3630#ifdef FEAT_LISTCMDS
3631 (flags & ECMD_ADDBUF) ||
3632#endif
3633 check_fname() == FAIL)
3634 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01003635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 oldbuf = (flags & ECMD_OLDBUF);
3637 }
3638
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003639#ifdef FEAT_AUTOCMD
3640 buf = curbuf;
3641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3643 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003644 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 else
3647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 /* Don't make a buffer listed if it's a help buffer. Useful when
3649 * using CTRL-O to go back to a help file. */
3650 if (!curbuf->b_help)
3651 set_buflisted(TRUE);
3652 }
3653
3654#ifdef FEAT_AUTOCMD
3655 /* If autocommands change buffers under our fingers, forget about
3656 * editing the file. */
3657 if (buf != curbuf)
3658 goto theend;
3659# ifdef FEAT_EVAL
3660 if (aborting()) /* autocmds may abort script processing */
3661 goto theend;
3662# endif
3663
3664 /* Since we are starting to edit a file, consider the filetype to be
3665 * unset. Helps for when an autocommand changes files and expects syntax
3666 * highlighting to work in the other file. */
3667 did_filetype = FALSE;
3668#endif
3669
3670/*
3671 * other_file oldbuf
3672 * FALSE FALSE re-edit same file, buffer is re-used
3673 * FALSE TRUE re-edit same file, nothing changes
3674 * TRUE FALSE start editing new file, new buffer
3675 * TRUE TRUE start editing in existing buffer (nothing to do)
3676 */
3677 if (!other_file && !oldbuf) /* re-use the buffer */
3678 {
3679 set_last_cursor(curwin); /* may set b_last_cursor */
3680 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3681 {
3682 newlnum = curwin->w_cursor.lnum;
3683 solcol = curwin->w_cursor.col;
3684 }
3685#ifdef FEAT_AUTOCMD
3686 buf = curbuf;
3687 if (buf->b_fname != NULL)
3688 new_name = vim_strsave(buf->b_fname);
3689 else
3690 new_name = NULL;
3691#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003692 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
3693 {
3694 /* Save all the text, so that the reload can be undone.
3695 * Sync first so that this is a separate undo-able action. */
3696 u_sync(FALSE);
3697 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
3698 == FAIL)
3699 goto theend;
3700 u_unchanged(curbuf);
3701 buf_freeall(curbuf, BFA_KEEP_UNDO);
3702
3703 /* tell readfile() not to clear or reload undo info */
3704 readfile_flags = READ_KEEP_UNDO;
3705 }
3706 else
3707 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708#ifdef FEAT_AUTOCMD
3709 /* If autocommands deleted the buffer we were going to re-edit, give
3710 * up and jump to the end. */
3711 if (!buf_valid(buf))
3712 {
3713 delbuf_msg(new_name); /* frees new_name */
3714 goto theend;
3715 }
3716 vim_free(new_name);
3717
3718 /* If autocommands change buffers under our fingers, forget about
3719 * re-editing the file. Should do the buf_clear_file(), but perhaps
3720 * the autocommands changed the buffer... */
3721 if (buf != curbuf)
3722 goto theend;
3723# ifdef FEAT_EVAL
3724 if (aborting()) /* autocmds may abort script processing */
3725 goto theend;
3726# endif
3727#endif
3728 buf_clear_file(curbuf);
3729 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3730 curbuf->b_op_end.lnum = 0;
3731 }
3732
3733/*
3734 * If we get here we are sure to start editing
3735 */
3736 /* don't redraw until the cursor is in the right line */
3737 ++RedrawingDisabled;
3738
3739 /* Assume success now */
3740 retval = OK;
3741
3742 /*
3743 * Reset cursor position, could be used by autocommands.
3744 */
3745 check_cursor();
3746
3747 /*
3748 * Check if we are editing the w_arg_idx file in the argument list.
3749 */
3750 check_arg_idx(curwin);
3751
3752#ifdef FEAT_AUTOCMD
3753 if (!auto_buf)
3754#endif
3755 {
3756 /*
3757 * Set cursor and init window before reading the file and executing
3758 * autocommands. This allows for the autocommands to position the
3759 * cursor.
3760 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003761 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003764 /* It's possible that all lines in the buffer changed. Need to update
3765 * automatic folding for all windows where it's used. */
3766# ifdef FEAT_WINDOWS
3767 {
3768 win_T *win;
3769 tabpage_T *tp;
3770
3771 FOR_ALL_TAB_WINDOWS(tp, win)
3772 if (win->w_buffer == curbuf)
3773 foldUpdateAll(win);
3774 }
3775# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 foldUpdateAll(curwin);
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#endif
3779
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003780 /* Change directories when the 'acd' option is set. */
3781 DO_AUTOCHDIR
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 /*
3784 * Careful: open_buffer() and apply_autocmds() may change the current
3785 * buffer and window.
3786 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01003787 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 topline = curwin->w_topline;
3789 if (!oldbuf) /* need to read the file */
3790 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003791#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 swap_exists_action = SEA_DIALOG;
3793#endif
3794 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3795
3796 /*
3797 * Open the buffer and read the file.
3798 */
3799#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003800 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 retval = FAIL;
3802#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003803 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804#endif
3805
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003806#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (swap_exists_action == SEA_QUIT)
3808 retval = FAIL;
3809 handle_swap_exists(old_curbuf);
3810#endif
3811 }
3812#ifdef FEAT_AUTOCMD
3813 else
3814 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003815 /* Read the modelines, but only to set window-local options. Any
3816 * buffer-local options have already been set and may have been
3817 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00003818 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3821 &retval);
3822 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3823 &retval);
3824 }
3825 check_arg_idx(curwin);
3826#endif
3827
Bram Moolenaareab316b2015-03-24 11:46:30 +01003828 /* If autocommands change the cursor position or topline, we should
3829 * keep it. Also when it moves within a line. */
3830 if (!equalpos(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
3832 newlnum = curwin->w_cursor.lnum;
3833 newcol = curwin->w_cursor.col;
3834 }
3835 if (curwin->w_topline == topline)
3836 topline = 0;
3837
3838 /* Even when cursor didn't move we need to recompute topline. */
3839 changed_line_abv_curs();
3840
3841#ifdef FEAT_TITLE
3842 maketitle();
3843#endif
3844 }
3845
3846#ifdef FEAT_DIFF
3847 /* Tell the diff stuff that this buffer is new and/or needs updating.
3848 * Also needed when re-editing the same buffer, because unloading will
3849 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003850 if (curwin->w_p_diff)
3851 {
3852 diff_buf_add(curbuf);
3853 diff_invalidate(curbuf);
3854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#endif
3856
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003857#ifdef FEAT_SPELL
3858 /* If the window options were changed may need to set the spell language.
3859 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003860 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
3861 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003862#endif
3863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (command == NULL)
3865 {
3866 if (newcol >= 0) /* position set by autocommands */
3867 {
3868 curwin->w_cursor.lnum = newlnum;
3869 curwin->w_cursor.col = newcol;
3870 check_cursor();
3871 }
3872 else if (newlnum > 0) /* line number from caller or old position */
3873 {
3874 curwin->w_cursor.lnum = newlnum;
3875 check_cursor_lnum();
3876 if (solcol >= 0 && !p_sol)
3877 {
3878 /* 'sol' is off: Use last known column. */
3879 curwin->w_cursor.col = solcol;
3880 check_cursor_col();
3881#ifdef FEAT_VIRTUALEDIT
3882 curwin->w_cursor.coladd = 0;
3883#endif
3884 curwin->w_set_curswant = TRUE;
3885 }
3886 else
3887 beginline(BL_SOL | BL_FIX);
3888 }
3889 else /* no line number, go to last line in Ex mode */
3890 {
3891 if (exmode_active)
3892 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3893 beginline(BL_WHITE | BL_FIX);
3894 }
3895 }
3896
3897#ifdef FEAT_WINDOWS
3898 /* Check if cursors in other windows on the same buffer are still valid */
3899 check_lnums(FALSE);
3900#endif
3901
3902 /*
3903 * Did not read the file, need to show some info about the file.
3904 * Do this after setting the cursor.
3905 */
3906 if (oldbuf
3907#ifdef FEAT_AUTOCMD
3908 && !auto_buf
3909#endif
3910 )
3911 {
3912 int msg_scroll_save = msg_scroll;
3913
3914 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3915 * message. */
3916 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3917 msg_scroll = FALSE;
3918 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3919 check_for_delay(FALSE);
3920 msg_start();
3921 msg_scroll = msg_scroll_save;
3922 msg_scrolled_ign = TRUE;
3923
3924 fileinfo(FALSE, TRUE, FALSE);
3925
3926 msg_scrolled_ign = FALSE;
3927 }
3928
3929 if (command != NULL)
3930 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3931
3932#ifdef FEAT_KEYMAP
3933 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003934 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#endif
3936
3937 --RedrawingDisabled;
3938 if (!skip_redraw)
3939 {
3940 n = p_so;
3941 if (topline == 0 && command == NULL)
3942 p_so = 999; /* force cursor halfway the window */
3943 update_topline();
3944#ifdef FEAT_SCROLLBIND
3945 curwin->w_scbind_pos = curwin->w_topline;
3946#endif
3947 p_so = n;
3948 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3949 }
3950
3951 if (p_im)
3952 need_start_insertmode = TRUE;
3953
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003954 /* Change directories when the 'acd' option is set. */
3955 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003957#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003958 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02003961 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3963# endif
3964# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003965 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003966 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967# endif
3968 }
3969#endif
3970
3971theend:
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003972#ifdef FEAT_AUTOCMD
3973 if (did_set_swapcommand)
3974 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
3975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#ifdef FEAT_BROWSE
3977 vim_free(browse_file);
3978#endif
3979 vim_free(free_fname);
3980 return retval;
3981}
3982
3983#ifdef FEAT_AUTOCMD
3984 static void
3985delbuf_msg(name)
3986 char_u *name;
3987{
3988 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3989 name == NULL ? (char_u *)"" : name);
3990 vim_free(name);
3991 au_new_curbuf = NULL;
3992}
3993#endif
3994
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003995static int append_indent = 0; /* autoindent for first line */
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997/*
3998 * ":insert" and ":append", also used by ":change"
3999 */
4000 void
4001ex_append(eap)
4002 exarg_T *eap;
4003{
4004 char_u *theline;
4005 int did_undo = FALSE;
4006 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004007 int indent = 0;
4008 char_u *p;
4009 int vcol;
4010 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004012 /* the ! flag toggles autoindent */
4013 if (eap->forceit)
4014 curbuf->b_p_ai = !curbuf->b_p_ai;
4015
4016 /* First autoindent comes from the line we start on */
4017 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4018 append_indent = get_indent_lnum(lnum);
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (eap->cmdidx != CMD_append)
4021 --lnum;
4022
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004023 /* when the buffer is empty append to line 0 and delete the dummy line */
4024 if (empty && lnum == 1)
4025 lnum = 0;
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 State = INSERT; /* behave like in Insert mode */
4028 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4029 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004030
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004031 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
4033 msg_scroll = TRUE;
4034 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004035 if (curbuf->b_p_ai)
4036 {
4037 if (append_indent >= 0)
4038 {
4039 indent = append_indent;
4040 append_indent = -1;
4041 }
4042 else if (lnum > 0)
4043 indent = get_indent_lnum(lnum);
4044 }
4045 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004047 {
4048 /* No getline() function, use the lines that follow. This ends
4049 * when there is no more. */
4050 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4051 break;
4052 p = vim_strchr(eap->nextcmd, NL);
4053 if (p == NULL)
4054 p = eap->nextcmd + STRLEN(eap->nextcmd);
4055 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4056 if (*p != NUL)
4057 ++p;
4058 eap->nextcmd = p;
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004061 {
4062 int save_State = State;
4063
4064 /* Set State to avoid the cursor shape to be set to INSERT mode
4065 * when getline() returns. */
4066 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 theline = eap->getline(
4068#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004069 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004071 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004072 State = save_State;
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004075 if (theline == NULL)
4076 break;
4077
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004078 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4079 if (ex_keep_indent)
4080 append_indent = indent;
4081
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004082 /* Look for the "." after automatic indent. */
4083 vcol = 0;
4084 for (p = theline; indent > vcol; ++p)
4085 {
4086 if (*p == ' ')
4087 ++vcol;
4088 else if (*p == TAB)
4089 vcol += 8 - vcol % 8;
4090 else
4091 break;
4092 }
4093 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004094 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4095 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
4097 vim_free(theline);
4098 break;
4099 }
4100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004101 /* don't use autoindent if nothing was typed. */
4102 if (p[0] == NUL)
4103 theline[0] = NUL;
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 did_undo = TRUE;
4106 ml_append(lnum, theline, (colnr_T)0, FALSE);
4107 appended_lines_mark(lnum, 1L);
4108
4109 vim_free(theline);
4110 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004111
4112 if (empty)
4113 {
4114 ml_delete(2L, FALSE);
4115 empty = FALSE;
4116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 State = NORMAL;
4119
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004120 if (eap->forceit)
4121 curbuf->b_p_ai = !curbuf->b_p_ai;
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004124 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * "end" is set to lnum when something has been appended, otherwise
4126 * it is the same than "start" -- Acevedo */
4127 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4128 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4129 if (eap->cmdidx != CMD_append)
4130 --curbuf->b_op_start.lnum;
4131 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4132 ? lnum : curbuf->b_op_start.lnum;
4133 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4134 curwin->w_cursor.lnum = lnum;
4135 check_cursor_lnum();
4136 beginline(BL_SOL | BL_FIX);
4137
4138 need_wait_return = FALSE; /* don't use wait_return() now */
4139 ex_no_reprint = TRUE;
4140}
4141
4142/*
4143 * ":change"
4144 */
4145 void
4146ex_change(eap)
4147 exarg_T *eap;
4148{
4149 linenr_T lnum;
4150
4151 if (eap->line2 >= eap->line1
4152 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4153 return;
4154
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004155 /* the ! flag toggles autoindent */
4156 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4157 append_indent = get_indent_lnum(eap->line1);
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4160 {
4161 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4162 break;
4163 ml_delete(eap->line1, FALSE);
4164 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004165
4166 /* make sure the cursor is not beyond the end of the file now */
4167 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4169
4170 /* ":append" on the line above the deleted lines. */
4171 eap->line2 = eap->line1;
4172 ex_append(eap);
4173}
4174
4175 void
4176ex_z(eap)
4177 exarg_T *eap;
4178{
4179 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004180 int bigness;
4181 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 int minus = 0;
4183 linenr_T start, end, curs, i;
4184 int j;
4185 linenr_T lnum = eap->line2;
4186
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004187 /* Vi compatible: ":z!" uses display height, without a count uses
4188 * 'scroll' */
4189 if (eap->forceit)
4190 bigness = curwin->w_height;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004191#ifdef FEAT_WINDOWS
Bram Moolenaar631abc32014-02-22 22:27:47 +01004192 else if (firstwin != lastwin)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004193 bigness = curwin->w_height - 3;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004194#endif
Bram Moolenaar631abc32014-02-22 22:27:47 +01004195 else
4196 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if (bigness < 1)
4198 bigness = 1;
4199
4200 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004201 kind = x;
4202 if (*kind == '-' || *kind == '+' || *kind == '='
4203 || *kind == '^' || *kind == '.')
4204 ++x;
4205 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 ++x;
4207
4208 if (*x != 0)
4209 {
4210 if (!VIM_ISDIGIT(*x))
4211 {
4212 EMSG(_("E144: non-numeric argument to :z"));
4213 return;
4214 }
4215 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004218 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004219 if (*kind == '=')
4220 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004224 /* the number of '-' and '+' multiplies the distance */
4225 if (*kind == '-' || *kind == '+')
4226 for (x = kind + 1; *x == *kind; ++x)
4227 ;
4228
4229 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
4231 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004232 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4233 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004234 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 break;
4236
4237 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004238 start = lnum - (bigness + 1) / 2 + 1;
4239 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 curs = lnum;
4241 minus = 1;
4242 break;
4243
4244 case '^':
4245 start = lnum - bigness * 2;
4246 end = lnum - bigness;
4247 curs = lnum - bigness;
4248 break;
4249
4250 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004251 start = lnum - (bigness + 1) / 2 + 1;
4252 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 curs = end;
4254 break;
4255
4256 default: /* '+' */
4257 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004258 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004259 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004260 else if (eap->addr_count == 0)
4261 ++start;
4262 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 curs = end;
4264 break;
4265 }
4266
4267 if (start < 1)
4268 start = 1;
4269
4270 if (end > curbuf->b_ml.ml_line_count)
4271 end = curbuf->b_ml.ml_line_count;
4272
4273 if (curs > curbuf->b_ml.ml_line_count)
4274 curs = curbuf->b_ml.ml_line_count;
4275
4276 for (i = start; i <= end; i++)
4277 {
4278 if (minus && i == lnum)
4279 {
4280 msg_putchar('\n');
4281
4282 for (j = 1; j < Columns; j++)
4283 msg_putchar('-');
4284 }
4285
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004286 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 if (minus && i == lnum)
4289 {
4290 msg_putchar('\n');
4291
4292 for (j = 1; j < Columns; j++)
4293 msg_putchar('-');
4294 }
4295 }
4296
4297 curwin->w_cursor.lnum = curs;
4298 ex_no_reprint = TRUE;
4299}
4300
4301/*
4302 * Check if the restricted flag is set.
4303 * If so, give an error message and return TRUE.
4304 * Otherwise, return FALSE.
4305 */
4306 int
4307check_restricted()
4308{
4309 if (restricted)
4310 {
4311 EMSG(_("E145: Shell commands not allowed in rvim"));
4312 return TRUE;
4313 }
4314 return FALSE;
4315}
4316
4317/*
4318 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4319 * If so, give an error message and return TRUE.
4320 * Otherwise, return FALSE.
4321 */
4322 int
4323check_secure()
4324{
4325 if (secure)
4326 {
4327 secure = 2;
4328 EMSG(_(e_curdir));
4329 return TRUE;
4330 }
4331#ifdef HAVE_SANDBOX
4332 /*
4333 * In the sandbox more things are not allowed, including the things
4334 * disallowed in secure mode.
4335 */
4336 if (sandbox != 0)
4337 {
4338 EMSG(_(e_sandbox));
4339 return TRUE;
4340 }
4341#endif
4342 return FALSE;
4343}
4344
4345static char_u *old_sub = NULL; /* previous substitute pattern */
4346static int global_need_beginline; /* call beginline() after ":g" */
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348/* do_sub()
4349 *
4350 * Perform a substitution from line eap->line1 to line eap->line2 using the
4351 * command pointed to by eap->arg which should be of the form:
4352 *
4353 * /pattern/substitution/{flags}
4354 *
4355 * The usual escapes are supported as described in the regexp docs.
4356 */
4357 void
4358do_sub(eap)
4359 exarg_T *eap;
4360{
4361 linenr_T lnum;
4362 long i = 0;
4363 regmmatch_T regmatch;
4364 static int do_all = FALSE; /* do multiple substitutions per line */
4365 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004366 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 static int do_error = TRUE; /* if false, ignore errors */
4368 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004369 static int do_list = FALSE; /* list last line with subs. */
4370 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 static int do_ic = 0; /* ignore case flag */
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004372 int save_do_all; /* remember user specified 'g' flag */
4373 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4375 int delimiter;
4376 int sublen;
4377 int got_quit = FALSE;
4378 int got_match = FALSE;
4379 int temp;
4380 int which_pat;
4381 char_u *cmd;
4382 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004383 linenr_T first_line = 0; /* first changed line */
4384 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 * change */
4386 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4387 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004388 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004389 char_u *sub_firstline; /* allocated copy of first sub line */
4390 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004391 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004392 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004393#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004394 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 cmd = eap->arg;
4398 if (!global_busy)
4399 {
4400 sub_nsubs = 0;
4401 sub_nlines = 0;
4402 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004403 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (eap->cmdidx == CMD_tilde)
4406 which_pat = RE_LAST; /* use last used regexp */
4407 else
4408 which_pat = RE_SUBST; /* use last substitute regexp */
4409
4410 /* new pattern and substitution */
4411 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4412 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4413 {
4414 /* don't accept alphanumeric for separator */
4415 if (isalpha(*cmd))
4416 {
4417 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4418 return;
4419 }
4420 /*
4421 * undocumented vi feature:
4422 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4423 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4424 */
4425 if (*cmd == '\\')
4426 {
4427 ++cmd;
4428 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4429 {
4430 EMSG(_(e_backslash));
4431 return;
4432 }
4433 if (*cmd != '&')
4434 which_pat = RE_SEARCH; /* use last '/' pattern */
4435 pat = (char_u *)""; /* empty search pattern */
4436 delimiter = *cmd++; /* remember delimiter character */
4437 }
4438 else /* find the end of the regexp */
4439 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004440#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4441 if (p_altkeymap && curwin->w_p_rl)
4442 lrF_sub(cmd);
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 which_pat = RE_LAST; /* use last used regexp */
4445 delimiter = *cmd++; /* remember delimiter character */
4446 pat = cmd; /* remember start of search pat */
4447 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4448 if (cmd[0] == delimiter) /* end delimiter found */
4449 *cmd++ = NUL; /* replace it with a NUL */
4450 }
4451
4452 /*
4453 * Small incompatibility: vi sees '\n' as end of the command, but in
4454 * Vim we want to use '\n' to find/substitute a NUL.
4455 */
4456 sub = cmd; /* remember the start of the substitution */
4457
4458 while (cmd[0])
4459 {
4460 if (cmd[0] == delimiter) /* end delimiter found */
4461 {
4462 *cmd++ = NUL; /* replace it with a NUL */
4463 break;
4464 }
4465 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4466 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004467 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469
4470 if (!eap->skip)
4471 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004472 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4473 if (STRCMP(sub, "%") == 0
4474 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4475 {
4476 if (old_sub == NULL) /* there is no previous command */
4477 {
4478 EMSG(_(e_nopresub));
4479 return;
4480 }
4481 sub = old_sub;
4482 }
4483 else
4484 {
4485 vim_free(old_sub);
4486 old_sub = vim_strsave(sub);
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 }
4490 else if (!eap->skip) /* use previous pattern and substitution */
4491 {
4492 if (old_sub == NULL) /* there is no previous command */
4493 {
4494 EMSG(_(e_nopresub));
4495 return;
4496 }
4497 pat = NULL; /* search_regcomp() will use previous pattern */
4498 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004499
4500 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4501 * last column after using "$". */
4502 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004505 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4506 * more efficient.
4507 * TODO: find a generic solution to make line-joining operations more
4508 * efficient, avoid allocating a string that grows in size.
4509 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004510 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004511 && *sub == NUL
4512 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4513 || *cmd == 'p' || *cmd == '#'))))
4514 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004515 linenr_T joined_lines_count;
4516
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004517 curwin->w_cursor.lnum = eap->line1;
4518 if (*cmd == 'l')
4519 eap->flags = EXFLAG_LIST;
4520 else if (*cmd == '#')
4521 eap->flags = EXFLAG_NR;
4522 else if (*cmd == 'p')
4523 eap->flags = EXFLAG_PRINT;
4524
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004525 /* The number of lines joined is the number of lines in the range plus
4526 * one. One less when the last line is included. */
4527 joined_lines_count = eap->line2 - eap->line1 + 1;
4528 if (eap->line2 < curbuf->b_ml.ml_line_count)
4529 ++joined_lines_count;
4530 if (joined_lines_count > 1)
4531 {
4532 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
4533 sub_nsubs = joined_lines_count - 1;
4534 sub_nlines = 1;
4535 (void)do_sub_msg(FALSE);
4536 ex_may_print(eap);
4537 }
4538
4539 if (!cmdmod.keeppatterns)
4540 save_re_pat(RE_SUBST, pat, p_magic);
4541#ifdef FEAT_CMDHIST
4542 /* put pattern in history */
4543 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
4544#endif
4545
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004546 return;
4547 }
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /*
4550 * Find trailing options. When '&' is used, keep old options.
4551 */
4552 if (*cmd == '&')
4553 ++cmd;
4554 else
4555 {
4556 if (!p_ed)
4557 {
4558 if (p_gd) /* default is global on */
4559 do_all = TRUE;
4560 else
4561 do_all = FALSE;
4562 do_ask = FALSE;
4563 }
4564 do_error = TRUE;
4565 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004566 do_count = FALSE;
Bram Moolenaarfe40d1a2007-07-24 09:16:38 +00004567 do_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 do_ic = 0;
4569 }
4570 while (*cmd)
4571 {
4572 /*
4573 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4574 * 'r' is never inverted.
4575 */
4576 if (*cmd == 'g')
4577 do_all = !do_all;
4578 else if (*cmd == 'c')
4579 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004580 else if (*cmd == 'n')
4581 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 else if (*cmd == 'e')
4583 do_error = !do_error;
4584 else if (*cmd == 'r') /* use last used regexp */
4585 which_pat = RE_LAST;
4586 else if (*cmd == 'p')
4587 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004588 else if (*cmd == '#')
4589 {
4590 do_print = TRUE;
4591 do_number = TRUE;
4592 }
4593 else if (*cmd == 'l')
4594 {
4595 do_print = TRUE;
4596 do_list = TRUE;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else if (*cmd == 'i') /* ignore case */
4599 do_ic = 'i';
4600 else if (*cmd == 'I') /* don't ignore case */
4601 do_ic = 'I';
4602 else
4603 break;
4604 ++cmd;
4605 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004606 if (do_count)
4607 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004609 save_do_all = do_all;
4610 save_do_ask = do_ask;
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 /*
4613 * check for a trailing count
4614 */
4615 cmd = skipwhite(cmd);
4616 if (VIM_ISDIGIT(*cmd))
4617 {
4618 i = getdigits(&cmd);
4619 if (i <= 0 && !eap->skip && do_error)
4620 {
4621 EMSG(_(e_zerocount));
4622 return;
4623 }
4624 eap->line1 = eap->line2;
4625 eap->line2 += i - 1;
4626 if (eap->line2 > curbuf->b_ml.ml_line_count)
4627 eap->line2 = curbuf->b_ml.ml_line_count;
4628 }
4629
4630 /*
4631 * check for trailing command or garbage
4632 */
4633 cmd = skipwhite(cmd);
4634 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4635 {
4636 eap->nextcmd = check_nextcmd(cmd);
4637 if (eap->nextcmd == NULL)
4638 {
4639 EMSG(_(e_trailing));
4640 return;
4641 }
4642 }
4643
4644 if (eap->skip) /* not executing commands, only parsing */
4645 return;
4646
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004647 if (!do_count && !curbuf->b_p_ma)
4648 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004649 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004650 EMSG(_(e_modifiable));
4651 return;
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4655 {
4656 if (do_error)
4657 EMSG(_(e_invcmd));
4658 return;
4659 }
4660
4661 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4662 if (do_ic == 'i')
4663 regmatch.rmm_ic = TRUE;
4664 else if (do_ic == 'I')
4665 regmatch.rmm_ic = FALSE;
4666
4667 sub_firstline = NULL;
4668
4669 /*
4670 * ~ in the substitute pattern is replaced with the old pattern.
4671 * We do it here once to avoid it to be replaced over and over again.
4672 * But don't do it when it starts with "\=", then it's an expression.
4673 */
4674 if (!(sub[0] == '\\' && sub[1] == '='))
4675 sub = regtilde(sub, p_magic);
4676
4677 /*
4678 * Check for a match on each line.
4679 */
4680 line2 = eap->line2;
4681 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4682#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4683 || aborting()
4684#endif
4685 ); ++lnum)
4686 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004687 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
4688 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (nmatch)
4690 {
4691 colnr_T copycol;
4692 colnr_T matchcol;
4693 colnr_T prev_matchcol = MAXCOL;
4694 char_u *new_end, *new_start = NULL;
4695 unsigned new_start_len = 0;
4696 char_u *p1;
4697 int did_sub = FALSE;
4698 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004699 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 long nmatch_tl = 0; /* nr of lines matched below lnum */
4701 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004702 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004703 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
4705 /*
4706 * The new text is build up step by step, to avoid too much
4707 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00004708 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 * copycol Column in the old text where we started
4710 * looking for a match; from here old text still
4711 * needs to be copied to the new text.
4712 * matchcol Column number of the old text where to look
4713 * for the next match. It's just after the
4714 * previous match or one further.
4715 * prev_matchcol Column just after the previous match (if any).
4716 * Mostly equal to matchcol, except for the first
4717 * match and after skipping an empty match.
4718 * regmatch.*pos Where the pattern matched in the old text.
4719 * new_start The new text, all that has been produced so
4720 * far.
4721 * new_end The new text, where to append new text.
4722 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004723 * lnum The line number where we found the start of
4724 * the match. Can be below the line we searched
4725 * when there is a \n before a \zs in the
4726 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 * sub_firstlnum The line number in the buffer where to look
4728 * for a match. Can be different from "lnum"
4729 * when the pattern or substitute string contains
4730 * line breaks.
4731 *
4732 * Special situations:
4733 * - When the substitute string contains a line break, the part up
4734 * to the line break is inserted in the text, but the copy of
4735 * the original line is kept. "sub_firstlnum" is adjusted for
4736 * the inserted lines.
4737 * - When the matched pattern contains a line break, the old line
4738 * is taken from the line at the end of the pattern. The lines
4739 * in the match are deleted later, "sub_firstlnum" is adjusted
4740 * accordingly.
4741 *
4742 * The new text is built up in new_start[]. It has some extra
4743 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004744 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 *
4746 * Make a copy of the old line, so it won't be taken away when
4747 * updating the screen or handling a multi-line match. The "old_"
4748 * pointers point into this copy.
4749 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004750 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 copycol = 0;
4752 matchcol = 0;
4753
4754 /* At first match, remember current cursor position. */
4755 if (!got_match)
4756 {
4757 setpcmark();
4758 got_match = TRUE;
4759 }
4760
4761 /*
4762 * Loop until nothing more to replace in this line.
4763 * 1. Handle match with empty string.
4764 * 2. If do_ask is set, ask for confirmation.
4765 * 3. substitute the string.
4766 * 4. if do_all is set, find next match
4767 * 5. break if there isn't another match in this line
4768 */
4769 for (;;)
4770 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004771 /* Advance "lnum" to the line where the match starts. The
4772 * match does not start in the first line when there is a line
4773 * break before \zs. */
4774 if (regmatch.startpos[0].lnum > 0)
4775 {
4776 lnum += regmatch.startpos[0].lnum;
4777 sub_firstlnum += regmatch.startpos[0].lnum;
4778 nmatch -= regmatch.startpos[0].lnum;
4779 vim_free(sub_firstline);
4780 sub_firstline = NULL;
4781 }
4782
4783 if (sub_firstline == NULL)
4784 {
4785 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4786 if (sub_firstline == NULL)
4787 {
4788 vim_free(new_start);
4789 goto outofmem;
4790 }
4791 }
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 /* Save the line number of the last change for the final
4794 * cursor position (just like Vi). */
4795 curwin->w_cursor.lnum = lnum;
4796 do_again = FALSE;
4797
4798 /*
4799 * 1. Match empty string does not count, except for first
4800 * match. This reproduces the strange vi behaviour.
4801 * This also catches endless loops.
4802 */
4803 if (matchcol == prev_matchcol
4804 && regmatch.endpos[0].lnum == 0
4805 && matchcol == regmatch.endpos[0].col)
4806 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004807 if (sub_firstline[matchcol] == NUL)
4808 /* We already were at the end of the line. Don't look
4809 * for a match in this line again. */
4810 skip_match = TRUE;
4811 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02004812 {
4813 /* search for a match at next column */
4814#ifdef FEAT_MBYTE
4815 if (has_mbyte)
4816 matchcol += mb_ptr2len(sub_firstline + matchcol);
4817 else
4818#endif
4819 ++matchcol;
4820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 goto skip;
4822 }
4823
4824 /* Normally we continue searching for a match just after the
4825 * previous match. */
4826 matchcol = regmatch.endpos[0].col;
4827 prev_matchcol = matchcol;
4828
4829 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004830 * 2. If do_count is set only increase the counter.
4831 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004833 if (do_count)
4834 {
4835 /* For a multi-line match, put matchcol at the NUL at
4836 * the end of the line and set nmatch to one, so that
4837 * we continue looking for a match on the next line.
4838 * Avoids that ":s/\nB\@=//gc" get stuck. */
4839 if (nmatch > 1)
4840 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004841 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004842 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00004843 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004844 }
4845 sub_nsubs++;
4846 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004847#ifdef FEAT_EVAL
4848 /* Skip the substitution, unless an expression is used,
4849 * then it is evaluated in the sandbox. */
4850 if (!(sub[0] == '\\' && sub[1] == '='))
4851#endif
4852 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004853 }
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (do_ask)
4856 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00004857 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 /* change State to CONFIRM, so that the mouse works
4860 * properly */
4861 save_State = State;
4862 State = CONFIRM;
4863#ifdef FEAT_MOUSE
4864 setmouse(); /* disable mouse in xterm */
4865#endif
4866 curwin->w_cursor.col = regmatch.startpos[0].col;
4867
4868 /* When 'cpoptions' contains "u" don't sync undo when
4869 * asking for confirmation. */
4870 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4871 ++no_u_sync;
4872
4873 /*
4874 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4875 */
4876 while (do_ask)
4877 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004878 if (exmode_active)
4879 {
4880 char_u *resp;
4881 colnr_T sc, ec;
4882
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004883 print_line_no_prefix(lnum, do_number, do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004884
4885 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4886 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4887 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004888 if (do_number || curwin->w_p_nu)
4889 {
4890 int numw = number_width(curwin) + 1;
4891 sc += numw;
4892 ec += numw;
4893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004894 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004895 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004896 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004897 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004898 msg_putchar('^');
4899
4900 resp = getexmodeline('?', NULL, 0);
4901 if (resp != NULL)
4902 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004903 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004904 vim_free(resp);
4905 }
4906 }
4907 else
4908 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004909 char_u *orig_line = NULL;
4910 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004912 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004914 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004916 /* Invert the matched string.
4917 * Remove the inversion afterwards. */
4918 temp = RedrawingDisabled;
4919 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004921 if (new_start != NULL)
4922 {
4923 /* There already was a substitution, we would
4924 * like to show this to the user. We cannot
4925 * really update the line, it would change
4926 * what matches. Temporarily replace the line
4927 * and change it back afterwards. */
4928 orig_line = vim_strsave(ml_get(lnum));
4929 if (orig_line != NULL)
4930 {
4931 char_u *new_line = concat_str(new_start,
4932 sub_firstline + copycol);
4933
4934 if (new_line == NULL)
4935 {
4936 vim_free(orig_line);
4937 orig_line = NULL;
4938 }
4939 else
4940 {
4941 /* Position the cursor relative to the
4942 * end of the line, the previous
4943 * substitute may have inserted or
4944 * deleted characters before the
4945 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01004946 len_change = (int)STRLEN(new_line)
4947 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004948 curwin->w_cursor.col += len_change;
4949 ml_replace(lnum, new_line, FALSE);
4950 }
4951 }
4952 }
4953
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004954 search_match_lines = regmatch.endpos[0].lnum
4955 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004956 search_match_endcol = regmatch.endpos[0].col
4957 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004958 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004960 update_topline();
4961 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00004962 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004963 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00004964 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004967 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004969 if (msg_row == Rows - 1)
4970 msg_didout = FALSE; /* avoid a scroll-up */
4971 msg_starthere();
4972 i = msg_scroll;
4973 msg_scroll = 0; /* truncate msg when
4974 needed */
4975 msg_no_more = TRUE;
4976 /* write message same highlighting as for
4977 * wait_return */
4978 smsg_attr(hl_attr(HLF_R),
4979 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4980 msg_no_more = FALSE;
4981 msg_scroll = i;
4982 showruler(TRUE);
4983 windgoto(msg_row, msg_col);
4984 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
4986#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004987 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004989 ++no_mapping; /* don't map this key */
4990 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004991 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004992 --allow_keys;
4993 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004995 /* clear the question */
4996 msg_didout = FALSE; /* don't scroll up */
4997 msg_col = 0;
4998 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004999
5000 /* restore the line */
5001 if (orig_line != NULL)
5002 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005003 }
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005006 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005008 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009#endif
5010 )
5011 {
5012 got_quit = TRUE;
5013 break;
5014 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005015 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005017 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005019 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
5021 /* last: replace and then stop */
5022 do_all = FALSE;
5023 line2 = lnum;
5024 break;
5025 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005026 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 do_ask = FALSE;
5029 break;
5030 }
5031#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005032 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005034 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 scrolldown_clamp();
5036#endif
5037 }
5038 State = save_State;
5039#ifdef FEAT_MOUSE
5040 setmouse();
5041#endif
5042 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5043 --no_u_sync;
5044
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005045 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 /* For a multi-line match, put matchcol at the NUL at
5048 * the end of the line and set nmatch to one, so that
5049 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005050 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5051 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (nmatch > 1)
5053 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005054 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005055 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 goto skip;
5058 }
5059 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005060 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062
5063 /* Move the cursor to the start of the match, so that we can
5064 * use "\=col("."). */
5065 curwin->w_cursor.col = regmatch.startpos[0].col;
5066
5067 /*
5068 * 3. substitute the string.
5069 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005070#ifdef FEAT_EVAL
5071 if (do_count)
5072 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005073 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005074 save_ma = curbuf->b_p_ma;
5075 curbuf->b_p_ma = FALSE;
5076 sandbox++;
5077 }
5078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005080 sublen = vim_regsub_multi(&regmatch,
5081 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005083#ifdef FEAT_EVAL
5084 if (do_count)
5085 {
5086 curbuf->b_p_ma = save_ma;
5087 sandbox--;
5088 goto skip;
5089 }
5090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaar4463f292005-09-25 22:20:24 +00005092 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005093 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005094 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5095 {
5096 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5097 skip_match = TRUE;
5098 }
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /* Need room for:
5101 * - result so far in new_start (not for first sub in line)
5102 * - original text up to match
5103 * - length of substituted part
5104 * - original text after match
5105 */
5106 if (nmatch == 1)
5107 p1 = sub_firstline;
5108 else
5109 {
5110 p1 = ml_get(sub_firstlnum + nmatch - 1);
5111 nmatch_tl += nmatch - 1;
5112 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005113 copy_len = regmatch.startpos[0].col - copycol;
5114 needed_len = copy_len + ((unsigned)STRLEN(p1)
5115 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (new_start == NULL)
5117 {
5118 /*
5119 * Get some space for a temporary buffer to do the
5120 * substitution into (and some extra space to avoid
5121 * too many calls to alloc()/free()).
5122 */
5123 new_start_len = needed_len + 50;
5124 if ((new_start = alloc_check(new_start_len)) == NULL)
5125 goto outofmem;
5126 *new_start = NUL;
5127 new_end = new_start;
5128 }
5129 else
5130 {
5131 /*
5132 * Check if the temporary buffer is long enough to do the
5133 * substitution into. If not, make it larger (with a bit
5134 * extra to avoid too many calls to alloc()/free()).
5135 */
5136 len = (unsigned)STRLEN(new_start);
5137 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005138 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 new_start_len = needed_len + 50;
5141 if ((p1 = alloc_check(new_start_len)) == NULL)
5142 {
5143 vim_free(new_start);
5144 goto outofmem;
5145 }
5146 mch_memmove(p1, new_start, (size_t)(len + 1));
5147 vim_free(new_start);
5148 new_start = p1;
5149 }
5150 new_end = new_start + len;
5151 }
5152
5153 /*
5154 * copy the text up to the part that matched
5155 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005156 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5157 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005159 (void)vim_regsub_multi(&regmatch,
5160 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 sub, new_end, TRUE, p_magic, TRUE);
5162 sub_nsubs++;
5163 did_sub = TRUE;
5164
5165 /* Move the cursor to the start of the line, to avoid that it
5166 * is beyond the end of the line after the substitution. */
5167 curwin->w_cursor.col = 0;
5168
5169 /* For a multi-line match, make a copy of the last matched
5170 * line and continue in that one. */
5171 if (nmatch > 1)
5172 {
5173 sub_firstlnum += nmatch - 1;
5174 vim_free(sub_firstline);
5175 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5176 /* When going beyond the last line, stop substituting. */
5177 if (sub_firstlnum <= line2)
5178 do_again = TRUE;
5179 else
5180 do_all = FALSE;
5181 }
5182
5183 /* Remember next character to be copied. */
5184 copycol = regmatch.endpos[0].col;
5185
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005186 if (skip_match)
5187 {
5188 /* Already hit end of the buffer, sub_firstlnum is one
5189 * less than what it ought to be. */
5190 vim_free(sub_firstline);
5191 sub_firstline = vim_strsave((char_u *)"");
5192 copycol = 0;
5193 }
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /*
5196 * Now the trick is to replace CTRL-M chars with a real line
5197 * break. This would make it impossible to insert a CTRL-M in
5198 * the text. The line break can be avoided by preceding the
5199 * CTRL-M with a backslash. To be able to insert a backslash,
5200 * they must be doubled in the string and are halved here.
5201 * That is Vi compatible.
5202 */
5203 for (p1 = new_end; *p1; ++p1)
5204 {
5205 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005206 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 else if (*p1 == CAR)
5208 {
5209 if (u_inssub(lnum) == OK) /* prepare for undo */
5210 {
5211 *p1 = NUL; /* truncate up to the CR */
5212 ml_append(lnum - 1, new_start,
5213 (colnr_T)(p1 - new_start + 1), FALSE);
5214 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
5215 if (do_ask)
5216 appended_lines(lnum - 1, 1L);
5217 else
5218 {
5219 if (first_line == 0)
5220 first_line = lnum;
5221 last_line = lnum + 1;
5222 }
5223 /* All line numbers increase. */
5224 ++sub_firstlnum;
5225 ++lnum;
5226 ++line2;
5227 /* move the cursor to the new line, like Vi */
5228 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005229 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 p1 = new_start - 1;
5232 }
5233 }
5234#ifdef FEAT_MBYTE
5235 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005236 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
5238 }
5239
5240 /*
5241 * 4. If do_all is set, find next match.
5242 * Prevent endless loop with patterns that match empty
5243 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5244 * But ":s/\n/#/" is OK.
5245 */
5246skip:
5247 /* We already know that we did the last subst when we are at
5248 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005249 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5250 * "line2" when there is a \zs in the pattern after a line
5251 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005252 lastone = (skip_match
5253 || got_int
5254 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005255 || lnum > line2
Bram Moolenaar8299df92004-07-10 09:47:34 +00005256 || !(do_all || do_again)
5257 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5258 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 nmatch = -1;
5260
5261 /*
5262 * Replace the line in the buffer when needed. This is
5263 * skipped when there are more matches.
5264 * The check for nmatch_tl is needed for when multi-line
5265 * matching must replace the lines before trying to do another
5266 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005267 * When the match starts below where we start searching also
5268 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 */
5270 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 || nmatch_tl > 0
5272 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005273 curbuf, sub_firstlnum,
5274 matchcol, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005275 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 if (new_start != NULL)
5278 {
5279 /*
5280 * Copy the rest of the line, that didn't match.
5281 * "matchcol" has to be adjusted, we use the end of
5282 * the line as reference, because the substitute may
5283 * have changed the number of characters. Same for
5284 * "prev_matchcol".
5285 */
5286 STRCAT(new_start, sub_firstline + copycol);
5287 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5288 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5289 - prev_matchcol;
5290
5291 if (u_savesub(lnum) != OK)
5292 break;
5293 ml_replace(lnum, new_start, TRUE);
5294
5295 if (nmatch_tl > 0)
5296 {
5297 /*
5298 * Matched lines have now been substituted and are
5299 * useless, delete them. The part after the match
5300 * has been appended to new_start, we don't need
5301 * it in the buffer.
5302 */
5303 ++lnum;
5304 if (u_savedel(lnum, nmatch_tl) != OK)
5305 break;
5306 for (i = 0; i < nmatch_tl; ++i)
5307 ml_delete(lnum, (int)FALSE);
5308 mark_adjust(lnum, lnum + nmatch_tl - 1,
5309 (long)MAXLNUM, -nmatch_tl);
5310 if (do_ask)
5311 deleted_lines(lnum, nmatch_tl);
5312 --lnum;
5313 line2 -= nmatch_tl; /* nr of lines decreases */
5314 nmatch_tl = 0;
5315 }
5316
5317 /* When asking, undo is saved each time, must also set
5318 * changed flag each time. */
5319 if (do_ask)
5320 changed_bytes(lnum, 0);
5321 else
5322 {
5323 if (first_line == 0)
5324 first_line = lnum;
5325 last_line = lnum + 1;
5326 }
5327
5328 sub_firstlnum = lnum;
5329 vim_free(sub_firstline); /* free the temp buffer */
5330 sub_firstline = new_start;
5331 new_start = NULL;
5332 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5333 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5334 - prev_matchcol;
5335 copycol = 0;
5336 }
5337 if (nmatch == -1 && !lastone)
5338 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005339 sub_firstlnum, matchcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 /*
5342 * 5. break if there isn't another match in this line
5343 */
5344 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005345 {
5346 /* If the match found didn't start where we were
5347 * searching, do the next search in the line where we
5348 * found the match. */
5349 if (nmatch == -1)
5350 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354
5355 line_breakcheck();
5356 }
5357
5358 if (did_sub)
5359 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005360 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 vim_free(sub_firstline); /* free the copy of the original line */
5362 sub_firstline = NULL;
5363 }
5364
5365 line_breakcheck();
5366 }
5367
5368 if (first_line != 0)
5369 {
5370 /* Need to subtract the number of added lines from "last_line" to get
5371 * the line number before the change (same as adding the number of
5372 * deleted lines). */
5373 i = curbuf->b_ml.ml_line_count - old_line_count;
5374 changed_lines(first_line, 0, last_line - i, i);
5375 }
5376
5377outofmem:
5378 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005379
5380 /* ":s/pat//n" doesn't move the cursor */
5381 if (do_count)
5382 curwin->w_cursor = old_cursor;
5383
Bram Moolenaar46475522010-03-23 17:36:29 +01005384 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
5386 /* Set the '[ and '] marks. */
5387 curbuf->b_op_start.lnum = eap->line1;
5388 curbuf->b_op_end.lnum = line2;
5389 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5390
5391 if (!global_busy)
5392 {
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005393 if (!do_ask) /* when interactive leave cursor on the match */
5394 {
5395 if (endcolumn)
5396 coladvance((colnr_T)MAXCOL);
5397 else
5398 beginline(BL_WHITE | BL_FIX);
5399 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005400 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 MSG("");
5402 }
5403 else
5404 global_need_beginline = TRUE;
5405 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005406 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 else if (!global_busy)
5409 {
5410 if (got_int) /* interrupted */
5411 EMSG(_(e_interr));
5412 else if (got_match) /* did find something but nothing substituted */
5413 MSG("");
5414 else if (do_error) /* nothing found */
5415 EMSG2(_(e_patnotf2), get_search_pat());
5416 }
5417
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005418#ifdef FEAT_FOLDING
5419 if (do_ask && hasAnyFolding(curwin))
5420 /* Cursor position may require updating */
5421 changed_window_setting();
5422#endif
5423
Bram Moolenaar473de612013-06-08 18:19:48 +02005424 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005425
5426 /* Restore the flag values, they can be used for ":&&". */
5427 do_all = save_do_all;
5428 do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429}
5430
5431/*
5432 * Give message for number of substitutions.
5433 * Can also be used after a ":global" command.
5434 * Return TRUE if a message was given.
5435 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005436 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00005437do_sub_msg(count_only)
5438 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 /*
5441 * Only report substitutions when:
5442 * - more than 'report' substitutions
5443 * - command was typed by user, or number of changed lines > 'report'
5444 * - giving messages is not disabled by 'lazyredraw'
5445 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005446 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5447 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 && messaging())
5449 {
5450 if (got_int)
5451 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005452 else
5453 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005455 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005456 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005458 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005459 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 sub_nsubs);
5461 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005462 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005463 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005465 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005466 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005469 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 return TRUE;
5471 }
5472 if (got_int)
5473 {
5474 EMSG(_(e_interr));
5475 return TRUE;
5476 }
5477 return FALSE;
5478}
5479
5480/*
5481 * Execute a global command of the form:
5482 *
5483 * g/pattern/X : execute X on all lines where pattern matches
5484 * v/pattern/X : execute X on all lines where pattern does not match
5485 *
5486 * where 'X' is an EX command
5487 *
5488 * The command character (as well as the trailing slash) is optional, and
5489 * is assumed to be 'p' if missing.
5490 *
5491 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005492 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 * for each line that has a mark. This is required because after deleting
5494 * lines we do not know where to search for the next match.
5495 */
5496 void
5497ex_global(eap)
5498 exarg_T *eap;
5499{
5500 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005501 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 int type; /* first char of cmd: 'v' or 'g' */
5503 char_u *cmd; /* command argument */
5504
5505 char_u delim; /* delimiter, normally '/' */
5506 char_u *pat;
5507 regmmatch_T regmatch;
5508 int match;
5509 int which_pat;
5510
5511 if (global_busy)
5512 {
5513 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5514 return;
5515 }
5516
5517 if (eap->forceit) /* ":global!" is like ":vglobal" */
5518 type = 'v';
5519 else
5520 type = *eap->cmd;
5521 cmd = eap->arg;
5522 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 /*
5525 * undocumented vi feature:
5526 * "\/" and "\?": use previous search pattern.
5527 * "\&": use previous substitute pattern.
5528 */
5529 if (*cmd == '\\')
5530 {
5531 ++cmd;
5532 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5533 {
5534 EMSG(_(e_backslash));
5535 return;
5536 }
5537 if (*cmd == '&')
5538 which_pat = RE_SUBST; /* use previous substitute pattern */
5539 else
5540 which_pat = RE_SEARCH; /* use previous search pattern */
5541 ++cmd;
5542 pat = (char_u *)"";
5543 }
5544 else if (*cmd == NUL)
5545 {
5546 EMSG(_("E148: Regular expression missing from global"));
5547 return;
5548 }
5549 else
5550 {
5551 delim = *cmd; /* get the delimiter */
5552 if (delim)
5553 ++cmd; /* skip delimiter if there is one */
5554 pat = cmd; /* remember start of pattern */
5555 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5556 if (cmd[0] == delim) /* end delimiter found */
5557 *cmd++ = NUL; /* replace it with a NUL */
5558 }
5559
5560#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5561 if (p_altkeymap && curwin->w_p_rl)
5562 lrFswap(pat,0);
5563#endif
5564
5565 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5566 {
5567 EMSG(_(e_invcmd));
5568 return;
5569 }
5570
5571 /*
5572 * pass 1: set marks for each (not) matching line
5573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5575 {
5576 /* a match on this line? */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005577 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5578 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if ((type == 'g' && match) || (type == 'v' && !match))
5580 {
5581 ml_setmarked(lnum);
5582 ndone++;
5583 }
5584 line_breakcheck();
5585 }
5586
5587 /*
5588 * pass 2: execute the command for each line that has been marked
5589 */
5590 if (got_int)
5591 MSG(_(e_interr));
5592 else if (ndone == 0)
5593 {
5594 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005595 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 else
Bram Moolenaarc389fd32013-03-07 16:08:35 +01005597 smsg((char_u *)_("Pattern not found: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005600 {
5601#ifdef FEAT_CLIPBOARD
5602 start_global_changes();
5603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005605#ifdef FEAT_CLIPBOARD
5606 end_global_changes();
5607#endif
5608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
5610 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar473de612013-06-08 18:19:48 +02005611 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613
5614/*
5615 * Execute "cmd" on lines marked with ml_setmarked().
5616 */
5617 void
5618global_exe(cmd)
5619 char_u *cmd;
5620{
Bram Moolenaarbb993222011-05-10 16:00:47 +02005621 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5622 buf_T *old_buf = curbuf; /* remember what buffer we started in */
5623 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 /*
5626 * Set current position only once for a global command.
5627 * If global_busy is set, setpcmark() will not do anything.
5628 * If there is an error, global_busy will be incremented.
5629 */
5630 setpcmark();
5631
5632 /* When the command writes a message, don't overwrite the command. */
5633 msg_didout = TRUE;
5634
Bram Moolenaard25bc232010-03-23 17:49:24 +01005635 sub_nsubs = 0;
5636 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 global_need_beginline = FALSE;
5638 global_busy = 1;
5639 old_lcount = curbuf->b_ml.ml_line_count;
5640 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5641 {
5642 curwin->w_cursor.lnum = lnum;
5643 curwin->w_cursor.col = 0;
5644 if (*cmd == NUL || *cmd == '\n')
5645 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5646 else
5647 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5648 ui_breakcheck();
5649 }
5650
5651 global_busy = 0;
5652 if (global_need_beginline)
5653 beginline(BL_WHITE | BL_FIX);
5654 else
5655 check_cursor(); /* cursor may be beyond the end of the line */
5656
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005657 /* the cursor may not have moved in the text but a change in a previous
5658 * line may move it on the screen */
5659 changed_line_abv_curs();
5660
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 /* If it looks like no message was written, allow overwriting the
5662 * command with the report for number of changes. */
5663 if (msg_col == 0 && msg_scrolled == 0)
5664 msg_didout = FALSE;
5665
Bram Moolenaar45667512007-05-10 19:24:43 +00005666 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02005667 * number of extra or deleted lines.
5668 * Don't report extra or deleted lines in the edge case where the buffer
5669 * we are in after execution is different from the buffer we started in. */
5670 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5672}
5673
5674#ifdef FEAT_VIMINFO
5675 int
5676read_viminfo_sub_string(virp, force)
5677 vir_T *virp;
5678 int force;
5679{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005680 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 vim_free(old_sub);
5682 if (force || old_sub == NULL)
5683 old_sub = viminfo_readstring(virp, 1, TRUE);
5684 return viminfo_readline(virp);
5685}
5686
5687 void
5688write_viminfo_sub_string(fp)
5689 FILE *fp;
5690{
5691 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5692 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005693 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 viminfo_writestring(fp, old_sub);
5695 }
5696}
5697#endif /* FEAT_VIMINFO */
5698
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005699#if defined(EXITFREE) || defined(PROTO)
5700 void
5701free_old_sub()
5702{
5703 vim_free(old_sub);
5704}
5705#endif
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5708/*
5709 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005710 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005712 int
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005713prepare_tagpreview(undo_sync)
5714 int undo_sync; /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 win_T *wp;
5717
5718# ifdef FEAT_GUI
5719 need_mouse_correct = TRUE;
5720# endif
5721
5722 /*
5723 * If there is already a preview window open, use that one.
5724 */
5725 if (!curwin->w_p_pvw)
5726 {
5727 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5728 if (wp->w_p_pvw)
5729 break;
5730 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005731 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 else
5733 {
5734 /*
5735 * There is no preview window open yet. Create one.
5736 */
5737 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5738 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005739 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 curwin->w_p_pvw = TRUE;
5741 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005742 RESET_BINDING(curwin); /* don't take over 'scrollbind'
5743 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744# ifdef FEAT_DIFF
5745 curwin->w_p_diff = FALSE; /* no 'diff' */
5746# endif
5747# ifdef FEAT_FOLDING
5748 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5749# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005750 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005753 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754}
5755
5756#endif
5757
5758
5759/*
5760 * ":help": open a read-only window on a help file
5761 */
5762 void
5763ex_help(eap)
5764 exarg_T *eap;
5765{
5766 char_u *arg;
5767 char_u *tag;
5768 FILE *helpfd; /* file descriptor of help file */
5769 int n;
5770 int i;
5771#ifdef FEAT_WINDOWS
5772 win_T *wp;
5773#endif
5774 int num_matches;
5775 char_u **matches;
5776 char_u *p;
5777 int empty_fnum = 0;
5778 int alt_fnum = 0;
5779 buf_T *buf;
5780#ifdef FEAT_MULTI_LANG
5781 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005782 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02005784#ifdef FEAT_FOLDING
5785 int old_KeyTyped = KeyTyped;
5786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 if (eap != NULL)
5789 {
5790 /*
5791 * A ":help" command ends at the first LF, or at a '|' that is
5792 * followed by some text. Set nextcmd to the following command.
5793 */
5794 for (arg = eap->arg; *arg; ++arg)
5795 {
5796 if (*arg == '\n' || *arg == '\r'
5797 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5798 {
5799 *arg++ = NUL;
5800 eap->nextcmd = arg;
5801 break;
5802 }
5803 }
5804 arg = eap->arg;
5805
Bram Moolenaar3dbde622012-04-05 16:05:05 +02005806 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
5808 EMSG(_("E478: Don't panic!"));
5809 return;
5810 }
5811
5812 if (eap->skip) /* not executing commands */
5813 return;
5814 }
5815 else
5816 arg = (char_u *)"";
5817
5818 /* remove trailing blanks */
5819 p = arg + STRLEN(arg) - 1;
5820 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5821 *p-- = NUL;
5822
5823#ifdef FEAT_MULTI_LANG
5824 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005825 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
5827
5828 /* When no argument given go to the index. */
5829 if (*arg == NUL)
5830 arg = (char_u *)"help.txt";
5831
5832 /*
5833 * Check if there is a match for the argument.
5834 */
5835 n = find_help_tags(arg, &num_matches, &matches,
5836 eap != NULL && eap->forceit);
5837
5838 i = 0;
5839#ifdef FEAT_MULTI_LANG
5840 if (n != FAIL && lang != NULL)
5841 /* Find first item with the requested language. */
5842 for (i = 0; i < num_matches; ++i)
5843 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005844 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 if (len > 3 && matches[i][len - 3] == '@'
5846 && STRICMP(matches[i] + len - 2, lang) == 0)
5847 break;
5848 }
5849#endif
5850 if (i >= num_matches || n == FAIL)
5851 {
5852#ifdef FEAT_MULTI_LANG
5853 if (lang != NULL)
5854 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5855 else
5856#endif
5857 EMSG2(_("E149: Sorry, no help for %s"), arg);
5858 if (n != FAIL)
5859 FreeWild(num_matches, matches);
5860 return;
5861 }
5862
5863 /* The first match (in the requested language) is the best match. */
5864 tag = vim_strsave(matches[i]);
5865 FreeWild(num_matches, matches);
5866
5867#ifdef FEAT_GUI
5868 need_mouse_correct = TRUE;
5869#endif
5870
5871 /*
5872 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005873 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005875 if (!curwin->w_buffer->b_help
5876#ifdef FEAT_WINDOWS
5877 || cmdmod.tab != 0
5878#endif
5879 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005882 if (cmdmod.tab != 0)
5883 wp = NULL;
5884 else
5885 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5886 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5887 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5889 win_enter(wp, TRUE);
5890 else
5891#endif
5892 {
5893 /*
5894 * There is no help window yet.
5895 * Try to open the file specified by the "helpfile" option.
5896 */
5897 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5898 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005899 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 goto erret;
5901 }
5902 fclose(helpfd);
5903
5904#ifdef FEAT_WINDOWS
5905 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005906 * specified, the current window is vertically split and
5907 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 n = WSP_HELP;
5909# ifdef FEAT_VERTSPLIT
5910 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005911 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 n |= WSP_TOP;
5913# endif
5914 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005915 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916#else
5917 /* use current window */
5918 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922#ifdef FEAT_WINDOWS
5923 if (curwin->w_height < p_hh)
5924 win_setheight((int)p_hh);
5925#endif
5926
5927 /*
5928 * Open help file (do_ecmd() will set b_help flag, readfile() will
5929 * set b_p_ro flag).
5930 * Set the alternate file to the previously edited file.
5931 */
5932 alt_fnum = curbuf->b_fnum;
5933 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005934 ECMD_HIDE + ECMD_SET_HELP,
5935#ifdef FEAT_WINDOWS
5936 NULL /* buffer is still open, don't store info */
5937#else
5938 curwin
5939#endif
5940 );
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005941 if (!cmdmod.keepalt)
5942 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 empty_fnum = curbuf->b_fnum;
5944 }
5945 }
5946
5947 if (!p_im)
5948 restart_edit = 0; /* don't want insert mode in help file */
5949
Bram Moolenaar8f535582011-09-30 17:30:31 +02005950#ifdef FEAT_FOLDING
5951 /* Restore KeyTyped, setting 'filetype=help' may reset it.
5952 * It is needed for do_tag top open folds under the cursor. */
5953 KeyTyped = old_KeyTyped;
5954#endif
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (tag != NULL)
5957 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5958
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005959 /* Delete the empty buffer if we're not using it. Careful: autocommands
5960 * may have jumped to another window, check that the buffer is not in a
5961 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5963 {
5964 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005965 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 wipe_buffer(buf, TRUE);
5967 }
5968
5969 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005970 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 curwin->w_alt_fnum = alt_fnum;
5972
5973erret:
5974 vim_free(tag);
5975}
5976
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005977/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005978 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005979 */
5980 void
5981ex_helpclose(eap)
5982 exarg_T *eap UNUSED;
5983{
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005984#if defined(FEAT_WINDOWS)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005985 win_T *win;
5986
5987 FOR_ALL_WINDOWS(win)
5988 {
5989 if (win->w_buffer->b_help)
5990 {
5991 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005992 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005993 }
5994 }
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005995#endif
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005996}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005998#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5999/*
6000 * In an argument search for a language specifiers in the form "@xx".
6001 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6002 * Returns NULL if not found.
6003 */
6004 char_u *
6005check_help_lang(arg)
6006 char_u *arg;
6007{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006008 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006009
6010 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6011 && ASCII_ISALPHA(arg[len - 1]))
6012 {
6013 arg[len - 3] = NUL; /* remove the '@' */
6014 return arg + len - 2;
6015 }
6016 return NULL;
6017}
6018#endif
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
6021 * Return a heuristic indicating how well the given string matches. The
6022 * smaller the number, the better the match. This is the order of priorities,
6023 * from best match to worst match:
6024 * - Match with least alpha-numeric characters is better.
6025 * - Match with least total characters is better.
6026 * - Match towards the start is better.
6027 * - Match starting with "+" is worse (feature instead of command)
6028 * Assumption is made that the matched_string passed has already been found to
6029 * match some string for which help is requested. webb.
6030 */
6031 int
6032help_heuristic(matched_string, offset, wrong_case)
6033 char_u *matched_string;
6034 int offset; /* offset for match */
6035 int wrong_case; /* no matching case */
6036{
6037 int num_letters;
6038 char_u *p;
6039
6040 num_letters = 0;
6041 for (p = matched_string; *p; p++)
6042 if (ASCII_ISALNUM(*p))
6043 num_letters++;
6044
6045 /*
6046 * Multiply the number of letters by 100 to give it a much bigger
6047 * weighting than the number of characters.
6048 * If there only is a match while ignoring case, add 5000.
6049 * If the match starts in the middle of a word, add 10000 to put it
6050 * somewhere in the last half.
6051 * If the match is more than 2 chars from the start, multiply by 200 to
6052 * put it after matches at the start.
6053 */
6054 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6055 && ASCII_ISALNUM(matched_string[offset - 1]))
6056 offset += 10000;
6057 else if (offset > 2)
6058 offset *= 200;
6059 if (wrong_case)
6060 offset += 5000;
6061 /* Features are less interesting than the subjects themselves, but "+"
6062 * alone is not a feature. */
6063 if (matched_string[0] == '+' && matched_string[1] != NUL)
6064 offset += 100;
6065 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6066}
6067
6068/*
6069 * Compare functions for qsort() below, that checks the help heuristics number
6070 * that has been put after the tagname by find_tags().
6071 */
6072 static int
6073#ifdef __BORLANDC__
6074_RTLENTRYF
6075#endif
6076help_compare(s1, s2)
6077 const void *s1;
6078 const void *s2;
6079{
6080 char *p1;
6081 char *p2;
6082
6083 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6084 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6085 return strcmp(p1, p2);
6086}
6087
6088/*
6089 * Find all help tags matching "arg", sort them and return in matches[], with
6090 * the number of matches in num_matches.
6091 * The matches will be sorted with a "best" match algorithm.
6092 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6093 */
6094 int
6095find_help_tags(arg, num_matches, matches, keep_lang)
6096 char_u *arg;
6097 int *num_matches;
6098 char_u ***matches;
6099 int keep_lang;
6100{
6101 char_u *s, *d;
6102 int i;
6103 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006104 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006105 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006106 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006107 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 "[count]", "[quotex]", "[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006109 "[pattern]", "\\|", "\\%$",
6110 "s/\\~", "s/\\U", "s/\\L",
6111 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006113 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006114 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006115 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006116 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 "\\[count]", "\\[quotex]", "\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006118 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006119 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006120 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 int flags;
6122
6123 d = IObuff; /* assume IObuff is long enough! */
6124
6125 /*
6126 * Recognize a few exceptions to the rule. Some strings that contain '*'
6127 * with "star". Otherwise '*' is recognized as a wildcard.
6128 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006129 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (STRCMP(arg, mtable[i]) == 0)
6131 {
6132 STRCPY(d, rtable[i]);
6133 break;
6134 }
6135
6136 if (i < 0) /* no match in table */
6137 {
6138 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6139 * Also replace "\%^" and "\%(", they match every tag too.
6140 * Also "\zs", "\z1", etc.
6141 * Also "\@<", "\@=", "\@<=", etc.
6142 * And also "\_$" and "\_^". */
6143 if (arg[0] == '\\'
6144 && ((arg[1] != NUL && arg[2] == NUL)
6145 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6146 && arg[2] != NUL)))
6147 {
6148 STRCPY(d, "/\\\\");
6149 STRCPY(d + 3, arg + 1);
6150 /* Check for "/\\_$", should be "/\\_\$" */
6151 if (d[3] == '_' && d[4] == '$')
6152 STRCPY(d + 4, "\\$");
6153 }
6154 else
6155 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006156 /* Replace:
6157 * "[:...:]" with "\[:...:]"
6158 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006159 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006160 */
6161 if ((arg[0] == '[' && (arg[1] == ':'
6162 || (arg[1] == '+' && arg[2] == '+')))
6163 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 *d++ = '\\';
6165
6166 for (s = arg; *s; ++s)
6167 {
6168 /*
6169 * Replace "|" with "bar" and '"' with "quote" to match the name of
6170 * the tags for these commands.
6171 * Replace "*" with ".*" and "?" with "." to match command line
6172 * completion.
6173 * Insert a backslash before '~', '$' and '.' to avoid their
6174 * special meaning.
6175 */
6176 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6177 break;
6178 switch (*s)
6179 {
6180 case '|': STRCPY(d, "bar");
6181 d += 3;
6182 continue;
6183 case '"': STRCPY(d, "quote");
6184 d += 5;
6185 continue;
6186 case '*': *d++ = '.';
6187 break;
6188 case '?': *d++ = '.';
6189 continue;
6190 case '$':
6191 case '.':
6192 case '~': *d++ = '\\';
6193 break;
6194 }
6195
6196 /*
6197 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6198 * ":help i_^_CTRL-D" work.
6199 * Insert '-' before and after "CTRL-X" when applicable.
6200 */
6201 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6202 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6203 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006204 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6205 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 STRCPY(d, "CTRL-");
6207 d += 5;
6208 if (*s < ' ')
6209 {
6210#ifdef EBCDIC
6211 *d++ = CtrlChar(*s);
6212#else
6213 *d++ = *s + '@';
6214#endif
6215 if (d[-1] == '\\')
6216 *d++ = '\\'; /* double a backslash */
6217 }
6218 else
6219 *d++ = *++s;
6220 if (s[1] != NUL && s[1] != '_')
6221 *d++ = '_'; /* append a '_' */
6222 continue;
6223 }
6224 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6225 *d++ = '\\';
6226
6227 /*
6228 * Insert a backslash before a backslash after a slash, for search
6229 * pattern tags: "/\|" --> "/\\|".
6230 */
6231 else if (s[0] == '\\' && s[1] != '\\'
6232 && *arg == '/' && s == arg + 1)
6233 *d++ = '\\';
6234
6235 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6236 * "CTRL-\_CTRL-N" */
6237 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6238 {
6239 STRCPY(d, "CTRL-\\\\");
6240 d += 7;
6241 s += 6;
6242 }
6243
6244 *d++ = *s;
6245
6246 /*
6247 * If tag starts with ', toss everything after a second '. Fixes
6248 * CTRL-] on 'option'. (would include the trailing '.').
6249 */
6250 if (*s == '\'' && s > arg && *arg == '\'')
6251 break;
6252 }
6253 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006254
6255 if (*IObuff == '`')
6256 {
6257 if (d > IObuff + 2 && d[-1] == '`')
6258 {
6259 /* remove the backticks from `command` */
6260 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6261 d[-2] = NUL;
6262 }
6263 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6264 {
6265 /* remove the backticks and comma from `command`, */
6266 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6267 d[-3] = NUL;
6268 }
6269 else if (d > IObuff + 4 && d[-3] == '`'
6270 && d[-2] == '\\' && d[-1] == '.')
6271 {
6272 /* remove the backticks and dot from `command`\. */
6273 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6274 d[-4] = NUL;
6275 }
6276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 }
6278 }
6279
6280 *matches = (char_u **)"";
6281 *num_matches = 0;
6282 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6283 if (keep_lang)
6284 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006285 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 /* Sort the matches found on the heuristic number that is after the
6289 * tag name. */
6290 qsort((void *)*matches, (size_t)*num_matches,
6291 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006292 /* Delete more than TAG_MANY to reduce the size of the listing. */
6293 while (*num_matches > TAG_MANY)
6294 vim_free((*matches)[--*num_matches]);
6295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 return OK;
6297}
6298
6299/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006300 * Called when starting to edit a buffer for a help file.
6301 */
6302 static void
6303prepare_help_buffer()
6304{
6305 char_u *p;
6306
6307 curbuf->b_help = TRUE;
6308#ifdef FEAT_QUICKFIX
6309 set_string_option_direct((char_u *)"buftype", -1,
6310 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6311#endif
6312
6313 /*
6314 * Always set these options after jumping to a help tag, because the
6315 * user may have an autocommand that gets in the way.
6316 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6317 * latin1 word characters (for translated help files).
6318 * Only set it when needed, buf_init_chartab() is some work.
6319 */
6320 p =
6321#ifdef EBCDIC
6322 (char_u *)"65-255,^*,^|,^\"";
6323#else
6324 (char_u *)"!-~,^*,^|,^\",192-255";
6325#endif
6326 if (STRCMP(curbuf->b_p_isk, p) != 0)
6327 {
6328 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6329 check_buf_options(curbuf);
6330 (void)buf_init_chartab(curbuf, FALSE);
6331 }
6332
Bram Moolenaar0b105412014-11-30 13:34:23 +01006333#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006334 /* Don't use the global foldmethod.*/
6335 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6336 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006337#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006338
6339 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6340 curwin->w_p_list = FALSE; /* no list mode */
6341
6342 curbuf->b_p_ma = FALSE; /* not modifiable */
6343 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6344 curwin->w_p_nu = 0; /* no line numbers */
6345 curwin->w_p_rnu = 0; /* no relative line numbers */
6346 RESET_BINDING(curwin); /* no scroll or cursor binding */
6347#ifdef FEAT_ARABIC
6348 curwin->w_p_arab = FALSE; /* no arabic mode */
6349#endif
6350#ifdef FEAT_RIGHTLEFT
6351 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6352#endif
6353#ifdef FEAT_FOLDING
6354 curwin->w_p_fen = FALSE; /* No folding in the help window */
6355#endif
6356#ifdef FEAT_DIFF
6357 curwin->w_p_diff = FALSE; /* No 'diff' */
6358#endif
6359#ifdef FEAT_SPELL
6360 curwin->w_p_spell = FALSE; /* No spell checking */
6361#endif
6362
6363 set_buflisted(FALSE);
6364}
6365
6366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * After reading a help file: May cleanup a help buffer when syntax
6368 * highlighting is not used.
6369 */
6370 void
6371fix_help_buffer()
6372{
6373 linenr_T lnum;
6374 char_u *line;
6375 int in_example = FALSE;
6376 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006377 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 char_u *p;
6379 char_u *rt;
6380 int mustfree;
6381
6382 /* set filetype to "help". */
6383 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6384
6385#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006386 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387#endif
6388 {
6389 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6390 {
6391 line = ml_get_buf(curbuf, lnum, FALSE);
6392 len = (int)STRLEN(line);
6393 if (in_example && len > 0 && !vim_iswhite(line[0]))
6394 {
6395 /* End of example: non-white or '<' in first column. */
6396 if (line[0] == '<')
6397 {
6398 /* blank-out a '<' in the first column */
6399 line = ml_get_buf(curbuf, lnum, TRUE);
6400 line[0] = ' ';
6401 }
6402 in_example = FALSE;
6403 }
6404 if (!in_example && len > 0)
6405 {
6406 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6407 {
6408 /* blank-out a '>' in the last column (start of example) */
6409 line = ml_get_buf(curbuf, lnum, TRUE);
6410 line[len - 1] = ' ';
6411 in_example = TRUE;
6412 }
6413 else if (line[len - 1] == '~')
6414 {
6415 /* blank-out a '~' at the end of line (header marker) */
6416 line = ml_get_buf(curbuf, lnum, TRUE);
6417 line[len - 1] = ' ';
6418 }
6419 }
6420 }
6421 }
6422
6423 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006424 * In the "help.txt" and "help.abx" file, add the locally added help
6425 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006427 fname = gettail(curbuf->b_fname);
6428 if (fnamecmp(fname, "help.txt") == 0
6429#ifdef FEAT_MULTI_LANG
6430 || (fnamencmp(fname, "help.", 5) == 0
6431 && ASCII_ISALPHA(fname[5])
6432 && ASCII_ISALPHA(fname[6])
6433 && TOLOWER_ASC(fname[7]) == 'x'
6434 && fname[8] == NUL)
6435#endif
6436 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
6438 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6439 {
6440 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006441 if (strstr((char *)line, "*local-additions*") == NULL)
6442 continue;
6443
6444 /* Go through all directories in 'runtimepath', skipping
6445 * $VIMRUNTIME. */
6446 p = p_rtp;
6447 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006449 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6450 mustfree = FALSE;
6451 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
6452 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006454 int fcount;
6455 char_u **fnames;
6456 FILE *fd;
6457 char_u *s;
6458 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006460 vimconv_T vc;
6461 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462#endif
6463
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006464 /* Find all "doc/ *.txt" files in this directory. */
6465 add_pathsep(NameBuff);
6466#ifdef FEAT_MULTI_LANG
6467 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006469 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006471 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6472 &fnames, EW_FILE|EW_SILENT) == OK
6473 && fcount > 0)
6474 {
6475#ifdef FEAT_MULTI_LANG
6476 int i1;
6477 int i2;
6478 char_u *f1;
6479 char_u *f2;
6480 char_u *t1;
6481 char_u *e1;
6482 char_u *e2;
6483
6484 /* If foo.abx is found use it instead of foo.txt in
6485 * the same directory. */
6486 for (i1 = 0; i1 < fcount; ++i1)
6487 {
6488 for (i2 = 0; i2 < fcount; ++i2)
6489 {
6490 if (i1 == i2)
6491 continue;
6492 if (fnames[i1] == NULL || fnames[i2] == NULL)
6493 continue;
6494 f1 = fnames[i1];
6495 f2 = fnames[i2];
6496 t1 = gettail(f1);
6497 if (fnamencmp(f1, f2, t1 - f1) != 0)
6498 continue;
6499 e1 = vim_strrchr(t1, '.');
6500 e2 = vim_strrchr(gettail(f2), '.');
6501 if (e1 == NUL || e2 == NUL)
6502 continue;
6503 if (fnamecmp(e1, ".txt") != 0
6504 && fnamecmp(e1, fname + 4) != 0)
6505 {
6506 /* Not .txt and not .abx, remove it. */
6507 vim_free(fnames[i1]);
6508 fnames[i1] = NULL;
6509 continue;
6510 }
6511 if (fnamencmp(f1, f2, e1 - f1) != 0)
6512 continue;
6513 if (fnamecmp(e1, ".txt") == 0
6514 && fnamecmp(e2, fname + 4) == 0)
6515 {
6516 /* use .abx instead of .txt */
6517 vim_free(fnames[i1]);
6518 fnames[i1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 }
6520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006522#endif
6523 for (fi = 0; fi < fcount; ++fi)
6524 {
6525 if (fnames[fi] == NULL)
6526 continue;
6527 fd = mch_fopen((char *)fnames[fi], "r");
6528 if (fd != NULL)
6529 {
6530 vim_fgets(IObuff, IOSIZE, fd);
6531 if (IObuff[0] == '*'
6532 && (s = vim_strchr(IObuff + 1, '*'))
6533 != NULL)
6534 {
6535#ifdef FEAT_MBYTE
6536 int this_utf = MAYBE;
6537#endif
6538 /* Change tag definition to a
6539 * reference and remove <CR>/<NL>. */
6540 IObuff[0] = '|';
6541 *s = '|';
6542 while (*s != NUL)
6543 {
6544 if (*s == '\r' || *s == '\n')
6545 *s = NUL;
6546#ifdef FEAT_MBYTE
6547 /* The text is utf-8 when a byte
6548 * above 127 is found and no
6549 * illegal byte sequence is found.
6550 */
6551 if (*s >= 0x80 && this_utf != FALSE)
6552 {
6553 int l;
6554
6555 this_utf = TRUE;
6556 l = utf_ptr2len(s);
6557 if (l == 1)
6558 this_utf = FALSE;
6559 s += l - 1;
6560 }
6561#endif
6562 ++s;
6563 }
6564#ifdef FEAT_MBYTE
6565 /* The help file is latin1 or utf-8;
6566 * conversion to the current
6567 * 'encoding' may be required. */
6568 vc.vc_type = CONV_NONE;
6569 convert_setup(&vc, (char_u *)(
6570 this_utf == TRUE ? "utf-8"
6571 : "latin1"), p_enc);
6572 if (vc.vc_type == CONV_NONE)
6573 /* No conversion needed. */
6574 cp = IObuff;
6575 else
6576 {
6577 /* Do the conversion. If it fails
6578 * use the unconverted text. */
6579 cp = string_convert(&vc, IObuff,
6580 NULL);
6581 if (cp == NULL)
6582 cp = IObuff;
6583 }
6584 convert_setup(&vc, NULL, NULL);
6585
6586 ml_append(lnum, cp, (colnr_T)0, FALSE);
6587 if (cp != IObuff)
6588 vim_free(cp);
6589#else
6590 ml_append(lnum, IObuff, (colnr_T)0,
6591 FALSE);
6592#endif
6593 ++lnum;
6594 }
6595 fclose(fd);
6596 }
6597 }
6598 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006601 if (mustfree)
6602 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006604 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
6606 }
6607}
6608
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006609/*
6610 * ":exusage"
6611 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006612 void
6613ex_exusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006614 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006615{
6616 do_cmdline_cmd((char_u *)"help ex-cmd-index");
6617}
6618
6619/*
6620 * ":viusage"
6621 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006622 void
6623ex_viusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006624 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006625{
6626 do_cmdline_cmd((char_u *)"help normal-index");
6627}
6628
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#if defined(FEAT_EX_EXTRA) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01006630static void helptags_one(char_u *dir, char_u *ext, char_u *lang, int add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632/*
6633 * ":helptags"
6634 */
6635 void
6636ex_helptags(eap)
6637 exarg_T *eap;
6638{
6639 garray_T ga;
6640 int i, j;
6641 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006642#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006644#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006645 expand_T xpc;
6646 char_u *dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 char_u ext[5];
6648 char_u fname[8];
6649 int filecount;
6650 char_u **files;
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006651 int add_help_tags = FALSE;
6652
6653 /* Check for ":helptags ++t {dir}". */
6654 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3]))
6655 {
6656 add_help_tags = TRUE;
6657 eap->arg = skipwhite(eap->arg + 3);
6658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar0e253142008-01-13 12:31:34 +00006660 ExpandInit(&xpc);
6661 xpc.xp_context = EXPAND_DIRECTORIES;
6662 dirname = ExpandOne(&xpc, eap->arg, NULL,
6663 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
6664 if (dirname == NULL || !mch_isdir(dirname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 EMSG2(_("E150: Not a directory: %s"), eap->arg);
Bram Moolenaar1c2836e2015-11-10 21:05:48 +01006667 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 return;
6669 }
6670
6671#ifdef FEAT_MULTI_LANG
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006672 /* Get a list of all files in the help directory and in subdirectories. */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006673 STRCPY(NameBuff, dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 add_pathsep(NameBuff);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006675 STRCAT(NameBuff, "**");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6677 EW_FILE|EW_SILENT) == FAIL
6678 || filecount == 0)
6679 {
6680 EMSG2("E151: No match: %s", NameBuff);
Bram Moolenaar0e253142008-01-13 12:31:34 +00006681 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 return;
6683 }
6684
6685 /* Go over all files in the directory to find out what languages are
6686 * present. */
6687 ga_init2(&ga, 1, 10);
6688 for (i = 0; i < filecount; ++i)
6689 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006690 len = (int)STRLEN(files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (len > 4)
6692 {
6693 if (STRICMP(files[i] + len - 4, ".txt") == 0)
6694 {
6695 /* ".txt" -> language "en" */
6696 lang[0] = 'e';
6697 lang[1] = 'n';
6698 }
6699 else if (files[i][len - 4] == '.'
6700 && ASCII_ISALPHA(files[i][len - 3])
6701 && ASCII_ISALPHA(files[i][len - 2])
6702 && TOLOWER_ASC(files[i][len - 1]) == 'x')
6703 {
6704 /* ".abx" -> language "ab" */
6705 lang[0] = TOLOWER_ASC(files[i][len - 3]);
6706 lang[1] = TOLOWER_ASC(files[i][len - 2]);
6707 }
6708 else
6709 continue;
6710
6711 /* Did we find this language already? */
6712 for (j = 0; j < ga.ga_len; j += 2)
6713 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
6714 break;
6715 if (j == ga.ga_len)
6716 {
6717 /* New language, add it. */
6718 if (ga_grow(&ga, 2) == FAIL)
6719 break;
6720 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
6721 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723 }
6724 }
6725
6726 /*
6727 * Loop over the found languages to generate a tags file for each one.
6728 */
6729 for (j = 0; j < ga.ga_len; j += 2)
6730 {
6731 STRCPY(fname, "tags-xx");
6732 fname[5] = ((char_u *)ga.ga_data)[j];
6733 fname[6] = ((char_u *)ga.ga_data)[j + 1];
6734 if (fname[5] == 'e' && fname[6] == 'n')
6735 {
6736 /* English is an exception: use ".txt" and "tags". */
6737 fname[4] = NUL;
6738 STRCPY(ext, ".txt");
6739 }
6740 else
6741 {
6742 /* Language "ab" uses ".abx" and "tags-ab". */
6743 STRCPY(ext, ".xxx");
6744 ext[1] = fname[5];
6745 ext[2] = fname[6];
6746 }
Bram Moolenaar0e253142008-01-13 12:31:34 +00006747 helptags_one(dirname, ext, fname, add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749
6750 ga_clear(&ga);
6751 FreeWild(filecount, files);
6752
6753#else
6754 /* No language support, just use "*.txt" and "tags". */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006755 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006757 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758}
6759
6760 static void
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006761helptags_one(dir, ext, tagfname, add_help_tags)
6762 char_u *dir; /* doc directory */
6763 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006764 char_u *tagfname; /* "tags" for English, "tags-fr" for French. */
6765 int add_help_tags; /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 FILE *fd_tags;
6768 FILE *fd;
6769 garray_T ga;
6770 int filecount;
6771 char_u **files;
6772 char_u *p1, *p2;
6773 int fi;
6774 char_u *s;
6775 int i;
6776 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006777 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778# ifdef FEAT_MBYTE
6779 int utf8 = MAYBE;
6780 int this_utf8;
6781 int firstline;
6782 int mix = FALSE; /* detected mixed encodings */
6783# endif
6784
6785 /*
6786 * Find all *.txt files.
6787 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01006788 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006790 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 STRCAT(NameBuff, ext);
6792 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6793 EW_FILE|EW_SILENT) == FAIL
6794 || filecount == 0)
6795 {
6796 if (!got_int)
6797 EMSG2("E151: No match: %s", NameBuff);
6798 return;
6799 }
6800
6801 /*
6802 * Open the tags file for writing.
6803 * Do this before scanning through all the files.
6804 */
6805 STRCPY(NameBuff, dir);
6806 add_pathsep(NameBuff);
6807 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006808 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (fd_tags == NULL)
6810 {
6811 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6812 FreeWild(filecount, files);
6813 return;
6814 }
6815
6816 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006817 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
6818 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 */
6820 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006821 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
6822 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {
6824 if (ga_grow(&ga, 1) == FAIL)
6825 got_int = TRUE;
6826 else
6827 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006828 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 if (s == NULL)
6830 got_int = TRUE;
6831 else
6832 {
6833 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6834 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6835 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837 }
6838 }
6839
6840 /*
6841 * Go over all the files and extract the tags.
6842 */
6843 for (fi = 0; fi < filecount && !got_int; ++fi)
6844 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006845 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (fd == NULL)
6847 {
6848 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6849 continue;
6850 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006851 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853# ifdef FEAT_MBYTE
6854 firstline = TRUE;
6855# endif
6856 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6857 {
6858# ifdef FEAT_MBYTE
6859 if (firstline)
6860 {
6861 /* Detect utf-8 file by a non-ASCII char in the first line. */
6862 this_utf8 = MAYBE;
6863 for (s = IObuff; *s != NUL; ++s)
6864 if (*s >= 0x80)
6865 {
6866 int l;
6867
6868 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006869 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (l == 1)
6871 {
6872 /* Illegal UTF-8 byte sequence. */
6873 this_utf8 = FALSE;
6874 break;
6875 }
6876 s += l - 1;
6877 }
6878 if (this_utf8 == MAYBE) /* only ASCII characters found */
6879 this_utf8 = FALSE;
6880 if (utf8 == MAYBE) /* first file */
6881 utf8 = this_utf8;
6882 else if (utf8 != this_utf8)
6883 {
6884 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6885 mix = !got_int;
6886 got_int = TRUE;
6887 }
6888 firstline = FALSE;
6889 }
6890# endif
6891 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6892 while (p1 != NULL)
6893 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02006894 /* Use vim_strbyte() instead of vim_strchr() so that when
6895 * 'encoding' is dbcs it still works, don't find '*' in the
6896 * second byte. */
6897 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6899 {
6900 for (s = p1 + 1; s < p2; ++s)
6901 if (*s == ' ' || *s == '\t' || *s == '|')
6902 break;
6903
6904 /*
6905 * Only accept a *tag* when it consists of valid
6906 * characters, there is white space before it and is
6907 * followed by a white character or end-of-line.
6908 */
6909 if (s == p2
6910 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6911 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6912 || s[1] == '\0'))
6913 {
6914 *p2 = '\0';
6915 ++p1;
6916 if (ga_grow(&ga, 1) == FAIL)
6917 {
6918 got_int = TRUE;
6919 break;
6920 }
6921 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6922 if (s == NULL)
6923 {
6924 got_int = TRUE;
6925 break;
6926 }
6927 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6928 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 sprintf((char *)s, "%s\t%s", p1, fname);
6930
6931 /* find next '*' */
6932 p2 = vim_strchr(p2 + 1, '*');
6933 }
6934 }
6935 p1 = p2;
6936 }
6937 line_breakcheck();
6938 }
6939
6940 fclose(fd);
6941 }
6942
6943 FreeWild(filecount, files);
6944
6945 if (!got_int)
6946 {
6947 /*
6948 * Sort the tags.
6949 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02006950 if (ga.ga_data != NULL)
6951 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953 /*
6954 * Check for duplicates.
6955 */
6956 for (i = 1; i < ga.ga_len; ++i)
6957 {
6958 p1 = ((char_u **)ga.ga_data)[i - 1];
6959 p2 = ((char_u **)ga.ga_data)[i];
6960 while (*p1 == *p2)
6961 {
6962 if (*p2 == '\t')
6963 {
6964 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006965 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6967 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6968 EMSG(NameBuff);
6969 *p2 = '\t';
6970 break;
6971 }
6972 ++p1;
6973 ++p2;
6974 }
6975 }
6976
6977# ifdef FEAT_MBYTE
6978 if (utf8 == TRUE)
6979 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6980# endif
6981
6982 /*
6983 * Write the tags into the file.
6984 */
6985 for (i = 0; i < ga.ga_len; ++i)
6986 {
6987 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00006988 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00006990 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 else
6992 {
6993 fprintf(fd_tags, "%s\t/*", s);
6994 for (p1 = s; *p1 != '\t'; ++p1)
6995 {
6996 /* insert backslash before '\\' and '/' */
6997 if (*p1 == '\\' || *p1 == '/')
6998 putc('\\', fd_tags);
6999 putc(*p1, fd_tags);
7000 }
7001 fprintf(fd_tags, "*\n");
7002 }
7003 }
7004 }
7005#ifdef FEAT_MBYTE
7006 if (mix)
7007 got_int = FALSE; /* continue with other languages */
7008#endif
7009
7010 for (i = 0; i < ga.ga_len; ++i)
7011 vim_free(((char_u **)ga.ga_data)[i]);
7012 ga_clear(&ga);
7013 fclose(fd_tags); /* there is no check for an error... */
7014}
7015#endif
7016
7017#if defined(FEAT_SIGNS) || defined(PROTO)
7018
7019/*
7020 * Struct to hold the sign properties.
7021 */
7022typedef struct sign sign_T;
7023
7024struct sign
7025{
7026 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007027 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 char_u *sn_name; /* name of sign */
7029 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007030# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007032# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 char_u *sn_text; /* text used instead of pixmap */
7034 int sn_line_hl; /* highlight ID for line */
7035 int sn_text_hl; /* highlight ID for text */
7036};
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007039static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007041static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
7042static void sign_list_defined(sign_T *sp);
7043static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007045static char *cmds[] = {
7046 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007047# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007048 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007049# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007050 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007051# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007052 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007053# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007054 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007055# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007056 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007057# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007058 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007059# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007060};
7061
7062/*
7063 * Find index of a ":sign" subcmd from its name.
7064 * "*end_cmd" must be writable.
7065 */
7066 static int
7067sign_cmd_idx(begin_cmd, end_cmd)
Bram Moolenaar985cb442009-05-14 19:51:46 +00007068 char_u *begin_cmd; /* begin of sign subcmd */
7069 char_u *end_cmd; /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007070{
7071 int idx;
7072 char save = *end_cmd;
7073
7074 *end_cmd = NUL;
7075 for (idx = 0; ; ++idx)
7076 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7077 break;
7078 *end_cmd = save;
7079 return idx;
7080}
7081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082/*
7083 * ":sign" command
7084 */
7085 void
7086ex_sign(eap)
7087 exarg_T *eap;
7088{
7089 char_u *arg = eap->arg;
7090 char_u *p;
7091 int idx;
7092 sign_T *sp;
7093 sign_T *sp_prev;
7094 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
7096 /* Parse the subcommand. */
7097 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007098 idx = sign_cmd_idx(arg, p);
7099 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007101 EMSG2(_("E160: Unknown sign command: %s"), arg);
7102 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 }
7104 arg = skipwhite(p);
7105
7106 if (idx <= SIGNCMD_LIST)
7107 {
7108 /*
7109 * Define, undefine or list signs.
7110 */
7111 if (idx == SIGNCMD_LIST && *arg == NUL)
7112 {
7113 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007114 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 sign_list_defined(sp);
7116 }
7117 else if (*arg == NUL)
7118 EMSG(_("E156: Missing sign name"));
7119 else
7120 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007121 /* Isolate the sign name. If it's a number skip leading zeroes,
7122 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 p = skiptowhite(arg);
7124 if (*p != NUL)
7125 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007126 while (arg[0] == '0' && arg[1] != NUL)
7127 ++arg;
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 sp_prev = NULL;
7130 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7131 {
7132 if (STRCMP(sp->sn_name, arg) == 0)
7133 break;
7134 sp_prev = sp;
7135 }
7136 if (idx == SIGNCMD_DEFINE)
7137 {
7138 /* ":sign define {name} ...": define a sign */
7139 if (sp == NULL)
7140 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007141 sign_T *lp;
7142 int start = next_sign_typenr;
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 /* Allocate a new sign. */
7145 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7146 if (sp == NULL)
7147 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007149 /* Check that next_sign_typenr is not already being used.
7150 * This only happens after wrapping around. Hopefully
7151 * another one got deleted and we can use its number. */
7152 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007154 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007156 ++next_sign_typenr;
7157 if (next_sign_typenr == MAX_TYPENR)
7158 next_sign_typenr = 1;
7159 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007161 vim_free(sp);
7162 EMSG(_("E612: Too many signs defined"));
7163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007165 lp = first_sign; /* start all over */
7166 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007168 lp = lp->sn_next;
7169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007171 sp->sn_typenr = next_sign_typenr;
7172 if (++next_sign_typenr == MAX_TYPENR)
7173 next_sign_typenr = 1; /* wrap around */
7174
7175 sp->sn_name = vim_strsave(arg);
7176 if (sp->sn_name == NULL) /* out of memory */
7177 {
7178 vim_free(sp);
7179 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007181
7182 /* add the new sign to the list of signs */
7183 if (sp_prev == NULL)
7184 first_sign = sp;
7185 else
7186 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188
7189 /* set values for a defined sign. */
7190 for (;;)
7191 {
7192 arg = skipwhite(p);
7193 if (*arg == NUL)
7194 break;
7195 p = skiptowhite_esc(arg);
7196 if (STRNCMP(arg, "icon=", 5) == 0)
7197 {
7198 arg += 5;
7199 vim_free(sp->sn_icon);
7200 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7201 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007202# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 if (gui.in_use)
7204 {
7205 out_flush();
7206 if (sp->sn_image != NULL)
7207 gui_mch_destroy_sign(sp->sn_image);
7208 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7209 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212 else if (STRNCMP(arg, "text=", 5) == 0)
7213 {
7214 char_u *s;
7215 int cells;
7216 int len;
7217
7218 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007219# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 /* Count cells and check for non-printable chars */
7221 if (has_mbyte)
7222 {
7223 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007224 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
7226 if (!vim_isprintc((*mb_ptr2char)(s)))
7227 break;
7228 cells += (*mb_ptr2cells)(s);
7229 }
7230 }
7231 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
7234 for (s = arg; s < p; ++s)
7235 if (!vim_isprintc(*s))
7236 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007237 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239 /* Currently must be one or two display cells */
7240 if (s != p || cells < 1 || cells > 2)
7241 {
7242 *p = NUL;
7243 EMSG2(_("E239: Invalid sign text: %s"), arg);
7244 return;
7245 }
7246
7247 vim_free(sp->sn_text);
7248 /* Allocate one byte more if we need to pad up
7249 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 sp->sn_text = vim_strnsave(arg, len);
7252
7253 if (sp->sn_text != NULL && cells == 1)
7254 STRCPY(sp->sn_text + len - 1, " ");
7255 }
7256 else if (STRNCMP(arg, "linehl=", 7) == 0)
7257 {
7258 arg += 7;
7259 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7260 }
7261 else if (STRNCMP(arg, "texthl=", 7) == 0)
7262 {
7263 arg += 7;
7264 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7265 }
7266 else
7267 {
7268 EMSG2(_(e_invarg2), arg);
7269 return;
7270 }
7271 }
7272 }
7273 else if (sp == NULL)
7274 EMSG2(_("E155: Unknown sign: %s"), arg);
7275 else if (idx == SIGNCMD_LIST)
7276 /* ":sign list {name}" */
7277 sign_list_defined(sp);
7278 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007280 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282 }
7283 else
7284 {
7285 int id = -1;
7286 linenr_T lnum = -1;
7287 char_u *sign_name = NULL;
7288 char_u *arg1;
7289
7290 if (*arg == NUL)
7291 {
7292 if (idx == SIGNCMD_PLACE)
7293 {
7294 /* ":sign place": list placed signs in all buffers */
7295 sign_list_placed(NULL);
7296 }
7297 else if (idx == SIGNCMD_UNPLACE)
7298 {
7299 /* ":sign unplace": remove placed sign at cursor */
7300 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7301 if (id > 0)
7302 {
7303 buf_delsign(curwin->w_buffer, id);
7304 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7305 }
7306 else
7307 EMSG(_("E159: Missing sign number"));
7308 }
7309 else
7310 EMSG(_(e_argreq));
7311 return;
7312 }
7313
7314 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7315 {
7316 /* ":sign unplace *": remove all placed signs */
7317 buf_delete_all_signs();
7318 return;
7319 }
7320
7321 /* first arg could be placed sign id */
7322 arg1 = arg;
7323 if (VIM_ISDIGIT(*arg))
7324 {
7325 id = getdigits(&arg);
7326 if (!vim_iswhite(*arg) && *arg != NUL)
7327 {
7328 id = -1;
7329 arg = arg1;
7330 }
7331 else
7332 {
7333 arg = skipwhite(arg);
7334 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7335 {
7336 /* ":sign unplace {id}": remove placed sign by number */
7337 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7338 if ((lnum = buf_delsign(buf, id)) != 0)
7339 update_debug_sign(buf, lnum);
7340 return;
7341 }
7342 }
7343 }
7344
7345 /*
7346 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7347 * Leave "arg" pointing to {fname}.
7348 */
7349 for (;;)
7350 {
7351 if (STRNCMP(arg, "line=", 5) == 0)
7352 {
7353 arg += 5;
7354 lnum = atoi((char *)arg);
7355 arg = skiptowhite(arg);
7356 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007357 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7358 {
7359 if (id != -1)
7360 {
7361 EMSG(_(e_invarg));
7362 return;
7363 }
7364 id = -2;
7365 arg = skiptowhite(arg + 1);
7366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 else if (STRNCMP(arg, "name=", 5) == 0)
7368 {
7369 arg += 5;
7370 sign_name = arg;
7371 arg = skiptowhite(arg);
7372 if (*arg != NUL)
7373 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007374 while (sign_name[0] == '0' && sign_name[1] != NUL)
7375 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 }
7377 else if (STRNCMP(arg, "file=", 5) == 0)
7378 {
7379 arg += 5;
7380 buf = buflist_findname(arg);
7381 break;
7382 }
7383 else if (STRNCMP(arg, "buffer=", 7) == 0)
7384 {
7385 arg += 7;
7386 buf = buflist_findnr((int)getdigits(&arg));
7387 if (*skipwhite(arg) != NUL)
7388 EMSG(_(e_trailing));
7389 break;
7390 }
7391 else
7392 {
7393 EMSG(_(e_invarg));
7394 return;
7395 }
7396 arg = skipwhite(arg);
7397 }
7398
7399 if (buf == NULL)
7400 {
7401 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7402 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007403 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 {
7405 if (lnum >= 0 || sign_name != NULL)
7406 EMSG(_(e_invarg));
7407 else
7408 /* ":sign place file={fname}": list placed signs in one file */
7409 sign_list_placed(buf);
7410 }
7411 else if (idx == SIGNCMD_JUMP)
7412 {
7413 /* ":sign jump {id} file={fname}" */
7414 if (lnum >= 0 || sign_name != NULL)
7415 EMSG(_(e_invarg));
7416 else if ((lnum = buf_findsign(buf, id)) > 0)
7417 { /* goto a sign ... */
7418 if (buf_jump_open_win(buf) != NULL)
7419 { /* ... in a current window */
7420 curwin->w_cursor.lnum = lnum;
7421 check_cursor_lnum();
7422 beginline(BL_WHITE);
7423 }
7424 else
7425 { /* ... not currently in a window */
7426 char_u *cmd;
7427
7428 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7429 if (cmd == NULL)
7430 return;
7431 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7432 do_cmdline_cmd(cmd);
7433 vim_free(cmd);
7434 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007435# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 }
7439 else
7440 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7441 }
7442 else if (idx == SIGNCMD_UNPLACE)
7443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (lnum >= 0 || sign_name != NULL)
7445 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007446 else if (id == -2)
7447 {
7448 /* ":sign unplace * file={fname}" */
7449 redraw_buf_later(buf, NOT_VALID);
7450 buf_delete_signs(buf);
7451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 else
7453 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007454 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 lnum = buf_delsign(buf, id);
7456 update_debug_sign(buf, lnum);
7457 }
7458 }
7459 /* idx == SIGNCMD_PLACE */
7460 else if (sign_name != NULL)
7461 {
7462 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7463 if (STRCMP(sp->sn_name, sign_name) == 0)
7464 break;
7465 if (sp == NULL)
7466 {
7467 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7468 return;
7469 }
7470 if (lnum > 0)
7471 /* ":sign place {id} line={lnum} name={name} file={fname}":
7472 * place a sign */
7473 buf_addsign(buf, id, lnum, sp->sn_typenr);
7474 else
7475 /* ":sign place {id} file={fname}": change sign type */
7476 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007477 if (lnum > 0)
7478 update_debug_sign(buf, lnum);
7479 else
7480 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482 else
7483 EMSG(_(e_invarg));
7484 }
7485}
7486
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007487# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488/*
7489 * Allocate the icons. Called when the GUI has started. Allows defining
7490 * signs before it starts.
7491 */
7492 void
7493sign_gui_started()
7494{
7495 sign_T *sp;
7496
7497 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7498 if (sp->sn_icon != NULL)
7499 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7500}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
7503/*
7504 * List one sign.
7505 */
7506 static void
7507sign_list_defined(sp)
7508 sign_T *sp;
7509{
7510 char_u *p;
7511
Bram Moolenaar555b2802005-05-19 21:08:39 +00007512 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 if (sp->sn_icon != NULL)
7514 {
7515 MSG_PUTS(" icon=");
7516 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007517# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (sp->sn_image == NULL)
7519 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007520# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524 if (sp->sn_text != NULL)
7525 {
7526 MSG_PUTS(" text=");
7527 msg_outtrans(sp->sn_text);
7528 }
7529 if (sp->sn_line_hl > 0)
7530 {
7531 MSG_PUTS(" linehl=");
7532 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
7533 if (p == NULL)
7534 MSG_PUTS("NONE");
7535 else
7536 msg_puts(p);
7537 }
7538 if (sp->sn_text_hl > 0)
7539 {
7540 MSG_PUTS(" texthl=");
7541 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
7542 if (p == NULL)
7543 MSG_PUTS("NONE");
7544 else
7545 msg_puts(p);
7546 }
7547}
7548
7549/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007550 * Undefine a sign and free its memory.
7551 */
7552 static void
7553sign_undefine(sp, sp_prev)
7554 sign_T *sp;
7555 sign_T *sp_prev;
7556{
7557 vim_free(sp->sn_name);
7558 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007559# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007560 if (sp->sn_image != NULL)
7561 {
7562 out_flush();
7563 gui_mch_destroy_sign(sp->sn_image);
7564 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007565# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007566 vim_free(sp->sn_text);
7567 if (sp_prev == NULL)
7568 first_sign = sp->sn_next;
7569 else
7570 sp_prev->sn_next = sp->sn_next;
7571 vim_free(sp);
7572}
7573
7574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 * Get highlighting attribute for sign "typenr".
7576 * If "line" is TRUE: line highl, if FALSE: text highl.
7577 */
7578 int
7579sign_get_attr(typenr, line)
7580 int typenr;
7581 int line;
7582{
7583 sign_T *sp;
7584
7585 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7586 if (sp->sn_typenr == typenr)
7587 {
7588 if (line)
7589 {
7590 if (sp->sn_line_hl > 0)
7591 return syn_id2attr(sp->sn_line_hl);
7592 }
7593 else
7594 {
7595 if (sp->sn_text_hl > 0)
7596 return syn_id2attr(sp->sn_text_hl);
7597 }
7598 break;
7599 }
7600 return 0;
7601}
7602
7603/*
7604 * Get text mark for sign "typenr".
7605 * Returns NULL if there isn't one.
7606 */
7607 char_u *
7608sign_get_text(typenr)
7609 int typenr;
7610{
7611 sign_T *sp;
7612
7613 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7614 if (sp->sn_typenr == typenr)
7615 return sp->sn_text;
7616 return NULL;
7617}
7618
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007619# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 void *
7621sign_get_image(typenr)
7622 int typenr; /* the attribute which may have a sign */
7623{
7624 sign_T *sp;
7625
7626 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7627 if (sp->sn_typenr == typenr)
7628 return sp->sn_image;
7629 return NULL;
7630}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633/*
7634 * Get the name of a sign by its typenr.
7635 */
7636 char_u *
7637sign_typenr2name(typenr)
7638 int typenr;
7639{
7640 sign_T *sp;
7641
7642 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7643 if (sp->sn_typenr == typenr)
7644 return sp->sn_name;
7645 return (char_u *)_("[Deleted]");
7646}
7647
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007648# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007649/*
7650 * Undefine/free all signs.
7651 */
7652 void
7653free_signs()
7654{
7655 while (first_sign != NULL)
7656 sign_undefine(first_sign, NULL);
7657}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007658# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007659
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007660# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007661static enum
7662{
7663 EXP_SUBCMD, /* expand :sign sub-commands */
7664 EXP_DEFINE, /* expand :sign define {name} args */
7665 EXP_PLACE, /* expand :sign place {id} args */
7666 EXP_UNPLACE, /* expand :sign unplace" */
7667 EXP_SIGN_NAMES /* expand with name of placed signs */
7668} expand_what;
7669
7670/*
7671 * Function given to ExpandGeneric() to obtain the sign command
7672 * expansion.
7673 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007674 char_u *
7675get_sign_name(xp, idx)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00007676 expand_T *xp UNUSED;
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007677 int idx;
7678{
7679 sign_T *sp;
7680 int current_idx;
7681
7682 switch (expand_what)
7683 {
7684 case EXP_SUBCMD:
7685 return (char_u *)cmds[idx];
7686 case EXP_DEFINE:
7687 {
7688 char *define_arg[] =
7689 {
7690 "icon=", "linehl=", "text=", "texthl=", NULL
7691 };
7692 return (char_u *)define_arg[idx];
7693 }
7694 case EXP_PLACE:
7695 {
7696 char *place_arg[] =
7697 {
7698 "line=", "name=", "file=", "buffer=", NULL
7699 };
7700 return (char_u *)place_arg[idx];
7701 }
7702 case EXP_UNPLACE:
7703 {
7704 char *unplace_arg[] = { "file=", "buffer=", NULL };
7705 return (char_u *)unplace_arg[idx];
7706 }
7707 case EXP_SIGN_NAMES:
7708 /* Complete with name of signs already defined */
7709 current_idx = 0;
7710 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7711 if (current_idx++ == idx)
7712 return sp->sn_name;
7713 return NULL;
7714 default:
7715 return NULL;
7716 }
7717}
7718
7719/*
7720 * Handle command line completion for :sign command.
7721 */
7722 void
7723set_context_in_sign_cmd(xp, arg)
7724 expand_T *xp;
7725 char_u *arg;
7726{
7727 char_u *p;
7728 char_u *end_subcmd;
7729 char_u *last;
7730 int cmd_idx;
7731 char_u *begin_subcmd_args;
7732
7733 /* Default: expand subcommands. */
7734 xp->xp_context = EXPAND_SIGN;
7735 expand_what = EXP_SUBCMD;
7736 xp->xp_pattern = arg;
7737
7738 end_subcmd = skiptowhite(arg);
7739 if (*end_subcmd == NUL)
7740 /* expand subcmd name
7741 * :sign {subcmd}<CTRL-D>*/
7742 return;
7743
7744 cmd_idx = sign_cmd_idx(arg, end_subcmd);
7745
7746 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007747 * |
7748 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007749 begin_subcmd_args = skipwhite(end_subcmd);
7750 p = skiptowhite(begin_subcmd_args);
7751 if (*p == NUL)
7752 {
7753 /*
7754 * Expand first argument of subcmd when possible.
7755 * For ":jump {id}" and ":unplace {id}", we could
7756 * possibly expand the ids of all signs already placed.
7757 */
7758 xp->xp_pattern = begin_subcmd_args;
7759 switch (cmd_idx)
7760 {
7761 case SIGNCMD_LIST:
7762 case SIGNCMD_UNDEFINE:
7763 /* :sign list <CTRL-D>
7764 * :sign undefine <CTRL-D> */
7765 expand_what = EXP_SIGN_NAMES;
7766 break;
7767 default:
7768 xp->xp_context = EXPAND_NOTHING;
7769 }
7770 return;
7771 }
7772
7773 /* expand last argument of subcmd */
7774
7775 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007776 * |
7777 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007778
7779 /* Loop until reaching last argument. */
7780 do
7781 {
7782 p = skipwhite(p);
7783 last = p;
7784 p = skiptowhite(p);
7785 } while (*p != NUL);
7786
7787 p = vim_strchr(last, '=');
7788
7789 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007790 * | |
7791 * last p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007792 if (p == NUL)
7793 {
7794 /* Expand last argument name (before equal sign). */
7795 xp->xp_pattern = last;
7796 switch (cmd_idx)
7797 {
7798 case SIGNCMD_DEFINE:
7799 expand_what = EXP_DEFINE;
7800 break;
7801 case SIGNCMD_PLACE:
7802 expand_what = EXP_PLACE;
7803 break;
7804 case SIGNCMD_JUMP:
7805 case SIGNCMD_UNPLACE:
7806 expand_what = EXP_UNPLACE;
7807 break;
7808 default:
7809 xp->xp_context = EXPAND_NOTHING;
7810 }
7811 }
7812 else
7813 {
7814 /* Expand last argument value (after equal sign). */
7815 xp->xp_pattern = p + 1;
7816 switch (cmd_idx)
7817 {
7818 case SIGNCMD_DEFINE:
7819 if (STRNCMP(last, "texthl", p - last) == 0 ||
7820 STRNCMP(last, "linehl", p - last) == 0)
7821 xp->xp_context = EXPAND_HIGHLIGHT;
7822 else if (STRNCMP(last, "icon", p - last) == 0)
7823 xp->xp_context = EXPAND_FILES;
7824 else
7825 xp->xp_context = EXPAND_NOTHING;
7826 break;
7827 case SIGNCMD_PLACE:
7828 if (STRNCMP(last, "name", p - last) == 0)
7829 expand_what = EXP_SIGN_NAMES;
7830 else
7831 xp->xp_context = EXPAND_NOTHING;
7832 break;
7833 default:
7834 xp->xp_context = EXPAND_NOTHING;
7835 }
7836 }
7837}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007838# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007839#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007840
7841/*
7842 * Make the user happy.
7843 */
7844 void
7845ex_smile(eap)
7846 exarg_T *eap UNUSED;
7847{
7848 static char *code = "\34 \4o\14$\4ox\30 \2o\30$\1ox\25 \2o\36$\1o\11 \1o\1$\3 \2$\1 \1o\1$x\5 \1o\1 \1$\1 \2o\10 \1o\44$\1o\7 \2$\1 \2$\1 \2$\1o\1$x\2 \2o\1 \1$\1 \1$\1 \1\"\1$\6 \1o\11$\4 \15$\4 \11$\1o\7 \3$\1o\2$\1o\1$x\2 \1\"\6$\1o\1$\5 \1o\11$\6 \13$\6 \12$\1o\4 \10$x\4 \7$\4 \13$\6 \13$\6 \27$x\4 \27$\4 \15$\4 \16$\2 \3\"\3$x\5 \1\"\3$\4\"\61$\5 \1\"\3$x\6 \3$\3 \1o\62$\5 \1\"\3$\1ox\5 \1o\2$\1\"\3 \63$\7 \3$\1ox\5 \3$\4 \55$\1\"\1 \1\"\6$\5o\4$\1ox\4 \1o\3$\4o\5$\2 \45$\3 \1o\21$x\4 \10$\1\"\4$\3 \42$\5 \4$\10\"x\3 \4\"\7 \4$\4 \1\"\34$\1\"\6 \1o\3$x\16 \1\"\3$\1o\5 \3\"\22$\1\"\2$\1\"\11 \3$x\20 \3$\1o\12 \1\"\2$\2\"\6$\4\"\13 \1o\3$x\21 \4$\1o\40 \1o\3$\1\"x\22 \1\"\4$\1o\6 \1o\6$\1o\1\"\4$\1o\10 \1o\4$x\24 \1\"\5$\2o\5 \2\"\4$\1o\5$\1o\3 \1o\4$\2\"x\27 \2\"\5$\4o\2 \1\"\3$\1o\11$\3\"x\32 \2\"\7$\2o\1 \12$x\42 \4\"\13$x\46 \14$x\47 \12$\1\"x\50 \1\"\3$\4\"x";
7849 char *p;
7850 int n;
7851
7852 msg_start();
7853 msg_putchar('\n');
7854 for (p = code; *p != NUL; ++p)
7855 if (*p == 'x')
7856 msg_putchar('\n');
7857 else
7858 for (n = *p++; n > 0; --n)
Bram Moolenaar2d820802015-12-31 20:46:39 +01007859 if (*p == 'o' || *p == '$')
7860 msg_putchar_attr(*p, hl_attr(HLF_L));
7861 else
7862 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007863 msg_clr_eos();
7864}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
7866#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
7867/*
7868 * ":drop"
7869 * Opens the first argument in a window. When there are two or more arguments
7870 * the argument list is redefined.
7871 */
7872 void
7873ex_drop(eap)
7874 exarg_T *eap;
7875{
7876 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 win_T *wp;
7878 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007879# ifdef FEAT_WINDOWS
7880 tabpage_T *tp;
7881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882
7883 /*
7884 * Check if the first argument is already being edited in a window. If
7885 * so, jump to that window.
7886 * We would actually need to check all arguments, but that's complicated
7887 * and mostly only one file is dropped.
7888 * This also ignores wildcards, since it is very unlikely the user is
7889 * editing a file name with a wildcard character.
7890 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007891 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00007893 /*
7894 * Expanding wildcards may result in an empty argument list. E.g. when
7895 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
7896 * already did an error message for this.
7897 */
7898 if (ARGCOUNT == 0)
7899 return;
7900
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007901# ifdef FEAT_WINDOWS
7902 if (cmdmod.tab)
7903 {
7904 /* ":tab drop file ...": open a tab for each argument that isn't
7905 * edited in a window yet. It's like ":tab all" but without closing
7906 * windows or tabs. */
7907 ex_all(eap);
7908 }
7909 else
7910# endif
7911 {
7912 /* ":drop file ...": Edit the first argument. Jump to an existing
7913 * window if possible, edit in current window if the current buffer
7914 * can be abandoned, otherwise open a new window. */
7915 buf = buflist_findnr(ARGLIST[0].ae_fnum);
7916
7917 FOR_ALL_TAB_WINDOWS(tp, wp)
7918 {
7919 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007921# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007922 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 return;
7926 }
7927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007929 /*
7930 * Check whether the current buffer is changed. If so, we will need
7931 * to split the current window or data could be lost.
7932 * Skip the check if the 'hidden' option is set, as in this case the
7933 * buffer won't be lost.
7934 */
7935 if (!P_HID(curbuf))
7936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007938 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939# endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007940 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007942 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007944 if (split)
7945 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007949 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7950 if (split)
7951 {
7952 eap->cmdidx = CMD_sfirst;
7953 eap->cmd[0] = 's';
7954 }
7955 else
7956 eap->cmdidx = CMD_first;
7957 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960#endif