blob: 05575ef70cdfebd73ed8f0d373a177eb80e71093 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarf28dbce2016-01-29 22:03:47 +010021static int linelen(int *has_tab);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010022static 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 +000023#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010024static char_u *viminfo_filename(char_u *);
25static void do_viminfo(FILE *fp_in, FILE *fp_out, int flags);
26static int viminfo_encoding(vir_T *virp);
27static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#endif
29
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static int check_readonly(int *forceit, buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void delbuf_msg(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static int
35#ifdef __BORLANDC__
36 _RTLENTRYF
37#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038 help_compare(const void *s1, const void *s2);
39static void prepare_help_buffer(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
42 * ":ascii" and "ga".
43 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010045do_ascii(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046{
47 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000048 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 char buf1[20];
50 char buf2[20];
51 char_u buf3[7];
52#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000053 int cc[MAX_MCO];
54 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 int len;
56
57 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000058 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 else
60#endif
61 c = gchar_cursor();
62 if (c == NUL)
63 {
64 MSG("NUL");
65 return;
66 }
67
68#ifdef FEAT_MBYTE
69 IObuff[0] = NUL;
70 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
71#endif
72 {
73 if (c == NL) /* NUL is stored as NL */
74 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000075 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
76 cval = NL; /* NL is stored as CR */
77 else
78 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 if (vim_isprintc_strict(c) && (c < ' '
80#ifndef EBCDIC
81 || c > '~'
82#endif
83 ))
84 {
85 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000086 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 }
88 else
89 buf1[0] = NUL;
90#ifndef EBCDIC
91 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000092 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
93 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 else
95#endif
96 buf2[0] = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +000097 vim_snprintf((char *)IObuff, IOSIZE,
98 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000099 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000101 if (enc_utf8)
102 c = cc[ci++];
103 else
104 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106 }
107
108#ifdef FEAT_MBYTE
109 /* Repeat for combining characters. */
110 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
111 {
112 len = (int)STRLEN(IObuff);
113 /* This assumes every multi-byte char is printable... */
114 if (len > 0)
115 IObuff[len++] = ' ';
116 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000117 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000118# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 )
122 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000123 len += (*mb_char2bytes)(c, IObuff + len);
124 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
126 : _("> %d, Hex %08x, Octal %o"), c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000127 if (ci == MAX_MCO)
128 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000129 if (enc_utf8)
130 c = cc[ci++];
131 else
132 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 }
134#endif
135
136 msg(IObuff);
137}
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * ":left", ":center" and ":right": align text.
141 */
142 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100143ex_align(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 pos_T save_curpos;
146 int len;
147 int indent = 0;
148 int new_indent;
149 int has_tab;
150 int width;
151
152#ifdef FEAT_RIGHTLEFT
153 if (curwin->w_p_rl)
154 {
155 /* switch left and right aligning */
156 if (eap->cmdidx == CMD_right)
157 eap->cmdidx = CMD_left;
158 else if (eap->cmdidx == CMD_left)
159 eap->cmdidx = CMD_right;
160 }
161#endif
162
163 width = atoi((char *)eap->arg);
164 save_curpos = curwin->w_cursor;
165 if (eap->cmdidx == CMD_left) /* width is used for new indent */
166 {
167 if (width >= 0)
168 indent = width;
169 }
170 else
171 {
172 /*
173 * if 'textwidth' set, use it
174 * else if 'wrapmargin' set, use it
175 * if invalid value, use 80
176 */
177 if (width <= 0)
178 width = curbuf->b_p_tw;
179 if (width == 0 && curbuf->b_p_wm > 0)
180 width = W_WIDTH(curwin) - curbuf->b_p_wm;
181 if (width <= 0)
182 width = 80;
183 }
184
185 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
186 return;
187
188 for (curwin->w_cursor.lnum = eap->line1;
189 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
190 {
191 if (eap->cmdidx == CMD_left) /* left align */
192 new_indent = indent;
193 else
194 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000195 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 len = linelen(eap->cmdidx == CMD_right ? &has_tab
197 : NULL) - get_indent();
198
199 if (len <= 0) /* skip blank lines */
200 continue;
201
202 if (eap->cmdidx == CMD_center)
203 new_indent = (width - len) / 2;
204 else
205 {
206 new_indent = width - len; /* right align */
207
208 /*
209 * Make sure that embedded TABs don't make the text go too far
210 * to the right.
211 */
212 if (has_tab)
213 while (new_indent > 0)
214 {
215 (void)set_indent(new_indent, 0);
216 if (linelen(NULL) <= width)
217 {
218 /*
219 * Now try to move the line as much as possible to
220 * the right. Stop when it moves too far.
221 */
222 do
223 (void)set_indent(++new_indent, 0);
224 while (linelen(NULL) <= width);
225 --new_indent;
226 break;
227 }
228 --new_indent;
229 }
230 }
231 }
232 if (new_indent < 0)
233 new_indent = 0;
234 (void)set_indent(new_indent, 0); /* set indent */
235 }
236 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
237 curwin->w_cursor = save_curpos;
238 beginline(BL_WHITE | BL_FIX);
239}
240
241/*
242 * Get the length of the current line, excluding trailing white space.
243 */
244 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100245linelen(int *has_tab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 char_u *line;
248 char_u *first;
249 char_u *last;
250 int save;
251 int len;
252
253 /* find the first non-blank character */
254 line = ml_get_curline();
255 first = skipwhite(line);
256
257 /* find the character after the last non-blank character */
258 for (last = first + STRLEN(first);
259 last > first && vim_iswhite(last[-1]); --last)
260 ;
261 save = *last;
262 *last = NUL;
263 len = linetabsize(line); /* get line length */
264 if (has_tab != NULL) /* check for embedded TAB */
265 *has_tab = (vim_strrchr(first, TAB) != NULL);
266 *last = save;
267
268 return len;
269}
270
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000271/* Buffer for two lines used during sorting. They are allocated to
272 * contain the longest line being sorted. */
273static char_u *sortbuf1;
274static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000275
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100276static int sort_ic; /* ignore case */
277static int sort_nr; /* sort on number */
278static int sort_rx; /* sort on regex instead of skipping it */
279#ifdef FEAT_FLOAT
280static int sort_flt; /* sort on floating number */
281#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000282
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100283static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000284
285/* Struct to store info to be sorted. */
286typedef struct
287{
288 linenr_T lnum; /* line number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100289 union {
290 struct
291 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200292 varnumber_T start_col_nr; /* starting column number */
293 varnumber_T end_col_nr; /* ending column number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100294 } line;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200295 varnumber_T value; /* value if sorting by integer */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100296#ifdef FEAT_FLOAT
297 float_T value_flt; /* value if sorting by float */
298#endif
299 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000300} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000301
302static int
303#ifdef __BORLANDC__
304_RTLENTRYF
305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306sort_compare(const void *s1, const void *s2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000307
308 static int
309#ifdef __BORLANDC__
310_RTLENTRYF
311#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100312sort_compare(const void *s1, const void *s2)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000313{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000314 sorti_T l1 = *(sorti_T *)s1;
315 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000316 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000317
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000318 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000319 * if we return 0 every time, qsort will assume it's done sorting and
320 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000321 if (sort_abort)
322 return 0;
323 fast_breakcheck();
324 if (got_int)
325 sort_abort = TRUE;
326
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000327 /* When sorting numbers "start_col_nr" is the number, not the column
328 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000329 if (sort_nr)
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100330 result = l1.st_u.value == l2.st_u.value ? 0
331 : l1.st_u.value > l2.st_u.value ? 1 : -1;
332#ifdef FEAT_FLOAT
333 else if (sort_flt)
334 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
335 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
336#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337 else
338 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000339 /* We need to copy one line into "sortbuf1", because there is no
340 * guarantee that the first pointer becomes invalid when obtaining the
341 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100342 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
343 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
344 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
345 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
346 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
347 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000348
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000349 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
350 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000351 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000352
353 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000354 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000355 return (int)(l1.lnum - l2.lnum);
356 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000357}
358
359/*
360 * ":sort".
361 */
362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100363ex_sort(exarg_T *eap)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000364{
365 regmatch_T regmatch;
366 int len;
367 linenr_T lnum;
368 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000369 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000370 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000371 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000372 char_u *p;
373 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000374 char_u *s2;
375 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000376 int unique = FALSE;
377 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000378 colnr_T start_col;
379 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100380 int sort_what = 0;
381 int format_found = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000382
Bram Moolenaar48c99162008-02-18 18:42:52 +0000383 /* Sorting one line is really quick! */
384 if (count <= 1)
385 return;
386
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000387 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
388 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000389 sortbuf1 = NULL;
390 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000391 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000392 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000393 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000394 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000395
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100396 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100397#ifdef FEAT_FLOAT
398 sort_flt = 0;
399#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000400
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000401 for (p = eap->arg; *p != NUL; ++p)
402 {
403 if (vim_iswhite(*p))
404 ;
405 else if (*p == 'i')
406 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000407 else if (*p == 'r')
408 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000409 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100410 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100411 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100412 ++format_found;
413 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100414#ifdef FEAT_FLOAT
415 else if (*p == 'f')
416 {
417 sort_flt = 1;
418 ++format_found;
419 }
420#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100421 else if (*p == 'b')
422 {
423 sort_what = STR2NR_BIN + STR2NR_FORCE;
424 ++format_found;
425 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000426 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100427 {
428 sort_what = STR2NR_OCT + STR2NR_FORCE;
429 ++format_found;
430 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000431 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100432 {
433 sort_what = STR2NR_HEX + STR2NR_FORCE;
434 ++format_found;
435 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000436 else if (*p == 'u')
437 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000438 else if (*p == '"') /* comment start */
439 break;
440 else if (check_nextcmd(p) != NULL)
441 {
442 eap->nextcmd = check_nextcmd(p);
443 break;
444 }
445 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000446 {
447 s = skip_regexp(p + 1, *p, TRUE, NULL);
448 if (*s != *p)
449 {
450 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000451 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000452 }
453 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000454 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100455 if (s == p + 1)
456 {
457 if (last_search_pat() == NULL)
458 {
459 EMSG(_(e_noprevre));
460 goto sortend;
461 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000462 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100463 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000464 else
465 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000466 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000467 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000468 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000469 regmatch.rm_ic = p_ic;
470 }
471 else
472 {
473 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000474 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000475 }
476 }
477
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100478 /* Can only have one of 'n', 'b', 'o' and 'x'. */
479 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000480 {
481 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000482 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000483 }
484
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100485 /* From here on "sort_nr" is used as a flag for any integer number
486 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100487 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000488
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000489 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000490 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000491 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000492 * When sorting on strings "start_col_nr" is the offset in the line, for
493 * numbers sorting it's the number to sort on. This means the pattern
494 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000495 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000496 */
497 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
498 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000499 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000500 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000501 if (maxlen < len)
502 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000503
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000504 start_col = 0;
505 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000506 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000507 {
508 if (sort_rx)
509 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000510 start_col = (colnr_T)(regmatch.startp[0] - s);
511 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000512 }
513 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000514 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000515 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000516 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000517 if (regmatch.regprog != NULL)
518 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000519
Bram Moolenaar937204a2016-01-30 23:37:38 +0100520 if (sort_nr
521#ifdef FEAT_FLOAT
522 || sort_flt
523#endif
524 )
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000525 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000526 /* Make sure vim_str2nr doesn't read any digits past the end
527 * of the match, by temporarily terminating the string there */
528 s2 = s + end_col;
529 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200530 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000531 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000532 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100533 if (sort_nr)
534 {
535 if (sort_what & STR2NR_HEX)
536 s = skiptohex(p);
537 else if (sort_what & STR2NR_BIN)
538 s = skiptobin(p);
539 else
540 s = skiptodigit(p);
541 if (s > p && s[-1] == '-')
542 --s; /* include preceding negative sign */
543 if (*s == NUL)
544 /* empty line should sort before any number */
545 nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
546 else
547 vim_str2nr(s, NULL, NULL, sort_what,
548 &nrs[lnum - eap->line1].st_u.value, NULL, 0);
549 }
550#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000551 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100552 {
553 s = skipwhite(p);
554 if (*s == '+')
555 s = skipwhite(s + 1);
556
557 if (*s == NUL)
558 /* empty line should sort before any number */
559 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
560 else
561 nrs[lnum - eap->line1].st_u.value_flt =
562 strtod((char *)s, NULL);
563 }
564#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200565 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000566 }
567 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000568 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000569 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100570 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
571 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000572 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000573
574 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000575
576 if (regmatch.regprog != NULL)
577 fast_breakcheck();
578 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000579 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000580 }
581
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000582 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000583 sortbuf1 = alloc((unsigned)maxlen + 1);
584 if (sortbuf1 == NULL)
585 goto sortend;
586 sortbuf2 = alloc((unsigned)maxlen + 1);
587 if (sortbuf2 == NULL)
588 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000589
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000590 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000591 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000592
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000593 if (sort_abort)
594 goto sortend;
595
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000596 /* Insert the lines in the sorted order below the last one. */
597 lnum = eap->line2;
598 for (i = 0; i < count; ++i)
599 {
600 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
601 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000602 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000603 {
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100604 /* Copy the line into a buffer, it may become invalid in
605 * ml_append(). And it's needed for "unique". */
606 STRCPY(sortbuf1, s);
607 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000608 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000609 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000610 fast_breakcheck();
611 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000612 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000613 }
614
615 /* delete the original lines if appending worked */
616 if (i == count)
617 for (i = 0; i < count; ++i)
618 ml_delete(eap->line1, FALSE);
619 else
620 count = 0;
621
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000622 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000623 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000624 if (deleted > 0)
625 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
626 else if (deleted < 0)
627 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
628 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000629
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000630 curwin->w_cursor.lnum = eap->line1;
631 beginline(BL_WHITE | BL_FIX);
632
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000633sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000634 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000635 vim_free(sortbuf1);
636 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200637 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000638 if (got_int)
639 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000640}
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642/*
643 * ":retab".
644 */
645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100646ex_retab(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 linenr_T lnum;
649 int got_tab = FALSE;
650 long num_spaces = 0;
651 long num_tabs;
652 long len;
653 long col;
654 long vcol;
655 long start_col = 0; /* For start of white-space string */
656 long start_vcol = 0; /* For start of white-space string */
657 int temp;
658 long old_len;
659 char_u *ptr;
660 char_u *new_line = (char_u *)1; /* init to non-NULL */
661 int did_undo; /* called u_save for current line */
662 int new_ts;
663 int save_list;
664 linenr_T first_line = 0; /* first changed line */
665 linenr_T last_line = 0; /* last changed line */
666
667 save_list = curwin->w_p_list;
668 curwin->w_p_list = 0; /* don't want list mode here */
669
670 new_ts = getdigits(&(eap->arg));
671 if (new_ts < 0)
672 {
673 EMSG(_(e_positive));
674 return;
675 }
676 if (new_ts == 0)
677 new_ts = curbuf->b_p_ts;
678 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
679 {
680 ptr = ml_get(lnum);
681 col = 0;
682 vcol = 0;
683 did_undo = FALSE;
684 for (;;)
685 {
686 if (vim_iswhite(ptr[col]))
687 {
688 if (!got_tab && num_spaces == 0)
689 {
690 /* First consecutive white-space */
691 start_vcol = vcol;
692 start_col = col;
693 }
694 if (ptr[col] == ' ')
695 num_spaces++;
696 else
697 got_tab = TRUE;
698 }
699 else
700 {
701 if (got_tab || (eap->forceit && num_spaces > 1))
702 {
703 /* Retabulate this string of white-space */
704
705 /* len is virtual length of white string */
706 len = num_spaces = vcol - start_vcol;
707 num_tabs = 0;
708 if (!curbuf->b_p_et)
709 {
710 temp = new_ts - (start_vcol % new_ts);
711 if (num_spaces >= temp)
712 {
713 num_spaces -= temp;
714 num_tabs++;
715 }
716 num_tabs += num_spaces / new_ts;
717 num_spaces -= (num_spaces / new_ts) * new_ts;
718 }
719 if (curbuf->b_p_et || got_tab ||
720 (num_spaces + num_tabs < len))
721 {
722 if (did_undo == FALSE)
723 {
724 did_undo = TRUE;
725 if (u_save((linenr_T)(lnum - 1),
726 (linenr_T)(lnum + 1)) == FAIL)
727 {
728 new_line = NULL; /* flag out-of-memory */
729 break;
730 }
731 }
732
733 /* len is actual number of white characters used */
734 len = num_spaces + num_tabs;
735 old_len = (long)STRLEN(ptr);
736 new_line = lalloc(old_len - col + start_col + len + 1,
737 TRUE);
738 if (new_line == NULL)
739 break;
740 if (start_col > 0)
741 mch_memmove(new_line, ptr, (size_t)start_col);
742 mch_memmove(new_line + start_col + len,
743 ptr + col, (size_t)(old_len - col + 1));
744 ptr = new_line + start_col;
745 for (col = 0; col < len; col++)
746 ptr[col] = (col < num_tabs) ? '\t' : ' ';
747 ml_replace(lnum, new_line, FALSE);
748 if (first_line == 0)
749 first_line = lnum;
750 last_line = lnum;
751 ptr = new_line;
752 col = start_col + len;
753 }
754 }
755 got_tab = FALSE;
756 num_spaces = 0;
757 }
758 if (ptr[col] == NUL)
759 break;
760 vcol += chartabsize(ptr + col, (colnr_T)vcol);
761#ifdef FEAT_MBYTE
762 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000763 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 else
765#endif
766 ++col;
767 }
768 if (new_line == NULL) /* out of memory */
769 break;
770 line_breakcheck();
771 }
772 if (got_int)
773 EMSG(_(e_interr));
774
775 if (curbuf->b_p_ts != new_ts)
776 redraw_curbuf_later(NOT_VALID);
777 if (first_line != 0)
778 changed_lines(first_line, 0, last_line + 1, 0L);
779
780 curwin->w_p_list = save_list; /* restore 'list' */
781
782 curbuf->b_p_ts = new_ts;
783 coladvance(curwin->w_curswant);
784
785 u_clearline();
786}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788/*
789 * :move command - move lines line1-line2 to line dest
790 *
791 * return FAIL for failure, OK otherwise
792 */
793 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100794do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 char_u *str;
797 linenr_T l;
798 linenr_T extra; /* Num lines added before line1 */
799 linenr_T num_lines; /* Num lines moved */
800 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200801#ifdef FEAT_FOLDING
802 int isFolded;
803
804 /* Moving lines seems to corrupt the folds, delete folding info now
805 * and recreate it when finished. Don't do this for manual folding, it
806 * would delete all folds. */
807 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin);
808 if (isFolded)
809 deleteFoldRecurse(&curwin->w_folds);
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 if (dest >= line1 && dest < line2)
813 {
814 EMSG(_("E134: Move lines into themselves"));
815 return FAIL;
816 }
817
818 num_lines = line2 - line1 + 1;
819
820 /*
821 * First we copy the old text to its new location -- webb
822 * Also copy the flag that ":global" command uses.
823 */
824 if (u_save(dest, dest + 1) == FAIL)
825 return FAIL;
826 for (extra = 0, l = line1; l <= line2; l++)
827 {
828 str = vim_strsave(ml_get(l + extra));
829 if (str != NULL)
830 {
831 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
832 vim_free(str);
833 if (dest < line1)
834 extra++;
835 }
836 }
837
838 /*
839 * Now we must be careful adjusting our marks so that we don't overlap our
840 * mark_adjust() calls.
841 *
842 * We adjust the marks within the old text so that they refer to the
843 * last lines of the file (temporarily), because we know no other marks
844 * will be set there since these line numbers did not exist until we added
845 * our new lines.
846 *
847 * Then we adjust the marks on lines between the old and new text positions
848 * (either forwards or backwards).
849 *
850 * And Finally we adjust the marks we put at the end of the file back to
851 * their final destination at the new text position -- webb
852 */
853 last_line = curbuf->b_ml.ml_line_count;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100854 mark_adjust_nofold(line1, line2, last_line - line2, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (dest >= line2)
856 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100857 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
858#ifdef FEAT_FOLDING
859 win_T *win;
860 tabpage_T *tp;
861
862 FOR_ALL_TAB_WINDOWS(tp, win) {
863 if (win->w_buffer == curbuf)
864 foldSwapRange(&win->w_folds, line1, line2, dest + 1,
865 dest + num_lines);
866 }
867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 curbuf->b_op_start.lnum = dest - num_lines + 1;
869 curbuf->b_op_end.lnum = dest;
870 }
871 else
872 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100873 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
874#ifdef FEAT_FOLDING
875 win_T *win;
876 tabpage_T *tp;
877
878 FOR_ALL_TAB_WINDOWS(tp, win) {
879 if (win->w_buffer == curbuf)
880 foldSwapRange(&win->w_folds, dest + 1, line1 - 1, line1, line2);
881 }
882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 curbuf->b_op_start.lnum = dest + 1;
884 curbuf->b_op_end.lnum = dest + num_lines;
885 }
886 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100887 mark_adjust_nofold(last_line - num_lines + 1, last_line,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 -(last_line - dest - extra), 0L);
889
890 /*
891 * Now we delete the original text -- webb
892 */
893 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
894 return FAIL;
895
896 for (l = line1; l <= line2; l++)
897 ml_delete(line1 + extra, TRUE);
898
899 if (!global_busy && num_lines > p_report)
900 {
901 if (num_lines == 1)
902 MSG(_("1 line moved"));
903 else
904 smsg((char_u *)_("%ld lines moved"), num_lines);
905 }
906
907 /*
908 * Leave the cursor on the last of the moved lines.
909 */
910 if (dest >= line1)
911 curwin->w_cursor.lnum = dest;
912 else
913 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
914
915 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +0100916 {
917 dest += num_lines + 1;
918 last_line = curbuf->b_ml.ml_line_count;
919 if (dest > last_line + 1)
920 dest = last_line + 1;
921 changed_lines(line1, 0, dest, 0L);
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 else
924 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
925
Bram Moolenaard5f69332015-04-15 12:43:50 +0200926#ifdef FEAT_FOLDING
927 /* recreate folds */
928 if (isFolded)
929 foldUpdateAll(curwin);
930#endif
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 return OK;
933}
934
935/*
936 * ":copy"
937 */
938 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100939ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
941 linenr_T count;
942 char_u *p;
943
944 count = line2 - line1 + 1;
945 curbuf->b_op_start.lnum = n + 1;
946 curbuf->b_op_end.lnum = n + count;
947 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
948
949 /*
950 * there are three situations:
951 * 1. destination is above line1
952 * 2. destination is between line1 and line2
953 * 3. destination is below line2
954 *
955 * n = destination (when starting)
956 * curwin->w_cursor.lnum = destination (while copying)
957 * line1 = start of source (while copying)
958 * line2 = end of source (while copying)
959 */
960 if (u_save(n, n + 1) == FAIL)
961 return;
962
963 curwin->w_cursor.lnum = n;
964 while (line1 <= line2)
965 {
966 /* need to use vim_strsave() because the line will be unlocked within
967 * ml_append() */
968 p = vim_strsave(ml_get(line1));
969 if (p != NULL)
970 {
971 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
972 vim_free(p);
973 }
974 /* situation 2: skip already copied lines */
975 if (line1 == n)
976 line1 = curwin->w_cursor.lnum;
977 ++line1;
978 if (curwin->w_cursor.lnum < line1)
979 ++line1;
980 if (curwin->w_cursor.lnum < line2)
981 ++line2;
982 ++curwin->w_cursor.lnum;
983 }
984
985 appended_lines_mark(n, count);
986
987 msgmore((long)count);
988}
989
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000990static char_u *prevcmd = NULL; /* the previous command */
991
992#if defined(EXITFREE) || defined(PROTO)
993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100994free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000995{
996 vim_free(prevcmd);
997}
998#endif
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/*
1001 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
1002 * Bangs in the argument are replaced with the previously entered command.
1003 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
1005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001006do_bang(
1007 int addr_count,
1008 exarg_T *eap,
1009 int forceit,
1010 int do_in,
1011 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
1013 char_u *arg = eap->arg; /* command */
1014 linenr_T line1 = eap->line1; /* start of range */
1015 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 char_u *newcmd = NULL; /* the new command */
1017 int free_newcmd = FALSE; /* need to free() newcmd */
1018 int ins_prevcmd;
1019 char_u *t;
1020 char_u *p;
1021 char_u *trailarg;
1022 int len;
1023 int scroll_save = msg_scroll;
1024
1025 /*
1026 * Disallow shell commands for "rvim".
1027 * Disallow shell commands from .exrc and .vimrc in current directory for
1028 * security reasons.
1029 */
1030 if (check_restricted() || check_secure())
1031 return;
1032
1033 if (addr_count == 0) /* :! */
1034 {
1035 msg_scroll = FALSE; /* don't scroll here */
1036 autowrite_all();
1037 msg_scroll = scroll_save;
1038 }
1039
1040 /*
1041 * Try to find an embedded bang, like in :!<cmd> ! [args]
1042 * (:!! is indicated by the 'forceit' variable)
1043 */
1044 ins_prevcmd = forceit;
1045 trailarg = arg;
1046 do
1047 {
1048 len = (int)STRLEN(trailarg) + 1;
1049 if (newcmd != NULL)
1050 len += (int)STRLEN(newcmd);
1051 if (ins_prevcmd)
1052 {
1053 if (prevcmd == NULL)
1054 {
1055 EMSG(_(e_noprev));
1056 vim_free(newcmd);
1057 return;
1058 }
1059 len += (int)STRLEN(prevcmd);
1060 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001061 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {
1063 vim_free(newcmd);
1064 return;
1065 }
1066 *t = NUL;
1067 if (newcmd != NULL)
1068 STRCAT(t, newcmd);
1069 if (ins_prevcmd)
1070 STRCAT(t, prevcmd);
1071 p = t + STRLEN(t);
1072 STRCAT(t, trailarg);
1073 vim_free(newcmd);
1074 newcmd = t;
1075
1076 /*
1077 * Scan the rest of the argument for '!', which is replaced by the
1078 * previous command. "\!" is replaced by "!" (this is vi compatible).
1079 */
1080 trailarg = NULL;
1081 while (*p)
1082 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001083 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001086 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 else
1088 {
1089 trailarg = p;
1090 *trailarg++ = NUL;
1091 ins_prevcmd = TRUE;
1092 break;
1093 }
1094 }
1095 ++p;
1096 }
1097 } while (trailarg != NULL);
1098
1099 vim_free(prevcmd);
1100 prevcmd = newcmd;
1101
1102 if (bangredo) /* put cmd in redo buffer for ! command */
1103 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001104 /* If % or # appears in the command, it must have been escaped.
1105 * Reescape them, so that redoing them does not substitute them by the
1106 * buffername. */
1107 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1108
1109 if (cmd != NULL)
1110 {
1111 AppendToRedobuffLit(cmd, -1);
1112 vim_free(cmd);
1113 }
1114 else
1115 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 AppendToRedobuff((char_u *)"\n");
1117 bangredo = FALSE;
1118 }
1119 /*
1120 * Add quotes around the command, for shells that need them.
1121 */
1122 if (*p_shq != NUL)
1123 {
1124 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1125 if (newcmd == NULL)
1126 return;
1127 STRCPY(newcmd, p_shq);
1128 STRCAT(newcmd, prevcmd);
1129 STRCAT(newcmd, p_shq);
1130 free_newcmd = TRUE;
1131 }
1132 if (addr_count == 0) /* :! */
1133 {
1134 /* echo the command */
1135 msg_start();
1136 msg_putchar(':');
1137 msg_putchar('!');
1138 msg_outtrans(newcmd);
1139 msg_clr_eos();
1140 windgoto(msg_row, msg_col);
1141
1142 do_shell(newcmd, 0);
1143 }
1144 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 /* Careful: This may recursively call do_bang() again! (because of
1147 * autocommands) */
1148 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001149#ifdef FEAT_AUTOCMD
1150 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1151#endif
1152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (free_newcmd)
1154 vim_free(newcmd);
1155}
1156
1157/*
1158 * do_filter: filter lines through a command given by the user
1159 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 * We mostly use temp files and the call_shell() routine here. This would
1161 * normally be done using pipes on a UNIX machine, but this is more portable
1162 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * to deal with redirection somehow, and should handle things like looking
1164 * at the PATH env. variable, and adding reasonable extensions to the
1165 * command name given by the user. All reasonable versions of call_shell()
1166 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 * Alternatively, if on Unix and redirecting input or output, but not both,
1168 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 * We use input redirection if do_in is TRUE.
1170 * We use output redirection if do_out is TRUE.
1171 */
1172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001173do_filter(
1174 linenr_T line1,
1175 linenr_T line2,
1176 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1177 char_u *cmd,
1178 int do_in,
1179 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 char_u *itmp = NULL;
1182 char_u *otmp = NULL;
1183 linenr_T linecount;
1184 linenr_T read_linecount;
1185 pos_T cursor_save;
1186 char_u *cmd_buf;
1187#ifdef FEAT_AUTOCMD
1188 buf_T *old_curbuf = curbuf;
1189#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
1192 if (*cmd == NUL) /* no filter command */
1193 return;
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 cursor_save = curwin->w_cursor;
1196 linecount = line2 - line1 + 1;
1197 curwin->w_cursor.lnum = line1;
1198 curwin->w_cursor.col = 0;
1199 changed_line_abv_curs();
1200 invalidate_botline();
1201
1202 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 * When using temp files:
1204 * 1. * Form temp file names
1205 * 2. * Write the lines to a temp file
1206 * 3. Run the filter command on the temp file
1207 * 4. * Read the output of the command into the buffer
1208 * 5. * Delete the original lines to be filtered
1209 * 6. * Remove the temp files
1210 *
1211 * When writing the input with a pipe or when catching the output with a
1212 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 */
1214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215 if (do_out)
1216 shell_flags |= SHELL_DOOUT;
1217
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001218#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1222 shell_flags |= SHELL_READ;
1223 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225 else if (do_in && !do_out && !p_stmp)
1226 {
1227 /* Use a pipe to write stdin of the command, do not use a temp file. */
1228 shell_flags |= SHELL_WRITE;
1229 curbuf->b_op_start.lnum = line1;
1230 curbuf->b_op_end.lnum = line2;
1231 }
1232 else if (do_in && do_out && !p_stmp)
1233 {
1234 /* Use a pipe to write stdin and fetch stdout of the command, do not
1235 * use a temp file. */
1236 shell_flags |= SHELL_READ|SHELL_WRITE;
1237 curbuf->b_op_start.lnum = line1;
1238 curbuf->b_op_end.lnum = line2;
1239 curwin->w_cursor.lnum = line2;
1240 }
1241 else
1242#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001243 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1244 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245 {
1246 EMSG(_(e_notmp));
1247 goto filterend;
1248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
1250/*
1251 * The writing and reading of temp files will not be shown.
1252 * Vi also doesn't do this and the messages are not very informative.
1253 */
1254 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001255 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 FALSE, FALSE, FALSE, TRUE) == FAIL)
1257 {
1258 msg_putchar('\n'); /* keep message from buf_write() */
1259 --no_wait_return;
1260#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1261 if (!aborting())
1262#endif
1263 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1264 goto filterend;
1265 }
1266#ifdef FEAT_AUTOCMD
1267 if (curbuf != old_curbuf)
1268 goto filterend;
1269#endif
1270
1271 if (!do_out)
1272 msg_putchar('\n');
1273
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001274 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1276 if (cmd_buf == NULL)
1277 goto filterend;
1278
1279 windgoto((int)Rows - 1, 0);
1280 cursor_on();
1281
1282 /*
1283 * When not redirecting the output the command can write anything to the
1284 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1285 * stderr output of external command. Clear the screen later.
1286 * If do_in is FALSE, this could be something like ":r !cat", which may
1287 * also mess up the screen, clear it later.
1288 */
1289 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1290 redraw_later_clear();
1291
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001292 if (do_out)
1293 {
1294 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001295 {
1296 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001297 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001298 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001299 redraw_curbuf_later(VALID);
1300 }
1301 read_linecount = curbuf->b_ml.ml_line_count;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /*
1304 * When call_shell() fails wait_return() is called to give the user a
1305 * chance to read the error messages. Otherwise errors are ignored, so you
1306 * can see the error messages from the command that appear on stdout; use
1307 * 'u' to fix the text
1308 * Switch to cooked mode when not redirecting stdin, avoids that something
1309 * like ":r !cat" hangs.
1310 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1311 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001312 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {
1314 redraw_later_clear();
1315 wait_return(FALSE);
1316 }
1317 vim_free(cmd_buf);
1318
1319 did_check_timestamps = FALSE;
1320 need_check_timestamps = TRUE;
1321
1322 /* When interrupting the shell command, it may still have produced some
1323 * useful output. Reset got_int here, so that readfile() won't cancel
1324 * reading. */
1325 ui_breakcheck();
1326 got_int = FALSE;
1327
1328 if (do_out)
1329 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001330 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001332 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001333 eap, READ_FILTER) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001335#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1336 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001338 {
1339 msg_putchar('\n');
1340 EMSG2(_(e_notread), otmp);
1341 }
1342 goto error;
1343 }
1344#ifdef FEAT_AUTOCMD
1345 if (curbuf != old_curbuf)
1346 goto filterend;
1347#endif
1348 }
1349
1350 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1351
1352 if (shell_flags & SHELL_READ)
1353 {
1354 curbuf->b_op_start.lnum = line2 + 1;
1355 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1356 appended_lines_mark(line2, read_linecount);
1357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 if (do_in)
1360 {
1361 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (read_linecount >= linecount)
1364 /* move all marks from old lines to new lines */
1365 mark_adjust(line1, line2, linecount, 0L);
1366 else
1367 {
1368 /* move marks from old lines to new lines, delete marks
1369 * that are in deleted lines */
1370 mark_adjust(line1, line1 + read_linecount - 1,
1371 linecount, 0L);
1372 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1373 }
1374 }
1375
1376 /*
1377 * Put cursor on first filtered line for ":range!cmd".
1378 * Adjust '[ and '] (set by buf_write()).
1379 */
1380 curwin->w_cursor.lnum = line1;
1381 del_lines(linecount, TRUE);
1382 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1383 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1384 write_lnum_adjust(-linecount); /* adjust last line
1385 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001386#ifdef FEAT_FOLDING
1387 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 else
1391 {
1392 /*
1393 * Put cursor on last new line for ":r !cmd".
1394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001396 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1400 --no_wait_return;
1401
1402 if (linecount > p_report)
1403 {
1404 if (do_in)
1405 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001406 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1407 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001410 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 else
1413 msgmore((long)linecount);
1414 }
1415 }
1416 else
1417 {
1418error:
1419 /* put cursor back in same position for ":w !cmd" */
1420 curwin->w_cursor = cursor_save;
1421 --no_wait_return;
1422 wait_return(FALSE);
1423 }
1424
1425filterend:
1426
1427#ifdef FEAT_AUTOCMD
1428 if (curbuf != old_curbuf)
1429 {
1430 --no_wait_return;
1431 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1432 }
1433#endif
1434 if (itmp != NULL)
1435 mch_remove(itmp);
1436 if (otmp != NULL)
1437 mch_remove(otmp);
1438 vim_free(itmp);
1439 vim_free(otmp);
1440}
1441
1442/*
1443 * Call a shell to execute a command.
1444 * When "cmd" is NULL start an interactive shell.
1445 */
1446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001447do_shell(
1448 char_u *cmd,
1449 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 buf_T *buf;
1452#ifndef FEAT_GUI_MSWIN
1453 int save_nwr;
1454#endif
1455#ifdef MSWIN
1456 int winstart = FALSE;
1457#endif
1458
1459 /*
1460 * Disallow shell commands for "rvim".
1461 * Disallow shell commands from .exrc and .vimrc in current directory for
1462 * security reasons.
1463 */
1464 if (check_restricted() || check_secure())
1465 {
1466 msg_end();
1467 return;
1468 }
1469
1470#ifdef MSWIN
1471 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 * Check if ":!start" is used.
1473 */
1474 if (cmd != NULL)
1475 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1476#endif
1477
1478 /*
1479 * For autocommands we want to get the output on the current screen, to
1480 * avoid having to type return below.
1481 */
1482 msg_putchar('\r'); /* put cursor at start of line */
1483#ifdef FEAT_AUTOCMD
1484 if (!autocmd_busy)
1485#endif
1486 {
1487#ifdef MSWIN
1488 if (!winstart)
1489#endif
1490 stoptermcap();
1491 }
1492#ifdef MSWIN
1493 if (!winstart)
1494#endif
1495 msg_putchar('\n'); /* may shift screen one line up */
1496
1497 /* warning message before calling the shell */
1498 if (p_warn
1499#ifdef FEAT_AUTOCMD
1500 && !autocmd_busy
1501#endif
1502 && msg_silent == 0)
Bram Moolenaar29323592016-07-24 22:04:11 +02001503 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (bufIsChanged(buf))
1505 {
1506#ifdef FEAT_GUI_MSWIN
1507 if (!winstart)
1508 starttermcap(); /* don't want a message box here */
1509#endif
1510 MSG_PUTS(_("[No write since last change]\n"));
1511#ifdef FEAT_GUI_MSWIN
1512 if (!winstart)
1513 stoptermcap();
1514#endif
1515 break;
1516 }
1517
1518 /* This windgoto is required for when the '\n' resulted in a "delete line
1519 * 1" command to the terminal. */
1520 if (!swapping_screen())
1521 windgoto(msg_row, msg_col);
1522 cursor_on();
1523 (void)call_shell(cmd, SHELL_COOKED | flags);
1524 did_check_timestamps = FALSE;
1525 need_check_timestamps = TRUE;
1526
1527 /*
1528 * put the message cursor at the end of the screen, avoids wait_return()
1529 * to overwrite the text that the external command showed
1530 */
1531 if (!swapping_screen())
1532 {
1533 msg_row = Rows - 1;
1534 msg_col = 0;
1535 }
1536
1537#ifdef FEAT_AUTOCMD
1538 if (autocmd_busy)
1539 {
1540 if (msg_silent == 0)
1541 redraw_later_clear();
1542 }
1543 else
1544#endif
1545 {
1546 /*
1547 * For ":sh" there is no need to call wait_return(), just redraw.
1548 * Also for the Win32 GUI (the output is in a console window).
1549 * Otherwise there is probably text on the screen that the user wants
1550 * to read before redrawing, so call wait_return().
1551 */
1552#ifndef FEAT_GUI_MSWIN
1553 if (cmd == NULL
1554# ifdef WIN3264
1555 || (winstart && !need_wait_return)
1556# endif
1557 )
1558 {
1559 if (msg_silent == 0)
1560 redraw_later_clear();
1561 need_wait_return = FALSE;
1562 }
1563 else
1564 {
1565 /*
1566 * If we switch screens when starttermcap() is called, we really
1567 * want to wait for "hit return to continue".
1568 */
1569 save_nwr = no_wait_return;
1570 if (swapping_screen())
1571 no_wait_return = FALSE;
1572# ifdef AMIGA
1573 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1574# else
1575 wait_return(msg_silent == 0);
1576# endif
1577 no_wait_return = save_nwr;
1578 }
1579#endif /* FEAT_GUI_W32 */
1580
1581#ifdef MSWIN
1582 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1583#endif
1584 starttermcap(); /* start termcap if not done by wait_return() */
1585
1586 /*
1587 * In an Amiga window redrawing is caused by asking the window size.
1588 * If we got an interrupt this will not work. The chance that the
1589 * window size is wrong is very small, but we need to redraw the
1590 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1591 * but it saves an extra redraw.
1592 */
1593#ifdef AMIGA
1594 if (skip_redraw) /* ':' hit in wait_return() */
1595 {
1596 if (msg_silent == 0)
1597 redraw_later_clear();
1598 }
1599 else if (term_console)
1600 {
1601 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1602 if (got_int && msg_silent == 0)
1603 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1604 else
1605 must_redraw = 0; /* no extra redraw needed */
1606 }
1607#endif
1608 }
1609
1610 /* display any error messages now */
1611 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001612
1613#ifdef FEAT_AUTOCMD
1614 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616}
1617
1618/*
1619 * Create a shell command from a command string, input redirection file and
1620 * output redirection file.
1621 * Returns an allocated string with the shell command, or NULL for failure.
1622 */
1623 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001624make_filter_cmd(
1625 char_u *cmd, /* command */
1626 char_u *itmp, /* NULL or name of input file */
1627 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 char_u *buf;
1630 long_u len;
1631
Bram Moolenaar53076832015-12-31 19:53:21 +01001632#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001633 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001634 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001635
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001636 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001637 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1638 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001639 if (is_fish_shell)
1640 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1641 else
1642#endif
1643 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (itmp != NULL)
1645 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1646 if (otmp != NULL)
1647 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1648 buf = lalloc(len, TRUE);
1649 if (buf == NULL)
1650 return NULL;
1651
Bram Moolenaar53076832015-12-31 19:53:21 +01001652#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001654 * Put braces around the command (for concatenated commands) when
1655 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001657 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001658 {
1659 if (is_fish_shell)
1660 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1661 else
1662 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1663 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001664 else
1665 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (itmp != NULL)
1667 {
1668 STRCAT(buf, " < ");
1669 STRCAT(buf, itmp);
1670 }
1671#else
1672 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001673 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 * the use of commands in a pipe.
1675 */
1676 STRCPY(buf, cmd);
1677 if (itmp != NULL)
1678 {
1679 char_u *p;
1680
1681 /*
1682 * If there is a pipe, we have to put the '<' in front of it.
1683 * Don't do this when 'shellquote' is not empty, otherwise the
1684 * redirection would be inside the quotes.
1685 */
1686 if (*p_shq == NUL)
1687 {
1688 p = vim_strchr(buf, '|');
1689 if (p != NULL)
1690 *p = NUL;
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1693 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (*p_shq == NUL)
1695 {
1696 p = vim_strchr(cmd, '|');
1697 if (p != NULL)
1698 {
1699 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1700 STRCAT(buf, p);
1701 }
1702 }
1703 }
1704#endif
1705 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001706 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 return buf;
1709}
1710
1711/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001712 * Append output redirection for file "fname" to the end of string buffer
1713 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 * Works with the 'shellredir' and 'shellpipe' options.
1715 * The caller should make sure that there is enough room:
1716 * STRLEN(opt) + STRLEN(fname) + 3
1717 */
1718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719append_redir(
1720 char_u *buf,
1721 int buflen,
1722 char_u *opt,
1723 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001726 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001728 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001729 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001731 {
1732 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001734 if (p[1] == '%') /* skip %% */
1735 ++p;
1736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (p != NULL)
1738 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001739 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1740 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1741 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001744 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 " %s %s",
1747#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749#endif
1750 (char *)opt, (char *)fname);
1751}
1752
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001753#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001755static int no_viminfo(void);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001756static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001757static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001758static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759static int viminfo_errcnt;
1760
1761 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001762no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 /* "vim -i NONE" does not read or write a viminfo file */
1765 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1766}
1767
1768/*
1769 * Report an error for reading a viminfo file.
1770 * Count the number of errors. When there are more than 10, return TRUE.
1771 */
1772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001773viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001775 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1776 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001777 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001778 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1779 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 emsg(IObuff);
1781 if (++viminfo_errcnt >= 10)
1782 {
1783 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1784 return TRUE;
1785 }
1786 return FALSE;
1787}
1788
1789/*
1790 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001791 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 */
1793 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001794read_viminfo(
1795 char_u *file, /* file name or NULL to use default name */
1796 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 FILE *fp;
1799 char_u *fname;
1800
1801 if (no_viminfo())
1802 return FAIL;
1803
Bram Moolenaard812df62008-11-09 12:46:09 +00001804 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (fname == NULL)
1806 return FAIL;
1807 fp = mch_fopen((char *)fname, READBIN);
1808
1809 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001810 {
1811 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001812 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1813 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001814 (flags & VIF_WANT_INFO) ? _(" info") : "",
1815 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1816 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001817 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001818 verbose_leave();
1819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 vim_free(fname);
1822 if (fp == NULL)
1823 return FAIL;
1824
1825 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001826 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 return OK;
1830}
1831
1832/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001833 * Write the viminfo file. The old one is read in first so that effectively a
1834 * merge of current info and old info is done. This allows multiple vims to
1835 * run simultaneously, without losing any marks etc.
1836 * If "forceit" is TRUE, then the old file is not read in, and only internal
1837 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001840write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 char_u *fname;
1843 FILE *fp_in = NULL; /* input viminfo file, if any */
1844 FILE *fp_out = NULL; /* output viminfo file */
1845 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001846 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 char_u *wp;
1848#if defined(UNIX) || defined(VMS)
1849 mode_t umask_save;
1850#endif
1851#ifdef UNIX
1852 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001853 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001855#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001856 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859 if (no_viminfo())
1860 return;
1861
1862 fname = viminfo_filename(file); /* may set to default if NULL */
1863 if (fname == NULL)
1864 return;
1865
1866 fp_in = mch_fopen((char *)fname, READBIN);
1867 if (fp_in == NULL)
1868 {
1869 /* if it does exist, but we can't read it, don't try writing */
1870 if (mch_stat((char *)fname, &st_new) == 0)
1871 goto end;
1872#if defined(UNIX) || defined(VMS)
1873 /*
1874 * For Unix we create the .viminfo non-accessible for others,
1875 * because it may contain text from non-accessible documents.
1876 */
1877 umask_save = umask(077);
1878#endif
1879 fp_out = mch_fopen((char *)fname, WRITEBIN);
1880#if defined(UNIX) || defined(VMS)
1881 (void)umask(umask_save);
1882#endif
1883 }
1884 else
1885 {
1886 /*
1887 * There is an existing viminfo file. Create a temporary file to
1888 * write the new viminfo into, in the same directory as the
1889 * existing viminfo file, which will be renamed later.
1890 */
1891#ifdef UNIX
1892 /*
1893 * For Unix we check the owner of the file. It's not very nice to
1894 * overwrite a user's viminfo file after a "su root", with a
1895 * viminfo file that the user can't read.
1896 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001897 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001898 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001900 if (mch_stat((char *)fname, &st_old) == 0
1901 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001902 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 ? (st_old.st_mode & 0200)
1904 : (st_old.st_gid == getgid()
1905 ? (st_old.st_mode & 0020)
1906 : (st_old.st_mode & 0002))))
1907 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001908 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1912 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001913 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 goto end;
1915 }
1916#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001917#ifdef WIN3264
1918 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001919 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 /*
1923 * Make tempname.
1924 * May try twice: Once normal and once with shortname set, just in
1925 * case somebody puts his viminfo file in an 8.3 filesystem.
1926 */
1927 for (;;)
1928 {
1929 tempname = buf_modname(
1930#ifdef UNIX
1931 shortname,
1932#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934#endif
1935 fname,
1936#ifdef VMS
1937 (char_u *)"-tmp",
1938#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#endif
1941 FALSE);
1942 if (tempname == NULL) /* out of memory */
1943 break;
1944
1945 /*
1946 * Check if tempfile already exists. Never overwrite an
1947 * existing file!
1948 */
1949 if (mch_stat((char *)tempname, &st_new) == 0)
1950 {
1951#ifdef UNIX
1952 /*
1953 * Check if tempfile is same as original file. May happen
1954 * when modname() gave the same file back. E.g. silly
1955 * link, or file name-length reached. Try again with
1956 * shortname set.
1957 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001958 if (!shortname && st_new.st_dev == st_old.st_dev
1959 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 vim_free(tempname);
1962 tempname = NULL;
1963 shortname = TRUE;
1964 continue;
1965 }
1966#endif
1967 /*
1968 * Try another name. Change one character, just before
1969 * the extension. This should also work for an 8.3
1970 * file name, when after adding the extension it still is
1971 * the same file as the original.
1972 */
1973 wp = tempname + STRLEN(tempname) - 5;
1974 if (wp < gettail(tempname)) /* empty file name? */
1975 wp = gettail(tempname);
1976 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1977 --*wp)
1978 {
1979 /*
1980 * They all exist? Must be something wrong! Don't
1981 * write the viminfo file then.
1982 */
1983 if (*wp == 'a')
1984 {
Bram Moolenaar2d358992016-06-12 21:20:54 +02001985 EMSG2(_("E929: Too many viminfo temp files, like %s!"),
1986 tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 vim_free(tempname);
1988 tempname = NULL;
1989 break;
1990 }
1991 }
1992 }
1993 break;
1994 }
1995
1996 if (tempname != NULL)
1997 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00001998#ifdef VMS
1999 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00002000 umask_save = umask(077);
2001 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2002 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002003#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00002004 int fd;
2005
2006 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002007 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002008 * Unix: same as original file, but strip s-bit. Reset umask to
2009 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002010 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00002011# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002012 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002013 fd = mch_open((char *)tempname,
2014 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002015 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002016 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002017# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002018 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002019 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002020# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00002021 if (fd < 0)
2022 fp_out = NULL;
2023 else
2024 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002025#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 /*
2028 * If we can't create in the same directory, try creating a
2029 * "normal" temp file.
2030 */
2031 if (fp_out == NULL)
2032 {
2033 vim_free(tempname);
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002034 if ((tempname = vim_tempname('o', TRUE)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2036 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037
2038#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00002040 * Make sure the owner can read/write it. This only works for
2041 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 */
2043 if (fp_out != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002044 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046 }
2047 }
2048
2049 /*
2050 * Check if the new viminfo file can be written to.
2051 */
2052 if (fp_out == NULL)
2053 {
2054 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002055 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (fp_in != NULL)
2057 fclose(fp_in);
2058 goto end;
2059 }
2060
2061 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002062 {
2063 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002064 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002065 verbose_leave();
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002069 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar927030a2016-03-15 18:23:55 +01002071 if (fclose(fp_out) == EOF)
2072 ++viminfo_errcnt;
2073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (fp_in != NULL)
2075 {
2076 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002077
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002078 /* In case of an error keep the original viminfo file. Otherwise
2079 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002080 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002081 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002082 if (vim_rename(tempname, fname) == -1)
2083 {
2084 ++viminfo_errcnt;
2085 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2086 }
2087# ifdef WIN3264
2088 /* If the viminfo file was hidden then also hide the new file. */
2089 else if (hidden)
2090 mch_hide(fname);
2091# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002092 }
2093 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 mch_remove(tempname);
2095 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097end:
2098 vim_free(fname);
2099 vim_free(tempname);
2100}
2101
2102/*
2103 * Get the viminfo file name to use.
2104 * If "file" is given and not empty, use it (has already been expanded by
2105 * cmdline functions).
2106 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2107 * expand environment variables.
2108 * Returns an allocated string. NULL when out of memory.
2109 */
2110 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002111viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 if (file == NULL || *file == NUL)
2114 {
2115 if (use_viminfo != NULL)
2116 file = use_viminfo;
2117 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2118 {
2119#ifdef VIMINFO_FILE2
2120 /* don't use $HOME when not defined (turned into "c:/"!). */
2121# ifdef VMS
2122 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2123# else
2124 if (mch_getenv((char_u *)"HOME") == NULL)
2125# endif
2126 {
2127 /* don't use $VIM when not available. */
2128 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2129 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2130 file = (char_u *)VIMINFO_FILE2;
2131 else
2132 file = (char_u *)VIMINFO_FILE;
2133 }
2134 else
2135#endif
2136 file = (char_u *)VIMINFO_FILE;
2137 }
2138 expand_env(file, NameBuff, MAXPATHL);
2139 file = NameBuff;
2140 }
2141 return vim_strsave(file);
2142}
2143
2144/*
2145 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2146 */
2147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002148do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 int eof = FALSE;
2151 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002152 int merge = FALSE;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002153 int do_copy_marks = FALSE;
2154 garray_T buflist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2157 return;
2158 vir.vir_fd = fp_in;
2159#ifdef FEAT_MBYTE
2160 vir.vir_conv.vc_type = CONV_NONE;
2161#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002162 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002163 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 if (fp_in != NULL)
2166 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002167 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002168 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002169 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002170 {
2171 /* Registers and marks are read and kept separate from what
2172 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002173 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002174 prepare_viminfo_marks();
2175 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002176
Bram Moolenaard812df62008-11-09 12:46:09 +00002177 eof = read_viminfo_up_to_marks(&vir,
2178 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002179 merge = TRUE;
2180 }
2181 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 /* Skip info, find start of marks */
2183 while (!(eof = viminfo_readline(&vir))
2184 && vir.vir_line[0] != '>')
2185 ;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002186
2187 do_copy_marks = (flags &
2188 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (fp_out != NULL)
2192 {
2193 /* Write the info: */
2194 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2195 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002196 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002197 write_viminfo_version(fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002199 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2201#endif
2202 write_viminfo_search_pattern(fp_out);
2203 write_viminfo_sub_string(fp_out);
2204#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002205 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#endif
2207 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002208 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209#ifdef FEAT_EVAL
2210 write_viminfo_varlist(fp_out);
2211#endif
2212 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002213 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002215 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002216
2217 if (do_copy_marks)
2218 ga_init2(&buflist, sizeof(buf_T *), 50);
2219 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002221
2222 if (do_copy_marks)
2223 {
2224 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2225 if (fp_out != NULL)
2226 ga_clear(&buflist);
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 vim_free(vir.vir_line);
2230#ifdef FEAT_MBYTE
2231 if (vir.vir_conv.vc_type != CONV_NONE)
2232 convert_setup(&vir.vir_conv, NULL, NULL);
2233#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002234 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235}
2236
2237/*
2238 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2239 * first part of the viminfo file which contains everything but the marks that
2240 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2241 */
2242 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002243read_viminfo_up_to_marks(
2244 vir_T *virp,
2245 int forceit,
2246 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
2248 int eof;
2249 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002250 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
2252#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002253 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 eof = viminfo_readline(virp);
2257 while (!eof && virp->vir_line[0] != '>')
2258 {
2259 switch (virp->vir_line[0])
2260 {
2261 /* Characters reserved for future expansion, ignored now */
2262 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 case '^': /* to be defined */
2264 case '<': /* long line - ignored */
2265 /* A comment or empty line. */
2266 case NUL:
2267 case '\r':
2268 case '\n':
2269 case '#':
2270 eof = viminfo_readline(virp);
2271 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002272 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002273 eof = read_viminfo_barline(virp, got_encoding,
2274 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002277 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 eof = viminfo_encoding(virp);
2279 break;
2280 case '!': /* global variable */
2281#ifdef FEAT_EVAL
2282 eof = read_viminfo_varlist(virp, writing);
2283#else
2284 eof = viminfo_readline(virp);
2285#endif
2286 break;
2287 case '%': /* entry for buffer list */
2288 eof = read_viminfo_bufferlist(virp, writing);
2289 break;
2290 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002291 /* When registers are in bar lines skip the old style register
2292 * lines. */
2293 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2294 eof = read_viminfo_register(virp, forceit);
2295 else
2296 do {
2297 eof = viminfo_readline(virp);
2298 } while (!eof && (virp->vir_line[0] == TAB
2299 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 break;
2301 case '/': /* Search string */
2302 case '&': /* Substitute search string */
2303 case '~': /* Last search string, followed by '/' or '&' */
2304 eof = read_viminfo_search_pattern(virp, forceit);
2305 break;
2306 case '$':
2307 eof = read_viminfo_sub_string(virp, forceit);
2308 break;
2309 case ':':
2310 case '?':
2311 case '=':
2312 case '@':
2313#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002314 /* When history is in bar lines skip the old style history
2315 * lines. */
2316 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2317 eof = read_viminfo_history(virp, writing);
2318 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002320 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 break;
2322 case '-':
2323 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002324 /* When file marks are in bar lines skip the old style lines. */
2325 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2326 eof = read_viminfo_filemark(virp, forceit);
2327 else
2328 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 break;
2330 default:
2331 if (viminfo_error("E575: ", _("Illegal starting char"),
2332 virp->vir_line))
2333 eof = TRUE;
2334 else
2335 eof = viminfo_readline(virp);
2336 break;
2337 }
2338 }
2339
2340#ifdef FEAT_CMDHIST
2341 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002342 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002343 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#endif
2345
2346 /* Change file names to buffer numbers for fmarks. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002347 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 fmarks_check_names(buf);
2349
2350 return eof;
2351}
2352
2353/*
2354 * Compare the 'encoding' value in the viminfo file with the current value of
2355 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2356 * conversion of text with iconv() in viminfo_readstring().
2357 */
2358 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002359viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361#ifdef FEAT_MBYTE
2362 char_u *p;
2363 int i;
2364
2365 if (get_viminfo_parameter('c') != 0)
2366 {
2367 p = vim_strchr(virp->vir_line, '=');
2368 if (p != NULL)
2369 {
2370 /* remove trailing newline */
2371 ++p;
2372 for (i = 0; vim_isprintc(p[i]); ++i)
2373 ;
2374 p[i] = NUL;
2375
2376 convert_setup(&virp->vir_conv, p, p_enc);
2377 }
2378 }
2379#endif
2380 return viminfo_readline(virp);
2381}
2382
2383/*
2384 * Read a line from the viminfo file.
2385 * Returns TRUE for end-of-file;
2386 */
2387 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002388viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2391}
2392
2393/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002394 * Check string read from viminfo file.
2395 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * - replace CTRL-V CTRL-V with CTRL-V
2397 * - replace CTRL-V 'n' with '\n'
2398 *
2399 * Check for a long line as written by viminfo_writestring().
2400 *
2401 * Return the string in allocated memory (NULL when out of memory).
2402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002404viminfo_readstring(
2405 vir_T *virp,
2406 int off, /* offset for virp->vir_line */
2407 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 char_u *retval;
2410 char_u *s, *d;
2411 long len;
2412
2413 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2414 {
2415 len = atol((char *)virp->vir_line + off + 1);
2416 retval = lalloc(len, TRUE);
2417 if (retval == NULL)
2418 {
2419 /* Line too long? File messed up? Skip next line. */
2420 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2421 return NULL;
2422 }
2423 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2424 s = retval + 1; /* Skip the leading '<' */
2425 }
2426 else
2427 {
2428 retval = vim_strsave(virp->vir_line + off);
2429 if (retval == NULL)
2430 return NULL;
2431 s = retval;
2432 }
2433
2434 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2435 d = retval;
2436 while (*s != NUL && *s != '\n')
2437 {
2438 if (s[0] == Ctrl_V && s[1] != NUL)
2439 {
2440 if (s[1] == 'n')
2441 *d++ = '\n';
2442 else
2443 *d++ = Ctrl_V;
2444 s += 2;
2445 }
2446 else
2447 *d++ = *s++;
2448 }
2449 *d = NUL;
2450
2451#ifdef FEAT_MBYTE
2452 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2453 {
2454 d = string_convert(&virp->vir_conv, retval, NULL);
2455 if (d != NULL)
2456 {
2457 vim_free(retval);
2458 retval = d;
2459 }
2460 }
2461#endif
2462
2463 return retval;
2464}
2465
2466/*
2467 * write string to viminfo file
2468 * - replace CTRL-V with CTRL-V CTRL-V
2469 * - replace '\n' with CTRL-V 'n'
2470 * - add a '\n' at the end
2471 *
2472 * For a long line:
2473 * - write " CTRL-V <length> \n " in first line
2474 * - write " < <string> \n " in second line
2475 */
2476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002477viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 int c;
2480 char_u *s;
2481 int len = 0;
2482
2483 for (s = p; *s != NUL; ++s)
2484 {
2485 if (*s == Ctrl_V || *s == '\n')
2486 ++len;
2487 ++len;
2488 }
2489
2490 /* If the string will be too long, write its length and put it in the next
2491 * line. Take into account that some room is needed for what comes before
2492 * the string (e.g., variable name). Add something to the length for the
2493 * '<', NL and trailing NUL. */
2494 if (len > LSIZE / 2)
2495 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2496
2497 while ((c = *p++) != NUL)
2498 {
2499 if (c == Ctrl_V || c == '\n')
2500 {
2501 putc(Ctrl_V, fd);
2502 if (c == '\n')
2503 c = 'n';
2504 }
2505 putc(c, fd);
2506 }
2507 putc('\n', fd);
2508}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002509
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002510/*
2511 * Write a string in quotes that barline_parse() can read back.
2512 * Breaks the line in less than LSIZE pieces when needed.
2513 * Returns remaining characters in the line.
2514 */
2515 int
2516barline_writestring(FILE *fd, char_u *s, int remaining_start)
2517{
2518 char_u *p;
2519 int remaining = remaining_start;
2520 int len = 2;
2521
2522 /* Count the number of characters produced, including quotes. */
2523 for (p = s; *p != NUL; ++p)
2524 {
2525 if (*p == NL)
2526 len += 2;
2527 else if (*p == '"' || *p == '\\')
2528 len += 2;
2529 else
2530 ++len;
2531 }
Bram Moolenaar25709572016-08-26 20:41:16 +02002532 if (len > remaining - 2)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002533 {
2534 fprintf(fd, ">%d\n|<", len);
2535 remaining = LSIZE - 20;
2536 }
2537
2538 putc('"', fd);
2539 for (p = s; *p != NUL; ++p)
2540 {
2541 if (*p == NL)
2542 {
2543 putc('\\', fd);
2544 putc('n', fd);
2545 --remaining;
2546 }
2547 else if (*p == '"' || *p == '\\')
2548 {
2549 putc('\\', fd);
2550 putc(*p, fd);
2551 --remaining;
2552 }
2553 else
2554 putc(*p, fd);
2555 --remaining;
2556
2557 if (remaining < 3)
2558 {
2559 putc('\n', fd);
2560 putc('|', fd);
2561 putc('<', fd);
2562 /* Leave enough space for another continuation. */
2563 remaining = LSIZE - 20;
2564 }
2565 }
2566 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002567 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002568}
2569
2570/*
2571 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002572 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002573 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002574 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002575 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002576barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002577{
2578 char_u *p = text;
2579 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002580 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002581 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002582 int i;
2583 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002584 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002585#ifdef FEAT_MBYTE
2586 char_u *sconv;
2587 int converted;
2588#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002589
2590 while (*p == ',')
2591 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002592 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002593 if (ga_grow(values, 1) == FAIL)
2594 break;
2595 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002596
2597 if (*p == '>')
2598 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002599 /* Need to read a continuation line. Put strings in allocated
2600 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002601 if (!allocated)
2602 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002603 for (i = 0; i < values->ga_len; ++i)
2604 {
2605 bval_T *vp = (bval_T *)(values->ga_data) + i;
2606
2607 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002608 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002609 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2610 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002611 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002612 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002613 allocated = TRUE;
2614 }
2615
2616 if (vim_isdigit(p[1]))
2617 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002618 size_t len;
2619 size_t todo;
2620 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002621
2622 /* String value was split into lines that are each shorter
2623 * than LSIZE:
2624 * |{bartype},>{length of "{text}{text2}"}
2625 * |<"{text1}
2626 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002627 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002628 */
2629 ++p;
2630 len = getdigits(&p);
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002631 buf = alloc((int)(len + 1));
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002632 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002633 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002634 p = buf;
2635 for (todo = len; todo > 0; todo -= n)
2636 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002637 eof = viminfo_readline(virp);
2638 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002639 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002640 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002641 /* File was truncated or garbled. Read another line if
2642 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002643 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002644 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002645 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002646 /* Get length of text, excluding |< and NL chars. */
2647 n = STRLEN(virp->vir_line);
2648 while (n > 0 && (virp->vir_line[n - 1] == NL
2649 || virp->vir_line[n - 1] == CAR))
2650 --n;
2651 n -= 2;
2652 if (n > todo)
2653 {
2654 /* more values follow after the string */
2655 nextp = virp->vir_line + 2 + todo;
2656 n = todo;
2657 }
2658 mch_memmove(p, virp->vir_line + 2, n);
2659 p += n;
2660 }
2661 *p = NUL;
2662 p = buf;
2663 }
2664 else
2665 {
2666 /* Line ending in ">" continues in the next line:
2667 * |{bartype},{lots of values},>
2668 * |<{value},{value}
2669 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002670 eof = viminfo_readline(virp);
2671 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002672 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002673 /* File was truncated or garbled. Read another line if
2674 * this one starts with '|'. */
2675 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002676 p = virp->vir_line + 2;
2677 }
2678 }
2679
2680 if (isdigit(*p))
2681 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002682 value->bv_type = BVAL_NR;
2683 value->bv_nr = getdigits(&p);
2684 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002685 }
2686 else if (*p == '"')
2687 {
2688 int len = 0;
2689 char_u *s = p;
2690
2691 /* Unescape special characters in-place. */
2692 ++p;
2693 while (*p != '"')
2694 {
2695 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002696 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002697 if (*p == '\\')
2698 {
2699 ++p;
2700 if (*p == 'n')
2701 s[len++] = '\n';
2702 else
2703 s[len++] = *p;
2704 ++p;
2705 }
2706 else
2707 s[len++] = *p++;
2708 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002709 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002710 s[len] = NUL;
2711
Bram Moolenaar01227092016-06-11 14:47:40 +02002712#ifdef FEAT_MBYTE
2713 converted = FALSE;
2714 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2715 {
2716 sconv = string_convert(&virp->vir_conv, s, NULL);
2717 if (sconv != NULL)
2718 {
2719 if (s == buf)
2720 vim_free(s);
2721 s = sconv;
2722 buf = s;
2723 converted = TRUE;
2724 }
2725 }
2726#endif
2727 /* Need to copy in allocated memory if the string wasn't allocated
2728 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002729 if (s != buf && allocated)
2730 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002731 value->bv_string = s;
2732 value->bv_type = BVAL_STRING;
2733 value->bv_len = len;
2734 value->bv_allocated = allocated
Bram Moolenaar01227092016-06-11 14:47:40 +02002735#ifdef FEAT_MBYTE
2736 || converted
2737#endif
2738 ;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002739 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002740 if (nextp != NULL)
2741 {
2742 /* values following a long string */
2743 p = nextp;
2744 nextp = NULL;
2745 }
2746 }
2747 else if (*p == ',')
2748 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002749 value->bv_type = BVAL_EMPTY;
2750 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002751 }
2752 else
2753 break;
2754 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002755 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002756}
2757
2758 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002759read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002760{
2761 char_u *p = virp->vir_line + 1;
2762 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002763 garray_T values;
2764 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002765 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002766 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002767
2768 /* The format is: |{bartype},{value},...
2769 * For a very long string:
2770 * |{bartype},>{length of "{text}{text2}"}
2771 * |<{text1}
2772 * |<{text2},{value}
2773 * For a long line not using a string
2774 * |{bartype},{lots of values},>
2775 * |<{value},{value}
2776 */
2777 if (*p == '<')
2778 {
2779 /* Continuation line of an unrecognized item. */
2780 if (writing)
2781 ga_add_string(&virp->vir_barlines, virp->vir_line);
2782 }
2783 else
2784 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002785 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002786 bartype = getdigits(&p);
2787 switch (bartype)
2788 {
2789 case BARTYPE_VERSION:
2790 /* Only use the version when it comes before the encoding.
2791 * If it comes later it was copied by a Vim version that
2792 * doesn't understand the version. */
2793 if (!got_encoding)
2794 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002795 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002796 vp = (bval_T *)values.ga_data;
2797 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2798 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002799 }
2800 break;
2801
2802 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002803 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002804 handle_viminfo_history(&values, writing);
2805 break;
2806
2807 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002808 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002809 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002810 break;
2811
Bram Moolenaar2d358992016-06-12 21:20:54 +02002812 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002813 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002814 handle_viminfo_mark(&values, force);
2815 break;
2816
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002817 default:
2818 /* copy unrecognized line (for future use) */
2819 if (writing)
2820 ga_add_string(&virp->vir_barlines, virp->vir_line);
2821 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002822 for (i = 0; i < values.ga_len; ++i)
2823 {
2824 vp = (bval_T *)values.ga_data + i;
2825 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2826 vim_free(vp->bv_string);
2827 }
2828 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002829 }
2830
Bram Moolenaarecefe712016-06-20 12:50:17 +02002831 if (read_next)
2832 return viminfo_readline(virp);
2833 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002834}
2835
2836 static void
2837write_viminfo_version(FILE *fp_out)
2838{
2839 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2840 BARTYPE_VERSION, VIMINFO_VERSION);
2841}
2842
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002843 static void
2844write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2845{
2846 int i;
2847 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002848 int seen_useful = FALSE;
2849 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002850
2851 if (gap->ga_len > 0)
2852 {
2853 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2854
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002855 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002856 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002857 {
2858 line = ((char **)(gap->ga_data))[i];
2859 if (seen_useful || line[1] != '<')
2860 {
2861 fputs(line, fp_out);
2862 seen_useful = TRUE;
2863 }
2864 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002865 }
2866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif /* FEAT_VIMINFO */
2868
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002869/*
2870 * Return the current time in seconds. Calls time(), unless test_settime()
2871 * was used.
2872 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002873 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002874vim_time(void)
2875{
2876# ifdef FEAT_EVAL
2877 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2878# else
2879 return time(NULL);
2880# endif
2881}
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883/*
2884 * Implementation of ":fixdel", also used by get_stty().
2885 * <BS> resulting <Del>
2886 * ^? ^H
2887 * not ^? ^?
2888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002890do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
2892 char_u *p;
2893
2894 p = find_termcode((char_u *)"kb");
2895 add_termcode((char_u *)"kD", p != NULL
2896 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2897}
2898
2899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002900print_line_no_prefix(
2901 linenr_T lnum,
2902 int use_number,
2903 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002905 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 if (curwin->w_p_nu || use_number)
2908 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002909 vim_snprintf((char *)numbuf, sizeof(numbuf),
2910 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2912 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002913 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914}
2915
2916/*
2917 * Print a text line. Also in silent mode ("ex -s").
2918 */
2919 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002920print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 int save_silent = silent_mode;
2923
Bram Moolenaard29459b2016-08-26 22:29:11 +02002924 /* apply :filter /pat/ */
2925 if (message_filtered(ml_get(lnum)))
2926 return;
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002929 silent_mode = FALSE;
2930 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2931 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (save_silent)
2933 {
2934 msg_putchar('\n');
2935 cursor_on(); /* msg_start() switches it off */
2936 out_flush();
2937 silent_mode = save_silent;
2938 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00002939 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002942 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002943rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002944{
2945 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02002946 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002947
Bram Moolenaar7e283842013-05-30 11:43:15 +02002948#ifdef FEAT_AUTOCMD
2949 buf = curbuf;
2950 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002951 /* buffer changed, don't change name now */
2952 if (buf != curbuf)
2953 return FAIL;
2954# ifdef FEAT_EVAL
2955 if (aborting()) /* autocmds may abort script processing */
2956 return FAIL;
2957# endif
2958#endif
2959 /*
2960 * The name of the current buffer will be changed.
2961 * A new (unlisted) buffer entry needs to be made to hold the old file
2962 * name, which will become the alternate file name.
2963 * But don't set the alternate file name if the buffer didn't have a
2964 * name.
2965 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02002966 fname = curbuf->b_ffname;
2967 sfname = curbuf->b_sfname;
2968 xfname = curbuf->b_fname;
2969 curbuf->b_ffname = NULL;
2970 curbuf->b_sfname = NULL;
2971 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002972 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02002973 curbuf->b_ffname = fname;
2974 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002975 return FAIL;
2976 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02002977 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002978 if (xfname != NULL && *xfname != NUL)
2979 {
2980 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2981 if (buf != NULL && !cmdmod.keepalt)
2982 curwin->w_alt_fnum = buf->b_fnum;
2983 }
2984 vim_free(fname);
2985 vim_free(sfname);
2986#ifdef FEAT_AUTOCMD
Bram Moolenaar7e283842013-05-30 11:43:15 +02002987 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002988#endif
2989 /* Change directories when the 'acd' option is set. */
2990 DO_AUTOCHDIR
2991 return OK;
2992}
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
2995 * ":file[!] [fname]".
2996 */
2997 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002998ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003000 /* ":0file" removes the file name. Check for illegal uses ":3file",
3001 * "0file name", etc. */
3002 if (eap->addr_count > 0
3003 && (*eap->arg != NUL
3004 || eap->line2 > 0
3005 || eap->addr_count > 1))
3006 {
3007 EMSG(_(e_invarg));
3008 return;
3009 }
3010
3011 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003013 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016 /* print full file name if :cd used */
Bram Moolenaar426dd022016-03-15 15:09:29 +01003017 if (!shortmess(SHM_FILEINFO))
3018 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021/*
3022 * ":update".
3023 */
3024 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 if (curbufIsChanged())
3028 (void)do_write(eap);
3029}
3030
3031/*
3032 * ":write" and ":saveas".
3033 */
3034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003035ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 if (eap->usefilter) /* input lines to shell command */
3038 do_bang(1, eap, FALSE, TRUE, FALSE);
3039 else
3040 (void)do_write(eap);
3041}
3042
3043/*
3044 * write current buffer to file 'eap->arg'
3045 * if 'eap->append' is TRUE, append to the file
3046 *
3047 * if *eap->arg == NUL write to current file
3048 *
3049 * return FAIL for failure, OK otherwise
3050 */
3051 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003052do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 int other;
3055 char_u *fname = NULL; /* init to shut up gcc */
3056 char_u *ffname;
3057 int retval = FAIL;
3058 char_u *free_fname = NULL;
3059#ifdef FEAT_BROWSE
3060 char_u *browse_file = NULL;
3061#endif
3062 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003063 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 if (not_writing()) /* check 'write' option */
3066 return FAIL;
3067
3068 ffname = eap->arg;
3069#ifdef FEAT_BROWSE
3070 if (cmdmod.browse)
3071 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003072 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 NULL, NULL, NULL, curbuf);
3074 if (browse_file == NULL)
3075 goto theend;
3076 ffname = browse_file;
3077 }
3078#endif
3079 if (*ffname == NUL)
3080 {
3081 if (eap->cmdidx == CMD_saveas)
3082 {
3083 EMSG(_(e_argreq));
3084 goto theend;
3085 }
3086 other = FALSE;
3087 }
3088 else
3089 {
3090 fname = ffname;
3091 free_fname = fix_fname(ffname);
3092 /*
3093 * When out-of-memory, keep unexpanded file name, because we MUST be
3094 * able to write the file in this situation.
3095 */
3096 if (free_fname != NULL)
3097 ffname = free_fname;
3098 other = otherfile(ffname);
3099 }
3100
3101 /*
3102 * If we have a new file, put its name in the list of alternate file names.
3103 */
3104 if (other)
3105 {
3106 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3107 || eap->cmdidx == CMD_saveas)
3108 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3109 else
3110 alt_buf = buflist_findname(ffname);
3111 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3112 {
3113 /* Overwriting a file that is loaded in another buffer is not a
3114 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00003115 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 goto theend;
3117 }
3118 }
3119
3120 /*
3121 * Writing to the current file is not allowed in readonly mode
3122 * and a file name is required.
3123 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3124 */
3125 if (!other && (
3126#ifdef FEAT_QUICKFIX
3127 bt_dontwrite_msg(curbuf) ||
3128#endif
3129 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3130 goto theend;
3131
3132 if (!other)
3133 {
3134 ffname = curbuf->b_ffname;
3135 fname = curbuf->b_fname;
3136 /*
3137 * Not writing the whole file is only allowed with '!'.
3138 */
3139 if ( (eap->line1 != 1
3140 || eap->line2 != curbuf->b_ml.ml_line_count)
3141 && !eap->forceit
3142 && !eap->append
3143 && !p_wa)
3144 {
3145#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3146 if (p_confirm || cmdmod.confirm)
3147 {
3148 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3149 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3150 goto theend;
3151 eap->forceit = TRUE;
3152 }
3153 else
3154#endif
3155 {
3156 EMSG(_("E140: Use ! to write partial buffer"));
3157 goto theend;
3158 }
3159 }
3160 }
3161
3162 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3163 {
3164 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3165 {
3166#ifdef FEAT_AUTOCMD
3167 buf_T *was_curbuf = curbuf;
3168
3169 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003170 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171# ifdef FEAT_EVAL
3172 if (curbuf != was_curbuf || aborting())
3173# else
3174 if (curbuf != was_curbuf)
3175# endif
3176 {
3177 /* buffer changed, don't change name now */
3178 retval = FAIL;
3179 goto theend;
3180 }
3181#endif
3182 /* Exchange the file names for the current and the alternate
3183 * buffer. This makes it look like we are now editing the buffer
3184 * under the new name. Must be done before buf_write(), because
3185 * if there is no file name and 'cpo' contains 'F', it will set
3186 * the file name. */
3187 fname = alt_buf->b_fname;
3188 alt_buf->b_fname = curbuf->b_fname;
3189 curbuf->b_fname = fname;
3190 fname = alt_buf->b_ffname;
3191 alt_buf->b_ffname = curbuf->b_ffname;
3192 curbuf->b_ffname = fname;
3193 fname = alt_buf->b_sfname;
3194 alt_buf->b_sfname = curbuf->b_sfname;
3195 curbuf->b_sfname = fname;
3196 buf_name_changed(curbuf);
3197#ifdef FEAT_AUTOCMD
3198 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003199 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (!alt_buf->b_p_bl)
3201 {
3202 alt_buf->b_p_bl = TRUE;
3203 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3204 }
3205# ifdef FEAT_EVAL
3206 if (curbuf != was_curbuf || aborting())
3207# else
3208 if (curbuf != was_curbuf)
3209# endif
3210 {
3211 /* buffer changed, don't write the file */
3212 retval = FAIL;
3213 goto theend;
3214 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003215
3216 /* If 'filetype' was empty try detecting it now. */
3217 if (*curbuf->b_p_ft == NUL)
3218 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003219 if (au_has_group((char_u *)"filetypedetect"))
3220 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003221 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003222 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003223 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003224
3225 /* Autocommands may have changed buffer names, esp. when
3226 * 'autochdir' is set. */
3227 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
3229 }
3230
Bram Moolenaar5c719942016-07-09 23:40:45 +02003231 name_was_missing = curbuf->b_ffname == NULL;
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3234 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003235
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003236 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003237 if (eap->cmdidx == CMD_saveas)
3238 {
3239 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003240 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003241 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003242#ifdef FEAT_WINDOWS
3243 redraw_tabline = TRUE;
3244#endif
3245 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003246 }
3247
3248 /* Change directories when the 'acd' option is set and the file name
3249 * got changed or set. */
3250 if (eap->cmdidx == CMD_saveas || name_was_missing)
3251 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003252 DO_AUTOCHDIR
3253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 }
3255
3256theend:
3257#ifdef FEAT_BROWSE
3258 vim_free(browse_file);
3259#endif
3260 vim_free(free_fname);
3261 return retval;
3262}
3263
3264/*
3265 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3266 * BF_NEW or BF_READERR, check for overwriting current file.
3267 * May set eap->forceit if a dialog says it's OK to overwrite.
3268 * Return OK if it's OK, FAIL if it is not.
3269 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003270 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271check_overwrite(
3272 exarg_T *eap,
3273 buf_T *buf,
3274 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003276 char_u *ffname, /* full path version of fname */
3277 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 /*
3280 * write to other file or b_flags set or not writing the whole file:
3281 * overwriting only allowed with '!'
3282 */
3283 if ( (other
3284 || (buf->b_flags & BF_NOTEDITED)
3285 || ((buf->b_flags & BF_NEW)
3286 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3287 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003289#ifdef FEAT_QUICKFIX
3290 && !bt_nofile(buf)
3291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 && vim_fexists(ffname))
3293 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003294 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003296#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003297 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003298 if (mch_isdir(ffname))
3299 {
3300 EMSG2(_(e_isadir2), ffname);
3301 return FAIL;
3302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#endif
3304#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003305 if (p_confirm || cmdmod.confirm)
3306 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003307 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003309 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3310 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3311 return FAIL;
3312 eap->forceit = TRUE;
3313 }
3314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003316 {
3317 EMSG(_(e_exists));
3318 return FAIL;
3319 }
3320 }
3321
3322 /* For ":w! filename" check that no swap file exists for "filename". */
3323 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003325 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003326 char_u *p;
3327 int r;
3328 char_u *swapname;
3329
3330 /* We only try the first entry in 'directory', without checking if
3331 * it's writable. If the "." directory is not writable the write
3332 * will probably fail anyway.
3333 * Use 'shortname' of the current buffer, since there is no buffer
3334 * for the written file. */
3335 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003336 {
3337 dir = alloc(5);
3338 if (dir == NULL)
3339 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003340 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003341 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003342 else
3343 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003344 dir = alloc(MAXPATHL);
3345 if (dir == NULL)
3346 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003347 p = p_dir;
3348 copy_option_part(&p, dir, MAXPATHL, ",");
3349 }
3350 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003351 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003352 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003353 if (r)
3354 {
3355#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3356 if (p_confirm || cmdmod.confirm)
3357 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003358 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003359
3360 dialog_msg(buff,
3361 _("Swap file \"%s\" exists, overwrite anyway?"),
3362 swapname);
3363 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3364 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003365 {
3366 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003367 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003368 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003369 eap->forceit = TRUE;
3370 }
3371 else
3372#endif
3373 {
3374 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
3375 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003376 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003377 return FAIL;
3378 }
3379 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003380 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 }
3383 return OK;
3384}
3385
3386/*
3387 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3388 */
3389 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003390ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 int i;
3393
3394 if (eap->cmd[1] == 'n')
3395 i = curwin->w_arg_idx + (int)eap->line2;
3396 else
3397 i = curwin->w_arg_idx - (int)eap->line2;
3398 eap->line1 = 1;
3399 eap->line2 = curbuf->b_ml.ml_line_count;
3400 if (do_write(eap) != FAIL)
3401 do_argfile(eap, i);
3402}
3403
3404/*
3405 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3406 */
3407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003408do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
3410 buf_T *buf;
3411 int error = 0;
3412 int save_forceit = eap->forceit;
3413
3414 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3415 exiting = TRUE;
3416
Bram Moolenaar29323592016-07-24 22:04:11 +02003417 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 if (bufIsChanged(buf))
3420 {
3421 /*
3422 * Check if there is a reason the buffer cannot be written:
3423 * 1. if the 'write' option is set
3424 * 2. if there is no file name (even after browsing)
3425 * 3. if the 'readonly' is set (even after a dialog)
3426 * 4. if overwriting is allowed (even after a dialog)
3427 */
3428 if (not_writing())
3429 {
3430 ++error;
3431 break;
3432 }
3433#ifdef FEAT_BROWSE
3434 /* ":browse wall": ask for file name if there isn't one */
3435 if (buf->b_ffname == NULL && cmdmod.browse)
3436 browse_save_fname(buf);
3437#endif
3438 if (buf->b_ffname == NULL)
3439 {
3440 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3441 ++error;
3442 }
3443 else if (check_readonly(&eap->forceit, buf)
3444 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3445 FALSE) == FAIL)
3446 {
3447 ++error;
3448 }
3449 else
3450 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003451#ifdef FEAT_AUTOCMD
3452 bufref_T bufref;
3453
3454 set_bufref(&bufref, buf);
3455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (buf_write_all(buf, eap->forceit) == FAIL)
3457 ++error;
3458#ifdef FEAT_AUTOCMD
3459 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003460 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 buf = firstbuf;
3462#endif
3463 }
3464 eap->forceit = save_forceit; /* check_overwrite() may set it */
3465 }
3466 }
3467 if (exiting)
3468 {
3469 if (!error)
3470 getout(0); /* exit Vim */
3471 not_exiting();
3472 }
3473}
3474
3475/*
3476 * Check the 'write' option.
3477 * Return TRUE and give a message when it's not st.
3478 */
3479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003480not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482 if (p_write)
3483 return FALSE;
3484 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3485 return TRUE;
3486}
3487
3488/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003489 * Check if a buffer is read-only (either 'readonly' option is set or file is
3490 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3491 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
3493 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003494check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003496 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003497
3498 /* Handle a file being readonly when the 'readonly' option is set or when
3499 * the file exists and permissions are read-only.
3500 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3501 * important for device checks but not here. */
3502 if (!*forceit && (buf->b_p_ro
3503 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3504 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3507 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3508 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003509 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar5386a122007-06-28 20:02:32 +00003511 if (buf->b_p_ro)
3512 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3513 buf->b_fname);
3514 else
3515 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 +00003516 buf->b_fname);
3517
3518 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3519 {
3520 /* Set forceit, to force the writing of a readonly file */
3521 *forceit = TRUE;
3522 return FALSE;
3523 }
3524 else
3525 return TRUE;
3526 }
3527 else
3528#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003529 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003531 else
3532 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3533 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return TRUE;
3535 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 return FALSE;
3538}
3539
3540/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003541 * Try to abandon current file and edit a new or existing file.
3542 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003544 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003545 * -1 for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * 'lnum' is the line number for the cursor in the new file (if non-zero).
3547 */
3548 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003549getfile(
3550 int fnum,
3551 char_u *ffname,
3552 char_u *sfname,
3553 int setpm,
3554 linenr_T lnum,
3555 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
3557 int other;
3558 int retval;
3559 char_u *free_me = NULL;
3560
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003561 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003563#ifdef FEAT_AUTOCMD
3564 if (curbuf_locked())
3565 return 1;
3566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 if (fnum == 0)
3569 {
3570 /* make ffname full path, set sfname */
3571 fname_expand(curbuf, &ffname, &sfname);
3572 other = otherfile(ffname);
3573 free_me = ffname; /* has been allocated, free() later */
3574 }
3575 else
3576 other = (fnum != curbuf->b_fnum);
3577
3578 if (other)
3579 ++no_wait_return; /* don't wait for autowrite message */
3580 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
3581 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3582 {
3583#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3584 if (p_confirm && p_write)
3585 dialog_changed(curbuf, FALSE);
3586 if (curbufIsChanged())
3587#endif
3588 {
3589 if (other)
3590 --no_wait_return;
3591 EMSG(_(e_nowrtmsg));
3592 retval = 2; /* file has been changed */
3593 goto theend;
3594 }
3595 }
3596 if (other)
3597 --no_wait_return;
3598 if (setpm)
3599 setpcmark();
3600 if (!other)
3601 {
3602 if (lnum != 0)
3603 curwin->w_cursor.lnum = lnum;
3604 check_cursor_lnum();
3605 beginline(BL_SOL | BL_FIX);
3606 retval = 0; /* it's in the same file */
3607 }
3608 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003609 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
3610 curwin) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 retval = -1; /* opened another file */
3612 else
3613 retval = 1; /* error encountered */
3614
3615theend:
3616 vim_free(free_me);
3617 return retval;
3618}
3619
3620/*
3621 * start editing a new file
3622 *
3623 * fnum: file number; if zero use ffname/sfname
3624 * ffname: the file name
3625 * - full path if sfname used,
3626 * - any file name if sfname is NULL
3627 * - empty string to re-edit with the same file name (but may be
3628 * in a different directory)
3629 * - NULL to start an empty buffer
3630 * sfname: the short file name (or NULL)
3631 * eap: contains the command to be executed after loading the file and
3632 * forced 'ff' and 'fenc'
3633 * newlnum: if > 0: put cursor on this line number (if possible)
3634 * if ECMD_LASTL: use last position in loaded file
3635 * if ECMD_LAST: use last position in all files
3636 * if ECMD_ONE: use first line
3637 * flags:
3638 * ECMD_HIDE: if TRUE don't free the current buffer
3639 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3640 * ECMD_OLDBUF: use existing buffer if it exists
3641 * ECMD_FORCEIT: ! used for Ex command
3642 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003643 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003644 * window, NULL when splitting the window first. When not NULL info
3645 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 *
3647 * return FAIL for failure, OK otherwise
3648 */
3649 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003650do_ecmd(
3651 int fnum,
3652 char_u *ffname,
3653 char_u *sfname,
3654 exarg_T *eap, /* can be NULL! */
3655 linenr_T newlnum,
3656 int flags,
3657 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 int other_file; /* TRUE if editing another file */
3660 int oldbuf; /* TRUE if using existing buffer */
3661#ifdef FEAT_AUTOCMD
3662 int auto_buf = FALSE; /* TRUE if autocommands brought us
3663 into the buffer unexpectedly */
3664 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003665 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666#endif
3667 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003668 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003670 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671#endif
3672 char_u *free_fname = NULL;
3673#ifdef FEAT_BROWSE
3674 char_u *browse_file = NULL;
3675#endif
3676 int retval = FAIL;
3677 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003678 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 linenr_T topline = 0;
3680 int newcol = -1;
3681 int solcol = -1;
3682 pos_T *pos;
3683#ifdef FEAT_SUN_WORKSHOP
3684 char_u *cp;
3685#endif
3686 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003687#ifdef FEAT_SPELL
3688 int did_get_winopts = FALSE;
3689#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003690 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003691 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693 if (eap != NULL)
3694 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003695#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3696 set_bufref(&old_curbuf, curbuf);
3697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
3699 if (fnum != 0)
3700 {
3701 if (fnum == curbuf->b_fnum) /* file is already being edited */
3702 return OK; /* nothing to do */
3703 other_file = TRUE;
3704 }
3705 else
3706 {
3707#ifdef FEAT_BROWSE
3708 if (cmdmod.browse)
3709 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003710# ifdef FEAT_AUTOCMD
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003711 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003712# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003713 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003714# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003715 au_has_group((char_u *)"FileExplorer"))
3716 {
3717 /* No browsing supported but we do have the file explorer:
3718 * Edit the directory. */
3719 if (ffname == NULL || !mch_isdir(ffname))
3720 ffname = (char_u *)".";
3721 }
3722 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003723# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003724 {
3725 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003727 if (browse_file == NULL)
3728 goto theend;
3729 ffname = browse_file;
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732#endif
3733 /* if no short name given, use ffname for short name */
3734 if (sfname == NULL)
3735 sfname = ffname;
3736#ifdef USE_FNAME_CASE
3737# ifdef USE_LONG_FNAME
3738 if (USE_LONG_FNAME)
3739# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003740 if (sfname != NULL)
3741 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#endif
3743
3744#ifdef FEAT_LISTCMDS
3745 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3746 goto theend;
3747#endif
3748
3749 if (ffname == NULL)
3750 other_file = TRUE;
3751 /* there is no file name */
3752 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3753 other_file = FALSE;
3754 else
3755 {
3756 if (*ffname == NUL) /* re-edit with same file name */
3757 {
3758 ffname = curbuf->b_ffname;
3759 sfname = curbuf->b_fname;
3760 }
3761 free_fname = fix_fname(ffname); /* may expand to full path name */
3762 if (free_fname != NULL)
3763 ffname = free_fname;
3764 other_file = otherfile(ffname);
3765#ifdef FEAT_SUN_WORKSHOP
3766 if (usingSunWorkShop && p_acd
3767 && (cp = vim_strrchr(sfname, '/')) != NULL)
3768 sfname = ++cp;
3769#endif
3770 }
3771 }
3772
3773 /*
3774 * if the file was changed we may not be allowed to abandon it
3775 * - if we are going to re-edit the same file
3776 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3777 */
3778 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3779 || (curbuf->b_nwindows == 1
3780 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003781 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3782 | (other_file ? 0 : CCGD_MULTWIN)
3783 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3784 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
3786 if (fnum == 0 && other_file && ffname != NULL)
3787 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3788 goto theend;
3789 }
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 /*
3792 * End Visual mode before switching to another buffer, so the text can be
3793 * copied into the GUI selection buffer.
3794 */
3795 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003797#ifdef FEAT_AUTOCMD
3798 if ((command != NULL || newlnum > (linenr_T)0)
3799 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3800 {
3801 int len;
3802 char_u *p;
3803
3804 /* Set v:swapcommand for the SwapExists autocommands. */
3805 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003806 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003807 else
3808 len = 30;
3809 p = alloc((unsigned)len);
3810 if (p != NULL)
3811 {
3812 if (command != NULL)
3813 vim_snprintf((char *)p, len, ":%s\r", command);
3814 else
3815 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3816 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3817 did_set_swapcommand = TRUE;
3818 vim_free(p);
3819 }
3820 }
3821#endif
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 /*
3824 * If we are starting to edit another file, open a (new) buffer.
3825 * Otherwise we re-use the current buffer.
3826 */
3827 if (other_file)
3828 {
3829#ifdef FEAT_LISTCMDS
3830 if (!(flags & ECMD_ADDBUF))
3831#endif
3832 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003833 if (!cmdmod.keepalt)
3834 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003835 if (oldwin != NULL)
3836 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838
3839 if (fnum)
3840 buf = buflist_findnr(fnum);
3841 else
3842 {
3843#ifdef FEAT_LISTCMDS
3844 if (flags & ECMD_ADDBUF)
3845 {
3846 linenr_T tlnum = 1L;
3847
3848 if (command != NULL)
3849 {
3850 tlnum = atol((char *)command);
3851 if (tlnum <= 0)
3852 tlnum = 1L;
3853 }
3854 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3855 goto theend;
3856 }
3857#endif
3858 buf = buflist_new(ffname, sfname, 0L,
3859 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003860#ifdef FEAT_AUTOCMD
3861 /* autocommands may change curwin and curbuf */
3862 if (oldwin != NULL)
3863 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003864 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
3867 if (buf == NULL)
3868 goto theend;
3869 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3870 {
3871 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873 else /* existing memfile */
3874 {
3875 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003876 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003878 /* Check if autocommands made the buffer invalid or changed the
3879 * current buffer. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003880 if (!bufref_valid(&bufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003882 || curbuf != old_curbuf.br_buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883#endif
3884 )
3885 goto theend;
3886#ifdef FEAT_EVAL
3887 if (aborting()) /* autocmds may abort script processing */
3888 goto theend;
3889#endif
3890 }
3891
3892 /* May jump to last used line number for a loaded buffer or when asked
3893 * for explicitly */
3894 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3895 {
3896 pos = buflist_findfpos(buf);
3897 newlnum = pos->lnum;
3898 solcol = pos->col;
3899 }
3900
3901 /*
3902 * Make the (new) buffer the one used by the current window.
3903 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3904 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003905 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 */
3907 if (buf != curbuf)
3908 {
3909#ifdef FEAT_AUTOCMD
3910 /*
3911 * Be careful: The autocommands may delete any buffer and change
3912 * the current buffer.
3913 * - If the buffer we are going to edit is deleted, give up.
3914 * - If the current buffer is deleted, prefer to load the new
3915 * buffer when loading a buffer is required. This avoids
3916 * loading another buffer which then must be closed again.
3917 * - If we ended up in the new buffer already, need to skip a few
3918 * things, set auto_buf.
3919 */
3920 if (buf->b_fname != NULL)
3921 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003922 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003924 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003926 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 delbuf_msg(new_name); /* frees new_name */
3928 goto theend;
3929 }
3930# ifdef FEAT_EVAL
3931 if (aborting()) /* autocmds may abort script processing */
3932 {
3933 vim_free(new_name);
3934 goto theend;
3935 }
3936# endif
3937 if (buf == curbuf) /* already in new buffer */
3938 auto_buf = TRUE;
3939 else
3940 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02003941 win_T *the_curwin = curwin;
3942
3943 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003944 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003945 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003946 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02003947
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003948 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#endif
3950 buf_copy_options(buf, BCO_ENTER);
3951
Bram Moolenaar5a497892016-09-03 16:29:04 +02003952 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02003953 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003954 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003955 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003956 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
3958#ifdef FEAT_AUTOCMD
Bram Moolenaar5a497892016-09-03 16:29:04 +02003959 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003960 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01003961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962# ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02003963 /* autocmds may abort script processing */
3964 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
3966 vim_free(new_name);
3967 goto theend;
3968 }
3969# endif
3970 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003971 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003973 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 delbuf_msg(new_name); /* frees new_name */
3975 goto theend;
3976 }
3977 if (buf == curbuf) /* already in new buffer */
3978 auto_buf = TRUE;
3979 else
3980#endif
3981 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003982#ifdef FEAT_SYN_HL
3983 /*
3984 * <VN> We could instead free the synblock
3985 * and re-attach to buffer, perhaps.
3986 */
Bram Moolenaarc4bfeda2016-12-14 21:42:00 +01003987 if (curwin->w_buffer != NULL
3988 && curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02003989 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 curwin->w_buffer = buf;
3992 curbuf = buf;
3993 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003994
3995 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
3996 if (!oldbuf && eap != NULL)
3997 {
3998 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003999#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004000 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004001#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004
4005 /* May get the window options from the last time this buffer
4006 * was in this window (or another window). If not used
4007 * before, reset the local window options to the global
4008 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004009 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004010#ifdef FEAT_SPELL
4011 did_get_winopts = TRUE;
4012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014#ifdef FEAT_AUTOCMD
4015 }
4016 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004017 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004018 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#endif
4020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 curwin->w_pcmark.lnum = 1;
4023 curwin->w_pcmark.col = 0;
4024 }
4025 else /* !other_file */
4026 {
4027 if (
4028#ifdef FEAT_LISTCMDS
4029 (flags & ECMD_ADDBUF) ||
4030#endif
4031 check_fname() == FAIL)
4032 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 oldbuf = (flags & ECMD_OLDBUF);
4035 }
4036
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004037 /* Don't redraw until the cursor is in the right line, otherwise
4038 * autocommands may cause ml_get errors. */
4039 ++RedrawingDisabled;
4040 did_inc_redrawing_disabled = TRUE;
4041
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004042#ifdef FEAT_AUTOCMD
4043 buf = curbuf;
4044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4046 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004047 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 else
4050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* Don't make a buffer listed if it's a help buffer. Useful when
4052 * using CTRL-O to go back to a help file. */
4053 if (!curbuf->b_help)
4054 set_buflisted(TRUE);
4055 }
4056
4057#ifdef FEAT_AUTOCMD
4058 /* If autocommands change buffers under our fingers, forget about
4059 * editing the file. */
4060 if (buf != curbuf)
4061 goto theend;
4062# ifdef FEAT_EVAL
4063 if (aborting()) /* autocmds may abort script processing */
4064 goto theend;
4065# endif
4066
4067 /* Since we are starting to edit a file, consider the filetype to be
4068 * unset. Helps for when an autocommand changes files and expects syntax
4069 * highlighting to work in the other file. */
4070 did_filetype = FALSE;
4071#endif
4072
4073/*
4074 * other_file oldbuf
4075 * FALSE FALSE re-edit same file, buffer is re-used
4076 * FALSE TRUE re-edit same file, nothing changes
4077 * TRUE FALSE start editing new file, new buffer
4078 * TRUE TRUE start editing in existing buffer (nothing to do)
4079 */
4080 if (!other_file && !oldbuf) /* re-use the buffer */
4081 {
4082 set_last_cursor(curwin); /* may set b_last_cursor */
4083 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4084 {
4085 newlnum = curwin->w_cursor.lnum;
4086 solcol = curwin->w_cursor.col;
4087 }
4088#ifdef FEAT_AUTOCMD
4089 buf = curbuf;
4090 if (buf->b_fname != NULL)
4091 new_name = vim_strsave(buf->b_fname);
4092 else
4093 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004094 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004096 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4097 {
4098 /* Save all the text, so that the reload can be undone.
4099 * Sync first so that this is a separate undo-able action. */
4100 u_sync(FALSE);
4101 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4102 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004103 {
Bram Moolenaarfc1f2012016-07-30 23:18:47 +02004104#ifdef FEAT_AUTOCMD
Bram Moolenaar1e225822016-07-30 21:48:59 +02004105 vim_free(new_name);
Bram Moolenaarfc1f2012016-07-30 23:18:47 +02004106#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004107 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004108 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004109 u_unchanged(curbuf);
4110 buf_freeall(curbuf, BFA_KEEP_UNDO);
4111
4112 /* tell readfile() not to clear or reload undo info */
4113 readfile_flags = READ_KEEP_UNDO;
4114 }
4115 else
4116 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#ifdef FEAT_AUTOCMD
4118 /* If autocommands deleted the buffer we were going to re-edit, give
4119 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004120 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 delbuf_msg(new_name); /* frees new_name */
4123 goto theend;
4124 }
4125 vim_free(new_name);
4126
4127 /* If autocommands change buffers under our fingers, forget about
4128 * re-editing the file. Should do the buf_clear_file(), but perhaps
4129 * the autocommands changed the buffer... */
4130 if (buf != curbuf)
4131 goto theend;
4132# ifdef FEAT_EVAL
4133 if (aborting()) /* autocmds may abort script processing */
4134 goto theend;
4135# endif
4136#endif
4137 buf_clear_file(curbuf);
4138 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4139 curbuf->b_op_end.lnum = 0;
4140 }
4141
4142/*
4143 * If we get here we are sure to start editing
4144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 /* Assume success now */
4146 retval = OK;
4147
4148 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * Check if we are editing the w_arg_idx file in the argument list.
4150 */
4151 check_arg_idx(curwin);
4152
4153#ifdef FEAT_AUTOCMD
4154 if (!auto_buf)
4155#endif
4156 {
4157 /*
4158 * Set cursor and init window before reading the file and executing
4159 * autocommands. This allows for the autocommands to position the
4160 * cursor.
4161 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004162 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004165 /* It's possible that all lines in the buffer changed. Need to update
4166 * automatic folding for all windows where it's used. */
4167# ifdef FEAT_WINDOWS
4168 {
4169 win_T *win;
4170 tabpage_T *tp;
4171
4172 FOR_ALL_TAB_WINDOWS(tp, win)
4173 if (win->w_buffer == curbuf)
4174 foldUpdateAll(win);
4175 }
4176# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 foldUpdateAll(curwin);
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#endif
4180
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004181 /* Change directories when the 'acd' option is set. */
4182 DO_AUTOCHDIR
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /*
4185 * Careful: open_buffer() and apply_autocmds() may change the current
4186 * buffer and window.
4187 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004188 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 topline = curwin->w_topline;
4190 if (!oldbuf) /* need to read the file */
4191 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004192#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 swap_exists_action = SEA_DIALOG;
4194#endif
4195 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4196
4197 /*
4198 * Open the buffer and read the file.
4199 */
4200#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004201 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 retval = FAIL;
4203#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004204 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#endif
4206
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004207#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (swap_exists_action == SEA_QUIT)
4209 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004210 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#endif
4212 }
4213#ifdef FEAT_AUTOCMD
4214 else
4215 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004216 /* Read the modelines, but only to set window-local options. Any
4217 * buffer-local options have already been set and may have been
4218 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004219 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4222 &retval);
4223 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4224 &retval);
4225 }
4226 check_arg_idx(curwin);
4227#endif
4228
Bram Moolenaareab316b2015-03-24 11:46:30 +01004229 /* If autocommands change the cursor position or topline, we should
4230 * keep it. Also when it moves within a line. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004231 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 newlnum = curwin->w_cursor.lnum;
4234 newcol = curwin->w_cursor.col;
4235 }
4236 if (curwin->w_topline == topline)
4237 topline = 0;
4238
4239 /* Even when cursor didn't move we need to recompute topline. */
4240 changed_line_abv_curs();
4241
4242#ifdef FEAT_TITLE
4243 maketitle();
4244#endif
4245 }
4246
4247#ifdef FEAT_DIFF
4248 /* Tell the diff stuff that this buffer is new and/or needs updating.
4249 * Also needed when re-editing the same buffer, because unloading will
4250 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004251 if (curwin->w_p_diff)
4252 {
4253 diff_buf_add(curbuf);
4254 diff_invalidate(curbuf);
4255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif
4257
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004258#ifdef FEAT_SPELL
4259 /* If the window options were changed may need to set the spell language.
4260 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004261 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4262 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004263#endif
4264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (command == NULL)
4266 {
4267 if (newcol >= 0) /* position set by autocommands */
4268 {
4269 curwin->w_cursor.lnum = newlnum;
4270 curwin->w_cursor.col = newcol;
4271 check_cursor();
4272 }
4273 else if (newlnum > 0) /* line number from caller or old position */
4274 {
4275 curwin->w_cursor.lnum = newlnum;
4276 check_cursor_lnum();
4277 if (solcol >= 0 && !p_sol)
4278 {
4279 /* 'sol' is off: Use last known column. */
4280 curwin->w_cursor.col = solcol;
4281 check_cursor_col();
4282#ifdef FEAT_VIRTUALEDIT
4283 curwin->w_cursor.coladd = 0;
4284#endif
4285 curwin->w_set_curswant = TRUE;
4286 }
4287 else
4288 beginline(BL_SOL | BL_FIX);
4289 }
4290 else /* no line number, go to last line in Ex mode */
4291 {
4292 if (exmode_active)
4293 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4294 beginline(BL_WHITE | BL_FIX);
4295 }
4296 }
4297
4298#ifdef FEAT_WINDOWS
4299 /* Check if cursors in other windows on the same buffer are still valid */
4300 check_lnums(FALSE);
4301#endif
4302
4303 /*
4304 * Did not read the file, need to show some info about the file.
4305 * Do this after setting the cursor.
4306 */
4307 if (oldbuf
4308#ifdef FEAT_AUTOCMD
4309 && !auto_buf
4310#endif
4311 )
4312 {
4313 int msg_scroll_save = msg_scroll;
4314
4315 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4316 * message. */
4317 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4318 msg_scroll = FALSE;
4319 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4320 check_for_delay(FALSE);
4321 msg_start();
4322 msg_scroll = msg_scroll_save;
4323 msg_scrolled_ign = TRUE;
4324
Bram Moolenaar426dd022016-03-15 15:09:29 +01004325 if (!shortmess(SHM_FILEINFO))
4326 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 msg_scrolled_ign = FALSE;
4329 }
4330
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004331#ifdef FEAT_VIMINFO
4332 curbuf->b_last_used = vim_time();
4333#endif
4334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (command != NULL)
4336 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4337
4338#ifdef FEAT_KEYMAP
4339 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004340 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif
4342
4343 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004344 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (!skip_redraw)
4346 {
4347 n = p_so;
4348 if (topline == 0 && command == NULL)
4349 p_so = 999; /* force cursor halfway the window */
4350 update_topline();
4351#ifdef FEAT_SCROLLBIND
4352 curwin->w_scbind_pos = curwin->w_topline;
4353#endif
4354 p_so = n;
4355 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4356 }
4357
4358 if (p_im)
4359 need_start_insertmode = TRUE;
4360
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004361 /* Change directories when the 'acd' option is set. */
4362 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00004364#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004365 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
4367# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02004368 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
4370# endif
4371# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004372 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004373 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374# endif
4375 }
4376#endif
4377
4378theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004379 if (did_inc_redrawing_disabled)
4380 --RedrawingDisabled;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004381#ifdef FEAT_AUTOCMD
4382 if (did_set_swapcommand)
4383 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#ifdef FEAT_BROWSE
4386 vim_free(browse_file);
4387#endif
4388 vim_free(free_fname);
4389 return retval;
4390}
4391
4392#ifdef FEAT_AUTOCMD
4393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4397 name == NULL ? (char_u *)"" : name);
4398 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004399 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004400 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401}
4402#endif
4403
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004404static int append_indent = 0; /* autoindent for first line */
4405
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406/*
4407 * ":insert" and ":append", also used by ":change"
4408 */
4409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004410ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 char_u *theline;
4413 int did_undo = FALSE;
4414 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004415 int indent = 0;
4416 char_u *p;
4417 int vcol;
4418 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004420 /* the ! flag toggles autoindent */
4421 if (eap->forceit)
4422 curbuf->b_p_ai = !curbuf->b_p_ai;
4423
4424 /* First autoindent comes from the line we start on */
4425 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4426 append_indent = get_indent_lnum(lnum);
4427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (eap->cmdidx != CMD_append)
4429 --lnum;
4430
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004431 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004432 if (empty && lnum == 1)
4433 lnum = 0;
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 State = INSERT; /* behave like in Insert mode */
4436 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4437 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004438
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004439 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 msg_scroll = TRUE;
4442 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004443 if (curbuf->b_p_ai)
4444 {
4445 if (append_indent >= 0)
4446 {
4447 indent = append_indent;
4448 append_indent = -1;
4449 }
4450 else if (lnum > 0)
4451 indent = get_indent_lnum(lnum);
4452 }
4453 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004455 {
4456 /* No getline() function, use the lines that follow. This ends
4457 * when there is no more. */
4458 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4459 break;
4460 p = vim_strchr(eap->nextcmd, NL);
4461 if (p == NULL)
4462 p = eap->nextcmd + STRLEN(eap->nextcmd);
4463 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4464 if (*p != NUL)
4465 ++p;
4466 eap->nextcmd = p;
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004469 {
4470 int save_State = State;
4471
4472 /* Set State to avoid the cursor shape to be set to INSERT mode
4473 * when getline() returns. */
4474 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 theline = eap->getline(
4476#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004477 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004479 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004480 State = save_State;
4481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004483 if (theline == NULL)
4484 break;
4485
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004486 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4487 if (ex_keep_indent)
4488 append_indent = indent;
4489
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004490 /* Look for the "." after automatic indent. */
4491 vcol = 0;
4492 for (p = theline; indent > vcol; ++p)
4493 {
4494 if (*p == ' ')
4495 ++vcol;
4496 else if (*p == TAB)
4497 vcol += 8 - vcol % 8;
4498 else
4499 break;
4500 }
4501 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004502 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4503 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 vim_free(theline);
4506 break;
4507 }
4508
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004509 /* don't use autoindent if nothing was typed. */
4510 if (p[0] == NUL)
4511 theline[0] = NUL;
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 did_undo = TRUE;
4514 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004515 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517 vim_free(theline);
4518 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004519
4520 if (empty)
4521 {
4522 ml_delete(2L, FALSE);
4523 empty = FALSE;
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 State = NORMAL;
4527
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004528 if (eap->forceit)
4529 curbuf->b_p_ai = !curbuf->b_p_ai;
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004532 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 * "end" is set to lnum when something has been appended, otherwise
4534 * it is the same than "start" -- Acevedo */
4535 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4536 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4537 if (eap->cmdidx != CMD_append)
4538 --curbuf->b_op_start.lnum;
4539 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4540 ? lnum : curbuf->b_op_start.lnum;
4541 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4542 curwin->w_cursor.lnum = lnum;
4543 check_cursor_lnum();
4544 beginline(BL_SOL | BL_FIX);
4545
4546 need_wait_return = FALSE; /* don't use wait_return() now */
4547 ex_no_reprint = TRUE;
4548}
4549
4550/*
4551 * ":change"
4552 */
4553 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004554ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 linenr_T lnum;
4557
4558 if (eap->line2 >= eap->line1
4559 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4560 return;
4561
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004562 /* the ! flag toggles autoindent */
4563 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4564 append_indent = get_indent_lnum(eap->line1);
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4567 {
4568 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4569 break;
4570 ml_delete(eap->line1, FALSE);
4571 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004572
4573 /* make sure the cursor is not beyond the end of the file now */
4574 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4576
4577 /* ":append" on the line above the deleted lines. */
4578 eap->line2 = eap->line1;
4579 ex_append(eap);
4580}
4581
4582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004583ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584{
4585 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004586 int bigness;
4587 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 int minus = 0;
4589 linenr_T start, end, curs, i;
4590 int j;
4591 linenr_T lnum = eap->line2;
4592
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004593 /* Vi compatible: ":z!" uses display height, without a count uses
4594 * 'scroll' */
4595 if (eap->forceit)
4596 bigness = curwin->w_height;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004597#ifdef FEAT_WINDOWS
Bram Moolenaar459ca562016-11-10 18:16:33 +01004598 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004599 bigness = curwin->w_height - 3;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004600#endif
Bram Moolenaar631abc32014-02-22 22:27:47 +01004601 else
4602 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (bigness < 1)
4604 bigness = 1;
4605
4606 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004607 kind = x;
4608 if (*kind == '-' || *kind == '+' || *kind == '='
4609 || *kind == '^' || *kind == '.')
4610 ++x;
4611 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 ++x;
4613
4614 if (*x != 0)
4615 {
4616 if (!VIM_ISDIGIT(*x))
4617 {
4618 EMSG(_("E144: non-numeric argument to :z"));
4619 return;
4620 }
4621 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004624 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004625 if (*kind == '=')
4626 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004630 /* the number of '-' and '+' multiplies the distance */
4631 if (*kind == '-' || *kind == '+')
4632 for (x = kind + 1; *x == *kind; ++x)
4633 ;
4634
4635 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004638 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4639 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004640 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
4642
4643 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004644 start = lnum - (bigness + 1) / 2 + 1;
4645 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 curs = lnum;
4647 minus = 1;
4648 break;
4649
4650 case '^':
4651 start = lnum - bigness * 2;
4652 end = lnum - bigness;
4653 curs = lnum - bigness;
4654 break;
4655
4656 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004657 start = lnum - (bigness + 1) / 2 + 1;
4658 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 curs = end;
4660 break;
4661
4662 default: /* '+' */
4663 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004664 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004665 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004666 else if (eap->addr_count == 0)
4667 ++start;
4668 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 curs = end;
4670 break;
4671 }
4672
4673 if (start < 1)
4674 start = 1;
4675
4676 if (end > curbuf->b_ml.ml_line_count)
4677 end = curbuf->b_ml.ml_line_count;
4678
4679 if (curs > curbuf->b_ml.ml_line_count)
4680 curs = curbuf->b_ml.ml_line_count;
4681
4682 for (i = start; i <= end; i++)
4683 {
4684 if (minus && i == lnum)
4685 {
4686 msg_putchar('\n');
4687
4688 for (j = 1; j < Columns; j++)
4689 msg_putchar('-');
4690 }
4691
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004692 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
4694 if (minus && i == lnum)
4695 {
4696 msg_putchar('\n');
4697
4698 for (j = 1; j < Columns; j++)
4699 msg_putchar('-');
4700 }
4701 }
4702
4703 curwin->w_cursor.lnum = curs;
4704 ex_no_reprint = TRUE;
4705}
4706
4707/*
4708 * Check if the restricted flag is set.
4709 * If so, give an error message and return TRUE.
4710 * Otherwise, return FALSE.
4711 */
4712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 if (restricted)
4716 {
4717 EMSG(_("E145: Shell commands not allowed in rvim"));
4718 return TRUE;
4719 }
4720 return FALSE;
4721}
4722
4723/*
4724 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4725 * If so, give an error message and return TRUE.
4726 * Otherwise, return FALSE.
4727 */
4728 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004729check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 if (secure)
4732 {
4733 secure = 2;
4734 EMSG(_(e_curdir));
4735 return TRUE;
4736 }
4737#ifdef HAVE_SANDBOX
4738 /*
4739 * In the sandbox more things are not allowed, including the things
4740 * disallowed in secure mode.
4741 */
4742 if (sandbox != 0)
4743 {
4744 EMSG(_(e_sandbox));
4745 return TRUE;
4746 }
4747#endif
4748 return FALSE;
4749}
4750
4751static char_u *old_sub = NULL; /* previous substitute pattern */
4752static int global_need_beginline; /* call beginline() after ":g" */
4753
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004754/*
4755 * Flags that are kept between calls to :substitute.
4756 */
4757typedef struct {
4758 int do_all; /* do multiple substitutions per line */
4759 int do_ask; /* ask for confirmation */
4760 int do_count; /* count only */
4761 int do_error; /* if false, ignore errors */
4762 int do_print; /* print last line with subs. */
4763 int do_list; /* list last line with subs. */
4764 int do_number; /* list last line with line nr*/
4765 int do_ic; /* ignore case flag */
4766} subflags_T;
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768/* do_sub()
4769 *
4770 * Perform a substitution from line eap->line1 to line eap->line2 using the
4771 * command pointed to by eap->arg which should be of the form:
4772 *
4773 * /pattern/substitution/{flags}
4774 *
4775 * The usual escapes are supported as described in the regexp docs.
4776 */
4777 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004778do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 linenr_T lnum;
4781 long i = 0;
4782 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004783 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4784 FALSE, FALSE, 0};
4785#ifdef FEAT_EVAL
4786 subflags_T subflags_save;
4787#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004788 int save_do_all; /* remember user specified 'g' flag */
4789 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4791 int delimiter;
4792 int sublen;
4793 int got_quit = FALSE;
4794 int got_match = FALSE;
4795 int temp;
4796 int which_pat;
4797 char_u *cmd;
4798 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004799 linenr_T first_line = 0; /* first changed line */
4800 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 * change */
4802 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4803 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004804 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004805 char_u *sub_firstline; /* allocated copy of first sub line */
4806 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004807 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004808 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004809#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004810 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 cmd = eap->arg;
4814 if (!global_busy)
4815 {
4816 sub_nsubs = 0;
4817 sub_nlines = 0;
4818 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004819 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (eap->cmdidx == CMD_tilde)
4822 which_pat = RE_LAST; /* use last used regexp */
4823 else
4824 which_pat = RE_SUBST; /* use last substitute regexp */
4825
4826 /* new pattern and substitution */
4827 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4828 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4829 {
4830 /* don't accept alphanumeric for separator */
4831 if (isalpha(*cmd))
4832 {
4833 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4834 return;
4835 }
4836 /*
4837 * undocumented vi feature:
4838 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4839 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4840 */
4841 if (*cmd == '\\')
4842 {
4843 ++cmd;
4844 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4845 {
4846 EMSG(_(e_backslash));
4847 return;
4848 }
4849 if (*cmd != '&')
4850 which_pat = RE_SEARCH; /* use last '/' pattern */
4851 pat = (char_u *)""; /* empty search pattern */
4852 delimiter = *cmd++; /* remember delimiter character */
4853 }
4854 else /* find the end of the regexp */
4855 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004856#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4857 if (p_altkeymap && curwin->w_p_rl)
4858 lrF_sub(cmd);
4859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 which_pat = RE_LAST; /* use last used regexp */
4861 delimiter = *cmd++; /* remember delimiter character */
4862 pat = cmd; /* remember start of search pat */
4863 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4864 if (cmd[0] == delimiter) /* end delimiter found */
4865 *cmd++ = NUL; /* replace it with a NUL */
4866 }
4867
4868 /*
4869 * Small incompatibility: vi sees '\n' as end of the command, but in
4870 * Vim we want to use '\n' to find/substitute a NUL.
4871 */
4872 sub = cmd; /* remember the start of the substitution */
4873
4874 while (cmd[0])
4875 {
4876 if (cmd[0] == delimiter) /* end delimiter found */
4877 {
4878 *cmd++ = NUL; /* replace it with a NUL */
4879 break;
4880 }
4881 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4882 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004883 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885
4886 if (!eap->skip)
4887 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004888 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4889 if (STRCMP(sub, "%") == 0
4890 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4891 {
4892 if (old_sub == NULL) /* there is no previous command */
4893 {
4894 EMSG(_(e_nopresub));
4895 return;
4896 }
4897 sub = old_sub;
4898 }
4899 else
4900 {
4901 vim_free(old_sub);
4902 old_sub = vim_strsave(sub);
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 }
4906 else if (!eap->skip) /* use previous pattern and substitution */
4907 {
4908 if (old_sub == NULL) /* there is no previous command */
4909 {
4910 EMSG(_(e_nopresub));
4911 return;
4912 }
4913 pat = NULL; /* search_regcomp() will use previous pattern */
4914 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004915
4916 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4917 * last column after using "$". */
4918 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004921 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4922 * more efficient.
4923 * TODO: find a generic solution to make line-joining operations more
4924 * efficient, avoid allocating a string that grows in size.
4925 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004926 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004927 && *sub == NUL
4928 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4929 || *cmd == 'p' || *cmd == '#'))))
4930 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004931 linenr_T joined_lines_count;
4932
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004933 curwin->w_cursor.lnum = eap->line1;
4934 if (*cmd == 'l')
4935 eap->flags = EXFLAG_LIST;
4936 else if (*cmd == '#')
4937 eap->flags = EXFLAG_NR;
4938 else if (*cmd == 'p')
4939 eap->flags = EXFLAG_PRINT;
4940
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004941 /* The number of lines joined is the number of lines in the range plus
4942 * one. One less when the last line is included. */
4943 joined_lines_count = eap->line2 - eap->line1 + 1;
4944 if (eap->line2 < curbuf->b_ml.ml_line_count)
4945 ++joined_lines_count;
4946 if (joined_lines_count > 1)
4947 {
4948 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
4949 sub_nsubs = joined_lines_count - 1;
4950 sub_nlines = 1;
4951 (void)do_sub_msg(FALSE);
4952 ex_may_print(eap);
4953 }
4954
4955 if (!cmdmod.keeppatterns)
4956 save_re_pat(RE_SUBST, pat, p_magic);
4957#ifdef FEAT_CMDHIST
4958 /* put pattern in history */
4959 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
4960#endif
4961
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004962 return;
4963 }
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 /*
4966 * Find trailing options. When '&' is used, keep old options.
4967 */
4968 if (*cmd == '&')
4969 ++cmd;
4970 else
4971 {
4972 if (!p_ed)
4973 {
4974 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004975 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004977 subflags.do_all = FALSE;
4978 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004980 subflags.do_error = TRUE;
4981 subflags.do_print = FALSE;
4982 subflags.do_count = FALSE;
4983 subflags.do_number = FALSE;
4984 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 while (*cmd)
4987 {
4988 /*
4989 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4990 * 'r' is never inverted.
4991 */
4992 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004993 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004995 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004996 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004997 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004999 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else if (*cmd == 'r') /* use last used regexp */
5001 which_pat = RE_LAST;
5002 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005003 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005004 else if (*cmd == '#')
5005 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005006 subflags.do_print = TRUE;
5007 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005008 }
5009 else if (*cmd == 'l')
5010 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005011 subflags.do_print = TRUE;
5012 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005015 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005017 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 else
5019 break;
5020 ++cmd;
5021 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005022 if (subflags.do_count)
5023 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005025 save_do_all = subflags.do_all;
5026 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 /*
5029 * check for a trailing count
5030 */
5031 cmd = skipwhite(cmd);
5032 if (VIM_ISDIGIT(*cmd))
5033 {
5034 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005035 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 EMSG(_(e_zerocount));
5038 return;
5039 }
5040 eap->line1 = eap->line2;
5041 eap->line2 += i - 1;
5042 if (eap->line2 > curbuf->b_ml.ml_line_count)
5043 eap->line2 = curbuf->b_ml.ml_line_count;
5044 }
5045
5046 /*
5047 * check for trailing command or garbage
5048 */
5049 cmd = skipwhite(cmd);
5050 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5051 {
5052 eap->nextcmd = check_nextcmd(cmd);
5053 if (eap->nextcmd == NULL)
5054 {
5055 EMSG(_(e_trailing));
5056 return;
5057 }
5058 }
5059
5060 if (eap->skip) /* not executing commands, only parsing */
5061 return;
5062
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005063 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005064 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005065 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005066 EMSG(_(e_modifiable));
5067 return;
5068 }
5069
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5071 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005072 if (subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 EMSG(_(e_invcmd));
5074 return;
5075 }
5076
5077 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005078 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005080 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 regmatch.rmm_ic = FALSE;
5082
5083 sub_firstline = NULL;
5084
5085 /*
5086 * ~ in the substitute pattern is replaced with the old pattern.
5087 * We do it here once to avoid it to be replaced over and over again.
5088 * But don't do it when it starts with "\=", then it's an expression.
5089 */
5090 if (!(sub[0] == '\\' && sub[1] == '='))
5091 sub = regtilde(sub, p_magic);
5092
5093 /*
5094 * Check for a match on each line.
5095 */
5096 line2 = eap->line2;
5097 for (lnum = eap->line1; lnum <= line2 && !(got_quit
5098#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
5099 || aborting()
5100#endif
5101 ); ++lnum)
5102 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005103 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5104 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (nmatch)
5106 {
5107 colnr_T copycol;
5108 colnr_T matchcol;
5109 colnr_T prev_matchcol = MAXCOL;
5110 char_u *new_end, *new_start = NULL;
5111 unsigned new_start_len = 0;
5112 char_u *p1;
5113 int did_sub = FALSE;
5114 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005115 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 long nmatch_tl = 0; /* nr of lines matched below lnum */
5117 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005118 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005119 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 /*
5122 * The new text is build up step by step, to avoid too much
5123 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005124 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 * copycol Column in the old text where we started
5126 * looking for a match; from here old text still
5127 * needs to be copied to the new text.
5128 * matchcol Column number of the old text where to look
5129 * for the next match. It's just after the
5130 * previous match or one further.
5131 * prev_matchcol Column just after the previous match (if any).
5132 * Mostly equal to matchcol, except for the first
5133 * match and after skipping an empty match.
5134 * regmatch.*pos Where the pattern matched in the old text.
5135 * new_start The new text, all that has been produced so
5136 * far.
5137 * new_end The new text, where to append new text.
5138 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005139 * lnum The line number where we found the start of
5140 * the match. Can be below the line we searched
5141 * when there is a \n before a \zs in the
5142 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 * sub_firstlnum The line number in the buffer where to look
5144 * for a match. Can be different from "lnum"
5145 * when the pattern or substitute string contains
5146 * line breaks.
5147 *
5148 * Special situations:
5149 * - When the substitute string contains a line break, the part up
5150 * to the line break is inserted in the text, but the copy of
5151 * the original line is kept. "sub_firstlnum" is adjusted for
5152 * the inserted lines.
5153 * - When the matched pattern contains a line break, the old line
5154 * is taken from the line at the end of the pattern. The lines
5155 * in the match are deleted later, "sub_firstlnum" is adjusted
5156 * accordingly.
5157 *
5158 * The new text is built up in new_start[]. It has some extra
5159 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005160 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 *
5162 * Make a copy of the old line, so it won't be taken away when
5163 * updating the screen or handling a multi-line match. The "old_"
5164 * pointers point into this copy.
5165 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005166 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 copycol = 0;
5168 matchcol = 0;
5169
5170 /* At first match, remember current cursor position. */
5171 if (!got_match)
5172 {
5173 setpcmark();
5174 got_match = TRUE;
5175 }
5176
5177 /*
5178 * Loop until nothing more to replace in this line.
5179 * 1. Handle match with empty string.
5180 * 2. If do_ask is set, ask for confirmation.
5181 * 3. substitute the string.
5182 * 4. if do_all is set, find next match
5183 * 5. break if there isn't another match in this line
5184 */
5185 for (;;)
5186 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005187 /* Advance "lnum" to the line where the match starts. The
5188 * match does not start in the first line when there is a line
5189 * break before \zs. */
5190 if (regmatch.startpos[0].lnum > 0)
5191 {
5192 lnum += regmatch.startpos[0].lnum;
5193 sub_firstlnum += regmatch.startpos[0].lnum;
5194 nmatch -= regmatch.startpos[0].lnum;
5195 vim_free(sub_firstline);
5196 sub_firstline = NULL;
5197 }
5198
5199 if (sub_firstline == NULL)
5200 {
5201 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5202 if (sub_firstline == NULL)
5203 {
5204 vim_free(new_start);
5205 goto outofmem;
5206 }
5207 }
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 /* Save the line number of the last change for the final
5210 * cursor position (just like Vi). */
5211 curwin->w_cursor.lnum = lnum;
5212 do_again = FALSE;
5213
5214 /*
5215 * 1. Match empty string does not count, except for first
5216 * match. This reproduces the strange vi behaviour.
5217 * This also catches endless loops.
5218 */
5219 if (matchcol == prev_matchcol
5220 && regmatch.endpos[0].lnum == 0
5221 && matchcol == regmatch.endpos[0].col)
5222 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005223 if (sub_firstline[matchcol] == NUL)
5224 /* We already were at the end of the line. Don't look
5225 * for a match in this line again. */
5226 skip_match = TRUE;
5227 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005228 {
5229 /* search for a match at next column */
5230#ifdef FEAT_MBYTE
5231 if (has_mbyte)
5232 matchcol += mb_ptr2len(sub_firstline + matchcol);
5233 else
5234#endif
5235 ++matchcol;
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 goto skip;
5238 }
5239
5240 /* Normally we continue searching for a match just after the
5241 * previous match. */
5242 matchcol = regmatch.endpos[0].col;
5243 prev_matchcol = matchcol;
5244
5245 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005246 * 2. If do_count is set only increase the counter.
5247 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005249 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005250 {
5251 /* For a multi-line match, put matchcol at the NUL at
5252 * the end of the line and set nmatch to one, so that
5253 * we continue looking for a match on the next line.
5254 * Avoids that ":s/\nB\@=//gc" get stuck. */
5255 if (nmatch > 1)
5256 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005257 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005258 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005259 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005260 }
5261 sub_nsubs++;
5262 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005263#ifdef FEAT_EVAL
5264 /* Skip the substitution, unless an expression is used,
5265 * then it is evaluated in the sandbox. */
5266 if (!(sub[0] == '\\' && sub[1] == '='))
5267#endif
5268 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005269 }
5270
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005271 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005273 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /* change State to CONFIRM, so that the mouse works
5276 * properly */
5277 save_State = State;
5278 State = CONFIRM;
5279#ifdef FEAT_MOUSE
5280 setmouse(); /* disable mouse in xterm */
5281#endif
5282 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005283#ifdef FEAT_CURSORBIND
5284 if (curwin->w_p_crb)
5285 do_check_cursorbind();
5286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /* When 'cpoptions' contains "u" don't sync undo when
5289 * asking for confirmation. */
5290 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5291 ++no_u_sync;
5292
5293 /*
5294 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5295 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005296 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005298 if (exmode_active)
5299 {
5300 char_u *resp;
5301 colnr_T sc, ec;
5302
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005303 print_line_no_prefix(lnum,
5304 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005305
5306 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5307 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005308 if (curwin->w_cursor.col < 0)
5309 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005310 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005311 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005312 {
5313 int numw = number_width(curwin) + 1;
5314 sc += numw;
5315 ec += numw;
5316 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005317 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005318 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005319 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005320 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005321 msg_putchar('^');
5322
5323 resp = getexmodeline('?', NULL, 0);
5324 if (resp != NULL)
5325 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005326 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005327 vim_free(resp);
5328 }
5329 }
5330 else
5331 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005332 char_u *orig_line = NULL;
5333 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005335 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005337 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005339 /* Invert the matched string.
5340 * Remove the inversion afterwards. */
5341 temp = RedrawingDisabled;
5342 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005344 if (new_start != NULL)
5345 {
5346 /* There already was a substitution, we would
5347 * like to show this to the user. We cannot
5348 * really update the line, it would change
5349 * what matches. Temporarily replace the line
5350 * and change it back afterwards. */
5351 orig_line = vim_strsave(ml_get(lnum));
5352 if (orig_line != NULL)
5353 {
5354 char_u *new_line = concat_str(new_start,
5355 sub_firstline + copycol);
5356
5357 if (new_line == NULL)
5358 {
5359 vim_free(orig_line);
5360 orig_line = NULL;
5361 }
5362 else
5363 {
5364 /* Position the cursor relative to the
5365 * end of the line, the previous
5366 * substitute may have inserted or
5367 * deleted characters before the
5368 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005369 len_change = (int)STRLEN(new_line)
5370 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005371 curwin->w_cursor.col += len_change;
5372 ml_replace(lnum, new_line, FALSE);
5373 }
5374 }
5375 }
5376
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005377 search_match_lines = regmatch.endpos[0].lnum
5378 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005379 search_match_endcol = regmatch.endpos[0].col
5380 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005381 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005383 update_topline();
5384 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005385 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005386 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005387 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005390 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005392 if (msg_row == Rows - 1)
5393 msg_didout = FALSE; /* avoid a scroll-up */
5394 msg_starthere();
5395 i = msg_scroll;
5396 msg_scroll = 0; /* truncate msg when
5397 needed */
5398 msg_no_more = TRUE;
5399 /* write message same highlighting as for
5400 * wait_return */
5401 smsg_attr(hl_attr(HLF_R),
5402 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
5403 msg_no_more = FALSE;
5404 msg_scroll = i;
5405 showruler(TRUE);
5406 windgoto(msg_row, msg_col);
5407 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005410 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005412 ++no_mapping; /* don't map this key */
5413 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005414 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005415 --allow_keys;
5416 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005418 /* clear the question */
5419 msg_didout = FALSE; /* don't scroll up */
5420 msg_col = 0;
5421 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005422
5423 /* restore the line */
5424 if (orig_line != NULL)
5425 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005426 }
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005429 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005431 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432#endif
5433 )
5434 {
5435 got_quit = TRUE;
5436 break;
5437 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005438 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005440 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005442 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {
5444 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005445 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 line2 = lnum;
5447 break;
5448 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005449 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005451 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 break;
5453 }
5454#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005455 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005457 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 scrolldown_clamp();
5459#endif
5460 }
5461 State = save_State;
5462#ifdef FEAT_MOUSE
5463 setmouse();
5464#endif
5465 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5466 --no_u_sync;
5467
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005468 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 /* For a multi-line match, put matchcol at the NUL at
5471 * the end of the line and set nmatch to one, so that
5472 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005473 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5474 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (nmatch > 1)
5476 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005477 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005478 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480 goto skip;
5481 }
5482 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005483 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485
5486 /* Move the cursor to the start of the match, so that we can
5487 * use "\=col("."). */
5488 curwin->w_cursor.col = regmatch.startpos[0].col;
5489
5490 /*
5491 * 3. substitute the string.
5492 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005493#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005494 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005495 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005496 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005497 save_ma = curbuf->b_p_ma;
5498 curbuf->b_p_ma = FALSE;
5499 sandbox++;
5500 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005501 /* Save flags for recursion. They can change for e.g.
5502 * :s/^/\=execute("s#^##gn") */
5503 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005506 sublen = vim_regsub_multi(&regmatch,
5507 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005509#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005510 /* Don't keep flags set by a recursive call. */
5511 subflags = subflags_save;
5512 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005513 {
5514 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005515 if (sandbox > 0)
5516 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005517 goto skip;
5518 }
5519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar4463f292005-09-25 22:20:24 +00005521 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005522 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005523 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5524 {
5525 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5526 skip_match = TRUE;
5527 }
5528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 /* Need room for:
5530 * - result so far in new_start (not for first sub in line)
5531 * - original text up to match
5532 * - length of substituted part
5533 * - original text after match
5534 */
5535 if (nmatch == 1)
5536 p1 = sub_firstline;
5537 else
5538 {
5539 p1 = ml_get(sub_firstlnum + nmatch - 1);
5540 nmatch_tl += nmatch - 1;
5541 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005542 copy_len = regmatch.startpos[0].col - copycol;
5543 needed_len = copy_len + ((unsigned)STRLEN(p1)
5544 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if (new_start == NULL)
5546 {
5547 /*
5548 * Get some space for a temporary buffer to do the
5549 * substitution into (and some extra space to avoid
5550 * too many calls to alloc()/free()).
5551 */
5552 new_start_len = needed_len + 50;
5553 if ((new_start = alloc_check(new_start_len)) == NULL)
5554 goto outofmem;
5555 *new_start = NUL;
5556 new_end = new_start;
5557 }
5558 else
5559 {
5560 /*
5561 * Check if the temporary buffer is long enough to do the
5562 * substitution into. If not, make it larger (with a bit
5563 * extra to avoid too many calls to alloc()/free()).
5564 */
5565 len = (unsigned)STRLEN(new_start);
5566 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005567 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 new_start_len = needed_len + 50;
5570 if ((p1 = alloc_check(new_start_len)) == NULL)
5571 {
5572 vim_free(new_start);
5573 goto outofmem;
5574 }
5575 mch_memmove(p1, new_start, (size_t)(len + 1));
5576 vim_free(new_start);
5577 new_start = p1;
5578 }
5579 new_end = new_start + len;
5580 }
5581
5582 /*
5583 * copy the text up to the part that matched
5584 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005585 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5586 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005588 (void)vim_regsub_multi(&regmatch,
5589 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 sub, new_end, TRUE, p_magic, TRUE);
5591 sub_nsubs++;
5592 did_sub = TRUE;
5593
5594 /* Move the cursor to the start of the line, to avoid that it
5595 * is beyond the end of the line after the substitution. */
5596 curwin->w_cursor.col = 0;
5597
5598 /* For a multi-line match, make a copy of the last matched
5599 * line and continue in that one. */
5600 if (nmatch > 1)
5601 {
5602 sub_firstlnum += nmatch - 1;
5603 vim_free(sub_firstline);
5604 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5605 /* When going beyond the last line, stop substituting. */
5606 if (sub_firstlnum <= line2)
5607 do_again = TRUE;
5608 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005609 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611
5612 /* Remember next character to be copied. */
5613 copycol = regmatch.endpos[0].col;
5614
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005615 if (skip_match)
5616 {
5617 /* Already hit end of the buffer, sub_firstlnum is one
5618 * less than what it ought to be. */
5619 vim_free(sub_firstline);
5620 sub_firstline = vim_strsave((char_u *)"");
5621 copycol = 0;
5622 }
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 /*
5625 * Now the trick is to replace CTRL-M chars with a real line
5626 * break. This would make it impossible to insert a CTRL-M in
5627 * the text. The line break can be avoided by preceding the
5628 * CTRL-M with a backslash. To be able to insert a backslash,
5629 * they must be doubled in the string and are halved here.
5630 * That is Vi compatible.
5631 */
5632 for (p1 = new_end; *p1; ++p1)
5633 {
5634 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005635 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 else if (*p1 == CAR)
5637 {
5638 if (u_inssub(lnum) == OK) /* prepare for undo */
5639 {
5640 *p1 = NUL; /* truncate up to the CR */
5641 ml_append(lnum - 1, new_start,
5642 (colnr_T)(p1 - new_start + 1), FALSE);
5643 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005644 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 appended_lines(lnum - 1, 1L);
5646 else
5647 {
5648 if (first_line == 0)
5649 first_line = lnum;
5650 last_line = lnum + 1;
5651 }
5652 /* All line numbers increase. */
5653 ++sub_firstlnum;
5654 ++lnum;
5655 ++line2;
5656 /* move the cursor to the new line, like Vi */
5657 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005658 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005659 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 p1 = new_start - 1;
5661 }
5662 }
5663#ifdef FEAT_MBYTE
5664 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005665 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666#endif
5667 }
5668
5669 /*
5670 * 4. If do_all is set, find next match.
5671 * Prevent endless loop with patterns that match empty
5672 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5673 * But ":s/\n/#/" is OK.
5674 */
5675skip:
5676 /* We already know that we did the last subst when we are at
5677 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005678 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5679 * "line2" when there is a \zs in the pattern after a line
5680 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005681 lastone = (skip_match
5682 || got_int
5683 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005684 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005685 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005686 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5687 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 nmatch = -1;
5689
5690 /*
5691 * Replace the line in the buffer when needed. This is
5692 * skipped when there are more matches.
5693 * The check for nmatch_tl is needed for when multi-line
5694 * matching must replace the lines before trying to do another
5695 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005696 * When the match starts below where we start searching also
5697 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 */
5699 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 || nmatch_tl > 0
5701 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005702 curbuf, sub_firstlnum,
5703 matchcol, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005704 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 if (new_start != NULL)
5707 {
5708 /*
5709 * Copy the rest of the line, that didn't match.
5710 * "matchcol" has to be adjusted, we use the end of
5711 * the line as reference, because the substitute may
5712 * have changed the number of characters. Same for
5713 * "prev_matchcol".
5714 */
5715 STRCAT(new_start, sub_firstline + copycol);
5716 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5717 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5718 - prev_matchcol;
5719
5720 if (u_savesub(lnum) != OK)
5721 break;
5722 ml_replace(lnum, new_start, TRUE);
5723
5724 if (nmatch_tl > 0)
5725 {
5726 /*
5727 * Matched lines have now been substituted and are
5728 * useless, delete them. The part after the match
5729 * has been appended to new_start, we don't need
5730 * it in the buffer.
5731 */
5732 ++lnum;
5733 if (u_savedel(lnum, nmatch_tl) != OK)
5734 break;
5735 for (i = 0; i < nmatch_tl; ++i)
5736 ml_delete(lnum, (int)FALSE);
5737 mark_adjust(lnum, lnum + nmatch_tl - 1,
5738 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005739 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 deleted_lines(lnum, nmatch_tl);
5741 --lnum;
5742 line2 -= nmatch_tl; /* nr of lines decreases */
5743 nmatch_tl = 0;
5744 }
5745
5746 /* When asking, undo is saved each time, must also set
5747 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005748 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 changed_bytes(lnum, 0);
5750 else
5751 {
5752 if (first_line == 0)
5753 first_line = lnum;
5754 last_line = lnum + 1;
5755 }
5756
5757 sub_firstlnum = lnum;
5758 vim_free(sub_firstline); /* free the temp buffer */
5759 sub_firstline = new_start;
5760 new_start = NULL;
5761 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5762 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5763 - prev_matchcol;
5764 copycol = 0;
5765 }
5766 if (nmatch == -1 && !lastone)
5767 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005768 sub_firstlnum, matchcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 /*
5771 * 5. break if there isn't another match in this line
5772 */
5773 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005774 {
5775 /* If the match found didn't start where we were
5776 * searching, do the next search in the line where we
5777 * found the match. */
5778 if (nmatch == -1)
5779 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783
5784 line_breakcheck();
5785 }
5786
5787 if (did_sub)
5788 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005789 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 vim_free(sub_firstline); /* free the copy of the original line */
5791 sub_firstline = NULL;
5792 }
5793
5794 line_breakcheck();
5795 }
5796
5797 if (first_line != 0)
5798 {
5799 /* Need to subtract the number of added lines from "last_line" to get
5800 * the line number before the change (same as adding the number of
5801 * deleted lines). */
5802 i = curbuf->b_ml.ml_line_count - old_line_count;
5803 changed_lines(first_line, 0, last_line - i, i);
5804 }
5805
5806outofmem:
5807 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005808
5809 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005810 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005811 curwin->w_cursor = old_cursor;
5812
Bram Moolenaar46475522010-03-23 17:36:29 +01005813 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815 /* Set the '[ and '] marks. */
5816 curbuf->b_op_start.lnum = eap->line1;
5817 curbuf->b_op_end.lnum = line2;
5818 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5819
5820 if (!global_busy)
5821 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005822 /* when interactive leave cursor on the match */
5823 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005824 {
5825 if (endcolumn)
5826 coladvance((colnr_T)MAXCOL);
5827 else
5828 beginline(BL_WHITE | BL_FIX);
5829 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005830 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 MSG("");
5832 }
5833 else
5834 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005835 if (subflags.do_print)
5836 print_line(curwin->w_cursor.lnum,
5837 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839 else if (!global_busy)
5840 {
5841 if (got_int) /* interrupted */
5842 EMSG(_(e_interr));
5843 else if (got_match) /* did find something but nothing substituted */
5844 MSG("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005845 else if (subflags.do_error) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 EMSG2(_(e_patnotf2), get_search_pat());
5847 }
5848
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005849#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005850 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005851 /* Cursor position may require updating */
5852 changed_window_setting();
5853#endif
5854
Bram Moolenaar473de612013-06-08 18:19:48 +02005855 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005856
5857 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005858 subflags.do_all = save_do_all;
5859 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860}
5861
5862/*
5863 * Give message for number of substitutions.
5864 * Can also be used after a ":global" command.
5865 * Return TRUE if a message was given.
5866 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005867 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005868do_sub_msg(
5869 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 /*
5872 * Only report substitutions when:
5873 * - more than 'report' substitutions
5874 * - command was typed by user, or number of changed lines > 'report'
5875 * - giving messages is not disabled by 'lazyredraw'
5876 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005877 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5878 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 && messaging())
5880 {
5881 if (got_int)
5882 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005883 else
5884 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005886 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005887 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005889 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005890 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 sub_nsubs);
5892 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005893 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005894 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005896 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005897 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005900 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return TRUE;
5902 }
5903 if (got_int)
5904 {
5905 EMSG(_(e_interr));
5906 return TRUE;
5907 }
5908 return FALSE;
5909}
5910
5911/*
5912 * Execute a global command of the form:
5913 *
5914 * g/pattern/X : execute X on all lines where pattern matches
5915 * v/pattern/X : execute X on all lines where pattern does not match
5916 *
5917 * where 'X' is an EX command
5918 *
5919 * The command character (as well as the trailing slash) is optional, and
5920 * is assumed to be 'p' if missing.
5921 *
5922 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005923 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 * for each line that has a mark. This is required because after deleting
5925 * lines we do not know where to search for the next match.
5926 */
5927 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005928ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005931 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 int type; /* first char of cmd: 'v' or 'g' */
5933 char_u *cmd; /* command argument */
5934
5935 char_u delim; /* delimiter, normally '/' */
5936 char_u *pat;
5937 regmmatch_T regmatch;
5938 int match;
5939 int which_pat;
5940
5941 if (global_busy)
5942 {
5943 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5944 return;
5945 }
5946
5947 if (eap->forceit) /* ":global!" is like ":vglobal" */
5948 type = 'v';
5949 else
5950 type = *eap->cmd;
5951 cmd = eap->arg;
5952 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
5954 /*
5955 * undocumented vi feature:
5956 * "\/" and "\?": use previous search pattern.
5957 * "\&": use previous substitute pattern.
5958 */
5959 if (*cmd == '\\')
5960 {
5961 ++cmd;
5962 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5963 {
5964 EMSG(_(e_backslash));
5965 return;
5966 }
5967 if (*cmd == '&')
5968 which_pat = RE_SUBST; /* use previous substitute pattern */
5969 else
5970 which_pat = RE_SEARCH; /* use previous search pattern */
5971 ++cmd;
5972 pat = (char_u *)"";
5973 }
5974 else if (*cmd == NUL)
5975 {
5976 EMSG(_("E148: Regular expression missing from global"));
5977 return;
5978 }
5979 else
5980 {
5981 delim = *cmd; /* get the delimiter */
5982 if (delim)
5983 ++cmd; /* skip delimiter if there is one */
5984 pat = cmd; /* remember start of pattern */
5985 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5986 if (cmd[0] == delim) /* end delimiter found */
5987 *cmd++ = NUL; /* replace it with a NUL */
5988 }
5989
5990#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5991 if (p_altkeymap && curwin->w_p_rl)
5992 lrFswap(pat,0);
5993#endif
5994
5995 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5996 {
5997 EMSG(_(e_invcmd));
5998 return;
5999 }
6000
6001 /*
6002 * pass 1: set marks for each (not) matching line
6003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6005 {
6006 /* a match on this line? */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006007 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
6008 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if ((type == 'g' && match) || (type == 'v' && !match))
6010 {
6011 ml_setmarked(lnum);
6012 ndone++;
6013 }
6014 line_breakcheck();
6015 }
6016
6017 /*
6018 * pass 2: execute the command for each line that has been marked
6019 */
6020 if (got_int)
6021 MSG(_(e_interr));
6022 else if (ndone == 0)
6023 {
6024 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00006025 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 else
Bram Moolenaarc389fd32013-03-07 16:08:35 +01006027 smsg((char_u *)_("Pattern not found: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 }
6029 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006030 {
6031#ifdef FEAT_CLIPBOARD
6032 start_global_changes();
6033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006035#ifdef FEAT_CLIPBOARD
6036 end_global_changes();
6037#endif
6038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
6040 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar473de612013-06-08 18:19:48 +02006041 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042}
6043
6044/*
6045 * Execute "cmd" on lines marked with ml_setmarked().
6046 */
6047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006048global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006050 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6051 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6052 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054 /*
6055 * Set current position only once for a global command.
6056 * If global_busy is set, setpcmark() will not do anything.
6057 * If there is an error, global_busy will be incremented.
6058 */
6059 setpcmark();
6060
6061 /* When the command writes a message, don't overwrite the command. */
6062 msg_didout = TRUE;
6063
Bram Moolenaard25bc232010-03-23 17:49:24 +01006064 sub_nsubs = 0;
6065 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 global_need_beginline = FALSE;
6067 global_busy = 1;
6068 old_lcount = curbuf->b_ml.ml_line_count;
6069 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6070 {
6071 curwin->w_cursor.lnum = lnum;
6072 curwin->w_cursor.col = 0;
6073 if (*cmd == NUL || *cmd == '\n')
6074 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
6075 else
6076 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
6077 ui_breakcheck();
6078 }
6079
6080 global_busy = 0;
6081 if (global_need_beginline)
6082 beginline(BL_WHITE | BL_FIX);
6083 else
6084 check_cursor(); /* cursor may be beyond the end of the line */
6085
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006086 /* the cursor may not have moved in the text but a change in a previous
6087 * line may move it on the screen */
6088 changed_line_abv_curs();
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 /* If it looks like no message was written, allow overwriting the
6091 * command with the report for number of changes. */
6092 if (msg_col == 0 && msg_scrolled == 0)
6093 msg_didout = FALSE;
6094
Bram Moolenaar45667512007-05-10 19:24:43 +00006095 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006096 * number of extra or deleted lines.
6097 * Don't report extra or deleted lines in the edge case where the buffer
6098 * we are in after execution is different from the buffer we started in. */
6099 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6101}
6102
6103#ifdef FEAT_VIMINFO
6104 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006105read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006107 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 vim_free(old_sub);
6109 if (force || old_sub == NULL)
6110 old_sub = viminfo_readstring(virp, 1, TRUE);
6111 return viminfo_readline(virp);
6112}
6113
6114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006115write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6118 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006119 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 viminfo_writestring(fp, old_sub);
6121 }
6122}
6123#endif /* FEAT_VIMINFO */
6124
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006125#if defined(EXITFREE) || defined(PROTO)
6126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006128{
6129 vim_free(old_sub);
6130}
6131#endif
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
6134/*
6135 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006136 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006138 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006139prepare_tagpreview(
6140 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 win_T *wp;
6143
6144# ifdef FEAT_GUI
6145 need_mouse_correct = TRUE;
6146# endif
6147
6148 /*
6149 * If there is already a preview window open, use that one.
6150 */
6151 if (!curwin->w_p_pvw)
6152 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006153 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (wp->w_p_pvw)
6155 break;
6156 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006157 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 else
6159 {
6160 /*
6161 * There is no preview window open yet. Create one.
6162 */
6163 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6164 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006165 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 curwin->w_p_pvw = TRUE;
6167 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006168 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6169 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170# ifdef FEAT_DIFF
6171 curwin->w_p_diff = FALSE; /* no 'diff' */
6172# endif
6173# ifdef FEAT_FOLDING
6174 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6175# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006176 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006179 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180}
6181
6182#endif
6183
6184
6185/*
6186 * ":help": open a read-only window on a help file
6187 */
6188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 char_u *arg;
6192 char_u *tag;
6193 FILE *helpfd; /* file descriptor of help file */
6194 int n;
6195 int i;
6196#ifdef FEAT_WINDOWS
6197 win_T *wp;
6198#endif
6199 int num_matches;
6200 char_u **matches;
6201 char_u *p;
6202 int empty_fnum = 0;
6203 int alt_fnum = 0;
6204 buf_T *buf;
6205#ifdef FEAT_MULTI_LANG
6206 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006207 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006209#ifdef FEAT_FOLDING
6210 int old_KeyTyped = KeyTyped;
6211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212
6213 if (eap != NULL)
6214 {
6215 /*
6216 * A ":help" command ends at the first LF, or at a '|' that is
6217 * followed by some text. Set nextcmd to the following command.
6218 */
6219 for (arg = eap->arg; *arg; ++arg)
6220 {
6221 if (*arg == '\n' || *arg == '\r'
6222 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6223 {
6224 *arg++ = NUL;
6225 eap->nextcmd = arg;
6226 break;
6227 }
6228 }
6229 arg = eap->arg;
6230
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006231 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
6233 EMSG(_("E478: Don't panic!"));
6234 return;
6235 }
6236
6237 if (eap->skip) /* not executing commands */
6238 return;
6239 }
6240 else
6241 arg = (char_u *)"";
6242
6243 /* remove trailing blanks */
6244 p = arg + STRLEN(arg) - 1;
6245 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
6246 *p-- = NUL;
6247
6248#ifdef FEAT_MULTI_LANG
6249 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006250 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251#endif
6252
6253 /* When no argument given go to the index. */
6254 if (*arg == NUL)
6255 arg = (char_u *)"help.txt";
6256
6257 /*
6258 * Check if there is a match for the argument.
6259 */
6260 n = find_help_tags(arg, &num_matches, &matches,
6261 eap != NULL && eap->forceit);
6262
6263 i = 0;
6264#ifdef FEAT_MULTI_LANG
6265 if (n != FAIL && lang != NULL)
6266 /* Find first item with the requested language. */
6267 for (i = 0; i < num_matches; ++i)
6268 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006269 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 if (len > 3 && matches[i][len - 3] == '@'
6271 && STRICMP(matches[i] + len - 2, lang) == 0)
6272 break;
6273 }
6274#endif
6275 if (i >= num_matches || n == FAIL)
6276 {
6277#ifdef FEAT_MULTI_LANG
6278 if (lang != NULL)
6279 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
6280 else
6281#endif
6282 EMSG2(_("E149: Sorry, no help for %s"), arg);
6283 if (n != FAIL)
6284 FreeWild(num_matches, matches);
6285 return;
6286 }
6287
6288 /* The first match (in the requested language) is the best match. */
6289 tag = vim_strsave(matches[i]);
6290 FreeWild(num_matches, matches);
6291
6292#ifdef FEAT_GUI
6293 need_mouse_correct = TRUE;
6294#endif
6295
6296 /*
6297 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006298 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006300 if (!curwin->w_buffer->b_help
6301#ifdef FEAT_WINDOWS
6302 || cmdmod.tab != 0
6303#endif
6304 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
6306#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006307 if (cmdmod.tab != 0)
6308 wp = NULL;
6309 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006310 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006311 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
6312 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6314 win_enter(wp, TRUE);
6315 else
6316#endif
6317 {
6318 /*
6319 * There is no help window yet.
6320 * Try to open the file specified by the "helpfile" option.
6321 */
6322 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6323 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006324 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 goto erret;
6326 }
6327 fclose(helpfd);
6328
6329#ifdef FEAT_WINDOWS
6330 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006331 * specified, the current window is vertically split and
6332 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006335 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006338 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339#else
6340 /* use current window */
6341 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
6345#ifdef FEAT_WINDOWS
6346 if (curwin->w_height < p_hh)
6347 win_setheight((int)p_hh);
6348#endif
6349
6350 /*
6351 * Open help file (do_ecmd() will set b_help flag, readfile() will
6352 * set b_p_ro flag).
6353 * Set the alternate file to the previously edited file.
6354 */
6355 alt_fnum = curbuf->b_fnum;
6356 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006357 ECMD_HIDE + ECMD_SET_HELP,
6358#ifdef FEAT_WINDOWS
6359 NULL /* buffer is still open, don't store info */
6360#else
6361 curwin
6362#endif
6363 );
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006364 if (!cmdmod.keepalt)
6365 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 empty_fnum = curbuf->b_fnum;
6367 }
6368 }
6369
6370 if (!p_im)
6371 restart_edit = 0; /* don't want insert mode in help file */
6372
Bram Moolenaar8f535582011-09-30 17:30:31 +02006373#ifdef FEAT_FOLDING
6374 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6375 * It is needed for do_tag top open folds under the cursor. */
6376 KeyTyped = old_KeyTyped;
6377#endif
6378
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 if (tag != NULL)
6380 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6381
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006382 /* Delete the empty buffer if we're not using it. Careful: autocommands
6383 * may have jumped to another window, check that the buffer is not in a
6384 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6386 {
6387 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006388 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 wipe_buffer(buf, TRUE);
6390 }
6391
6392 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006393 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 curwin->w_alt_fnum = alt_fnum;
6395
6396erret:
6397 vim_free(tag);
6398}
6399
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006400/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006401 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006402 */
6403 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006404ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006405{
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02006406#if defined(FEAT_WINDOWS)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006407 win_T *win;
6408
6409 FOR_ALL_WINDOWS(win)
6410 {
6411 if (win->w_buffer->b_help)
6412 {
6413 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006414 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006415 }
6416 }
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02006417#endif
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006420#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6421/*
6422 * In an argument search for a language specifiers in the form "@xx".
6423 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6424 * Returns NULL if not found.
6425 */
6426 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006427check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006428{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006429 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006430
6431 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6432 && ASCII_ISALPHA(arg[len - 1]))
6433 {
6434 arg[len - 3] = NUL; /* remove the '@' */
6435 return arg + len - 2;
6436 }
6437 return NULL;
6438}
6439#endif
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441/*
6442 * Return a heuristic indicating how well the given string matches. The
6443 * smaller the number, the better the match. This is the order of priorities,
6444 * from best match to worst match:
6445 * - Match with least alpha-numeric characters is better.
6446 * - Match with least total characters is better.
6447 * - Match towards the start is better.
6448 * - Match starting with "+" is worse (feature instead of command)
6449 * Assumption is made that the matched_string passed has already been found to
6450 * match some string for which help is requested. webb.
6451 */
6452 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006453help_heuristic(
6454 char_u *matched_string,
6455 int offset, /* offset for match */
6456 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 int num_letters;
6459 char_u *p;
6460
6461 num_letters = 0;
6462 for (p = matched_string; *p; p++)
6463 if (ASCII_ISALNUM(*p))
6464 num_letters++;
6465
6466 /*
6467 * Multiply the number of letters by 100 to give it a much bigger
6468 * weighting than the number of characters.
6469 * If there only is a match while ignoring case, add 5000.
6470 * If the match starts in the middle of a word, add 10000 to put it
6471 * somewhere in the last half.
6472 * If the match is more than 2 chars from the start, multiply by 200 to
6473 * put it after matches at the start.
6474 */
6475 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6476 && ASCII_ISALNUM(matched_string[offset - 1]))
6477 offset += 10000;
6478 else if (offset > 2)
6479 offset *= 200;
6480 if (wrong_case)
6481 offset += 5000;
6482 /* Features are less interesting than the subjects themselves, but "+"
6483 * alone is not a feature. */
6484 if (matched_string[0] == '+' && matched_string[1] != NUL)
6485 offset += 100;
6486 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6487}
6488
6489/*
6490 * Compare functions for qsort() below, that checks the help heuristics number
6491 * that has been put after the tagname by find_tags().
6492 */
6493 static int
6494#ifdef __BORLANDC__
6495_RTLENTRYF
6496#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006497help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 char *p1;
6500 char *p2;
6501
6502 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6503 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6504 return strcmp(p1, p2);
6505}
6506
6507/*
6508 * Find all help tags matching "arg", sort them and return in matches[], with
6509 * the number of matches in num_matches.
6510 * The matches will be sorted with a "best" match algorithm.
6511 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6512 */
6513 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514find_help_tags(
6515 char_u *arg,
6516 int *num_matches,
6517 char_u ***matches,
6518 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 char_u *s, *d;
6521 int i;
6522 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006523 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006524 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaardad73092017-02-09 11:54:50 +01006525 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006526 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006527 "[count]", "[quotex]",
6528 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006529 "[pattern]", "\\|", "\\%$",
6530 "s/\\~", "s/\\U", "s/\\L",
6531 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006533 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006534 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaardad73092017-02-09 11:54:50 +01006535 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006536 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006537 "\\[count]", "\\[quotex]",
6538 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006539 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006540 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006541 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 int flags;
6543
6544 d = IObuff; /* assume IObuff is long enough! */
6545
6546 /*
6547 * Recognize a few exceptions to the rule. Some strings that contain '*'
6548 * with "star". Otherwise '*' is recognized as a wildcard.
6549 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006550 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (STRCMP(arg, mtable[i]) == 0)
6552 {
6553 STRCPY(d, rtable[i]);
6554 break;
6555 }
6556
6557 if (i < 0) /* no match in table */
6558 {
6559 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6560 * Also replace "\%^" and "\%(", they match every tag too.
6561 * Also "\zs", "\z1", etc.
6562 * Also "\@<", "\@=", "\@<=", etc.
6563 * And also "\_$" and "\_^". */
6564 if (arg[0] == '\\'
6565 && ((arg[1] != NUL && arg[2] == NUL)
6566 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6567 && arg[2] != NUL)))
6568 {
6569 STRCPY(d, "/\\\\");
6570 STRCPY(d + 3, arg + 1);
6571 /* Check for "/\\_$", should be "/\\_\$" */
6572 if (d[3] == '_' && d[4] == '$')
6573 STRCPY(d + 4, "\\$");
6574 }
6575 else
6576 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006577 /* Replace:
6578 * "[:...:]" with "\[:...:]"
6579 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006580 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006581 */
6582 if ((arg[0] == '[' && (arg[1] == ':'
6583 || (arg[1] == '+' && arg[2] == '+')))
6584 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 *d++ = '\\';
6586
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006587 /*
6588 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6589 */
6590 if (*arg == '(' && arg[1] == '\'')
6591 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 for (s = arg; *s; ++s)
6593 {
6594 /*
6595 * Replace "|" with "bar" and '"' with "quote" to match the name of
6596 * the tags for these commands.
6597 * Replace "*" with ".*" and "?" with "." to match command line
6598 * completion.
6599 * Insert a backslash before '~', '$' and '.' to avoid their
6600 * special meaning.
6601 */
6602 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6603 break;
6604 switch (*s)
6605 {
6606 case '|': STRCPY(d, "bar");
6607 d += 3;
6608 continue;
6609 case '"': STRCPY(d, "quote");
6610 d += 5;
6611 continue;
6612 case '*': *d++ = '.';
6613 break;
6614 case '?': *d++ = '.';
6615 continue;
6616 case '$':
6617 case '.':
6618 case '~': *d++ = '\\';
6619 break;
6620 }
6621
6622 /*
6623 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6624 * ":help i_^_CTRL-D" work.
6625 * Insert '-' before and after "CTRL-X" when applicable.
6626 */
6627 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6628 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6629 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006630 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6631 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 STRCPY(d, "CTRL-");
6633 d += 5;
6634 if (*s < ' ')
6635 {
6636#ifdef EBCDIC
6637 *d++ = CtrlChar(*s);
6638#else
6639 *d++ = *s + '@';
6640#endif
6641 if (d[-1] == '\\')
6642 *d++ = '\\'; /* double a backslash */
6643 }
6644 else
6645 *d++ = *++s;
6646 if (s[1] != NUL && s[1] != '_')
6647 *d++ = '_'; /* append a '_' */
6648 continue;
6649 }
6650 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6651 *d++ = '\\';
6652
6653 /*
6654 * Insert a backslash before a backslash after a slash, for search
6655 * pattern tags: "/\|" --> "/\\|".
6656 */
6657 else if (s[0] == '\\' && s[1] != '\\'
6658 && *arg == '/' && s == arg + 1)
6659 *d++ = '\\';
6660
6661 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6662 * "CTRL-\_CTRL-N" */
6663 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6664 {
6665 STRCPY(d, "CTRL-\\\\");
6666 d += 7;
6667 s += 6;
6668 }
6669
6670 *d++ = *s;
6671
6672 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006673 * If tag contains "({" or "([", tag terminates at the "(".
6674 * This is for help on functions, e.g.: abs({expr}).
6675 */
6676 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6677 break;
6678
6679 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 * If tag starts with ', toss everything after a second '. Fixes
6681 * CTRL-] on 'option'. (would include the trailing '.').
6682 */
6683 if (*s == '\'' && s > arg && *arg == '\'')
6684 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006685 /* Also '{' and '}'. */
6686 if (*s == '}' && s > arg && *arg == '{')
6687 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 }
6689 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006690
6691 if (*IObuff == '`')
6692 {
6693 if (d > IObuff + 2 && d[-1] == '`')
6694 {
6695 /* remove the backticks from `command` */
6696 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6697 d[-2] = NUL;
6698 }
6699 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6700 {
6701 /* remove the backticks and comma from `command`, */
6702 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6703 d[-3] = NUL;
6704 }
6705 else if (d > IObuff + 4 && d[-3] == '`'
6706 && d[-2] == '\\' && d[-1] == '.')
6707 {
6708 /* remove the backticks and dot from `command`\. */
6709 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6710 d[-4] = NUL;
6711 }
6712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714 }
6715
6716 *matches = (char_u **)"";
6717 *num_matches = 0;
6718 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6719 if (keep_lang)
6720 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006721 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 /* Sort the matches found on the heuristic number that is after the
6725 * tag name. */
6726 qsort((void *)*matches, (size_t)*num_matches,
6727 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006728 /* Delete more than TAG_MANY to reduce the size of the listing. */
6729 while (*num_matches > TAG_MANY)
6730 vim_free((*matches)[--*num_matches]);
6731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 return OK;
6733}
6734
6735/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006736 * Called when starting to edit a buffer for a help file.
6737 */
6738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006739prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006740{
6741 char_u *p;
6742
6743 curbuf->b_help = TRUE;
6744#ifdef FEAT_QUICKFIX
6745 set_string_option_direct((char_u *)"buftype", -1,
6746 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6747#endif
6748
6749 /*
6750 * Always set these options after jumping to a help tag, because the
6751 * user may have an autocommand that gets in the way.
6752 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6753 * latin1 word characters (for translated help files).
6754 * Only set it when needed, buf_init_chartab() is some work.
6755 */
6756 p =
6757#ifdef EBCDIC
6758 (char_u *)"65-255,^*,^|,^\"";
6759#else
6760 (char_u *)"!-~,^*,^|,^\",192-255";
6761#endif
6762 if (STRCMP(curbuf->b_p_isk, p) != 0)
6763 {
6764 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6765 check_buf_options(curbuf);
6766 (void)buf_init_chartab(curbuf, FALSE);
6767 }
6768
Bram Moolenaar0b105412014-11-30 13:34:23 +01006769#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006770 /* Don't use the global foldmethod.*/
6771 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6772 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006773#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006774
6775 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6776 curwin->w_p_list = FALSE; /* no list mode */
6777
6778 curbuf->b_p_ma = FALSE; /* not modifiable */
6779 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6780 curwin->w_p_nu = 0; /* no line numbers */
6781 curwin->w_p_rnu = 0; /* no relative line numbers */
6782 RESET_BINDING(curwin); /* no scroll or cursor binding */
6783#ifdef FEAT_ARABIC
6784 curwin->w_p_arab = FALSE; /* no arabic mode */
6785#endif
6786#ifdef FEAT_RIGHTLEFT
6787 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6788#endif
6789#ifdef FEAT_FOLDING
6790 curwin->w_p_fen = FALSE; /* No folding in the help window */
6791#endif
6792#ifdef FEAT_DIFF
6793 curwin->w_p_diff = FALSE; /* No 'diff' */
6794#endif
6795#ifdef FEAT_SPELL
6796 curwin->w_p_spell = FALSE; /* No spell checking */
6797#endif
6798
6799 set_buflisted(FALSE);
6800}
6801
6802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 * After reading a help file: May cleanup a help buffer when syntax
6804 * highlighting is not used.
6805 */
6806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006807fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 linenr_T lnum;
6810 char_u *line;
6811 int in_example = FALSE;
6812 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006813 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 char_u *p;
6815 char_u *rt;
6816 int mustfree;
6817
6818 /* set filetype to "help". */
6819 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6820
6821#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006822 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823#endif
6824 {
6825 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6826 {
6827 line = ml_get_buf(curbuf, lnum, FALSE);
6828 len = (int)STRLEN(line);
6829 if (in_example && len > 0 && !vim_iswhite(line[0]))
6830 {
6831 /* End of example: non-white or '<' in first column. */
6832 if (line[0] == '<')
6833 {
6834 /* blank-out a '<' in the first column */
6835 line = ml_get_buf(curbuf, lnum, TRUE);
6836 line[0] = ' ';
6837 }
6838 in_example = FALSE;
6839 }
6840 if (!in_example && len > 0)
6841 {
6842 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6843 {
6844 /* blank-out a '>' in the last column (start of example) */
6845 line = ml_get_buf(curbuf, lnum, TRUE);
6846 line[len - 1] = ' ';
6847 in_example = TRUE;
6848 }
6849 else if (line[len - 1] == '~')
6850 {
6851 /* blank-out a '~' at the end of line (header marker) */
6852 line = ml_get_buf(curbuf, lnum, TRUE);
6853 line[len - 1] = ' ';
6854 }
6855 }
6856 }
6857 }
6858
6859 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006860 * In the "help.txt" and "help.abx" file, add the locally added help
6861 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006863 fname = gettail(curbuf->b_fname);
6864 if (fnamecmp(fname, "help.txt") == 0
6865#ifdef FEAT_MULTI_LANG
6866 || (fnamencmp(fname, "help.", 5) == 0
6867 && ASCII_ISALPHA(fname[5])
6868 && ASCII_ISALPHA(fname[6])
6869 && TOLOWER_ASC(fname[7]) == 'x'
6870 && fname[8] == NUL)
6871#endif
6872 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {
6874 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6875 {
6876 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006877 if (strstr((char *)line, "*local-additions*") == NULL)
6878 continue;
6879
6880 /* Go through all directories in 'runtimepath', skipping
6881 * $VIMRUNTIME. */
6882 p = p_rtp;
6883 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006885 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6886 mustfree = FALSE;
6887 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
6888 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006890 int fcount;
6891 char_u **fnames;
6892 FILE *fd;
6893 char_u *s;
6894 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006896 vimconv_T vc;
6897 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898#endif
6899
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006900 /* Find all "doc/ *.txt" files in this directory. */
6901 add_pathsep(NameBuff);
6902#ifdef FEAT_MULTI_LANG
6903 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006905 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006907 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6908 &fnames, EW_FILE|EW_SILENT) == OK
6909 && fcount > 0)
6910 {
6911#ifdef FEAT_MULTI_LANG
6912 int i1;
6913 int i2;
6914 char_u *f1;
6915 char_u *f2;
6916 char_u *t1;
6917 char_u *e1;
6918 char_u *e2;
6919
6920 /* If foo.abx is found use it instead of foo.txt in
6921 * the same directory. */
6922 for (i1 = 0; i1 < fcount; ++i1)
6923 {
6924 for (i2 = 0; i2 < fcount; ++i2)
6925 {
6926 if (i1 == i2)
6927 continue;
6928 if (fnames[i1] == NULL || fnames[i2] == NULL)
6929 continue;
6930 f1 = fnames[i1];
6931 f2 = fnames[i2];
6932 t1 = gettail(f1);
6933 if (fnamencmp(f1, f2, t1 - f1) != 0)
6934 continue;
6935 e1 = vim_strrchr(t1, '.');
6936 e2 = vim_strrchr(gettail(f2), '.');
Bram Moolenaar7756e742016-10-21 20:35:37 +02006937 if (e1 == NULL || e2 == NULL)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006938 continue;
6939 if (fnamecmp(e1, ".txt") != 0
6940 && fnamecmp(e1, fname + 4) != 0)
6941 {
6942 /* Not .txt and not .abx, remove it. */
6943 vim_free(fnames[i1]);
6944 fnames[i1] = NULL;
6945 continue;
6946 }
6947 if (fnamencmp(f1, f2, e1 - f1) != 0)
6948 continue;
6949 if (fnamecmp(e1, ".txt") == 0
6950 && fnamecmp(e2, fname + 4) == 0)
6951 {
6952 /* use .abx instead of .txt */
6953 vim_free(fnames[i1]);
6954 fnames[i1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 }
6956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006958#endif
6959 for (fi = 0; fi < fcount; ++fi)
6960 {
6961 if (fnames[fi] == NULL)
6962 continue;
6963 fd = mch_fopen((char *)fnames[fi], "r");
6964 if (fd != NULL)
6965 {
6966 vim_fgets(IObuff, IOSIZE, fd);
6967 if (IObuff[0] == '*'
6968 && (s = vim_strchr(IObuff + 1, '*'))
6969 != NULL)
6970 {
6971#ifdef FEAT_MBYTE
6972 int this_utf = MAYBE;
6973#endif
6974 /* Change tag definition to a
6975 * reference and remove <CR>/<NL>. */
6976 IObuff[0] = '|';
6977 *s = '|';
6978 while (*s != NUL)
6979 {
6980 if (*s == '\r' || *s == '\n')
6981 *s = NUL;
6982#ifdef FEAT_MBYTE
6983 /* The text is utf-8 when a byte
6984 * above 127 is found and no
6985 * illegal byte sequence is found.
6986 */
6987 if (*s >= 0x80 && this_utf != FALSE)
6988 {
6989 int l;
6990
6991 this_utf = TRUE;
6992 l = utf_ptr2len(s);
6993 if (l == 1)
6994 this_utf = FALSE;
6995 s += l - 1;
6996 }
6997#endif
6998 ++s;
6999 }
7000#ifdef FEAT_MBYTE
7001 /* The help file is latin1 or utf-8;
7002 * conversion to the current
7003 * 'encoding' may be required. */
7004 vc.vc_type = CONV_NONE;
7005 convert_setup(&vc, (char_u *)(
7006 this_utf == TRUE ? "utf-8"
7007 : "latin1"), p_enc);
7008 if (vc.vc_type == CONV_NONE)
7009 /* No conversion needed. */
7010 cp = IObuff;
7011 else
7012 {
7013 /* Do the conversion. If it fails
7014 * use the unconverted text. */
7015 cp = string_convert(&vc, IObuff,
7016 NULL);
7017 if (cp == NULL)
7018 cp = IObuff;
7019 }
7020 convert_setup(&vc, NULL, NULL);
7021
7022 ml_append(lnum, cp, (colnr_T)0, FALSE);
7023 if (cp != IObuff)
7024 vim_free(cp);
7025#else
7026 ml_append(lnum, IObuff, (colnr_T)0,
7027 FALSE);
7028#endif
7029 ++lnum;
7030 }
7031 fclose(fd);
7032 }
7033 }
7034 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007037 if (mustfree)
7038 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007040 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 }
7042 }
7043}
7044
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007045/*
7046 * ":exusage"
7047 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007049ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007050{
7051 do_cmdline_cmd((char_u *)"help ex-cmd-index");
7052}
7053
7054/*
7055 * ":viusage"
7056 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007058ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007059{
7060 do_cmdline_cmd((char_u *)"help normal-index");
7061}
7062
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007064 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007067helptags_one(
7068 char_u *dir, /* doc directory */
7069 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
7070 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
7071 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 FILE *fd_tags;
7074 FILE *fd;
7075 garray_T ga;
7076 int filecount;
7077 char_u **files;
7078 char_u *p1, *p2;
7079 int fi;
7080 char_u *s;
7081 int i;
7082 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007083 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084# ifdef FEAT_MBYTE
7085 int utf8 = MAYBE;
7086 int this_utf8;
7087 int firstline;
7088 int mix = FALSE; /* detected mixed encodings */
7089# endif
7090
7091 /*
7092 * Find all *.txt files.
7093 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007094 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007096 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 STRCAT(NameBuff, ext);
7098 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7099 EW_FILE|EW_SILENT) == FAIL
7100 || filecount == 0)
7101 {
7102 if (!got_int)
Bram Moolenaar5b302912016-08-24 22:11:55 +02007103 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 return;
7105 }
7106
7107 /*
7108 * Open the tags file for writing.
7109 * Do this before scanning through all the files.
7110 */
7111 STRCPY(NameBuff, dir);
7112 add_pathsep(NameBuff);
7113 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007114 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 if (fd_tags == NULL)
7116 {
7117 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
7118 FreeWild(filecount, files);
7119 return;
7120 }
7121
7122 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007123 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7124 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 */
7126 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007127 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
7128 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {
7130 if (ga_grow(&ga, 1) == FAIL)
7131 got_int = TRUE;
7132 else
7133 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007134 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (s == NULL)
7136 got_int = TRUE;
7137 else
7138 {
7139 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7140 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7141 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
7143 }
7144 }
7145
7146 /*
7147 * Go over all the files and extract the tags.
7148 */
7149 for (fi = 0; fi < filecount && !got_int; ++fi)
7150 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007151 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (fd == NULL)
7153 {
7154 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
7155 continue;
7156 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007157 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159# ifdef FEAT_MBYTE
7160 firstline = TRUE;
7161# endif
7162 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7163 {
7164# ifdef FEAT_MBYTE
7165 if (firstline)
7166 {
7167 /* Detect utf-8 file by a non-ASCII char in the first line. */
7168 this_utf8 = MAYBE;
7169 for (s = IObuff; *s != NUL; ++s)
7170 if (*s >= 0x80)
7171 {
7172 int l;
7173
7174 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007175 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (l == 1)
7177 {
7178 /* Illegal UTF-8 byte sequence. */
7179 this_utf8 = FALSE;
7180 break;
7181 }
7182 s += l - 1;
7183 }
7184 if (this_utf8 == MAYBE) /* only ASCII characters found */
7185 this_utf8 = FALSE;
7186 if (utf8 == MAYBE) /* first file */
7187 utf8 = this_utf8;
7188 else if (utf8 != this_utf8)
7189 {
7190 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
7191 mix = !got_int;
7192 got_int = TRUE;
7193 }
7194 firstline = FALSE;
7195 }
7196# endif
7197 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7198 while (p1 != NULL)
7199 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007200 /* Use vim_strbyte() instead of vim_strchr() so that when
7201 * 'encoding' is dbcs it still works, don't find '*' in the
7202 * second byte. */
7203 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7205 {
7206 for (s = p1 + 1; s < p2; ++s)
7207 if (*s == ' ' || *s == '\t' || *s == '|')
7208 break;
7209
7210 /*
7211 * Only accept a *tag* when it consists of valid
7212 * characters, there is white space before it and is
7213 * followed by a white character or end-of-line.
7214 */
7215 if (s == p2
7216 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7217 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7218 || s[1] == '\0'))
7219 {
7220 *p2 = '\0';
7221 ++p1;
7222 if (ga_grow(&ga, 1) == FAIL)
7223 {
7224 got_int = TRUE;
7225 break;
7226 }
7227 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7228 if (s == NULL)
7229 {
7230 got_int = TRUE;
7231 break;
7232 }
7233 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7234 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 sprintf((char *)s, "%s\t%s", p1, fname);
7236
7237 /* find next '*' */
7238 p2 = vim_strchr(p2 + 1, '*');
7239 }
7240 }
7241 p1 = p2;
7242 }
7243 line_breakcheck();
7244 }
7245
7246 fclose(fd);
7247 }
7248
7249 FreeWild(filecount, files);
7250
7251 if (!got_int)
7252 {
7253 /*
7254 * Sort the tags.
7255 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007256 if (ga.ga_data != NULL)
7257 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 /*
7260 * Check for duplicates.
7261 */
7262 for (i = 1; i < ga.ga_len; ++i)
7263 {
7264 p1 = ((char_u **)ga.ga_data)[i - 1];
7265 p2 = ((char_u **)ga.ga_data)[i];
7266 while (*p1 == *p2)
7267 {
7268 if (*p2 == '\t')
7269 {
7270 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007271 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7273 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
7274 EMSG(NameBuff);
7275 *p2 = '\t';
7276 break;
7277 }
7278 ++p1;
7279 ++p2;
7280 }
7281 }
7282
7283# ifdef FEAT_MBYTE
7284 if (utf8 == TRUE)
7285 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
7286# endif
7287
7288 /*
7289 * Write the tags into the file.
7290 */
7291 for (i = 0; i < ga.ga_len; ++i)
7292 {
7293 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007294 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007296 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 else
7298 {
7299 fprintf(fd_tags, "%s\t/*", s);
7300 for (p1 = s; *p1 != '\t'; ++p1)
7301 {
7302 /* insert backslash before '\\' and '/' */
7303 if (*p1 == '\\' || *p1 == '/')
7304 putc('\\', fd_tags);
7305 putc(*p1, fd_tags);
7306 }
7307 fprintf(fd_tags, "*\n");
7308 }
7309 }
7310 }
7311#ifdef FEAT_MBYTE
7312 if (mix)
7313 got_int = FALSE; /* continue with other languages */
7314#endif
7315
7316 for (i = 0; i < ga.ga_len; ++i)
7317 vim_free(((char_u **)ga.ga_data)[i]);
7318 ga_clear(&ga);
7319 fclose(fd_tags); /* there is no check for an error... */
7320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007322/*
7323 * Generate tags in one help directory, taking care of translations.
7324 */
7325 static void
7326do_helptags(char_u *dirname, int add_help_tags)
7327{
7328#ifdef FEAT_MULTI_LANG
7329 int len;
7330 int i, j;
7331 garray_T ga;
7332 char_u lang[2];
7333 char_u ext[5];
7334 char_u fname[8];
7335 int filecount;
7336 char_u **files;
7337
7338 /* Get a list of all files in the help directory and in subdirectories. */
7339 STRCPY(NameBuff, dirname);
7340 add_pathsep(NameBuff);
7341 STRCAT(NameBuff, "**");
7342 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7343 EW_FILE|EW_SILENT) == FAIL
7344 || filecount == 0)
7345 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007346 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007347 return;
7348 }
7349
7350 /* Go over all files in the directory to find out what languages are
7351 * present. */
7352 ga_init2(&ga, 1, 10);
7353 for (i = 0; i < filecount; ++i)
7354 {
7355 len = (int)STRLEN(files[i]);
7356 if (len > 4)
7357 {
7358 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7359 {
7360 /* ".txt" -> language "en" */
7361 lang[0] = 'e';
7362 lang[1] = 'n';
7363 }
7364 else if (files[i][len - 4] == '.'
7365 && ASCII_ISALPHA(files[i][len - 3])
7366 && ASCII_ISALPHA(files[i][len - 2])
7367 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7368 {
7369 /* ".abx" -> language "ab" */
7370 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7371 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7372 }
7373 else
7374 continue;
7375
7376 /* Did we find this language already? */
7377 for (j = 0; j < ga.ga_len; j += 2)
7378 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7379 break;
7380 if (j == ga.ga_len)
7381 {
7382 /* New language, add it. */
7383 if (ga_grow(&ga, 2) == FAIL)
7384 break;
7385 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7386 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7387 }
7388 }
7389 }
7390
7391 /*
7392 * Loop over the found languages to generate a tags file for each one.
7393 */
7394 for (j = 0; j < ga.ga_len; j += 2)
7395 {
7396 STRCPY(fname, "tags-xx");
7397 fname[5] = ((char_u *)ga.ga_data)[j];
7398 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7399 if (fname[5] == 'e' && fname[6] == 'n')
7400 {
7401 /* English is an exception: use ".txt" and "tags". */
7402 fname[4] = NUL;
7403 STRCPY(ext, ".txt");
7404 }
7405 else
7406 {
7407 /* Language "ab" uses ".abx" and "tags-ab". */
7408 STRCPY(ext, ".xxx");
7409 ext[1] = fname[5];
7410 ext[2] = fname[6];
7411 }
7412 helptags_one(dirname, ext, fname, add_help_tags);
7413 }
7414
7415 ga_clear(&ga);
7416 FreeWild(filecount, files);
7417
7418#else
7419 /* No language support, just use "*.txt" and "tags". */
7420 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7421#endif
7422}
7423
7424 static void
7425helptags_cb(char_u *fname, void *cookie)
7426{
7427 do_helptags(fname, *(int *)cookie);
7428}
7429
7430/*
7431 * ":helptags"
7432 */
7433 void
7434ex_helptags(exarg_T *eap)
7435{
7436 expand_T xpc;
7437 char_u *dirname;
7438 int add_help_tags = FALSE;
7439
7440 /* Check for ":helptags ++t {dir}". */
7441 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3]))
7442 {
7443 add_help_tags = TRUE;
7444 eap->arg = skipwhite(eap->arg + 3);
7445 }
7446
7447 if (STRCMP(eap->arg, "ALL") == 0)
7448 {
7449 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7450 helptags_cb, &add_help_tags);
7451 }
7452 else
7453 {
7454 ExpandInit(&xpc);
7455 xpc.xp_context = EXPAND_DIRECTORIES;
7456 dirname = ExpandOne(&xpc, eap->arg, NULL,
7457 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7458 if (dirname == NULL || !mch_isdir(dirname))
7459 EMSG2(_("E150: Not a directory: %s"), eap->arg);
7460 else
7461 do_helptags(dirname, add_help_tags);
7462 vim_free(dirname);
7463 }
7464}
7465
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#if defined(FEAT_SIGNS) || defined(PROTO)
7467
7468/*
7469 * Struct to hold the sign properties.
7470 */
7471typedef struct sign sign_T;
7472
7473struct sign
7474{
7475 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007476 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 char_u *sn_name; /* name of sign */
7478 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007479# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 char_u *sn_text; /* text used instead of pixmap */
7483 int sn_line_hl; /* highlight ID for line */
7484 int sn_text_hl; /* highlight ID for text */
7485};
7486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007488static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007490static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
7491static void sign_list_defined(sign_T *sp);
7492static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007494static char *cmds[] = {
7495 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007496# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007497 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007498# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007499 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007500# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007501 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007502# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007503 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007504# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007505 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007506# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007507 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007508# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007509};
7510
7511/*
7512 * Find index of a ":sign" subcmd from its name.
7513 * "*end_cmd" must be writable.
7514 */
7515 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007516sign_cmd_idx(
7517 char_u *begin_cmd, /* begin of sign subcmd */
7518 char_u *end_cmd) /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007519{
7520 int idx;
7521 char save = *end_cmd;
7522
7523 *end_cmd = NUL;
7524 for (idx = 0; ; ++idx)
7525 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7526 break;
7527 *end_cmd = save;
7528 return idx;
7529}
7530
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531/*
7532 * ":sign" command
7533 */
7534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007535ex_sign(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 char_u *arg = eap->arg;
7538 char_u *p;
7539 int idx;
7540 sign_T *sp;
7541 sign_T *sp_prev;
7542 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
7544 /* Parse the subcommand. */
7545 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007546 idx = sign_cmd_idx(arg, p);
7547 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007549 EMSG2(_("E160: Unknown sign command: %s"), arg);
7550 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 }
7552 arg = skipwhite(p);
7553
7554 if (idx <= SIGNCMD_LIST)
7555 {
7556 /*
7557 * Define, undefine or list signs.
7558 */
7559 if (idx == SIGNCMD_LIST && *arg == NUL)
7560 {
7561 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007562 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 sign_list_defined(sp);
7564 }
7565 else if (*arg == NUL)
7566 EMSG(_("E156: Missing sign name"));
7567 else
7568 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007569 /* Isolate the sign name. If it's a number skip leading zeroes,
7570 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 p = skiptowhite(arg);
7572 if (*p != NUL)
7573 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007574 while (arg[0] == '0' && arg[1] != NUL)
7575 ++arg;
7576
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 sp_prev = NULL;
7578 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7579 {
7580 if (STRCMP(sp->sn_name, arg) == 0)
7581 break;
7582 sp_prev = sp;
7583 }
7584 if (idx == SIGNCMD_DEFINE)
7585 {
7586 /* ":sign define {name} ...": define a sign */
7587 if (sp == NULL)
7588 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007589 sign_T *lp;
7590 int start = next_sign_typenr;
7591
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 /* Allocate a new sign. */
7593 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7594 if (sp == NULL)
7595 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007597 /* Check that next_sign_typenr is not already being used.
7598 * This only happens after wrapping around. Hopefully
7599 * another one got deleted and we can use its number. */
7600 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007602 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007604 ++next_sign_typenr;
7605 if (next_sign_typenr == MAX_TYPENR)
7606 next_sign_typenr = 1;
7607 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007609 vim_free(sp);
7610 EMSG(_("E612: Too many signs defined"));
7611 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007613 lp = first_sign; /* start all over */
7614 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007616 lp = lp->sn_next;
7617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007619 sp->sn_typenr = next_sign_typenr;
7620 if (++next_sign_typenr == MAX_TYPENR)
7621 next_sign_typenr = 1; /* wrap around */
7622
7623 sp->sn_name = vim_strsave(arg);
7624 if (sp->sn_name == NULL) /* out of memory */
7625 {
7626 vim_free(sp);
7627 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007629
7630 /* add the new sign to the list of signs */
7631 if (sp_prev == NULL)
7632 first_sign = sp;
7633 else
7634 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636
7637 /* set values for a defined sign. */
7638 for (;;)
7639 {
7640 arg = skipwhite(p);
7641 if (*arg == NUL)
7642 break;
7643 p = skiptowhite_esc(arg);
7644 if (STRNCMP(arg, "icon=", 5) == 0)
7645 {
7646 arg += 5;
7647 vim_free(sp->sn_icon);
7648 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7649 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007650# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 if (gui.in_use)
7652 {
7653 out_flush();
7654 if (sp->sn_image != NULL)
7655 gui_mch_destroy_sign(sp->sn_image);
7656 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7657 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
7660 else if (STRNCMP(arg, "text=", 5) == 0)
7661 {
7662 char_u *s;
7663 int cells;
7664 int len;
7665
7666 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007667# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 /* Count cells and check for non-printable chars */
7669 if (has_mbyte)
7670 {
7671 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007672 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
7674 if (!vim_isprintc((*mb_ptr2char)(s)))
7675 break;
7676 cells += (*mb_ptr2cells)(s);
7677 }
7678 }
7679 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
7682 for (s = arg; s < p; ++s)
7683 if (!vim_isprintc(*s))
7684 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007685 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687 /* Currently must be one or two display cells */
7688 if (s != p || cells < 1 || cells > 2)
7689 {
7690 *p = NUL;
7691 EMSG2(_("E239: Invalid sign text: %s"), arg);
7692 return;
7693 }
7694
7695 vim_free(sp->sn_text);
7696 /* Allocate one byte more if we need to pad up
7697 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007698 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 sp->sn_text = vim_strnsave(arg, len);
7700
7701 if (sp->sn_text != NULL && cells == 1)
7702 STRCPY(sp->sn_text + len - 1, " ");
7703 }
7704 else if (STRNCMP(arg, "linehl=", 7) == 0)
7705 {
7706 arg += 7;
7707 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7708 }
7709 else if (STRNCMP(arg, "texthl=", 7) == 0)
7710 {
7711 arg += 7;
7712 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7713 }
7714 else
7715 {
7716 EMSG2(_(e_invarg2), arg);
7717 return;
7718 }
7719 }
7720 }
7721 else if (sp == NULL)
7722 EMSG2(_("E155: Unknown sign: %s"), arg);
7723 else if (idx == SIGNCMD_LIST)
7724 /* ":sign list {name}" */
7725 sign_list_defined(sp);
7726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007728 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
7730 }
7731 else
7732 {
7733 int id = -1;
7734 linenr_T lnum = -1;
7735 char_u *sign_name = NULL;
7736 char_u *arg1;
7737
7738 if (*arg == NUL)
7739 {
7740 if (idx == SIGNCMD_PLACE)
7741 {
7742 /* ":sign place": list placed signs in all buffers */
7743 sign_list_placed(NULL);
7744 }
7745 else if (idx == SIGNCMD_UNPLACE)
7746 {
7747 /* ":sign unplace": remove placed sign at cursor */
7748 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7749 if (id > 0)
7750 {
7751 buf_delsign(curwin->w_buffer, id);
7752 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7753 }
7754 else
7755 EMSG(_("E159: Missing sign number"));
7756 }
7757 else
7758 EMSG(_(e_argreq));
7759 return;
7760 }
7761
7762 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7763 {
7764 /* ":sign unplace *": remove all placed signs */
7765 buf_delete_all_signs();
7766 return;
7767 }
7768
7769 /* first arg could be placed sign id */
7770 arg1 = arg;
7771 if (VIM_ISDIGIT(*arg))
7772 {
7773 id = getdigits(&arg);
7774 if (!vim_iswhite(*arg) && *arg != NUL)
7775 {
7776 id = -1;
7777 arg = arg1;
7778 }
7779 else
7780 {
7781 arg = skipwhite(arg);
7782 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7783 {
7784 /* ":sign unplace {id}": remove placed sign by number */
Bram Moolenaar29323592016-07-24 22:04:11 +02007785 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if ((lnum = buf_delsign(buf, id)) != 0)
7787 update_debug_sign(buf, lnum);
7788 return;
7789 }
7790 }
7791 }
7792
7793 /*
7794 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7795 * Leave "arg" pointing to {fname}.
7796 */
7797 for (;;)
7798 {
7799 if (STRNCMP(arg, "line=", 5) == 0)
7800 {
7801 arg += 5;
7802 lnum = atoi((char *)arg);
7803 arg = skiptowhite(arg);
7804 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007805 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7806 {
7807 if (id != -1)
7808 {
7809 EMSG(_(e_invarg));
7810 return;
7811 }
7812 id = -2;
7813 arg = skiptowhite(arg + 1);
7814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 else if (STRNCMP(arg, "name=", 5) == 0)
7816 {
7817 arg += 5;
7818 sign_name = arg;
7819 arg = skiptowhite(arg);
7820 if (*arg != NUL)
7821 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007822 while (sign_name[0] == '0' && sign_name[1] != NUL)
7823 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825 else if (STRNCMP(arg, "file=", 5) == 0)
7826 {
7827 arg += 5;
7828 buf = buflist_findname(arg);
7829 break;
7830 }
7831 else if (STRNCMP(arg, "buffer=", 7) == 0)
7832 {
7833 arg += 7;
7834 buf = buflist_findnr((int)getdigits(&arg));
7835 if (*skipwhite(arg) != NUL)
7836 EMSG(_(e_trailing));
7837 break;
7838 }
7839 else
7840 {
7841 EMSG(_(e_invarg));
7842 return;
7843 }
7844 arg = skipwhite(arg);
7845 }
7846
7847 if (buf == NULL)
7848 {
7849 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7850 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007851 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
7853 if (lnum >= 0 || sign_name != NULL)
7854 EMSG(_(e_invarg));
7855 else
7856 /* ":sign place file={fname}": list placed signs in one file */
7857 sign_list_placed(buf);
7858 }
7859 else if (idx == SIGNCMD_JUMP)
7860 {
7861 /* ":sign jump {id} file={fname}" */
7862 if (lnum >= 0 || sign_name != NULL)
7863 EMSG(_(e_invarg));
7864 else if ((lnum = buf_findsign(buf, id)) > 0)
7865 { /* goto a sign ... */
7866 if (buf_jump_open_win(buf) != NULL)
7867 { /* ... in a current window */
7868 curwin->w_cursor.lnum = lnum;
7869 check_cursor_lnum();
7870 beginline(BL_WHITE);
7871 }
7872 else
7873 { /* ... not currently in a window */
7874 char_u *cmd;
7875
Bram Moolenaarbfd096d2016-08-17 22:29:09 +02007876 if (buf->b_fname == NULL)
7877 {
7878 EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
7879 return;
7880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7882 if (cmd == NULL)
7883 return;
7884 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7885 do_cmdline_cmd(cmd);
7886 vim_free(cmd);
7887 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007888# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 else
7893 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7894 }
7895 else if (idx == SIGNCMD_UNPLACE)
7896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 if (lnum >= 0 || sign_name != NULL)
7898 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007899 else if (id == -2)
7900 {
7901 /* ":sign unplace * file={fname}" */
7902 redraw_buf_later(buf, NOT_VALID);
7903 buf_delete_signs(buf);
7904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 else
7906 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007907 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 lnum = buf_delsign(buf, id);
7909 update_debug_sign(buf, lnum);
7910 }
7911 }
7912 /* idx == SIGNCMD_PLACE */
7913 else if (sign_name != NULL)
7914 {
7915 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7916 if (STRCMP(sp->sn_name, sign_name) == 0)
7917 break;
7918 if (sp == NULL)
7919 {
7920 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7921 return;
7922 }
7923 if (lnum > 0)
7924 /* ":sign place {id} line={lnum} name={name} file={fname}":
7925 * place a sign */
7926 buf_addsign(buf, id, lnum, sp->sn_typenr);
7927 else
7928 /* ":sign place {id} file={fname}": change sign type */
7929 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007930 if (lnum > 0)
7931 update_debug_sign(buf, lnum);
7932 else
7933 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935 else
7936 EMSG(_(e_invarg));
7937 }
7938}
7939
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007940# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941/*
7942 * Allocate the icons. Called when the GUI has started. Allows defining
7943 * signs before it starts.
7944 */
7945 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007946sign_gui_started(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 sign_T *sp;
7949
7950 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7951 if (sp->sn_icon != NULL)
7952 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7953}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
7956/*
7957 * List one sign.
7958 */
7959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007960sign_list_defined(sign_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 char_u *p;
7963
Bram Moolenaar555b2802005-05-19 21:08:39 +00007964 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (sp->sn_icon != NULL)
7966 {
7967 MSG_PUTS(" icon=");
7968 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007969# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (sp->sn_image == NULL)
7971 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007972# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 }
7976 if (sp->sn_text != NULL)
7977 {
7978 MSG_PUTS(" text=");
7979 msg_outtrans(sp->sn_text);
7980 }
7981 if (sp->sn_line_hl > 0)
7982 {
7983 MSG_PUTS(" linehl=");
7984 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
7985 if (p == NULL)
7986 MSG_PUTS("NONE");
7987 else
7988 msg_puts(p);
7989 }
7990 if (sp->sn_text_hl > 0)
7991 {
7992 MSG_PUTS(" texthl=");
7993 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
7994 if (p == NULL)
7995 MSG_PUTS("NONE");
7996 else
7997 msg_puts(p);
7998 }
7999}
8000
8001/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008002 * Undefine a sign and free its memory.
8003 */
8004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008005sign_undefine(sign_T *sp, sign_T *sp_prev)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008006{
8007 vim_free(sp->sn_name);
8008 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008009# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008010 if (sp->sn_image != NULL)
8011 {
8012 out_flush();
8013 gui_mch_destroy_sign(sp->sn_image);
8014 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008015# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008016 vim_free(sp->sn_text);
8017 if (sp_prev == NULL)
8018 first_sign = sp->sn_next;
8019 else
8020 sp_prev->sn_next = sp->sn_next;
8021 vim_free(sp);
8022}
8023
8024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 * Get highlighting attribute for sign "typenr".
8026 * If "line" is TRUE: line highl, if FALSE: text highl.
8027 */
8028 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029sign_get_attr(int typenr, int line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 sign_T *sp;
8032
8033 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8034 if (sp->sn_typenr == typenr)
8035 {
8036 if (line)
8037 {
8038 if (sp->sn_line_hl > 0)
8039 return syn_id2attr(sp->sn_line_hl);
8040 }
8041 else
8042 {
8043 if (sp->sn_text_hl > 0)
8044 return syn_id2attr(sp->sn_text_hl);
8045 }
8046 break;
8047 }
8048 return 0;
8049}
8050
8051/*
8052 * Get text mark for sign "typenr".
8053 * Returns NULL if there isn't one.
8054 */
8055 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056sign_get_text(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 sign_T *sp;
8059
8060 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8061 if (sp->sn_typenr == typenr)
8062 return sp->sn_text;
8063 return NULL;
8064}
8065
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008066# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008068sign_get_image(
8069 int typenr) /* the attribute which may have a sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 sign_T *sp;
8072
8073 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8074 if (sp->sn_typenr == typenr)
8075 return sp->sn_image;
8076 return NULL;
8077}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080/*
8081 * Get the name of a sign by its typenr.
8082 */
8083 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084sign_typenr2name(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 sign_T *sp;
8087
8088 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8089 if (sp->sn_typenr == typenr)
8090 return sp->sn_name;
8091 return (char_u *)_("[Deleted]");
8092}
8093
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008094# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008095/*
8096 * Undefine/free all signs.
8097 */
8098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008099free_signs(void)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008100{
8101 while (first_sign != NULL)
8102 sign_undefine(first_sign, NULL);
8103}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008104# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008105
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008106# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008107static enum
8108{
8109 EXP_SUBCMD, /* expand :sign sub-commands */
8110 EXP_DEFINE, /* expand :sign define {name} args */
8111 EXP_PLACE, /* expand :sign place {id} args */
8112 EXP_UNPLACE, /* expand :sign unplace" */
8113 EXP_SIGN_NAMES /* expand with name of placed signs */
8114} expand_what;
8115
8116/*
8117 * Function given to ExpandGeneric() to obtain the sign command
8118 * expansion.
8119 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008120 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008121get_sign_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008122{
8123 sign_T *sp;
8124 int current_idx;
8125
8126 switch (expand_what)
8127 {
8128 case EXP_SUBCMD:
8129 return (char_u *)cmds[idx];
8130 case EXP_DEFINE:
8131 {
8132 char *define_arg[] =
8133 {
8134 "icon=", "linehl=", "text=", "texthl=", NULL
8135 };
8136 return (char_u *)define_arg[idx];
8137 }
8138 case EXP_PLACE:
8139 {
8140 char *place_arg[] =
8141 {
8142 "line=", "name=", "file=", "buffer=", NULL
8143 };
8144 return (char_u *)place_arg[idx];
8145 }
8146 case EXP_UNPLACE:
8147 {
8148 char *unplace_arg[] = { "file=", "buffer=", NULL };
8149 return (char_u *)unplace_arg[idx];
8150 }
8151 case EXP_SIGN_NAMES:
8152 /* Complete with name of signs already defined */
8153 current_idx = 0;
8154 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8155 if (current_idx++ == idx)
8156 return sp->sn_name;
8157 return NULL;
8158 default:
8159 return NULL;
8160 }
8161}
8162
8163/*
8164 * Handle command line completion for :sign command.
8165 */
8166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008167set_context_in_sign_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008168{
8169 char_u *p;
8170 char_u *end_subcmd;
8171 char_u *last;
8172 int cmd_idx;
8173 char_u *begin_subcmd_args;
8174
8175 /* Default: expand subcommands. */
8176 xp->xp_context = EXPAND_SIGN;
8177 expand_what = EXP_SUBCMD;
8178 xp->xp_pattern = arg;
8179
8180 end_subcmd = skiptowhite(arg);
8181 if (*end_subcmd == NUL)
8182 /* expand subcmd name
8183 * :sign {subcmd}<CTRL-D>*/
8184 return;
8185
8186 cmd_idx = sign_cmd_idx(arg, end_subcmd);
8187
8188 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008189 * |
8190 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008191 begin_subcmd_args = skipwhite(end_subcmd);
8192 p = skiptowhite(begin_subcmd_args);
8193 if (*p == NUL)
8194 {
8195 /*
8196 * Expand first argument of subcmd when possible.
8197 * For ":jump {id}" and ":unplace {id}", we could
8198 * possibly expand the ids of all signs already placed.
8199 */
8200 xp->xp_pattern = begin_subcmd_args;
8201 switch (cmd_idx)
8202 {
8203 case SIGNCMD_LIST:
8204 case SIGNCMD_UNDEFINE:
8205 /* :sign list <CTRL-D>
8206 * :sign undefine <CTRL-D> */
8207 expand_what = EXP_SIGN_NAMES;
8208 break;
8209 default:
8210 xp->xp_context = EXPAND_NOTHING;
8211 }
8212 return;
8213 }
8214
8215 /* expand last argument of subcmd */
8216
8217 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008218 * |
8219 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008220
8221 /* Loop until reaching last argument. */
8222 do
8223 {
8224 p = skipwhite(p);
8225 last = p;
8226 p = skiptowhite(p);
8227 } while (*p != NUL);
8228
8229 p = vim_strchr(last, '=');
8230
8231 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008232 * | |
8233 * last p */
Bram Moolenaar7756e742016-10-21 20:35:37 +02008234 if (p == NULL)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008235 {
8236 /* Expand last argument name (before equal sign). */
8237 xp->xp_pattern = last;
8238 switch (cmd_idx)
8239 {
8240 case SIGNCMD_DEFINE:
8241 expand_what = EXP_DEFINE;
8242 break;
8243 case SIGNCMD_PLACE:
8244 expand_what = EXP_PLACE;
8245 break;
8246 case SIGNCMD_JUMP:
8247 case SIGNCMD_UNPLACE:
8248 expand_what = EXP_UNPLACE;
8249 break;
8250 default:
8251 xp->xp_context = EXPAND_NOTHING;
8252 }
8253 }
8254 else
8255 {
8256 /* Expand last argument value (after equal sign). */
8257 xp->xp_pattern = p + 1;
8258 switch (cmd_idx)
8259 {
8260 case SIGNCMD_DEFINE:
8261 if (STRNCMP(last, "texthl", p - last) == 0 ||
8262 STRNCMP(last, "linehl", p - last) == 0)
8263 xp->xp_context = EXPAND_HIGHLIGHT;
8264 else if (STRNCMP(last, "icon", p - last) == 0)
8265 xp->xp_context = EXPAND_FILES;
8266 else
8267 xp->xp_context = EXPAND_NOTHING;
8268 break;
8269 case SIGNCMD_PLACE:
8270 if (STRNCMP(last, "name", p - last) == 0)
8271 expand_what = EXP_SIGN_NAMES;
8272 else
8273 xp->xp_context = EXPAND_NOTHING;
8274 break;
8275 default:
8276 xp->xp_context = EXPAND_NOTHING;
8277 }
8278 }
8279}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008280# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008281#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008282
8283/*
8284 * Make the user happy.
8285 */
8286 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008287ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008288{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008289 static char *code[] = {
8290 "\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$",
8291 "\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"
8292 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008293 char *p;
8294 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008295 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008296
8297 msg_start();
8298 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008299 for (i = 0; i < 2; ++i)
8300 for (p = code[i]; *p != NUL; ++p)
8301 if (*p == 'x')
8302 msg_putchar('\n');
8303 else
8304 for (n = *p++; n > 0; --n)
8305 if (*p == 'o' || *p == '$')
8306 msg_putchar_attr(*p, hl_attr(HLF_L));
8307 else
8308 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008309 msg_clr_eos();
8310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
8312#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
8313/*
8314 * ":drop"
8315 * Opens the first argument in a window. When there are two or more arguments
8316 * the argument list is redefined.
8317 */
8318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 win_T *wp;
8323 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008324# ifdef FEAT_WINDOWS
8325 tabpage_T *tp;
8326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327
8328 /*
8329 * Check if the first argument is already being edited in a window. If
8330 * so, jump to that window.
8331 * We would actually need to check all arguments, but that's complicated
8332 * and mostly only one file is dropped.
8333 * This also ignores wildcards, since it is very unlikely the user is
8334 * editing a file name with a wildcard character.
8335 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008336 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00008338 /*
8339 * Expanding wildcards may result in an empty argument list. E.g. when
8340 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
8341 * already did an error message for this.
8342 */
8343 if (ARGCOUNT == 0)
8344 return;
8345
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008346# ifdef FEAT_WINDOWS
8347 if (cmdmod.tab)
8348 {
8349 /* ":tab drop file ...": open a tab for each argument that isn't
8350 * edited in a window yet. It's like ":tab all" but without closing
8351 * windows or tabs. */
8352 ex_all(eap);
8353 }
8354 else
8355# endif
8356 {
8357 /* ":drop file ...": Edit the first argument. Jump to an existing
8358 * window if possible, edit in current window if the current buffer
8359 * can be abandoned, otherwise open a new window. */
8360 buf = buflist_findnr(ARGLIST[0].ae_fnum);
8361
8362 FOR_ALL_TAB_WINDOWS(tp, wp)
8363 {
8364 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008366# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008367 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 return;
8371 }
8372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008374 /*
8375 * Check whether the current buffer is changed. If so, we will need
8376 * to split the current window or data could be lost.
8377 * Skip the check if the 'hidden' option is set, as in this case the
8378 * buffer won't be lost.
8379 */
8380 if (!P_HID(curbuf))
8381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008383 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384# endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008385 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008387 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008389 if (split)
8390 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008394 /* Fake a ":sfirst" or ":first" command edit the first argument. */
8395 if (split)
8396 {
8397 eap->cmdidx = CMD_sfirst;
8398 eap->cmd[0] = 's';
8399 }
8400 else
8401 eap->cmdidx = CMD_first;
8402 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
8405#endif
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008406
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008407/*
8408 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
8409 * Put the start of the pattern in "*s", unless "s" is NULL.
8410 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
8411 * If "s" is not NULL terminate the pattern with a NUL.
8412 * Return a pointer to the char just past the pattern plus flags.
8413 */
8414 char_u *
8415skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
8416{
8417 int c;
8418
8419 if (vim_isIDc(*p))
8420 {
8421 /* ":vimgrep pattern fname" */
8422 if (s != NULL)
8423 *s = p;
8424 p = skiptowhite(p);
8425 if (s != NULL && *p != NUL)
8426 *p++ = NUL;
8427 }
8428 else
8429 {
8430 /* ":vimgrep /pattern/[g][j] fname" */
8431 if (s != NULL)
8432 *s = p + 1;
8433 c = *p;
8434 p = skip_regexp(p + 1, c, TRUE, NULL);
8435 if (*p != c)
8436 return NULL;
8437
8438 /* Truncate the pattern. */
8439 if (s != NULL)
8440 *p = NUL;
8441 ++p;
8442
8443 /* Find the flags */
8444 while (*p == 'g' || *p == 'j')
8445 {
8446 if (flags != NULL)
8447 {
8448 if (*p == 'g')
8449 *flags |= VGR_GLOBAL;
8450 else
8451 *flags |= VGR_NOJUMP;
8452 }
8453 ++p;
8454 }
8455 }
8456 return p;
8457}
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008458
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008459#if defined(FEAT_EVAL) || defined(PROTO)
8460/*
8461 * List v:oldfiles in a nice way.
8462 */
8463 void
8464ex_oldfiles(exarg_T *eap UNUSED)
8465{
8466 list_T *l = get_vim_var_list(VV_OLDFILES);
8467 listitem_T *li;
8468 int nr = 0;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008469 char_u *fname;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008470
8471 if (l == NULL)
8472 msg((char_u *)_("No old files"));
8473 else
8474 {
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008475 msg_start();
8476 msg_scroll = TRUE;
8477 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8478 {
8479 ++nr;
8480 fname = get_tv_string(&li->li_tv);
Bram Moolenaard6f2ee32016-08-24 00:30:52 +02008481 if (!message_filtered(fname))
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008482 {
8483 msg_outnum((long)nr);
8484 MSG_PUTS(": ");
8485 msg_outtrans(fname);
Bram Moolenaar885c00e2016-08-28 17:08:17 +02008486 msg_clr_eos();
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008487 msg_putchar('\n');
8488 out_flush(); /* output one line at a time */
8489 ui_breakcheck();
8490 }
8491 }
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008492
8493 /* Assume "got_int" was set to truncate the listing. */
8494 got_int = FALSE;
8495
8496# ifdef FEAT_BROWSE_CMD
8497 if (cmdmod.browse)
8498 {
8499 quit_more = FALSE;
8500 nr = prompt_for_number(FALSE);
8501 msg_starthere();
8502 if (nr > 0)
8503 {
8504 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8505 (long)nr);
8506
8507 if (p != NULL)
8508 {
8509 p = expand_env_save(p);
8510 eap->arg = p;
8511 eap->cmdidx = CMD_edit;
8512 cmdmod.browse = FALSE;
8513 do_exedit(eap, NULL);
8514 vim_free(p);
8515 }
8516 }
8517 }
8518# endif
8519 }
8520}
8521#endif
8522