blob: 99c19f9136755a997c79a24c2aa9bca04dd21b34 [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)
Bram Moolenaarb0e982b2018-09-21 12:46:22 +0200652 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000653 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
Bram Moolenaarb0e982b2018-09-21 12:46:22 +0200654 msgmore(-deleted);
655 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000656 else if (deleted < 0)
657 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200658
659 if (change_occurred || deleted != 0)
660 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000661
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000662 curwin->w_cursor.lnum = eap->line1;
663 beginline(BL_WHITE | BL_FIX);
664
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000665sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000666 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000667 vim_free(sortbuf1);
668 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200669 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670 if (got_int)
671 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000672}
673
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674/*
675 * ":retab".
676 */
677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100678ex_retab(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
680 linenr_T lnum;
681 int got_tab = FALSE;
682 long num_spaces = 0;
683 long num_tabs;
684 long len;
685 long col;
686 long vcol;
687 long start_col = 0; /* For start of white-space string */
688 long start_vcol = 0; /* For start of white-space string */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 long old_len;
690 char_u *ptr;
691 char_u *new_line = (char_u *)1; /* init to non-NULL */
692 int did_undo; /* called u_save for current line */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200693#ifdef FEAT_VARTABS
Bram Moolenaar0119a592018-06-24 23:53:28 +0200694 int *new_vts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200695 char_u *new_ts_str; /* string value of tab argument */
696#else
697 int temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 int new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 int save_list;
701 linenr_T first_line = 0; /* first changed line */
702 linenr_T last_line = 0; /* last changed line */
703
704 save_list = curwin->w_p_list;
705 curwin->w_p_list = 0; /* don't want list mode here */
706
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200707#ifdef FEAT_VARTABS
708 new_ts_str = eap->arg;
Bram Moolenaar0119a592018-06-24 23:53:28 +0200709 if (!tabstop_set(eap->arg, &new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200710 return;
711 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
712 ++(eap->arg);
713
Bram Moolenaar0119a592018-06-24 23:53:28 +0200714 // This ensures that either new_vts_array and new_ts_str are freshly
715 // allocated, or new_vts_array points to an existing array and new_ts_str
716 // is null.
717 if (new_vts_array == NULL)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200718 {
Bram Moolenaar0119a592018-06-24 23:53:28 +0200719 new_vts_array = curbuf->b_p_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200720 new_ts_str = NULL;
721 }
722 else
723 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
724#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 new_ts = getdigits(&(eap->arg));
726 if (new_ts < 0)
727 {
728 EMSG(_(e_positive));
729 return;
730 }
731 if (new_ts == 0)
732 new_ts = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
735 {
736 ptr = ml_get(lnum);
737 col = 0;
738 vcol = 0;
739 did_undo = FALSE;
740 for (;;)
741 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100742 if (VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
744 if (!got_tab && num_spaces == 0)
745 {
746 /* First consecutive white-space */
747 start_vcol = vcol;
748 start_col = col;
749 }
750 if (ptr[col] == ' ')
751 num_spaces++;
752 else
753 got_tab = TRUE;
754 }
755 else
756 {
757 if (got_tab || (eap->forceit && num_spaces > 1))
758 {
759 /* Retabulate this string of white-space */
760
761 /* len is virtual length of white string */
762 len = num_spaces = vcol - start_vcol;
763 num_tabs = 0;
764 if (!curbuf->b_p_et)
765 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200766#ifdef FEAT_VARTABS
767 int t, s;
768
769 tabstop_fromto(start_vcol, vcol,
Bram Moolenaar0119a592018-06-24 23:53:28 +0200770 curbuf->b_p_ts, new_vts_array, &t, &s);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200771 num_tabs = t;
772 num_spaces = s;
773#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 temp = new_ts - (start_vcol % new_ts);
775 if (num_spaces >= temp)
776 {
777 num_spaces -= temp;
778 num_tabs++;
779 }
780 num_tabs += num_spaces / new_ts;
781 num_spaces -= (num_spaces / new_ts) * new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 if (curbuf->b_p_et || got_tab ||
785 (num_spaces + num_tabs < len))
786 {
787 if (did_undo == FALSE)
788 {
789 did_undo = TRUE;
790 if (u_save((linenr_T)(lnum - 1),
791 (linenr_T)(lnum + 1)) == FAIL)
792 {
793 new_line = NULL; /* flag out-of-memory */
794 break;
795 }
796 }
797
798 /* len is actual number of white characters used */
799 len = num_spaces + num_tabs;
800 old_len = (long)STRLEN(ptr);
801 new_line = lalloc(old_len - col + start_col + len + 1,
802 TRUE);
803 if (new_line == NULL)
804 break;
805 if (start_col > 0)
806 mch_memmove(new_line, ptr, (size_t)start_col);
807 mch_memmove(new_line + start_col + len,
808 ptr + col, (size_t)(old_len - col + 1));
809 ptr = new_line + start_col;
810 for (col = 0; col < len; col++)
811 ptr[col] = (col < num_tabs) ? '\t' : ' ';
812 ml_replace(lnum, new_line, FALSE);
813 if (first_line == 0)
814 first_line = lnum;
815 last_line = lnum;
816 ptr = new_line;
817 col = start_col + len;
818 }
819 }
820 got_tab = FALSE;
821 num_spaces = 0;
822 }
823 if (ptr[col] == NUL)
824 break;
825 vcol += chartabsize(ptr + col, (colnr_T)vcol);
826#ifdef FEAT_MBYTE
827 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000828 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else
830#endif
831 ++col;
832 }
833 if (new_line == NULL) /* out of memory */
834 break;
835 line_breakcheck();
836 }
837 if (got_int)
838 EMSG(_(e_interr));
839
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200840#ifdef FEAT_VARTABS
841 // If a single value was given then it can be considered equal to
842 // either the value of 'tabstop' or the value of 'vartabstop'.
843 if (tabstop_count(curbuf->b_p_vts_array) == 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200844 && tabstop_count(new_vts_array) == 1
845 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200846 ; /* not changed */
847 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200848 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200849 ; /* not changed */
850 else
851 redraw_curbuf_later(NOT_VALID);
852#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (curbuf->b_p_ts != new_ts)
854 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (first_line != 0)
857 changed_lines(first_line, 0, last_line + 1, 0L);
858
859 curwin->w_p_list = save_list; /* restore 'list' */
860
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200861#ifdef FEAT_VARTABS
862 if (new_ts_str != NULL) /* set the new tabstop */
863 {
864 // If 'vartabstop' is in use or if the value given to retab has more
865 // than one tabstop then update 'vartabstop'.
866 int *old_vts_ary = curbuf->b_p_vts_array;
867
Bram Moolenaar0119a592018-06-24 23:53:28 +0200868 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200869 {
870 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
871 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0119a592018-06-24 23:53:28 +0200872 curbuf->b_p_vts_array = new_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200873 vim_free(old_vts_ary);
874 }
875 else
876 {
877 // 'vartabstop' wasn't in use and a single value was given to
878 // retab then update 'tabstop'.
Bram Moolenaar0119a592018-06-24 23:53:28 +0200879 curbuf->b_p_ts = tabstop_first(new_vts_array);
880 vim_free(new_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200881 }
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200882 vim_free(new_ts_str);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200883 }
884#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 curbuf->b_p_ts = new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 coladvance(curwin->w_curswant);
888
889 u_clearline();
890}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892/*
893 * :move command - move lines line1-line2 to line dest
894 *
895 * return FAIL for failure, OK otherwise
896 */
897 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100898do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 char_u *str;
901 linenr_T l;
902 linenr_T extra; /* Num lines added before line1 */
903 linenr_T num_lines; /* Num lines moved */
904 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200905#ifdef FEAT_FOLDING
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100906 win_T *win;
907 tabpage_T *tp;
Bram Moolenaard5f69332015-04-15 12:43:50 +0200908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910 if (dest >= line1 && dest < line2)
911 {
912 EMSG(_("E134: Move lines into themselves"));
913 return FAIL;
914 }
915
916 num_lines = line2 - line1 + 1;
917
918 /*
919 * First we copy the old text to its new location -- webb
920 * Also copy the flag that ":global" command uses.
921 */
922 if (u_save(dest, dest + 1) == FAIL)
923 return FAIL;
924 for (extra = 0, l = line1; l <= line2; l++)
925 {
926 str = vim_strsave(ml_get(l + extra));
927 if (str != NULL)
928 {
929 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
930 vim_free(str);
931 if (dest < line1)
932 extra++;
933 }
934 }
935
936 /*
937 * Now we must be careful adjusting our marks so that we don't overlap our
938 * mark_adjust() calls.
939 *
940 * We adjust the marks within the old text so that they refer to the
941 * last lines of the file (temporarily), because we know no other marks
942 * will be set there since these line numbers did not exist until we added
943 * our new lines.
944 *
945 * Then we adjust the marks on lines between the old and new text positions
946 * (either forwards or backwards).
947 *
948 * And Finally we adjust the marks we put at the end of the file back to
949 * their final destination at the new text position -- webb
950 */
951 last_line = curbuf->b_ml.ml_line_count;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100952 mark_adjust_nofold(line1, line2, last_line - line2, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (dest >= line2)
954 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100955 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
956#ifdef FEAT_FOLDING
957 FOR_ALL_TAB_WINDOWS(tp, win) {
958 if (win->w_buffer == curbuf)
959 foldMoveRange(&win->w_folds, line1, line2, dest);
960 }
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 curbuf->b_op_start.lnum = dest - num_lines + 1;
963 curbuf->b_op_end.lnum = dest;
964 }
965 else
966 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100967 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
968#ifdef FEAT_FOLDING
969 FOR_ALL_TAB_WINDOWS(tp, win) {
970 if (win->w_buffer == curbuf)
971 foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
972 }
973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 curbuf->b_op_start.lnum = dest + 1;
975 curbuf->b_op_end.lnum = dest + num_lines;
976 }
977 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100978 mark_adjust_nofold(last_line - num_lines + 1, last_line,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 -(last_line - dest - extra), 0L);
980
981 /*
982 * Now we delete the original text -- webb
983 */
984 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
985 return FAIL;
986
987 for (l = line1; l <= line2; l++)
988 ml_delete(line1 + extra, TRUE);
989
990 if (!global_busy && num_lines > p_report)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200991 smsg((char_u *)NGETTEXT("%ld line moved", "%ld lines moved", num_lines),
992 (long)num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 /*
995 * Leave the cursor on the last of the moved lines.
996 */
997 if (dest >= line1)
998 curwin->w_cursor.lnum = dest;
999 else
1000 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
1001
1002 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +01001003 {
1004 dest += num_lines + 1;
1005 last_line = curbuf->b_ml.ml_line_count;
1006 if (dest > last_line + 1)
1007 dest = last_line + 1;
1008 changed_lines(line1, 0, dest, 0L);
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else
1011 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
1012
1013 return OK;
1014}
1015
1016/*
1017 * ":copy"
1018 */
1019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001020ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 linenr_T count;
1023 char_u *p;
1024
1025 count = line2 - line1 + 1;
1026 curbuf->b_op_start.lnum = n + 1;
1027 curbuf->b_op_end.lnum = n + count;
1028 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
1029
1030 /*
1031 * there are three situations:
1032 * 1. destination is above line1
1033 * 2. destination is between line1 and line2
1034 * 3. destination is below line2
1035 *
1036 * n = destination (when starting)
1037 * curwin->w_cursor.lnum = destination (while copying)
1038 * line1 = start of source (while copying)
1039 * line2 = end of source (while copying)
1040 */
1041 if (u_save(n, n + 1) == FAIL)
1042 return;
1043
1044 curwin->w_cursor.lnum = n;
1045 while (line1 <= line2)
1046 {
1047 /* need to use vim_strsave() because the line will be unlocked within
1048 * ml_append() */
1049 p = vim_strsave(ml_get(line1));
1050 if (p != NULL)
1051 {
1052 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
1053 vim_free(p);
1054 }
1055 /* situation 2: skip already copied lines */
1056 if (line1 == n)
1057 line1 = curwin->w_cursor.lnum;
1058 ++line1;
1059 if (curwin->w_cursor.lnum < line1)
1060 ++line1;
1061 if (curwin->w_cursor.lnum < line2)
1062 ++line2;
1063 ++curwin->w_cursor.lnum;
1064 }
1065
1066 appended_lines_mark(n, count);
1067
1068 msgmore((long)count);
1069}
1070
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001071static char_u *prevcmd = NULL; /* the previous command */
1072
1073#if defined(EXITFREE) || defined(PROTO)
1074 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001075free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001076{
1077 vim_free(prevcmd);
1078}
1079#endif
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081/*
1082 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
1083 * Bangs in the argument are replaced with the previously entered command.
1084 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
1086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001087do_bang(
1088 int addr_count,
1089 exarg_T *eap,
1090 int forceit,
1091 int do_in,
1092 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
1094 char_u *arg = eap->arg; /* command */
1095 linenr_T line1 = eap->line1; /* start of range */
1096 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 char_u *newcmd = NULL; /* the new command */
1098 int free_newcmd = FALSE; /* need to free() newcmd */
1099 int ins_prevcmd;
1100 char_u *t;
1101 char_u *p;
1102 char_u *trailarg;
1103 int len;
1104 int scroll_save = msg_scroll;
1105
1106 /*
1107 * Disallow shell commands for "rvim".
1108 * Disallow shell commands from .exrc and .vimrc in current directory for
1109 * security reasons.
1110 */
1111 if (check_restricted() || check_secure())
1112 return;
1113
1114 if (addr_count == 0) /* :! */
1115 {
1116 msg_scroll = FALSE; /* don't scroll here */
1117 autowrite_all();
1118 msg_scroll = scroll_save;
1119 }
1120
1121 /*
1122 * Try to find an embedded bang, like in :!<cmd> ! [args]
1123 * (:!! is indicated by the 'forceit' variable)
1124 */
1125 ins_prevcmd = forceit;
1126 trailarg = arg;
1127 do
1128 {
1129 len = (int)STRLEN(trailarg) + 1;
1130 if (newcmd != NULL)
1131 len += (int)STRLEN(newcmd);
1132 if (ins_prevcmd)
1133 {
1134 if (prevcmd == NULL)
1135 {
1136 EMSG(_(e_noprev));
1137 vim_free(newcmd);
1138 return;
1139 }
1140 len += (int)STRLEN(prevcmd);
1141 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001142 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 vim_free(newcmd);
1145 return;
1146 }
1147 *t = NUL;
1148 if (newcmd != NULL)
1149 STRCAT(t, newcmd);
1150 if (ins_prevcmd)
1151 STRCAT(t, prevcmd);
1152 p = t + STRLEN(t);
1153 STRCAT(t, trailarg);
1154 vim_free(newcmd);
1155 newcmd = t;
1156
1157 /*
1158 * Scan the rest of the argument for '!', which is replaced by the
1159 * previous command. "\!" is replaced by "!" (this is vi compatible).
1160 */
1161 trailarg = NULL;
1162 while (*p)
1163 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001164 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001167 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 else
1169 {
1170 trailarg = p;
1171 *trailarg++ = NUL;
1172 ins_prevcmd = TRUE;
1173 break;
1174 }
1175 }
1176 ++p;
1177 }
1178 } while (trailarg != NULL);
1179
1180 vim_free(prevcmd);
1181 prevcmd = newcmd;
1182
1183 if (bangredo) /* put cmd in redo buffer for ! command */
1184 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001185 /* If % or # appears in the command, it must have been escaped.
1186 * Reescape them, so that redoing them does not substitute them by the
1187 * buffername. */
1188 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1189
1190 if (cmd != NULL)
1191 {
1192 AppendToRedobuffLit(cmd, -1);
1193 vim_free(cmd);
1194 }
1195 else
1196 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 AppendToRedobuff((char_u *)"\n");
1198 bangredo = FALSE;
1199 }
1200 /*
1201 * Add quotes around the command, for shells that need them.
1202 */
1203 if (*p_shq != NUL)
1204 {
1205 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1206 if (newcmd == NULL)
1207 return;
1208 STRCPY(newcmd, p_shq);
1209 STRCAT(newcmd, prevcmd);
1210 STRCAT(newcmd, p_shq);
1211 free_newcmd = TRUE;
1212 }
1213 if (addr_count == 0) /* :! */
1214 {
1215 /* echo the command */
1216 msg_start();
1217 msg_putchar(':');
1218 msg_putchar('!');
1219 msg_outtrans(newcmd);
1220 msg_clr_eos();
1221 windgoto(msg_row, msg_col);
1222
1223 do_shell(newcmd, 0);
1224 }
1225 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 /* Careful: This may recursively call do_bang() again! (because of
1228 * autocommands) */
1229 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001230 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarade00832006-03-10 21:46:58 +00001231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (free_newcmd)
1233 vim_free(newcmd);
1234}
1235
1236/*
1237 * do_filter: filter lines through a command given by the user
1238 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239 * We mostly use temp files and the call_shell() routine here. This would
1240 * normally be done using pipes on a UNIX machine, but this is more portable
1241 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 * to deal with redirection somehow, and should handle things like looking
1243 * at the PATH env. variable, and adding reasonable extensions to the
1244 * command name given by the user. All reasonable versions of call_shell()
1245 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246 * Alternatively, if on Unix and redirecting input or output, but not both,
1247 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 * We use input redirection if do_in is TRUE.
1249 * We use output redirection if do_out is TRUE.
1250 */
1251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001252do_filter(
1253 linenr_T line1,
1254 linenr_T line2,
1255 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1256 char_u *cmd,
1257 int do_in,
1258 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
1260 char_u *itmp = NULL;
1261 char_u *otmp = NULL;
1262 linenr_T linecount;
1263 linenr_T read_linecount;
1264 pos_T cursor_save;
1265 char_u *cmd_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 buf_T *old_curbuf = curbuf;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001267 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 if (*cmd == NUL) /* no filter command */
1270 return;
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 cursor_save = curwin->w_cursor;
1273 linecount = line2 - line1 + 1;
1274 curwin->w_cursor.lnum = line1;
1275 curwin->w_cursor.col = 0;
1276 changed_line_abv_curs();
1277 invalidate_botline();
1278
1279 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001280 * When using temp files:
1281 * 1. * Form temp file names
1282 * 2. * Write the lines to a temp file
1283 * 3. Run the filter command on the temp file
1284 * 4. * Read the output of the command into the buffer
1285 * 5. * Delete the original lines to be filtered
1286 * 6. * Remove the temp files
1287 *
1288 * When writing the input with a pipe or when catching the output with a
1289 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
1291
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001292 if (do_out)
1293 shell_flags |= SHELL_DOOUT;
1294
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001295#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001296 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001298 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1299 shell_flags |= SHELL_READ;
1300 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001302 else if (do_in && !do_out && !p_stmp)
1303 {
1304 /* Use a pipe to write stdin of the command, do not use a temp file. */
1305 shell_flags |= SHELL_WRITE;
1306 curbuf->b_op_start.lnum = line1;
1307 curbuf->b_op_end.lnum = line2;
1308 }
1309 else if (do_in && do_out && !p_stmp)
1310 {
1311 /* Use a pipe to write stdin and fetch stdout of the command, do not
1312 * use a temp file. */
1313 shell_flags |= SHELL_READ|SHELL_WRITE;
1314 curbuf->b_op_start.lnum = line1;
1315 curbuf->b_op_end.lnum = line2;
1316 curwin->w_cursor.lnum = line2;
1317 }
1318 else
1319#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001320 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1321 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001322 {
1323 EMSG(_(e_notmp));
1324 goto filterend;
1325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327/*
1328 * The writing and reading of temp files will not be shown.
1329 * Vi also doesn't do this and the messages are not very informative.
1330 */
1331 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001332 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 FALSE, FALSE, FALSE, TRUE) == FAIL)
1334 {
1335 msg_putchar('\n'); /* keep message from buf_write() */
1336 --no_wait_return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001337#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (!aborting())
1339#endif
1340 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1341 goto filterend;
1342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (curbuf != old_curbuf)
1344 goto filterend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 if (!do_out)
1347 msg_putchar('\n');
1348
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001349 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1351 if (cmd_buf == NULL)
1352 goto filterend;
1353
1354 windgoto((int)Rows - 1, 0);
1355 cursor_on();
1356
1357 /*
1358 * When not redirecting the output the command can write anything to the
1359 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1360 * stderr output of external command. Clear the screen later.
1361 * If do_in is FALSE, this could be something like ":r !cat", which may
1362 * also mess up the screen, clear it later.
1363 */
1364 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1365 redraw_later_clear();
1366
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001367 if (do_out)
1368 {
1369 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001370 {
1371 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001372 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001373 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001374 redraw_curbuf_later(VALID);
1375 }
1376 read_linecount = curbuf->b_ml.ml_line_count;
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /*
1379 * When call_shell() fails wait_return() is called to give the user a
1380 * chance to read the error messages. Otherwise errors are ignored, so you
1381 * can see the error messages from the command that appear on stdout; use
1382 * 'u' to fix the text
1383 * Switch to cooked mode when not redirecting stdin, avoids that something
1384 * like ":r !cat" hangs.
1385 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1386 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001387 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389 redraw_later_clear();
1390 wait_return(FALSE);
1391 }
1392 vim_free(cmd_buf);
1393
1394 did_check_timestamps = FALSE;
1395 need_check_timestamps = TRUE;
1396
1397 /* When interrupting the shell command, it may still have produced some
1398 * useful output. Reset got_int here, so that readfile() won't cancel
1399 * reading. */
1400 ui_breakcheck();
1401 got_int = FALSE;
1402
1403 if (do_out)
1404 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001405 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001407 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001408 eap, READ_FILTER) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001410#if defined(FEAT_EVAL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001411 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001413 {
1414 msg_putchar('\n');
1415 EMSG2(_(e_notread), otmp);
1416 }
1417 goto error;
1418 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001419 if (curbuf != old_curbuf)
1420 goto filterend;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001421 }
1422
1423 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1424
1425 if (shell_flags & SHELL_READ)
1426 {
1427 curbuf->b_op_start.lnum = line2 + 1;
1428 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1429 appended_lines_mark(line2, read_linecount);
1430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
1432 if (do_in)
1433 {
1434 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (read_linecount >= linecount)
1437 /* move all marks from old lines to new lines */
1438 mark_adjust(line1, line2, linecount, 0L);
1439 else
1440 {
1441 /* move marks from old lines to new lines, delete marks
1442 * that are in deleted lines */
1443 mark_adjust(line1, line1 + read_linecount - 1,
1444 linecount, 0L);
1445 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1446 }
1447 }
1448
1449 /*
1450 * Put cursor on first filtered line for ":range!cmd".
1451 * Adjust '[ and '] (set by buf_write()).
1452 */
1453 curwin->w_cursor.lnum = line1;
1454 del_lines(linecount, TRUE);
1455 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1456 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1457 write_lnum_adjust(-linecount); /* adjust last line
1458 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001459#ifdef FEAT_FOLDING
1460 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
1464 {
1465 /*
1466 * Put cursor on last new line for ":r !cmd".
1467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001469 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1473 --no_wait_return;
1474
1475 if (linecount > p_report)
1476 {
1477 if (do_in)
1478 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001479 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1480 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001483 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485 else
1486 msgmore((long)linecount);
1487 }
1488 }
1489 else
1490 {
1491error:
1492 /* put cursor back in same position for ":w !cmd" */
1493 curwin->w_cursor = cursor_save;
1494 --no_wait_return;
1495 wait_return(FALSE);
1496 }
1497
1498filterend:
1499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (curbuf != old_curbuf)
1501 {
1502 --no_wait_return;
1503 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (itmp != NULL)
1506 mch_remove(itmp);
1507 if (otmp != NULL)
1508 mch_remove(otmp);
1509 vim_free(itmp);
1510 vim_free(otmp);
1511}
1512
1513/*
1514 * Call a shell to execute a command.
1515 * When "cmd" is NULL start an interactive shell.
1516 */
1517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518do_shell(
1519 char_u *cmd,
1520 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 buf_T *buf;
1523#ifndef FEAT_GUI_MSWIN
1524 int save_nwr;
1525#endif
1526#ifdef MSWIN
1527 int winstart = FALSE;
1528#endif
1529
1530 /*
1531 * Disallow shell commands for "rvim".
1532 * Disallow shell commands from .exrc and .vimrc in current directory for
1533 * security reasons.
1534 */
1535 if (check_restricted() || check_secure())
1536 {
1537 msg_end();
1538 return;
1539 }
1540
1541#ifdef MSWIN
1542 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * Check if ":!start" is used.
1544 */
1545 if (cmd != NULL)
1546 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1547#endif
1548
1549 /*
1550 * For autocommands we want to get the output on the current screen, to
1551 * avoid having to type return below.
1552 */
1553 msg_putchar('\r'); /* put cursor at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556#ifdef MSWIN
1557 if (!winstart)
1558#endif
1559 stoptermcap();
1560 }
1561#ifdef MSWIN
1562 if (!winstart)
1563#endif
1564 msg_putchar('\n'); /* may shift screen one line up */
1565
1566 /* warning message before calling the shell */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001567 if (p_warn && !autocmd_busy && msg_silent == 0)
Bram Moolenaar29323592016-07-24 22:04:11 +02001568 FOR_ALL_BUFFERS(buf)
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01001569 if (bufIsChangedNotTerm(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571#ifdef FEAT_GUI_MSWIN
1572 if (!winstart)
1573 starttermcap(); /* don't want a message box here */
1574#endif
1575 MSG_PUTS(_("[No write since last change]\n"));
1576#ifdef FEAT_GUI_MSWIN
1577 if (!winstart)
1578 stoptermcap();
1579#endif
1580 break;
1581 }
1582
1583 /* This windgoto is required for when the '\n' resulted in a "delete line
1584 * 1" command to the terminal. */
1585 if (!swapping_screen())
1586 windgoto(msg_row, msg_col);
1587 cursor_on();
1588 (void)call_shell(cmd, SHELL_COOKED | flags);
1589 did_check_timestamps = FALSE;
1590 need_check_timestamps = TRUE;
1591
1592 /*
1593 * put the message cursor at the end of the screen, avoids wait_return()
1594 * to overwrite the text that the external command showed
1595 */
1596 if (!swapping_screen())
1597 {
1598 msg_row = Rows - 1;
1599 msg_col = 0;
1600 }
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (autocmd_busy)
1603 {
1604 if (msg_silent == 0)
1605 redraw_later_clear();
1606 }
1607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
1609 /*
1610 * For ":sh" there is no need to call wait_return(), just redraw.
1611 * Also for the Win32 GUI (the output is in a console window).
1612 * Otherwise there is probably text on the screen that the user wants
1613 * to read before redrawing, so call wait_return().
1614 */
1615#ifndef FEAT_GUI_MSWIN
1616 if (cmd == NULL
1617# ifdef WIN3264
1618 || (winstart && !need_wait_return)
1619# endif
1620 )
1621 {
1622 if (msg_silent == 0)
1623 redraw_later_clear();
1624 need_wait_return = FALSE;
1625 }
1626 else
1627 {
1628 /*
1629 * If we switch screens when starttermcap() is called, we really
1630 * want to wait for "hit return to continue".
1631 */
1632 save_nwr = no_wait_return;
1633 if (swapping_screen())
1634 no_wait_return = FALSE;
1635# ifdef AMIGA
1636 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1637# else
1638 wait_return(msg_silent == 0);
1639# endif
1640 no_wait_return = save_nwr;
1641 }
1642#endif /* FEAT_GUI_W32 */
1643
1644#ifdef MSWIN
1645 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1646#endif
1647 starttermcap(); /* start termcap if not done by wait_return() */
1648
1649 /*
1650 * In an Amiga window redrawing is caused by asking the window size.
1651 * If we got an interrupt this will not work. The chance that the
1652 * window size is wrong is very small, but we need to redraw the
1653 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1654 * but it saves an extra redraw.
1655 */
1656#ifdef AMIGA
1657 if (skip_redraw) /* ':' hit in wait_return() */
1658 {
1659 if (msg_silent == 0)
1660 redraw_later_clear();
1661 }
1662 else if (term_console)
1663 {
1664 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1665 if (got_int && msg_silent == 0)
1666 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1667 else
1668 must_redraw = 0; /* no extra redraw needed */
1669 }
1670#endif
1671 }
1672
1673 /* display any error messages now */
1674 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001675
Bram Moolenaarade00832006-03-10 21:46:58 +00001676 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677}
1678
1679/*
1680 * Create a shell command from a command string, input redirection file and
1681 * output redirection file.
1682 * Returns an allocated string with the shell command, or NULL for failure.
1683 */
1684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001685make_filter_cmd(
1686 char_u *cmd, /* command */
1687 char_u *itmp, /* NULL or name of input file */
1688 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 char_u *buf;
1691 long_u len;
1692
Bram Moolenaar53076832015-12-31 19:53:21 +01001693#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001694 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001695 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001696
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001697 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001698 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1699 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001700 if (is_fish_shell)
1701 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1702 else
1703#endif
1704 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (itmp != NULL)
1706 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1707 if (otmp != NULL)
1708 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1709 buf = lalloc(len, TRUE);
1710 if (buf == NULL)
1711 return NULL;
1712
Bram Moolenaar53076832015-12-31 19:53:21 +01001713#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001715 * Put braces around the command (for concatenated commands) when
1716 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001718 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001719 {
1720 if (is_fish_shell)
1721 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1722 else
1723 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1724 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001725 else
1726 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (itmp != NULL)
1728 {
1729 STRCAT(buf, " < ");
1730 STRCAT(buf, itmp);
1731 }
1732#else
1733 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001734 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 * the use of commands in a pipe.
1736 */
1737 STRCPY(buf, cmd);
1738 if (itmp != NULL)
1739 {
1740 char_u *p;
1741
1742 /*
1743 * If there is a pipe, we have to put the '<' in front of it.
1744 * Don't do this when 'shellquote' is not empty, otherwise the
1745 * redirection would be inside the quotes.
1746 */
1747 if (*p_shq == NUL)
1748 {
1749 p = vim_strchr(buf, '|');
1750 if (p != NULL)
1751 *p = NUL;
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1754 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (*p_shq == NUL)
1756 {
1757 p = vim_strchr(cmd, '|');
1758 if (p != NULL)
1759 {
1760 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1761 STRCAT(buf, p);
1762 }
1763 }
1764 }
1765#endif
1766 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001767 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 return buf;
1770}
1771
1772/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001773 * Append output redirection for file "fname" to the end of string buffer
1774 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 * Works with the 'shellredir' and 'shellpipe' options.
1776 * The caller should make sure that there is enough room:
1777 * STRLEN(opt) + STRLEN(fname) + 3
1778 */
1779 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001780append_redir(
1781 char_u *buf,
1782 int buflen,
1783 char_u *opt,
1784 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001787 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001789 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001790 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001792 {
1793 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001795 if (p[1] == '%') /* skip %% */
1796 ++p;
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (p != NULL)
1799 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001800 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1801 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1802 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001805 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 " %s %s",
1808#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#endif
1811 (char *)opt, (char *)fname);
1812}
1813
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001814#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001816static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001817static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001818static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819static int viminfo_errcnt;
1820
1821 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001822no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 /* "vim -i NONE" does not read or write a viminfo file */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001825 return STRCMP(p_viminfofile, "NONE") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826}
1827
1828/*
1829 * Report an error for reading a viminfo file.
1830 * Count the number of errors. When there are more than 10, return TRUE.
1831 */
1832 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001833viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001835 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1836 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001837 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001838 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1839 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 emsg(IObuff);
1841 if (++viminfo_errcnt >= 10)
1842 {
1843 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1844 return TRUE;
1845 }
1846 return FALSE;
1847}
1848
1849/*
1850 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001851 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 */
1853 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001854read_viminfo(
1855 char_u *file, /* file name or NULL to use default name */
1856 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 FILE *fp;
1859 char_u *fname;
1860
1861 if (no_viminfo())
1862 return FAIL;
1863
Bram Moolenaard812df62008-11-09 12:46:09 +00001864 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (fname == NULL)
1866 return FAIL;
1867 fp = mch_fopen((char *)fname, READBIN);
1868
1869 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001870 {
1871 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001872 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1873 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001874 (flags & VIF_WANT_INFO) ? _(" info") : "",
1875 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1876 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001877 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001878 verbose_leave();
1879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 vim_free(fname);
1882 if (fp == NULL)
1883 return FAIL;
1884
1885 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001886 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 return OK;
1890}
1891
1892/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001893 * Write the viminfo file. The old one is read in first so that effectively a
1894 * merge of current info and old info is done. This allows multiple vims to
1895 * run simultaneously, without losing any marks etc.
1896 * If "forceit" is TRUE, then the old file is not read in, and only internal
1897 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 */
1899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001900write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 char_u *fname;
1903 FILE *fp_in = NULL; /* input viminfo file, if any */
1904 FILE *fp_out = NULL; /* output viminfo file */
1905 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001906 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907#if defined(UNIX) || defined(VMS)
1908 mode_t umask_save;
1909#endif
1910#ifdef UNIX
1911 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001912 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001914#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001915 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918 if (no_viminfo())
1919 return;
1920
1921 fname = viminfo_filename(file); /* may set to default if NULL */
1922 if (fname == NULL)
1923 return;
1924
1925 fp_in = mch_fopen((char *)fname, READBIN);
1926 if (fp_in == NULL)
1927 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001928 int fd;
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 /* if it does exist, but we can't read it, don't try writing */
1931 if (mch_stat((char *)fname, &st_new) == 0)
1932 goto end;
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001933
1934 /* Create the new .viminfo non-accessible for others, because it may
1935 * contain text from non-accessible documents. It is up to the user to
1936 * widen access (e.g. to a group). This may also fail if there is a
1937 * race condition, then just give up. */
1938 fd = mch_open((char *)fname,
1939 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1940 if (fd < 0)
1941 goto end;
1942 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944 else
1945 {
1946 /*
1947 * There is an existing viminfo file. Create a temporary file to
1948 * write the new viminfo into, in the same directory as the
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001949 * existing viminfo file, which will be renamed once all writing is
1950 * successful.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
1952#ifdef UNIX
1953 /*
1954 * For Unix we check the owner of the file. It's not very nice to
1955 * overwrite a user's viminfo file after a "su root", with a
1956 * viminfo file that the user can't read.
1957 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001958 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001959 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001961 if (mch_stat((char *)fname, &st_old) == 0
1962 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001963 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 ? (st_old.st_mode & 0200)
1965 : (st_old.st_gid == getgid()
1966 ? (st_old.st_mode & 0020)
1967 : (st_old.st_mode & 0002))))
1968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001969 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1973 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001974 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto end;
1976 }
1977#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001978#ifdef WIN3264
1979 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001980 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001984 * Make tempname, find one that does not exist yet.
1985 * Beware of a race condition: If someone logs out and all Vim
1986 * instances exit at the same time a temp file might be created between
1987 * stat() and open(). Use mch_open() with O_EXCL to avoid that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * May try twice: Once normal and once with shortname set, just in
1989 * case somebody puts his viminfo file in an 8.3 filesystem.
1990 */
1991 for (;;)
1992 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001993 int next_char = 'z';
1994 char_u *wp;
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 tempname = buf_modname(
1997#ifdef UNIX
1998 shortname,
1999#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#endif
2002 fname,
2003#ifdef VMS
2004 (char_u *)"-tmp",
2005#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007#endif
2008 FALSE);
2009 if (tempname == NULL) /* out of memory */
2010 break;
2011
2012 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002013 * Try a series of names. Change one character, just before
2014 * the extension. This should also work for an 8.3
2015 * file name, when after adding the extension it still is
2016 * the same file as the original.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002018 wp = tempname + STRLEN(tempname) - 5;
2019 if (wp < gettail(tempname)) /* empty file name? */
2020 wp = gettail(tempname);
2021 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002023 /*
2024 * Check if tempfile already exists. Never overwrite an
2025 * existing file!
2026 */
2027 if (mch_stat((char *)tempname, &st_new) == 0)
2028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002031 * Check if tempfile is same as original file. May happen
2032 * when modname() gave the same file back. E.g. silly
2033 * link, or file name-length reached. Try again with
2034 * shortname set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002036 if (!shortname && st_new.st_dev == st_old.st_dev
2037 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002039 VIM_CLEAR(tempname);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002040 shortname = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 break;
2042 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002045 else
2046 {
2047 /* Try creating the file exclusively. This may fail if
2048 * another Vim tries to do it at the same time. */
2049#ifdef VMS
2050 /* fdopen() fails for some reason */
2051 umask_save = umask(077);
2052 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2053 (void)umask(umask_save);
2054#else
2055 int fd;
2056
2057 /* Use mch_open() to be able to use O_NOFOLLOW and set file
2058 * protection:
2059 * Unix: same as original file, but strip s-bit. Reset
2060 * umask to avoid it getting in the way.
2061 * Others: r&w for user only. */
2062# ifdef UNIX
2063 umask_save = umask(0);
2064 fd = mch_open((char *)tempname,
2065 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
2066 (int)((st_old.st_mode & 0777) | 0600));
2067 (void)umask(umask_save);
2068# else
2069 fd = mch_open((char *)tempname,
2070 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
2071# endif
2072 if (fd < 0)
2073 {
2074 fp_out = NULL;
2075# ifdef EEXIST
2076 /* Avoid trying lots of names while the problem is lack
2077 * of premission, only retry if the file already
2078 * exists. */
2079 if (errno != EEXIST)
2080 break;
2081# endif
2082 }
2083 else
2084 fp_out = fdopen(fd, WRITEBIN);
2085#endif /* VMS */
2086 if (fp_out != NULL)
2087 break;
2088 }
2089
2090 /* Assume file exists, try again with another name. */
2091 if (next_char == 'a' - 1)
2092 {
2093 /* They all exist? Must be something wrong! Don't write
2094 * the viminfo file then. */
2095 EMSG2(_("E929: Too many viminfo temp files, like %s!"),
2096 tempname);
2097 break;
2098 }
2099 *wp = next_char;
2100 --next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002102
2103 if (tempname != NULL)
2104 break;
2105 /* continue if shortname was set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107
Bram Moolenaara5792f52005-11-23 21:25:05 +00002108#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002109 if (tempname != NULL && fp_out != NULL)
2110 {
2111 stat_T tmp_st;
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 /*
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002114 * Make sure the original owner can read/write the tempfile and
2115 * otherwise preserve permissions, making sure the group matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002117 if (mch_stat((char *)tempname, &tmp_st) >= 0)
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002118 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002119 if (st_old.st_uid != tmp_st.st_uid)
2120 /* Changing the owner might fail, in which case the
2121 * file will now owned by the current user, oh well. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002122 vim_ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002123 if (st_old.st_gid != tmp_st.st_gid
2124 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
2125 /* can't set the group to what it should be, remove
2126 * group permissions */
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002127 (void)mch_setperm(tempname, 0600);
2128 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002129 else
2130 /* can't stat the file, set conservative permissions */
2131 (void)mch_setperm(tempname, 0600);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002133#endif
Bram Moolenaare0aa23f2017-11-26 17:08:03 +01002134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 /*
2137 * Check if the new viminfo file can be written to.
2138 */
2139 if (fp_out == NULL)
2140 {
2141 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002142 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (fp_in != NULL)
2144 fclose(fp_in);
2145 goto end;
2146 }
2147
2148 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002149 {
2150 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002151 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002152 verbose_leave();
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002156 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar927030a2016-03-15 18:23:55 +01002158 if (fclose(fp_out) == EOF)
2159 ++viminfo_errcnt;
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (fp_in != NULL)
2162 {
2163 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002164
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002165 /* In case of an error keep the original viminfo file. Otherwise
2166 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002167 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002168 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002169 if (vim_rename(tempname, fname) == -1)
2170 {
2171 ++viminfo_errcnt;
2172 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2173 }
2174# ifdef WIN3264
2175 /* If the viminfo file was hidden then also hide the new file. */
2176 else if (hidden)
2177 mch_hide(fname);
2178# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002179 }
2180 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 mch_remove(tempname);
2182 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184end:
2185 vim_free(fname);
2186 vim_free(tempname);
2187}
2188
2189/*
2190 * Get the viminfo file name to use.
2191 * If "file" is given and not empty, use it (has already been expanded by
2192 * cmdline functions).
2193 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2194 * expand environment variables.
2195 * Returns an allocated string. NULL when out of memory.
2196 */
2197 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002198viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 if (file == NULL || *file == NUL)
2201 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002202 if (*p_viminfofile != NUL)
2203 file = p_viminfofile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2205 {
2206#ifdef VIMINFO_FILE2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207# ifdef VMS
2208 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2209# else
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002210# ifdef MSWIN
2211 /* Use $VIM only if $HOME is the default "C:/". */
2212 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
2213 && mch_getenv((char_u *)"HOME") == NULL)
2214# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (mch_getenv((char_u *)"HOME") == NULL)
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# endif
2218 {
2219 /* don't use $VIM when not available. */
2220 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2221 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2222 file = (char_u *)VIMINFO_FILE2;
2223 else
2224 file = (char_u *)VIMINFO_FILE;
2225 }
2226 else
2227#endif
2228 file = (char_u *)VIMINFO_FILE;
2229 }
2230 expand_env(file, NameBuff, MAXPATHL);
2231 file = NameBuff;
2232 }
2233 return vim_strsave(file);
2234}
2235
2236/*
2237 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2238 */
2239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002240do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 int eof = FALSE;
2243 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002244 int merge = FALSE;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002245 int do_copy_marks = FALSE;
2246 garray_T buflist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2249 return;
2250 vir.vir_fd = fp_in;
2251#ifdef FEAT_MBYTE
2252 vir.vir_conv.vc_type = CONV_NONE;
2253#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002254 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002255 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257 if (fp_in != NULL)
2258 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002259 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002260 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002261 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002262 {
2263 /* Registers and marks are read and kept separate from what
2264 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002265 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002266 prepare_viminfo_marks();
2267 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002268
Bram Moolenaard812df62008-11-09 12:46:09 +00002269 eof = read_viminfo_up_to_marks(&vir,
2270 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002271 merge = TRUE;
2272 }
2273 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /* Skip info, find start of marks */
2275 while (!(eof = viminfo_readline(&vir))
2276 && vir.vir_line[0] != '>')
2277 ;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002278
2279 do_copy_marks = (flags &
2280 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (fp_out != NULL)
2284 {
2285 /* Write the info: */
2286 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2287 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002288 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002289 write_viminfo_version(fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002291 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2293#endif
2294 write_viminfo_search_pattern(fp_out);
2295 write_viminfo_sub_string(fp_out);
2296#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002297 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298#endif
2299 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002300 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#ifdef FEAT_EVAL
2302 write_viminfo_varlist(fp_out);
2303#endif
2304 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002305 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002307 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002308
2309 if (do_copy_marks)
2310 ga_init2(&buflist, sizeof(buf_T *), 50);
2311 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002313
2314 if (do_copy_marks)
2315 {
2316 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2317 if (fp_out != NULL)
2318 ga_clear(&buflist);
2319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 vim_free(vir.vir_line);
2322#ifdef FEAT_MBYTE
2323 if (vir.vir_conv.vc_type != CONV_NONE)
2324 convert_setup(&vir.vir_conv, NULL, NULL);
2325#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002326 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327}
2328
2329/*
2330 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2331 * first part of the viminfo file which contains everything but the marks that
2332 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2333 */
2334 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002335read_viminfo_up_to_marks(
2336 vir_T *virp,
2337 int forceit,
2338 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 int eof;
2341 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002342 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002345 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 eof = viminfo_readline(virp);
2349 while (!eof && virp->vir_line[0] != '>')
2350 {
2351 switch (virp->vir_line[0])
2352 {
2353 /* Characters reserved for future expansion, ignored now */
2354 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 case '^': /* to be defined */
2356 case '<': /* long line - ignored */
2357 /* A comment or empty line. */
2358 case NUL:
2359 case '\r':
2360 case '\n':
2361 case '#':
2362 eof = viminfo_readline(virp);
2363 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002364 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002365 eof = read_viminfo_barline(virp, got_encoding,
2366 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002369 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 eof = viminfo_encoding(virp);
2371 break;
2372 case '!': /* global variable */
2373#ifdef FEAT_EVAL
2374 eof = read_viminfo_varlist(virp, writing);
2375#else
2376 eof = viminfo_readline(virp);
2377#endif
2378 break;
2379 case '%': /* entry for buffer list */
2380 eof = read_viminfo_bufferlist(virp, writing);
2381 break;
2382 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002383 /* When registers are in bar lines skip the old style register
2384 * lines. */
2385 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2386 eof = read_viminfo_register(virp, forceit);
2387 else
2388 do {
2389 eof = viminfo_readline(virp);
2390 } while (!eof && (virp->vir_line[0] == TAB
2391 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 break;
2393 case '/': /* Search string */
2394 case '&': /* Substitute search string */
2395 case '~': /* Last search string, followed by '/' or '&' */
2396 eof = read_viminfo_search_pattern(virp, forceit);
2397 break;
2398 case '$':
2399 eof = read_viminfo_sub_string(virp, forceit);
2400 break;
2401 case ':':
2402 case '?':
2403 case '=':
2404 case '@':
2405#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002406 /* When history is in bar lines skip the old style history
2407 * lines. */
2408 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2409 eof = read_viminfo_history(virp, writing);
2410 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002412 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 break;
2414 case '-':
2415 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002416 /* When file marks are in bar lines skip the old style lines. */
2417 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2418 eof = read_viminfo_filemark(virp, forceit);
2419 else
2420 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 break;
2422 default:
2423 if (viminfo_error("E575: ", _("Illegal starting char"),
2424 virp->vir_line))
2425 eof = TRUE;
2426 else
2427 eof = viminfo_readline(virp);
2428 break;
2429 }
2430 }
2431
2432#ifdef FEAT_CMDHIST
2433 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002434 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002435 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
2437
2438 /* Change file names to buffer numbers for fmarks. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002439 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 fmarks_check_names(buf);
2441
2442 return eof;
2443}
2444
2445/*
2446 * Compare the 'encoding' value in the viminfo file with the current value of
2447 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2448 * conversion of text with iconv() in viminfo_readstring().
2449 */
2450 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002451viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453#ifdef FEAT_MBYTE
2454 char_u *p;
2455 int i;
2456
2457 if (get_viminfo_parameter('c') != 0)
2458 {
2459 p = vim_strchr(virp->vir_line, '=');
2460 if (p != NULL)
2461 {
2462 /* remove trailing newline */
2463 ++p;
2464 for (i = 0; vim_isprintc(p[i]); ++i)
2465 ;
2466 p[i] = NUL;
2467
2468 convert_setup(&virp->vir_conv, p, p_enc);
2469 }
2470 }
2471#endif
2472 return viminfo_readline(virp);
2473}
2474
2475/*
2476 * Read a line from the viminfo file.
2477 * Returns TRUE for end-of-file;
2478 */
2479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002480viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2483}
2484
2485/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002486 * Check string read from viminfo file.
2487 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 * - replace CTRL-V CTRL-V with CTRL-V
2489 * - replace CTRL-V 'n' with '\n'
2490 *
2491 * Check for a long line as written by viminfo_writestring().
2492 *
2493 * Return the string in allocated memory (NULL when out of memory).
2494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002496viminfo_readstring(
2497 vir_T *virp,
2498 int off, /* offset for virp->vir_line */
2499 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 char_u *retval;
2502 char_u *s, *d;
2503 long len;
2504
2505 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2506 {
2507 len = atol((char *)virp->vir_line + off + 1);
2508 retval = lalloc(len, TRUE);
2509 if (retval == NULL)
2510 {
2511 /* Line too long? File messed up? Skip next line. */
2512 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2513 return NULL;
2514 }
2515 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2516 s = retval + 1; /* Skip the leading '<' */
2517 }
2518 else
2519 {
2520 retval = vim_strsave(virp->vir_line + off);
2521 if (retval == NULL)
2522 return NULL;
2523 s = retval;
2524 }
2525
2526 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2527 d = retval;
2528 while (*s != NUL && *s != '\n')
2529 {
2530 if (s[0] == Ctrl_V && s[1] != NUL)
2531 {
2532 if (s[1] == 'n')
2533 *d++ = '\n';
2534 else
2535 *d++ = Ctrl_V;
2536 s += 2;
2537 }
2538 else
2539 *d++ = *s++;
2540 }
2541 *d = NUL;
2542
2543#ifdef FEAT_MBYTE
2544 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2545 {
2546 d = string_convert(&virp->vir_conv, retval, NULL);
2547 if (d != NULL)
2548 {
2549 vim_free(retval);
2550 retval = d;
2551 }
2552 }
2553#endif
2554
2555 return retval;
2556}
2557
2558/*
2559 * write string to viminfo file
2560 * - replace CTRL-V with CTRL-V CTRL-V
2561 * - replace '\n' with CTRL-V 'n'
2562 * - add a '\n' at the end
2563 *
2564 * For a long line:
2565 * - write " CTRL-V <length> \n " in first line
2566 * - write " < <string> \n " in second line
2567 */
2568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002569viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 int c;
2572 char_u *s;
2573 int len = 0;
2574
2575 for (s = p; *s != NUL; ++s)
2576 {
2577 if (*s == Ctrl_V || *s == '\n')
2578 ++len;
2579 ++len;
2580 }
2581
2582 /* If the string will be too long, write its length and put it in the next
2583 * line. Take into account that some room is needed for what comes before
2584 * the string (e.g., variable name). Add something to the length for the
2585 * '<', NL and trailing NUL. */
2586 if (len > LSIZE / 2)
2587 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2588
2589 while ((c = *p++) != NUL)
2590 {
2591 if (c == Ctrl_V || c == '\n')
2592 {
2593 putc(Ctrl_V, fd);
2594 if (c == '\n')
2595 c = 'n';
2596 }
2597 putc(c, fd);
2598 }
2599 putc('\n', fd);
2600}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002601
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002602/*
2603 * Write a string in quotes that barline_parse() can read back.
2604 * Breaks the line in less than LSIZE pieces when needed.
2605 * Returns remaining characters in the line.
2606 */
2607 int
2608barline_writestring(FILE *fd, char_u *s, int remaining_start)
2609{
2610 char_u *p;
2611 int remaining = remaining_start;
2612 int len = 2;
2613
2614 /* Count the number of characters produced, including quotes. */
2615 for (p = s; *p != NUL; ++p)
2616 {
2617 if (*p == NL)
2618 len += 2;
2619 else if (*p == '"' || *p == '\\')
2620 len += 2;
2621 else
2622 ++len;
2623 }
Bram Moolenaar25709572016-08-26 20:41:16 +02002624 if (len > remaining - 2)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002625 {
2626 fprintf(fd, ">%d\n|<", len);
2627 remaining = LSIZE - 20;
2628 }
2629
2630 putc('"', fd);
2631 for (p = s; *p != NUL; ++p)
2632 {
2633 if (*p == NL)
2634 {
2635 putc('\\', fd);
2636 putc('n', fd);
2637 --remaining;
2638 }
2639 else if (*p == '"' || *p == '\\')
2640 {
2641 putc('\\', fd);
2642 putc(*p, fd);
2643 --remaining;
2644 }
2645 else
2646 putc(*p, fd);
2647 --remaining;
2648
2649 if (remaining < 3)
2650 {
2651 putc('\n', fd);
2652 putc('|', fd);
2653 putc('<', fd);
2654 /* Leave enough space for another continuation. */
2655 remaining = LSIZE - 20;
2656 }
2657 }
2658 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002659 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002660}
2661
2662/*
2663 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002664 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002665 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002666 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002667 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002668barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002669{
2670 char_u *p = text;
2671 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002672 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002673 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002674 int i;
2675 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002676 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002677#ifdef FEAT_MBYTE
2678 char_u *sconv;
2679 int converted;
2680#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002681
2682 while (*p == ',')
2683 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002684 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002685 if (ga_grow(values, 1) == FAIL)
2686 break;
2687 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002688
2689 if (*p == '>')
2690 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002691 /* Need to read a continuation line. Put strings in allocated
2692 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002693 if (!allocated)
2694 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002695 for (i = 0; i < values->ga_len; ++i)
2696 {
2697 bval_T *vp = (bval_T *)(values->ga_data) + i;
2698
2699 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002700 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002701 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2702 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002703 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002704 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002705 allocated = TRUE;
2706 }
2707
2708 if (vim_isdigit(p[1]))
2709 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002710 size_t len;
2711 size_t todo;
2712 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002713
2714 /* String value was split into lines that are each shorter
2715 * than LSIZE:
2716 * |{bartype},>{length of "{text}{text2}"}
2717 * |<"{text1}
2718 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002719 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002720 */
2721 ++p;
2722 len = getdigits(&p);
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002723 buf = alloc((int)(len + 1));
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002724 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002725 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002726 p = buf;
2727 for (todo = len; todo > 0; todo -= n)
2728 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002729 eof = viminfo_readline(virp);
2730 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002731 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002732 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002733 /* File was truncated or garbled. Read another line if
2734 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002735 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002736 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002737 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002738 /* Get length of text, excluding |< and NL chars. */
2739 n = STRLEN(virp->vir_line);
2740 while (n > 0 && (virp->vir_line[n - 1] == NL
2741 || virp->vir_line[n - 1] == CAR))
2742 --n;
2743 n -= 2;
2744 if (n > todo)
2745 {
2746 /* more values follow after the string */
2747 nextp = virp->vir_line + 2 + todo;
2748 n = todo;
2749 }
2750 mch_memmove(p, virp->vir_line + 2, n);
2751 p += n;
2752 }
2753 *p = NUL;
2754 p = buf;
2755 }
2756 else
2757 {
2758 /* Line ending in ">" continues in the next line:
2759 * |{bartype},{lots of values},>
2760 * |<{value},{value}
2761 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002762 eof = viminfo_readline(virp);
2763 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002764 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002765 /* File was truncated or garbled. Read another line if
2766 * this one starts with '|'. */
2767 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002768 p = virp->vir_line + 2;
2769 }
2770 }
2771
2772 if (isdigit(*p))
2773 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002774 value->bv_type = BVAL_NR;
2775 value->bv_nr = getdigits(&p);
2776 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002777 }
2778 else if (*p == '"')
2779 {
2780 int len = 0;
2781 char_u *s = p;
2782
2783 /* Unescape special characters in-place. */
2784 ++p;
2785 while (*p != '"')
2786 {
2787 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002788 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002789 if (*p == '\\')
2790 {
2791 ++p;
2792 if (*p == 'n')
2793 s[len++] = '\n';
2794 else
2795 s[len++] = *p;
2796 ++p;
2797 }
2798 else
2799 s[len++] = *p++;
2800 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002801 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002802 s[len] = NUL;
2803
Bram Moolenaar01227092016-06-11 14:47:40 +02002804#ifdef FEAT_MBYTE
2805 converted = FALSE;
2806 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2807 {
2808 sconv = string_convert(&virp->vir_conv, s, NULL);
2809 if (sconv != NULL)
2810 {
2811 if (s == buf)
2812 vim_free(s);
2813 s = sconv;
2814 buf = s;
2815 converted = TRUE;
2816 }
2817 }
2818#endif
2819 /* Need to copy in allocated memory if the string wasn't allocated
2820 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002821 if (s != buf && allocated)
2822 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002823 value->bv_string = s;
2824 value->bv_type = BVAL_STRING;
2825 value->bv_len = len;
2826 value->bv_allocated = allocated
Bram Moolenaar01227092016-06-11 14:47:40 +02002827#ifdef FEAT_MBYTE
2828 || converted
2829#endif
2830 ;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002831 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002832 if (nextp != NULL)
2833 {
2834 /* values following a long string */
2835 p = nextp;
2836 nextp = NULL;
2837 }
2838 }
2839 else if (*p == ',')
2840 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002841 value->bv_type = BVAL_EMPTY;
2842 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002843 }
2844 else
2845 break;
2846 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002847 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002848}
2849
2850 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002851read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002852{
2853 char_u *p = virp->vir_line + 1;
2854 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002855 garray_T values;
2856 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002857 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002858 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002859
2860 /* The format is: |{bartype},{value},...
2861 * For a very long string:
2862 * |{bartype},>{length of "{text}{text2}"}
2863 * |<{text1}
2864 * |<{text2},{value}
2865 * For a long line not using a string
2866 * |{bartype},{lots of values},>
2867 * |<{value},{value}
2868 */
2869 if (*p == '<')
2870 {
2871 /* Continuation line of an unrecognized item. */
2872 if (writing)
2873 ga_add_string(&virp->vir_barlines, virp->vir_line);
2874 }
2875 else
2876 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002877 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002878 bartype = getdigits(&p);
2879 switch (bartype)
2880 {
2881 case BARTYPE_VERSION:
2882 /* Only use the version when it comes before the encoding.
2883 * If it comes later it was copied by a Vim version that
2884 * doesn't understand the version. */
2885 if (!got_encoding)
2886 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002887 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002888 vp = (bval_T *)values.ga_data;
2889 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2890 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002891 }
2892 break;
2893
2894 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002895 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002896 handle_viminfo_history(&values, writing);
2897 break;
2898
2899 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002900 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002901 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002902 break;
2903
Bram Moolenaar2d358992016-06-12 21:20:54 +02002904 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002905 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002906 handle_viminfo_mark(&values, force);
2907 break;
2908
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002909 default:
2910 /* copy unrecognized line (for future use) */
2911 if (writing)
2912 ga_add_string(&virp->vir_barlines, virp->vir_line);
2913 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002914 for (i = 0; i < values.ga_len; ++i)
2915 {
2916 vp = (bval_T *)values.ga_data + i;
2917 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2918 vim_free(vp->bv_string);
2919 }
2920 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002921 }
2922
Bram Moolenaarecefe712016-06-20 12:50:17 +02002923 if (read_next)
2924 return viminfo_readline(virp);
2925 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002926}
2927
2928 static void
2929write_viminfo_version(FILE *fp_out)
2930{
2931 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2932 BARTYPE_VERSION, VIMINFO_VERSION);
2933}
2934
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002935 static void
2936write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2937{
2938 int i;
2939 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002940 int seen_useful = FALSE;
2941 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002942
2943 if (gap->ga_len > 0)
2944 {
2945 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2946
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002947 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002948 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002949 {
2950 line = ((char **)(gap->ga_data))[i];
2951 if (seen_useful || line[1] != '<')
2952 {
2953 fputs(line, fp_out);
2954 seen_useful = TRUE;
2955 }
2956 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002957 }
2958}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#endif /* FEAT_VIMINFO */
2960
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002961/*
2962 * Return the current time in seconds. Calls time(), unless test_settime()
2963 * was used.
2964 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002965 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002966vim_time(void)
2967{
2968# ifdef FEAT_EVAL
2969 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2970# else
2971 return time(NULL);
2972# endif
2973}
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975/*
2976 * Implementation of ":fixdel", also used by get_stty().
2977 * <BS> resulting <Del>
2978 * ^? ^H
2979 * not ^? ^?
2980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002982do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 char_u *p;
2985
2986 p = find_termcode((char_u *)"kb");
2987 add_termcode((char_u *)"kD", p != NULL
2988 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2989}
2990
2991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002992print_line_no_prefix(
2993 linenr_T lnum,
2994 int use_number,
2995 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002997 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999 if (curwin->w_p_nu || use_number)
3000 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003001 vim_snprintf((char *)numbuf, sizeof(numbuf),
3002 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003003 msg_puts_attr(numbuf, HL_ATTR(HLF_N)); /* Highlight line nrs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003005 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
3008/*
3009 * Print a text line. Also in silent mode ("ex -s").
3010 */
3011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003012print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int save_silent = silent_mode;
3015
Bram Moolenaard29459b2016-08-26 22:29:11 +02003016 /* apply :filter /pat/ */
3017 if (message_filtered(ml_get(lnum)))
3018 return;
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003021 silent_mode = FALSE;
3022 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
3023 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (save_silent)
3025 {
3026 msg_putchar('\n');
3027 cursor_on(); /* msg_start() switches it off */
3028 out_flush();
3029 silent_mode = save_silent;
3030 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00003031 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032}
3033
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003034 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003035rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003036{
3037 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02003038 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003039
Bram Moolenaar7e283842013-05-30 11:43:15 +02003040 buf = curbuf;
3041 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003042 /* buffer changed, don't change name now */
3043 if (buf != curbuf)
3044 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003045#ifdef FEAT_EVAL
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003046 if (aborting()) /* autocmds may abort script processing */
3047 return FAIL;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003048#endif
3049 /*
3050 * The name of the current buffer will be changed.
3051 * A new (unlisted) buffer entry needs to be made to hold the old file
3052 * name, which will become the alternate file name.
3053 * But don't set the alternate file name if the buffer didn't have a
3054 * name.
3055 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02003056 fname = curbuf->b_ffname;
3057 sfname = curbuf->b_sfname;
3058 xfname = curbuf->b_fname;
3059 curbuf->b_ffname = NULL;
3060 curbuf->b_sfname = NULL;
3061 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003062 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02003063 curbuf->b_ffname = fname;
3064 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003065 return FAIL;
3066 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02003067 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003068 if (xfname != NULL && *xfname != NUL)
3069 {
3070 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
3071 if (buf != NULL && !cmdmod.keepalt)
3072 curwin->w_alt_fnum = buf->b_fnum;
3073 }
3074 vim_free(fname);
3075 vim_free(sfname);
Bram Moolenaar7e283842013-05-30 11:43:15 +02003076 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003077
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003078 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02003079 DO_AUTOCHDIR;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003080 return OK;
3081}
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083/*
3084 * ":file[!] [fname]".
3085 */
3086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003089 /* ":0file" removes the file name. Check for illegal uses ":3file",
3090 * "0file name", etc. */
3091 if (eap->addr_count > 0
3092 && (*eap->arg != NUL
3093 || eap->line2 > 0
3094 || eap->addr_count > 1))
3095 {
3096 EMSG(_(e_invarg));
3097 return;
3098 }
3099
3100 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003102 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 return;
Bram Moolenaarfc089602018-06-24 16:53:35 +02003104 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
Bram Moolenaarfc089602018-06-24 16:53:35 +02003106
3107 // print file name if no argument or 'F' is not in 'shortmess'
3108 if (*eap->arg == NUL || !shortmess(SHM_FILEINFO))
Bram Moolenaar426dd022016-03-15 15:09:29 +01003109 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110}
3111
3112/*
3113 * ":update".
3114 */
3115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003116ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 if (curbufIsChanged())
3119 (void)do_write(eap);
3120}
3121
3122/*
3123 * ":write" and ":saveas".
3124 */
3125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003126ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 if (eap->usefilter) /* input lines to shell command */
3129 do_bang(1, eap, FALSE, TRUE, FALSE);
3130 else
3131 (void)do_write(eap);
3132}
3133
3134/*
3135 * write current buffer to file 'eap->arg'
3136 * if 'eap->append' is TRUE, append to the file
3137 *
3138 * if *eap->arg == NUL write to current file
3139 *
3140 * return FAIL for failure, OK otherwise
3141 */
3142 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003143do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 int other;
3146 char_u *fname = NULL; /* init to shut up gcc */
3147 char_u *ffname;
3148 int retval = FAIL;
3149 char_u *free_fname = NULL;
3150#ifdef FEAT_BROWSE
3151 char_u *browse_file = NULL;
3152#endif
3153 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003154 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 if (not_writing()) /* check 'write' option */
3157 return FAIL;
3158
3159 ffname = eap->arg;
3160#ifdef FEAT_BROWSE
3161 if (cmdmod.browse)
3162 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003163 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 NULL, NULL, NULL, curbuf);
3165 if (browse_file == NULL)
3166 goto theend;
3167 ffname = browse_file;
3168 }
3169#endif
3170 if (*ffname == NUL)
3171 {
3172 if (eap->cmdidx == CMD_saveas)
3173 {
3174 EMSG(_(e_argreq));
3175 goto theend;
3176 }
3177 other = FALSE;
3178 }
3179 else
3180 {
3181 fname = ffname;
3182 free_fname = fix_fname(ffname);
3183 /*
3184 * When out-of-memory, keep unexpanded file name, because we MUST be
3185 * able to write the file in this situation.
3186 */
3187 if (free_fname != NULL)
3188 ffname = free_fname;
3189 other = otherfile(ffname);
3190 }
3191
3192 /*
3193 * If we have a new file, put its name in the list of alternate file names.
3194 */
3195 if (other)
3196 {
3197 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3198 || eap->cmdidx == CMD_saveas)
3199 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3200 else
3201 alt_buf = buflist_findname(ffname);
3202 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3203 {
3204 /* Overwriting a file that is loaded in another buffer is not a
3205 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00003206 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 goto theend;
3208 }
3209 }
3210
3211 /*
3212 * Writing to the current file is not allowed in readonly mode
3213 * and a file name is required.
3214 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3215 */
3216 if (!other && (
3217#ifdef FEAT_QUICKFIX
3218 bt_dontwrite_msg(curbuf) ||
3219#endif
3220 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3221 goto theend;
3222
3223 if (!other)
3224 {
3225 ffname = curbuf->b_ffname;
3226 fname = curbuf->b_fname;
3227 /*
3228 * Not writing the whole file is only allowed with '!'.
3229 */
3230 if ( (eap->line1 != 1
3231 || eap->line2 != curbuf->b_ml.ml_line_count)
3232 && !eap->forceit
3233 && !eap->append
3234 && !p_wa)
3235 {
3236#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3237 if (p_confirm || cmdmod.confirm)
3238 {
3239 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3240 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3241 goto theend;
3242 eap->forceit = TRUE;
3243 }
3244 else
3245#endif
3246 {
3247 EMSG(_("E140: Use ! to write partial buffer"));
3248 goto theend;
3249 }
3250 }
3251 }
3252
3253 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3254 {
3255 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 buf_T *was_curbuf = curbuf;
3258
3259 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003260 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003261#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003263#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 /* buffer changed, don't change name now */
3268 retval = FAIL;
3269 goto theend;
3270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 /* Exchange the file names for the current and the alternate
3272 * buffer. This makes it look like we are now editing the buffer
3273 * under the new name. Must be done before buf_write(), because
3274 * if there is no file name and 'cpo' contains 'F', it will set
3275 * the file name. */
3276 fname = alt_buf->b_fname;
3277 alt_buf->b_fname = curbuf->b_fname;
3278 curbuf->b_fname = fname;
3279 fname = alt_buf->b_ffname;
3280 alt_buf->b_ffname = curbuf->b_ffname;
3281 curbuf->b_ffname = fname;
3282 fname = alt_buf->b_sfname;
3283 alt_buf->b_sfname = curbuf->b_sfname;
3284 curbuf->b_sfname = fname;
3285 buf_name_changed(curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003288 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (!alt_buf->b_p_bl)
3290 {
3291 alt_buf->b_p_bl = TRUE;
3292 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3293 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003294#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003296#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 /* buffer changed, don't write the file */
3301 retval = FAIL;
3302 goto theend;
3303 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003304
3305 /* If 'filetype' was empty try detecting it now. */
3306 if (*curbuf->b_p_ft == NUL)
3307 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 if (au_has_group((char_u *)"filetypedetect"))
3309 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003310 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003311 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003312 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003313
3314 /* Autocommands may have changed buffer names, esp. when
3315 * 'autochdir' is set. */
3316 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318
Bram Moolenaar5c719942016-07-09 23:40:45 +02003319 name_was_missing = curbuf->b_ffname == NULL;
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3322 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003323
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003324 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003325 if (eap->cmdidx == CMD_saveas)
3326 {
3327 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003328 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003329 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003330 redraw_tabline = TRUE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003331 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003332 }
3333
3334 /* Change directories when the 'acd' option is set and the file name
3335 * got changed or set. */
3336 if (eap->cmdidx == CMD_saveas || name_was_missing)
3337 {
Bram Moolenaar6f470022018-04-10 18:47:20 +02003338 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341
3342theend:
3343#ifdef FEAT_BROWSE
3344 vim_free(browse_file);
3345#endif
3346 vim_free(free_fname);
3347 return retval;
3348}
3349
3350/*
3351 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3352 * BF_NEW or BF_READERR, check for overwriting current file.
3353 * May set eap->forceit if a dialog says it's OK to overwrite.
3354 * Return OK if it's OK, FAIL if it is not.
3355 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003356 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003357check_overwrite(
3358 exarg_T *eap,
3359 buf_T *buf,
3360 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003362 char_u *ffname, /* full path version of fname */
3363 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 /*
3366 * write to other file or b_flags set or not writing the whole file:
3367 * overwriting only allowed with '!'
3368 */
3369 if ( (other
3370 || (buf->b_flags & BF_NOTEDITED)
3371 || ((buf->b_flags & BF_NEW)
3372 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3373 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003375#ifdef FEAT_QUICKFIX
3376 && !bt_nofile(buf)
3377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 && vim_fexists(ffname))
3379 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003380 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003382#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003383 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003384 if (mch_isdir(ffname))
3385 {
3386 EMSG2(_(e_isadir2), ffname);
3387 return FAIL;
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#endif
3390#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003391 if (p_confirm || cmdmod.confirm)
3392 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003393 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003395 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3396 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3397 return FAIL;
3398 eap->forceit = TRUE;
3399 }
3400 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003402 {
3403 EMSG(_(e_exists));
3404 return FAIL;
3405 }
3406 }
3407
3408 /* For ":w! filename" check that no swap file exists for "filename". */
3409 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003411 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003412 char_u *p;
3413 int r;
3414 char_u *swapname;
3415
3416 /* We only try the first entry in 'directory', without checking if
3417 * it's writable. If the "." directory is not writable the write
3418 * will probably fail anyway.
3419 * Use 'shortname' of the current buffer, since there is no buffer
3420 * for the written file. */
3421 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003422 {
3423 dir = alloc(5);
3424 if (dir == NULL)
3425 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003426 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003427 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003428 else
3429 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003430 dir = alloc(MAXPATHL);
3431 if (dir == NULL)
3432 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003433 p = p_dir;
3434 copy_option_part(&p, dir, MAXPATHL, ",");
3435 }
3436 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003437 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003438 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003439 if (r)
3440 {
3441#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3442 if (p_confirm || cmdmod.confirm)
3443 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003444 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003445
3446 dialog_msg(buff,
3447 _("Swap file \"%s\" exists, overwrite anyway?"),
3448 swapname);
3449 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3450 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003451 {
3452 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003453 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003454 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003455 eap->forceit = TRUE;
3456 }
3457 else
3458#endif
3459 {
3460 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
3461 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003462 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003463 return FAIL;
3464 }
3465 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003466 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468 }
3469 return OK;
3470}
3471
3472/*
3473 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3474 */
3475 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003476ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 int i;
3479
3480 if (eap->cmd[1] == 'n')
3481 i = curwin->w_arg_idx + (int)eap->line2;
3482 else
3483 i = curwin->w_arg_idx - (int)eap->line2;
3484 eap->line1 = 1;
3485 eap->line2 = curbuf->b_ml.ml_line_count;
3486 if (do_write(eap) != FAIL)
3487 do_argfile(eap, i);
3488}
3489
3490/*
3491 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3492 */
3493 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003494do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 buf_T *buf;
3497 int error = 0;
3498 int save_forceit = eap->forceit;
3499
3500 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3501 exiting = TRUE;
3502
Bram Moolenaar29323592016-07-24 22:04:11 +02003503 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
Bram Moolenaar7a760922018-02-19 23:10:02 +01003505#ifdef FEAT_TERMINAL
3506 if (exiting && term_job_running(buf->b_term))
3507 {
3508 no_write_message_nobang(buf);
3509 ++error;
3510 }
3511 else
3512#endif
Bram Moolenaar059db5c2017-10-15 22:42:23 +02003513 if (bufIsChanged(buf) && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 /*
3516 * Check if there is a reason the buffer cannot be written:
3517 * 1. if the 'write' option is set
3518 * 2. if there is no file name (even after browsing)
3519 * 3. if the 'readonly' is set (even after a dialog)
3520 * 4. if overwriting is allowed (even after a dialog)
3521 */
3522 if (not_writing())
3523 {
3524 ++error;
3525 break;
3526 }
3527#ifdef FEAT_BROWSE
3528 /* ":browse wall": ask for file name if there isn't one */
3529 if (buf->b_ffname == NULL && cmdmod.browse)
3530 browse_save_fname(buf);
3531#endif
3532 if (buf->b_ffname == NULL)
3533 {
3534 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3535 ++error;
3536 }
3537 else if (check_readonly(&eap->forceit, buf)
3538 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3539 FALSE) == FAIL)
3540 {
3541 ++error;
3542 }
3543 else
3544 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003545 bufref_T bufref;
3546
3547 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (buf_write_all(buf, eap->forceit) == FAIL)
3549 ++error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003551 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554 eap->forceit = save_forceit; /* check_overwrite() may set it */
3555 }
3556 }
3557 if (exiting)
3558 {
3559 if (!error)
3560 getout(0); /* exit Vim */
3561 not_exiting();
3562 }
3563}
3564
3565/*
3566 * Check the 'write' option.
3567 * Return TRUE and give a message when it's not st.
3568 */
3569 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003570not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
3572 if (p_write)
3573 return FALSE;
3574 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3575 return TRUE;
3576}
3577
3578/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003579 * Check if a buffer is read-only (either 'readonly' option is set or file is
3580 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3581 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
3583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003584check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003586 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003587
3588 /* Handle a file being readonly when the 'readonly' option is set or when
3589 * the file exists and permissions are read-only.
3590 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3591 * important for device checks but not here. */
3592 if (!*forceit && (buf->b_p_ro
3593 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3594 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3597 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3598 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003599 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar5386a122007-06-28 20:02:32 +00003601 if (buf->b_p_ro)
3602 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3603 buf->b_fname);
3604 else
3605 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 +00003606 buf->b_fname);
3607
3608 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3609 {
3610 /* Set forceit, to force the writing of a readonly file */
3611 *forceit = TRUE;
3612 return FALSE;
3613 }
3614 else
3615 return TRUE;
3616 }
3617 else
3618#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003619 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003621 else
3622 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3623 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 return TRUE;
3625 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return FALSE;
3628}
3629
3630/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003631 * Try to abandon current file and edit a new or existing file.
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003632 * "fnum" is the number of the file, if zero use ffname/sfname.
3633 * "lnum" is the line number for the cursor in the new file (if non-zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 *
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003635 * Return:
3636 * GETFILE_ERROR for "normal" error,
3637 * GETFILE_NOT_WRITTEN for "not written" error,
3638 * GETFILE_SAME_FILE for success
3639 * GETFILE_OPEN_OTHER for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 */
3641 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003642getfile(
3643 int fnum,
3644 char_u *ffname,
3645 char_u *sfname,
3646 int setpm,
3647 linenr_T lnum,
3648 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
3650 int other;
3651 int retval;
3652 char_u *free_me = NULL;
3653
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003654 if (text_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003655 return GETFILE_ERROR;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003656 if (curbuf_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003657 return GETFILE_ERROR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
3659 if (fnum == 0)
3660 {
3661 /* make ffname full path, set sfname */
3662 fname_expand(curbuf, &ffname, &sfname);
3663 other = otherfile(ffname);
3664 free_me = ffname; /* has been allocated, free() later */
3665 }
3666 else
3667 other = (fnum != curbuf->b_fnum);
3668
3669 if (other)
3670 ++no_wait_return; /* don't wait for autowrite message */
Bram Moolenaareb44a682017-08-03 22:44:55 +02003671 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3673 {
3674#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3675 if (p_confirm && p_write)
3676 dialog_changed(curbuf, FALSE);
3677 if (curbufIsChanged())
3678#endif
3679 {
3680 if (other)
3681 --no_wait_return;
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02003682 no_write_message();
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003683 retval = GETFILE_NOT_WRITTEN; /* file has been changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 goto theend;
3685 }
3686 }
3687 if (other)
3688 --no_wait_return;
3689 if (setpm)
3690 setpcmark();
3691 if (!other)
3692 {
3693 if (lnum != 0)
3694 curwin->w_cursor.lnum = lnum;
3695 check_cursor_lnum();
3696 beginline(BL_SOL | BL_FIX);
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003697 retval = GETFILE_SAME_FILE; /* it's in the same file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02003700 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003701 curwin) == OK)
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003702 retval = GETFILE_OPEN_OTHER; /* opened another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 else
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003704 retval = GETFILE_ERROR; /* error encountered */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
3706theend:
3707 vim_free(free_me);
3708 return retval;
3709}
3710
3711/*
3712 * start editing a new file
3713 *
3714 * fnum: file number; if zero use ffname/sfname
3715 * ffname: the file name
3716 * - full path if sfname used,
3717 * - any file name if sfname is NULL
3718 * - empty string to re-edit with the same file name (but may be
3719 * in a different directory)
3720 * - NULL to start an empty buffer
3721 * sfname: the short file name (or NULL)
3722 * eap: contains the command to be executed after loading the file and
3723 * forced 'ff' and 'fenc'
3724 * newlnum: if > 0: put cursor on this line number (if possible)
3725 * if ECMD_LASTL: use last position in loaded file
3726 * if ECMD_LAST: use last position in all files
3727 * if ECMD_ONE: use first line
3728 * flags:
3729 * ECMD_HIDE: if TRUE don't free the current buffer
3730 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3731 * ECMD_OLDBUF: use existing buffer if it exists
3732 * ECMD_FORCEIT: ! used for Ex command
3733 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003734 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003735 * window, NULL when splitting the window first. When not NULL info
3736 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 *
3738 * return FAIL for failure, OK otherwise
3739 */
3740 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003741do_ecmd(
3742 int fnum,
3743 char_u *ffname,
3744 char_u *sfname,
3745 exarg_T *eap, /* can be NULL! */
3746 linenr_T newlnum,
3747 int flags,
3748 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
3750 int other_file; /* TRUE if editing another file */
3751 int oldbuf; /* TRUE if using existing buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 int auto_buf = FALSE; /* TRUE if autocommands brought us
3753 into the buffer unexpectedly */
3754 char_u *new_name = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003755#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003756 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#endif
3758 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003759 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003760 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 char_u *free_fname = NULL;
3762#ifdef FEAT_BROWSE
3763 char_u *browse_file = NULL;
3764#endif
3765 int retval = FAIL;
3766 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003767 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 linenr_T topline = 0;
3769 int newcol = -1;
3770 int solcol = -1;
3771 pos_T *pos;
3772#ifdef FEAT_SUN_WORKSHOP
3773 char_u *cp;
3774#endif
3775 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003776#ifdef FEAT_SPELL
3777 int did_get_winopts = FALSE;
3778#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003779 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003780 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 if (eap != NULL)
3783 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003784 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 if (fnum != 0)
3787 {
3788 if (fnum == curbuf->b_fnum) /* file is already being edited */
3789 return OK; /* nothing to do */
3790 other_file = TRUE;
3791 }
3792 else
3793 {
3794#ifdef FEAT_BROWSE
3795 if (cmdmod.browse)
3796 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003797 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003798# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003799 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003800# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003801 au_has_group((char_u *)"FileExplorer"))
3802 {
3803 /* No browsing supported but we do have the file explorer:
3804 * Edit the directory. */
3805 if (ffname == NULL || !mch_isdir(ffname))
3806 ffname = (char_u *)".";
3807 }
3808 else
3809 {
3810 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003812 if (browse_file == NULL)
3813 goto theend;
3814 ffname = browse_file;
3815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817#endif
3818 /* if no short name given, use ffname for short name */
3819 if (sfname == NULL)
3820 sfname = ffname;
3821#ifdef USE_FNAME_CASE
3822# ifdef USE_LONG_FNAME
3823 if (USE_LONG_FNAME)
3824# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003825 if (sfname != NULL)
3826 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827#endif
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3830 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 if (ffname == NULL)
3833 other_file = TRUE;
3834 /* there is no file name */
3835 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3836 other_file = FALSE;
3837 else
3838 {
3839 if (*ffname == NUL) /* re-edit with same file name */
3840 {
3841 ffname = curbuf->b_ffname;
3842 sfname = curbuf->b_fname;
3843 }
3844 free_fname = fix_fname(ffname); /* may expand to full path name */
3845 if (free_fname != NULL)
3846 ffname = free_fname;
3847 other_file = otherfile(ffname);
3848#ifdef FEAT_SUN_WORKSHOP
3849 if (usingSunWorkShop && p_acd
3850 && (cp = vim_strrchr(sfname, '/')) != NULL)
3851 sfname = ++cp;
3852#endif
3853 }
3854 }
3855
3856 /*
3857 * if the file was changed we may not be allowed to abandon it
3858 * - if we are going to re-edit the same file
3859 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3860 */
3861 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3862 || (curbuf->b_nwindows == 1
3863 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003864 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3865 | (other_file ? 0 : CCGD_MULTWIN)
3866 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3867 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 if (fnum == 0 && other_file && ffname != NULL)
3870 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3871 goto theend;
3872 }
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 /*
3875 * End Visual mode before switching to another buffer, so the text can be
3876 * copied into the GUI selection buffer.
3877 */
3878 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003880#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003881 if ((command != NULL || newlnum > (linenr_T)0)
3882 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3883 {
3884 int len;
3885 char_u *p;
3886
3887 /* Set v:swapcommand for the SwapExists autocommands. */
3888 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003889 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003890 else
3891 len = 30;
3892 p = alloc((unsigned)len);
3893 if (p != NULL)
3894 {
3895 if (command != NULL)
3896 vim_snprintf((char *)p, len, ":%s\r", command);
3897 else
3898 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3899 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3900 did_set_swapcommand = TRUE;
3901 vim_free(p);
3902 }
3903 }
3904#endif
3905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 /*
3907 * If we are starting to edit another file, open a (new) buffer.
3908 * Otherwise we re-use the current buffer.
3909 */
3910 if (other_file)
3911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (!(flags & ECMD_ADDBUF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003914 if (!cmdmod.keepalt)
3915 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003916 if (oldwin != NULL)
3917 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919
3920 if (fnum)
3921 buf = buflist_findnr(fnum);
3922 else
3923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (flags & ECMD_ADDBUF)
3925 {
3926 linenr_T tlnum = 1L;
3927
3928 if (command != NULL)
3929 {
3930 tlnum = atol((char *)command);
3931 if (tlnum <= 0)
3932 tlnum = 1L;
3933 }
3934 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3935 goto theend;
3936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 buf = buflist_new(ffname, sfname, 0L,
3938 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003939
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003940 /* autocommands may change curwin and curbuf */
3941 if (oldwin != NULL)
3942 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003943 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 if (buf == NULL)
3946 goto theend;
3947 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3948 {
3949 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 else /* existing memfile */
3952 {
3953 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003954 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003956 /* Check if autocommands made the buffer invalid or changed the
3957 * current buffer. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003958 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 goto theend;
3960#ifdef FEAT_EVAL
3961 if (aborting()) /* autocmds may abort script processing */
3962 goto theend;
3963#endif
3964 }
3965
3966 /* May jump to last used line number for a loaded buffer or when asked
3967 * for explicitly */
3968 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3969 {
3970 pos = buflist_findfpos(buf);
3971 newlnum = pos->lnum;
3972 solcol = pos->col;
3973 }
3974
3975 /*
3976 * Make the (new) buffer the one used by the current window.
3977 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3978 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003979 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 */
3981 if (buf != curbuf)
3982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 /*
3984 * Be careful: The autocommands may delete any buffer and change
3985 * the current buffer.
3986 * - If the buffer we are going to edit is deleted, give up.
3987 * - If the current buffer is deleted, prefer to load the new
3988 * buffer when loading a buffer is required. This avoids
3989 * loading another buffer which then must be closed again.
3990 * - If we ended up in the new buffer already, need to skip a few
3991 * things, set auto_buf.
3992 */
3993 if (buf->b_fname != NULL)
3994 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003995 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003997 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003999 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 delbuf_msg(new_name); /* frees new_name */
4001 goto theend;
4002 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004003#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (aborting()) /* autocmds may abort script processing */
4005 {
4006 vim_free(new_name);
4007 goto theend;
4008 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (buf == curbuf) /* already in new buffer */
4011 auto_buf = TRUE;
4012 else
4013 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02004014 win_T *the_curwin = curwin;
4015
4016 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004017 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004018 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004019 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02004020
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004021 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 buf_copy_options(buf, BCO_ENTER);
4023
Bram Moolenaar5a497892016-09-03 16:29:04 +02004024 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02004025 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004026 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004027 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004028 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
Bram Moolenaar5a497892016-09-03 16:29:04 +02004030 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004031 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01004032
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004033#ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02004034 /* autocmds may abort script processing */
4035 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
4037 vim_free(new_name);
4038 goto theend;
4039 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004042 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004044 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 delbuf_msg(new_name); /* frees new_name */
4046 goto theend;
4047 }
4048 if (buf == curbuf) /* already in new buffer */
4049 auto_buf = TRUE;
4050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004052#ifdef FEAT_SYN_HL
4053 /*
4054 * <VN> We could instead free the synblock
4055 * and re-attach to buffer, perhaps.
4056 */
Bram Moolenaarf1d13472017-07-11 18:28:46 +02004057 if (curwin->w_buffer == NULL
4058 || curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02004059 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 curwin->w_buffer = buf;
4062 curbuf = buf;
4063 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004064
4065 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
4066 if (!oldbuf && eap != NULL)
4067 {
4068 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004069#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004070 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004071#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
4075 /* May get the window options from the last time this buffer
4076 * was in this window (or another window). If not used
4077 * before, reset the local window options to the global
4078 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004079 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004080#ifdef FEAT_SPELL
4081 did_get_winopts = TRUE;
4082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004085 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004086 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 curwin->w_pcmark.lnum = 1;
4090 curwin->w_pcmark.col = 0;
4091 }
4092 else /* !other_file */
4093 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02004094 if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 oldbuf = (flags & ECMD_OLDBUF);
4098 }
4099
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004100 /* Don't redraw until the cursor is in the right line, otherwise
4101 * autocommands may cause ml_get errors. */
4102 ++RedrawingDisabled;
4103 did_inc_redrawing_disabled = TRUE;
4104
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004105 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4107 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004108 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 else
4111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 /* Don't make a buffer listed if it's a help buffer. Useful when
4113 * using CTRL-O to go back to a help file. */
4114 if (!curbuf->b_help)
4115 set_buflisted(TRUE);
4116 }
4117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 /* If autocommands change buffers under our fingers, forget about
4119 * editing the file. */
4120 if (buf != curbuf)
4121 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004122#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (aborting()) /* autocmds may abort script processing */
4124 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /* Since we are starting to edit a file, consider the filetype to be
4128 * unset. Helps for when an autocommand changes files and expects syntax
4129 * highlighting to work in the other file. */
4130 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132/*
4133 * other_file oldbuf
4134 * FALSE FALSE re-edit same file, buffer is re-used
4135 * FALSE TRUE re-edit same file, nothing changes
4136 * TRUE FALSE start editing new file, new buffer
4137 * TRUE TRUE start editing in existing buffer (nothing to do)
4138 */
4139 if (!other_file && !oldbuf) /* re-use the buffer */
4140 {
4141 set_last_cursor(curwin); /* may set b_last_cursor */
4142 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4143 {
4144 newlnum = curwin->w_cursor.lnum;
4145 solcol = curwin->w_cursor.col;
4146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 buf = curbuf;
4148 if (buf->b_fname != NULL)
4149 new_name = vim_strsave(buf->b_fname);
4150 else
4151 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004152 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004153
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004154 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4155 {
4156 /* Save all the text, so that the reload can be undone.
4157 * Sync first so that this is a separate undo-able action. */
4158 u_sync(FALSE);
4159 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4160 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004161 {
4162 vim_free(new_name);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004163 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004164 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004165 u_unchanged(curbuf);
4166 buf_freeall(curbuf, BFA_KEEP_UNDO);
4167
4168 /* tell readfile() not to clear or reload undo info */
4169 readfile_flags = READ_KEEP_UNDO;
4170 }
4171 else
4172 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 /* If autocommands deleted the buffer we were going to re-edit, give
4175 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004176 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 delbuf_msg(new_name); /* frees new_name */
4179 goto theend;
4180 }
4181 vim_free(new_name);
4182
4183 /* If autocommands change buffers under our fingers, forget about
4184 * re-editing the file. Should do the buf_clear_file(), but perhaps
4185 * the autocommands changed the buffer... */
4186 if (buf != curbuf)
4187 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004188#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (aborting()) /* autocmds may abort script processing */
4190 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#endif
4192 buf_clear_file(curbuf);
4193 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4194 curbuf->b_op_end.lnum = 0;
4195 }
4196
4197/*
4198 * If we get here we are sure to start editing
4199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 /* Assume success now */
4201 retval = OK;
4202
4203 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * Check if we are editing the w_arg_idx file in the argument list.
4205 */
4206 check_arg_idx(curwin);
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (!auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 /*
4211 * Set cursor and init window before reading the file and executing
4212 * autocommands. This allows for the autocommands to position the
4213 * cursor.
4214 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004215 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004218 /* It's possible that all lines in the buffer changed. Need to update
4219 * automatic folding for all windows where it's used. */
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004220 {
4221 win_T *win;
4222 tabpage_T *tp;
4223
4224 FOR_ALL_TAB_WINDOWS(tp, win)
4225 if (win->w_buffer == curbuf)
4226 foldUpdateAll(win);
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#endif
4229
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004230 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004231 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 /*
4234 * Careful: open_buffer() and apply_autocmds() may change the current
4235 * buffer and window.
4236 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004237 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 topline = curwin->w_topline;
4239 if (!oldbuf) /* need to read the file */
4240 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004241#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 swap_exists_action = SEA_DIALOG;
4243#endif
4244 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4245
4246 /*
4247 * Open the buffer and read the file.
4248 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004249#if defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004250 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 retval = FAIL;
4252#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004253 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#endif
4255
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004256#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (swap_exists_action == SEA_QUIT)
4258 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004259 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260#endif
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 else
4263 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004264 /* Read the modelines, but only to set window-local options. Any
4265 * buffer-local options have already been set and may have been
4266 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004267 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4270 &retval);
4271 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4272 &retval);
4273 }
4274 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
Bram Moolenaareab316b2015-03-24 11:46:30 +01004276 /* If autocommands change the cursor position or topline, we should
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004277 * keep it. Also when it moves within a line. But not when it moves
4278 * to the first non-blank. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004279 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004281 char_u *text = ml_get_curline();
4282
4283 if (curwin->w_cursor.lnum != orig_pos.lnum
4284 || curwin->w_cursor.col != (int)(skipwhite(text) - text))
4285 {
4286 newlnum = curwin->w_cursor.lnum;
4287 newcol = curwin->w_cursor.col;
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 if (curwin->w_topline == topline)
4291 topline = 0;
4292
4293 /* Even when cursor didn't move we need to recompute topline. */
4294 changed_line_abv_curs();
4295
4296#ifdef FEAT_TITLE
4297 maketitle();
4298#endif
4299 }
4300
4301#ifdef FEAT_DIFF
4302 /* Tell the diff stuff that this buffer is new and/or needs updating.
4303 * Also needed when re-editing the same buffer, because unloading will
4304 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004305 if (curwin->w_p_diff)
4306 {
4307 diff_buf_add(curbuf);
4308 diff_invalidate(curbuf);
4309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#endif
4311
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004312#ifdef FEAT_SPELL
4313 /* If the window options were changed may need to set the spell language.
4314 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004315 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4316 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004317#endif
4318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (command == NULL)
4320 {
4321 if (newcol >= 0) /* position set by autocommands */
4322 {
4323 curwin->w_cursor.lnum = newlnum;
4324 curwin->w_cursor.col = newcol;
4325 check_cursor();
4326 }
4327 else if (newlnum > 0) /* line number from caller or old position */
4328 {
4329 curwin->w_cursor.lnum = newlnum;
4330 check_cursor_lnum();
4331 if (solcol >= 0 && !p_sol)
4332 {
4333 /* 'sol' is off: Use last known column. */
4334 curwin->w_cursor.col = solcol;
4335 check_cursor_col();
4336#ifdef FEAT_VIRTUALEDIT
4337 curwin->w_cursor.coladd = 0;
4338#endif
4339 curwin->w_set_curswant = TRUE;
4340 }
4341 else
4342 beginline(BL_SOL | BL_FIX);
4343 }
4344 else /* no line number, go to last line in Ex mode */
4345 {
4346 if (exmode_active)
4347 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4348 beginline(BL_WHITE | BL_FIX);
4349 }
4350 }
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 /* Check if cursors in other windows on the same buffer are still valid */
4353 check_lnums(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 /*
4356 * Did not read the file, need to show some info about the file.
4357 * Do this after setting the cursor.
4358 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004359 if (oldbuf && !auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
4361 int msg_scroll_save = msg_scroll;
4362
4363 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4364 * message. */
4365 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4366 msg_scroll = FALSE;
4367 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4368 check_for_delay(FALSE);
4369 msg_start();
4370 msg_scroll = msg_scroll_save;
4371 msg_scrolled_ign = TRUE;
4372
Bram Moolenaar426dd022016-03-15 15:09:29 +01004373 if (!shortmess(SHM_FILEINFO))
4374 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
4376 msg_scrolled_ign = FALSE;
4377 }
4378
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004379#ifdef FEAT_VIMINFO
4380 curbuf->b_last_used = vim_time();
4381#endif
4382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (command != NULL)
4384 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4385
4386#ifdef FEAT_KEYMAP
4387 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004388 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#endif
4390
4391 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004392 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (!skip_redraw)
4394 {
4395 n = p_so;
4396 if (topline == 0 && command == NULL)
4397 p_so = 999; /* force cursor halfway the window */
4398 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 curwin->w_scbind_pos = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 p_so = n;
4401 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4402 }
4403
4404 if (p_im)
4405 need_start_insertmode = TRUE;
4406
Bram Moolenaar15833232018-02-03 18:33:17 +01004407#ifdef FEAT_AUTOCHDIR
4408 /* Change directories when the 'acd' option is set and we aren't already in
4409 * that directory (should already be done above). Expect getcwd() to be
4410 * faster than calling shorten_fnames() unnecessarily. */
4411 if (p_acd && curbuf->b_ffname != NULL)
4412 {
4413 char_u curdir[MAXPATHL];
4414 char_u filedir[MAXPATHL];
4415
4416 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1);
4417 *gettail_sep(filedir) = NUL;
4418 if (mch_dirname(curdir, MAXPATHL) != FAIL
4419 && vim_fnamecmp(curdir, filedir) != 0)
4420 do_autochdir();
4421 }
4422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00004424#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004425 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
4427# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02004428 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
4430# endif
4431# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004432 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004433 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434# endif
4435 }
4436#endif
4437
4438theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004439 if (did_inc_redrawing_disabled)
4440 --RedrawingDisabled;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004441#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004442 if (did_set_swapcommand)
4443 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445#ifdef FEAT_BROWSE
4446 vim_free(browse_file);
4447#endif
4448 vim_free(free_fname);
4449 return retval;
4450}
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004453delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
4455 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4456 name == NULL ? (char_u *)"" : name);
4457 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004458 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004459 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004462static int append_indent = 0; /* autoindent for first line */
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464/*
4465 * ":insert" and ":append", also used by ":change"
4466 */
4467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004468ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
4470 char_u *theline;
4471 int did_undo = FALSE;
4472 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004473 int indent = 0;
4474 char_u *p;
4475 int vcol;
4476 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004478 /* the ! flag toggles autoindent */
4479 if (eap->forceit)
4480 curbuf->b_p_ai = !curbuf->b_p_ai;
4481
4482 /* First autoindent comes from the line we start on */
4483 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4484 append_indent = get_indent_lnum(lnum);
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (eap->cmdidx != CMD_append)
4487 --lnum;
4488
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004489 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004490 if (empty && lnum == 1)
4491 lnum = 0;
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 State = INSERT; /* behave like in Insert mode */
4494 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4495 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004496
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004497 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
4499 msg_scroll = TRUE;
4500 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004501 if (curbuf->b_p_ai)
4502 {
4503 if (append_indent >= 0)
4504 {
4505 indent = append_indent;
4506 append_indent = -1;
4507 }
4508 else if (lnum > 0)
4509 indent = get_indent_lnum(lnum);
4510 }
4511 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004513 {
4514 /* No getline() function, use the lines that follow. This ends
4515 * when there is no more. */
4516 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4517 break;
4518 p = vim_strchr(eap->nextcmd, NL);
4519 if (p == NULL)
4520 p = eap->nextcmd + STRLEN(eap->nextcmd);
4521 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4522 if (*p != NUL)
4523 ++p;
4524 eap->nextcmd = p;
4525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004527 {
4528 int save_State = State;
4529
4530 /* Set State to avoid the cursor shape to be set to INSERT mode
4531 * when getline() returns. */
4532 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 theline = eap->getline(
4534#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004535 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004537 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004538 State = save_State;
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004541 if (theline == NULL)
4542 break;
4543
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004544 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4545 if (ex_keep_indent)
4546 append_indent = indent;
4547
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004548 /* Look for the "." after automatic indent. */
4549 vcol = 0;
4550 for (p = theline; indent > vcol; ++p)
4551 {
4552 if (*p == ' ')
4553 ++vcol;
4554 else if (*p == TAB)
4555 vcol += 8 - vcol % 8;
4556 else
4557 break;
4558 }
4559 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004560 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4561 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 vim_free(theline);
4564 break;
4565 }
4566
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004567 /* don't use autoindent if nothing was typed. */
4568 if (p[0] == NUL)
4569 theline[0] = NUL;
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 did_undo = TRUE;
4572 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004573 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 vim_free(theline);
4576 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004577
4578 if (empty)
4579 {
4580 ml_delete(2L, FALSE);
4581 empty = FALSE;
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584 State = NORMAL;
4585
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004586 if (eap->forceit)
4587 curbuf->b_p_ai = !curbuf->b_p_ai;
4588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004590 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 * "end" is set to lnum when something has been appended, otherwise
4592 * it is the same than "start" -- Acevedo */
4593 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4594 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4595 if (eap->cmdidx != CMD_append)
4596 --curbuf->b_op_start.lnum;
4597 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4598 ? lnum : curbuf->b_op_start.lnum;
4599 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4600 curwin->w_cursor.lnum = lnum;
4601 check_cursor_lnum();
4602 beginline(BL_SOL | BL_FIX);
4603
4604 need_wait_return = FALSE; /* don't use wait_return() now */
4605 ex_no_reprint = TRUE;
4606}
4607
4608/*
4609 * ":change"
4610 */
4611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004612ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613{
4614 linenr_T lnum;
4615
4616 if (eap->line2 >= eap->line1
4617 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4618 return;
4619
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004620 /* the ! flag toggles autoindent */
4621 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4622 append_indent = get_indent_lnum(eap->line1);
4623
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4625 {
4626 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4627 break;
4628 ml_delete(eap->line1, FALSE);
4629 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004630
4631 /* make sure the cursor is not beyond the end of the file now */
4632 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4634
4635 /* ":append" on the line above the deleted lines. */
4636 eap->line2 = eap->line1;
4637 ex_append(eap);
4638}
4639
4640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004641ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 char_u *x;
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004644 long bigness;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004645 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 int minus = 0;
4647 linenr_T start, end, curs, i;
4648 int j;
4649 linenr_T lnum = eap->line2;
4650
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004651 /* Vi compatible: ":z!" uses display height, without a count uses
4652 * 'scroll' */
4653 if (eap->forceit)
4654 bigness = curwin->w_height;
Bram Moolenaar459ca562016-11-10 18:16:33 +01004655 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004656 bigness = curwin->w_height - 3;
Bram Moolenaar631abc32014-02-22 22:27:47 +01004657 else
4658 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (bigness < 1)
4660 bigness = 1;
4661
4662 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004663 kind = x;
4664 if (*kind == '-' || *kind == '+' || *kind == '='
4665 || *kind == '^' || *kind == '.')
4666 ++x;
4667 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 ++x;
4669
4670 if (*x != 0)
4671 {
4672 if (!VIM_ISDIGIT(*x))
4673 {
4674 EMSG(_("E144: non-numeric argument to :z"));
4675 return;
4676 }
4677 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004678 {
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004679 bigness = atol((char *)x);
4680
4681 /* bigness could be < 0 if atol(x) overflows. */
4682 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
4683 bigness = 2 * curbuf->b_ml.ml_line_count;
4684
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004685 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004686 if (*kind == '=')
4687 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004691 /* the number of '-' and '+' multiplies the distance */
4692 if (*kind == '-' || *kind == '+')
4693 for (x = kind + 1; *x == *kind; ++x)
4694 ;
4695
4696 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004699 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4700 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004701 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 break;
4703
4704 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004705 start = lnum - (bigness + 1) / 2 + 1;
4706 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 curs = lnum;
4708 minus = 1;
4709 break;
4710
4711 case '^':
4712 start = lnum - bigness * 2;
4713 end = lnum - bigness;
4714 curs = lnum - bigness;
4715 break;
4716
4717 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004718 start = lnum - (bigness + 1) / 2 + 1;
4719 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 curs = end;
4721 break;
4722
4723 default: /* '+' */
4724 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004725 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004726 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004727 else if (eap->addr_count == 0)
4728 ++start;
4729 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 curs = end;
4731 break;
4732 }
4733
4734 if (start < 1)
4735 start = 1;
4736
4737 if (end > curbuf->b_ml.ml_line_count)
4738 end = curbuf->b_ml.ml_line_count;
4739
4740 if (curs > curbuf->b_ml.ml_line_count)
4741 curs = curbuf->b_ml.ml_line_count;
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004742 else if (curs < 1)
4743 curs = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745 for (i = start; i <= end; i++)
4746 {
4747 if (minus && i == lnum)
4748 {
4749 msg_putchar('\n');
4750
4751 for (j = 1; j < Columns; j++)
4752 msg_putchar('-');
4753 }
4754
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004755 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
4757 if (minus && i == lnum)
4758 {
4759 msg_putchar('\n');
4760
4761 for (j = 1; j < Columns; j++)
4762 msg_putchar('-');
4763 }
4764 }
4765
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004766 if (curwin->w_cursor.lnum != curs)
4767 {
4768 curwin->w_cursor.lnum = curs;
4769 curwin->w_cursor.col = 0;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 ex_no_reprint = TRUE;
4772}
4773
4774/*
4775 * Check if the restricted flag is set.
4776 * If so, give an error message and return TRUE.
4777 * Otherwise, return FALSE.
4778 */
4779 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004780check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
4782 if (restricted)
4783 {
4784 EMSG(_("E145: Shell commands not allowed in rvim"));
4785 return TRUE;
4786 }
4787 return FALSE;
4788}
4789
4790/*
4791 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4792 * If so, give an error message and return TRUE.
4793 * Otherwise, return FALSE.
4794 */
4795 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 if (secure)
4799 {
4800 secure = 2;
4801 EMSG(_(e_curdir));
4802 return TRUE;
4803 }
4804#ifdef HAVE_SANDBOX
4805 /*
4806 * In the sandbox more things are not allowed, including the things
4807 * disallowed in secure mode.
4808 */
4809 if (sandbox != 0)
4810 {
4811 EMSG(_(e_sandbox));
4812 return TRUE;
4813 }
4814#endif
4815 return FALSE;
4816}
4817
4818static char_u *old_sub = NULL; /* previous substitute pattern */
4819static int global_need_beginline; /* call beginline() after ":g" */
4820
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004821/*
4822 * Flags that are kept between calls to :substitute.
4823 */
4824typedef struct {
4825 int do_all; /* do multiple substitutions per line */
4826 int do_ask; /* ask for confirmation */
4827 int do_count; /* count only */
4828 int do_error; /* if false, ignore errors */
4829 int do_print; /* print last line with subs. */
4830 int do_list; /* list last line with subs. */
4831 int do_number; /* list last line with line nr*/
4832 int do_ic; /* ignore case flag */
4833} subflags_T;
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835/* do_sub()
4836 *
4837 * Perform a substitution from line eap->line1 to line eap->line2 using the
4838 * command pointed to by eap->arg which should be of the form:
4839 *
4840 * /pattern/substitution/{flags}
4841 *
4842 * The usual escapes are supported as described in the regexp docs.
4843 */
4844 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
4847 linenr_T lnum;
4848 long i = 0;
4849 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004850 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4851 FALSE, FALSE, 0};
4852#ifdef FEAT_EVAL
4853 subflags_T subflags_save;
4854#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004855 int save_do_all; /* remember user specified 'g' flag */
4856 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4858 int delimiter;
4859 int sublen;
4860 int got_quit = FALSE;
4861 int got_match = FALSE;
4862 int temp;
4863 int which_pat;
4864 char_u *cmd;
4865 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004866 linenr_T first_line = 0; /* first changed line */
4867 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 * change */
4869 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4870 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004871 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004872 char_u *sub_firstline; /* allocated copy of first sub line */
4873 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004874 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004875 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004876#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004877 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 cmd = eap->arg;
4881 if (!global_busy)
4882 {
4883 sub_nsubs = 0;
4884 sub_nlines = 0;
4885 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004886 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (eap->cmdidx == CMD_tilde)
4889 which_pat = RE_LAST; /* use last used regexp */
4890 else
4891 which_pat = RE_SUBST; /* use last substitute regexp */
4892
4893 /* new pattern and substitution */
Bram Moolenaar1c465442017-03-12 20:10:05 +01004894 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4896 {
4897 /* don't accept alphanumeric for separator */
4898 if (isalpha(*cmd))
4899 {
4900 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4901 return;
4902 }
4903 /*
4904 * undocumented vi feature:
4905 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4906 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4907 */
4908 if (*cmd == '\\')
4909 {
4910 ++cmd;
4911 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4912 {
4913 EMSG(_(e_backslash));
4914 return;
4915 }
4916 if (*cmd != '&')
4917 which_pat = RE_SEARCH; /* use last '/' pattern */
4918 pat = (char_u *)""; /* empty search pattern */
4919 delimiter = *cmd++; /* remember delimiter character */
4920 }
4921 else /* find the end of the regexp */
4922 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004923#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4924 if (p_altkeymap && curwin->w_p_rl)
4925 lrF_sub(cmd);
4926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 which_pat = RE_LAST; /* use last used regexp */
4928 delimiter = *cmd++; /* remember delimiter character */
4929 pat = cmd; /* remember start of search pat */
4930 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4931 if (cmd[0] == delimiter) /* end delimiter found */
4932 *cmd++ = NUL; /* replace it with a NUL */
4933 }
4934
4935 /*
4936 * Small incompatibility: vi sees '\n' as end of the command, but in
4937 * Vim we want to use '\n' to find/substitute a NUL.
4938 */
4939 sub = cmd; /* remember the start of the substitution */
4940
4941 while (cmd[0])
4942 {
4943 if (cmd[0] == delimiter) /* end delimiter found */
4944 {
4945 *cmd++ = NUL; /* replace it with a NUL */
4946 break;
4947 }
4948 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4949 ++cmd;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004950 MB_PTR_ADV(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952
4953 if (!eap->skip)
4954 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004955 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4956 if (STRCMP(sub, "%") == 0
4957 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4958 {
4959 if (old_sub == NULL) /* there is no previous command */
4960 {
4961 EMSG(_(e_nopresub));
4962 return;
4963 }
4964 sub = old_sub;
4965 }
4966 else
4967 {
4968 vim_free(old_sub);
4969 old_sub = vim_strsave(sub);
4970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 }
4972 }
4973 else if (!eap->skip) /* use previous pattern and substitution */
4974 {
4975 if (old_sub == NULL) /* there is no previous command */
4976 {
4977 EMSG(_(e_nopresub));
4978 return;
4979 }
4980 pat = NULL; /* search_regcomp() will use previous pattern */
4981 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004982
4983 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4984 * last column after using "$". */
4985 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004988 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4989 * more efficient.
4990 * TODO: find a generic solution to make line-joining operations more
4991 * efficient, avoid allocating a string that grows in size.
4992 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004993 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004994 && *sub == NUL
4995 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4996 || *cmd == 'p' || *cmd == '#'))))
4997 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004998 linenr_T joined_lines_count;
4999
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005000 curwin->w_cursor.lnum = eap->line1;
5001 if (*cmd == 'l')
5002 eap->flags = EXFLAG_LIST;
5003 else if (*cmd == '#')
5004 eap->flags = EXFLAG_NR;
5005 else if (*cmd == 'p')
5006 eap->flags = EXFLAG_PRINT;
5007
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005008 /* The number of lines joined is the number of lines in the range plus
5009 * one. One less when the last line is included. */
5010 joined_lines_count = eap->line2 - eap->line1 + 1;
5011 if (eap->line2 < curbuf->b_ml.ml_line_count)
5012 ++joined_lines_count;
5013 if (joined_lines_count > 1)
5014 {
5015 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
5016 sub_nsubs = joined_lines_count - 1;
5017 sub_nlines = 1;
5018 (void)do_sub_msg(FALSE);
5019 ex_may_print(eap);
5020 }
5021
5022 if (!cmdmod.keeppatterns)
5023 save_re_pat(RE_SUBST, pat, p_magic);
5024#ifdef FEAT_CMDHIST
5025 /* put pattern in history */
5026 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
5027#endif
5028
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005029 return;
5030 }
5031
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 /*
5033 * Find trailing options. When '&' is used, keep old options.
5034 */
5035 if (*cmd == '&')
5036 ++cmd;
5037 else
5038 {
5039 if (!p_ed)
5040 {
5041 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005042 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005044 subflags.do_all = FALSE;
5045 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005047 subflags.do_error = TRUE;
5048 subflags.do_print = FALSE;
5049 subflags.do_count = FALSE;
5050 subflags.do_number = FALSE;
5051 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 while (*cmd)
5054 {
5055 /*
5056 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
5057 * 'r' is never inverted.
5058 */
5059 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005060 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005062 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005063 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005064 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005066 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 else if (*cmd == 'r') /* use last used regexp */
5068 which_pat = RE_LAST;
5069 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005070 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005071 else if (*cmd == '#')
5072 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005073 subflags.do_print = TRUE;
5074 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005075 }
5076 else if (*cmd == 'l')
5077 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005078 subflags.do_print = TRUE;
5079 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005082 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005084 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else
5086 break;
5087 ++cmd;
5088 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005089 if (subflags.do_count)
5090 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005092 save_do_all = subflags.do_all;
5093 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /*
5096 * check for a trailing count
5097 */
5098 cmd = skipwhite(cmd);
5099 if (VIM_ISDIGIT(*cmd))
5100 {
5101 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005102 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 EMSG(_(e_zerocount));
5105 return;
5106 }
5107 eap->line1 = eap->line2;
5108 eap->line2 += i - 1;
5109 if (eap->line2 > curbuf->b_ml.ml_line_count)
5110 eap->line2 = curbuf->b_ml.ml_line_count;
5111 }
5112
5113 /*
5114 * check for trailing command or garbage
5115 */
5116 cmd = skipwhite(cmd);
5117 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5118 {
5119 eap->nextcmd = check_nextcmd(cmd);
5120 if (eap->nextcmd == NULL)
5121 {
5122 EMSG(_(e_trailing));
5123 return;
5124 }
5125 }
5126
5127 if (eap->skip) /* not executing commands, only parsing */
5128 return;
5129
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005130 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005131 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005132 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005133 EMSG(_(e_modifiable));
5134 return;
5135 }
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5138 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005139 if (subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 EMSG(_(e_invcmd));
5141 return;
5142 }
5143
5144 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005145 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005147 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 regmatch.rmm_ic = FALSE;
5149
5150 sub_firstline = NULL;
5151
5152 /*
5153 * ~ in the substitute pattern is replaced with the old pattern.
5154 * We do it here once to avoid it to be replaced over and over again.
5155 * But don't do it when it starts with "\=", then it's an expression.
5156 */
5157 if (!(sub[0] == '\\' && sub[1] == '='))
5158 sub = regtilde(sub, p_magic);
5159
5160 /*
5161 * Check for a match on each line.
5162 */
5163 line2 = eap->line2;
5164 for (lnum = eap->line1; lnum <= line2 && !(got_quit
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005165#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 || aborting()
5167#endif
5168 ); ++lnum)
5169 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005170 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005171 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (nmatch)
5173 {
5174 colnr_T copycol;
5175 colnr_T matchcol;
5176 colnr_T prev_matchcol = MAXCOL;
5177 char_u *new_end, *new_start = NULL;
5178 unsigned new_start_len = 0;
5179 char_u *p1;
5180 int did_sub = FALSE;
5181 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005182 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 long nmatch_tl = 0; /* nr of lines matched below lnum */
5184 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005185 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005186 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 /*
5189 * The new text is build up step by step, to avoid too much
5190 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005191 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 * copycol Column in the old text where we started
5193 * looking for a match; from here old text still
5194 * needs to be copied to the new text.
5195 * matchcol Column number of the old text where to look
5196 * for the next match. It's just after the
5197 * previous match or one further.
5198 * prev_matchcol Column just after the previous match (if any).
5199 * Mostly equal to matchcol, except for the first
5200 * match and after skipping an empty match.
5201 * regmatch.*pos Where the pattern matched in the old text.
5202 * new_start The new text, all that has been produced so
5203 * far.
5204 * new_end The new text, where to append new text.
5205 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005206 * lnum The line number where we found the start of
5207 * the match. Can be below the line we searched
5208 * when there is a \n before a \zs in the
5209 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 * sub_firstlnum The line number in the buffer where to look
5211 * for a match. Can be different from "lnum"
5212 * when the pattern or substitute string contains
5213 * line breaks.
5214 *
5215 * Special situations:
5216 * - When the substitute string contains a line break, the part up
5217 * to the line break is inserted in the text, but the copy of
5218 * the original line is kept. "sub_firstlnum" is adjusted for
5219 * the inserted lines.
5220 * - When the matched pattern contains a line break, the old line
5221 * is taken from the line at the end of the pattern. The lines
5222 * in the match are deleted later, "sub_firstlnum" is adjusted
5223 * accordingly.
5224 *
5225 * The new text is built up in new_start[]. It has some extra
5226 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005227 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 *
5229 * Make a copy of the old line, so it won't be taken away when
5230 * updating the screen or handling a multi-line match. The "old_"
5231 * pointers point into this copy.
5232 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005233 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 copycol = 0;
5235 matchcol = 0;
5236
5237 /* At first match, remember current cursor position. */
5238 if (!got_match)
5239 {
5240 setpcmark();
5241 got_match = TRUE;
5242 }
5243
5244 /*
5245 * Loop until nothing more to replace in this line.
5246 * 1. Handle match with empty string.
5247 * 2. If do_ask is set, ask for confirmation.
5248 * 3. substitute the string.
5249 * 4. if do_all is set, find next match
5250 * 5. break if there isn't another match in this line
5251 */
5252 for (;;)
5253 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005254 /* Advance "lnum" to the line where the match starts. The
5255 * match does not start in the first line when there is a line
5256 * break before \zs. */
5257 if (regmatch.startpos[0].lnum > 0)
5258 {
5259 lnum += regmatch.startpos[0].lnum;
5260 sub_firstlnum += regmatch.startpos[0].lnum;
5261 nmatch -= regmatch.startpos[0].lnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005262 VIM_CLEAR(sub_firstline);
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005263 }
5264
5265 if (sub_firstline == NULL)
5266 {
5267 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5268 if (sub_firstline == NULL)
5269 {
5270 vim_free(new_start);
5271 goto outofmem;
5272 }
5273 }
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /* Save the line number of the last change for the final
5276 * cursor position (just like Vi). */
5277 curwin->w_cursor.lnum = lnum;
5278 do_again = FALSE;
5279
5280 /*
5281 * 1. Match empty string does not count, except for first
5282 * match. This reproduces the strange vi behaviour.
5283 * This also catches endless loops.
5284 */
5285 if (matchcol == prev_matchcol
5286 && regmatch.endpos[0].lnum == 0
5287 && matchcol == regmatch.endpos[0].col)
5288 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005289 if (sub_firstline[matchcol] == NUL)
5290 /* We already were at the end of the line. Don't look
5291 * for a match in this line again. */
5292 skip_match = TRUE;
5293 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005294 {
5295 /* search for a match at next column */
5296#ifdef FEAT_MBYTE
5297 if (has_mbyte)
5298 matchcol += mb_ptr2len(sub_firstline + matchcol);
5299 else
5300#endif
5301 ++matchcol;
5302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 goto skip;
5304 }
5305
5306 /* Normally we continue searching for a match just after the
5307 * previous match. */
5308 matchcol = regmatch.endpos[0].col;
5309 prev_matchcol = matchcol;
5310
5311 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005312 * 2. If do_count is set only increase the counter.
5313 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005315 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005316 {
5317 /* For a multi-line match, put matchcol at the NUL at
5318 * the end of the line and set nmatch to one, so that
5319 * we continue looking for a match on the next line.
5320 * Avoids that ":s/\nB\@=//gc" get stuck. */
5321 if (nmatch > 1)
5322 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005323 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005324 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005325 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005326 }
5327 sub_nsubs++;
5328 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005329#ifdef FEAT_EVAL
5330 /* Skip the substitution, unless an expression is used,
5331 * then it is evaluated in the sandbox. */
5332 if (!(sub[0] == '\\' && sub[1] == '='))
5333#endif
5334 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005335 }
5336
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005337 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005339 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 /* change State to CONFIRM, so that the mouse works
5342 * properly */
5343 save_State = State;
5344 State = CONFIRM;
5345#ifdef FEAT_MOUSE
5346 setmouse(); /* disable mouse in xterm */
5347#endif
5348 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005349 if (curwin->w_p_crb)
5350 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 /* When 'cpoptions' contains "u" don't sync undo when
5353 * asking for confirmation. */
5354 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5355 ++no_u_sync;
5356
5357 /*
5358 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5359 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005360 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005362 if (exmode_active)
5363 {
5364 char_u *resp;
5365 colnr_T sc, ec;
5366
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005367 print_line_no_prefix(lnum,
5368 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005369
5370 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5371 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005372 if (curwin->w_cursor.col < 0)
5373 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005374 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005375 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005376 {
5377 int numw = number_width(curwin) + 1;
5378 sc += numw;
5379 ec += numw;
5380 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005381 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005382 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005383 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005384 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005385 msg_putchar('^');
5386
5387 resp = getexmodeline('?', NULL, 0);
5388 if (resp != NULL)
5389 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005390 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005391 vim_free(resp);
5392 }
5393 }
5394 else
5395 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005396 char_u *orig_line = NULL;
5397 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005399 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005401 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005403 /* Invert the matched string.
5404 * Remove the inversion afterwards. */
5405 temp = RedrawingDisabled;
5406 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005408 if (new_start != NULL)
5409 {
5410 /* There already was a substitution, we would
5411 * like to show this to the user. We cannot
5412 * really update the line, it would change
5413 * what matches. Temporarily replace the line
5414 * and change it back afterwards. */
5415 orig_line = vim_strsave(ml_get(lnum));
5416 if (orig_line != NULL)
5417 {
5418 char_u *new_line = concat_str(new_start,
5419 sub_firstline + copycol);
5420
5421 if (new_line == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005422 VIM_CLEAR(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005423 else
5424 {
5425 /* Position the cursor relative to the
5426 * end of the line, the previous
5427 * substitute may have inserted or
5428 * deleted characters before the
5429 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005430 len_change = (int)STRLEN(new_line)
5431 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005432 curwin->w_cursor.col += len_change;
5433 ml_replace(lnum, new_line, FALSE);
5434 }
5435 }
5436 }
5437
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005438 search_match_lines = regmatch.endpos[0].lnum
5439 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005440 search_match_endcol = regmatch.endpos[0].col
5441 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005442 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005444 update_topline();
5445 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005446 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005447 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005448 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005451 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005453 if (msg_row == Rows - 1)
5454 msg_didout = FALSE; /* avoid a scroll-up */
5455 msg_starthere();
5456 i = msg_scroll;
5457 msg_scroll = 0; /* truncate msg when
5458 needed */
5459 msg_no_more = TRUE;
5460 /* write message same highlighting as for
5461 * wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005462 smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005463 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
5464 msg_no_more = FALSE;
5465 msg_scroll = i;
5466 showruler(TRUE);
5467 windgoto(msg_row, msg_col);
5468 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005471 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005473 ++no_mapping; /* don't map this key */
5474 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005475 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005476 --allow_keys;
5477 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005479 /* clear the question */
5480 msg_didout = FALSE; /* don't scroll up */
5481 msg_col = 0;
5482 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005483
5484 /* restore the line */
5485 if (orig_line != NULL)
5486 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005487 }
5488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005490 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005492 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493#endif
5494 )
5495 {
5496 got_quit = TRUE;
5497 break;
5498 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005499 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005501 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005503 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005506 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 line2 = lnum;
5508 break;
5509 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005510 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005512 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 break;
5514 }
5515#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005516 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005518 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 scrolldown_clamp();
5520#endif
5521 }
5522 State = save_State;
5523#ifdef FEAT_MOUSE
5524 setmouse();
5525#endif
5526 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5527 --no_u_sync;
5528
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005529 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 /* For a multi-line match, put matchcol at the NUL at
5532 * the end of the line and set nmatch to one, so that
5533 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005534 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5535 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 if (nmatch > 1)
5537 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005538 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005539 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541 goto skip;
5542 }
5543 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005544 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546
5547 /* Move the cursor to the start of the match, so that we can
5548 * use "\=col("."). */
5549 curwin->w_cursor.col = regmatch.startpos[0].col;
5550
5551 /*
5552 * 3. substitute the string.
5553 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005554#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005555 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005556 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005557 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005558 save_ma = curbuf->b_p_ma;
5559 curbuf->b_p_ma = FALSE;
5560 sandbox++;
5561 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005562 /* Save flags for recursion. They can change for e.g.
5563 * :s/^/\=execute("s#^##gn") */
5564 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005567 sublen = vim_regsub_multi(&regmatch,
5568 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005570#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005571 /* Don't keep flags set by a recursive call. */
5572 subflags = subflags_save;
5573 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005574 {
5575 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005576 if (sandbox > 0)
5577 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005578 goto skip;
5579 }
5580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
Bram Moolenaar4463f292005-09-25 22:20:24 +00005582 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005583 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005584 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5585 {
5586 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5587 skip_match = TRUE;
5588 }
5589
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 /* Need room for:
5591 * - result so far in new_start (not for first sub in line)
5592 * - original text up to match
5593 * - length of substituted part
5594 * - original text after match
5595 */
5596 if (nmatch == 1)
5597 p1 = sub_firstline;
5598 else
5599 {
5600 p1 = ml_get(sub_firstlnum + nmatch - 1);
5601 nmatch_tl += nmatch - 1;
5602 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005603 copy_len = regmatch.startpos[0].col - copycol;
5604 needed_len = copy_len + ((unsigned)STRLEN(p1)
5605 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (new_start == NULL)
5607 {
5608 /*
5609 * Get some space for a temporary buffer to do the
5610 * substitution into (and some extra space to avoid
5611 * too many calls to alloc()/free()).
5612 */
5613 new_start_len = needed_len + 50;
5614 if ((new_start = alloc_check(new_start_len)) == NULL)
5615 goto outofmem;
5616 *new_start = NUL;
5617 new_end = new_start;
5618 }
5619 else
5620 {
5621 /*
5622 * Check if the temporary buffer is long enough to do the
5623 * substitution into. If not, make it larger (with a bit
5624 * extra to avoid too many calls to alloc()/free()).
5625 */
5626 len = (unsigned)STRLEN(new_start);
5627 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005628 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
5630 new_start_len = needed_len + 50;
5631 if ((p1 = alloc_check(new_start_len)) == NULL)
5632 {
5633 vim_free(new_start);
5634 goto outofmem;
5635 }
5636 mch_memmove(p1, new_start, (size_t)(len + 1));
5637 vim_free(new_start);
5638 new_start = p1;
5639 }
5640 new_end = new_start + len;
5641 }
5642
5643 /*
5644 * copy the text up to the part that matched
5645 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005646 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5647 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005649 (void)vim_regsub_multi(&regmatch,
5650 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 sub, new_end, TRUE, p_magic, TRUE);
5652 sub_nsubs++;
5653 did_sub = TRUE;
5654
5655 /* Move the cursor to the start of the line, to avoid that it
5656 * is beyond the end of the line after the substitution. */
5657 curwin->w_cursor.col = 0;
5658
5659 /* For a multi-line match, make a copy of the last matched
5660 * line and continue in that one. */
5661 if (nmatch > 1)
5662 {
5663 sub_firstlnum += nmatch - 1;
5664 vim_free(sub_firstline);
5665 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5666 /* When going beyond the last line, stop substituting. */
5667 if (sub_firstlnum <= line2)
5668 do_again = TRUE;
5669 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005670 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672
5673 /* Remember next character to be copied. */
5674 copycol = regmatch.endpos[0].col;
5675
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005676 if (skip_match)
5677 {
5678 /* Already hit end of the buffer, sub_firstlnum is one
5679 * less than what it ought to be. */
5680 vim_free(sub_firstline);
5681 sub_firstline = vim_strsave((char_u *)"");
5682 copycol = 0;
5683 }
5684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 /*
5686 * Now the trick is to replace CTRL-M chars with a real line
5687 * break. This would make it impossible to insert a CTRL-M in
5688 * the text. The line break can be avoided by preceding the
5689 * CTRL-M with a backslash. To be able to insert a backslash,
5690 * they must be doubled in the string and are halved here.
5691 * That is Vi compatible.
5692 */
5693 for (p1 = new_end; *p1; ++p1)
5694 {
5695 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005696 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else if (*p1 == CAR)
5698 {
5699 if (u_inssub(lnum) == OK) /* prepare for undo */
5700 {
5701 *p1 = NUL; /* truncate up to the CR */
5702 ml_append(lnum - 1, new_start,
5703 (colnr_T)(p1 - new_start + 1), FALSE);
5704 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005705 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 appended_lines(lnum - 1, 1L);
5707 else
5708 {
5709 if (first_line == 0)
5710 first_line = lnum;
5711 last_line = lnum + 1;
5712 }
5713 /* All line numbers increase. */
5714 ++sub_firstlnum;
5715 ++lnum;
5716 ++line2;
5717 /* move the cursor to the new line, like Vi */
5718 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005719 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005720 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 p1 = new_start - 1;
5722 }
5723 }
5724#ifdef FEAT_MBYTE
5725 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005726 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727#endif
5728 }
5729
5730 /*
5731 * 4. If do_all is set, find next match.
5732 * Prevent endless loop with patterns that match empty
5733 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5734 * But ":s/\n/#/" is OK.
5735 */
5736skip:
5737 /* We already know that we did the last subst when we are at
5738 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005739 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5740 * "line2" when there is a \zs in the pattern after a line
5741 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005742 lastone = (skip_match
5743 || got_int
5744 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005745 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005746 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005747 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5748 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 nmatch = -1;
5750
5751 /*
5752 * Replace the line in the buffer when needed. This is
5753 * skipped when there are more matches.
5754 * The check for nmatch_tl is needed for when multi-line
5755 * matching must replace the lines before trying to do another
5756 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005757 * When the match starts below where we start searching also
5758 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 */
5760 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 || nmatch_tl > 0
5762 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005763 curbuf, sub_firstlnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005764 matchcol, NULL, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005765 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 if (new_start != NULL)
5768 {
5769 /*
5770 * Copy the rest of the line, that didn't match.
5771 * "matchcol" has to be adjusted, we use the end of
5772 * the line as reference, because the substitute may
5773 * have changed the number of characters. Same for
5774 * "prev_matchcol".
5775 */
5776 STRCAT(new_start, sub_firstline + copycol);
5777 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5778 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5779 - prev_matchcol;
5780
5781 if (u_savesub(lnum) != OK)
5782 break;
5783 ml_replace(lnum, new_start, TRUE);
5784
5785 if (nmatch_tl > 0)
5786 {
5787 /*
5788 * Matched lines have now been substituted and are
5789 * useless, delete them. The part after the match
5790 * has been appended to new_start, we don't need
5791 * it in the buffer.
5792 */
5793 ++lnum;
5794 if (u_savedel(lnum, nmatch_tl) != OK)
5795 break;
5796 for (i = 0; i < nmatch_tl; ++i)
5797 ml_delete(lnum, (int)FALSE);
5798 mark_adjust(lnum, lnum + nmatch_tl - 1,
5799 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005800 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 deleted_lines(lnum, nmatch_tl);
5802 --lnum;
5803 line2 -= nmatch_tl; /* nr of lines decreases */
5804 nmatch_tl = 0;
5805 }
5806
5807 /* When asking, undo is saved each time, must also set
5808 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005809 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 changed_bytes(lnum, 0);
5811 else
5812 {
5813 if (first_line == 0)
5814 first_line = lnum;
5815 last_line = lnum + 1;
5816 }
5817
5818 sub_firstlnum = lnum;
5819 vim_free(sub_firstline); /* free the temp buffer */
5820 sub_firstline = new_start;
5821 new_start = NULL;
5822 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5823 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5824 - prev_matchcol;
5825 copycol = 0;
5826 }
5827 if (nmatch == -1 && !lastone)
5828 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005829 sub_firstlnum, matchcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831 /*
5832 * 5. break if there isn't another match in this line
5833 */
5834 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005835 {
5836 /* If the match found didn't start where we were
5837 * searching, do the next search in the line where we
5838 * found the match. */
5839 if (nmatch == -1)
5840 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844
5845 line_breakcheck();
5846 }
5847
5848 if (did_sub)
5849 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005850 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaard23a8232018-02-10 18:45:26 +01005851 VIM_CLEAR(sub_firstline); /* free the copy of the original line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853
5854 line_breakcheck();
5855 }
5856
5857 if (first_line != 0)
5858 {
5859 /* Need to subtract the number of added lines from "last_line" to get
5860 * the line number before the change (same as adding the number of
5861 * deleted lines). */
5862 i = curbuf->b_ml.ml_line_count - old_line_count;
5863 changed_lines(first_line, 0, last_line - i, i);
5864 }
5865
5866outofmem:
5867 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005868
5869 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005870 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005871 curwin->w_cursor = old_cursor;
5872
Bram Moolenaar46475522010-03-23 17:36:29 +01005873 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
5875 /* Set the '[ and '] marks. */
5876 curbuf->b_op_start.lnum = eap->line1;
5877 curbuf->b_op_end.lnum = line2;
5878 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5879
5880 if (!global_busy)
5881 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005882 /* when interactive leave cursor on the match */
5883 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005884 {
5885 if (endcolumn)
5886 coladvance((colnr_T)MAXCOL);
5887 else
5888 beginline(BL_WHITE | BL_FIX);
5889 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005890 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 MSG("");
5892 }
5893 else
5894 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005895 if (subflags.do_print)
5896 print_line(curwin->w_cursor.lnum,
5897 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 }
5899 else if (!global_busy)
5900 {
5901 if (got_int) /* interrupted */
5902 EMSG(_(e_interr));
5903 else if (got_match) /* did find something but nothing substituted */
5904 MSG("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005905 else if (subflags.do_error) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 EMSG2(_(e_patnotf2), get_search_pat());
5907 }
5908
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005909#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005910 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005911 /* Cursor position may require updating */
5912 changed_window_setting();
5913#endif
5914
Bram Moolenaar473de612013-06-08 18:19:48 +02005915 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005916
5917 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005918 subflags.do_all = save_do_all;
5919 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920}
5921
5922/*
5923 * Give message for number of substitutions.
5924 * Can also be used after a ":global" command.
5925 * Return TRUE if a message was given.
5926 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005927 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005928do_sub_msg(
5929 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 /*
5932 * Only report substitutions when:
5933 * - more than 'report' substitutions
5934 * - command was typed by user, or number of changed lines > 'report'
5935 * - giving messages is not disabled by 'lazyredraw'
5936 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005937 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5938 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 && messaging())
5940 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005941 char *msg_single;
5942 char *msg_plural;
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 if (got_int)
5945 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005946 else
5947 *msg_buf = NUL;
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005948
5949 msg_single = count_only
5950 ? NGETTEXT("%ld match on %ld line",
5951 "%ld matches on %ld line", sub_nsubs)
5952 : NGETTEXT("%ld substitution on %ld line",
5953 "%ld substitutions on %ld line", sub_nsubs);
5954 msg_plural = count_only
5955 ? NGETTEXT("%ld match on %ld lines",
5956 "%ld matches on %ld lines", sub_nsubs)
5957 : NGETTEXT("%ld substitution on %ld lines",
5958 "%ld substitutions on %ld lines", sub_nsubs);
5959
5960 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
5961 NGETTEXT(msg_single, msg_plural, sub_nlines),
5962 sub_nsubs, (long)sub_nlines);
5963
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005966 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 return TRUE;
5968 }
5969 if (got_int)
5970 {
5971 EMSG(_(e_interr));
5972 return TRUE;
5973 }
5974 return FALSE;
5975}
5976
Bram Moolenaarf84b1222017-06-10 14:29:52 +02005977 static void
5978global_exe_one(char_u *cmd, linenr_T lnum)
5979{
5980 curwin->w_cursor.lnum = lnum;
5981 curwin->w_cursor.col = 0;
5982 if (*cmd == NUL || *cmd == '\n')
5983 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5984 else
5985 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5986}
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988/*
5989 * Execute a global command of the form:
5990 *
5991 * g/pattern/X : execute X on all lines where pattern matches
5992 * v/pattern/X : execute X on all lines where pattern does not match
5993 *
5994 * where 'X' is an EX command
5995 *
5996 * The command character (as well as the trailing slash) is optional, and
5997 * is assumed to be 'p' if missing.
5998 *
5999 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006000 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 * for each line that has a mark. This is required because after deleting
6002 * lines we do not know where to search for the next match.
6003 */
6004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
6007 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006008 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 int type; /* first char of cmd: 'v' or 'g' */
6010 char_u *cmd; /* command argument */
6011
6012 char_u delim; /* delimiter, normally '/' */
6013 char_u *pat;
6014 regmmatch_T regmatch;
6015 int match;
6016 int which_pat;
6017
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006018 /* When nesting the command works on one line. This allows for
6019 * ":g/found/v/notfound/command". */
6020 if (global_busy && (eap->line1 != 1
6021 || eap->line2 != curbuf->b_ml.ml_line_count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006023 /* will increment global_busy to break out of the loop */
6024 EMSG(_("E147: Cannot do :global recursive with a range"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 return;
6026 }
6027
6028 if (eap->forceit) /* ":global!" is like ":vglobal" */
6029 type = 'v';
6030 else
6031 type = *eap->cmd;
6032 cmd = eap->arg;
6033 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035 /*
6036 * undocumented vi feature:
6037 * "\/" and "\?": use previous search pattern.
6038 * "\&": use previous substitute pattern.
6039 */
6040 if (*cmd == '\\')
6041 {
6042 ++cmd;
6043 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
6044 {
6045 EMSG(_(e_backslash));
6046 return;
6047 }
6048 if (*cmd == '&')
6049 which_pat = RE_SUBST; /* use previous substitute pattern */
6050 else
6051 which_pat = RE_SEARCH; /* use previous search pattern */
6052 ++cmd;
6053 pat = (char_u *)"";
6054 }
6055 else if (*cmd == NUL)
6056 {
6057 EMSG(_("E148: Regular expression missing from global"));
6058 return;
6059 }
6060 else
6061 {
6062 delim = *cmd; /* get the delimiter */
6063 if (delim)
6064 ++cmd; /* skip delimiter if there is one */
6065 pat = cmd; /* remember start of pattern */
6066 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
6067 if (cmd[0] == delim) /* end delimiter found */
6068 *cmd++ = NUL; /* replace it with a NUL */
6069 }
6070
6071#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
6072 if (p_altkeymap && curwin->w_p_rl)
6073 lrFswap(pat,0);
6074#endif
6075
6076 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
6077 {
6078 EMSG(_(e_invcmd));
6079 return;
6080 }
6081
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006082 if (global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006084 lnum = curwin->w_cursor.lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006085 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006086 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if ((type == 'g' && match) || (type == 'v' && !match))
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006088 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 }
6090 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006091 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006092 /*
6093 * pass 1: set marks for each (not) matching line
6094 */
6095 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6096 {
6097 /* a match on this line? */
6098 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006099 (colnr_T)0, NULL, NULL);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006100 if ((type == 'g' && match) || (type == 'v' && !match))
6101 {
6102 ml_setmarked(lnum);
6103 ndone++;
6104 }
6105 line_breakcheck();
6106 }
6107
6108 /*
6109 * pass 2: execute the command for each line that has been marked
6110 */
6111 if (got_int)
6112 MSG(_(e_interr));
6113 else if (ndone == 0)
6114 {
6115 if (type == 'v')
6116 smsg((char_u *)_("Pattern found in every line: %s"), pat);
6117 else
6118 smsg((char_u *)_("Pattern not found: %s"), pat);
6119 }
6120 else
6121 {
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006122#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006123 start_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006124#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006125 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006126#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006127 end_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006128#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006129 }
6130
6131 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
Bram Moolenaar473de612013-06-08 18:19:48 +02006134 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135}
6136
6137/*
6138 * Execute "cmd" on lines marked with ml_setmarked().
6139 */
6140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006143 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6144 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6145 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 /*
6148 * Set current position only once for a global command.
6149 * If global_busy is set, setpcmark() will not do anything.
6150 * If there is an error, global_busy will be incremented.
6151 */
6152 setpcmark();
6153
6154 /* When the command writes a message, don't overwrite the command. */
6155 msg_didout = TRUE;
6156
Bram Moolenaard25bc232010-03-23 17:49:24 +01006157 sub_nsubs = 0;
6158 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 global_need_beginline = FALSE;
6160 global_busy = 1;
6161 old_lcount = curbuf->b_ml.ml_line_count;
6162 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6163 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006164 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 ui_breakcheck();
6166 }
6167
6168 global_busy = 0;
6169 if (global_need_beginline)
6170 beginline(BL_WHITE | BL_FIX);
6171 else
6172 check_cursor(); /* cursor may be beyond the end of the line */
6173
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006174 /* the cursor may not have moved in the text but a change in a previous
6175 * line may move it on the screen */
6176 changed_line_abv_curs();
6177
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 /* If it looks like no message was written, allow overwriting the
6179 * command with the report for number of changes. */
6180 if (msg_col == 0 && msg_scrolled == 0)
6181 msg_didout = FALSE;
6182
Bram Moolenaar45667512007-05-10 19:24:43 +00006183 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006184 * number of extra or deleted lines.
6185 * Don't report extra or deleted lines in the edge case where the buffer
6186 * we are in after execution is different from the buffer we started in. */
6187 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6189}
6190
6191#ifdef FEAT_VIMINFO
6192 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006195 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 vim_free(old_sub);
6197 if (force || old_sub == NULL)
6198 old_sub = viminfo_readstring(virp, 1, TRUE);
6199 return viminfo_readline(virp);
6200}
6201
6202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
6205 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6206 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006207 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 viminfo_writestring(fp, old_sub);
6209 }
6210}
6211#endif /* FEAT_VIMINFO */
6212
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006213#if defined(EXITFREE) || defined(PROTO)
6214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006215free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006216{
6217 vim_free(old_sub);
6218}
6219#endif
6220
Bram Moolenaar4033c552017-09-16 20:54:51 +02006221#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222/*
6223 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006224 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006226 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006227prepare_tagpreview(
6228 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 win_T *wp;
6231
6232# ifdef FEAT_GUI
6233 need_mouse_correct = TRUE;
6234# endif
6235
6236 /*
6237 * If there is already a preview window open, use that one.
6238 */
6239 if (!curwin->w_p_pvw)
6240 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 if (wp->w_p_pvw)
6243 break;
6244 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006245 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 else
6247 {
6248 /*
6249 * There is no preview window open yet. Create one.
6250 */
6251 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6252 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006253 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 curwin->w_p_pvw = TRUE;
6255 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006256 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6257 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258# ifdef FEAT_DIFF
6259 curwin->w_p_diff = FALSE; /* no 'diff' */
6260# endif
6261# ifdef FEAT_FOLDING
6262 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6263# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006264 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
6266 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006267 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268}
6269
6270#endif
6271
6272
6273/*
6274 * ":help": open a read-only window on a help file
6275 */
6276 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 char_u *arg;
6280 char_u *tag;
6281 FILE *helpfd; /* file descriptor of help file */
6282 int n;
6283 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 int num_matches;
6286 char_u **matches;
6287 char_u *p;
6288 int empty_fnum = 0;
6289 int alt_fnum = 0;
6290 buf_T *buf;
6291#ifdef FEAT_MULTI_LANG
6292 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006293 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006295#ifdef FEAT_FOLDING
6296 int old_KeyTyped = KeyTyped;
6297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 if (eap != NULL)
6300 {
6301 /*
6302 * A ":help" command ends at the first LF, or at a '|' that is
6303 * followed by some text. Set nextcmd to the following command.
6304 */
6305 for (arg = eap->arg; *arg; ++arg)
6306 {
6307 if (*arg == '\n' || *arg == '\r'
6308 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6309 {
6310 *arg++ = NUL;
6311 eap->nextcmd = arg;
6312 break;
6313 }
6314 }
6315 arg = eap->arg;
6316
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006317 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 {
6319 EMSG(_("E478: Don't panic!"));
6320 return;
6321 }
6322
6323 if (eap->skip) /* not executing commands */
6324 return;
6325 }
6326 else
6327 arg = (char_u *)"";
6328
6329 /* remove trailing blanks */
6330 p = arg + STRLEN(arg) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006331 while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 *p-- = NUL;
6333
6334#ifdef FEAT_MULTI_LANG
6335 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006336 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337#endif
6338
6339 /* When no argument given go to the index. */
6340 if (*arg == NUL)
6341 arg = (char_u *)"help.txt";
6342
6343 /*
6344 * Check if there is a match for the argument.
6345 */
6346 n = find_help_tags(arg, &num_matches, &matches,
6347 eap != NULL && eap->forceit);
6348
6349 i = 0;
6350#ifdef FEAT_MULTI_LANG
6351 if (n != FAIL && lang != NULL)
6352 /* Find first item with the requested language. */
6353 for (i = 0; i < num_matches; ++i)
6354 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006355 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (len > 3 && matches[i][len - 3] == '@'
6357 && STRICMP(matches[i] + len - 2, lang) == 0)
6358 break;
6359 }
6360#endif
6361 if (i >= num_matches || n == FAIL)
6362 {
6363#ifdef FEAT_MULTI_LANG
6364 if (lang != NULL)
6365 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
6366 else
6367#endif
6368 EMSG2(_("E149: Sorry, no help for %s"), arg);
6369 if (n != FAIL)
6370 FreeWild(num_matches, matches);
6371 return;
6372 }
6373
6374 /* The first match (in the requested language) is the best match. */
6375 tag = vim_strsave(matches[i]);
6376 FreeWild(num_matches, matches);
6377
6378#ifdef FEAT_GUI
6379 need_mouse_correct = TRUE;
6380#endif
6381
6382 /*
6383 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006384 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006386 if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 {
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006388 if (cmdmod.tab != 0)
6389 wp = NULL;
6390 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006391 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006392 if (bt_help(wp->w_buffer))
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6395 win_enter(wp, TRUE);
6396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 {
6398 /*
6399 * There is no help window yet.
6400 * Try to open the file specified by the "helpfile" option.
6401 */
6402 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6403 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006404 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 goto erret;
6406 }
6407 fclose(helpfd);
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006410 * specified, the current window is vertically split and
6411 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006414 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006417 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (curwin->w_height < p_hh)
6420 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 /*
6423 * Open help file (do_ecmd() will set b_help flag, readfile() will
6424 * set b_p_ro flag).
6425 * Set the alternate file to the previously edited file.
6426 */
6427 alt_fnum = curbuf->b_fnum;
6428 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006429 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaar4033c552017-09-16 20:54:51 +02006430 NULL); /* buffer is still open, don't store info */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006431 if (!cmdmod.keepalt)
6432 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 empty_fnum = curbuf->b_fnum;
6434 }
6435 }
6436
6437 if (!p_im)
6438 restart_edit = 0; /* don't want insert mode in help file */
6439
Bram Moolenaar8f535582011-09-30 17:30:31 +02006440#ifdef FEAT_FOLDING
6441 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6442 * It is needed for do_tag top open folds under the cursor. */
6443 KeyTyped = old_KeyTyped;
6444#endif
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (tag != NULL)
6447 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6448
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006449 /* Delete the empty buffer if we're not using it. Careful: autocommands
6450 * may have jumped to another window, check that the buffer is not in a
6451 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6453 {
6454 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006455 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 wipe_buffer(buf, TRUE);
6457 }
6458
6459 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006460 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 curwin->w_alt_fnum = alt_fnum;
6462
6463erret:
6464 vim_free(tag);
6465}
6466
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006467/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006468 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006469 */
6470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006471ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006472{
6473 win_T *win;
6474
6475 FOR_ALL_WINDOWS(win)
6476 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006477 if (bt_help(win->w_buffer))
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006478 {
6479 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006480 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006481 }
6482 }
6483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006485#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6486/*
6487 * In an argument search for a language specifiers in the form "@xx".
6488 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6489 * Returns NULL if not found.
6490 */
6491 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006493{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006494 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006495
6496 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6497 && ASCII_ISALPHA(arg[len - 1]))
6498 {
6499 arg[len - 3] = NUL; /* remove the '@' */
6500 return arg + len - 2;
6501 }
6502 return NULL;
6503}
6504#endif
6505
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506/*
6507 * Return a heuristic indicating how well the given string matches. The
6508 * smaller the number, the better the match. This is the order of priorities,
6509 * from best match to worst match:
6510 * - Match with least alpha-numeric characters is better.
6511 * - Match with least total characters is better.
6512 * - Match towards the start is better.
6513 * - Match starting with "+" is worse (feature instead of command)
6514 * Assumption is made that the matched_string passed has already been found to
6515 * match some string for which help is requested. webb.
6516 */
6517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006518help_heuristic(
6519 char_u *matched_string,
6520 int offset, /* offset for match */
6521 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 int num_letters;
6524 char_u *p;
6525
6526 num_letters = 0;
6527 for (p = matched_string; *p; p++)
6528 if (ASCII_ISALNUM(*p))
6529 num_letters++;
6530
6531 /*
6532 * Multiply the number of letters by 100 to give it a much bigger
6533 * weighting than the number of characters.
6534 * If there only is a match while ignoring case, add 5000.
6535 * If the match starts in the middle of a word, add 10000 to put it
6536 * somewhere in the last half.
6537 * If the match is more than 2 chars from the start, multiply by 200 to
6538 * put it after matches at the start.
6539 */
6540 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6541 && ASCII_ISALNUM(matched_string[offset - 1]))
6542 offset += 10000;
6543 else if (offset > 2)
6544 offset *= 200;
6545 if (wrong_case)
6546 offset += 5000;
6547 /* Features are less interesting than the subjects themselves, but "+"
6548 * alone is not a feature. */
6549 if (matched_string[0] == '+' && matched_string[1] != NUL)
6550 offset += 100;
6551 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6552}
6553
6554/*
6555 * Compare functions for qsort() below, that checks the help heuristics number
6556 * that has been put after the tagname by find_tags().
6557 */
6558 static int
6559#ifdef __BORLANDC__
6560_RTLENTRYF
6561#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006562help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 char *p1;
6565 char *p2;
6566
6567 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6568 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6569 return strcmp(p1, p2);
6570}
6571
6572/*
6573 * Find all help tags matching "arg", sort them and return in matches[], with
6574 * the number of matches in num_matches.
6575 * The matches will be sorted with a "best" match algorithm.
6576 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6577 */
6578 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006579find_help_tags(
6580 char_u *arg,
6581 int *num_matches,
6582 char_u ***matches,
6583 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 char_u *s, *d;
6586 int i;
6587 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006588 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006589 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006590 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6591 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006592 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006593 "[count]", "[quotex]",
6594 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006595 "[pattern]", "\\|", "\\%$",
6596 "s/\\~", "s/\\U", "s/\\L",
6597 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006599 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006600 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006601 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6602 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006603 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006604 "\\[count]", "\\[quotex]",
6605 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006606 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006607 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006608 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006609 static char *(expr_table[]) = {"!=?", "!~?", "<=?", "<?", "==?", "=~?",
6610 ">=?", ">?", "is?", "isnot?"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 int flags;
6612
6613 d = IObuff; /* assume IObuff is long enough! */
6614
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006615 if (STRNICMP(arg, "expr-", 5) == 0)
6616 {
6617 // When the string starting with "expr-" and containing '?' and matches
6618 // the table, it is taken literally. Otherwise '?' is recognized as a
6619 // wildcard.
6620 for (i = (int)(sizeof(expr_table) / sizeof(char *)); --i >= 0; )
6621 if (STRCMP(arg + 5, expr_table[i]) == 0)
6622 {
6623 STRCPY(d, arg);
6624 break;
6625 }
6626 }
6627 else
6628 {
6629 // Recognize a few exceptions to the rule. Some strings that contain
6630 // '*' with "star". Otherwise '*' is recognized as a wildcard.
6631 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
6632 if (STRCMP(arg, mtable[i]) == 0)
6633 {
6634 STRCPY(d, rtable[i]);
6635 break;
6636 }
6637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 if (i < 0) /* no match in table */
6640 {
6641 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6642 * Also replace "\%^" and "\%(", they match every tag too.
6643 * Also "\zs", "\z1", etc.
6644 * Also "\@<", "\@=", "\@<=", etc.
6645 * And also "\_$" and "\_^". */
6646 if (arg[0] == '\\'
6647 && ((arg[1] != NUL && arg[2] == NUL)
6648 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6649 && arg[2] != NUL)))
6650 {
6651 STRCPY(d, "/\\\\");
6652 STRCPY(d + 3, arg + 1);
6653 /* Check for "/\\_$", should be "/\\_\$" */
6654 if (d[3] == '_' && d[4] == '$')
6655 STRCPY(d + 4, "\\$");
6656 }
6657 else
6658 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006659 /* Replace:
6660 * "[:...:]" with "\[:...:]"
6661 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006662 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006663 */
6664 if ((arg[0] == '[' && (arg[1] == ':'
6665 || (arg[1] == '+' && arg[2] == '+')))
6666 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 *d++ = '\\';
6668
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006669 /*
6670 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6671 */
6672 if (*arg == '(' && arg[1] == '\'')
6673 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 for (s = arg; *s; ++s)
6675 {
6676 /*
6677 * Replace "|" with "bar" and '"' with "quote" to match the name of
6678 * the tags for these commands.
6679 * Replace "*" with ".*" and "?" with "." to match command line
6680 * completion.
6681 * Insert a backslash before '~', '$' and '.' to avoid their
6682 * special meaning.
6683 */
6684 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6685 break;
6686 switch (*s)
6687 {
6688 case '|': STRCPY(d, "bar");
6689 d += 3;
6690 continue;
6691 case '"': STRCPY(d, "quote");
6692 d += 5;
6693 continue;
6694 case '*': *d++ = '.';
6695 break;
6696 case '?': *d++ = '.';
6697 continue;
6698 case '$':
6699 case '.':
6700 case '~': *d++ = '\\';
6701 break;
6702 }
6703
6704 /*
6705 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6706 * ":help i_^_CTRL-D" work.
6707 * Insert '-' before and after "CTRL-X" when applicable.
6708 */
6709 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6710 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6711 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006712 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6713 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 STRCPY(d, "CTRL-");
6715 d += 5;
6716 if (*s < ' ')
6717 {
6718#ifdef EBCDIC
6719 *d++ = CtrlChar(*s);
6720#else
6721 *d++ = *s + '@';
6722#endif
6723 if (d[-1] == '\\')
6724 *d++ = '\\'; /* double a backslash */
6725 }
6726 else
6727 *d++ = *++s;
6728 if (s[1] != NUL && s[1] != '_')
6729 *d++ = '_'; /* append a '_' */
6730 continue;
6731 }
6732 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6733 *d++ = '\\';
6734
6735 /*
6736 * Insert a backslash before a backslash after a slash, for search
6737 * pattern tags: "/\|" --> "/\\|".
6738 */
6739 else if (s[0] == '\\' && s[1] != '\\'
6740 && *arg == '/' && s == arg + 1)
6741 *d++ = '\\';
6742
6743 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6744 * "CTRL-\_CTRL-N" */
6745 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6746 {
6747 STRCPY(d, "CTRL-\\\\");
6748 d += 7;
6749 s += 6;
6750 }
6751
6752 *d++ = *s;
6753
6754 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006755 * If tag contains "({" or "([", tag terminates at the "(".
6756 * This is for help on functions, e.g.: abs({expr}).
6757 */
6758 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6759 break;
6760
6761 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 * If tag starts with ', toss everything after a second '. Fixes
6763 * CTRL-] on 'option'. (would include the trailing '.').
6764 */
6765 if (*s == '\'' && s > arg && *arg == '\'')
6766 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006767 /* Also '{' and '}'. */
6768 if (*s == '}' && s > arg && *arg == '{')
6769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006772
6773 if (*IObuff == '`')
6774 {
6775 if (d > IObuff + 2 && d[-1] == '`')
6776 {
6777 /* remove the backticks from `command` */
6778 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6779 d[-2] = NUL;
6780 }
6781 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6782 {
6783 /* remove the backticks and comma from `command`, */
6784 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6785 d[-3] = NUL;
6786 }
6787 else if (d > IObuff + 4 && d[-3] == '`'
6788 && d[-2] == '\\' && d[-1] == '.')
6789 {
6790 /* remove the backticks and dot from `command`\. */
6791 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6792 d[-4] = NUL;
6793 }
6794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 }
6796 }
6797
6798 *matches = (char_u **)"";
6799 *num_matches = 0;
6800 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6801 if (keep_lang)
6802 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006803 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 /* Sort the matches found on the heuristic number that is after the
6807 * tag name. */
6808 qsort((void *)*matches, (size_t)*num_matches,
6809 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006810 /* Delete more than TAG_MANY to reduce the size of the listing. */
6811 while (*num_matches > TAG_MANY)
6812 vim_free((*matches)[--*num_matches]);
6813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 return OK;
6815}
6816
6817/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006818 * Called when starting to edit a buffer for a help file.
6819 */
6820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006821prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006822{
6823 char_u *p;
6824
6825 curbuf->b_help = TRUE;
6826#ifdef FEAT_QUICKFIX
6827 set_string_option_direct((char_u *)"buftype", -1,
6828 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6829#endif
6830
6831 /*
6832 * Always set these options after jumping to a help tag, because the
6833 * user may have an autocommand that gets in the way.
6834 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6835 * latin1 word characters (for translated help files).
6836 * Only set it when needed, buf_init_chartab() is some work.
6837 */
6838 p =
6839#ifdef EBCDIC
6840 (char_u *)"65-255,^*,^|,^\"";
6841#else
6842 (char_u *)"!-~,^*,^|,^\",192-255";
6843#endif
6844 if (STRCMP(curbuf->b_p_isk, p) != 0)
6845 {
6846 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6847 check_buf_options(curbuf);
6848 (void)buf_init_chartab(curbuf, FALSE);
6849 }
6850
Bram Moolenaar0b105412014-11-30 13:34:23 +01006851#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006852 /* Don't use the global foldmethod.*/
6853 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6854 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006855#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006856
6857 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6858 curwin->w_p_list = FALSE; /* no list mode */
6859
6860 curbuf->b_p_ma = FALSE; /* not modifiable */
6861 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6862 curwin->w_p_nu = 0; /* no line numbers */
6863 curwin->w_p_rnu = 0; /* no relative line numbers */
6864 RESET_BINDING(curwin); /* no scroll or cursor binding */
6865#ifdef FEAT_ARABIC
6866 curwin->w_p_arab = FALSE; /* no arabic mode */
6867#endif
6868#ifdef FEAT_RIGHTLEFT
6869 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6870#endif
6871#ifdef FEAT_FOLDING
6872 curwin->w_p_fen = FALSE; /* No folding in the help window */
6873#endif
6874#ifdef FEAT_DIFF
6875 curwin->w_p_diff = FALSE; /* No 'diff' */
6876#endif
6877#ifdef FEAT_SPELL
6878 curwin->w_p_spell = FALSE; /* No spell checking */
6879#endif
6880
6881 set_buflisted(FALSE);
6882}
6883
6884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 * After reading a help file: May cleanup a help buffer when syntax
6886 * highlighting is not used.
6887 */
6888 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006889fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 linenr_T lnum;
6892 char_u *line;
6893 int in_example = FALSE;
6894 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006895 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 char_u *p;
6897 char_u *rt;
6898 int mustfree;
6899
Bram Moolenaar90492982017-06-22 14:16:31 +02006900 /* Set filetype to "help" if still needed. */
6901 if (STRCMP(curbuf->b_p_ft, "help") != 0)
Bram Moolenaar18141832017-06-25 21:17:25 +02006902 {
6903 ++curbuf_lock;
Bram Moolenaar90492982017-06-22 14:16:31 +02006904 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
Bram Moolenaar18141832017-06-25 21:17:25 +02006905 --curbuf_lock;
6906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006909 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910#endif
6911 {
6912 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6913 {
6914 line = ml_get_buf(curbuf, lnum, FALSE);
6915 len = (int)STRLEN(line);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006916 if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
6918 /* End of example: non-white or '<' in first column. */
6919 if (line[0] == '<')
6920 {
6921 /* blank-out a '<' in the first column */
6922 line = ml_get_buf(curbuf, lnum, TRUE);
6923 line[0] = ' ';
6924 }
6925 in_example = FALSE;
6926 }
6927 if (!in_example && len > 0)
6928 {
6929 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6930 {
6931 /* blank-out a '>' in the last column (start of example) */
6932 line = ml_get_buf(curbuf, lnum, TRUE);
6933 line[len - 1] = ' ';
6934 in_example = TRUE;
6935 }
6936 else if (line[len - 1] == '~')
6937 {
6938 /* blank-out a '~' at the end of line (header marker) */
6939 line = ml_get_buf(curbuf, lnum, TRUE);
6940 line[len - 1] = ' ';
6941 }
6942 }
6943 }
6944 }
6945
6946 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006947 * In the "help.txt" and "help.abx" file, add the locally added help
6948 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006950 fname = gettail(curbuf->b_fname);
6951 if (fnamecmp(fname, "help.txt") == 0
6952#ifdef FEAT_MULTI_LANG
6953 || (fnamencmp(fname, "help.", 5) == 0
6954 && ASCII_ISALPHA(fname[5])
6955 && ASCII_ISALPHA(fname[6])
6956 && TOLOWER_ASC(fname[7]) == 'x'
6957 && fname[8] == NUL)
6958#endif
6959 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
6961 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6962 {
6963 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006964 if (strstr((char *)line, "*local-additions*") == NULL)
6965 continue;
6966
6967 /* Go through all directories in 'runtimepath', skipping
6968 * $VIMRUNTIME. */
6969 p = p_rtp;
6970 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006972 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6973 mustfree = FALSE;
6974 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
Bram Moolenaare4db7ae2018-02-13 13:12:11 +01006975 if (rt != NULL && fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006977 int fcount;
6978 char_u **fnames;
6979 FILE *fd;
6980 char_u *s;
6981 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006983 vimconv_T vc;
6984 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985#endif
6986
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006987 /* Find all "doc/ *.txt" files in this directory. */
6988 add_pathsep(NameBuff);
6989#ifdef FEAT_MULTI_LANG
6990 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006992 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006994 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6995 &fnames, EW_FILE|EW_SILENT) == OK
6996 && fcount > 0)
6997 {
6998#ifdef FEAT_MULTI_LANG
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006999 int i1, i2;
7000 char_u *f1, *f2;
7001 char_u *t1, *t2;
7002 char_u *e1, *e2;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007003
7004 /* If foo.abx is found use it instead of foo.txt in
7005 * the same directory. */
7006 for (i1 = 0; i1 < fcount; ++i1)
7007 {
7008 for (i2 = 0; i2 < fcount; ++i2)
7009 {
7010 if (i1 == i2)
7011 continue;
7012 if (fnames[i1] == NULL || fnames[i2] == NULL)
7013 continue;
7014 f1 = fnames[i1];
7015 f2 = fnames[i2];
7016 t1 = gettail(f1);
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007017 t2 = gettail(f2);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007018 e1 = vim_strrchr(t1, '.');
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007019 e2 = vim_strrchr(t2, '.');
Bram Moolenaar7756e742016-10-21 20:35:37 +02007020 if (e1 == NULL || e2 == NULL)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007021 continue;
7022 if (fnamecmp(e1, ".txt") != 0
7023 && fnamecmp(e1, fname + 4) != 0)
7024 {
7025 /* Not .txt and not .abx, remove it. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007026 VIM_CLEAR(fnames[i1]);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007027 continue;
7028 }
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007029 if (e1 - f1 != e2 - f2
7030 || fnamencmp(f1, f2, e1 - f1) != 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007031 continue;
7032 if (fnamecmp(e1, ".txt") == 0
7033 && fnamecmp(e2, fname + 4) == 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007034 /* use .abx instead of .txt */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007035 VIM_CLEAR(fnames[i1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007038#endif
7039 for (fi = 0; fi < fcount; ++fi)
7040 {
7041 if (fnames[fi] == NULL)
7042 continue;
7043 fd = mch_fopen((char *)fnames[fi], "r");
7044 if (fd != NULL)
7045 {
7046 vim_fgets(IObuff, IOSIZE, fd);
7047 if (IObuff[0] == '*'
7048 && (s = vim_strchr(IObuff + 1, '*'))
7049 != NULL)
7050 {
7051#ifdef FEAT_MBYTE
7052 int this_utf = MAYBE;
7053#endif
7054 /* Change tag definition to a
7055 * reference and remove <CR>/<NL>. */
7056 IObuff[0] = '|';
7057 *s = '|';
7058 while (*s != NUL)
7059 {
7060 if (*s == '\r' || *s == '\n')
7061 *s = NUL;
7062#ifdef FEAT_MBYTE
7063 /* The text is utf-8 when a byte
7064 * above 127 is found and no
7065 * illegal byte sequence is found.
7066 */
7067 if (*s >= 0x80 && this_utf != FALSE)
7068 {
7069 int l;
7070
7071 this_utf = TRUE;
7072 l = utf_ptr2len(s);
7073 if (l == 1)
7074 this_utf = FALSE;
7075 s += l - 1;
7076 }
7077#endif
7078 ++s;
7079 }
7080#ifdef FEAT_MBYTE
7081 /* The help file is latin1 or utf-8;
7082 * conversion to the current
7083 * 'encoding' may be required. */
7084 vc.vc_type = CONV_NONE;
7085 convert_setup(&vc, (char_u *)(
7086 this_utf == TRUE ? "utf-8"
7087 : "latin1"), p_enc);
7088 if (vc.vc_type == CONV_NONE)
7089 /* No conversion needed. */
7090 cp = IObuff;
7091 else
7092 {
7093 /* Do the conversion. If it fails
7094 * use the unconverted text. */
7095 cp = string_convert(&vc, IObuff,
7096 NULL);
7097 if (cp == NULL)
7098 cp = IObuff;
7099 }
7100 convert_setup(&vc, NULL, NULL);
7101
7102 ml_append(lnum, cp, (colnr_T)0, FALSE);
7103 if (cp != IObuff)
7104 vim_free(cp);
7105#else
7106 ml_append(lnum, IObuff, (colnr_T)0,
7107 FALSE);
7108#endif
7109 ++lnum;
7110 }
7111 fclose(fd);
7112 }
7113 }
7114 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007117 if (mustfree)
7118 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122 }
7123}
7124
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007125/*
7126 * ":exusage"
7127 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007128 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007130{
7131 do_cmdline_cmd((char_u *)"help ex-cmd-index");
7132}
7133
7134/*
7135 * ":viusage"
7136 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007137 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007138ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007139{
7140 do_cmdline_cmd((char_u *)"help normal-index");
7141}
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007144 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147helptags_one(
7148 char_u *dir, /* doc directory */
7149 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
7150 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
7151 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
7153 FILE *fd_tags;
7154 FILE *fd;
7155 garray_T ga;
7156 int filecount;
7157 char_u **files;
7158 char_u *p1, *p2;
7159 int fi;
7160 char_u *s;
7161 int i;
7162 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007163 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164# ifdef FEAT_MBYTE
7165 int utf8 = MAYBE;
7166 int this_utf8;
7167 int firstline;
7168 int mix = FALSE; /* detected mixed encodings */
7169# endif
7170
7171 /*
7172 * Find all *.txt files.
7173 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007174 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007176 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 STRCAT(NameBuff, ext);
7178 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7179 EW_FILE|EW_SILENT) == FAIL
7180 || filecount == 0)
7181 {
7182 if (!got_int)
Bram Moolenaar5b302912016-08-24 22:11:55 +02007183 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 return;
7185 }
7186
7187 /*
7188 * Open the tags file for writing.
7189 * Do this before scanning through all the files.
7190 */
7191 STRCPY(NameBuff, dir);
7192 add_pathsep(NameBuff);
7193 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007194 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (fd_tags == NULL)
7196 {
7197 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
7198 FreeWild(filecount, files);
7199 return;
7200 }
7201
7202 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007203 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7204 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 */
7206 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007207 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
7208 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {
7210 if (ga_grow(&ga, 1) == FAIL)
7211 got_int = TRUE;
7212 else
7213 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007214 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 if (s == NULL)
7216 got_int = TRUE;
7217 else
7218 {
7219 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7220 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7221 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223 }
7224 }
7225
7226 /*
7227 * Go over all the files and extract the tags.
7228 */
7229 for (fi = 0; fi < filecount && !got_int; ++fi)
7230 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007231 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 if (fd == NULL)
7233 {
7234 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
7235 continue;
7236 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007237 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239# ifdef FEAT_MBYTE
7240 firstline = TRUE;
7241# endif
7242 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7243 {
7244# ifdef FEAT_MBYTE
7245 if (firstline)
7246 {
7247 /* Detect utf-8 file by a non-ASCII char in the first line. */
7248 this_utf8 = MAYBE;
7249 for (s = IObuff; *s != NUL; ++s)
7250 if (*s >= 0x80)
7251 {
7252 int l;
7253
7254 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007255 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (l == 1)
7257 {
7258 /* Illegal UTF-8 byte sequence. */
7259 this_utf8 = FALSE;
7260 break;
7261 }
7262 s += l - 1;
7263 }
7264 if (this_utf8 == MAYBE) /* only ASCII characters found */
7265 this_utf8 = FALSE;
7266 if (utf8 == MAYBE) /* first file */
7267 utf8 = this_utf8;
7268 else if (utf8 != this_utf8)
7269 {
7270 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
7271 mix = !got_int;
7272 got_int = TRUE;
7273 }
7274 firstline = FALSE;
7275 }
7276# endif
7277 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7278 while (p1 != NULL)
7279 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007280 /* Use vim_strbyte() instead of vim_strchr() so that when
7281 * 'encoding' is dbcs it still works, don't find '*' in the
7282 * second byte. */
7283 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7285 {
7286 for (s = p1 + 1; s < p2; ++s)
7287 if (*s == ' ' || *s == '\t' || *s == '|')
7288 break;
7289
7290 /*
7291 * Only accept a *tag* when it consists of valid
7292 * characters, there is white space before it and is
7293 * followed by a white character or end-of-line.
7294 */
7295 if (s == p2
7296 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7297 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7298 || s[1] == '\0'))
7299 {
7300 *p2 = '\0';
7301 ++p1;
7302 if (ga_grow(&ga, 1) == FAIL)
7303 {
7304 got_int = TRUE;
7305 break;
7306 }
7307 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7308 if (s == NULL)
7309 {
7310 got_int = TRUE;
7311 break;
7312 }
7313 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7314 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 sprintf((char *)s, "%s\t%s", p1, fname);
7316
7317 /* find next '*' */
7318 p2 = vim_strchr(p2 + 1, '*');
7319 }
7320 }
7321 p1 = p2;
7322 }
7323 line_breakcheck();
7324 }
7325
7326 fclose(fd);
7327 }
7328
7329 FreeWild(filecount, files);
7330
7331 if (!got_int)
7332 {
7333 /*
7334 * Sort the tags.
7335 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007336 if (ga.ga_data != NULL)
7337 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
7339 /*
7340 * Check for duplicates.
7341 */
7342 for (i = 1; i < ga.ga_len; ++i)
7343 {
7344 p1 = ((char_u **)ga.ga_data)[i - 1];
7345 p2 = ((char_u **)ga.ga_data)[i];
7346 while (*p1 == *p2)
7347 {
7348 if (*p2 == '\t')
7349 {
7350 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007351 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7353 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
7354 EMSG(NameBuff);
7355 *p2 = '\t';
7356 break;
7357 }
7358 ++p1;
7359 ++p2;
7360 }
7361 }
7362
7363# ifdef FEAT_MBYTE
7364 if (utf8 == TRUE)
7365 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
7366# endif
7367
7368 /*
7369 * Write the tags into the file.
7370 */
7371 for (i = 0; i < ga.ga_len; ++i)
7372 {
7373 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007374 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007376 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 else
7378 {
7379 fprintf(fd_tags, "%s\t/*", s);
7380 for (p1 = s; *p1 != '\t'; ++p1)
7381 {
7382 /* insert backslash before '\\' and '/' */
7383 if (*p1 == '\\' || *p1 == '/')
7384 putc('\\', fd_tags);
7385 putc(*p1, fd_tags);
7386 }
7387 fprintf(fd_tags, "*\n");
7388 }
7389 }
7390 }
7391#ifdef FEAT_MBYTE
7392 if (mix)
7393 got_int = FALSE; /* continue with other languages */
7394#endif
7395
7396 for (i = 0; i < ga.ga_len; ++i)
7397 vim_free(((char_u **)ga.ga_data)[i]);
7398 ga_clear(&ga);
7399 fclose(fd_tags); /* there is no check for an error... */
7400}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007402/*
7403 * Generate tags in one help directory, taking care of translations.
7404 */
7405 static void
7406do_helptags(char_u *dirname, int add_help_tags)
7407{
7408#ifdef FEAT_MULTI_LANG
7409 int len;
7410 int i, j;
7411 garray_T ga;
7412 char_u lang[2];
7413 char_u ext[5];
7414 char_u fname[8];
7415 int filecount;
7416 char_u **files;
7417
7418 /* Get a list of all files in the help directory and in subdirectories. */
7419 STRCPY(NameBuff, dirname);
7420 add_pathsep(NameBuff);
7421 STRCAT(NameBuff, "**");
7422 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7423 EW_FILE|EW_SILENT) == FAIL
7424 || filecount == 0)
7425 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007426 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007427 return;
7428 }
7429
7430 /* Go over all files in the directory to find out what languages are
7431 * present. */
7432 ga_init2(&ga, 1, 10);
7433 for (i = 0; i < filecount; ++i)
7434 {
7435 len = (int)STRLEN(files[i]);
7436 if (len > 4)
7437 {
7438 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7439 {
7440 /* ".txt" -> language "en" */
7441 lang[0] = 'e';
7442 lang[1] = 'n';
7443 }
7444 else if (files[i][len - 4] == '.'
7445 && ASCII_ISALPHA(files[i][len - 3])
7446 && ASCII_ISALPHA(files[i][len - 2])
7447 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7448 {
7449 /* ".abx" -> language "ab" */
7450 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7451 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7452 }
7453 else
7454 continue;
7455
7456 /* Did we find this language already? */
7457 for (j = 0; j < ga.ga_len; j += 2)
7458 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7459 break;
7460 if (j == ga.ga_len)
7461 {
7462 /* New language, add it. */
7463 if (ga_grow(&ga, 2) == FAIL)
7464 break;
7465 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7466 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7467 }
7468 }
7469 }
7470
7471 /*
7472 * Loop over the found languages to generate a tags file for each one.
7473 */
7474 for (j = 0; j < ga.ga_len; j += 2)
7475 {
7476 STRCPY(fname, "tags-xx");
7477 fname[5] = ((char_u *)ga.ga_data)[j];
7478 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7479 if (fname[5] == 'e' && fname[6] == 'n')
7480 {
7481 /* English is an exception: use ".txt" and "tags". */
7482 fname[4] = NUL;
7483 STRCPY(ext, ".txt");
7484 }
7485 else
7486 {
7487 /* Language "ab" uses ".abx" and "tags-ab". */
7488 STRCPY(ext, ".xxx");
7489 ext[1] = fname[5];
7490 ext[2] = fname[6];
7491 }
7492 helptags_one(dirname, ext, fname, add_help_tags);
7493 }
7494
7495 ga_clear(&ga);
7496 FreeWild(filecount, files);
7497
7498#else
7499 /* No language support, just use "*.txt" and "tags". */
7500 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7501#endif
7502}
7503
7504 static void
7505helptags_cb(char_u *fname, void *cookie)
7506{
7507 do_helptags(fname, *(int *)cookie);
7508}
7509
7510/*
7511 * ":helptags"
7512 */
7513 void
7514ex_helptags(exarg_T *eap)
7515{
7516 expand_T xpc;
7517 char_u *dirname;
7518 int add_help_tags = FALSE;
7519
7520 /* Check for ":helptags ++t {dir}". */
Bram Moolenaar1c465442017-03-12 20:10:05 +01007521 if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3]))
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007522 {
7523 add_help_tags = TRUE;
7524 eap->arg = skipwhite(eap->arg + 3);
7525 }
7526
7527 if (STRCMP(eap->arg, "ALL") == 0)
7528 {
7529 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7530 helptags_cb, &add_help_tags);
7531 }
7532 else
7533 {
7534 ExpandInit(&xpc);
7535 xpc.xp_context = EXPAND_DIRECTORIES;
7536 dirname = ExpandOne(&xpc, eap->arg, NULL,
7537 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7538 if (dirname == NULL || !mch_isdir(dirname))
7539 EMSG2(_("E150: Not a directory: %s"), eap->arg);
7540 else
7541 do_helptags(dirname, add_help_tags);
7542 vim_free(dirname);
7543 }
7544}
7545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546#if defined(FEAT_SIGNS) || defined(PROTO)
7547
7548/*
7549 * Struct to hold the sign properties.
7550 */
7551typedef struct sign sign_T;
7552
7553struct sign
7554{
7555 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007556 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 char_u *sn_name; /* name of sign */
7558 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007559# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 char_u *sn_text; /* text used instead of pixmap */
7563 int sn_line_hl; /* highlight ID for line */
7564 int sn_text_hl; /* highlight ID for text */
7565};
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007568static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007570static void sign_list_defined(sign_T *sp);
7571static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007573static char *cmds[] = {
7574 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007575# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007576 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007577# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007578 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007579# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007580 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007581# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007582 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007583# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007584 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007585# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007586 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007587# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007588};
7589
7590/*
7591 * Find index of a ":sign" subcmd from its name.
7592 * "*end_cmd" must be writable.
7593 */
7594 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007595sign_cmd_idx(
7596 char_u *begin_cmd, /* begin of sign subcmd */
7597 char_u *end_cmd) /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007598{
7599 int idx;
7600 char save = *end_cmd;
7601
7602 *end_cmd = NUL;
7603 for (idx = 0; ; ++idx)
7604 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7605 break;
7606 *end_cmd = save;
7607 return idx;
7608}
7609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610/*
7611 * ":sign" command
7612 */
7613 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007614ex_sign(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 char_u *arg = eap->arg;
7617 char_u *p;
7618 int idx;
7619 sign_T *sp;
7620 sign_T *sp_prev;
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01007621 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622
7623 /* Parse the subcommand. */
7624 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007625 idx = sign_cmd_idx(arg, p);
7626 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007628 EMSG2(_("E160: Unknown sign command: %s"), arg);
7629 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631 arg = skipwhite(p);
7632
7633 if (idx <= SIGNCMD_LIST)
7634 {
7635 /*
7636 * Define, undefine or list signs.
7637 */
7638 if (idx == SIGNCMD_LIST && *arg == NUL)
7639 {
7640 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007641 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 sign_list_defined(sp);
7643 }
7644 else if (*arg == NUL)
7645 EMSG(_("E156: Missing sign name"));
7646 else
7647 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007648 /* Isolate the sign name. If it's a number skip leading zeroes,
7649 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 p = skiptowhite(arg);
7651 if (*p != NUL)
7652 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007653 while (arg[0] == '0' && arg[1] != NUL)
7654 ++arg;
7655
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 sp_prev = NULL;
7657 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7658 {
7659 if (STRCMP(sp->sn_name, arg) == 0)
7660 break;
7661 sp_prev = sp;
7662 }
7663 if (idx == SIGNCMD_DEFINE)
7664 {
7665 /* ":sign define {name} ...": define a sign */
7666 if (sp == NULL)
7667 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007668 sign_T *lp;
7669 int start = next_sign_typenr;
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 /* Allocate a new sign. */
7672 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7673 if (sp == NULL)
7674 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007676 /* Check that next_sign_typenr is not already being used.
7677 * This only happens after wrapping around. Hopefully
7678 * another one got deleted and we can use its number. */
7679 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007681 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007683 ++next_sign_typenr;
7684 if (next_sign_typenr == MAX_TYPENR)
7685 next_sign_typenr = 1;
7686 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007688 vim_free(sp);
7689 EMSG(_("E612: Too many signs defined"));
7690 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007692 lp = first_sign; /* start all over */
7693 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007695 lp = lp->sn_next;
7696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007698 sp->sn_typenr = next_sign_typenr;
7699 if (++next_sign_typenr == MAX_TYPENR)
7700 next_sign_typenr = 1; /* wrap around */
7701
7702 sp->sn_name = vim_strsave(arg);
7703 if (sp->sn_name == NULL) /* out of memory */
7704 {
7705 vim_free(sp);
7706 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007708
7709 /* add the new sign to the list of signs */
7710 if (sp_prev == NULL)
7711 first_sign = sp;
7712 else
7713 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715
7716 /* set values for a defined sign. */
7717 for (;;)
7718 {
7719 arg = skipwhite(p);
7720 if (*arg == NUL)
7721 break;
7722 p = skiptowhite_esc(arg);
7723 if (STRNCMP(arg, "icon=", 5) == 0)
7724 {
7725 arg += 5;
7726 vim_free(sp->sn_icon);
7727 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7728 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007729# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 if (gui.in_use)
7731 {
7732 out_flush();
7733 if (sp->sn_image != NULL)
7734 gui_mch_destroy_sign(sp->sn_image);
7735 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7736 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739 else if (STRNCMP(arg, "text=", 5) == 0)
7740 {
7741 char_u *s;
7742 int cells;
7743 int len;
7744
7745 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007746# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 /* Count cells and check for non-printable chars */
7748 if (has_mbyte)
7749 {
7750 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007751 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
7753 if (!vim_isprintc((*mb_ptr2char)(s)))
7754 break;
7755 cells += (*mb_ptr2cells)(s);
7756 }
7757 }
7758 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
7761 for (s = arg; s < p; ++s)
7762 if (!vim_isprintc(*s))
7763 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007764 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766 /* Currently must be one or two display cells */
7767 if (s != p || cells < 1 || cells > 2)
7768 {
7769 *p = NUL;
7770 EMSG2(_("E239: Invalid sign text: %s"), arg);
7771 return;
7772 }
7773
7774 vim_free(sp->sn_text);
7775 /* Allocate one byte more if we need to pad up
7776 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007777 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 sp->sn_text = vim_strnsave(arg, len);
7779
7780 if (sp->sn_text != NULL && cells == 1)
7781 STRCPY(sp->sn_text + len - 1, " ");
7782 }
7783 else if (STRNCMP(arg, "linehl=", 7) == 0)
7784 {
7785 arg += 7;
7786 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7787 }
7788 else if (STRNCMP(arg, "texthl=", 7) == 0)
7789 {
7790 arg += 7;
7791 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7792 }
7793 else
7794 {
7795 EMSG2(_(e_invarg2), arg);
7796 return;
7797 }
7798 }
7799 }
7800 else if (sp == NULL)
7801 EMSG2(_("E155: Unknown sign: %s"), arg);
7802 else if (idx == SIGNCMD_LIST)
7803 /* ":sign list {name}" */
7804 sign_list_defined(sp);
7805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007807 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 }
7810 else
7811 {
7812 int id = -1;
7813 linenr_T lnum = -1;
7814 char_u *sign_name = NULL;
7815 char_u *arg1;
7816
7817 if (*arg == NUL)
7818 {
7819 if (idx == SIGNCMD_PLACE)
7820 {
7821 /* ":sign place": list placed signs in all buffers */
7822 sign_list_placed(NULL);
7823 }
7824 else if (idx == SIGNCMD_UNPLACE)
7825 {
7826 /* ":sign unplace": remove placed sign at cursor */
7827 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7828 if (id > 0)
7829 {
7830 buf_delsign(curwin->w_buffer, id);
7831 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7832 }
7833 else
7834 EMSG(_("E159: Missing sign number"));
7835 }
7836 else
7837 EMSG(_(e_argreq));
7838 return;
7839 }
7840
7841 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7842 {
7843 /* ":sign unplace *": remove all placed signs */
7844 buf_delete_all_signs();
7845 return;
7846 }
7847
7848 /* first arg could be placed sign id */
7849 arg1 = arg;
7850 if (VIM_ISDIGIT(*arg))
7851 {
7852 id = getdigits(&arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007853 if (!VIM_ISWHITE(*arg) && *arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
7855 id = -1;
7856 arg = arg1;
7857 }
7858 else
7859 {
7860 arg = skipwhite(arg);
7861 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7862 {
7863 /* ":sign unplace {id}": remove placed sign by number */
Bram Moolenaar29323592016-07-24 22:04:11 +02007864 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 if ((lnum = buf_delsign(buf, id)) != 0)
7866 update_debug_sign(buf, lnum);
7867 return;
7868 }
7869 }
7870 }
7871
7872 /*
7873 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7874 * Leave "arg" pointing to {fname}.
7875 */
7876 for (;;)
7877 {
7878 if (STRNCMP(arg, "line=", 5) == 0)
7879 {
7880 arg += 5;
7881 lnum = atoi((char *)arg);
7882 arg = skiptowhite(arg);
7883 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007884 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7885 {
7886 if (id != -1)
7887 {
7888 EMSG(_(e_invarg));
7889 return;
7890 }
7891 id = -2;
7892 arg = skiptowhite(arg + 1);
7893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 else if (STRNCMP(arg, "name=", 5) == 0)
7895 {
7896 arg += 5;
7897 sign_name = arg;
7898 arg = skiptowhite(arg);
7899 if (*arg != NUL)
7900 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007901 while (sign_name[0] == '0' && sign_name[1] != NUL)
7902 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 else if (STRNCMP(arg, "file=", 5) == 0)
7905 {
7906 arg += 5;
7907 buf = buflist_findname(arg);
7908 break;
7909 }
7910 else if (STRNCMP(arg, "buffer=", 7) == 0)
7911 {
7912 arg += 7;
7913 buf = buflist_findnr((int)getdigits(&arg));
7914 if (*skipwhite(arg) != NUL)
7915 EMSG(_(e_trailing));
7916 break;
7917 }
7918 else
7919 {
7920 EMSG(_(e_invarg));
7921 return;
7922 }
7923 arg = skipwhite(arg);
7924 }
7925
7926 if (buf == NULL)
7927 {
7928 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7929 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007930 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
7932 if (lnum >= 0 || sign_name != NULL)
7933 EMSG(_(e_invarg));
7934 else
7935 /* ":sign place file={fname}": list placed signs in one file */
7936 sign_list_placed(buf);
7937 }
7938 else if (idx == SIGNCMD_JUMP)
7939 {
7940 /* ":sign jump {id} file={fname}" */
7941 if (lnum >= 0 || sign_name != NULL)
7942 EMSG(_(e_invarg));
7943 else if ((lnum = buf_findsign(buf, id)) > 0)
7944 { /* goto a sign ... */
7945 if (buf_jump_open_win(buf) != NULL)
7946 { /* ... in a current window */
7947 curwin->w_cursor.lnum = lnum;
7948 check_cursor_lnum();
7949 beginline(BL_WHITE);
7950 }
7951 else
7952 { /* ... not currently in a window */
7953 char_u *cmd;
7954
Bram Moolenaarbfd096d2016-08-17 22:29:09 +02007955 if (buf->b_fname == NULL)
7956 {
7957 EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
7958 return;
7959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7961 if (cmd == NULL)
7962 return;
7963 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7964 do_cmdline_cmd(cmd);
7965 vim_free(cmd);
7966 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007967# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 }
7971 else
7972 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7973 }
7974 else if (idx == SIGNCMD_UNPLACE)
7975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (lnum >= 0 || sign_name != NULL)
7977 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007978 else if (id == -2)
7979 {
7980 /* ":sign unplace * file={fname}" */
7981 redraw_buf_later(buf, NOT_VALID);
7982 buf_delete_signs(buf);
7983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 else
7985 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007986 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 lnum = buf_delsign(buf, id);
7988 update_debug_sign(buf, lnum);
7989 }
7990 }
7991 /* idx == SIGNCMD_PLACE */
7992 else if (sign_name != NULL)
7993 {
7994 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7995 if (STRCMP(sp->sn_name, sign_name) == 0)
7996 break;
7997 if (sp == NULL)
7998 {
7999 EMSG2(_("E155: Unknown sign: %s"), sign_name);
8000 return;
8001 }
8002 if (lnum > 0)
8003 /* ":sign place {id} line={lnum} name={name} file={fname}":
8004 * place a sign */
8005 buf_addsign(buf, id, lnum, sp->sn_typenr);
8006 else
8007 /* ":sign place {id} file={fname}": change sign type */
8008 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02008009 if (lnum > 0)
8010 update_debug_sign(buf, lnum);
8011 else
8012 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 }
8014 else
8015 EMSG(_(e_invarg));
8016 }
8017}
8018
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008019# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020/*
8021 * Allocate the icons. Called when the GUI has started. Allows defining
8022 * signs before it starts.
8023 */
8024 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008025sign_gui_started(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 sign_T *sp;
8028
8029 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8030 if (sp->sn_icon != NULL)
8031 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
8032}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
8035/*
8036 * List one sign.
8037 */
8038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008039sign_list_defined(sign_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 char_u *p;
8042
Bram Moolenaar555b2802005-05-19 21:08:39 +00008043 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (sp->sn_icon != NULL)
8045 {
8046 MSG_PUTS(" icon=");
8047 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008048# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 if (sp->sn_image == NULL)
8050 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008051# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008053# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055 if (sp->sn_text != NULL)
8056 {
8057 MSG_PUTS(" text=");
8058 msg_outtrans(sp->sn_text);
8059 }
8060 if (sp->sn_line_hl > 0)
8061 {
8062 MSG_PUTS(" linehl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008063 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 if (p == NULL)
8065 MSG_PUTS("NONE");
8066 else
8067 msg_puts(p);
8068 }
8069 if (sp->sn_text_hl > 0)
8070 {
8071 MSG_PUTS(" texthl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008072 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 if (p == NULL)
8074 MSG_PUTS("NONE");
8075 else
8076 msg_puts(p);
8077 }
8078}
8079
8080/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008081 * Undefine a sign and free its memory.
8082 */
8083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084sign_undefine(sign_T *sp, sign_T *sp_prev)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008085{
8086 vim_free(sp->sn_name);
8087 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008088# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008089 if (sp->sn_image != NULL)
8090 {
8091 out_flush();
8092 gui_mch_destroy_sign(sp->sn_image);
8093 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008094# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008095 vim_free(sp->sn_text);
8096 if (sp_prev == NULL)
8097 first_sign = sp->sn_next;
8098 else
8099 sp_prev->sn_next = sp->sn_next;
8100 vim_free(sp);
8101}
8102
8103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 * Get highlighting attribute for sign "typenr".
8105 * If "line" is TRUE: line highl, if FALSE: text highl.
8106 */
8107 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008108sign_get_attr(int typenr, int line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 sign_T *sp;
8111
8112 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8113 if (sp->sn_typenr == typenr)
8114 {
8115 if (line)
8116 {
8117 if (sp->sn_line_hl > 0)
8118 return syn_id2attr(sp->sn_line_hl);
8119 }
8120 else
8121 {
8122 if (sp->sn_text_hl > 0)
8123 return syn_id2attr(sp->sn_text_hl);
8124 }
8125 break;
8126 }
8127 return 0;
8128}
8129
8130/*
8131 * Get text mark for sign "typenr".
8132 * Returns NULL if there isn't one.
8133 */
8134 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008135sign_get_text(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 sign_T *sp;
8138
8139 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8140 if (sp->sn_typenr == typenr)
8141 return sp->sn_text;
8142 return NULL;
8143}
8144
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008145# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008147sign_get_image(
8148 int typenr) /* the attribute which may have a sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
8150 sign_T *sp;
8151
8152 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8153 if (sp->sn_typenr == typenr)
8154 return sp->sn_image;
8155 return NULL;
8156}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
8159/*
8160 * Get the name of a sign by its typenr.
8161 */
8162 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008163sign_typenr2name(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164{
8165 sign_T *sp;
8166
8167 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8168 if (sp->sn_typenr == typenr)
8169 return sp->sn_name;
8170 return (char_u *)_("[Deleted]");
8171}
8172
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008173# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008174/*
8175 * Undefine/free all signs.
8176 */
8177 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008178free_signs(void)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008179{
8180 while (first_sign != NULL)
8181 sign_undefine(first_sign, NULL);
8182}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008183# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008184
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008185# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008186static enum
8187{
8188 EXP_SUBCMD, /* expand :sign sub-commands */
8189 EXP_DEFINE, /* expand :sign define {name} args */
8190 EXP_PLACE, /* expand :sign place {id} args */
8191 EXP_UNPLACE, /* expand :sign unplace" */
8192 EXP_SIGN_NAMES /* expand with name of placed signs */
8193} expand_what;
8194
8195/*
8196 * Function given to ExpandGeneric() to obtain the sign command
8197 * expansion.
8198 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008199 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200get_sign_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008201{
8202 sign_T *sp;
8203 int current_idx;
8204
8205 switch (expand_what)
8206 {
8207 case EXP_SUBCMD:
8208 return (char_u *)cmds[idx];
8209 case EXP_DEFINE:
8210 {
8211 char *define_arg[] =
8212 {
8213 "icon=", "linehl=", "text=", "texthl=", NULL
8214 };
8215 return (char_u *)define_arg[idx];
8216 }
8217 case EXP_PLACE:
8218 {
8219 char *place_arg[] =
8220 {
8221 "line=", "name=", "file=", "buffer=", NULL
8222 };
8223 return (char_u *)place_arg[idx];
8224 }
8225 case EXP_UNPLACE:
8226 {
8227 char *unplace_arg[] = { "file=", "buffer=", NULL };
8228 return (char_u *)unplace_arg[idx];
8229 }
8230 case EXP_SIGN_NAMES:
8231 /* Complete with name of signs already defined */
8232 current_idx = 0;
8233 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8234 if (current_idx++ == idx)
8235 return sp->sn_name;
8236 return NULL;
8237 default:
8238 return NULL;
8239 }
8240}
8241
8242/*
8243 * Handle command line completion for :sign command.
8244 */
8245 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008246set_context_in_sign_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008247{
8248 char_u *p;
8249 char_u *end_subcmd;
8250 char_u *last;
8251 int cmd_idx;
8252 char_u *begin_subcmd_args;
8253
8254 /* Default: expand subcommands. */
8255 xp->xp_context = EXPAND_SIGN;
8256 expand_what = EXP_SUBCMD;
8257 xp->xp_pattern = arg;
8258
8259 end_subcmd = skiptowhite(arg);
8260 if (*end_subcmd == NUL)
8261 /* expand subcmd name
8262 * :sign {subcmd}<CTRL-D>*/
8263 return;
8264
8265 cmd_idx = sign_cmd_idx(arg, end_subcmd);
8266
8267 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008268 * |
8269 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008270 begin_subcmd_args = skipwhite(end_subcmd);
8271 p = skiptowhite(begin_subcmd_args);
8272 if (*p == NUL)
8273 {
8274 /*
8275 * Expand first argument of subcmd when possible.
8276 * For ":jump {id}" and ":unplace {id}", we could
8277 * possibly expand the ids of all signs already placed.
8278 */
8279 xp->xp_pattern = begin_subcmd_args;
8280 switch (cmd_idx)
8281 {
8282 case SIGNCMD_LIST:
8283 case SIGNCMD_UNDEFINE:
8284 /* :sign list <CTRL-D>
8285 * :sign undefine <CTRL-D> */
8286 expand_what = EXP_SIGN_NAMES;
8287 break;
8288 default:
8289 xp->xp_context = EXPAND_NOTHING;
8290 }
8291 return;
8292 }
8293
8294 /* expand last argument of subcmd */
8295
8296 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008297 * |
8298 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008299
8300 /* Loop until reaching last argument. */
8301 do
8302 {
8303 p = skipwhite(p);
8304 last = p;
8305 p = skiptowhite(p);
8306 } while (*p != NUL);
8307
8308 p = vim_strchr(last, '=');
8309
8310 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008311 * | |
8312 * last p */
Bram Moolenaar7756e742016-10-21 20:35:37 +02008313 if (p == NULL)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008314 {
8315 /* Expand last argument name (before equal sign). */
8316 xp->xp_pattern = last;
8317 switch (cmd_idx)
8318 {
8319 case SIGNCMD_DEFINE:
8320 expand_what = EXP_DEFINE;
8321 break;
8322 case SIGNCMD_PLACE:
8323 expand_what = EXP_PLACE;
8324 break;
8325 case SIGNCMD_JUMP:
8326 case SIGNCMD_UNPLACE:
8327 expand_what = EXP_UNPLACE;
8328 break;
8329 default:
8330 xp->xp_context = EXPAND_NOTHING;
8331 }
8332 }
8333 else
8334 {
8335 /* Expand last argument value (after equal sign). */
8336 xp->xp_pattern = p + 1;
8337 switch (cmd_idx)
8338 {
8339 case SIGNCMD_DEFINE:
8340 if (STRNCMP(last, "texthl", p - last) == 0 ||
8341 STRNCMP(last, "linehl", p - last) == 0)
8342 xp->xp_context = EXPAND_HIGHLIGHT;
8343 else if (STRNCMP(last, "icon", p - last) == 0)
8344 xp->xp_context = EXPAND_FILES;
8345 else
8346 xp->xp_context = EXPAND_NOTHING;
8347 break;
8348 case SIGNCMD_PLACE:
8349 if (STRNCMP(last, "name", p - last) == 0)
8350 expand_what = EXP_SIGN_NAMES;
8351 else
8352 xp->xp_context = EXPAND_NOTHING;
8353 break;
8354 default:
8355 xp->xp_context = EXPAND_NOTHING;
8356 }
8357 }
8358}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008359# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008360#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008361
8362/*
8363 * Make the user happy.
8364 */
8365 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008366ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008367{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008368 static char *code[] = {
8369 "\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$",
8370 "\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"
8371 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008372 char *p;
8373 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008374 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008375
8376 msg_start();
8377 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008378 for (i = 0; i < 2; ++i)
8379 for (p = code[i]; *p != NUL; ++p)
8380 if (*p == 'x')
8381 msg_putchar('\n');
8382 else
8383 for (n = *p++; n > 0; --n)
8384 if (*p == 'o' || *p == '$')
Bram Moolenaar8820b482017-03-16 17:23:31 +01008385 msg_putchar_attr(*p, HL_ATTR(HLF_L));
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008386 else
8387 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008388 msg_clr_eos();
8389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*
8392 * ":drop"
8393 * Opens the first argument in a window. When there are two or more arguments
8394 * the argument list is redefined.
8395 */
8396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008397ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 win_T *wp;
8401 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008402 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 /*
8405 * Check if the first argument is already being edited in a window. If
8406 * so, jump to that window.
8407 * We would actually need to check all arguments, but that's complicated
8408 * and mostly only one file is dropped.
8409 * This also ignores wildcards, since it is very unlikely the user is
8410 * editing a file name with a wildcard character.
8411 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008412 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00008414 /*
8415 * Expanding wildcards may result in an empty argument list. E.g. when
8416 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
8417 * already did an error message for this.
8418 */
8419 if (ARGCOUNT == 0)
8420 return;
8421
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008422 if (cmdmod.tab)
8423 {
8424 /* ":tab drop file ...": open a tab for each argument that isn't
8425 * edited in a window yet. It's like ":tab all" but without closing
8426 * windows or tabs. */
8427 ex_all(eap);
8428 }
8429 else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008430 {
8431 /* ":drop file ...": Edit the first argument. Jump to an existing
8432 * window if possible, edit in current window if the current buffer
8433 * can be abandoned, otherwise open a new window. */
8434 buf = buflist_findnr(ARGLIST[0].ae_fnum);
8435
8436 FOR_ALL_TAB_WINDOWS(tp, wp)
8437 {
8438 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008440 goto_tabpage_win(tp, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 return;
8443 }
8444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008446 /*
8447 * Check whether the current buffer is changed. If so, we will need
8448 * to split the current window or data could be lost.
8449 * Skip the check if the 'hidden' option is set, as in this case the
8450 * buffer won't be lost.
8451 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02008452 if (!buf_hide(curbuf))
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008453 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008454 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008455 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008456 --emsg_off;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008459 /* Fake a ":sfirst" or ":first" command edit the first argument. */
8460 if (split)
8461 {
8462 eap->cmdidx = CMD_sfirst;
8463 eap->cmd[0] = 's';
8464 }
8465 else
8466 eap->cmdidx = CMD_first;
8467 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469}
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008470
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008471/*
8472 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
8473 * Put the start of the pattern in "*s", unless "s" is NULL.
8474 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
8475 * If "s" is not NULL terminate the pattern with a NUL.
8476 * Return a pointer to the char just past the pattern plus flags.
8477 */
8478 char_u *
8479skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
8480{
8481 int c;
8482
8483 if (vim_isIDc(*p))
8484 {
8485 /* ":vimgrep pattern fname" */
8486 if (s != NULL)
8487 *s = p;
8488 p = skiptowhite(p);
8489 if (s != NULL && *p != NUL)
8490 *p++ = NUL;
8491 }
8492 else
8493 {
8494 /* ":vimgrep /pattern/[g][j] fname" */
8495 if (s != NULL)
8496 *s = p + 1;
8497 c = *p;
8498 p = skip_regexp(p + 1, c, TRUE, NULL);
8499 if (*p != c)
8500 return NULL;
8501
8502 /* Truncate the pattern. */
8503 if (s != NULL)
8504 *p = NUL;
8505 ++p;
8506
8507 /* Find the flags */
8508 while (*p == 'g' || *p == 'j')
8509 {
8510 if (flags != NULL)
8511 {
8512 if (*p == 'g')
8513 *flags |= VGR_GLOBAL;
8514 else
8515 *flags |= VGR_NOJUMP;
8516 }
8517 ++p;
8518 }
8519 }
8520 return p;
8521}
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008522
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008523#if defined(FEAT_EVAL) || defined(PROTO)
8524/*
8525 * List v:oldfiles in a nice way.
8526 */
8527 void
8528ex_oldfiles(exarg_T *eap UNUSED)
8529{
8530 list_T *l = get_vim_var_list(VV_OLDFILES);
8531 listitem_T *li;
8532 int nr = 0;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008533 char_u *fname;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008534
8535 if (l == NULL)
8536 msg((char_u *)_("No old files"));
8537 else
8538 {
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008539 msg_start();
8540 msg_scroll = TRUE;
8541 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8542 {
8543 ++nr;
8544 fname = get_tv_string(&li->li_tv);
Bram Moolenaard6f2ee32016-08-24 00:30:52 +02008545 if (!message_filtered(fname))
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008546 {
8547 msg_outnum((long)nr);
8548 MSG_PUTS(": ");
8549 msg_outtrans(fname);
Bram Moolenaar885c00e2016-08-28 17:08:17 +02008550 msg_clr_eos();
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008551 msg_putchar('\n');
8552 out_flush(); /* output one line at a time */
8553 ui_breakcheck();
8554 }
8555 }
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008556
8557 /* Assume "got_int" was set to truncate the listing. */
8558 got_int = FALSE;
8559
8560# ifdef FEAT_BROWSE_CMD
8561 if (cmdmod.browse)
8562 {
8563 quit_more = FALSE;
8564 nr = prompt_for_number(FALSE);
8565 msg_starthere();
8566 if (nr > 0)
8567 {
8568 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8569 (long)nr);
8570
8571 if (p != NULL)
8572 {
8573 p = expand_env_save(p);
8574 eap->arg = p;
8575 eap->cmdidx = CMD_edit;
8576 cmdmod.browse = FALSE;
8577 do_exedit(eap, NULL);
8578 vim_free(p);
8579 }
8580 }
8581 }
8582# endif
8583 }
8584}
8585#endif