blob: bee50c5955f2f23c06d6dde6ed513250d45affba [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 Moolenaarf28dbce2016-01-29 22:03:47 +010031static void delbuf_msg(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static int
33#ifdef __BORLANDC__
34 _RTLENTRYF
35#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036 help_compare(const void *s1, const void *s2);
37static void prepare_help_buffer(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
40 * ":ascii" and "ga".
41 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010043do_ascii(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000046 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 char buf1[20];
48 char buf2[20];
49 char_u buf3[7];
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010050#ifdef FEAT_DIGRAPHS
51 char_u *dig;
52#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000054 int cc[MAX_MCO];
55 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 int len;
57
58 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000059 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 else
61#endif
62 c = gchar_cursor();
63 if (c == NUL)
64 {
65 MSG("NUL");
66 return;
67 }
68
69#ifdef FEAT_MBYTE
70 IObuff[0] = NUL;
71 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
72#endif
73 {
74 if (c == NL) /* NUL is stored as NL */
75 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000076 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
77 cval = NL; /* NL is stored as CR */
78 else
79 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 if (vim_isprintc_strict(c) && (c < ' '
81#ifndef EBCDIC
82 || c > '~'
83#endif
84 ))
85 {
86 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000087 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 }
89 else
90 buf1[0] = NUL;
91#ifndef EBCDIC
92 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000093 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
94 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 else
96#endif
97 buf2[0] = NUL;
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010098#ifdef FEAT_DIGRAPHS
99 dig = get_digraph_for_char(cval);
100 if (dig != NULL)
101 vim_snprintf((char *)IObuff, IOSIZE,
102 _("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"),
103 transchar(c), buf1, buf2, cval, cval, cval, dig);
104 else
105#endif
106 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar555b2802005-05-19 21:08:39 +0000107 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +0000108 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#ifdef FEAT_MBYTE
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000110 if (enc_utf8)
111 c = cc[ci++];
112 else
113 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
115 }
116
117#ifdef FEAT_MBYTE
118 /* Repeat for combining characters. */
119 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
120 {
121 len = (int)STRLEN(IObuff);
122 /* This assumes every multi-byte char is printable... */
123 if (len > 0)
124 IObuff[len++] = ' ';
125 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000126 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000127# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 )
131 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000132 len += (*mb_char2bytes)(c, IObuff + len);
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100133#ifdef FEAT_DIGRAPHS
134 dig = get_digraph_for_char(c);
135 if (dig != NULL)
136 vim_snprintf((char *)IObuff + len, IOSIZE - len,
137 c < 0x10000 ? _("> %d, Hex %04x, Oct %o, Digr %s")
138 : _("> %d, Hex %08x, Oct %o, Digr %s"),
139 c, c, c, dig);
140 else
141#endif
142 vim_snprintf((char *)IObuff + len, IOSIZE - len,
143 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
144 : _("> %d, Hex %08x, Octal %o"),
145 c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000146 if (ci == MAX_MCO)
147 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000148 if (enc_utf8)
149 c = cc[ci++];
150 else
151 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 }
153#endif
154
155 msg(IObuff);
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * ":left", ":center" and ":right": align text.
160 */
161 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100162ex_align(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
164 pos_T save_curpos;
165 int len;
166 int indent = 0;
167 int new_indent;
168 int has_tab;
169 int width;
170
171#ifdef FEAT_RIGHTLEFT
172 if (curwin->w_p_rl)
173 {
174 /* switch left and right aligning */
175 if (eap->cmdidx == CMD_right)
176 eap->cmdidx = CMD_left;
177 else if (eap->cmdidx == CMD_left)
178 eap->cmdidx = CMD_right;
179 }
180#endif
181
182 width = atoi((char *)eap->arg);
183 save_curpos = curwin->w_cursor;
184 if (eap->cmdidx == CMD_left) /* width is used for new indent */
185 {
186 if (width >= 0)
187 indent = width;
188 }
189 else
190 {
191 /*
192 * if 'textwidth' set, use it
193 * else if 'wrapmargin' set, use it
194 * if invalid value, use 80
195 */
196 if (width <= 0)
197 width = curbuf->b_p_tw;
198 if (width == 0 && curbuf->b_p_wm > 0)
Bram Moolenaar02631462017-09-22 15:20:32 +0200199 width = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 if (width <= 0)
201 width = 80;
202 }
203
204 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
205 return;
206
207 for (curwin->w_cursor.lnum = eap->line1;
208 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
209 {
210 if (eap->cmdidx == CMD_left) /* left align */
211 new_indent = indent;
212 else
213 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000214 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 len = linelen(eap->cmdidx == CMD_right ? &has_tab
216 : NULL) - get_indent();
217
218 if (len <= 0) /* skip blank lines */
219 continue;
220
221 if (eap->cmdidx == CMD_center)
222 new_indent = (width - len) / 2;
223 else
224 {
225 new_indent = width - len; /* right align */
226
227 /*
228 * Make sure that embedded TABs don't make the text go too far
229 * to the right.
230 */
231 if (has_tab)
232 while (new_indent > 0)
233 {
234 (void)set_indent(new_indent, 0);
235 if (linelen(NULL) <= width)
236 {
237 /*
238 * Now try to move the line as much as possible to
239 * the right. Stop when it moves too far.
240 */
241 do
242 (void)set_indent(++new_indent, 0);
243 while (linelen(NULL) <= width);
244 --new_indent;
245 break;
246 }
247 --new_indent;
248 }
249 }
250 }
251 if (new_indent < 0)
252 new_indent = 0;
253 (void)set_indent(new_indent, 0); /* set indent */
254 }
255 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
256 curwin->w_cursor = save_curpos;
257 beginline(BL_WHITE | BL_FIX);
258}
259
260/*
261 * Get the length of the current line, excluding trailing white space.
262 */
263 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100264linelen(int *has_tab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 char_u *line;
267 char_u *first;
268 char_u *last;
269 int save;
270 int len;
271
272 /* find the first non-blank character */
273 line = ml_get_curline();
274 first = skipwhite(line);
275
276 /* find the character after the last non-blank character */
277 for (last = first + STRLEN(first);
Bram Moolenaar1c465442017-03-12 20:10:05 +0100278 last > first && VIM_ISWHITE(last[-1]); --last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 ;
280 save = *last;
281 *last = NUL;
282 len = linetabsize(line); /* get line length */
283 if (has_tab != NULL) /* check for embedded TAB */
Bram Moolenaarb31a3ac2018-08-11 14:41:55 +0200284 *has_tab = (vim_strchr(first, TAB) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 *last = save;
286
287 return len;
288}
289
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000290/* Buffer for two lines used during sorting. They are allocated to
291 * contain the longest line being sorted. */
292static char_u *sortbuf1;
293static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000294
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100295static int sort_ic; /* ignore case */
296static int sort_nr; /* sort on number */
297static int sort_rx; /* sort on regex instead of skipping it */
298#ifdef FEAT_FLOAT
299static int sort_flt; /* sort on floating number */
300#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000301
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100302static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000303
304/* Struct to store info to be sorted. */
305typedef struct
306{
307 linenr_T lnum; /* line number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100308 union {
309 struct
310 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200311 varnumber_T start_col_nr; /* starting column number */
312 varnumber_T end_col_nr; /* ending column number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100313 } line;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200314 varnumber_T value; /* value if sorting by integer */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100315#ifdef FEAT_FLOAT
316 float_T value_flt; /* value if sorting by float */
317#endif
318 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000319} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000320
321static int
322#ifdef __BORLANDC__
323_RTLENTRYF
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325sort_compare(const void *s1, const void *s2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000326
327 static int
328#ifdef __BORLANDC__
329_RTLENTRYF
330#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100331sort_compare(const void *s1, const void *s2)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000332{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000333 sorti_T l1 = *(sorti_T *)s1;
334 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000335 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000336
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000338 * if we return 0 every time, qsort will assume it's done sorting and
339 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000340 if (sort_abort)
341 return 0;
342 fast_breakcheck();
343 if (got_int)
344 sort_abort = TRUE;
345
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000346 /* When sorting numbers "start_col_nr" is the number, not the column
347 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000348 if (sort_nr)
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100349 result = l1.st_u.value == l2.st_u.value ? 0
350 : l1.st_u.value > l2.st_u.value ? 1 : -1;
351#ifdef FEAT_FLOAT
352 else if (sort_flt)
353 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
354 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
355#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000356 else
357 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000358 /* We need to copy one line into "sortbuf1", because there is no
359 * guarantee that the first pointer becomes invalid when obtaining the
360 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100361 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
362 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
363 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
364 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
365 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
366 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000367
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000368 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
369 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000370 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000371
372 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000373 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000374 return (int)(l1.lnum - l2.lnum);
375 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000376}
377
378/*
379 * ":sort".
380 */
381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100382ex_sort(exarg_T *eap)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000383{
384 regmatch_T regmatch;
385 int len;
386 linenr_T lnum;
387 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000388 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000389 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000390 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000391 char_u *p;
392 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000393 char_u *s2;
394 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000395 int unique = FALSE;
396 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000397 colnr_T start_col;
398 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100399 int sort_what = 0;
400 int format_found = 0;
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200401 int change_occurred = FALSE; // Buffer contents changed.
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000402
Bram Moolenaar48c99162008-02-18 18:42:52 +0000403 /* Sorting one line is really quick! */
404 if (count <= 1)
405 return;
406
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000407 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
408 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000409 sortbuf1 = NULL;
410 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000411 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000412 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000413 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000414 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000415
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100416 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100417#ifdef FEAT_FLOAT
418 sort_flt = 0;
419#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000420
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000421 for (p = eap->arg; *p != NUL; ++p)
422 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100423 if (VIM_ISWHITE(*p))
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000424 ;
425 else if (*p == 'i')
426 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000427 else if (*p == 'r')
428 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000429 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100430 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100431 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100432 ++format_found;
433 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100434#ifdef FEAT_FLOAT
435 else if (*p == 'f')
436 {
437 sort_flt = 1;
438 ++format_found;
439 }
440#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100441 else if (*p == 'b')
442 {
443 sort_what = STR2NR_BIN + STR2NR_FORCE;
444 ++format_found;
445 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000446 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100447 {
448 sort_what = STR2NR_OCT + STR2NR_FORCE;
449 ++format_found;
450 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000451 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100452 {
453 sort_what = STR2NR_HEX + STR2NR_FORCE;
454 ++format_found;
455 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000456 else if (*p == 'u')
457 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000458 else if (*p == '"') /* comment start */
459 break;
460 else if (check_nextcmd(p) != NULL)
461 {
462 eap->nextcmd = check_nextcmd(p);
463 break;
464 }
465 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000466 {
467 s = skip_regexp(p + 1, *p, TRUE, NULL);
468 if (*s != *p)
469 {
470 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000471 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000472 }
473 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000474 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100475 if (s == p + 1)
476 {
477 if (last_search_pat() == NULL)
478 {
479 EMSG(_(e_noprevre));
480 goto sortend;
481 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000482 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100483 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000484 else
485 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000486 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000487 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000488 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000489 regmatch.rm_ic = p_ic;
490 }
491 else
492 {
493 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000494 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000495 }
496 }
497
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100498 /* Can only have one of 'n', 'b', 'o' and 'x'. */
499 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000500 {
501 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000502 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000503 }
504
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100505 /* From here on "sort_nr" is used as a flag for any integer number
506 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100507 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000508
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000509 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000510 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000511 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000512 * When sorting on strings "start_col_nr" is the offset in the line, for
513 * numbers sorting it's the number to sort on. This means the pattern
514 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000515 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000516 */
517 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
518 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000519 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000520 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000521 if (maxlen < len)
522 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000523
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000524 start_col = 0;
525 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000526 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000527 {
528 if (sort_rx)
529 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000530 start_col = (colnr_T)(regmatch.startp[0] - s);
531 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000532 }
533 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000534 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000535 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000536 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000537 if (regmatch.regprog != NULL)
538 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000539
Bram Moolenaar937204a2016-01-30 23:37:38 +0100540 if (sort_nr
541#ifdef FEAT_FLOAT
542 || sort_flt
543#endif
544 )
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000545 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000546 /* Make sure vim_str2nr doesn't read any digits past the end
547 * of the match, by temporarily terminating the string there */
548 s2 = s + end_col;
549 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200550 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000551 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000552 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100553 if (sort_nr)
554 {
555 if (sort_what & STR2NR_HEX)
556 s = skiptohex(p);
557 else if (sort_what & STR2NR_BIN)
558 s = skiptobin(p);
559 else
560 s = skiptodigit(p);
561 if (s > p && s[-1] == '-')
562 --s; /* include preceding negative sign */
563 if (*s == NUL)
564 /* empty line should sort before any number */
565 nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
566 else
567 vim_str2nr(s, NULL, NULL, sort_what,
568 &nrs[lnum - eap->line1].st_u.value, NULL, 0);
569 }
570#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000571 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100572 {
573 s = skipwhite(p);
574 if (*s == '+')
575 s = skipwhite(s + 1);
576
577 if (*s == NUL)
578 /* empty line should sort before any number */
579 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
580 else
581 nrs[lnum - eap->line1].st_u.value_flt =
582 strtod((char *)s, NULL);
583 }
584#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200585 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000586 }
587 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000588 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000589 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100590 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
591 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000592 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000593
594 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000595
596 if (regmatch.regprog != NULL)
597 fast_breakcheck();
598 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000599 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000600 }
601
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000602 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000603 sortbuf1 = alloc((unsigned)maxlen + 1);
604 if (sortbuf1 == NULL)
605 goto sortend;
606 sortbuf2 = alloc((unsigned)maxlen + 1);
607 if (sortbuf2 == NULL)
608 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000609
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000610 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000611 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000612
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000613 if (sort_abort)
614 goto sortend;
615
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000616 /* Insert the lines in the sorted order below the last one. */
617 lnum = eap->line2;
618 for (i = 0; i < count; ++i)
619 {
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200620 linenr_T get_lnum = nrs[eap->forceit ? count - i - 1 : i].lnum;
621
622 // If the original line number of the line being placed is not the same
623 // as "lnum" (accounting for offset), we know that the buffer changed.
624 if (get_lnum + ((linenr_T)count - 1) != lnum)
625 change_occurred = TRUE;
626
627 s = ml_get(get_lnum);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000628 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000629 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000630 {
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200631 // Copy the line into a buffer, it may become invalid in
632 // ml_append(). And it's needed for "unique".
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100633 STRCPY(sortbuf1, s);
634 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000635 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000636 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000637 fast_breakcheck();
638 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000639 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000640 }
641
642 /* delete the original lines if appending worked */
643 if (i == count)
644 for (i = 0; i < count; ++i)
645 ml_delete(eap->line1, FALSE);
646 else
647 count = 0;
648
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000649 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000650 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000651 if (deleted > 0)
652 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
653 else if (deleted < 0)
654 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200655
656 if (change_occurred || deleted != 0)
657 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000658
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000659 curwin->w_cursor.lnum = eap->line1;
660 beginline(BL_WHITE | BL_FIX);
661
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000662sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000663 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000664 vim_free(sortbuf1);
665 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200666 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000667 if (got_int)
668 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000669}
670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671/*
672 * ":retab".
673 */
674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100675ex_retab(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 linenr_T lnum;
678 int got_tab = FALSE;
679 long num_spaces = 0;
680 long num_tabs;
681 long len;
682 long col;
683 long vcol;
684 long start_col = 0; /* For start of white-space string */
685 long start_vcol = 0; /* For start of white-space string */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 long old_len;
687 char_u *ptr;
688 char_u *new_line = (char_u *)1; /* init to non-NULL */
689 int did_undo; /* called u_save for current line */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200690#ifdef FEAT_VARTABS
Bram Moolenaar0119a592018-06-24 23:53:28 +0200691 int *new_vts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200692 char_u *new_ts_str; /* string value of tab argument */
693#else
694 int temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 int new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 int save_list;
698 linenr_T first_line = 0; /* first changed line */
699 linenr_T last_line = 0; /* last changed line */
700
701 save_list = curwin->w_p_list;
702 curwin->w_p_list = 0; /* don't want list mode here */
703
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200704#ifdef FEAT_VARTABS
705 new_ts_str = eap->arg;
Bram Moolenaar0119a592018-06-24 23:53:28 +0200706 if (!tabstop_set(eap->arg, &new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200707 return;
708 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
709 ++(eap->arg);
710
Bram Moolenaar0119a592018-06-24 23:53:28 +0200711 // This ensures that either new_vts_array and new_ts_str are freshly
712 // allocated, or new_vts_array points to an existing array and new_ts_str
713 // is null.
714 if (new_vts_array == NULL)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200715 {
Bram Moolenaar0119a592018-06-24 23:53:28 +0200716 new_vts_array = curbuf->b_p_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200717 new_ts_str = NULL;
718 }
719 else
720 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
721#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 new_ts = getdigits(&(eap->arg));
723 if (new_ts < 0)
724 {
725 EMSG(_(e_positive));
726 return;
727 }
728 if (new_ts == 0)
729 new_ts = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
732 {
733 ptr = ml_get(lnum);
734 col = 0;
735 vcol = 0;
736 did_undo = FALSE;
737 for (;;)
738 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100739 if (VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
741 if (!got_tab && num_spaces == 0)
742 {
743 /* First consecutive white-space */
744 start_vcol = vcol;
745 start_col = col;
746 }
747 if (ptr[col] == ' ')
748 num_spaces++;
749 else
750 got_tab = TRUE;
751 }
752 else
753 {
754 if (got_tab || (eap->forceit && num_spaces > 1))
755 {
756 /* Retabulate this string of white-space */
757
758 /* len is virtual length of white string */
759 len = num_spaces = vcol - start_vcol;
760 num_tabs = 0;
761 if (!curbuf->b_p_et)
762 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200763#ifdef FEAT_VARTABS
764 int t, s;
765
766 tabstop_fromto(start_vcol, vcol,
Bram Moolenaar0119a592018-06-24 23:53:28 +0200767 curbuf->b_p_ts, new_vts_array, &t, &s);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200768 num_tabs = t;
769 num_spaces = s;
770#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 temp = new_ts - (start_vcol % new_ts);
772 if (num_spaces >= temp)
773 {
774 num_spaces -= temp;
775 num_tabs++;
776 }
777 num_tabs += num_spaces / new_ts;
778 num_spaces -= (num_spaces / new_ts) * new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 if (curbuf->b_p_et || got_tab ||
782 (num_spaces + num_tabs < len))
783 {
784 if (did_undo == FALSE)
785 {
786 did_undo = TRUE;
787 if (u_save((linenr_T)(lnum - 1),
788 (linenr_T)(lnum + 1)) == FAIL)
789 {
790 new_line = NULL; /* flag out-of-memory */
791 break;
792 }
793 }
794
795 /* len is actual number of white characters used */
796 len = num_spaces + num_tabs;
797 old_len = (long)STRLEN(ptr);
798 new_line = lalloc(old_len - col + start_col + len + 1,
799 TRUE);
800 if (new_line == NULL)
801 break;
802 if (start_col > 0)
803 mch_memmove(new_line, ptr, (size_t)start_col);
804 mch_memmove(new_line + start_col + len,
805 ptr + col, (size_t)(old_len - col + 1));
806 ptr = new_line + start_col;
807 for (col = 0; col < len; col++)
808 ptr[col] = (col < num_tabs) ? '\t' : ' ';
809 ml_replace(lnum, new_line, FALSE);
810 if (first_line == 0)
811 first_line = lnum;
812 last_line = lnum;
813 ptr = new_line;
814 col = start_col + len;
815 }
816 }
817 got_tab = FALSE;
818 num_spaces = 0;
819 }
820 if (ptr[col] == NUL)
821 break;
822 vcol += chartabsize(ptr + col, (colnr_T)vcol);
823#ifdef FEAT_MBYTE
824 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000825 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 else
827#endif
828 ++col;
829 }
830 if (new_line == NULL) /* out of memory */
831 break;
832 line_breakcheck();
833 }
834 if (got_int)
835 EMSG(_(e_interr));
836
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200837#ifdef FEAT_VARTABS
838 // If a single value was given then it can be considered equal to
839 // either the value of 'tabstop' or the value of 'vartabstop'.
840 if (tabstop_count(curbuf->b_p_vts_array) == 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200841 && tabstop_count(new_vts_array) == 1
842 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200843 ; /* not changed */
844 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200845 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200846 ; /* not changed */
847 else
848 redraw_curbuf_later(NOT_VALID);
849#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (curbuf->b_p_ts != new_ts)
851 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (first_line != 0)
854 changed_lines(first_line, 0, last_line + 1, 0L);
855
856 curwin->w_p_list = save_list; /* restore 'list' */
857
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200858#ifdef FEAT_VARTABS
859 if (new_ts_str != NULL) /* set the new tabstop */
860 {
861 // If 'vartabstop' is in use or if the value given to retab has more
862 // than one tabstop then update 'vartabstop'.
863 int *old_vts_ary = curbuf->b_p_vts_array;
864
Bram Moolenaar0119a592018-06-24 23:53:28 +0200865 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200866 {
867 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
868 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0119a592018-06-24 23:53:28 +0200869 curbuf->b_p_vts_array = new_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200870 vim_free(old_vts_ary);
871 }
872 else
873 {
874 // 'vartabstop' wasn't in use and a single value was given to
875 // retab then update 'tabstop'.
Bram Moolenaar0119a592018-06-24 23:53:28 +0200876 curbuf->b_p_ts = tabstop_first(new_vts_array);
877 vim_free(new_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200878 }
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200879 vim_free(new_ts_str);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200880 }
881#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 curbuf->b_p_ts = new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 coladvance(curwin->w_curswant);
885
886 u_clearline();
887}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889/*
890 * :move command - move lines line1-line2 to line dest
891 *
892 * return FAIL for failure, OK otherwise
893 */
894 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100895do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897 char_u *str;
898 linenr_T l;
899 linenr_T extra; /* Num lines added before line1 */
900 linenr_T num_lines; /* Num lines moved */
901 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200902#ifdef FEAT_FOLDING
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100903 win_T *win;
904 tabpage_T *tp;
Bram Moolenaard5f69332015-04-15 12:43:50 +0200905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907 if (dest >= line1 && dest < line2)
908 {
909 EMSG(_("E134: Move lines into themselves"));
910 return FAIL;
911 }
912
913 num_lines = line2 - line1 + 1;
914
915 /*
916 * First we copy the old text to its new location -- webb
917 * Also copy the flag that ":global" command uses.
918 */
919 if (u_save(dest, dest + 1) == FAIL)
920 return FAIL;
921 for (extra = 0, l = line1; l <= line2; l++)
922 {
923 str = vim_strsave(ml_get(l + extra));
924 if (str != NULL)
925 {
926 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
927 vim_free(str);
928 if (dest < line1)
929 extra++;
930 }
931 }
932
933 /*
934 * Now we must be careful adjusting our marks so that we don't overlap our
935 * mark_adjust() calls.
936 *
937 * We adjust the marks within the old text so that they refer to the
938 * last lines of the file (temporarily), because we know no other marks
939 * will be set there since these line numbers did not exist until we added
940 * our new lines.
941 *
942 * Then we adjust the marks on lines between the old and new text positions
943 * (either forwards or backwards).
944 *
945 * And Finally we adjust the marks we put at the end of the file back to
946 * their final destination at the new text position -- webb
947 */
948 last_line = curbuf->b_ml.ml_line_count;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100949 mark_adjust_nofold(line1, line2, last_line - line2, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 if (dest >= line2)
951 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100952 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
953#ifdef FEAT_FOLDING
954 FOR_ALL_TAB_WINDOWS(tp, win) {
955 if (win->w_buffer == curbuf)
956 foldMoveRange(&win->w_folds, line1, line2, dest);
957 }
958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 curbuf->b_op_start.lnum = dest - num_lines + 1;
960 curbuf->b_op_end.lnum = dest;
961 }
962 else
963 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100964 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
965#ifdef FEAT_FOLDING
966 FOR_ALL_TAB_WINDOWS(tp, win) {
967 if (win->w_buffer == curbuf)
968 foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
969 }
970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 curbuf->b_op_start.lnum = dest + 1;
972 curbuf->b_op_end.lnum = dest + num_lines;
973 }
974 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100975 mark_adjust_nofold(last_line - num_lines + 1, last_line,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 -(last_line - dest - extra), 0L);
977
978 /*
979 * Now we delete the original text -- webb
980 */
981 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
982 return FAIL;
983
984 for (l = line1; l <= line2; l++)
985 ml_delete(line1 + extra, TRUE);
986
987 if (!global_busy && num_lines > p_report)
988 {
989 if (num_lines == 1)
990 MSG(_("1 line moved"));
991 else
992 smsg((char_u *)_("%ld lines moved"), num_lines);
993 }
994
995 /*
996 * Leave the cursor on the last of the moved lines.
997 */
998 if (dest >= line1)
999 curwin->w_cursor.lnum = dest;
1000 else
1001 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
1002
1003 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +01001004 {
1005 dest += num_lines + 1;
1006 last_line = curbuf->b_ml.ml_line_count;
1007 if (dest > last_line + 1)
1008 dest = last_line + 1;
1009 changed_lines(line1, 0, dest, 0L);
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 else
1012 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
1013
1014 return OK;
1015}
1016
1017/*
1018 * ":copy"
1019 */
1020 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001021ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 linenr_T count;
1024 char_u *p;
1025
1026 count = line2 - line1 + 1;
1027 curbuf->b_op_start.lnum = n + 1;
1028 curbuf->b_op_end.lnum = n + count;
1029 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
1030
1031 /*
1032 * there are three situations:
1033 * 1. destination is above line1
1034 * 2. destination is between line1 and line2
1035 * 3. destination is below line2
1036 *
1037 * n = destination (when starting)
1038 * curwin->w_cursor.lnum = destination (while copying)
1039 * line1 = start of source (while copying)
1040 * line2 = end of source (while copying)
1041 */
1042 if (u_save(n, n + 1) == FAIL)
1043 return;
1044
1045 curwin->w_cursor.lnum = n;
1046 while (line1 <= line2)
1047 {
1048 /* need to use vim_strsave() because the line will be unlocked within
1049 * ml_append() */
1050 p = vim_strsave(ml_get(line1));
1051 if (p != NULL)
1052 {
1053 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
1054 vim_free(p);
1055 }
1056 /* situation 2: skip already copied lines */
1057 if (line1 == n)
1058 line1 = curwin->w_cursor.lnum;
1059 ++line1;
1060 if (curwin->w_cursor.lnum < line1)
1061 ++line1;
1062 if (curwin->w_cursor.lnum < line2)
1063 ++line2;
1064 ++curwin->w_cursor.lnum;
1065 }
1066
1067 appended_lines_mark(n, count);
1068
1069 msgmore((long)count);
1070}
1071
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001072static char_u *prevcmd = NULL; /* the previous command */
1073
1074#if defined(EXITFREE) || defined(PROTO)
1075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001076free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001077{
1078 vim_free(prevcmd);
1079}
1080#endif
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
1083 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
1084 * Bangs in the argument are replaced with the previously entered command.
1085 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
1087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001088do_bang(
1089 int addr_count,
1090 exarg_T *eap,
1091 int forceit,
1092 int do_in,
1093 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 char_u *arg = eap->arg; /* command */
1096 linenr_T line1 = eap->line1; /* start of range */
1097 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 char_u *newcmd = NULL; /* the new command */
1099 int free_newcmd = FALSE; /* need to free() newcmd */
1100 int ins_prevcmd;
1101 char_u *t;
1102 char_u *p;
1103 char_u *trailarg;
1104 int len;
1105 int scroll_save = msg_scroll;
1106
1107 /*
1108 * Disallow shell commands for "rvim".
1109 * Disallow shell commands from .exrc and .vimrc in current directory for
1110 * security reasons.
1111 */
1112 if (check_restricted() || check_secure())
1113 return;
1114
1115 if (addr_count == 0) /* :! */
1116 {
1117 msg_scroll = FALSE; /* don't scroll here */
1118 autowrite_all();
1119 msg_scroll = scroll_save;
1120 }
1121
1122 /*
1123 * Try to find an embedded bang, like in :!<cmd> ! [args]
1124 * (:!! is indicated by the 'forceit' variable)
1125 */
1126 ins_prevcmd = forceit;
1127 trailarg = arg;
1128 do
1129 {
1130 len = (int)STRLEN(trailarg) + 1;
1131 if (newcmd != NULL)
1132 len += (int)STRLEN(newcmd);
1133 if (ins_prevcmd)
1134 {
1135 if (prevcmd == NULL)
1136 {
1137 EMSG(_(e_noprev));
1138 vim_free(newcmd);
1139 return;
1140 }
1141 len += (int)STRLEN(prevcmd);
1142 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001143 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 vim_free(newcmd);
1146 return;
1147 }
1148 *t = NUL;
1149 if (newcmd != NULL)
1150 STRCAT(t, newcmd);
1151 if (ins_prevcmd)
1152 STRCAT(t, prevcmd);
1153 p = t + STRLEN(t);
1154 STRCAT(t, trailarg);
1155 vim_free(newcmd);
1156 newcmd = t;
1157
1158 /*
1159 * Scan the rest of the argument for '!', which is replaced by the
1160 * previous command. "\!" is replaced by "!" (this is vi compatible).
1161 */
1162 trailarg = NULL;
1163 while (*p)
1164 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001165 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001168 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else
1170 {
1171 trailarg = p;
1172 *trailarg++ = NUL;
1173 ins_prevcmd = TRUE;
1174 break;
1175 }
1176 }
1177 ++p;
1178 }
1179 } while (trailarg != NULL);
1180
1181 vim_free(prevcmd);
1182 prevcmd = newcmd;
1183
1184 if (bangredo) /* put cmd in redo buffer for ! command */
1185 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001186 /* If % or # appears in the command, it must have been escaped.
1187 * Reescape them, so that redoing them does not substitute them by the
1188 * buffername. */
1189 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1190
1191 if (cmd != NULL)
1192 {
1193 AppendToRedobuffLit(cmd, -1);
1194 vim_free(cmd);
1195 }
1196 else
1197 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 AppendToRedobuff((char_u *)"\n");
1199 bangredo = FALSE;
1200 }
1201 /*
1202 * Add quotes around the command, for shells that need them.
1203 */
1204 if (*p_shq != NUL)
1205 {
1206 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1207 if (newcmd == NULL)
1208 return;
1209 STRCPY(newcmd, p_shq);
1210 STRCAT(newcmd, prevcmd);
1211 STRCAT(newcmd, p_shq);
1212 free_newcmd = TRUE;
1213 }
1214 if (addr_count == 0) /* :! */
1215 {
1216 /* echo the command */
1217 msg_start();
1218 msg_putchar(':');
1219 msg_putchar('!');
1220 msg_outtrans(newcmd);
1221 msg_clr_eos();
1222 windgoto(msg_row, msg_col);
1223
1224 do_shell(newcmd, 0);
1225 }
1226 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 /* Careful: This may recursively call do_bang() again! (because of
1229 * autocommands) */
1230 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001231 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarade00832006-03-10 21:46:58 +00001232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (free_newcmd)
1234 vim_free(newcmd);
1235}
1236
1237/*
1238 * do_filter: filter lines through a command given by the user
1239 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240 * We mostly use temp files and the call_shell() routine here. This would
1241 * normally be done using pipes on a UNIX machine, but this is more portable
1242 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 * to deal with redirection somehow, and should handle things like looking
1244 * at the PATH env. variable, and adding reasonable extensions to the
1245 * command name given by the user. All reasonable versions of call_shell()
1246 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 * Alternatively, if on Unix and redirecting input or output, but not both,
1248 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 * We use input redirection if do_in is TRUE.
1250 * We use output redirection if do_out is TRUE.
1251 */
1252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001253do_filter(
1254 linenr_T line1,
1255 linenr_T line2,
1256 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1257 char_u *cmd,
1258 int do_in,
1259 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
1261 char_u *itmp = NULL;
1262 char_u *otmp = NULL;
1263 linenr_T linecount;
1264 linenr_T read_linecount;
1265 pos_T cursor_save;
1266 char_u *cmd_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 buf_T *old_curbuf = curbuf;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001268 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270 if (*cmd == NUL) /* no filter command */
1271 return;
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 cursor_save = curwin->w_cursor;
1274 linecount = line2 - line1 + 1;
1275 curwin->w_cursor.lnum = line1;
1276 curwin->w_cursor.col = 0;
1277 changed_line_abv_curs();
1278 invalidate_botline();
1279
1280 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001281 * When using temp files:
1282 * 1. * Form temp file names
1283 * 2. * Write the lines to a temp file
1284 * 3. Run the filter command on the temp file
1285 * 4. * Read the output of the command into the buffer
1286 * 5. * Delete the original lines to be filtered
1287 * 6. * Remove the temp files
1288 *
1289 * When writing the input with a pipe or when catching the output with a
1290 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 */
1292
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001293 if (do_out)
1294 shell_flags |= SHELL_DOOUT;
1295
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001296#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001297 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001299 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1300 shell_flags |= SHELL_READ;
1301 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001303 else if (do_in && !do_out && !p_stmp)
1304 {
1305 /* Use a pipe to write stdin of the command, do not use a temp file. */
1306 shell_flags |= SHELL_WRITE;
1307 curbuf->b_op_start.lnum = line1;
1308 curbuf->b_op_end.lnum = line2;
1309 }
1310 else if (do_in && do_out && !p_stmp)
1311 {
1312 /* Use a pipe to write stdin and fetch stdout of the command, do not
1313 * use a temp file. */
1314 shell_flags |= SHELL_READ|SHELL_WRITE;
1315 curbuf->b_op_start.lnum = line1;
1316 curbuf->b_op_end.lnum = line2;
1317 curwin->w_cursor.lnum = line2;
1318 }
1319 else
1320#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001321 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1322 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001323 {
1324 EMSG(_(e_notmp));
1325 goto filterend;
1326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
1328/*
1329 * The writing and reading of temp files will not be shown.
1330 * Vi also doesn't do this and the messages are not very informative.
1331 */
1332 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001333 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 FALSE, FALSE, FALSE, TRUE) == FAIL)
1335 {
1336 msg_putchar('\n'); /* keep message from buf_write() */
1337 --no_wait_return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001338#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (!aborting())
1340#endif
1341 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1342 goto filterend;
1343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (curbuf != old_curbuf)
1345 goto filterend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 if (!do_out)
1348 msg_putchar('\n');
1349
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001350 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1352 if (cmd_buf == NULL)
1353 goto filterend;
1354
1355 windgoto((int)Rows - 1, 0);
1356 cursor_on();
1357
1358 /*
1359 * When not redirecting the output the command can write anything to the
1360 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1361 * stderr output of external command. Clear the screen later.
1362 * If do_in is FALSE, this could be something like ":r !cat", which may
1363 * also mess up the screen, clear it later.
1364 */
1365 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1366 redraw_later_clear();
1367
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001368 if (do_out)
1369 {
1370 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001371 {
1372 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001373 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001374 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001375 redraw_curbuf_later(VALID);
1376 }
1377 read_linecount = curbuf->b_ml.ml_line_count;
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /*
1380 * When call_shell() fails wait_return() is called to give the user a
1381 * chance to read the error messages. Otherwise errors are ignored, so you
1382 * can see the error messages from the command that appear on stdout; use
1383 * 'u' to fix the text
1384 * Switch to cooked mode when not redirecting stdin, avoids that something
1385 * like ":r !cat" hangs.
1386 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1387 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001388 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 redraw_later_clear();
1391 wait_return(FALSE);
1392 }
1393 vim_free(cmd_buf);
1394
1395 did_check_timestamps = FALSE;
1396 need_check_timestamps = TRUE;
1397
1398 /* When interrupting the shell command, it may still have produced some
1399 * useful output. Reset got_int here, so that readfile() won't cancel
1400 * reading. */
1401 ui_breakcheck();
1402 got_int = FALSE;
1403
1404 if (do_out)
1405 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001406 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001408 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001409 eap, READ_FILTER) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001411#if defined(FEAT_EVAL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001412 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001414 {
1415 msg_putchar('\n');
1416 EMSG2(_(e_notread), otmp);
1417 }
1418 goto error;
1419 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001420 if (curbuf != old_curbuf)
1421 goto filterend;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001422 }
1423
1424 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1425
1426 if (shell_flags & SHELL_READ)
1427 {
1428 curbuf->b_op_start.lnum = line2 + 1;
1429 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1430 appended_lines_mark(line2, read_linecount);
1431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
1433 if (do_in)
1434 {
1435 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (read_linecount >= linecount)
1438 /* move all marks from old lines to new lines */
1439 mark_adjust(line1, line2, linecount, 0L);
1440 else
1441 {
1442 /* move marks from old lines to new lines, delete marks
1443 * that are in deleted lines */
1444 mark_adjust(line1, line1 + read_linecount - 1,
1445 linecount, 0L);
1446 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1447 }
1448 }
1449
1450 /*
1451 * Put cursor on first filtered line for ":range!cmd".
1452 * Adjust '[ and '] (set by buf_write()).
1453 */
1454 curwin->w_cursor.lnum = line1;
1455 del_lines(linecount, TRUE);
1456 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1457 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1458 write_lnum_adjust(-linecount); /* adjust last line
1459 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001460#ifdef FEAT_FOLDING
1461 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 else
1465 {
1466 /*
1467 * Put cursor on last new line for ":r !cmd".
1468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001470 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1474 --no_wait_return;
1475
1476 if (linecount > p_report)
1477 {
1478 if (do_in)
1479 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001480 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1481 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001484 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486 else
1487 msgmore((long)linecount);
1488 }
1489 }
1490 else
1491 {
1492error:
1493 /* put cursor back in same position for ":w !cmd" */
1494 curwin->w_cursor = cursor_save;
1495 --no_wait_return;
1496 wait_return(FALSE);
1497 }
1498
1499filterend:
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (curbuf != old_curbuf)
1502 {
1503 --no_wait_return;
1504 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (itmp != NULL)
1507 mch_remove(itmp);
1508 if (otmp != NULL)
1509 mch_remove(otmp);
1510 vim_free(itmp);
1511 vim_free(otmp);
1512}
1513
1514/*
1515 * Call a shell to execute a command.
1516 * When "cmd" is NULL start an interactive shell.
1517 */
1518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519do_shell(
1520 char_u *cmd,
1521 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 buf_T *buf;
1524#ifndef FEAT_GUI_MSWIN
1525 int save_nwr;
1526#endif
1527#ifdef MSWIN
1528 int winstart = FALSE;
1529#endif
1530
1531 /*
1532 * Disallow shell commands for "rvim".
1533 * Disallow shell commands from .exrc and .vimrc in current directory for
1534 * security reasons.
1535 */
1536 if (check_restricted() || check_secure())
1537 {
1538 msg_end();
1539 return;
1540 }
1541
1542#ifdef MSWIN
1543 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * Check if ":!start" is used.
1545 */
1546 if (cmd != NULL)
1547 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1548#endif
1549
1550 /*
1551 * For autocommands we want to get the output on the current screen, to
1552 * avoid having to type return below.
1553 */
1554 msg_putchar('\r'); /* put cursor at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557#ifdef MSWIN
1558 if (!winstart)
1559#endif
1560 stoptermcap();
1561 }
1562#ifdef MSWIN
1563 if (!winstart)
1564#endif
1565 msg_putchar('\n'); /* may shift screen one line up */
1566
1567 /* warning message before calling the shell */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001568 if (p_warn && !autocmd_busy && msg_silent == 0)
Bram Moolenaar29323592016-07-24 22:04:11 +02001569 FOR_ALL_BUFFERS(buf)
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01001570 if (bufIsChangedNotTerm(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572#ifdef FEAT_GUI_MSWIN
1573 if (!winstart)
1574 starttermcap(); /* don't want a message box here */
1575#endif
1576 MSG_PUTS(_("[No write since last change]\n"));
1577#ifdef FEAT_GUI_MSWIN
1578 if (!winstart)
1579 stoptermcap();
1580#endif
1581 break;
1582 }
1583
1584 /* This windgoto is required for when the '\n' resulted in a "delete line
1585 * 1" command to the terminal. */
1586 if (!swapping_screen())
1587 windgoto(msg_row, msg_col);
1588 cursor_on();
1589 (void)call_shell(cmd, SHELL_COOKED | flags);
1590 did_check_timestamps = FALSE;
1591 need_check_timestamps = TRUE;
1592
1593 /*
1594 * put the message cursor at the end of the screen, avoids wait_return()
1595 * to overwrite the text that the external command showed
1596 */
1597 if (!swapping_screen())
1598 {
1599 msg_row = Rows - 1;
1600 msg_col = 0;
1601 }
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (autocmd_busy)
1604 {
1605 if (msg_silent == 0)
1606 redraw_later_clear();
1607 }
1608 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 /*
1611 * For ":sh" there is no need to call wait_return(), just redraw.
1612 * Also for the Win32 GUI (the output is in a console window).
1613 * Otherwise there is probably text on the screen that the user wants
1614 * to read before redrawing, so call wait_return().
1615 */
1616#ifndef FEAT_GUI_MSWIN
1617 if (cmd == NULL
1618# ifdef WIN3264
1619 || (winstart && !need_wait_return)
1620# endif
1621 )
1622 {
1623 if (msg_silent == 0)
1624 redraw_later_clear();
1625 need_wait_return = FALSE;
1626 }
1627 else
1628 {
1629 /*
1630 * If we switch screens when starttermcap() is called, we really
1631 * want to wait for "hit return to continue".
1632 */
1633 save_nwr = no_wait_return;
1634 if (swapping_screen())
1635 no_wait_return = FALSE;
1636# ifdef AMIGA
1637 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1638# else
1639 wait_return(msg_silent == 0);
1640# endif
1641 no_wait_return = save_nwr;
1642 }
1643#endif /* FEAT_GUI_W32 */
1644
1645#ifdef MSWIN
1646 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1647#endif
1648 starttermcap(); /* start termcap if not done by wait_return() */
1649
1650 /*
1651 * In an Amiga window redrawing is caused by asking the window size.
1652 * If we got an interrupt this will not work. The chance that the
1653 * window size is wrong is very small, but we need to redraw the
1654 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1655 * but it saves an extra redraw.
1656 */
1657#ifdef AMIGA
1658 if (skip_redraw) /* ':' hit in wait_return() */
1659 {
1660 if (msg_silent == 0)
1661 redraw_later_clear();
1662 }
1663 else if (term_console)
1664 {
1665 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1666 if (got_int && msg_silent == 0)
1667 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1668 else
1669 must_redraw = 0; /* no extra redraw needed */
1670 }
1671#endif
1672 }
1673
1674 /* display any error messages now */
1675 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001676
Bram Moolenaarade00832006-03-10 21:46:58 +00001677 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678}
1679
1680/*
1681 * Create a shell command from a command string, input redirection file and
1682 * output redirection file.
1683 * Returns an allocated string with the shell command, or NULL for failure.
1684 */
1685 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001686make_filter_cmd(
1687 char_u *cmd, /* command */
1688 char_u *itmp, /* NULL or name of input file */
1689 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 char_u *buf;
1692 long_u len;
1693
Bram Moolenaar53076832015-12-31 19:53:21 +01001694#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001695 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001696 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001697
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001698 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001699 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1700 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001701 if (is_fish_shell)
1702 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1703 else
1704#endif
1705 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (itmp != NULL)
1707 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1708 if (otmp != NULL)
1709 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1710 buf = lalloc(len, TRUE);
1711 if (buf == NULL)
1712 return NULL;
1713
Bram Moolenaar53076832015-12-31 19:53:21 +01001714#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001716 * Put braces around the command (for concatenated commands) when
1717 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001719 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001720 {
1721 if (is_fish_shell)
1722 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1723 else
1724 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1725 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001726 else
1727 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (itmp != NULL)
1729 {
1730 STRCAT(buf, " < ");
1731 STRCAT(buf, itmp);
1732 }
1733#else
1734 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001735 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 * the use of commands in a pipe.
1737 */
1738 STRCPY(buf, cmd);
1739 if (itmp != NULL)
1740 {
1741 char_u *p;
1742
1743 /*
1744 * If there is a pipe, we have to put the '<' in front of it.
1745 * Don't do this when 'shellquote' is not empty, otherwise the
1746 * redirection would be inside the quotes.
1747 */
1748 if (*p_shq == NUL)
1749 {
1750 p = vim_strchr(buf, '|');
1751 if (p != NULL)
1752 *p = NUL;
1753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1755 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (*p_shq == NUL)
1757 {
1758 p = vim_strchr(cmd, '|');
1759 if (p != NULL)
1760 {
1761 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1762 STRCAT(buf, p);
1763 }
1764 }
1765 }
1766#endif
1767 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001768 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 return buf;
1771}
1772
1773/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001774 * Append output redirection for file "fname" to the end of string buffer
1775 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 * Works with the 'shellredir' and 'shellpipe' options.
1777 * The caller should make sure that there is enough room:
1778 * STRLEN(opt) + STRLEN(fname) + 3
1779 */
1780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001781append_redir(
1782 char_u *buf,
1783 int buflen,
1784 char_u *opt,
1785 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001788 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001790 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001791 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001793 {
1794 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001796 if (p[1] == '%') /* skip %% */
1797 ++p;
1798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (p != NULL)
1800 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001801 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1802 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1803 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001806 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 " %s %s",
1809#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#endif
1812 (char *)opt, (char *)fname);
1813}
1814
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001815#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001817static int no_viminfo(void);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001818static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001819static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001820static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821static int viminfo_errcnt;
1822
1823 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001824no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 /* "vim -i NONE" does not read or write a viminfo file */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001827 return STRCMP(p_viminfofile, "NONE") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
1830/*
1831 * Report an error for reading a viminfo file.
1832 * Count the number of errors. When there are more than 10, return TRUE.
1833 */
1834 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001835viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001837 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1838 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001839 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001840 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1841 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 emsg(IObuff);
1843 if (++viminfo_errcnt >= 10)
1844 {
1845 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1846 return TRUE;
1847 }
1848 return FALSE;
1849}
1850
1851/*
1852 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001853 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001856read_viminfo(
1857 char_u *file, /* file name or NULL to use default name */
1858 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859{
1860 FILE *fp;
1861 char_u *fname;
1862
1863 if (no_viminfo())
1864 return FAIL;
1865
Bram Moolenaard812df62008-11-09 12:46:09 +00001866 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (fname == NULL)
1868 return FAIL;
1869 fp = mch_fopen((char *)fname, READBIN);
1870
1871 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001872 {
1873 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001874 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1875 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001876 (flags & VIF_WANT_INFO) ? _(" info") : "",
1877 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1878 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001879 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001880 verbose_leave();
1881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 vim_free(fname);
1884 if (fp == NULL)
1885 return FAIL;
1886
1887 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001888 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 return OK;
1892}
1893
1894/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001895 * Write the viminfo file. The old one is read in first so that effectively a
1896 * merge of current info and old info is done. This allows multiple vims to
1897 * run simultaneously, without losing any marks etc.
1898 * If "forceit" is TRUE, then the old file is not read in, and only internal
1899 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 */
1901 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001902write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 char_u *fname;
1905 FILE *fp_in = NULL; /* input viminfo file, if any */
1906 FILE *fp_out = NULL; /* output viminfo file */
1907 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001908 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909#if defined(UNIX) || defined(VMS)
1910 mode_t umask_save;
1911#endif
1912#ifdef UNIX
1913 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001914 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001916#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001917 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 if (no_viminfo())
1921 return;
1922
1923 fname = viminfo_filename(file); /* may set to default if NULL */
1924 if (fname == NULL)
1925 return;
1926
1927 fp_in = mch_fopen((char *)fname, READBIN);
1928 if (fp_in == NULL)
1929 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001930 int fd;
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 /* if it does exist, but we can't read it, don't try writing */
1933 if (mch_stat((char *)fname, &st_new) == 0)
1934 goto end;
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001935
1936 /* Create the new .viminfo non-accessible for others, because it may
1937 * contain text from non-accessible documents. It is up to the user to
1938 * widen access (e.g. to a group). This may also fail if there is a
1939 * race condition, then just give up. */
1940 fd = mch_open((char *)fname,
1941 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1942 if (fd < 0)
1943 goto end;
1944 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946 else
1947 {
1948 /*
1949 * There is an existing viminfo file. Create a temporary file to
1950 * write the new viminfo into, in the same directory as the
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001951 * existing viminfo file, which will be renamed once all writing is
1952 * successful.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 */
1954#ifdef UNIX
1955 /*
1956 * For Unix we check the owner of the file. It's not very nice to
1957 * overwrite a user's viminfo file after a "su root", with a
1958 * viminfo file that the user can't read.
1959 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001960 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001961 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001963 if (mch_stat((char *)fname, &st_old) == 0
1964 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001965 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 ? (st_old.st_mode & 0200)
1967 : (st_old.st_gid == getgid()
1968 ? (st_old.st_mode & 0020)
1969 : (st_old.st_mode & 0002))))
1970 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001971 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1975 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001976 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto end;
1978 }
1979#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001980#ifdef WIN3264
1981 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001982 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001986 * Make tempname, find one that does not exist yet.
1987 * Beware of a race condition: If someone logs out and all Vim
1988 * instances exit at the same time a temp file might be created between
1989 * stat() and open(). Use mch_open() with O_EXCL to avoid that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 * May try twice: Once normal and once with shortname set, just in
1991 * case somebody puts his viminfo file in an 8.3 filesystem.
1992 */
1993 for (;;)
1994 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001995 int next_char = 'z';
1996 char_u *wp;
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 tempname = buf_modname(
1999#ifdef UNIX
2000 shortname,
2001#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#endif
2004 fname,
2005#ifdef VMS
2006 (char_u *)"-tmp",
2007#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#endif
2010 FALSE);
2011 if (tempname == NULL) /* out of memory */
2012 break;
2013
2014 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002015 * Try a series of names. Change one character, just before
2016 * the extension. This should also work for an 8.3
2017 * file name, when after adding the extension it still is
2018 * the same file as the original.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002020 wp = tempname + STRLEN(tempname) - 5;
2021 if (wp < gettail(tempname)) /* empty file name? */
2022 wp = gettail(tempname);
2023 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002025 /*
2026 * Check if tempfile already exists. Never overwrite an
2027 * existing file!
2028 */
2029 if (mch_stat((char *)tempname, &st_new) == 0)
2030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002033 * Check if tempfile is same as original file. May happen
2034 * when modname() gave the same file back. E.g. silly
2035 * link, or file name-length reached. Try again with
2036 * shortname set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002038 if (!shortname && st_new.st_dev == st_old.st_dev
2039 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002041 VIM_CLEAR(tempname);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002042 shortname = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 break;
2044 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002047 else
2048 {
2049 /* Try creating the file exclusively. This may fail if
2050 * another Vim tries to do it at the same time. */
2051#ifdef VMS
2052 /* fdopen() fails for some reason */
2053 umask_save = umask(077);
2054 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2055 (void)umask(umask_save);
2056#else
2057 int fd;
2058
2059 /* Use mch_open() to be able to use O_NOFOLLOW and set file
2060 * protection:
2061 * Unix: same as original file, but strip s-bit. Reset
2062 * umask to avoid it getting in the way.
2063 * Others: r&w for user only. */
2064# ifdef UNIX
2065 umask_save = umask(0);
2066 fd = mch_open((char *)tempname,
2067 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
2068 (int)((st_old.st_mode & 0777) | 0600));
2069 (void)umask(umask_save);
2070# else
2071 fd = mch_open((char *)tempname,
2072 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
2073# endif
2074 if (fd < 0)
2075 {
2076 fp_out = NULL;
2077# ifdef EEXIST
2078 /* Avoid trying lots of names while the problem is lack
2079 * of premission, only retry if the file already
2080 * exists. */
2081 if (errno != EEXIST)
2082 break;
2083# endif
2084 }
2085 else
2086 fp_out = fdopen(fd, WRITEBIN);
2087#endif /* VMS */
2088 if (fp_out != NULL)
2089 break;
2090 }
2091
2092 /* Assume file exists, try again with another name. */
2093 if (next_char == 'a' - 1)
2094 {
2095 /* They all exist? Must be something wrong! Don't write
2096 * the viminfo file then. */
2097 EMSG2(_("E929: Too many viminfo temp files, like %s!"),
2098 tempname);
2099 break;
2100 }
2101 *wp = next_char;
2102 --next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002104
2105 if (tempname != NULL)
2106 break;
2107 /* continue if shortname was set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
2109
Bram Moolenaara5792f52005-11-23 21:25:05 +00002110#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002111 if (tempname != NULL && fp_out != NULL)
2112 {
2113 stat_T tmp_st;
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 /*
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002116 * Make sure the original owner can read/write the tempfile and
2117 * otherwise preserve permissions, making sure the group matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002119 if (mch_stat((char *)tempname, &tmp_st) >= 0)
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002120 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002121 if (st_old.st_uid != tmp_st.st_uid)
2122 /* Changing the owner might fail, in which case the
2123 * file will now owned by the current user, oh well. */
2124 ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
2125 if (st_old.st_gid != tmp_st.st_gid
2126 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
2127 /* can't set the group to what it should be, remove
2128 * group permissions */
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002129 (void)mch_setperm(tempname, 0600);
2130 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002131 else
2132 /* can't stat the file, set conservative permissions */
2133 (void)mch_setperm(tempname, 0600);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002135#endif
Bram Moolenaare0aa23f2017-11-26 17:08:03 +01002136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138 /*
2139 * Check if the new viminfo file can be written to.
2140 */
2141 if (fp_out == NULL)
2142 {
2143 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002144 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (fp_in != NULL)
2146 fclose(fp_in);
2147 goto end;
2148 }
2149
2150 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002151 {
2152 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002153 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002154 verbose_leave();
2155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002158 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
Bram Moolenaar927030a2016-03-15 18:23:55 +01002160 if (fclose(fp_out) == EOF)
2161 ++viminfo_errcnt;
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (fp_in != NULL)
2164 {
2165 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002166
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002167 /* In case of an error keep the original viminfo file. Otherwise
2168 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002169 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002170 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002171 if (vim_rename(tempname, fname) == -1)
2172 {
2173 ++viminfo_errcnt;
2174 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2175 }
2176# ifdef WIN3264
2177 /* If the viminfo file was hidden then also hide the new file. */
2178 else if (hidden)
2179 mch_hide(fname);
2180# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002181 }
2182 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 mch_remove(tempname);
2184 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186end:
2187 vim_free(fname);
2188 vim_free(tempname);
2189}
2190
2191/*
2192 * Get the viminfo file name to use.
2193 * If "file" is given and not empty, use it (has already been expanded by
2194 * cmdline functions).
2195 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2196 * expand environment variables.
2197 * Returns an allocated string. NULL when out of memory.
2198 */
2199 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002200viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
2202 if (file == NULL || *file == NUL)
2203 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002204 if (*p_viminfofile != NUL)
2205 file = p_viminfofile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2207 {
2208#ifdef VIMINFO_FILE2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209# ifdef VMS
2210 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2211# else
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002212# ifdef MSWIN
2213 /* Use $VIM only if $HOME is the default "C:/". */
2214 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
2215 && mch_getenv((char_u *)"HOME") == NULL)
2216# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (mch_getenv((char_u *)"HOME") == NULL)
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219# endif
2220 {
2221 /* don't use $VIM when not available. */
2222 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2223 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2224 file = (char_u *)VIMINFO_FILE2;
2225 else
2226 file = (char_u *)VIMINFO_FILE;
2227 }
2228 else
2229#endif
2230 file = (char_u *)VIMINFO_FILE;
2231 }
2232 expand_env(file, NameBuff, MAXPATHL);
2233 file = NameBuff;
2234 }
2235 return vim_strsave(file);
2236}
2237
2238/*
2239 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2240 */
2241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002242do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 int eof = FALSE;
2245 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002246 int merge = FALSE;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002247 int do_copy_marks = FALSE;
2248 garray_T buflist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2251 return;
2252 vir.vir_fd = fp_in;
2253#ifdef FEAT_MBYTE
2254 vir.vir_conv.vc_type = CONV_NONE;
2255#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002256 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002257 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 if (fp_in != NULL)
2260 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002261 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002262 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002263 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002264 {
2265 /* Registers and marks are read and kept separate from what
2266 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002267 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002268 prepare_viminfo_marks();
2269 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002270
Bram Moolenaard812df62008-11-09 12:46:09 +00002271 eof = read_viminfo_up_to_marks(&vir,
2272 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002273 merge = TRUE;
2274 }
2275 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* Skip info, find start of marks */
2277 while (!(eof = viminfo_readline(&vir))
2278 && vir.vir_line[0] != '>')
2279 ;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002280
2281 do_copy_marks = (flags &
2282 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (fp_out != NULL)
2286 {
2287 /* Write the info: */
2288 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2289 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002290 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002291 write_viminfo_version(fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002293 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2295#endif
2296 write_viminfo_search_pattern(fp_out);
2297 write_viminfo_sub_string(fp_out);
2298#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002299 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
2301 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002302 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#ifdef FEAT_EVAL
2304 write_viminfo_varlist(fp_out);
2305#endif
2306 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002307 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002309 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002310
2311 if (do_copy_marks)
2312 ga_init2(&buflist, sizeof(buf_T *), 50);
2313 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002315
2316 if (do_copy_marks)
2317 {
2318 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2319 if (fp_out != NULL)
2320 ga_clear(&buflist);
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 vim_free(vir.vir_line);
2324#ifdef FEAT_MBYTE
2325 if (vir.vir_conv.vc_type != CONV_NONE)
2326 convert_setup(&vir.vir_conv, NULL, NULL);
2327#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002328 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329}
2330
2331/*
2332 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2333 * first part of the viminfo file which contains everything but the marks that
2334 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2335 */
2336 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002337read_viminfo_up_to_marks(
2338 vir_T *virp,
2339 int forceit,
2340 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342 int eof;
2343 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002344 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002347 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 eof = viminfo_readline(virp);
2351 while (!eof && virp->vir_line[0] != '>')
2352 {
2353 switch (virp->vir_line[0])
2354 {
2355 /* Characters reserved for future expansion, ignored now */
2356 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 case '^': /* to be defined */
2358 case '<': /* long line - ignored */
2359 /* A comment or empty line. */
2360 case NUL:
2361 case '\r':
2362 case '\n':
2363 case '#':
2364 eof = viminfo_readline(virp);
2365 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002366 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002367 eof = read_viminfo_barline(virp, got_encoding,
2368 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002371 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 eof = viminfo_encoding(virp);
2373 break;
2374 case '!': /* global variable */
2375#ifdef FEAT_EVAL
2376 eof = read_viminfo_varlist(virp, writing);
2377#else
2378 eof = viminfo_readline(virp);
2379#endif
2380 break;
2381 case '%': /* entry for buffer list */
2382 eof = read_viminfo_bufferlist(virp, writing);
2383 break;
2384 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002385 /* When registers are in bar lines skip the old style register
2386 * lines. */
2387 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2388 eof = read_viminfo_register(virp, forceit);
2389 else
2390 do {
2391 eof = viminfo_readline(virp);
2392 } while (!eof && (virp->vir_line[0] == TAB
2393 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 break;
2395 case '/': /* Search string */
2396 case '&': /* Substitute search string */
2397 case '~': /* Last search string, followed by '/' or '&' */
2398 eof = read_viminfo_search_pattern(virp, forceit);
2399 break;
2400 case '$':
2401 eof = read_viminfo_sub_string(virp, forceit);
2402 break;
2403 case ':':
2404 case '?':
2405 case '=':
2406 case '@':
2407#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002408 /* When history is in bar lines skip the old style history
2409 * lines. */
2410 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2411 eof = read_viminfo_history(virp, writing);
2412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002414 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 break;
2416 case '-':
2417 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002418 /* When file marks are in bar lines skip the old style lines. */
2419 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2420 eof = read_viminfo_filemark(virp, forceit);
2421 else
2422 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 break;
2424 default:
2425 if (viminfo_error("E575: ", _("Illegal starting char"),
2426 virp->vir_line))
2427 eof = TRUE;
2428 else
2429 eof = viminfo_readline(virp);
2430 break;
2431 }
2432 }
2433
2434#ifdef FEAT_CMDHIST
2435 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002436 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002437 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#endif
2439
2440 /* Change file names to buffer numbers for fmarks. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002441 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 fmarks_check_names(buf);
2443
2444 return eof;
2445}
2446
2447/*
2448 * Compare the 'encoding' value in the viminfo file with the current value of
2449 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2450 * conversion of text with iconv() in viminfo_readstring().
2451 */
2452 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002453viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455#ifdef FEAT_MBYTE
2456 char_u *p;
2457 int i;
2458
2459 if (get_viminfo_parameter('c') != 0)
2460 {
2461 p = vim_strchr(virp->vir_line, '=');
2462 if (p != NULL)
2463 {
2464 /* remove trailing newline */
2465 ++p;
2466 for (i = 0; vim_isprintc(p[i]); ++i)
2467 ;
2468 p[i] = NUL;
2469
2470 convert_setup(&virp->vir_conv, p, p_enc);
2471 }
2472 }
2473#endif
2474 return viminfo_readline(virp);
2475}
2476
2477/*
2478 * Read a line from the viminfo file.
2479 * Returns TRUE for end-of-file;
2480 */
2481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002482viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2485}
2486
2487/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002488 * Check string read from viminfo file.
2489 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 * - replace CTRL-V CTRL-V with CTRL-V
2491 * - replace CTRL-V 'n' with '\n'
2492 *
2493 * Check for a long line as written by viminfo_writestring().
2494 *
2495 * Return the string in allocated memory (NULL when out of memory).
2496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002498viminfo_readstring(
2499 vir_T *virp,
2500 int off, /* offset for virp->vir_line */
2501 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 char_u *retval;
2504 char_u *s, *d;
2505 long len;
2506
2507 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2508 {
2509 len = atol((char *)virp->vir_line + off + 1);
2510 retval = lalloc(len, TRUE);
2511 if (retval == NULL)
2512 {
2513 /* Line too long? File messed up? Skip next line. */
2514 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2515 return NULL;
2516 }
2517 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2518 s = retval + 1; /* Skip the leading '<' */
2519 }
2520 else
2521 {
2522 retval = vim_strsave(virp->vir_line + off);
2523 if (retval == NULL)
2524 return NULL;
2525 s = retval;
2526 }
2527
2528 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2529 d = retval;
2530 while (*s != NUL && *s != '\n')
2531 {
2532 if (s[0] == Ctrl_V && s[1] != NUL)
2533 {
2534 if (s[1] == 'n')
2535 *d++ = '\n';
2536 else
2537 *d++ = Ctrl_V;
2538 s += 2;
2539 }
2540 else
2541 *d++ = *s++;
2542 }
2543 *d = NUL;
2544
2545#ifdef FEAT_MBYTE
2546 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2547 {
2548 d = string_convert(&virp->vir_conv, retval, NULL);
2549 if (d != NULL)
2550 {
2551 vim_free(retval);
2552 retval = d;
2553 }
2554 }
2555#endif
2556
2557 return retval;
2558}
2559
2560/*
2561 * write string to viminfo file
2562 * - replace CTRL-V with CTRL-V CTRL-V
2563 * - replace '\n' with CTRL-V 'n'
2564 * - add a '\n' at the end
2565 *
2566 * For a long line:
2567 * - write " CTRL-V <length> \n " in first line
2568 * - write " < <string> \n " in second line
2569 */
2570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002571viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 int c;
2574 char_u *s;
2575 int len = 0;
2576
2577 for (s = p; *s != NUL; ++s)
2578 {
2579 if (*s == Ctrl_V || *s == '\n')
2580 ++len;
2581 ++len;
2582 }
2583
2584 /* If the string will be too long, write its length and put it in the next
2585 * line. Take into account that some room is needed for what comes before
2586 * the string (e.g., variable name). Add something to the length for the
2587 * '<', NL and trailing NUL. */
2588 if (len > LSIZE / 2)
2589 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2590
2591 while ((c = *p++) != NUL)
2592 {
2593 if (c == Ctrl_V || c == '\n')
2594 {
2595 putc(Ctrl_V, fd);
2596 if (c == '\n')
2597 c = 'n';
2598 }
2599 putc(c, fd);
2600 }
2601 putc('\n', fd);
2602}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002603
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002604/*
2605 * Write a string in quotes that barline_parse() can read back.
2606 * Breaks the line in less than LSIZE pieces when needed.
2607 * Returns remaining characters in the line.
2608 */
2609 int
2610barline_writestring(FILE *fd, char_u *s, int remaining_start)
2611{
2612 char_u *p;
2613 int remaining = remaining_start;
2614 int len = 2;
2615
2616 /* Count the number of characters produced, including quotes. */
2617 for (p = s; *p != NUL; ++p)
2618 {
2619 if (*p == NL)
2620 len += 2;
2621 else if (*p == '"' || *p == '\\')
2622 len += 2;
2623 else
2624 ++len;
2625 }
Bram Moolenaar25709572016-08-26 20:41:16 +02002626 if (len > remaining - 2)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002627 {
2628 fprintf(fd, ">%d\n|<", len);
2629 remaining = LSIZE - 20;
2630 }
2631
2632 putc('"', fd);
2633 for (p = s; *p != NUL; ++p)
2634 {
2635 if (*p == NL)
2636 {
2637 putc('\\', fd);
2638 putc('n', fd);
2639 --remaining;
2640 }
2641 else if (*p == '"' || *p == '\\')
2642 {
2643 putc('\\', fd);
2644 putc(*p, fd);
2645 --remaining;
2646 }
2647 else
2648 putc(*p, fd);
2649 --remaining;
2650
2651 if (remaining < 3)
2652 {
2653 putc('\n', fd);
2654 putc('|', fd);
2655 putc('<', fd);
2656 /* Leave enough space for another continuation. */
2657 remaining = LSIZE - 20;
2658 }
2659 }
2660 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002661 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002662}
2663
2664/*
2665 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002666 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002667 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002668 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002669 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002670barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002671{
2672 char_u *p = text;
2673 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002674 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002675 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002676 int i;
2677 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002678 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002679#ifdef FEAT_MBYTE
2680 char_u *sconv;
2681 int converted;
2682#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002683
2684 while (*p == ',')
2685 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002686 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002687 if (ga_grow(values, 1) == FAIL)
2688 break;
2689 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002690
2691 if (*p == '>')
2692 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002693 /* Need to read a continuation line. Put strings in allocated
2694 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002695 if (!allocated)
2696 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002697 for (i = 0; i < values->ga_len; ++i)
2698 {
2699 bval_T *vp = (bval_T *)(values->ga_data) + i;
2700
2701 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002702 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002703 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2704 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002705 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002706 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002707 allocated = TRUE;
2708 }
2709
2710 if (vim_isdigit(p[1]))
2711 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002712 size_t len;
2713 size_t todo;
2714 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002715
2716 /* String value was split into lines that are each shorter
2717 * than LSIZE:
2718 * |{bartype},>{length of "{text}{text2}"}
2719 * |<"{text1}
2720 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002721 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002722 */
2723 ++p;
2724 len = getdigits(&p);
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002725 buf = alloc((int)(len + 1));
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002726 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002727 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002728 p = buf;
2729 for (todo = len; todo > 0; todo -= n)
2730 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002731 eof = viminfo_readline(virp);
2732 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002733 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002734 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002735 /* File was truncated or garbled. Read another line if
2736 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002737 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002738 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002739 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002740 /* Get length of text, excluding |< and NL chars. */
2741 n = STRLEN(virp->vir_line);
2742 while (n > 0 && (virp->vir_line[n - 1] == NL
2743 || virp->vir_line[n - 1] == CAR))
2744 --n;
2745 n -= 2;
2746 if (n > todo)
2747 {
2748 /* more values follow after the string */
2749 nextp = virp->vir_line + 2 + todo;
2750 n = todo;
2751 }
2752 mch_memmove(p, virp->vir_line + 2, n);
2753 p += n;
2754 }
2755 *p = NUL;
2756 p = buf;
2757 }
2758 else
2759 {
2760 /* Line ending in ">" continues in the next line:
2761 * |{bartype},{lots of values},>
2762 * |<{value},{value}
2763 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002764 eof = viminfo_readline(virp);
2765 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002766 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002767 /* File was truncated or garbled. Read another line if
2768 * this one starts with '|'. */
2769 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002770 p = virp->vir_line + 2;
2771 }
2772 }
2773
2774 if (isdigit(*p))
2775 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002776 value->bv_type = BVAL_NR;
2777 value->bv_nr = getdigits(&p);
2778 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002779 }
2780 else if (*p == '"')
2781 {
2782 int len = 0;
2783 char_u *s = p;
2784
2785 /* Unescape special characters in-place. */
2786 ++p;
2787 while (*p != '"')
2788 {
2789 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002790 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002791 if (*p == '\\')
2792 {
2793 ++p;
2794 if (*p == 'n')
2795 s[len++] = '\n';
2796 else
2797 s[len++] = *p;
2798 ++p;
2799 }
2800 else
2801 s[len++] = *p++;
2802 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002803 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002804 s[len] = NUL;
2805
Bram Moolenaar01227092016-06-11 14:47:40 +02002806#ifdef FEAT_MBYTE
2807 converted = FALSE;
2808 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2809 {
2810 sconv = string_convert(&virp->vir_conv, s, NULL);
2811 if (sconv != NULL)
2812 {
2813 if (s == buf)
2814 vim_free(s);
2815 s = sconv;
2816 buf = s;
2817 converted = TRUE;
2818 }
2819 }
2820#endif
2821 /* Need to copy in allocated memory if the string wasn't allocated
2822 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002823 if (s != buf && allocated)
2824 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002825 value->bv_string = s;
2826 value->bv_type = BVAL_STRING;
2827 value->bv_len = len;
2828 value->bv_allocated = allocated
Bram Moolenaar01227092016-06-11 14:47:40 +02002829#ifdef FEAT_MBYTE
2830 || converted
2831#endif
2832 ;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002833 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002834 if (nextp != NULL)
2835 {
2836 /* values following a long string */
2837 p = nextp;
2838 nextp = NULL;
2839 }
2840 }
2841 else if (*p == ',')
2842 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002843 value->bv_type = BVAL_EMPTY;
2844 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002845 }
2846 else
2847 break;
2848 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002849 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002850}
2851
2852 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002853read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002854{
2855 char_u *p = virp->vir_line + 1;
2856 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002857 garray_T values;
2858 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002859 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002860 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002861
2862 /* The format is: |{bartype},{value},...
2863 * For a very long string:
2864 * |{bartype},>{length of "{text}{text2}"}
2865 * |<{text1}
2866 * |<{text2},{value}
2867 * For a long line not using a string
2868 * |{bartype},{lots of values},>
2869 * |<{value},{value}
2870 */
2871 if (*p == '<')
2872 {
2873 /* Continuation line of an unrecognized item. */
2874 if (writing)
2875 ga_add_string(&virp->vir_barlines, virp->vir_line);
2876 }
2877 else
2878 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002879 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002880 bartype = getdigits(&p);
2881 switch (bartype)
2882 {
2883 case BARTYPE_VERSION:
2884 /* Only use the version when it comes before the encoding.
2885 * If it comes later it was copied by a Vim version that
2886 * doesn't understand the version. */
2887 if (!got_encoding)
2888 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002889 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002890 vp = (bval_T *)values.ga_data;
2891 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2892 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002893 }
2894 break;
2895
2896 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002897 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002898 handle_viminfo_history(&values, writing);
2899 break;
2900
2901 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002902 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002903 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002904 break;
2905
Bram Moolenaar2d358992016-06-12 21:20:54 +02002906 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002907 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002908 handle_viminfo_mark(&values, force);
2909 break;
2910
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002911 default:
2912 /* copy unrecognized line (for future use) */
2913 if (writing)
2914 ga_add_string(&virp->vir_barlines, virp->vir_line);
2915 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002916 for (i = 0; i < values.ga_len; ++i)
2917 {
2918 vp = (bval_T *)values.ga_data + i;
2919 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2920 vim_free(vp->bv_string);
2921 }
2922 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002923 }
2924
Bram Moolenaarecefe712016-06-20 12:50:17 +02002925 if (read_next)
2926 return viminfo_readline(virp);
2927 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002928}
2929
2930 static void
2931write_viminfo_version(FILE *fp_out)
2932{
2933 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2934 BARTYPE_VERSION, VIMINFO_VERSION);
2935}
2936
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002937 static void
2938write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2939{
2940 int i;
2941 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002942 int seen_useful = FALSE;
2943 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002944
2945 if (gap->ga_len > 0)
2946 {
2947 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2948
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002949 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002950 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002951 {
2952 line = ((char **)(gap->ga_data))[i];
2953 if (seen_useful || line[1] != '<')
2954 {
2955 fputs(line, fp_out);
2956 seen_useful = TRUE;
2957 }
2958 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002959 }
2960}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#endif /* FEAT_VIMINFO */
2962
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002963/*
2964 * Return the current time in seconds. Calls time(), unless test_settime()
2965 * was used.
2966 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002967 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002968vim_time(void)
2969{
2970# ifdef FEAT_EVAL
2971 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2972# else
2973 return time(NULL);
2974# endif
2975}
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977/*
2978 * Implementation of ":fixdel", also used by get_stty().
2979 * <BS> resulting <Del>
2980 * ^? ^H
2981 * not ^? ^?
2982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002984do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 char_u *p;
2987
2988 p = find_termcode((char_u *)"kb");
2989 add_termcode((char_u *)"kD", p != NULL
2990 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2991}
2992
2993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002994print_line_no_prefix(
2995 linenr_T lnum,
2996 int use_number,
2997 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002999 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 if (curwin->w_p_nu || use_number)
3002 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003003 vim_snprintf((char *)numbuf, sizeof(numbuf),
3004 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003005 msg_puts_attr(numbuf, HL_ATTR(HLF_N)); /* Highlight line nrs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003007 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008}
3009
3010/*
3011 * Print a text line. Also in silent mode ("ex -s").
3012 */
3013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003014print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 int save_silent = silent_mode;
3017
Bram Moolenaard29459b2016-08-26 22:29:11 +02003018 /* apply :filter /pat/ */
3019 if (message_filtered(ml_get(lnum)))
3020 return;
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003023 silent_mode = FALSE;
3024 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
3025 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (save_silent)
3027 {
3028 msg_putchar('\n');
3029 cursor_on(); /* msg_start() switches it off */
3030 out_flush();
3031 silent_mode = save_silent;
3032 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00003033 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034}
3035
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003036 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003037rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003038{
3039 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02003040 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003041
Bram Moolenaar7e283842013-05-30 11:43:15 +02003042 buf = curbuf;
3043 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003044 /* buffer changed, don't change name now */
3045 if (buf != curbuf)
3046 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003047#ifdef FEAT_EVAL
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003048 if (aborting()) /* autocmds may abort script processing */
3049 return FAIL;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003050#endif
3051 /*
3052 * The name of the current buffer will be changed.
3053 * A new (unlisted) buffer entry needs to be made to hold the old file
3054 * name, which will become the alternate file name.
3055 * But don't set the alternate file name if the buffer didn't have a
3056 * name.
3057 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02003058 fname = curbuf->b_ffname;
3059 sfname = curbuf->b_sfname;
3060 xfname = curbuf->b_fname;
3061 curbuf->b_ffname = NULL;
3062 curbuf->b_sfname = NULL;
3063 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003064 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02003065 curbuf->b_ffname = fname;
3066 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003067 return FAIL;
3068 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02003069 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003070 if (xfname != NULL && *xfname != NUL)
3071 {
3072 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
3073 if (buf != NULL && !cmdmod.keepalt)
3074 curwin->w_alt_fnum = buf->b_fnum;
3075 }
3076 vim_free(fname);
3077 vim_free(sfname);
Bram Moolenaar7e283842013-05-30 11:43:15 +02003078 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003079
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003080 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02003081 DO_AUTOCHDIR;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003082 return OK;
3083}
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085/*
3086 * ":file[!] [fname]".
3087 */
3088 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003089ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003091 /* ":0file" removes the file name. Check for illegal uses ":3file",
3092 * "0file name", etc. */
3093 if (eap->addr_count > 0
3094 && (*eap->arg != NUL
3095 || eap->line2 > 0
3096 || eap->addr_count > 1))
3097 {
3098 EMSG(_(e_invarg));
3099 return;
3100 }
3101
3102 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003104 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 return;
Bram Moolenaarfc089602018-06-24 16:53:35 +02003106 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaarfc089602018-06-24 16:53:35 +02003108
3109 // print file name if no argument or 'F' is not in 'shortmess'
3110 if (*eap->arg == NUL || !shortmess(SHM_FILEINFO))
Bram Moolenaar426dd022016-03-15 15:09:29 +01003111 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
3114/*
3115 * ":update".
3116 */
3117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003118ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 if (curbufIsChanged())
3121 (void)do_write(eap);
3122}
3123
3124/*
3125 * ":write" and ":saveas".
3126 */
3127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 if (eap->usefilter) /* input lines to shell command */
3131 do_bang(1, eap, FALSE, TRUE, FALSE);
3132 else
3133 (void)do_write(eap);
3134}
3135
3136/*
3137 * write current buffer to file 'eap->arg'
3138 * if 'eap->append' is TRUE, append to the file
3139 *
3140 * if *eap->arg == NUL write to current file
3141 *
3142 * return FAIL for failure, OK otherwise
3143 */
3144 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003145do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 int other;
3148 char_u *fname = NULL; /* init to shut up gcc */
3149 char_u *ffname;
3150 int retval = FAIL;
3151 char_u *free_fname = NULL;
3152#ifdef FEAT_BROWSE
3153 char_u *browse_file = NULL;
3154#endif
3155 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003156 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 if (not_writing()) /* check 'write' option */
3159 return FAIL;
3160
3161 ffname = eap->arg;
3162#ifdef FEAT_BROWSE
3163 if (cmdmod.browse)
3164 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003165 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 NULL, NULL, NULL, curbuf);
3167 if (browse_file == NULL)
3168 goto theend;
3169 ffname = browse_file;
3170 }
3171#endif
3172 if (*ffname == NUL)
3173 {
3174 if (eap->cmdidx == CMD_saveas)
3175 {
3176 EMSG(_(e_argreq));
3177 goto theend;
3178 }
3179 other = FALSE;
3180 }
3181 else
3182 {
3183 fname = ffname;
3184 free_fname = fix_fname(ffname);
3185 /*
3186 * When out-of-memory, keep unexpanded file name, because we MUST be
3187 * able to write the file in this situation.
3188 */
3189 if (free_fname != NULL)
3190 ffname = free_fname;
3191 other = otherfile(ffname);
3192 }
3193
3194 /*
3195 * If we have a new file, put its name in the list of alternate file names.
3196 */
3197 if (other)
3198 {
3199 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3200 || eap->cmdidx == CMD_saveas)
3201 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3202 else
3203 alt_buf = buflist_findname(ffname);
3204 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3205 {
3206 /* Overwriting a file that is loaded in another buffer is not a
3207 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00003208 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 goto theend;
3210 }
3211 }
3212
3213 /*
3214 * Writing to the current file is not allowed in readonly mode
3215 * and a file name is required.
3216 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3217 */
3218 if (!other && (
3219#ifdef FEAT_QUICKFIX
3220 bt_dontwrite_msg(curbuf) ||
3221#endif
3222 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3223 goto theend;
3224
3225 if (!other)
3226 {
3227 ffname = curbuf->b_ffname;
3228 fname = curbuf->b_fname;
3229 /*
3230 * Not writing the whole file is only allowed with '!'.
3231 */
3232 if ( (eap->line1 != 1
3233 || eap->line2 != curbuf->b_ml.ml_line_count)
3234 && !eap->forceit
3235 && !eap->append
3236 && !p_wa)
3237 {
3238#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3239 if (p_confirm || cmdmod.confirm)
3240 {
3241 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3242 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3243 goto theend;
3244 eap->forceit = TRUE;
3245 }
3246 else
3247#endif
3248 {
3249 EMSG(_("E140: Use ! to write partial buffer"));
3250 goto theend;
3251 }
3252 }
3253 }
3254
3255 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3256 {
3257 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 buf_T *was_curbuf = curbuf;
3260
3261 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003262 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003263#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003265#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 /* buffer changed, don't change name now */
3270 retval = FAIL;
3271 goto theend;
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /* Exchange the file names for the current and the alternate
3274 * buffer. This makes it look like we are now editing the buffer
3275 * under the new name. Must be done before buf_write(), because
3276 * if there is no file name and 'cpo' contains 'F', it will set
3277 * the file name. */
3278 fname = alt_buf->b_fname;
3279 alt_buf->b_fname = curbuf->b_fname;
3280 curbuf->b_fname = fname;
3281 fname = alt_buf->b_ffname;
3282 alt_buf->b_ffname = curbuf->b_ffname;
3283 curbuf->b_ffname = fname;
3284 fname = alt_buf->b_sfname;
3285 alt_buf->b_sfname = curbuf->b_sfname;
3286 curbuf->b_sfname = fname;
3287 buf_name_changed(curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003290 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (!alt_buf->b_p_bl)
3292 {
3293 alt_buf->b_p_bl = TRUE;
3294 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3295 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003296#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003298#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
3302 /* buffer changed, don't write the file */
3303 retval = FAIL;
3304 goto theend;
3305 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003306
3307 /* If 'filetype' was empty try detecting it now. */
3308 if (*curbuf->b_p_ft == NUL)
3309 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310 if (au_has_group((char_u *)"filetypedetect"))
3311 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003312 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003313 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003314 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003315
3316 /* Autocommands may have changed buffer names, esp. when
3317 * 'autochdir' is set. */
3318 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320
Bram Moolenaar5c719942016-07-09 23:40:45 +02003321 name_was_missing = curbuf->b_ffname == NULL;
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3324 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003326 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003327 if (eap->cmdidx == CMD_saveas)
3328 {
3329 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003330 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003331 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003332 redraw_tabline = TRUE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003333 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003334 }
3335
3336 /* Change directories when the 'acd' option is set and the file name
3337 * got changed or set. */
3338 if (eap->cmdidx == CMD_saveas || name_was_missing)
3339 {
Bram Moolenaar6f470022018-04-10 18:47:20 +02003340 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343
3344theend:
3345#ifdef FEAT_BROWSE
3346 vim_free(browse_file);
3347#endif
3348 vim_free(free_fname);
3349 return retval;
3350}
3351
3352/*
3353 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3354 * BF_NEW or BF_READERR, check for overwriting current file.
3355 * May set eap->forceit if a dialog says it's OK to overwrite.
3356 * Return OK if it's OK, FAIL if it is not.
3357 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003358 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359check_overwrite(
3360 exarg_T *eap,
3361 buf_T *buf,
3362 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003364 char_u *ffname, /* full path version of fname */
3365 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 /*
3368 * write to other file or b_flags set or not writing the whole file:
3369 * overwriting only allowed with '!'
3370 */
3371 if ( (other
3372 || (buf->b_flags & BF_NOTEDITED)
3373 || ((buf->b_flags & BF_NEW)
3374 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3375 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003377#ifdef FEAT_QUICKFIX
3378 && !bt_nofile(buf)
3379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 && vim_fexists(ffname))
3381 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003382 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003384#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003385 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003386 if (mch_isdir(ffname))
3387 {
3388 EMSG2(_(e_isadir2), ffname);
3389 return FAIL;
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#endif
3392#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003393 if (p_confirm || cmdmod.confirm)
3394 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003395 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003397 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3398 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3399 return FAIL;
3400 eap->forceit = TRUE;
3401 }
3402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003404 {
3405 EMSG(_(e_exists));
3406 return FAIL;
3407 }
3408 }
3409
3410 /* For ":w! filename" check that no swap file exists for "filename". */
3411 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003413 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003414 char_u *p;
3415 int r;
3416 char_u *swapname;
3417
3418 /* We only try the first entry in 'directory', without checking if
3419 * it's writable. If the "." directory is not writable the write
3420 * will probably fail anyway.
3421 * Use 'shortname' of the current buffer, since there is no buffer
3422 * for the written file. */
3423 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003424 {
3425 dir = alloc(5);
3426 if (dir == NULL)
3427 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003428 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003429 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003430 else
3431 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003432 dir = alloc(MAXPATHL);
3433 if (dir == NULL)
3434 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003435 p = p_dir;
3436 copy_option_part(&p, dir, MAXPATHL, ",");
3437 }
3438 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003439 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003440 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003441 if (r)
3442 {
3443#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3444 if (p_confirm || cmdmod.confirm)
3445 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003446 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003447
3448 dialog_msg(buff,
3449 _("Swap file \"%s\" exists, overwrite anyway?"),
3450 swapname);
3451 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3452 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003453 {
3454 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003455 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003456 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003457 eap->forceit = TRUE;
3458 }
3459 else
3460#endif
3461 {
3462 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
3463 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003464 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003465 return FAIL;
3466 }
3467 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003468 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 }
3471 return OK;
3472}
3473
3474/*
3475 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3476 */
3477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003478ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 int i;
3481
3482 if (eap->cmd[1] == 'n')
3483 i = curwin->w_arg_idx + (int)eap->line2;
3484 else
3485 i = curwin->w_arg_idx - (int)eap->line2;
3486 eap->line1 = 1;
3487 eap->line2 = curbuf->b_ml.ml_line_count;
3488 if (do_write(eap) != FAIL)
3489 do_argfile(eap, i);
3490}
3491
3492/*
3493 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3494 */
3495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003496do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 buf_T *buf;
3499 int error = 0;
3500 int save_forceit = eap->forceit;
3501
3502 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3503 exiting = TRUE;
3504
Bram Moolenaar29323592016-07-24 22:04:11 +02003505 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaar7a760922018-02-19 23:10:02 +01003507#ifdef FEAT_TERMINAL
3508 if (exiting && term_job_running(buf->b_term))
3509 {
3510 no_write_message_nobang(buf);
3511 ++error;
3512 }
3513 else
3514#endif
Bram Moolenaar059db5c2017-10-15 22:42:23 +02003515 if (bufIsChanged(buf) && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 /*
3518 * Check if there is a reason the buffer cannot be written:
3519 * 1. if the 'write' option is set
3520 * 2. if there is no file name (even after browsing)
3521 * 3. if the 'readonly' is set (even after a dialog)
3522 * 4. if overwriting is allowed (even after a dialog)
3523 */
3524 if (not_writing())
3525 {
3526 ++error;
3527 break;
3528 }
3529#ifdef FEAT_BROWSE
3530 /* ":browse wall": ask for file name if there isn't one */
3531 if (buf->b_ffname == NULL && cmdmod.browse)
3532 browse_save_fname(buf);
3533#endif
3534 if (buf->b_ffname == NULL)
3535 {
3536 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3537 ++error;
3538 }
3539 else if (check_readonly(&eap->forceit, buf)
3540 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3541 FALSE) == FAIL)
3542 {
3543 ++error;
3544 }
3545 else
3546 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003547 bufref_T bufref;
3548
3549 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (buf_write_all(buf, eap->forceit) == FAIL)
3551 ++error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003553 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556 eap->forceit = save_forceit; /* check_overwrite() may set it */
3557 }
3558 }
3559 if (exiting)
3560 {
3561 if (!error)
3562 getout(0); /* exit Vim */
3563 not_exiting();
3564 }
3565}
3566
3567/*
3568 * Check the 'write' option.
3569 * Return TRUE and give a message when it's not st.
3570 */
3571 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
3574 if (p_write)
3575 return FALSE;
3576 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3577 return TRUE;
3578}
3579
3580/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003581 * Check if a buffer is read-only (either 'readonly' option is set or file is
3582 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3583 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 */
3585 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003586check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003588 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003589
3590 /* Handle a file being readonly when the 'readonly' option is set or when
3591 * the file exists and permissions are read-only.
3592 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3593 * important for device checks but not here. */
3594 if (!*forceit && (buf->b_p_ro
3595 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3596 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3599 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3600 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003601 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
Bram Moolenaar5386a122007-06-28 20:02:32 +00003603 if (buf->b_p_ro)
3604 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3605 buf->b_fname);
3606 else
3607 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 +00003608 buf->b_fname);
3609
3610 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3611 {
3612 /* Set forceit, to force the writing of a readonly file */
3613 *forceit = TRUE;
3614 return FALSE;
3615 }
3616 else
3617 return TRUE;
3618 }
3619 else
3620#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003621 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003623 else
3624 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3625 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 return TRUE;
3627 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 return FALSE;
3630}
3631
3632/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003633 * Try to abandon current file and edit a new or existing file.
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003634 * "fnum" is the number of the file, if zero use ffname/sfname.
3635 * "lnum" is the line number for the cursor in the new file (if non-zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 *
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003637 * Return:
3638 * GETFILE_ERROR for "normal" error,
3639 * GETFILE_NOT_WRITTEN for "not written" error,
3640 * GETFILE_SAME_FILE for success
3641 * GETFILE_OPEN_OTHER for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003644getfile(
3645 int fnum,
3646 char_u *ffname,
3647 char_u *sfname,
3648 int setpm,
3649 linenr_T lnum,
3650 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652 int other;
3653 int retval;
3654 char_u *free_me = NULL;
3655
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003656 if (text_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003657 return GETFILE_ERROR;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003658 if (curbuf_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003659 return GETFILE_ERROR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 if (fnum == 0)
3662 {
3663 /* make ffname full path, set sfname */
3664 fname_expand(curbuf, &ffname, &sfname);
3665 other = otherfile(ffname);
3666 free_me = ffname; /* has been allocated, free() later */
3667 }
3668 else
3669 other = (fnum != curbuf->b_fnum);
3670
3671 if (other)
3672 ++no_wait_return; /* don't wait for autowrite message */
Bram Moolenaareb44a682017-08-03 22:44:55 +02003673 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3675 {
3676#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3677 if (p_confirm && p_write)
3678 dialog_changed(curbuf, FALSE);
3679 if (curbufIsChanged())
3680#endif
3681 {
3682 if (other)
3683 --no_wait_return;
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02003684 no_write_message();
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003685 retval = GETFILE_NOT_WRITTEN; /* file has been changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 goto theend;
3687 }
3688 }
3689 if (other)
3690 --no_wait_return;
3691 if (setpm)
3692 setpcmark();
3693 if (!other)
3694 {
3695 if (lnum != 0)
3696 curwin->w_cursor.lnum = lnum;
3697 check_cursor_lnum();
3698 beginline(BL_SOL | BL_FIX);
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003699 retval = GETFILE_SAME_FILE; /* it's in the same file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02003702 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003703 curwin) == OK)
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003704 retval = GETFILE_OPEN_OTHER; /* opened another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 else
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003706 retval = GETFILE_ERROR; /* error encountered */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708theend:
3709 vim_free(free_me);
3710 return retval;
3711}
3712
3713/*
3714 * start editing a new file
3715 *
3716 * fnum: file number; if zero use ffname/sfname
3717 * ffname: the file name
3718 * - full path if sfname used,
3719 * - any file name if sfname is NULL
3720 * - empty string to re-edit with the same file name (but may be
3721 * in a different directory)
3722 * - NULL to start an empty buffer
3723 * sfname: the short file name (or NULL)
3724 * eap: contains the command to be executed after loading the file and
3725 * forced 'ff' and 'fenc'
3726 * newlnum: if > 0: put cursor on this line number (if possible)
3727 * if ECMD_LASTL: use last position in loaded file
3728 * if ECMD_LAST: use last position in all files
3729 * if ECMD_ONE: use first line
3730 * flags:
3731 * ECMD_HIDE: if TRUE don't free the current buffer
3732 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3733 * ECMD_OLDBUF: use existing buffer if it exists
3734 * ECMD_FORCEIT: ! used for Ex command
3735 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003736 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003737 * window, NULL when splitting the window first. When not NULL info
3738 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 *
3740 * return FAIL for failure, OK otherwise
3741 */
3742 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003743do_ecmd(
3744 int fnum,
3745 char_u *ffname,
3746 char_u *sfname,
3747 exarg_T *eap, /* can be NULL! */
3748 linenr_T newlnum,
3749 int flags,
3750 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752 int other_file; /* TRUE if editing another file */
3753 int oldbuf; /* TRUE if using existing buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 int auto_buf = FALSE; /* TRUE if autocommands brought us
3755 into the buffer unexpectedly */
3756 char_u *new_name = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003757#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003758 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759#endif
3760 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003761 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003762 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 char_u *free_fname = NULL;
3764#ifdef FEAT_BROWSE
3765 char_u *browse_file = NULL;
3766#endif
3767 int retval = FAIL;
3768 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003769 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 linenr_T topline = 0;
3771 int newcol = -1;
3772 int solcol = -1;
3773 pos_T *pos;
3774#ifdef FEAT_SUN_WORKSHOP
3775 char_u *cp;
3776#endif
3777 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003778#ifdef FEAT_SPELL
3779 int did_get_winopts = FALSE;
3780#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003781 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003782 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 if (eap != NULL)
3785 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003786 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 if (fnum != 0)
3789 {
3790 if (fnum == curbuf->b_fnum) /* file is already being edited */
3791 return OK; /* nothing to do */
3792 other_file = TRUE;
3793 }
3794 else
3795 {
3796#ifdef FEAT_BROWSE
3797 if (cmdmod.browse)
3798 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003799 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003800# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003801 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003802# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003803 au_has_group((char_u *)"FileExplorer"))
3804 {
3805 /* No browsing supported but we do have the file explorer:
3806 * Edit the directory. */
3807 if (ffname == NULL || !mch_isdir(ffname))
3808 ffname = (char_u *)".";
3809 }
3810 else
3811 {
3812 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003814 if (browse_file == NULL)
3815 goto theend;
3816 ffname = browse_file;
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819#endif
3820 /* if no short name given, use ffname for short name */
3821 if (sfname == NULL)
3822 sfname = ffname;
3823#ifdef USE_FNAME_CASE
3824# ifdef USE_LONG_FNAME
3825 if (USE_LONG_FNAME)
3826# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003827 if (sfname != NULL)
3828 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#endif
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3832 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 if (ffname == NULL)
3835 other_file = TRUE;
3836 /* there is no file name */
3837 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3838 other_file = FALSE;
3839 else
3840 {
3841 if (*ffname == NUL) /* re-edit with same file name */
3842 {
3843 ffname = curbuf->b_ffname;
3844 sfname = curbuf->b_fname;
3845 }
3846 free_fname = fix_fname(ffname); /* may expand to full path name */
3847 if (free_fname != NULL)
3848 ffname = free_fname;
3849 other_file = otherfile(ffname);
3850#ifdef FEAT_SUN_WORKSHOP
3851 if (usingSunWorkShop && p_acd
3852 && (cp = vim_strrchr(sfname, '/')) != NULL)
3853 sfname = ++cp;
3854#endif
3855 }
3856 }
3857
3858 /*
3859 * if the file was changed we may not be allowed to abandon it
3860 * - if we are going to re-edit the same file
3861 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3862 */
3863 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3864 || (curbuf->b_nwindows == 1
3865 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003866 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3867 | (other_file ? 0 : CCGD_MULTWIN)
3868 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3869 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 if (fnum == 0 && other_file && ffname != NULL)
3872 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3873 goto theend;
3874 }
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 /*
3877 * End Visual mode before switching to another buffer, so the text can be
3878 * copied into the GUI selection buffer.
3879 */
3880 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003882#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003883 if ((command != NULL || newlnum > (linenr_T)0)
3884 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3885 {
3886 int len;
3887 char_u *p;
3888
3889 /* Set v:swapcommand for the SwapExists autocommands. */
3890 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003891 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003892 else
3893 len = 30;
3894 p = alloc((unsigned)len);
3895 if (p != NULL)
3896 {
3897 if (command != NULL)
3898 vim_snprintf((char *)p, len, ":%s\r", command);
3899 else
3900 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3901 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3902 did_set_swapcommand = TRUE;
3903 vim_free(p);
3904 }
3905 }
3906#endif
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 /*
3909 * If we are starting to edit another file, open a (new) buffer.
3910 * Otherwise we re-use the current buffer.
3911 */
3912 if (other_file)
3913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (!(flags & ECMD_ADDBUF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003916 if (!cmdmod.keepalt)
3917 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003918 if (oldwin != NULL)
3919 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921
3922 if (fnum)
3923 buf = buflist_findnr(fnum);
3924 else
3925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (flags & ECMD_ADDBUF)
3927 {
3928 linenr_T tlnum = 1L;
3929
3930 if (command != NULL)
3931 {
3932 tlnum = atol((char *)command);
3933 if (tlnum <= 0)
3934 tlnum = 1L;
3935 }
3936 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3937 goto theend;
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 buf = buflist_new(ffname, sfname, 0L,
3940 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003941
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003942 /* autocommands may change curwin and curbuf */
3943 if (oldwin != NULL)
3944 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003945 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 if (buf == NULL)
3948 goto theend;
3949 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3950 {
3951 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 else /* existing memfile */
3954 {
3955 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003956 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003958 /* Check if autocommands made the buffer invalid or changed the
3959 * current buffer. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003960 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 goto theend;
3962#ifdef FEAT_EVAL
3963 if (aborting()) /* autocmds may abort script processing */
3964 goto theend;
3965#endif
3966 }
3967
3968 /* May jump to last used line number for a loaded buffer or when asked
3969 * for explicitly */
3970 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3971 {
3972 pos = buflist_findfpos(buf);
3973 newlnum = pos->lnum;
3974 solcol = pos->col;
3975 }
3976
3977 /*
3978 * Make the (new) buffer the one used by the current window.
3979 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3980 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003981 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 */
3983 if (buf != curbuf)
3984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 /*
3986 * Be careful: The autocommands may delete any buffer and change
3987 * the current buffer.
3988 * - If the buffer we are going to edit is deleted, give up.
3989 * - If the current buffer is deleted, prefer to load the new
3990 * buffer when loading a buffer is required. This avoids
3991 * loading another buffer which then must be closed again.
3992 * - If we ended up in the new buffer already, need to skip a few
3993 * things, set auto_buf.
3994 */
3995 if (buf->b_fname != NULL)
3996 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003997 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003999 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004001 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 delbuf_msg(new_name); /* frees new_name */
4003 goto theend;
4004 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004005#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (aborting()) /* autocmds may abort script processing */
4007 {
4008 vim_free(new_name);
4009 goto theend;
4010 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (buf == curbuf) /* already in new buffer */
4013 auto_buf = TRUE;
4014 else
4015 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02004016 win_T *the_curwin = curwin;
4017
4018 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004019 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004020 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004021 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02004022
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004023 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 buf_copy_options(buf, BCO_ENTER);
4025
Bram Moolenaar5a497892016-09-03 16:29:04 +02004026 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02004027 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004028 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004029 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004030 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
Bram Moolenaar5a497892016-09-03 16:29:04 +02004032 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004033 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01004034
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004035#ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02004036 /* autocmds may abort script processing */
4037 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
4039 vim_free(new_name);
4040 goto theend;
4041 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004044 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004046 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 delbuf_msg(new_name); /* frees new_name */
4048 goto theend;
4049 }
4050 if (buf == curbuf) /* already in new buffer */
4051 auto_buf = TRUE;
4052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004054#ifdef FEAT_SYN_HL
4055 /*
4056 * <VN> We could instead free the synblock
4057 * and re-attach to buffer, perhaps.
4058 */
Bram Moolenaarf1d13472017-07-11 18:28:46 +02004059 if (curwin->w_buffer == NULL
4060 || curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02004061 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 curwin->w_buffer = buf;
4064 curbuf = buf;
4065 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004066
4067 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
4068 if (!oldbuf && eap != NULL)
4069 {
4070 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004071#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004072 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004073#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076
4077 /* May get the window options from the last time this buffer
4078 * was in this window (or another window). If not used
4079 * before, reset the local window options to the global
4080 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004081 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004082#ifdef FEAT_SPELL
4083 did_get_winopts = TRUE;
4084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004087 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004088 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091 curwin->w_pcmark.lnum = 1;
4092 curwin->w_pcmark.col = 0;
4093 }
4094 else /* !other_file */
4095 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02004096 if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 oldbuf = (flags & ECMD_OLDBUF);
4100 }
4101
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004102 /* Don't redraw until the cursor is in the right line, otherwise
4103 * autocommands may cause ml_get errors. */
4104 ++RedrawingDisabled;
4105 did_inc_redrawing_disabled = TRUE;
4106
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004107 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4109 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004110 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 else
4113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /* Don't make a buffer listed if it's a help buffer. Useful when
4115 * using CTRL-O to go back to a help file. */
4116 if (!curbuf->b_help)
4117 set_buflisted(TRUE);
4118 }
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 /* If autocommands change buffers under our fingers, forget about
4121 * editing the file. */
4122 if (buf != curbuf)
4123 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004124#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (aborting()) /* autocmds may abort script processing */
4126 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 /* Since we are starting to edit a file, consider the filetype to be
4130 * unset. Helps for when an autocommand changes files and expects syntax
4131 * highlighting to work in the other file. */
4132 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134/*
4135 * other_file oldbuf
4136 * FALSE FALSE re-edit same file, buffer is re-used
4137 * FALSE TRUE re-edit same file, nothing changes
4138 * TRUE FALSE start editing new file, new buffer
4139 * TRUE TRUE start editing in existing buffer (nothing to do)
4140 */
4141 if (!other_file && !oldbuf) /* re-use the buffer */
4142 {
4143 set_last_cursor(curwin); /* may set b_last_cursor */
4144 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4145 {
4146 newlnum = curwin->w_cursor.lnum;
4147 solcol = curwin->w_cursor.col;
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 buf = curbuf;
4150 if (buf->b_fname != NULL)
4151 new_name = vim_strsave(buf->b_fname);
4152 else
4153 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004154 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004155
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004156 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4157 {
4158 /* Save all the text, so that the reload can be undone.
4159 * Sync first so that this is a separate undo-able action. */
4160 u_sync(FALSE);
4161 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4162 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004163 {
4164 vim_free(new_name);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004165 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004166 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004167 u_unchanged(curbuf);
4168 buf_freeall(curbuf, BFA_KEEP_UNDO);
4169
4170 /* tell readfile() not to clear or reload undo info */
4171 readfile_flags = READ_KEEP_UNDO;
4172 }
4173 else
4174 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /* If autocommands deleted the buffer we were going to re-edit, give
4177 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004178 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 delbuf_msg(new_name); /* frees new_name */
4181 goto theend;
4182 }
4183 vim_free(new_name);
4184
4185 /* If autocommands change buffers under our fingers, forget about
4186 * re-editing the file. Should do the buf_clear_file(), but perhaps
4187 * the autocommands changed the buffer... */
4188 if (buf != curbuf)
4189 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004190#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (aborting()) /* autocmds may abort script processing */
4192 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#endif
4194 buf_clear_file(curbuf);
4195 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4196 curbuf->b_op_end.lnum = 0;
4197 }
4198
4199/*
4200 * If we get here we are sure to start editing
4201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /* Assume success now */
4203 retval = OK;
4204
4205 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 * Check if we are editing the w_arg_idx file in the argument list.
4207 */
4208 check_arg_idx(curwin);
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (!auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 /*
4213 * Set cursor and init window before reading the file and executing
4214 * autocommands. This allows for the autocommands to position the
4215 * cursor.
4216 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004217 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004220 /* It's possible that all lines in the buffer changed. Need to update
4221 * automatic folding for all windows where it's used. */
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004222 {
4223 win_T *win;
4224 tabpage_T *tp;
4225
4226 FOR_ALL_TAB_WINDOWS(tp, win)
4227 if (win->w_buffer == curbuf)
4228 foldUpdateAll(win);
4229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230#endif
4231
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004232 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004233 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004234
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 /*
4236 * Careful: open_buffer() and apply_autocmds() may change the current
4237 * buffer and window.
4238 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004239 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 topline = curwin->w_topline;
4241 if (!oldbuf) /* need to read the file */
4242 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004243#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 swap_exists_action = SEA_DIALOG;
4245#endif
4246 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4247
4248 /*
4249 * Open the buffer and read the file.
4250 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004251#if defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004252 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 retval = FAIL;
4254#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004255 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif
4257
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004258#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (swap_exists_action == SEA_QUIT)
4260 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004261 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#endif
4263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 else
4265 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004266 /* Read the modelines, but only to set window-local options. Any
4267 * buffer-local options have already been set and may have been
4268 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004269 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4272 &retval);
4273 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4274 &retval);
4275 }
4276 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaareab316b2015-03-24 11:46:30 +01004278 /* If autocommands change the cursor position or topline, we should
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004279 * keep it. Also when it moves within a line. But not when it moves
4280 * to the first non-blank. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004281 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004283 char_u *text = ml_get_curline();
4284
4285 if (curwin->w_cursor.lnum != orig_pos.lnum
4286 || curwin->w_cursor.col != (int)(skipwhite(text) - text))
4287 {
4288 newlnum = curwin->w_cursor.lnum;
4289 newcol = curwin->w_cursor.col;
4290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 if (curwin->w_topline == topline)
4293 topline = 0;
4294
4295 /* Even when cursor didn't move we need to recompute topline. */
4296 changed_line_abv_curs();
4297
4298#ifdef FEAT_TITLE
4299 maketitle();
4300#endif
4301 }
4302
4303#ifdef FEAT_DIFF
4304 /* Tell the diff stuff that this buffer is new and/or needs updating.
4305 * Also needed when re-editing the same buffer, because unloading will
4306 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004307 if (curwin->w_p_diff)
4308 {
4309 diff_buf_add(curbuf);
4310 diff_invalidate(curbuf);
4311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312#endif
4313
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004314#ifdef FEAT_SPELL
4315 /* If the window options were changed may need to set the spell language.
4316 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004317 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4318 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004319#endif
4320
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 if (command == NULL)
4322 {
4323 if (newcol >= 0) /* position set by autocommands */
4324 {
4325 curwin->w_cursor.lnum = newlnum;
4326 curwin->w_cursor.col = newcol;
4327 check_cursor();
4328 }
4329 else if (newlnum > 0) /* line number from caller or old position */
4330 {
4331 curwin->w_cursor.lnum = newlnum;
4332 check_cursor_lnum();
4333 if (solcol >= 0 && !p_sol)
4334 {
4335 /* 'sol' is off: Use last known column. */
4336 curwin->w_cursor.col = solcol;
4337 check_cursor_col();
4338#ifdef FEAT_VIRTUALEDIT
4339 curwin->w_cursor.coladd = 0;
4340#endif
4341 curwin->w_set_curswant = TRUE;
4342 }
4343 else
4344 beginline(BL_SOL | BL_FIX);
4345 }
4346 else /* no line number, go to last line in Ex mode */
4347 {
4348 if (exmode_active)
4349 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4350 beginline(BL_WHITE | BL_FIX);
4351 }
4352 }
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /* Check if cursors in other windows on the same buffer are still valid */
4355 check_lnums(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
4358 * Did not read the file, need to show some info about the file.
4359 * Do this after setting the cursor.
4360 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004361 if (oldbuf && !auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
4363 int msg_scroll_save = msg_scroll;
4364
4365 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4366 * message. */
4367 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4368 msg_scroll = FALSE;
4369 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4370 check_for_delay(FALSE);
4371 msg_start();
4372 msg_scroll = msg_scroll_save;
4373 msg_scrolled_ign = TRUE;
4374
Bram Moolenaar426dd022016-03-15 15:09:29 +01004375 if (!shortmess(SHM_FILEINFO))
4376 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 msg_scrolled_ign = FALSE;
4379 }
4380
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004381#ifdef FEAT_VIMINFO
4382 curbuf->b_last_used = vim_time();
4383#endif
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (command != NULL)
4386 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4387
4388#ifdef FEAT_KEYMAP
4389 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004390 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#endif
4392
4393 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004394 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (!skip_redraw)
4396 {
4397 n = p_so;
4398 if (topline == 0 && command == NULL)
4399 p_so = 999; /* force cursor halfway the window */
4400 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 curwin->w_scbind_pos = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 p_so = n;
4403 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4404 }
4405
4406 if (p_im)
4407 need_start_insertmode = TRUE;
4408
Bram Moolenaar15833232018-02-03 18:33:17 +01004409#ifdef FEAT_AUTOCHDIR
4410 /* Change directories when the 'acd' option is set and we aren't already in
4411 * that directory (should already be done above). Expect getcwd() to be
4412 * faster than calling shorten_fnames() unnecessarily. */
4413 if (p_acd && curbuf->b_ffname != NULL)
4414 {
4415 char_u curdir[MAXPATHL];
4416 char_u filedir[MAXPATHL];
4417
4418 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1);
4419 *gettail_sep(filedir) = NUL;
4420 if (mch_dirname(curdir, MAXPATHL) != FAIL
4421 && vim_fnamecmp(curdir, filedir) != 0)
4422 do_autochdir();
4423 }
4424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00004426#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004427 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
4429# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02004430 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
4432# endif
4433# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004434 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004435 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436# endif
4437 }
4438#endif
4439
4440theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004441 if (did_inc_redrawing_disabled)
4442 --RedrawingDisabled;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004443#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004444 if (did_set_swapcommand)
4445 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_BROWSE
4448 vim_free(browse_file);
4449#endif
4450 vim_free(free_fname);
4451 return retval;
4452}
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004455delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
4457 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4458 name == NULL ? (char_u *)"" : name);
4459 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004460 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004461 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004464static int append_indent = 0; /* autoindent for first line */
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466/*
4467 * ":insert" and ":append", also used by ":change"
4468 */
4469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004470ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
4472 char_u *theline;
4473 int did_undo = FALSE;
4474 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004475 int indent = 0;
4476 char_u *p;
4477 int vcol;
4478 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004480 /* the ! flag toggles autoindent */
4481 if (eap->forceit)
4482 curbuf->b_p_ai = !curbuf->b_p_ai;
4483
4484 /* First autoindent comes from the line we start on */
4485 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4486 append_indent = get_indent_lnum(lnum);
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (eap->cmdidx != CMD_append)
4489 --lnum;
4490
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004491 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004492 if (empty && lnum == 1)
4493 lnum = 0;
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 State = INSERT; /* behave like in Insert mode */
4496 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4497 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004498
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004499 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
4501 msg_scroll = TRUE;
4502 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004503 if (curbuf->b_p_ai)
4504 {
4505 if (append_indent >= 0)
4506 {
4507 indent = append_indent;
4508 append_indent = -1;
4509 }
4510 else if (lnum > 0)
4511 indent = get_indent_lnum(lnum);
4512 }
4513 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004515 {
4516 /* No getline() function, use the lines that follow. This ends
4517 * when there is no more. */
4518 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4519 break;
4520 p = vim_strchr(eap->nextcmd, NL);
4521 if (p == NULL)
4522 p = eap->nextcmd + STRLEN(eap->nextcmd);
4523 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4524 if (*p != NUL)
4525 ++p;
4526 eap->nextcmd = p;
4527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004529 {
4530 int save_State = State;
4531
4532 /* Set State to avoid the cursor shape to be set to INSERT mode
4533 * when getline() returns. */
4534 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 theline = eap->getline(
4536#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004537 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004539 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004540 State = save_State;
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004543 if (theline == NULL)
4544 break;
4545
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004546 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4547 if (ex_keep_indent)
4548 append_indent = indent;
4549
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004550 /* Look for the "." after automatic indent. */
4551 vcol = 0;
4552 for (p = theline; indent > vcol; ++p)
4553 {
4554 if (*p == ' ')
4555 ++vcol;
4556 else if (*p == TAB)
4557 vcol += 8 - vcol % 8;
4558 else
4559 break;
4560 }
4561 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004562 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4563 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 vim_free(theline);
4566 break;
4567 }
4568
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004569 /* don't use autoindent if nothing was typed. */
4570 if (p[0] == NUL)
4571 theline[0] = NUL;
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 did_undo = TRUE;
4574 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004575 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 vim_free(theline);
4578 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004579
4580 if (empty)
4581 {
4582 ml_delete(2L, FALSE);
4583 empty = FALSE;
4584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 State = NORMAL;
4587
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004588 if (eap->forceit)
4589 curbuf->b_p_ai = !curbuf->b_p_ai;
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004592 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 * "end" is set to lnum when something has been appended, otherwise
4594 * it is the same than "start" -- Acevedo */
4595 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4596 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4597 if (eap->cmdidx != CMD_append)
4598 --curbuf->b_op_start.lnum;
4599 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4600 ? lnum : curbuf->b_op_start.lnum;
4601 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4602 curwin->w_cursor.lnum = lnum;
4603 check_cursor_lnum();
4604 beginline(BL_SOL | BL_FIX);
4605
4606 need_wait_return = FALSE; /* don't use wait_return() now */
4607 ex_no_reprint = TRUE;
4608}
4609
4610/*
4611 * ":change"
4612 */
4613 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004614ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
4616 linenr_T lnum;
4617
4618 if (eap->line2 >= eap->line1
4619 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4620 return;
4621
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004622 /* the ! flag toggles autoindent */
4623 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4624 append_indent = get_indent_lnum(eap->line1);
4625
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4627 {
4628 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4629 break;
4630 ml_delete(eap->line1, FALSE);
4631 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004632
4633 /* make sure the cursor is not beyond the end of the file now */
4634 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4636
4637 /* ":append" on the line above the deleted lines. */
4638 eap->line2 = eap->line1;
4639 ex_append(eap);
4640}
4641
4642 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 char_u *x;
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004646 long bigness;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004647 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 int minus = 0;
4649 linenr_T start, end, curs, i;
4650 int j;
4651 linenr_T lnum = eap->line2;
4652
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004653 /* Vi compatible: ":z!" uses display height, without a count uses
4654 * 'scroll' */
4655 if (eap->forceit)
4656 bigness = curwin->w_height;
Bram Moolenaar459ca562016-11-10 18:16:33 +01004657 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004658 bigness = curwin->w_height - 3;
Bram Moolenaar631abc32014-02-22 22:27:47 +01004659 else
4660 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (bigness < 1)
4662 bigness = 1;
4663
4664 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004665 kind = x;
4666 if (*kind == '-' || *kind == '+' || *kind == '='
4667 || *kind == '^' || *kind == '.')
4668 ++x;
4669 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 ++x;
4671
4672 if (*x != 0)
4673 {
4674 if (!VIM_ISDIGIT(*x))
4675 {
4676 EMSG(_("E144: non-numeric argument to :z"));
4677 return;
4678 }
4679 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004680 {
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004681 bigness = atol((char *)x);
4682
4683 /* bigness could be < 0 if atol(x) overflows. */
4684 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
4685 bigness = 2 * curbuf->b_ml.ml_line_count;
4686
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004687 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004688 if (*kind == '=')
4689 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004693 /* the number of '-' and '+' multiplies the distance */
4694 if (*kind == '-' || *kind == '+')
4695 for (x = kind + 1; *x == *kind; ++x)
4696 ;
4697
4698 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004701 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4702 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004703 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 break;
4705
4706 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004707 start = lnum - (bigness + 1) / 2 + 1;
4708 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 curs = lnum;
4710 minus = 1;
4711 break;
4712
4713 case '^':
4714 start = lnum - bigness * 2;
4715 end = lnum - bigness;
4716 curs = lnum - bigness;
4717 break;
4718
4719 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004720 start = lnum - (bigness + 1) / 2 + 1;
4721 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 curs = end;
4723 break;
4724
4725 default: /* '+' */
4726 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004727 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004728 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004729 else if (eap->addr_count == 0)
4730 ++start;
4731 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 curs = end;
4733 break;
4734 }
4735
4736 if (start < 1)
4737 start = 1;
4738
4739 if (end > curbuf->b_ml.ml_line_count)
4740 end = curbuf->b_ml.ml_line_count;
4741
4742 if (curs > curbuf->b_ml.ml_line_count)
4743 curs = curbuf->b_ml.ml_line_count;
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004744 else if (curs < 1)
4745 curs = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 for (i = start; i <= end; i++)
4748 {
4749 if (minus && i == lnum)
4750 {
4751 msg_putchar('\n');
4752
4753 for (j = 1; j < Columns; j++)
4754 msg_putchar('-');
4755 }
4756
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004757 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 if (minus && i == lnum)
4760 {
4761 msg_putchar('\n');
4762
4763 for (j = 1; j < Columns; j++)
4764 msg_putchar('-');
4765 }
4766 }
4767
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004768 if (curwin->w_cursor.lnum != curs)
4769 {
4770 curwin->w_cursor.lnum = curs;
4771 curwin->w_cursor.col = 0;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 ex_no_reprint = TRUE;
4774}
4775
4776/*
4777 * Check if the restricted flag is set.
4778 * If so, give an error message and return TRUE.
4779 * Otherwise, return FALSE.
4780 */
4781 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004782check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 if (restricted)
4785 {
4786 EMSG(_("E145: Shell commands not allowed in rvim"));
4787 return TRUE;
4788 }
4789 return FALSE;
4790}
4791
4792/*
4793 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4794 * If so, give an error message and return TRUE.
4795 * Otherwise, return FALSE.
4796 */
4797 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004798check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 if (secure)
4801 {
4802 secure = 2;
4803 EMSG(_(e_curdir));
4804 return TRUE;
4805 }
4806#ifdef HAVE_SANDBOX
4807 /*
4808 * In the sandbox more things are not allowed, including the things
4809 * disallowed in secure mode.
4810 */
4811 if (sandbox != 0)
4812 {
4813 EMSG(_(e_sandbox));
4814 return TRUE;
4815 }
4816#endif
4817 return FALSE;
4818}
4819
4820static char_u *old_sub = NULL; /* previous substitute pattern */
4821static int global_need_beginline; /* call beginline() after ":g" */
4822
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004823/*
4824 * Flags that are kept between calls to :substitute.
4825 */
4826typedef struct {
4827 int do_all; /* do multiple substitutions per line */
4828 int do_ask; /* ask for confirmation */
4829 int do_count; /* count only */
4830 int do_error; /* if false, ignore errors */
4831 int do_print; /* print last line with subs. */
4832 int do_list; /* list last line with subs. */
4833 int do_number; /* list last line with line nr*/
4834 int do_ic; /* ignore case flag */
4835} subflags_T;
4836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837/* do_sub()
4838 *
4839 * Perform a substitution from line eap->line1 to line eap->line2 using the
4840 * command pointed to by eap->arg which should be of the form:
4841 *
4842 * /pattern/substitution/{flags}
4843 *
4844 * The usual escapes are supported as described in the regexp docs.
4845 */
4846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004847do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 linenr_T lnum;
4850 long i = 0;
4851 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004852 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4853 FALSE, FALSE, 0};
4854#ifdef FEAT_EVAL
4855 subflags_T subflags_save;
4856#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004857 int save_do_all; /* remember user specified 'g' flag */
4858 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4860 int delimiter;
4861 int sublen;
4862 int got_quit = FALSE;
4863 int got_match = FALSE;
4864 int temp;
4865 int which_pat;
4866 char_u *cmd;
4867 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004868 linenr_T first_line = 0; /* first changed line */
4869 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 * change */
4871 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4872 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004873 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004874 char_u *sub_firstline; /* allocated copy of first sub line */
4875 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004876 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004877 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004878#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004879 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 cmd = eap->arg;
4883 if (!global_busy)
4884 {
4885 sub_nsubs = 0;
4886 sub_nlines = 0;
4887 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004888 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (eap->cmdidx == CMD_tilde)
4891 which_pat = RE_LAST; /* use last used regexp */
4892 else
4893 which_pat = RE_SUBST; /* use last substitute regexp */
4894
4895 /* new pattern and substitution */
Bram Moolenaar1c465442017-03-12 20:10:05 +01004896 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4898 {
4899 /* don't accept alphanumeric for separator */
4900 if (isalpha(*cmd))
4901 {
4902 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4903 return;
4904 }
4905 /*
4906 * undocumented vi feature:
4907 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4908 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4909 */
4910 if (*cmd == '\\')
4911 {
4912 ++cmd;
4913 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4914 {
4915 EMSG(_(e_backslash));
4916 return;
4917 }
4918 if (*cmd != '&')
4919 which_pat = RE_SEARCH; /* use last '/' pattern */
4920 pat = (char_u *)""; /* empty search pattern */
4921 delimiter = *cmd++; /* remember delimiter character */
4922 }
4923 else /* find the end of the regexp */
4924 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004925#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4926 if (p_altkeymap && curwin->w_p_rl)
4927 lrF_sub(cmd);
4928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 which_pat = RE_LAST; /* use last used regexp */
4930 delimiter = *cmd++; /* remember delimiter character */
4931 pat = cmd; /* remember start of search pat */
4932 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4933 if (cmd[0] == delimiter) /* end delimiter found */
4934 *cmd++ = NUL; /* replace it with a NUL */
4935 }
4936
4937 /*
4938 * Small incompatibility: vi sees '\n' as end of the command, but in
4939 * Vim we want to use '\n' to find/substitute a NUL.
4940 */
4941 sub = cmd; /* remember the start of the substitution */
4942
4943 while (cmd[0])
4944 {
4945 if (cmd[0] == delimiter) /* end delimiter found */
4946 {
4947 *cmd++ = NUL; /* replace it with a NUL */
4948 break;
4949 }
4950 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4951 ++cmd;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004952 MB_PTR_ADV(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954
4955 if (!eap->skip)
4956 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004957 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4958 if (STRCMP(sub, "%") == 0
4959 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4960 {
4961 if (old_sub == NULL) /* there is no previous command */
4962 {
4963 EMSG(_(e_nopresub));
4964 return;
4965 }
4966 sub = old_sub;
4967 }
4968 else
4969 {
4970 vim_free(old_sub);
4971 old_sub = vim_strsave(sub);
4972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975 else if (!eap->skip) /* use previous pattern and substitution */
4976 {
4977 if (old_sub == NULL) /* there is no previous command */
4978 {
4979 EMSG(_(e_nopresub));
4980 return;
4981 }
4982 pat = NULL; /* search_regcomp() will use previous pattern */
4983 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004984
4985 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4986 * last column after using "$". */
4987 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004990 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4991 * more efficient.
4992 * TODO: find a generic solution to make line-joining operations more
4993 * efficient, avoid allocating a string that grows in size.
4994 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004995 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004996 && *sub == NUL
4997 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4998 || *cmd == 'p' || *cmd == '#'))))
4999 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005000 linenr_T joined_lines_count;
5001
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005002 curwin->w_cursor.lnum = eap->line1;
5003 if (*cmd == 'l')
5004 eap->flags = EXFLAG_LIST;
5005 else if (*cmd == '#')
5006 eap->flags = EXFLAG_NR;
5007 else if (*cmd == 'p')
5008 eap->flags = EXFLAG_PRINT;
5009
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005010 /* The number of lines joined is the number of lines in the range plus
5011 * one. One less when the last line is included. */
5012 joined_lines_count = eap->line2 - eap->line1 + 1;
5013 if (eap->line2 < curbuf->b_ml.ml_line_count)
5014 ++joined_lines_count;
5015 if (joined_lines_count > 1)
5016 {
5017 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
5018 sub_nsubs = joined_lines_count - 1;
5019 sub_nlines = 1;
5020 (void)do_sub_msg(FALSE);
5021 ex_may_print(eap);
5022 }
5023
5024 if (!cmdmod.keeppatterns)
5025 save_re_pat(RE_SUBST, pat, p_magic);
5026#ifdef FEAT_CMDHIST
5027 /* put pattern in history */
5028 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
5029#endif
5030
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005031 return;
5032 }
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /*
5035 * Find trailing options. When '&' is used, keep old options.
5036 */
5037 if (*cmd == '&')
5038 ++cmd;
5039 else
5040 {
5041 if (!p_ed)
5042 {
5043 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005044 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005046 subflags.do_all = FALSE;
5047 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005049 subflags.do_error = TRUE;
5050 subflags.do_print = FALSE;
5051 subflags.do_count = FALSE;
5052 subflags.do_number = FALSE;
5053 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 while (*cmd)
5056 {
5057 /*
5058 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
5059 * 'r' is never inverted.
5060 */
5061 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005062 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005064 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005065 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005066 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005068 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 else if (*cmd == 'r') /* use last used regexp */
5070 which_pat = RE_LAST;
5071 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005072 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005073 else if (*cmd == '#')
5074 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005075 subflags.do_print = TRUE;
5076 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005077 }
5078 else if (*cmd == 'l')
5079 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005080 subflags.do_print = TRUE;
5081 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005084 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005086 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 else
5088 break;
5089 ++cmd;
5090 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005091 if (subflags.do_count)
5092 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005094 save_do_all = subflags.do_all;
5095 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 /*
5098 * check for a trailing count
5099 */
5100 cmd = skipwhite(cmd);
5101 if (VIM_ISDIGIT(*cmd))
5102 {
5103 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005104 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 EMSG(_(e_zerocount));
5107 return;
5108 }
5109 eap->line1 = eap->line2;
5110 eap->line2 += i - 1;
5111 if (eap->line2 > curbuf->b_ml.ml_line_count)
5112 eap->line2 = curbuf->b_ml.ml_line_count;
5113 }
5114
5115 /*
5116 * check for trailing command or garbage
5117 */
5118 cmd = skipwhite(cmd);
5119 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5120 {
5121 eap->nextcmd = check_nextcmd(cmd);
5122 if (eap->nextcmd == NULL)
5123 {
5124 EMSG(_(e_trailing));
5125 return;
5126 }
5127 }
5128
5129 if (eap->skip) /* not executing commands, only parsing */
5130 return;
5131
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005132 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005133 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005134 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005135 EMSG(_(e_modifiable));
5136 return;
5137 }
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5140 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005141 if (subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 EMSG(_(e_invcmd));
5143 return;
5144 }
5145
5146 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005147 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005149 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 regmatch.rmm_ic = FALSE;
5151
5152 sub_firstline = NULL;
5153
5154 /*
5155 * ~ in the substitute pattern is replaced with the old pattern.
5156 * We do it here once to avoid it to be replaced over and over again.
5157 * But don't do it when it starts with "\=", then it's an expression.
5158 */
5159 if (!(sub[0] == '\\' && sub[1] == '='))
5160 sub = regtilde(sub, p_magic);
5161
5162 /*
5163 * Check for a match on each line.
5164 */
5165 line2 = eap->line2;
5166 for (lnum = eap->line1; lnum <= line2 && !(got_quit
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005167#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 || aborting()
5169#endif
5170 ); ++lnum)
5171 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005172 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005173 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (nmatch)
5175 {
5176 colnr_T copycol;
5177 colnr_T matchcol;
5178 colnr_T prev_matchcol = MAXCOL;
5179 char_u *new_end, *new_start = NULL;
5180 unsigned new_start_len = 0;
5181 char_u *p1;
5182 int did_sub = FALSE;
5183 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005184 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 long nmatch_tl = 0; /* nr of lines matched below lnum */
5186 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005187 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005188 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 /*
5191 * The new text is build up step by step, to avoid too much
5192 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005193 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 * copycol Column in the old text where we started
5195 * looking for a match; from here old text still
5196 * needs to be copied to the new text.
5197 * matchcol Column number of the old text where to look
5198 * for the next match. It's just after the
5199 * previous match or one further.
5200 * prev_matchcol Column just after the previous match (if any).
5201 * Mostly equal to matchcol, except for the first
5202 * match and after skipping an empty match.
5203 * regmatch.*pos Where the pattern matched in the old text.
5204 * new_start The new text, all that has been produced so
5205 * far.
5206 * new_end The new text, where to append new text.
5207 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005208 * lnum The line number where we found the start of
5209 * the match. Can be below the line we searched
5210 * when there is a \n before a \zs in the
5211 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 * sub_firstlnum The line number in the buffer where to look
5213 * for a match. Can be different from "lnum"
5214 * when the pattern or substitute string contains
5215 * line breaks.
5216 *
5217 * Special situations:
5218 * - When the substitute string contains a line break, the part up
5219 * to the line break is inserted in the text, but the copy of
5220 * the original line is kept. "sub_firstlnum" is adjusted for
5221 * the inserted lines.
5222 * - When the matched pattern contains a line break, the old line
5223 * is taken from the line at the end of the pattern. The lines
5224 * in the match are deleted later, "sub_firstlnum" is adjusted
5225 * accordingly.
5226 *
5227 * The new text is built up in new_start[]. It has some extra
5228 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005229 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 *
5231 * Make a copy of the old line, so it won't be taken away when
5232 * updating the screen or handling a multi-line match. The "old_"
5233 * pointers point into this copy.
5234 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005235 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 copycol = 0;
5237 matchcol = 0;
5238
5239 /* At first match, remember current cursor position. */
5240 if (!got_match)
5241 {
5242 setpcmark();
5243 got_match = TRUE;
5244 }
5245
5246 /*
5247 * Loop until nothing more to replace in this line.
5248 * 1. Handle match with empty string.
5249 * 2. If do_ask is set, ask for confirmation.
5250 * 3. substitute the string.
5251 * 4. if do_all is set, find next match
5252 * 5. break if there isn't another match in this line
5253 */
5254 for (;;)
5255 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005256 /* Advance "lnum" to the line where the match starts. The
5257 * match does not start in the first line when there is a line
5258 * break before \zs. */
5259 if (regmatch.startpos[0].lnum > 0)
5260 {
5261 lnum += regmatch.startpos[0].lnum;
5262 sub_firstlnum += regmatch.startpos[0].lnum;
5263 nmatch -= regmatch.startpos[0].lnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005264 VIM_CLEAR(sub_firstline);
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005265 }
5266
5267 if (sub_firstline == NULL)
5268 {
5269 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5270 if (sub_firstline == NULL)
5271 {
5272 vim_free(new_start);
5273 goto outofmem;
5274 }
5275 }
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 /* Save the line number of the last change for the final
5278 * cursor position (just like Vi). */
5279 curwin->w_cursor.lnum = lnum;
5280 do_again = FALSE;
5281
5282 /*
5283 * 1. Match empty string does not count, except for first
5284 * match. This reproduces the strange vi behaviour.
5285 * This also catches endless loops.
5286 */
5287 if (matchcol == prev_matchcol
5288 && regmatch.endpos[0].lnum == 0
5289 && matchcol == regmatch.endpos[0].col)
5290 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005291 if (sub_firstline[matchcol] == NUL)
5292 /* We already were at the end of the line. Don't look
5293 * for a match in this line again. */
5294 skip_match = TRUE;
5295 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005296 {
5297 /* search for a match at next column */
5298#ifdef FEAT_MBYTE
5299 if (has_mbyte)
5300 matchcol += mb_ptr2len(sub_firstline + matchcol);
5301 else
5302#endif
5303 ++matchcol;
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 goto skip;
5306 }
5307
5308 /* Normally we continue searching for a match just after the
5309 * previous match. */
5310 matchcol = regmatch.endpos[0].col;
5311 prev_matchcol = matchcol;
5312
5313 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005314 * 2. If do_count is set only increase the counter.
5315 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005317 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005318 {
5319 /* For a multi-line match, put matchcol at the NUL at
5320 * the end of the line and set nmatch to one, so that
5321 * we continue looking for a match on the next line.
5322 * Avoids that ":s/\nB\@=//gc" get stuck. */
5323 if (nmatch > 1)
5324 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005325 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005326 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005327 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005328 }
5329 sub_nsubs++;
5330 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005331#ifdef FEAT_EVAL
5332 /* Skip the substitution, unless an expression is used,
5333 * then it is evaluated in the sandbox. */
5334 if (!(sub[0] == '\\' && sub[1] == '='))
5335#endif
5336 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005337 }
5338
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005339 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005341 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 /* change State to CONFIRM, so that the mouse works
5344 * properly */
5345 save_State = State;
5346 State = CONFIRM;
5347#ifdef FEAT_MOUSE
5348 setmouse(); /* disable mouse in xterm */
5349#endif
5350 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005351 if (curwin->w_p_crb)
5352 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
5354 /* When 'cpoptions' contains "u" don't sync undo when
5355 * asking for confirmation. */
5356 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5357 ++no_u_sync;
5358
5359 /*
5360 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5361 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005362 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005364 if (exmode_active)
5365 {
5366 char_u *resp;
5367 colnr_T sc, ec;
5368
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005369 print_line_no_prefix(lnum,
5370 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005371
5372 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5373 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005374 if (curwin->w_cursor.col < 0)
5375 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005376 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005377 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005378 {
5379 int numw = number_width(curwin) + 1;
5380 sc += numw;
5381 ec += numw;
5382 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005383 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005384 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005385 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005386 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005387 msg_putchar('^');
5388
5389 resp = getexmodeline('?', NULL, 0);
5390 if (resp != NULL)
5391 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005392 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005393 vim_free(resp);
5394 }
5395 }
5396 else
5397 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005398 char_u *orig_line = NULL;
5399 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005401 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005403 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005405 /* Invert the matched string.
5406 * Remove the inversion afterwards. */
5407 temp = RedrawingDisabled;
5408 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005410 if (new_start != NULL)
5411 {
5412 /* There already was a substitution, we would
5413 * like to show this to the user. We cannot
5414 * really update the line, it would change
5415 * what matches. Temporarily replace the line
5416 * and change it back afterwards. */
5417 orig_line = vim_strsave(ml_get(lnum));
5418 if (orig_line != NULL)
5419 {
5420 char_u *new_line = concat_str(new_start,
5421 sub_firstline + copycol);
5422
5423 if (new_line == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005424 VIM_CLEAR(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005425 else
5426 {
5427 /* Position the cursor relative to the
5428 * end of the line, the previous
5429 * substitute may have inserted or
5430 * deleted characters before the
5431 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005432 len_change = (int)STRLEN(new_line)
5433 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005434 curwin->w_cursor.col += len_change;
5435 ml_replace(lnum, new_line, FALSE);
5436 }
5437 }
5438 }
5439
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005440 search_match_lines = regmatch.endpos[0].lnum
5441 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005442 search_match_endcol = regmatch.endpos[0].col
5443 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005444 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005446 update_topline();
5447 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005448 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005449 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005450 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005453 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005455 if (msg_row == Rows - 1)
5456 msg_didout = FALSE; /* avoid a scroll-up */
5457 msg_starthere();
5458 i = msg_scroll;
5459 msg_scroll = 0; /* truncate msg when
5460 needed */
5461 msg_no_more = TRUE;
5462 /* write message same highlighting as for
5463 * wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005464 smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005465 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
5466 msg_no_more = FALSE;
5467 msg_scroll = i;
5468 showruler(TRUE);
5469 windgoto(msg_row, msg_col);
5470 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005473 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005475 ++no_mapping; /* don't map this key */
5476 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005477 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005478 --allow_keys;
5479 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005481 /* clear the question */
5482 msg_didout = FALSE; /* don't scroll up */
5483 msg_col = 0;
5484 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005485
5486 /* restore the line */
5487 if (orig_line != NULL)
5488 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005489 }
5490
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005492 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005494 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495#endif
5496 )
5497 {
5498 got_quit = TRUE;
5499 break;
5500 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005501 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005503 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005505 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005508 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 line2 = lnum;
5510 break;
5511 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005512 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005514 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 break;
5516 }
5517#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005518 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005520 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 scrolldown_clamp();
5522#endif
5523 }
5524 State = save_State;
5525#ifdef FEAT_MOUSE
5526 setmouse();
5527#endif
5528 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5529 --no_u_sync;
5530
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005531 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
5533 /* For a multi-line match, put matchcol at the NUL at
5534 * the end of the line and set nmatch to one, so that
5535 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005536 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5537 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (nmatch > 1)
5539 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005540 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005541 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543 goto skip;
5544 }
5545 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005546 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
5548
5549 /* Move the cursor to the start of the match, so that we can
5550 * use "\=col("."). */
5551 curwin->w_cursor.col = regmatch.startpos[0].col;
5552
5553 /*
5554 * 3. substitute the string.
5555 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005556#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005557 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005558 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005559 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005560 save_ma = curbuf->b_p_ma;
5561 curbuf->b_p_ma = FALSE;
5562 sandbox++;
5563 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005564 /* Save flags for recursion. They can change for e.g.
5565 * :s/^/\=execute("s#^##gn") */
5566 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005569 sublen = vim_regsub_multi(&regmatch,
5570 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005572#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005573 /* Don't keep flags set by a recursive call. */
5574 subflags = subflags_save;
5575 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005576 {
5577 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005578 if (sandbox > 0)
5579 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005580 goto skip;
5581 }
5582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
Bram Moolenaar4463f292005-09-25 22:20:24 +00005584 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005585 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005586 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5587 {
5588 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5589 skip_match = TRUE;
5590 }
5591
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 /* Need room for:
5593 * - result so far in new_start (not for first sub in line)
5594 * - original text up to match
5595 * - length of substituted part
5596 * - original text after match
5597 */
5598 if (nmatch == 1)
5599 p1 = sub_firstline;
5600 else
5601 {
5602 p1 = ml_get(sub_firstlnum + nmatch - 1);
5603 nmatch_tl += nmatch - 1;
5604 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005605 copy_len = regmatch.startpos[0].col - copycol;
5606 needed_len = copy_len + ((unsigned)STRLEN(p1)
5607 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (new_start == NULL)
5609 {
5610 /*
5611 * Get some space for a temporary buffer to do the
5612 * substitution into (and some extra space to avoid
5613 * too many calls to alloc()/free()).
5614 */
5615 new_start_len = needed_len + 50;
5616 if ((new_start = alloc_check(new_start_len)) == NULL)
5617 goto outofmem;
5618 *new_start = NUL;
5619 new_end = new_start;
5620 }
5621 else
5622 {
5623 /*
5624 * Check if the temporary buffer is long enough to do the
5625 * substitution into. If not, make it larger (with a bit
5626 * extra to avoid too many calls to alloc()/free()).
5627 */
5628 len = (unsigned)STRLEN(new_start);
5629 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005630 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 new_start_len = needed_len + 50;
5633 if ((p1 = alloc_check(new_start_len)) == NULL)
5634 {
5635 vim_free(new_start);
5636 goto outofmem;
5637 }
5638 mch_memmove(p1, new_start, (size_t)(len + 1));
5639 vim_free(new_start);
5640 new_start = p1;
5641 }
5642 new_end = new_start + len;
5643 }
5644
5645 /*
5646 * copy the text up to the part that matched
5647 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005648 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5649 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005651 (void)vim_regsub_multi(&regmatch,
5652 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 sub, new_end, TRUE, p_magic, TRUE);
5654 sub_nsubs++;
5655 did_sub = TRUE;
5656
5657 /* Move the cursor to the start of the line, to avoid that it
5658 * is beyond the end of the line after the substitution. */
5659 curwin->w_cursor.col = 0;
5660
5661 /* For a multi-line match, make a copy of the last matched
5662 * line and continue in that one. */
5663 if (nmatch > 1)
5664 {
5665 sub_firstlnum += nmatch - 1;
5666 vim_free(sub_firstline);
5667 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5668 /* When going beyond the last line, stop substituting. */
5669 if (sub_firstlnum <= line2)
5670 do_again = TRUE;
5671 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005672 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674
5675 /* Remember next character to be copied. */
5676 copycol = regmatch.endpos[0].col;
5677
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005678 if (skip_match)
5679 {
5680 /* Already hit end of the buffer, sub_firstlnum is one
5681 * less than what it ought to be. */
5682 vim_free(sub_firstline);
5683 sub_firstline = vim_strsave((char_u *)"");
5684 copycol = 0;
5685 }
5686
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 /*
5688 * Now the trick is to replace CTRL-M chars with a real line
5689 * break. This would make it impossible to insert a CTRL-M in
5690 * the text. The line break can be avoided by preceding the
5691 * CTRL-M with a backslash. To be able to insert a backslash,
5692 * they must be doubled in the string and are halved here.
5693 * That is Vi compatible.
5694 */
5695 for (p1 = new_end; *p1; ++p1)
5696 {
5697 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005698 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 else if (*p1 == CAR)
5700 {
5701 if (u_inssub(lnum) == OK) /* prepare for undo */
5702 {
5703 *p1 = NUL; /* truncate up to the CR */
5704 ml_append(lnum - 1, new_start,
5705 (colnr_T)(p1 - new_start + 1), FALSE);
5706 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005707 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 appended_lines(lnum - 1, 1L);
5709 else
5710 {
5711 if (first_line == 0)
5712 first_line = lnum;
5713 last_line = lnum + 1;
5714 }
5715 /* All line numbers increase. */
5716 ++sub_firstlnum;
5717 ++lnum;
5718 ++line2;
5719 /* move the cursor to the new line, like Vi */
5720 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005721 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005722 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 p1 = new_start - 1;
5724 }
5725 }
5726#ifdef FEAT_MBYTE
5727 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005728 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729#endif
5730 }
5731
5732 /*
5733 * 4. If do_all is set, find next match.
5734 * Prevent endless loop with patterns that match empty
5735 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5736 * But ":s/\n/#/" is OK.
5737 */
5738skip:
5739 /* We already know that we did the last subst when we are at
5740 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005741 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5742 * "line2" when there is a \zs in the pattern after a line
5743 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005744 lastone = (skip_match
5745 || got_int
5746 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005747 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005748 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005749 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5750 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 nmatch = -1;
5752
5753 /*
5754 * Replace the line in the buffer when needed. This is
5755 * skipped when there are more matches.
5756 * The check for nmatch_tl is needed for when multi-line
5757 * matching must replace the lines before trying to do another
5758 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005759 * When the match starts below where we start searching also
5760 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 */
5762 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 || nmatch_tl > 0
5764 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005765 curbuf, sub_firstlnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005766 matchcol, NULL, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005767 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
5769 if (new_start != NULL)
5770 {
5771 /*
5772 * Copy the rest of the line, that didn't match.
5773 * "matchcol" has to be adjusted, we use the end of
5774 * the line as reference, because the substitute may
5775 * have changed the number of characters. Same for
5776 * "prev_matchcol".
5777 */
5778 STRCAT(new_start, sub_firstline + copycol);
5779 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5780 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5781 - prev_matchcol;
5782
5783 if (u_savesub(lnum) != OK)
5784 break;
5785 ml_replace(lnum, new_start, TRUE);
5786
5787 if (nmatch_tl > 0)
5788 {
5789 /*
5790 * Matched lines have now been substituted and are
5791 * useless, delete them. The part after the match
5792 * has been appended to new_start, we don't need
5793 * it in the buffer.
5794 */
5795 ++lnum;
5796 if (u_savedel(lnum, nmatch_tl) != OK)
5797 break;
5798 for (i = 0; i < nmatch_tl; ++i)
5799 ml_delete(lnum, (int)FALSE);
5800 mark_adjust(lnum, lnum + nmatch_tl - 1,
5801 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005802 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 deleted_lines(lnum, nmatch_tl);
5804 --lnum;
5805 line2 -= nmatch_tl; /* nr of lines decreases */
5806 nmatch_tl = 0;
5807 }
5808
5809 /* When asking, undo is saved each time, must also set
5810 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005811 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 changed_bytes(lnum, 0);
5813 else
5814 {
5815 if (first_line == 0)
5816 first_line = lnum;
5817 last_line = lnum + 1;
5818 }
5819
5820 sub_firstlnum = lnum;
5821 vim_free(sub_firstline); /* free the temp buffer */
5822 sub_firstline = new_start;
5823 new_start = NULL;
5824 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5825 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5826 - prev_matchcol;
5827 copycol = 0;
5828 }
5829 if (nmatch == -1 && !lastone)
5830 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005831 sub_firstlnum, matchcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
5833 /*
5834 * 5. break if there isn't another match in this line
5835 */
5836 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005837 {
5838 /* If the match found didn't start where we were
5839 * searching, do the next search in the line where we
5840 * found the match. */
5841 if (nmatch == -1)
5842 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
5846
5847 line_breakcheck();
5848 }
5849
5850 if (did_sub)
5851 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005852 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaard23a8232018-02-10 18:45:26 +01005853 VIM_CLEAR(sub_firstline); /* free the copy of the original line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 }
5855
5856 line_breakcheck();
5857 }
5858
5859 if (first_line != 0)
5860 {
5861 /* Need to subtract the number of added lines from "last_line" to get
5862 * the line number before the change (same as adding the number of
5863 * deleted lines). */
5864 i = curbuf->b_ml.ml_line_count - old_line_count;
5865 changed_lines(first_line, 0, last_line - i, i);
5866 }
5867
5868outofmem:
5869 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005870
5871 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005872 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005873 curwin->w_cursor = old_cursor;
5874
Bram Moolenaar46475522010-03-23 17:36:29 +01005875 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
5877 /* Set the '[ and '] marks. */
5878 curbuf->b_op_start.lnum = eap->line1;
5879 curbuf->b_op_end.lnum = line2;
5880 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5881
5882 if (!global_busy)
5883 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005884 /* when interactive leave cursor on the match */
5885 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005886 {
5887 if (endcolumn)
5888 coladvance((colnr_T)MAXCOL);
5889 else
5890 beginline(BL_WHITE | BL_FIX);
5891 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005892 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 MSG("");
5894 }
5895 else
5896 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005897 if (subflags.do_print)
5898 print_line(curwin->w_cursor.lnum,
5899 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901 else if (!global_busy)
5902 {
5903 if (got_int) /* interrupted */
5904 EMSG(_(e_interr));
5905 else if (got_match) /* did find something but nothing substituted */
5906 MSG("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005907 else if (subflags.do_error) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 EMSG2(_(e_patnotf2), get_search_pat());
5909 }
5910
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005911#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005912 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005913 /* Cursor position may require updating */
5914 changed_window_setting();
5915#endif
5916
Bram Moolenaar473de612013-06-08 18:19:48 +02005917 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005918
5919 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005920 subflags.do_all = save_do_all;
5921 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922}
5923
5924/*
5925 * Give message for number of substitutions.
5926 * Can also be used after a ":global" command.
5927 * Return TRUE if a message was given.
5928 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005929 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930do_sub_msg(
5931 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
5933 /*
5934 * Only report substitutions when:
5935 * - more than 'report' substitutions
5936 * - command was typed by user, or number of changed lines > 'report'
5937 * - giving messages is not disabled by 'lazyredraw'
5938 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005939 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5940 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 && messaging())
5942 {
5943 if (got_int)
5944 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005945 else
5946 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005948 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005949 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005951 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005952 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 sub_nsubs);
5954 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005955 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005956 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005958 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005959 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005962 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 return TRUE;
5964 }
5965 if (got_int)
5966 {
5967 EMSG(_(e_interr));
5968 return TRUE;
5969 }
5970 return FALSE;
5971}
5972
Bram Moolenaarf84b1222017-06-10 14:29:52 +02005973 static void
5974global_exe_one(char_u *cmd, linenr_T lnum)
5975{
5976 curwin->w_cursor.lnum = lnum;
5977 curwin->w_cursor.col = 0;
5978 if (*cmd == NUL || *cmd == '\n')
5979 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5980 else
5981 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5982}
5983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984/*
5985 * Execute a global command of the form:
5986 *
5987 * g/pattern/X : execute X on all lines where pattern matches
5988 * v/pattern/X : execute X on all lines where pattern does not match
5989 *
5990 * where 'X' is an EX command
5991 *
5992 * The command character (as well as the trailing slash) is optional, and
5993 * is assumed to be 'p' if missing.
5994 *
5995 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005996 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * for each line that has a mark. This is required because after deleting
5998 * lines we do not know where to search for the next match.
5999 */
6000 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006004 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 int type; /* first char of cmd: 'v' or 'g' */
6006 char_u *cmd; /* command argument */
6007
6008 char_u delim; /* delimiter, normally '/' */
6009 char_u *pat;
6010 regmmatch_T regmatch;
6011 int match;
6012 int which_pat;
6013
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006014 /* When nesting the command works on one line. This allows for
6015 * ":g/found/v/notfound/command". */
6016 if (global_busy && (eap->line1 != 1
6017 || eap->line2 != curbuf->b_ml.ml_line_count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006019 /* will increment global_busy to break out of the loop */
6020 EMSG(_("E147: Cannot do :global recursive with a range"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 return;
6022 }
6023
6024 if (eap->forceit) /* ":global!" is like ":vglobal" */
6025 type = 'v';
6026 else
6027 type = *eap->cmd;
6028 cmd = eap->arg;
6029 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 /*
6032 * undocumented vi feature:
6033 * "\/" and "\?": use previous search pattern.
6034 * "\&": use previous substitute pattern.
6035 */
6036 if (*cmd == '\\')
6037 {
6038 ++cmd;
6039 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
6040 {
6041 EMSG(_(e_backslash));
6042 return;
6043 }
6044 if (*cmd == '&')
6045 which_pat = RE_SUBST; /* use previous substitute pattern */
6046 else
6047 which_pat = RE_SEARCH; /* use previous search pattern */
6048 ++cmd;
6049 pat = (char_u *)"";
6050 }
6051 else if (*cmd == NUL)
6052 {
6053 EMSG(_("E148: Regular expression missing from global"));
6054 return;
6055 }
6056 else
6057 {
6058 delim = *cmd; /* get the delimiter */
6059 if (delim)
6060 ++cmd; /* skip delimiter if there is one */
6061 pat = cmd; /* remember start of pattern */
6062 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
6063 if (cmd[0] == delim) /* end delimiter found */
6064 *cmd++ = NUL; /* replace it with a NUL */
6065 }
6066
6067#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
6068 if (p_altkeymap && curwin->w_p_rl)
6069 lrFswap(pat,0);
6070#endif
6071
6072 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
6073 {
6074 EMSG(_(e_invcmd));
6075 return;
6076 }
6077
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006078 if (global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006080 lnum = curwin->w_cursor.lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006081 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006082 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if ((type == 'g' && match) || (type == 'v' && !match))
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006084 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 }
6086 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006087 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006088 /*
6089 * pass 1: set marks for each (not) matching line
6090 */
6091 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6092 {
6093 /* a match on this line? */
6094 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006095 (colnr_T)0, NULL, NULL);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006096 if ((type == 'g' && match) || (type == 'v' && !match))
6097 {
6098 ml_setmarked(lnum);
6099 ndone++;
6100 }
6101 line_breakcheck();
6102 }
6103
6104 /*
6105 * pass 2: execute the command for each line that has been marked
6106 */
6107 if (got_int)
6108 MSG(_(e_interr));
6109 else if (ndone == 0)
6110 {
6111 if (type == 'v')
6112 smsg((char_u *)_("Pattern found in every line: %s"), pat);
6113 else
6114 smsg((char_u *)_("Pattern not found: %s"), pat);
6115 }
6116 else
6117 {
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006118#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006119 start_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006120#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006121 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006122#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006123 end_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006124#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006125 }
6126
6127 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
Bram Moolenaar473de612013-06-08 18:19:48 +02006130 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131}
6132
6133/*
6134 * Execute "cmd" on lines marked with ml_setmarked().
6135 */
6136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006137global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006139 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6140 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6141 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 /*
6144 * Set current position only once for a global command.
6145 * If global_busy is set, setpcmark() will not do anything.
6146 * If there is an error, global_busy will be incremented.
6147 */
6148 setpcmark();
6149
6150 /* When the command writes a message, don't overwrite the command. */
6151 msg_didout = TRUE;
6152
Bram Moolenaard25bc232010-03-23 17:49:24 +01006153 sub_nsubs = 0;
6154 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 global_need_beginline = FALSE;
6156 global_busy = 1;
6157 old_lcount = curbuf->b_ml.ml_line_count;
6158 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6159 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006160 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 ui_breakcheck();
6162 }
6163
6164 global_busy = 0;
6165 if (global_need_beginline)
6166 beginline(BL_WHITE | BL_FIX);
6167 else
6168 check_cursor(); /* cursor may be beyond the end of the line */
6169
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006170 /* the cursor may not have moved in the text but a change in a previous
6171 * line may move it on the screen */
6172 changed_line_abv_curs();
6173
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 /* If it looks like no message was written, allow overwriting the
6175 * command with the report for number of changes. */
6176 if (msg_col == 0 && msg_scrolled == 0)
6177 msg_didout = FALSE;
6178
Bram Moolenaar45667512007-05-10 19:24:43 +00006179 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006180 * number of extra or deleted lines.
6181 * Don't report extra or deleted lines in the edge case where the buffer
6182 * we are in after execution is different from the buffer we started in. */
6183 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6185}
6186
6187#ifdef FEAT_VIMINFO
6188 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006191 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 vim_free(old_sub);
6193 if (force || old_sub == NULL)
6194 old_sub = viminfo_readstring(virp, 1, TRUE);
6195 return viminfo_readline(virp);
6196}
6197
6198 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006199write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6202 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006203 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 viminfo_writestring(fp, old_sub);
6205 }
6206}
6207#endif /* FEAT_VIMINFO */
6208
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006209#if defined(EXITFREE) || defined(PROTO)
6210 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006211free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006212{
6213 vim_free(old_sub);
6214}
6215#endif
6216
Bram Moolenaar4033c552017-09-16 20:54:51 +02006217#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218/*
6219 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006220 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006222 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006223prepare_tagpreview(
6224 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 win_T *wp;
6227
6228# ifdef FEAT_GUI
6229 need_mouse_correct = TRUE;
6230# endif
6231
6232 /*
6233 * If there is already a preview window open, use that one.
6234 */
6235 if (!curwin->w_p_pvw)
6236 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006237 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (wp->w_p_pvw)
6239 break;
6240 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006241 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 else
6243 {
6244 /*
6245 * There is no preview window open yet. Create one.
6246 */
6247 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6248 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006249 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 curwin->w_p_pvw = TRUE;
6251 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006252 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6253 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254# ifdef FEAT_DIFF
6255 curwin->w_p_diff = FALSE; /* no 'diff' */
6256# endif
6257# ifdef FEAT_FOLDING
6258 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6259# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006260 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006263 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264}
6265
6266#endif
6267
6268
6269/*
6270 * ":help": open a read-only window on a help file
6271 */
6272 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006273ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 char_u *arg;
6276 char_u *tag;
6277 FILE *helpfd; /* file descriptor of help file */
6278 int n;
6279 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 int num_matches;
6282 char_u **matches;
6283 char_u *p;
6284 int empty_fnum = 0;
6285 int alt_fnum = 0;
6286 buf_T *buf;
6287#ifdef FEAT_MULTI_LANG
6288 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006289 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006291#ifdef FEAT_FOLDING
6292 int old_KeyTyped = KeyTyped;
6293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 if (eap != NULL)
6296 {
6297 /*
6298 * A ":help" command ends at the first LF, or at a '|' that is
6299 * followed by some text. Set nextcmd to the following command.
6300 */
6301 for (arg = eap->arg; *arg; ++arg)
6302 {
6303 if (*arg == '\n' || *arg == '\r'
6304 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6305 {
6306 *arg++ = NUL;
6307 eap->nextcmd = arg;
6308 break;
6309 }
6310 }
6311 arg = eap->arg;
6312
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006313 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
6315 EMSG(_("E478: Don't panic!"));
6316 return;
6317 }
6318
6319 if (eap->skip) /* not executing commands */
6320 return;
6321 }
6322 else
6323 arg = (char_u *)"";
6324
6325 /* remove trailing blanks */
6326 p = arg + STRLEN(arg) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006327 while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 *p-- = NUL;
6329
6330#ifdef FEAT_MULTI_LANG
6331 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006332 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333#endif
6334
6335 /* When no argument given go to the index. */
6336 if (*arg == NUL)
6337 arg = (char_u *)"help.txt";
6338
6339 /*
6340 * Check if there is a match for the argument.
6341 */
6342 n = find_help_tags(arg, &num_matches, &matches,
6343 eap != NULL && eap->forceit);
6344
6345 i = 0;
6346#ifdef FEAT_MULTI_LANG
6347 if (n != FAIL && lang != NULL)
6348 /* Find first item with the requested language. */
6349 for (i = 0; i < num_matches; ++i)
6350 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006351 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (len > 3 && matches[i][len - 3] == '@'
6353 && STRICMP(matches[i] + len - 2, lang) == 0)
6354 break;
6355 }
6356#endif
6357 if (i >= num_matches || n == FAIL)
6358 {
6359#ifdef FEAT_MULTI_LANG
6360 if (lang != NULL)
6361 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
6362 else
6363#endif
6364 EMSG2(_("E149: Sorry, no help for %s"), arg);
6365 if (n != FAIL)
6366 FreeWild(num_matches, matches);
6367 return;
6368 }
6369
6370 /* The first match (in the requested language) is the best match. */
6371 tag = vim_strsave(matches[i]);
6372 FreeWild(num_matches, matches);
6373
6374#ifdef FEAT_GUI
6375 need_mouse_correct = TRUE;
6376#endif
6377
6378 /*
6379 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006380 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006382 if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006384 if (cmdmod.tab != 0)
6385 wp = NULL;
6386 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006387 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006388 if (bt_help(wp->w_buffer))
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6391 win_enter(wp, TRUE);
6392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 {
6394 /*
6395 * There is no help window yet.
6396 * Try to open the file specified by the "helpfile" option.
6397 */
6398 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6399 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006400 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 goto erret;
6402 }
6403 fclose(helpfd);
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006406 * specified, the current window is vertically split and
6407 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006410 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006413 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (curwin->w_height < p_hh)
6416 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418 /*
6419 * Open help file (do_ecmd() will set b_help flag, readfile() will
6420 * set b_p_ro flag).
6421 * Set the alternate file to the previously edited file.
6422 */
6423 alt_fnum = curbuf->b_fnum;
6424 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006425 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaar4033c552017-09-16 20:54:51 +02006426 NULL); /* buffer is still open, don't store info */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006427 if (!cmdmod.keepalt)
6428 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 empty_fnum = curbuf->b_fnum;
6430 }
6431 }
6432
6433 if (!p_im)
6434 restart_edit = 0; /* don't want insert mode in help file */
6435
Bram Moolenaar8f535582011-09-30 17:30:31 +02006436#ifdef FEAT_FOLDING
6437 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6438 * It is needed for do_tag top open folds under the cursor. */
6439 KeyTyped = old_KeyTyped;
6440#endif
6441
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (tag != NULL)
6443 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6444
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006445 /* Delete the empty buffer if we're not using it. Careful: autocommands
6446 * may have jumped to another window, check that the buffer is not in a
6447 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6449 {
6450 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006451 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 wipe_buffer(buf, TRUE);
6453 }
6454
6455 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006456 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 curwin->w_alt_fnum = alt_fnum;
6458
6459erret:
6460 vim_free(tag);
6461}
6462
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006463/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006464 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006465 */
6466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006467ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006468{
6469 win_T *win;
6470
6471 FOR_ALL_WINDOWS(win)
6472 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006473 if (bt_help(win->w_buffer))
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006474 {
6475 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006476 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006477 }
6478 }
6479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006481#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6482/*
6483 * In an argument search for a language specifiers in the form "@xx".
6484 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6485 * Returns NULL if not found.
6486 */
6487 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006488check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006489{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006490 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006491
6492 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6493 && ASCII_ISALPHA(arg[len - 1]))
6494 {
6495 arg[len - 3] = NUL; /* remove the '@' */
6496 return arg + len - 2;
6497 }
6498 return NULL;
6499}
6500#endif
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502/*
6503 * Return a heuristic indicating how well the given string matches. The
6504 * smaller the number, the better the match. This is the order of priorities,
6505 * from best match to worst match:
6506 * - Match with least alpha-numeric characters is better.
6507 * - Match with least total characters is better.
6508 * - Match towards the start is better.
6509 * - Match starting with "+" is worse (feature instead of command)
6510 * Assumption is made that the matched_string passed has already been found to
6511 * match some string for which help is requested. webb.
6512 */
6513 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514help_heuristic(
6515 char_u *matched_string,
6516 int offset, /* offset for match */
6517 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 int num_letters;
6520 char_u *p;
6521
6522 num_letters = 0;
6523 for (p = matched_string; *p; p++)
6524 if (ASCII_ISALNUM(*p))
6525 num_letters++;
6526
6527 /*
6528 * Multiply the number of letters by 100 to give it a much bigger
6529 * weighting than the number of characters.
6530 * If there only is a match while ignoring case, add 5000.
6531 * If the match starts in the middle of a word, add 10000 to put it
6532 * somewhere in the last half.
6533 * If the match is more than 2 chars from the start, multiply by 200 to
6534 * put it after matches at the start.
6535 */
6536 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6537 && ASCII_ISALNUM(matched_string[offset - 1]))
6538 offset += 10000;
6539 else if (offset > 2)
6540 offset *= 200;
6541 if (wrong_case)
6542 offset += 5000;
6543 /* Features are less interesting than the subjects themselves, but "+"
6544 * alone is not a feature. */
6545 if (matched_string[0] == '+' && matched_string[1] != NUL)
6546 offset += 100;
6547 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6548}
6549
6550/*
6551 * Compare functions for qsort() below, that checks the help heuristics number
6552 * that has been put after the tagname by find_tags().
6553 */
6554 static int
6555#ifdef __BORLANDC__
6556_RTLENTRYF
6557#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006558help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 char *p1;
6561 char *p2;
6562
6563 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6564 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6565 return strcmp(p1, p2);
6566}
6567
6568/*
6569 * Find all help tags matching "arg", sort them and return in matches[], with
6570 * the number of matches in num_matches.
6571 * The matches will be sorted with a "best" match algorithm.
6572 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6573 */
6574 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006575find_help_tags(
6576 char_u *arg,
6577 int *num_matches,
6578 char_u ***matches,
6579 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 char_u *s, *d;
6582 int i;
6583 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006584 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006585 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006586 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6587 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006588 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006589 "[count]", "[quotex]",
6590 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006591 "[pattern]", "\\|", "\\%$",
6592 "s/\\~", "s/\\U", "s/\\L",
6593 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006595 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006596 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006597 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6598 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006599 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006600 "\\[count]", "\\[quotex]",
6601 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006602 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006603 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006604 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006605 static char *(expr_table[]) = {"!=?", "!~?", "<=?", "<?", "==?", "=~?",
6606 ">=?", ">?", "is?", "isnot?"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 int flags;
6608
6609 d = IObuff; /* assume IObuff is long enough! */
6610
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006611 if (STRNICMP(arg, "expr-", 5) == 0)
6612 {
6613 // When the string starting with "expr-" and containing '?' and matches
6614 // the table, it is taken literally. Otherwise '?' is recognized as a
6615 // wildcard.
6616 for (i = (int)(sizeof(expr_table) / sizeof(char *)); --i >= 0; )
6617 if (STRCMP(arg + 5, expr_table[i]) == 0)
6618 {
6619 STRCPY(d, arg);
6620 break;
6621 }
6622 }
6623 else
6624 {
6625 // Recognize a few exceptions to the rule. Some strings that contain
6626 // '*' with "star". Otherwise '*' is recognized as a wildcard.
6627 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
6628 if (STRCMP(arg, mtable[i]) == 0)
6629 {
6630 STRCPY(d, rtable[i]);
6631 break;
6632 }
6633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635 if (i < 0) /* no match in table */
6636 {
6637 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6638 * Also replace "\%^" and "\%(", they match every tag too.
6639 * Also "\zs", "\z1", etc.
6640 * Also "\@<", "\@=", "\@<=", etc.
6641 * And also "\_$" and "\_^". */
6642 if (arg[0] == '\\'
6643 && ((arg[1] != NUL && arg[2] == NUL)
6644 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6645 && arg[2] != NUL)))
6646 {
6647 STRCPY(d, "/\\\\");
6648 STRCPY(d + 3, arg + 1);
6649 /* Check for "/\\_$", should be "/\\_\$" */
6650 if (d[3] == '_' && d[4] == '$')
6651 STRCPY(d + 4, "\\$");
6652 }
6653 else
6654 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006655 /* Replace:
6656 * "[:...:]" with "\[:...:]"
6657 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006658 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006659 */
6660 if ((arg[0] == '[' && (arg[1] == ':'
6661 || (arg[1] == '+' && arg[2] == '+')))
6662 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 *d++ = '\\';
6664
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006665 /*
6666 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6667 */
6668 if (*arg == '(' && arg[1] == '\'')
6669 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 for (s = arg; *s; ++s)
6671 {
6672 /*
6673 * Replace "|" with "bar" and '"' with "quote" to match the name of
6674 * the tags for these commands.
6675 * Replace "*" with ".*" and "?" with "." to match command line
6676 * completion.
6677 * Insert a backslash before '~', '$' and '.' to avoid their
6678 * special meaning.
6679 */
6680 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6681 break;
6682 switch (*s)
6683 {
6684 case '|': STRCPY(d, "bar");
6685 d += 3;
6686 continue;
6687 case '"': STRCPY(d, "quote");
6688 d += 5;
6689 continue;
6690 case '*': *d++ = '.';
6691 break;
6692 case '?': *d++ = '.';
6693 continue;
6694 case '$':
6695 case '.':
6696 case '~': *d++ = '\\';
6697 break;
6698 }
6699
6700 /*
6701 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6702 * ":help i_^_CTRL-D" work.
6703 * Insert '-' before and after "CTRL-X" when applicable.
6704 */
6705 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6706 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6707 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006708 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6709 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 STRCPY(d, "CTRL-");
6711 d += 5;
6712 if (*s < ' ')
6713 {
6714#ifdef EBCDIC
6715 *d++ = CtrlChar(*s);
6716#else
6717 *d++ = *s + '@';
6718#endif
6719 if (d[-1] == '\\')
6720 *d++ = '\\'; /* double a backslash */
6721 }
6722 else
6723 *d++ = *++s;
6724 if (s[1] != NUL && s[1] != '_')
6725 *d++ = '_'; /* append a '_' */
6726 continue;
6727 }
6728 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6729 *d++ = '\\';
6730
6731 /*
6732 * Insert a backslash before a backslash after a slash, for search
6733 * pattern tags: "/\|" --> "/\\|".
6734 */
6735 else if (s[0] == '\\' && s[1] != '\\'
6736 && *arg == '/' && s == arg + 1)
6737 *d++ = '\\';
6738
6739 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6740 * "CTRL-\_CTRL-N" */
6741 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6742 {
6743 STRCPY(d, "CTRL-\\\\");
6744 d += 7;
6745 s += 6;
6746 }
6747
6748 *d++ = *s;
6749
6750 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006751 * If tag contains "({" or "([", tag terminates at the "(".
6752 * This is for help on functions, e.g.: abs({expr}).
6753 */
6754 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6755 break;
6756
6757 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 * If tag starts with ', toss everything after a second '. Fixes
6759 * CTRL-] on 'option'. (would include the trailing '.').
6760 */
6761 if (*s == '\'' && s > arg && *arg == '\'')
6762 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006763 /* Also '{' and '}'. */
6764 if (*s == '}' && s > arg && *arg == '{')
6765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 }
6767 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006768
6769 if (*IObuff == '`')
6770 {
6771 if (d > IObuff + 2 && d[-1] == '`')
6772 {
6773 /* remove the backticks from `command` */
6774 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6775 d[-2] = NUL;
6776 }
6777 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6778 {
6779 /* remove the backticks and comma from `command`, */
6780 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6781 d[-3] = NUL;
6782 }
6783 else if (d > IObuff + 4 && d[-3] == '`'
6784 && d[-2] == '\\' && d[-1] == '.')
6785 {
6786 /* remove the backticks and dot from `command`\. */
6787 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6788 d[-4] = NUL;
6789 }
6790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 }
6792 }
6793
6794 *matches = (char_u **)"";
6795 *num_matches = 0;
6796 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6797 if (keep_lang)
6798 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006799 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 /* Sort the matches found on the heuristic number that is after the
6803 * tag name. */
6804 qsort((void *)*matches, (size_t)*num_matches,
6805 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006806 /* Delete more than TAG_MANY to reduce the size of the listing. */
6807 while (*num_matches > TAG_MANY)
6808 vim_free((*matches)[--*num_matches]);
6809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 return OK;
6811}
6812
6813/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006814 * Called when starting to edit a buffer for a help file.
6815 */
6816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006817prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006818{
6819 char_u *p;
6820
6821 curbuf->b_help = TRUE;
6822#ifdef FEAT_QUICKFIX
6823 set_string_option_direct((char_u *)"buftype", -1,
6824 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6825#endif
6826
6827 /*
6828 * Always set these options after jumping to a help tag, because the
6829 * user may have an autocommand that gets in the way.
6830 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6831 * latin1 word characters (for translated help files).
6832 * Only set it when needed, buf_init_chartab() is some work.
6833 */
6834 p =
6835#ifdef EBCDIC
6836 (char_u *)"65-255,^*,^|,^\"";
6837#else
6838 (char_u *)"!-~,^*,^|,^\",192-255";
6839#endif
6840 if (STRCMP(curbuf->b_p_isk, p) != 0)
6841 {
6842 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6843 check_buf_options(curbuf);
6844 (void)buf_init_chartab(curbuf, FALSE);
6845 }
6846
Bram Moolenaar0b105412014-11-30 13:34:23 +01006847#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006848 /* Don't use the global foldmethod.*/
6849 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6850 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006851#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006852
6853 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6854 curwin->w_p_list = FALSE; /* no list mode */
6855
6856 curbuf->b_p_ma = FALSE; /* not modifiable */
6857 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6858 curwin->w_p_nu = 0; /* no line numbers */
6859 curwin->w_p_rnu = 0; /* no relative line numbers */
6860 RESET_BINDING(curwin); /* no scroll or cursor binding */
6861#ifdef FEAT_ARABIC
6862 curwin->w_p_arab = FALSE; /* no arabic mode */
6863#endif
6864#ifdef FEAT_RIGHTLEFT
6865 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6866#endif
6867#ifdef FEAT_FOLDING
6868 curwin->w_p_fen = FALSE; /* No folding in the help window */
6869#endif
6870#ifdef FEAT_DIFF
6871 curwin->w_p_diff = FALSE; /* No 'diff' */
6872#endif
6873#ifdef FEAT_SPELL
6874 curwin->w_p_spell = FALSE; /* No spell checking */
6875#endif
6876
6877 set_buflisted(FALSE);
6878}
6879
6880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 * After reading a help file: May cleanup a help buffer when syntax
6882 * highlighting is not used.
6883 */
6884 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 linenr_T lnum;
6888 char_u *line;
6889 int in_example = FALSE;
6890 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006891 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 char_u *p;
6893 char_u *rt;
6894 int mustfree;
6895
Bram Moolenaar90492982017-06-22 14:16:31 +02006896 /* Set filetype to "help" if still needed. */
6897 if (STRCMP(curbuf->b_p_ft, "help") != 0)
Bram Moolenaar18141832017-06-25 21:17:25 +02006898 {
6899 ++curbuf_lock;
Bram Moolenaar90492982017-06-22 14:16:31 +02006900 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
Bram Moolenaar18141832017-06-25 21:17:25 +02006901 --curbuf_lock;
6902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006905 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#endif
6907 {
6908 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6909 {
6910 line = ml_get_buf(curbuf, lnum, FALSE);
6911 len = (int)STRLEN(line);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006912 if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
6914 /* End of example: non-white or '<' in first column. */
6915 if (line[0] == '<')
6916 {
6917 /* blank-out a '<' in the first column */
6918 line = ml_get_buf(curbuf, lnum, TRUE);
6919 line[0] = ' ';
6920 }
6921 in_example = FALSE;
6922 }
6923 if (!in_example && len > 0)
6924 {
6925 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6926 {
6927 /* blank-out a '>' in the last column (start of example) */
6928 line = ml_get_buf(curbuf, lnum, TRUE);
6929 line[len - 1] = ' ';
6930 in_example = TRUE;
6931 }
6932 else if (line[len - 1] == '~')
6933 {
6934 /* blank-out a '~' at the end of line (header marker) */
6935 line = ml_get_buf(curbuf, lnum, TRUE);
6936 line[len - 1] = ' ';
6937 }
6938 }
6939 }
6940 }
6941
6942 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006943 * In the "help.txt" and "help.abx" file, add the locally added help
6944 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006946 fname = gettail(curbuf->b_fname);
6947 if (fnamecmp(fname, "help.txt") == 0
6948#ifdef FEAT_MULTI_LANG
6949 || (fnamencmp(fname, "help.", 5) == 0
6950 && ASCII_ISALPHA(fname[5])
6951 && ASCII_ISALPHA(fname[6])
6952 && TOLOWER_ASC(fname[7]) == 'x'
6953 && fname[8] == NUL)
6954#endif
6955 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6958 {
6959 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006960 if (strstr((char *)line, "*local-additions*") == NULL)
6961 continue;
6962
6963 /* Go through all directories in 'runtimepath', skipping
6964 * $VIMRUNTIME. */
6965 p = p_rtp;
6966 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006968 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6969 mustfree = FALSE;
6970 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
Bram Moolenaare4db7ae2018-02-13 13:12:11 +01006971 if (rt != NULL && fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006973 int fcount;
6974 char_u **fnames;
6975 FILE *fd;
6976 char_u *s;
6977 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006979 vimconv_T vc;
6980 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981#endif
6982
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006983 /* Find all "doc/ *.txt" files in this directory. */
6984 add_pathsep(NameBuff);
6985#ifdef FEAT_MULTI_LANG
6986 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006988 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006990 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6991 &fnames, EW_FILE|EW_SILENT) == OK
6992 && fcount > 0)
6993 {
6994#ifdef FEAT_MULTI_LANG
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006995 int i1, i2;
6996 char_u *f1, *f2;
6997 char_u *t1, *t2;
6998 char_u *e1, *e2;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006999
7000 /* If foo.abx is found use it instead of foo.txt in
7001 * the same directory. */
7002 for (i1 = 0; i1 < fcount; ++i1)
7003 {
7004 for (i2 = 0; i2 < fcount; ++i2)
7005 {
7006 if (i1 == i2)
7007 continue;
7008 if (fnames[i1] == NULL || fnames[i2] == NULL)
7009 continue;
7010 f1 = fnames[i1];
7011 f2 = fnames[i2];
7012 t1 = gettail(f1);
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007013 t2 = gettail(f2);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007014 e1 = vim_strrchr(t1, '.');
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007015 e2 = vim_strrchr(t2, '.');
Bram Moolenaar7756e742016-10-21 20:35:37 +02007016 if (e1 == NULL || e2 == NULL)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007017 continue;
7018 if (fnamecmp(e1, ".txt") != 0
7019 && fnamecmp(e1, fname + 4) != 0)
7020 {
7021 /* Not .txt and not .abx, remove it. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007022 VIM_CLEAR(fnames[i1]);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007023 continue;
7024 }
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007025 if (e1 - f1 != e2 - f2
7026 || fnamencmp(f1, f2, e1 - f1) != 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007027 continue;
7028 if (fnamecmp(e1, ".txt") == 0
7029 && fnamecmp(e2, fname + 4) == 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007030 /* use .abx instead of .txt */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007031 VIM_CLEAR(fnames[i1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007034#endif
7035 for (fi = 0; fi < fcount; ++fi)
7036 {
7037 if (fnames[fi] == NULL)
7038 continue;
7039 fd = mch_fopen((char *)fnames[fi], "r");
7040 if (fd != NULL)
7041 {
7042 vim_fgets(IObuff, IOSIZE, fd);
7043 if (IObuff[0] == '*'
7044 && (s = vim_strchr(IObuff + 1, '*'))
7045 != NULL)
7046 {
7047#ifdef FEAT_MBYTE
7048 int this_utf = MAYBE;
7049#endif
7050 /* Change tag definition to a
7051 * reference and remove <CR>/<NL>. */
7052 IObuff[0] = '|';
7053 *s = '|';
7054 while (*s != NUL)
7055 {
7056 if (*s == '\r' || *s == '\n')
7057 *s = NUL;
7058#ifdef FEAT_MBYTE
7059 /* The text is utf-8 when a byte
7060 * above 127 is found and no
7061 * illegal byte sequence is found.
7062 */
7063 if (*s >= 0x80 && this_utf != FALSE)
7064 {
7065 int l;
7066
7067 this_utf = TRUE;
7068 l = utf_ptr2len(s);
7069 if (l == 1)
7070 this_utf = FALSE;
7071 s += l - 1;
7072 }
7073#endif
7074 ++s;
7075 }
7076#ifdef FEAT_MBYTE
7077 /* The help file is latin1 or utf-8;
7078 * conversion to the current
7079 * 'encoding' may be required. */
7080 vc.vc_type = CONV_NONE;
7081 convert_setup(&vc, (char_u *)(
7082 this_utf == TRUE ? "utf-8"
7083 : "latin1"), p_enc);
7084 if (vc.vc_type == CONV_NONE)
7085 /* No conversion needed. */
7086 cp = IObuff;
7087 else
7088 {
7089 /* Do the conversion. If it fails
7090 * use the unconverted text. */
7091 cp = string_convert(&vc, IObuff,
7092 NULL);
7093 if (cp == NULL)
7094 cp = IObuff;
7095 }
7096 convert_setup(&vc, NULL, NULL);
7097
7098 ml_append(lnum, cp, (colnr_T)0, FALSE);
7099 if (cp != IObuff)
7100 vim_free(cp);
7101#else
7102 ml_append(lnum, IObuff, (colnr_T)0,
7103 FALSE);
7104#endif
7105 ++lnum;
7106 }
7107 fclose(fd);
7108 }
7109 }
7110 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007113 if (mustfree)
7114 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
7118 }
7119}
7120
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007121/*
7122 * ":exusage"
7123 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007124 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007125ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007126{
7127 do_cmdline_cmd((char_u *)"help ex-cmd-index");
7128}
7129
7130/*
7131 * ":viusage"
7132 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007133 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007134ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007135{
7136 do_cmdline_cmd((char_u *)"help normal-index");
7137}
7138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007140 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007143helptags_one(
7144 char_u *dir, /* doc directory */
7145 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
7146 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
7147 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
7149 FILE *fd_tags;
7150 FILE *fd;
7151 garray_T ga;
7152 int filecount;
7153 char_u **files;
7154 char_u *p1, *p2;
7155 int fi;
7156 char_u *s;
7157 int i;
7158 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007159 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160# ifdef FEAT_MBYTE
7161 int utf8 = MAYBE;
7162 int this_utf8;
7163 int firstline;
7164 int mix = FALSE; /* detected mixed encodings */
7165# endif
7166
7167 /*
7168 * Find all *.txt files.
7169 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007170 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007172 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 STRCAT(NameBuff, ext);
7174 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7175 EW_FILE|EW_SILENT) == FAIL
7176 || filecount == 0)
7177 {
7178 if (!got_int)
Bram Moolenaar5b302912016-08-24 22:11:55 +02007179 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 return;
7181 }
7182
7183 /*
7184 * Open the tags file for writing.
7185 * Do this before scanning through all the files.
7186 */
7187 STRCPY(NameBuff, dir);
7188 add_pathsep(NameBuff);
7189 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007190 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (fd_tags == NULL)
7192 {
7193 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
7194 FreeWild(filecount, files);
7195 return;
7196 }
7197
7198 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007199 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7200 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
7202 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007203 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
7204 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
7206 if (ga_grow(&ga, 1) == FAIL)
7207 got_int = TRUE;
7208 else
7209 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007210 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 if (s == NULL)
7212 got_int = TRUE;
7213 else
7214 {
7215 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7216 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7217 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
7219 }
7220 }
7221
7222 /*
7223 * Go over all the files and extract the tags.
7224 */
7225 for (fi = 0; fi < filecount && !got_int; ++fi)
7226 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007227 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (fd == NULL)
7229 {
7230 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
7231 continue;
7232 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007233 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235# ifdef FEAT_MBYTE
7236 firstline = TRUE;
7237# endif
7238 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7239 {
7240# ifdef FEAT_MBYTE
7241 if (firstline)
7242 {
7243 /* Detect utf-8 file by a non-ASCII char in the first line. */
7244 this_utf8 = MAYBE;
7245 for (s = IObuff; *s != NUL; ++s)
7246 if (*s >= 0x80)
7247 {
7248 int l;
7249
7250 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007251 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (l == 1)
7253 {
7254 /* Illegal UTF-8 byte sequence. */
7255 this_utf8 = FALSE;
7256 break;
7257 }
7258 s += l - 1;
7259 }
7260 if (this_utf8 == MAYBE) /* only ASCII characters found */
7261 this_utf8 = FALSE;
7262 if (utf8 == MAYBE) /* first file */
7263 utf8 = this_utf8;
7264 else if (utf8 != this_utf8)
7265 {
7266 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
7267 mix = !got_int;
7268 got_int = TRUE;
7269 }
7270 firstline = FALSE;
7271 }
7272# endif
7273 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7274 while (p1 != NULL)
7275 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007276 /* Use vim_strbyte() instead of vim_strchr() so that when
7277 * 'encoding' is dbcs it still works, don't find '*' in the
7278 * second byte. */
7279 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7281 {
7282 for (s = p1 + 1; s < p2; ++s)
7283 if (*s == ' ' || *s == '\t' || *s == '|')
7284 break;
7285
7286 /*
7287 * Only accept a *tag* when it consists of valid
7288 * characters, there is white space before it and is
7289 * followed by a white character or end-of-line.
7290 */
7291 if (s == p2
7292 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7293 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7294 || s[1] == '\0'))
7295 {
7296 *p2 = '\0';
7297 ++p1;
7298 if (ga_grow(&ga, 1) == FAIL)
7299 {
7300 got_int = TRUE;
7301 break;
7302 }
7303 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7304 if (s == NULL)
7305 {
7306 got_int = TRUE;
7307 break;
7308 }
7309 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7310 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 sprintf((char *)s, "%s\t%s", p1, fname);
7312
7313 /* find next '*' */
7314 p2 = vim_strchr(p2 + 1, '*');
7315 }
7316 }
7317 p1 = p2;
7318 }
7319 line_breakcheck();
7320 }
7321
7322 fclose(fd);
7323 }
7324
7325 FreeWild(filecount, files);
7326
7327 if (!got_int)
7328 {
7329 /*
7330 * Sort the tags.
7331 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007332 if (ga.ga_data != NULL)
7333 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
7335 /*
7336 * Check for duplicates.
7337 */
7338 for (i = 1; i < ga.ga_len; ++i)
7339 {
7340 p1 = ((char_u **)ga.ga_data)[i - 1];
7341 p2 = ((char_u **)ga.ga_data)[i];
7342 while (*p1 == *p2)
7343 {
7344 if (*p2 == '\t')
7345 {
7346 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007347 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7349 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
7350 EMSG(NameBuff);
7351 *p2 = '\t';
7352 break;
7353 }
7354 ++p1;
7355 ++p2;
7356 }
7357 }
7358
7359# ifdef FEAT_MBYTE
7360 if (utf8 == TRUE)
7361 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
7362# endif
7363
7364 /*
7365 * Write the tags into the file.
7366 */
7367 for (i = 0; i < ga.ga_len; ++i)
7368 {
7369 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007370 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007372 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 else
7374 {
7375 fprintf(fd_tags, "%s\t/*", s);
7376 for (p1 = s; *p1 != '\t'; ++p1)
7377 {
7378 /* insert backslash before '\\' and '/' */
7379 if (*p1 == '\\' || *p1 == '/')
7380 putc('\\', fd_tags);
7381 putc(*p1, fd_tags);
7382 }
7383 fprintf(fd_tags, "*\n");
7384 }
7385 }
7386 }
7387#ifdef FEAT_MBYTE
7388 if (mix)
7389 got_int = FALSE; /* continue with other languages */
7390#endif
7391
7392 for (i = 0; i < ga.ga_len; ++i)
7393 vim_free(((char_u **)ga.ga_data)[i]);
7394 ga_clear(&ga);
7395 fclose(fd_tags); /* there is no check for an error... */
7396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007398/*
7399 * Generate tags in one help directory, taking care of translations.
7400 */
7401 static void
7402do_helptags(char_u *dirname, int add_help_tags)
7403{
7404#ifdef FEAT_MULTI_LANG
7405 int len;
7406 int i, j;
7407 garray_T ga;
7408 char_u lang[2];
7409 char_u ext[5];
7410 char_u fname[8];
7411 int filecount;
7412 char_u **files;
7413
7414 /* Get a list of all files in the help directory and in subdirectories. */
7415 STRCPY(NameBuff, dirname);
7416 add_pathsep(NameBuff);
7417 STRCAT(NameBuff, "**");
7418 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7419 EW_FILE|EW_SILENT) == FAIL
7420 || filecount == 0)
7421 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007422 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007423 return;
7424 }
7425
7426 /* Go over all files in the directory to find out what languages are
7427 * present. */
7428 ga_init2(&ga, 1, 10);
7429 for (i = 0; i < filecount; ++i)
7430 {
7431 len = (int)STRLEN(files[i]);
7432 if (len > 4)
7433 {
7434 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7435 {
7436 /* ".txt" -> language "en" */
7437 lang[0] = 'e';
7438 lang[1] = 'n';
7439 }
7440 else if (files[i][len - 4] == '.'
7441 && ASCII_ISALPHA(files[i][len - 3])
7442 && ASCII_ISALPHA(files[i][len - 2])
7443 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7444 {
7445 /* ".abx" -> language "ab" */
7446 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7447 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7448 }
7449 else
7450 continue;
7451
7452 /* Did we find this language already? */
7453 for (j = 0; j < ga.ga_len; j += 2)
7454 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7455 break;
7456 if (j == ga.ga_len)
7457 {
7458 /* New language, add it. */
7459 if (ga_grow(&ga, 2) == FAIL)
7460 break;
7461 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7462 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7463 }
7464 }
7465 }
7466
7467 /*
7468 * Loop over the found languages to generate a tags file for each one.
7469 */
7470 for (j = 0; j < ga.ga_len; j += 2)
7471 {
7472 STRCPY(fname, "tags-xx");
7473 fname[5] = ((char_u *)ga.ga_data)[j];
7474 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7475 if (fname[5] == 'e' && fname[6] == 'n')
7476 {
7477 /* English is an exception: use ".txt" and "tags". */
7478 fname[4] = NUL;
7479 STRCPY(ext, ".txt");
7480 }
7481 else
7482 {
7483 /* Language "ab" uses ".abx" and "tags-ab". */
7484 STRCPY(ext, ".xxx");
7485 ext[1] = fname[5];
7486 ext[2] = fname[6];
7487 }
7488 helptags_one(dirname, ext, fname, add_help_tags);
7489 }
7490
7491 ga_clear(&ga);
7492 FreeWild(filecount, files);
7493
7494#else
7495 /* No language support, just use "*.txt" and "tags". */
7496 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7497#endif
7498}
7499
7500 static void
7501helptags_cb(char_u *fname, void *cookie)
7502{
7503 do_helptags(fname, *(int *)cookie);
7504}
7505
7506/*
7507 * ":helptags"
7508 */
7509 void
7510ex_helptags(exarg_T *eap)
7511{
7512 expand_T xpc;
7513 char_u *dirname;
7514 int add_help_tags = FALSE;
7515
7516 /* Check for ":helptags ++t {dir}". */
Bram Moolenaar1c465442017-03-12 20:10:05 +01007517 if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3]))
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007518 {
7519 add_help_tags = TRUE;
7520 eap->arg = skipwhite(eap->arg + 3);
7521 }
7522
7523 if (STRCMP(eap->arg, "ALL") == 0)
7524 {
7525 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7526 helptags_cb, &add_help_tags);
7527 }
7528 else
7529 {
7530 ExpandInit(&xpc);
7531 xpc.xp_context = EXPAND_DIRECTORIES;
7532 dirname = ExpandOne(&xpc, eap->arg, NULL,
7533 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7534 if (dirname == NULL || !mch_isdir(dirname))
7535 EMSG2(_("E150: Not a directory: %s"), eap->arg);
7536 else
7537 do_helptags(dirname, add_help_tags);
7538 vim_free(dirname);
7539 }
7540}
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542#if defined(FEAT_SIGNS) || defined(PROTO)
7543
7544/*
7545 * Struct to hold the sign properties.
7546 */
7547typedef struct sign sign_T;
7548
7549struct sign
7550{
7551 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007552 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 char_u *sn_name; /* name of sign */
7554 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007555# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 char_u *sn_text; /* text used instead of pixmap */
7559 int sn_line_hl; /* highlight ID for line */
7560 int sn_text_hl; /* highlight ID for text */
7561};
7562
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007564static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007566static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
7567static void sign_list_defined(sign_T *sp);
7568static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007570static char *cmds[] = {
7571 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007572# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007573 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007574# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007575 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007576# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007577 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007578# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007579 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007580# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007581 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007582# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007583 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007584# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007585};
7586
7587/*
7588 * Find index of a ":sign" subcmd from its name.
7589 * "*end_cmd" must be writable.
7590 */
7591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007592sign_cmd_idx(
7593 char_u *begin_cmd, /* begin of sign subcmd */
7594 char_u *end_cmd) /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007595{
7596 int idx;
7597 char save = *end_cmd;
7598
7599 *end_cmd = NUL;
7600 for (idx = 0; ; ++idx)
7601 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7602 break;
7603 *end_cmd = save;
7604 return idx;
7605}
7606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607/*
7608 * ":sign" command
7609 */
7610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007611ex_sign(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 char_u *arg = eap->arg;
7614 char_u *p;
7615 int idx;
7616 sign_T *sp;
7617 sign_T *sp_prev;
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01007618 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620 /* Parse the subcommand. */
7621 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007622 idx = sign_cmd_idx(arg, p);
7623 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007625 EMSG2(_("E160: Unknown sign command: %s"), arg);
7626 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628 arg = skipwhite(p);
7629
7630 if (idx <= SIGNCMD_LIST)
7631 {
7632 /*
7633 * Define, undefine or list signs.
7634 */
7635 if (idx == SIGNCMD_LIST && *arg == NUL)
7636 {
7637 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007638 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 sign_list_defined(sp);
7640 }
7641 else if (*arg == NUL)
7642 EMSG(_("E156: Missing sign name"));
7643 else
7644 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007645 /* Isolate the sign name. If it's a number skip leading zeroes,
7646 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 p = skiptowhite(arg);
7648 if (*p != NUL)
7649 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007650 while (arg[0] == '0' && arg[1] != NUL)
7651 ++arg;
7652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 sp_prev = NULL;
7654 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7655 {
7656 if (STRCMP(sp->sn_name, arg) == 0)
7657 break;
7658 sp_prev = sp;
7659 }
7660 if (idx == SIGNCMD_DEFINE)
7661 {
7662 /* ":sign define {name} ...": define a sign */
7663 if (sp == NULL)
7664 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007665 sign_T *lp;
7666 int start = next_sign_typenr;
7667
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 /* Allocate a new sign. */
7669 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7670 if (sp == NULL)
7671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007673 /* Check that next_sign_typenr is not already being used.
7674 * This only happens after wrapping around. Hopefully
7675 * another one got deleted and we can use its number. */
7676 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007678 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007680 ++next_sign_typenr;
7681 if (next_sign_typenr == MAX_TYPENR)
7682 next_sign_typenr = 1;
7683 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007685 vim_free(sp);
7686 EMSG(_("E612: Too many signs defined"));
7687 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007689 lp = first_sign; /* start all over */
7690 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007692 lp = lp->sn_next;
7693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007695 sp->sn_typenr = next_sign_typenr;
7696 if (++next_sign_typenr == MAX_TYPENR)
7697 next_sign_typenr = 1; /* wrap around */
7698
7699 sp->sn_name = vim_strsave(arg);
7700 if (sp->sn_name == NULL) /* out of memory */
7701 {
7702 vim_free(sp);
7703 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007705
7706 /* add the new sign to the list of signs */
7707 if (sp_prev == NULL)
7708 first_sign = sp;
7709 else
7710 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 }
7712
7713 /* set values for a defined sign. */
7714 for (;;)
7715 {
7716 arg = skipwhite(p);
7717 if (*arg == NUL)
7718 break;
7719 p = skiptowhite_esc(arg);
7720 if (STRNCMP(arg, "icon=", 5) == 0)
7721 {
7722 arg += 5;
7723 vim_free(sp->sn_icon);
7724 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7725 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007726# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 if (gui.in_use)
7728 {
7729 out_flush();
7730 if (sp->sn_image != NULL)
7731 gui_mch_destroy_sign(sp->sn_image);
7732 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7733 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736 else if (STRNCMP(arg, "text=", 5) == 0)
7737 {
7738 char_u *s;
7739 int cells;
7740 int len;
7741
7742 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007743# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 /* Count cells and check for non-printable chars */
7745 if (has_mbyte)
7746 {
7747 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007748 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 if (!vim_isprintc((*mb_ptr2char)(s)))
7751 break;
7752 cells += (*mb_ptr2cells)(s);
7753 }
7754 }
7755 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 for (s = arg; s < p; ++s)
7759 if (!vim_isprintc(*s))
7760 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007761 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763 /* Currently must be one or two display cells */
7764 if (s != p || cells < 1 || cells > 2)
7765 {
7766 *p = NUL;
7767 EMSG2(_("E239: Invalid sign text: %s"), arg);
7768 return;
7769 }
7770
7771 vim_free(sp->sn_text);
7772 /* Allocate one byte more if we need to pad up
7773 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007774 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 sp->sn_text = vim_strnsave(arg, len);
7776
7777 if (sp->sn_text != NULL && cells == 1)
7778 STRCPY(sp->sn_text + len - 1, " ");
7779 }
7780 else if (STRNCMP(arg, "linehl=", 7) == 0)
7781 {
7782 arg += 7;
7783 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7784 }
7785 else if (STRNCMP(arg, "texthl=", 7) == 0)
7786 {
7787 arg += 7;
7788 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7789 }
7790 else
7791 {
7792 EMSG2(_(e_invarg2), arg);
7793 return;
7794 }
7795 }
7796 }
7797 else if (sp == NULL)
7798 EMSG2(_("E155: Unknown sign: %s"), arg);
7799 else if (idx == SIGNCMD_LIST)
7800 /* ":sign list {name}" */
7801 sign_list_defined(sp);
7802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007804 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
7806 }
7807 else
7808 {
7809 int id = -1;
7810 linenr_T lnum = -1;
7811 char_u *sign_name = NULL;
7812 char_u *arg1;
7813
7814 if (*arg == NUL)
7815 {
7816 if (idx == SIGNCMD_PLACE)
7817 {
7818 /* ":sign place": list placed signs in all buffers */
7819 sign_list_placed(NULL);
7820 }
7821 else if (idx == SIGNCMD_UNPLACE)
7822 {
7823 /* ":sign unplace": remove placed sign at cursor */
7824 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7825 if (id > 0)
7826 {
7827 buf_delsign(curwin->w_buffer, id);
7828 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7829 }
7830 else
7831 EMSG(_("E159: Missing sign number"));
7832 }
7833 else
7834 EMSG(_(e_argreq));
7835 return;
7836 }
7837
7838 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7839 {
7840 /* ":sign unplace *": remove all placed signs */
7841 buf_delete_all_signs();
7842 return;
7843 }
7844
7845 /* first arg could be placed sign id */
7846 arg1 = arg;
7847 if (VIM_ISDIGIT(*arg))
7848 {
7849 id = getdigits(&arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007850 if (!VIM_ISWHITE(*arg) && *arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {
7852 id = -1;
7853 arg = arg1;
7854 }
7855 else
7856 {
7857 arg = skipwhite(arg);
7858 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7859 {
7860 /* ":sign unplace {id}": remove placed sign by number */
Bram Moolenaar29323592016-07-24 22:04:11 +02007861 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if ((lnum = buf_delsign(buf, id)) != 0)
7863 update_debug_sign(buf, lnum);
7864 return;
7865 }
7866 }
7867 }
7868
7869 /*
7870 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7871 * Leave "arg" pointing to {fname}.
7872 */
7873 for (;;)
7874 {
7875 if (STRNCMP(arg, "line=", 5) == 0)
7876 {
7877 arg += 5;
7878 lnum = atoi((char *)arg);
7879 arg = skiptowhite(arg);
7880 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007881 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7882 {
7883 if (id != -1)
7884 {
7885 EMSG(_(e_invarg));
7886 return;
7887 }
7888 id = -2;
7889 arg = skiptowhite(arg + 1);
7890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 else if (STRNCMP(arg, "name=", 5) == 0)
7892 {
7893 arg += 5;
7894 sign_name = arg;
7895 arg = skiptowhite(arg);
7896 if (*arg != NUL)
7897 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007898 while (sign_name[0] == '0' && sign_name[1] != NUL)
7899 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
7901 else if (STRNCMP(arg, "file=", 5) == 0)
7902 {
7903 arg += 5;
7904 buf = buflist_findname(arg);
7905 break;
7906 }
7907 else if (STRNCMP(arg, "buffer=", 7) == 0)
7908 {
7909 arg += 7;
7910 buf = buflist_findnr((int)getdigits(&arg));
7911 if (*skipwhite(arg) != NUL)
7912 EMSG(_(e_trailing));
7913 break;
7914 }
7915 else
7916 {
7917 EMSG(_(e_invarg));
7918 return;
7919 }
7920 arg = skipwhite(arg);
7921 }
7922
7923 if (buf == NULL)
7924 {
7925 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7926 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007927 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {
7929 if (lnum >= 0 || sign_name != NULL)
7930 EMSG(_(e_invarg));
7931 else
7932 /* ":sign place file={fname}": list placed signs in one file */
7933 sign_list_placed(buf);
7934 }
7935 else if (idx == SIGNCMD_JUMP)
7936 {
7937 /* ":sign jump {id} file={fname}" */
7938 if (lnum >= 0 || sign_name != NULL)
7939 EMSG(_(e_invarg));
7940 else if ((lnum = buf_findsign(buf, id)) > 0)
7941 { /* goto a sign ... */
7942 if (buf_jump_open_win(buf) != NULL)
7943 { /* ... in a current window */
7944 curwin->w_cursor.lnum = lnum;
7945 check_cursor_lnum();
7946 beginline(BL_WHITE);
7947 }
7948 else
7949 { /* ... not currently in a window */
7950 char_u *cmd;
7951
Bram Moolenaarbfd096d2016-08-17 22:29:09 +02007952 if (buf->b_fname == NULL)
7953 {
7954 EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
7955 return;
7956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7958 if (cmd == NULL)
7959 return;
7960 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7961 do_cmdline_cmd(cmd);
7962 vim_free(cmd);
7963 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007964# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 }
7968 else
7969 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7970 }
7971 else if (idx == SIGNCMD_UNPLACE)
7972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 if (lnum >= 0 || sign_name != NULL)
7974 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007975 else if (id == -2)
7976 {
7977 /* ":sign unplace * file={fname}" */
7978 redraw_buf_later(buf, NOT_VALID);
7979 buf_delete_signs(buf);
7980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 else
7982 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007983 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 lnum = buf_delsign(buf, id);
7985 update_debug_sign(buf, lnum);
7986 }
7987 }
7988 /* idx == SIGNCMD_PLACE */
7989 else if (sign_name != NULL)
7990 {
7991 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7992 if (STRCMP(sp->sn_name, sign_name) == 0)
7993 break;
7994 if (sp == NULL)
7995 {
7996 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7997 return;
7998 }
7999 if (lnum > 0)
8000 /* ":sign place {id} line={lnum} name={name} file={fname}":
8001 * place a sign */
8002 buf_addsign(buf, id, lnum, sp->sn_typenr);
8003 else
8004 /* ":sign place {id} file={fname}": change sign type */
8005 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02008006 if (lnum > 0)
8007 update_debug_sign(buf, lnum);
8008 else
8009 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
8011 else
8012 EMSG(_(e_invarg));
8013 }
8014}
8015
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008016# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017/*
8018 * Allocate the icons. Called when the GUI has started. Allows defining
8019 * signs before it starts.
8020 */
8021 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008022sign_gui_started(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 sign_T *sp;
8025
8026 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8027 if (sp->sn_icon != NULL)
8028 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
8029}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031
8032/*
8033 * List one sign.
8034 */
8035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008036sign_list_defined(sign_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
8038 char_u *p;
8039
Bram Moolenaar555b2802005-05-19 21:08:39 +00008040 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (sp->sn_icon != NULL)
8042 {
8043 MSG_PUTS(" icon=");
8044 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008045# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 if (sp->sn_image == NULL)
8047 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008048# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052 if (sp->sn_text != NULL)
8053 {
8054 MSG_PUTS(" text=");
8055 msg_outtrans(sp->sn_text);
8056 }
8057 if (sp->sn_line_hl > 0)
8058 {
8059 MSG_PUTS(" linehl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008060 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 if (p == NULL)
8062 MSG_PUTS("NONE");
8063 else
8064 msg_puts(p);
8065 }
8066 if (sp->sn_text_hl > 0)
8067 {
8068 MSG_PUTS(" texthl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008069 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 if (p == NULL)
8071 MSG_PUTS("NONE");
8072 else
8073 msg_puts(p);
8074 }
8075}
8076
8077/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008078 * Undefine a sign and free its memory.
8079 */
8080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008081sign_undefine(sign_T *sp, sign_T *sp_prev)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008082{
8083 vim_free(sp->sn_name);
8084 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008085# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008086 if (sp->sn_image != NULL)
8087 {
8088 out_flush();
8089 gui_mch_destroy_sign(sp->sn_image);
8090 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008091# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008092 vim_free(sp->sn_text);
8093 if (sp_prev == NULL)
8094 first_sign = sp->sn_next;
8095 else
8096 sp_prev->sn_next = sp->sn_next;
8097 vim_free(sp);
8098}
8099
8100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 * Get highlighting attribute for sign "typenr".
8102 * If "line" is TRUE: line highl, if FALSE: text highl.
8103 */
8104 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008105sign_get_attr(int typenr, int line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 sign_T *sp;
8108
8109 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8110 if (sp->sn_typenr == typenr)
8111 {
8112 if (line)
8113 {
8114 if (sp->sn_line_hl > 0)
8115 return syn_id2attr(sp->sn_line_hl);
8116 }
8117 else
8118 {
8119 if (sp->sn_text_hl > 0)
8120 return syn_id2attr(sp->sn_text_hl);
8121 }
8122 break;
8123 }
8124 return 0;
8125}
8126
8127/*
8128 * Get text mark for sign "typenr".
8129 * Returns NULL if there isn't one.
8130 */
8131 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008132sign_get_text(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
8134 sign_T *sp;
8135
8136 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8137 if (sp->sn_typenr == typenr)
8138 return sp->sn_text;
8139 return NULL;
8140}
8141
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008142# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008144sign_get_image(
8145 int typenr) /* the attribute which may have a sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 sign_T *sp;
8148
8149 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8150 if (sp->sn_typenr == typenr)
8151 return sp->sn_image;
8152 return NULL;
8153}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
8156/*
8157 * Get the name of a sign by its typenr.
8158 */
8159 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008160sign_typenr2name(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
8162 sign_T *sp;
8163
8164 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8165 if (sp->sn_typenr == typenr)
8166 return sp->sn_name;
8167 return (char_u *)_("[Deleted]");
8168}
8169
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008170# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008171/*
8172 * Undefine/free all signs.
8173 */
8174 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175free_signs(void)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008176{
8177 while (first_sign != NULL)
8178 sign_undefine(first_sign, NULL);
8179}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008180# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008181
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008182# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008183static enum
8184{
8185 EXP_SUBCMD, /* expand :sign sub-commands */
8186 EXP_DEFINE, /* expand :sign define {name} args */
8187 EXP_PLACE, /* expand :sign place {id} args */
8188 EXP_UNPLACE, /* expand :sign unplace" */
8189 EXP_SIGN_NAMES /* expand with name of placed signs */
8190} expand_what;
8191
8192/*
8193 * Function given to ExpandGeneric() to obtain the sign command
8194 * expansion.
8195 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008196 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008197get_sign_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008198{
8199 sign_T *sp;
8200 int current_idx;
8201
8202 switch (expand_what)
8203 {
8204 case EXP_SUBCMD:
8205 return (char_u *)cmds[idx];
8206 case EXP_DEFINE:
8207 {
8208 char *define_arg[] =
8209 {
8210 "icon=", "linehl=", "text=", "texthl=", NULL
8211 };
8212 return (char_u *)define_arg[idx];
8213 }
8214 case EXP_PLACE:
8215 {
8216 char *place_arg[] =
8217 {
8218 "line=", "name=", "file=", "buffer=", NULL
8219 };
8220 return (char_u *)place_arg[idx];
8221 }
8222 case EXP_UNPLACE:
8223 {
8224 char *unplace_arg[] = { "file=", "buffer=", NULL };
8225 return (char_u *)unplace_arg[idx];
8226 }
8227 case EXP_SIGN_NAMES:
8228 /* Complete with name of signs already defined */
8229 current_idx = 0;
8230 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8231 if (current_idx++ == idx)
8232 return sp->sn_name;
8233 return NULL;
8234 default:
8235 return NULL;
8236 }
8237}
8238
8239/*
8240 * Handle command line completion for :sign command.
8241 */
8242 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008243set_context_in_sign_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008244{
8245 char_u *p;
8246 char_u *end_subcmd;
8247 char_u *last;
8248 int cmd_idx;
8249 char_u *begin_subcmd_args;
8250
8251 /* Default: expand subcommands. */
8252 xp->xp_context = EXPAND_SIGN;
8253 expand_what = EXP_SUBCMD;
8254 xp->xp_pattern = arg;
8255
8256 end_subcmd = skiptowhite(arg);
8257 if (*end_subcmd == NUL)
8258 /* expand subcmd name
8259 * :sign {subcmd}<CTRL-D>*/
8260 return;
8261
8262 cmd_idx = sign_cmd_idx(arg, end_subcmd);
8263
8264 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008265 * |
8266 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008267 begin_subcmd_args = skipwhite(end_subcmd);
8268 p = skiptowhite(begin_subcmd_args);
8269 if (*p == NUL)
8270 {
8271 /*
8272 * Expand first argument of subcmd when possible.
8273 * For ":jump {id}" and ":unplace {id}", we could
8274 * possibly expand the ids of all signs already placed.
8275 */
8276 xp->xp_pattern = begin_subcmd_args;
8277 switch (cmd_idx)
8278 {
8279 case SIGNCMD_LIST:
8280 case SIGNCMD_UNDEFINE:
8281 /* :sign list <CTRL-D>
8282 * :sign undefine <CTRL-D> */
8283 expand_what = EXP_SIGN_NAMES;
8284 break;
8285 default:
8286 xp->xp_context = EXPAND_NOTHING;
8287 }
8288 return;
8289 }
8290
8291 /* expand last argument of subcmd */
8292
8293 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008294 * |
8295 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008296
8297 /* Loop until reaching last argument. */
8298 do
8299 {
8300 p = skipwhite(p);
8301 last = p;
8302 p = skiptowhite(p);
8303 } while (*p != NUL);
8304
8305 p = vim_strchr(last, '=');
8306
8307 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008308 * | |
8309 * last p */
Bram Moolenaar7756e742016-10-21 20:35:37 +02008310 if (p == NULL)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008311 {
8312 /* Expand last argument name (before equal sign). */
8313 xp->xp_pattern = last;
8314 switch (cmd_idx)
8315 {
8316 case SIGNCMD_DEFINE:
8317 expand_what = EXP_DEFINE;
8318 break;
8319 case SIGNCMD_PLACE:
8320 expand_what = EXP_PLACE;
8321 break;
8322 case SIGNCMD_JUMP:
8323 case SIGNCMD_UNPLACE:
8324 expand_what = EXP_UNPLACE;
8325 break;
8326 default:
8327 xp->xp_context = EXPAND_NOTHING;
8328 }
8329 }
8330 else
8331 {
8332 /* Expand last argument value (after equal sign). */
8333 xp->xp_pattern = p + 1;
8334 switch (cmd_idx)
8335 {
8336 case SIGNCMD_DEFINE:
8337 if (STRNCMP(last, "texthl", p - last) == 0 ||
8338 STRNCMP(last, "linehl", p - last) == 0)
8339 xp->xp_context = EXPAND_HIGHLIGHT;
8340 else if (STRNCMP(last, "icon", p - last) == 0)
8341 xp->xp_context = EXPAND_FILES;
8342 else
8343 xp->xp_context = EXPAND_NOTHING;
8344 break;
8345 case SIGNCMD_PLACE:
8346 if (STRNCMP(last, "name", p - last) == 0)
8347 expand_what = EXP_SIGN_NAMES;
8348 else
8349 xp->xp_context = EXPAND_NOTHING;
8350 break;
8351 default:
8352 xp->xp_context = EXPAND_NOTHING;
8353 }
8354 }
8355}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008356# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008357#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008358
8359/*
8360 * Make the user happy.
8361 */
8362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008363ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008364{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008365 static char *code[] = {
8366 "\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$",
8367 "\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"
8368 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008369 char *p;
8370 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008371 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008372
8373 msg_start();
8374 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008375 for (i = 0; i < 2; ++i)
8376 for (p = code[i]; *p != NUL; ++p)
8377 if (*p == 'x')
8378 msg_putchar('\n');
8379 else
8380 for (n = *p++; n > 0; --n)
8381 if (*p == 'o' || *p == '$')
Bram Moolenaar8820b482017-03-16 17:23:31 +01008382 msg_putchar_attr(*p, HL_ATTR(HLF_L));
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008383 else
8384 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008385 msg_clr_eos();
8386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388/*
8389 * ":drop"
8390 * Opens the first argument in a window. When there are two or more arguments
8391 * the argument list is redefined.
8392 */
8393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008394ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395{
8396 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 win_T *wp;
8398 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008399 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400
8401 /*
8402 * Check if the first argument is already being edited in a window. If
8403 * so, jump to that window.
8404 * We would actually need to check all arguments, but that's complicated
8405 * and mostly only one file is dropped.
8406 * This also ignores wildcards, since it is very unlikely the user is
8407 * editing a file name with a wildcard character.
8408 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008409 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00008411 /*
8412 * Expanding wildcards may result in an empty argument list. E.g. when
8413 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
8414 * already did an error message for this.
8415 */
8416 if (ARGCOUNT == 0)
8417 return;
8418
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008419 if (cmdmod.tab)
8420 {
8421 /* ":tab drop file ...": open a tab for each argument that isn't
8422 * edited in a window yet. It's like ":tab all" but without closing
8423 * windows or tabs. */
8424 ex_all(eap);
8425 }
8426 else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008427 {
8428 /* ":drop file ...": Edit the first argument. Jump to an existing
8429 * window if possible, edit in current window if the current buffer
8430 * can be abandoned, otherwise open a new window. */
8431 buf = buflist_findnr(ARGLIST[0].ae_fnum);
8432
8433 FOR_ALL_TAB_WINDOWS(tp, wp)
8434 {
8435 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008437 goto_tabpage_win(tp, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 return;
8440 }
8441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008443 /*
8444 * Check whether the current buffer is changed. If so, we will need
8445 * to split the current window or data could be lost.
8446 * Skip the check if the 'hidden' option is set, as in this case the
8447 * buffer won't be lost.
8448 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02008449 if (!buf_hide(curbuf))
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008450 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008451 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008452 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008453 --emsg_off;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008456 /* Fake a ":sfirst" or ":first" command edit the first argument. */
8457 if (split)
8458 {
8459 eap->cmdidx = CMD_sfirst;
8460 eap->cmd[0] = 's';
8461 }
8462 else
8463 eap->cmdidx = CMD_first;
8464 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466}
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008467
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008468/*
8469 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
8470 * Put the start of the pattern in "*s", unless "s" is NULL.
8471 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
8472 * If "s" is not NULL terminate the pattern with a NUL.
8473 * Return a pointer to the char just past the pattern plus flags.
8474 */
8475 char_u *
8476skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
8477{
8478 int c;
8479
8480 if (vim_isIDc(*p))
8481 {
8482 /* ":vimgrep pattern fname" */
8483 if (s != NULL)
8484 *s = p;
8485 p = skiptowhite(p);
8486 if (s != NULL && *p != NUL)
8487 *p++ = NUL;
8488 }
8489 else
8490 {
8491 /* ":vimgrep /pattern/[g][j] fname" */
8492 if (s != NULL)
8493 *s = p + 1;
8494 c = *p;
8495 p = skip_regexp(p + 1, c, TRUE, NULL);
8496 if (*p != c)
8497 return NULL;
8498
8499 /* Truncate the pattern. */
8500 if (s != NULL)
8501 *p = NUL;
8502 ++p;
8503
8504 /* Find the flags */
8505 while (*p == 'g' || *p == 'j')
8506 {
8507 if (flags != NULL)
8508 {
8509 if (*p == 'g')
8510 *flags |= VGR_GLOBAL;
8511 else
8512 *flags |= VGR_NOJUMP;
8513 }
8514 ++p;
8515 }
8516 }
8517 return p;
8518}
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008519
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008520#if defined(FEAT_EVAL) || defined(PROTO)
8521/*
8522 * List v:oldfiles in a nice way.
8523 */
8524 void
8525ex_oldfiles(exarg_T *eap UNUSED)
8526{
8527 list_T *l = get_vim_var_list(VV_OLDFILES);
8528 listitem_T *li;
8529 int nr = 0;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008530 char_u *fname;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008531
8532 if (l == NULL)
8533 msg((char_u *)_("No old files"));
8534 else
8535 {
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008536 msg_start();
8537 msg_scroll = TRUE;
8538 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8539 {
8540 ++nr;
8541 fname = get_tv_string(&li->li_tv);
Bram Moolenaard6f2ee32016-08-24 00:30:52 +02008542 if (!message_filtered(fname))
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008543 {
8544 msg_outnum((long)nr);
8545 MSG_PUTS(": ");
8546 msg_outtrans(fname);
Bram Moolenaar885c00e2016-08-28 17:08:17 +02008547 msg_clr_eos();
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008548 msg_putchar('\n');
8549 out_flush(); /* output one line at a time */
8550 ui_breakcheck();
8551 }
8552 }
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008553
8554 /* Assume "got_int" was set to truncate the listing. */
8555 got_int = FALSE;
8556
8557# ifdef FEAT_BROWSE_CMD
8558 if (cmdmod.browse)
8559 {
8560 quit_more = FALSE;
8561 nr = prompt_for_number(FALSE);
8562 msg_starthere();
8563 if (nr > 0)
8564 {
8565 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8566 (long)nr);
8567
8568 if (p != NULL)
8569 {
8570 p = expand_env_save(p);
8571 eap->arg = p;
8572 eap->cmdidx = CMD_edit;
8573 cmdmod.browse = FALSE;
8574 do_exedit(eap, NULL);
8575 vim_free(p);
8576 }
8577 }
8578 }
8579# endif
8580 }
8581}
8582#endif