blob: 02750647e15f1c8c18503cee43a90a8fdbb1d548 [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 Moolenaareae1b912019-05-09 15:12:55 +020032static int help_compare(const void *s1, const void *s2);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static void prepare_help_buffer(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
36 * ":ascii" and "ga".
37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010039do_ascii(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000040{
41 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000042 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 char buf1[20];
44 char buf2[20];
45 char_u buf3[7];
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010046#ifdef FEAT_DIGRAPHS
47 char_u *dig;
48#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +000049 int cc[MAX_MCO];
50 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 int len;
52
53 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000054 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 c = gchar_cursor();
57 if (c == NUL)
58 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010059 msg("NUL");
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 return;
61 }
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 IObuff[0] = NUL;
64 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 {
66 if (c == NL) /* NUL is stored as NL */
67 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000068 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
69 cval = NL; /* NL is stored as CR */
70 else
71 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 if (vim_isprintc_strict(c) && (c < ' '
73#ifndef EBCDIC
74 || c > '~'
75#endif
76 ))
77 {
78 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000079 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 }
81 else
82 buf1[0] = NUL;
83#ifndef EBCDIC
84 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000085 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
86 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 else
88#endif
89 buf2[0] = NUL;
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010090#ifdef FEAT_DIGRAPHS
91 dig = get_digraph_for_char(cval);
92 if (dig != NULL)
93 vim_snprintf((char *)IObuff, IOSIZE,
94 _("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"),
95 transchar(c), buf1, buf2, cval, cval, cval, dig);
96 else
97#endif
98 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar555b2802005-05-19 21:08:39 +000099 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +0000100 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000101 if (enc_utf8)
102 c = cc[ci++];
103 else
104 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 }
106
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 /* Repeat for combining characters. */
108 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
109 {
110 len = (int)STRLEN(IObuff);
111 /* This assumes every multi-byte char is printable... */
112 if (len > 0)
113 IObuff[len++] = ' ';
114 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000115 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000116# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 )
120 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000121 len += (*mb_char2bytes)(c, IObuff + len);
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100122#ifdef FEAT_DIGRAPHS
123 dig = get_digraph_for_char(c);
124 if (dig != NULL)
125 vim_snprintf((char *)IObuff + len, IOSIZE - len,
126 c < 0x10000 ? _("> %d, Hex %04x, Oct %o, Digr %s")
127 : _("> %d, Hex %08x, Oct %o, Digr %s"),
128 c, c, c, dig);
129 else
130#endif
131 vim_snprintf((char *)IObuff + len, IOSIZE - len,
132 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
133 : _("> %d, Hex %08x, Octal %o"),
134 c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000135 if (ci == MAX_MCO)
136 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000137 if (enc_utf8)
138 c = cc[ci++];
139 else
140 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
Bram Moolenaar32526b32019-01-19 17:43:09 +0100143 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144}
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/*
147 * ":left", ":center" and ":right": align text.
148 */
149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100150ex_align(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151{
152 pos_T save_curpos;
153 int len;
154 int indent = 0;
155 int new_indent;
156 int has_tab;
157 int width;
158
159#ifdef FEAT_RIGHTLEFT
160 if (curwin->w_p_rl)
161 {
162 /* switch left and right aligning */
163 if (eap->cmdidx == CMD_right)
164 eap->cmdidx = CMD_left;
165 else if (eap->cmdidx == CMD_left)
166 eap->cmdidx = CMD_right;
167 }
168#endif
169
170 width = atoi((char *)eap->arg);
171 save_curpos = curwin->w_cursor;
172 if (eap->cmdidx == CMD_left) /* width is used for new indent */
173 {
174 if (width >= 0)
175 indent = width;
176 }
177 else
178 {
179 /*
180 * if 'textwidth' set, use it
181 * else if 'wrapmargin' set, use it
182 * if invalid value, use 80
183 */
184 if (width <= 0)
185 width = curbuf->b_p_tw;
186 if (width == 0 && curbuf->b_p_wm > 0)
Bram Moolenaar02631462017-09-22 15:20:32 +0200187 width = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (width <= 0)
189 width = 80;
190 }
191
192 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
193 return;
194
195 for (curwin->w_cursor.lnum = eap->line1;
196 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
197 {
198 if (eap->cmdidx == CMD_left) /* left align */
199 new_indent = indent;
200 else
201 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000202 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 len = linelen(eap->cmdidx == CMD_right ? &has_tab
204 : NULL) - get_indent();
205
206 if (len <= 0) /* skip blank lines */
207 continue;
208
209 if (eap->cmdidx == CMD_center)
210 new_indent = (width - len) / 2;
211 else
212 {
213 new_indent = width - len; /* right align */
214
215 /*
216 * Make sure that embedded TABs don't make the text go too far
217 * to the right.
218 */
219 if (has_tab)
220 while (new_indent > 0)
221 {
222 (void)set_indent(new_indent, 0);
223 if (linelen(NULL) <= width)
224 {
225 /*
226 * Now try to move the line as much as possible to
227 * the right. Stop when it moves too far.
228 */
229 do
230 (void)set_indent(++new_indent, 0);
231 while (linelen(NULL) <= width);
232 --new_indent;
233 break;
234 }
235 --new_indent;
236 }
237 }
238 }
239 if (new_indent < 0)
240 new_indent = 0;
241 (void)set_indent(new_indent, 0); /* set indent */
242 }
243 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
244 curwin->w_cursor = save_curpos;
245 beginline(BL_WHITE | BL_FIX);
246}
247
248/*
249 * Get the length of the current line, excluding trailing white space.
250 */
251 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100252linelen(int *has_tab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 char_u *line;
255 char_u *first;
256 char_u *last;
257 int save;
258 int len;
259
260 /* find the first non-blank character */
261 line = ml_get_curline();
262 first = skipwhite(line);
263
264 /* find the character after the last non-blank character */
265 for (last = first + STRLEN(first);
Bram Moolenaar1c465442017-03-12 20:10:05 +0100266 last > first && VIM_ISWHITE(last[-1]); --last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 ;
268 save = *last;
269 *last = NUL;
270 len = linetabsize(line); /* get line length */
271 if (has_tab != NULL) /* check for embedded TAB */
Bram Moolenaarb31a3ac2018-08-11 14:41:55 +0200272 *has_tab = (vim_strchr(first, TAB) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 *last = save;
274
275 return len;
276}
277
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000278/* Buffer for two lines used during sorting. They are allocated to
279 * contain the longest line being sorted. */
280static char_u *sortbuf1;
281static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000282
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100283static int sort_ic; /* ignore case */
284static int sort_nr; /* sort on number */
285static int sort_rx; /* sort on regex instead of skipping it */
286#ifdef FEAT_FLOAT
287static int sort_flt; /* sort on floating number */
288#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000289
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100290static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000291
292/* Struct to store info to be sorted. */
293typedef struct
294{
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100295 linenr_T lnum; // line number
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100296 union {
297 struct
298 {
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100299 varnumber_T start_col_nr; // starting column number
300 varnumber_T end_col_nr; // ending column number
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100301 } line;
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100302 struct
303 {
304 varnumber_T value; // value if sorting by integer
305 int is_number; // TRUE when line contains a number
306 } num;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100307#ifdef FEAT_FLOAT
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100308 float_T value_flt; // value if sorting by float
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100309#endif
310 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000311} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000312
Bram Moolenaareae1b912019-05-09 15:12:55 +0200313static int sort_compare(const void *s1, const void *s2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000314
315 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100316sort_compare(const void *s1, const void *s2)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000317{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000318 sorti_T l1 = *(sorti_T *)s1;
319 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000320 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000321
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000322 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000323 * if we return 0 every time, qsort will assume it's done sorting and
324 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000325 if (sort_abort)
326 return 0;
327 fast_breakcheck();
328 if (got_int)
329 sort_abort = TRUE;
330
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000331 if (sort_nr)
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100332 {
333 if (l1.st_u.num.is_number != l2.st_u.num.is_number)
334 result = l1.st_u.num.is_number - l2.st_u.num.is_number;
335 else
336 result = l1.st_u.num.value == l2.st_u.num.value ? 0
337 : l1.st_u.num.value > l2.st_u.num.value ? 1 : -1;
338 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100339#ifdef FEAT_FLOAT
340 else if (sort_flt)
341 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
342 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
343#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344 else
345 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000346 /* We need to copy one line into "sortbuf1", because there is no
347 * guarantee that the first pointer becomes invalid when obtaining the
348 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100349 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
350 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
351 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
352 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
353 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
354 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000355
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000356 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
357 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000358 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000359
360 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000361 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000362 return (int)(l1.lnum - l2.lnum);
363 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000364}
365
366/*
367 * ":sort".
368 */
369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100370ex_sort(exarg_T *eap)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000371{
372 regmatch_T regmatch;
373 int len;
374 linenr_T lnum;
375 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000376 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000377 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000378 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000379 char_u *p;
380 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000381 char_u *s2;
382 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000383 int unique = FALSE;
384 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000385 colnr_T start_col;
386 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100387 int sort_what = 0;
388 int format_found = 0;
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200389 int change_occurred = FALSE; // Buffer contents changed.
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000390
Bram Moolenaar48c99162008-02-18 18:42:52 +0000391 /* Sorting one line is really quick! */
392 if (count <= 1)
393 return;
394
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000395 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
396 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000397 sortbuf1 = NULL;
398 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000399 regmatch.regprog = NULL;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200400 nrs = (sorti_T *)alloc(count * sizeof(sorti_T));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000401 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000402 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000403
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100404 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100405#ifdef FEAT_FLOAT
406 sort_flt = 0;
407#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000408
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000409 for (p = eap->arg; *p != NUL; ++p)
410 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100411 if (VIM_ISWHITE(*p))
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000412 ;
413 else if (*p == 'i')
414 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000415 else if (*p == 'r')
416 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000417 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100418 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100419 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100420 ++format_found;
421 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100422#ifdef FEAT_FLOAT
423 else if (*p == 'f')
424 {
425 sort_flt = 1;
426 ++format_found;
427 }
428#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100429 else if (*p == 'b')
430 {
431 sort_what = STR2NR_BIN + STR2NR_FORCE;
432 ++format_found;
433 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000434 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100435 {
436 sort_what = STR2NR_OCT + STR2NR_FORCE;
437 ++format_found;
438 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000439 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100440 {
441 sort_what = STR2NR_HEX + STR2NR_FORCE;
442 ++format_found;
443 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000444 else if (*p == 'u')
445 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000446 else if (*p == '"') /* comment start */
447 break;
448 else if (check_nextcmd(p) != NULL)
449 {
450 eap->nextcmd = check_nextcmd(p);
451 break;
452 }
453 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000454 {
455 s = skip_regexp(p + 1, *p, TRUE, NULL);
456 if (*s != *p)
457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100458 emsg(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000459 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000460 }
461 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000462 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100463 if (s == p + 1)
464 {
465 if (last_search_pat() == NULL)
466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100467 emsg(_(e_noprevre));
Bram Moolenaar210f3702013-03-07 16:41:30 +0100468 goto sortend;
469 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000470 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100471 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000472 else
473 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000474 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000475 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000476 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000477 regmatch.rm_ic = p_ic;
478 }
479 else
480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100481 semsg(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000482 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000483 }
484 }
485
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100486 /* Can only have one of 'n', 'b', 'o' and 'x'. */
487 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100489 emsg(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000490 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000491 }
492
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100493 /* From here on "sort_nr" is used as a flag for any integer number
494 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100495 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000496
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000497 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000498 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000499 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000500 * When sorting on strings "start_col_nr" is the offset in the line, for
501 * numbers sorting it's the number to sort on. This means the pattern
502 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000503 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000504 */
505 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
506 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000507 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000508 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000509 if (maxlen < len)
510 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000511
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000512 start_col = 0;
513 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000514 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000515 {
516 if (sort_rx)
517 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000518 start_col = (colnr_T)(regmatch.startp[0] - s);
519 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000520 }
521 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000522 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000523 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000524 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000525 if (regmatch.regprog != NULL)
526 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000527
Bram Moolenaar937204a2016-01-30 23:37:38 +0100528 if (sort_nr
529#ifdef FEAT_FLOAT
530 || sort_flt
531#endif
532 )
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000533 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000534 /* Make sure vim_str2nr doesn't read any digits past the end
535 * of the match, by temporarily terminating the string there */
536 s2 = s + end_col;
537 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200538 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000539 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000540 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100541 if (sort_nr)
542 {
543 if (sort_what & STR2NR_HEX)
544 s = skiptohex(p);
545 else if (sort_what & STR2NR_BIN)
546 s = skiptobin(p);
547 else
548 s = skiptodigit(p);
549 if (s > p && s[-1] == '-')
550 --s; /* include preceding negative sign */
551 if (*s == NUL)
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100552 {
553 /* line without number should sort before any number */
554 nrs[lnum - eap->line1].st_u.num.is_number = FALSE;
555 nrs[lnum - eap->line1].st_u.num.value = 0;
556 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100557 else
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100558 {
559 nrs[lnum - eap->line1].st_u.num.is_number = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100560 vim_str2nr(s, NULL, NULL, sort_what,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200561 &nrs[lnum - eap->line1].st_u.num.value,
562 NULL, 0, FALSE);
Bram Moolenaara25e3d02019-02-20 22:19:05 +0100563 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100564 }
565#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000566 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100567 {
568 s = skipwhite(p);
569 if (*s == '+')
570 s = skipwhite(s + 1);
571
572 if (*s == NUL)
573 /* empty line should sort before any number */
574 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
575 else
576 nrs[lnum - eap->line1].st_u.value_flt =
577 strtod((char *)s, NULL);
578 }
579#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200580 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000581 }
582 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000583 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000584 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100585 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
586 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000587 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000588
589 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000590
591 if (regmatch.regprog != NULL)
592 fast_breakcheck();
593 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000594 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000595 }
596
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000597 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +0200598 sortbuf1 = alloc(maxlen + 1);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000599 if (sortbuf1 == NULL)
600 goto sortend;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200601 sortbuf2 = alloc(maxlen + 1);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000602 if (sortbuf2 == NULL)
603 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000604
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000605 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000606 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000607
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000608 if (sort_abort)
609 goto sortend;
610
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000611 /* Insert the lines in the sorted order below the last one. */
612 lnum = eap->line2;
613 for (i = 0; i < count; ++i)
614 {
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200615 linenr_T get_lnum = nrs[eap->forceit ? count - i - 1 : i].lnum;
616
617 // If the original line number of the line being placed is not the same
618 // as "lnum" (accounting for offset), we know that the buffer changed.
619 if (get_lnum + ((linenr_T)count - 1) != lnum)
620 change_occurred = TRUE;
621
622 s = ml_get(get_lnum);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000623 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000624 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000625 {
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200626 // Copy the line into a buffer, it may become invalid in
627 // ml_append(). And it's needed for "unique".
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100628 STRCPY(sortbuf1, s);
629 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000630 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000631 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000632 fast_breakcheck();
633 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000634 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000635 }
636
637 /* delete the original lines if appending worked */
638 if (i == count)
639 for (i = 0; i < count; ++i)
640 ml_delete(eap->line1, FALSE);
641 else
642 count = 0;
643
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000644 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000645 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000646 if (deleted > 0)
Bram Moolenaarb0e982b2018-09-21 12:46:22 +0200647 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000648 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
Bram Moolenaarb0e982b2018-09-21 12:46:22 +0200649 msgmore(-deleted);
650 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000651 else if (deleted < 0)
652 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
Bram Moolenaardc9e9552018-06-28 11:28:08 +0200653
654 if (change_occurred || deleted != 0)
655 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000656
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000657 curwin->w_cursor.lnum = eap->line1;
658 beginline(BL_WHITE | BL_FIX);
659
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000660sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000661 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000662 vim_free(sortbuf1);
663 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200664 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000665 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100666 emsg(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000667}
668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669/*
670 * ":retab".
671 */
672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100673ex_retab(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
675 linenr_T lnum;
676 int got_tab = FALSE;
677 long num_spaces = 0;
678 long num_tabs;
679 long len;
680 long col;
681 long vcol;
682 long start_col = 0; /* For start of white-space string */
683 long start_vcol = 0; /* For start of white-space string */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 long old_len;
685 char_u *ptr;
686 char_u *new_line = (char_u *)1; /* init to non-NULL */
687 int did_undo; /* called u_save for current line */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200688#ifdef FEAT_VARTABS
Bram Moolenaar0119a592018-06-24 23:53:28 +0200689 int *new_vts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200690 char_u *new_ts_str; /* string value of tab argument */
691#else
692 int temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 int save_list;
696 linenr_T first_line = 0; /* first changed line */
697 linenr_T last_line = 0; /* last changed line */
698
699 save_list = curwin->w_p_list;
700 curwin->w_p_list = 0; /* don't want list mode here */
701
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200702#ifdef FEAT_VARTABS
703 new_ts_str = eap->arg;
Bram Moolenaar0119a592018-06-24 23:53:28 +0200704 if (!tabstop_set(eap->arg, &new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200705 return;
706 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
707 ++(eap->arg);
708
Bram Moolenaar0119a592018-06-24 23:53:28 +0200709 // This ensures that either new_vts_array and new_ts_str are freshly
710 // allocated, or new_vts_array points to an existing array and new_ts_str
711 // is null.
712 if (new_vts_array == NULL)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200713 {
Bram Moolenaar0119a592018-06-24 23:53:28 +0200714 new_vts_array = curbuf->b_p_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200715 new_ts_str = NULL;
716 }
717 else
718 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
719#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 new_ts = getdigits(&(eap->arg));
721 if (new_ts < 0)
722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100723 emsg(_(e_positive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return;
725 }
726 if (new_ts == 0)
727 new_ts = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
730 {
731 ptr = ml_get(lnum);
732 col = 0;
733 vcol = 0;
734 did_undo = FALSE;
735 for (;;)
736 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100737 if (VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 {
739 if (!got_tab && num_spaces == 0)
740 {
741 /* First consecutive white-space */
742 start_vcol = vcol;
743 start_col = col;
744 }
745 if (ptr[col] == ' ')
746 num_spaces++;
747 else
748 got_tab = TRUE;
749 }
750 else
751 {
752 if (got_tab || (eap->forceit && num_spaces > 1))
753 {
754 /* Retabulate this string of white-space */
755
756 /* len is virtual length of white string */
757 len = num_spaces = vcol - start_vcol;
758 num_tabs = 0;
759 if (!curbuf->b_p_et)
760 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200761#ifdef FEAT_VARTABS
762 int t, s;
763
764 tabstop_fromto(start_vcol, vcol,
Bram Moolenaar0119a592018-06-24 23:53:28 +0200765 curbuf->b_p_ts, new_vts_array, &t, &s);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200766 num_tabs = t;
767 num_spaces = s;
768#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 temp = new_ts - (start_vcol % new_ts);
770 if (num_spaces >= temp)
771 {
772 num_spaces -= temp;
773 num_tabs++;
774 }
775 num_tabs += num_spaces / new_ts;
776 num_spaces -= (num_spaces / new_ts) * new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 if (curbuf->b_p_et || got_tab ||
780 (num_spaces + num_tabs < len))
781 {
782 if (did_undo == FALSE)
783 {
784 did_undo = TRUE;
785 if (u_save((linenr_T)(lnum - 1),
786 (linenr_T)(lnum + 1)) == FAIL)
787 {
788 new_line = NULL; /* flag out-of-memory */
789 break;
790 }
791 }
792
793 /* len is actual number of white characters used */
794 len = num_spaces + num_tabs;
795 old_len = (long)STRLEN(ptr);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200796 new_line = alloc(old_len - col + start_col + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 if (new_line == NULL)
798 break;
799 if (start_col > 0)
800 mch_memmove(new_line, ptr, (size_t)start_col);
801 mch_memmove(new_line + start_col + len,
802 ptr + col, (size_t)(old_len - col + 1));
803 ptr = new_line + start_col;
804 for (col = 0; col < len; col++)
805 ptr[col] = (col < num_tabs) ? '\t' : ' ';
806 ml_replace(lnum, new_line, FALSE);
807 if (first_line == 0)
808 first_line = lnum;
809 last_line = lnum;
810 ptr = new_line;
811 col = start_col + len;
812 }
813 }
814 got_tab = FALSE;
815 num_spaces = 0;
816 }
817 if (ptr[col] == NUL)
818 break;
819 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000821 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ++col;
824 }
825 if (new_line == NULL) /* out of memory */
826 break;
827 line_breakcheck();
828 }
829 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200832#ifdef FEAT_VARTABS
833 // If a single value was given then it can be considered equal to
834 // either the value of 'tabstop' or the value of 'vartabstop'.
835 if (tabstop_count(curbuf->b_p_vts_array) == 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200836 && tabstop_count(new_vts_array) == 1
837 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200838 ; /* not changed */
839 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200840 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200841 ; /* not changed */
842 else
843 redraw_curbuf_later(NOT_VALID);
844#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (curbuf->b_p_ts != new_ts)
846 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (first_line != 0)
849 changed_lines(first_line, 0, last_line + 1, 0L);
850
851 curwin->w_p_list = save_list; /* restore 'list' */
852
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200853#ifdef FEAT_VARTABS
854 if (new_ts_str != NULL) /* set the new tabstop */
855 {
856 // If 'vartabstop' is in use or if the value given to retab has more
857 // than one tabstop then update 'vartabstop'.
858 int *old_vts_ary = curbuf->b_p_vts_array;
859
Bram Moolenaar0119a592018-06-24 23:53:28 +0200860 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200861 {
862 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
863 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0119a592018-06-24 23:53:28 +0200864 curbuf->b_p_vts_array = new_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200865 vim_free(old_vts_ary);
866 }
867 else
868 {
869 // 'vartabstop' wasn't in use and a single value was given to
870 // retab then update 'tabstop'.
Bram Moolenaar0119a592018-06-24 23:53:28 +0200871 curbuf->b_p_ts = tabstop_first(new_vts_array);
872 vim_free(new_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200873 }
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200874 vim_free(new_ts_str);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200875 }
876#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 curbuf->b_p_ts = new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 coladvance(curwin->w_curswant);
880
881 u_clearline();
882}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884/*
885 * :move command - move lines line1-line2 to line dest
886 *
887 * return FAIL for failure, OK otherwise
888 */
889 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100890do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 char_u *str;
893 linenr_T l;
Bram Moolenaarddd1f912018-11-10 19:19:36 +0100894 linenr_T extra; // Num lines added before line1
895 linenr_T num_lines; // Num lines moved
896 linenr_T last_line; // Last line in file after adding new text
Bram Moolenaard5f69332015-04-15 12:43:50 +0200897#ifdef FEAT_FOLDING
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100898 win_T *win;
899 tabpage_T *tp;
Bram Moolenaard5f69332015-04-15 12:43:50 +0200900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 if (dest >= line1 && dest < line2)
903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_("E134: Cannot move a range of lines into itself"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 return FAIL;
906 }
907
Bram Moolenaarddd1f912018-11-10 19:19:36 +0100908 // Do nothing if we are not actually moving any lines. This will prevent
909 // the 'modified' flag from being set without cause.
910 if (dest == line1 - 1 || dest == line2)
911 {
912 // Move the cursor as if lines were moved (see below) to be backwards
913 // compatible.
914 if (dest >= line1)
915 curwin->w_cursor.lnum = dest;
916 else
917 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
918
919 return OK;
920 }
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 num_lines = line2 - line1 + 1;
923
924 /*
925 * First we copy the old text to its new location -- webb
926 * Also copy the flag that ":global" command uses.
927 */
928 if (u_save(dest, dest + 1) == FAIL)
929 return FAIL;
930 for (extra = 0, l = line1; l <= line2; l++)
931 {
932 str = vim_strsave(ml_get(l + extra));
933 if (str != NULL)
934 {
935 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
936 vim_free(str);
937 if (dest < line1)
938 extra++;
939 }
940 }
941
942 /*
943 * Now we must be careful adjusting our marks so that we don't overlap our
944 * mark_adjust() calls.
945 *
946 * We adjust the marks within the old text so that they refer to the
947 * last lines of the file (temporarily), because we know no other marks
948 * will be set there since these line numbers did not exist until we added
949 * our new lines.
950 *
951 * Then we adjust the marks on lines between the old and new text positions
952 * (either forwards or backwards).
953 *
954 * And Finally we adjust the marks we put at the end of the file back to
955 * their final destination at the new text position -- webb
956 */
957 last_line = curbuf->b_ml.ml_line_count;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100958 mark_adjust_nofold(line1, line2, last_line - line2, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (dest >= line2)
960 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100961 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
962#ifdef FEAT_FOLDING
963 FOR_ALL_TAB_WINDOWS(tp, win) {
964 if (win->w_buffer == curbuf)
965 foldMoveRange(&win->w_folds, line1, line2, dest);
966 }
967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 curbuf->b_op_start.lnum = dest - num_lines + 1;
969 curbuf->b_op_end.lnum = dest;
970 }
971 else
972 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100973 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
974#ifdef FEAT_FOLDING
975 FOR_ALL_TAB_WINDOWS(tp, win) {
976 if (win->w_buffer == curbuf)
977 foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
978 }
979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 curbuf->b_op_start.lnum = dest + 1;
981 curbuf->b_op_end.lnum = dest + num_lines;
982 }
983 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100984 mark_adjust_nofold(last_line - num_lines + 1, last_line,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 -(last_line - dest - extra), 0L);
986
987 /*
988 * Now we delete the original text -- webb
989 */
990 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
991 return FAIL;
992
993 for (l = line1; l <= line2; l++)
994 ml_delete(line1 + extra, TRUE);
995
996 if (!global_busy && num_lines > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100997 smsg(NGETTEXT("%ld line moved", "%ld lines moved", num_lines),
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200998 (long)num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 /*
1001 * Leave the cursor on the last of the moved lines.
1002 */
1003 if (dest >= line1)
1004 curwin->w_cursor.lnum = dest;
1005 else
1006 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
1007
1008 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +01001009 {
1010 dest += num_lines + 1;
1011 last_line = curbuf->b_ml.ml_line_count;
1012 if (dest > last_line + 1)
1013 dest = last_line + 1;
1014 changed_lines(line1, 0, dest, 0L);
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 else
1017 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
1018
1019 return OK;
1020}
1021
1022/*
1023 * ":copy"
1024 */
1025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001026ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 linenr_T count;
1029 char_u *p;
1030
1031 count = line2 - line1 + 1;
1032 curbuf->b_op_start.lnum = n + 1;
1033 curbuf->b_op_end.lnum = n + count;
1034 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
1035
1036 /*
1037 * there are three situations:
1038 * 1. destination is above line1
1039 * 2. destination is between line1 and line2
1040 * 3. destination is below line2
1041 *
1042 * n = destination (when starting)
1043 * curwin->w_cursor.lnum = destination (while copying)
1044 * line1 = start of source (while copying)
1045 * line2 = end of source (while copying)
1046 */
1047 if (u_save(n, n + 1) == FAIL)
1048 return;
1049
1050 curwin->w_cursor.lnum = n;
1051 while (line1 <= line2)
1052 {
1053 /* need to use vim_strsave() because the line will be unlocked within
1054 * ml_append() */
1055 p = vim_strsave(ml_get(line1));
1056 if (p != NULL)
1057 {
1058 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
1059 vim_free(p);
1060 }
1061 /* situation 2: skip already copied lines */
1062 if (line1 == n)
1063 line1 = curwin->w_cursor.lnum;
1064 ++line1;
1065 if (curwin->w_cursor.lnum < line1)
1066 ++line1;
1067 if (curwin->w_cursor.lnum < line2)
1068 ++line2;
1069 ++curwin->w_cursor.lnum;
1070 }
1071
1072 appended_lines_mark(n, count);
1073
1074 msgmore((long)count);
1075}
1076
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001077static char_u *prevcmd = NULL; /* the previous command */
1078
1079#if defined(EXITFREE) || defined(PROTO)
1080 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001081free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001082{
1083 vim_free(prevcmd);
1084}
1085#endif
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087/*
1088 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
1089 * Bangs in the argument are replaced with the previously entered command.
1090 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
1092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001093do_bang(
1094 int addr_count,
1095 exarg_T *eap,
1096 int forceit,
1097 int do_in,
1098 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 char_u *arg = eap->arg; /* command */
1101 linenr_T line1 = eap->line1; /* start of range */
1102 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 char_u *newcmd = NULL; /* the new command */
1104 int free_newcmd = FALSE; /* need to free() newcmd */
1105 int ins_prevcmd;
1106 char_u *t;
1107 char_u *p;
1108 char_u *trailarg;
1109 int len;
1110 int scroll_save = msg_scroll;
1111
1112 /*
1113 * Disallow shell commands for "rvim".
1114 * Disallow shell commands from .exrc and .vimrc in current directory for
1115 * security reasons.
1116 */
1117 if (check_restricted() || check_secure())
1118 return;
1119
1120 if (addr_count == 0) /* :! */
1121 {
1122 msg_scroll = FALSE; /* don't scroll here */
1123 autowrite_all();
1124 msg_scroll = scroll_save;
1125 }
1126
1127 /*
1128 * Try to find an embedded bang, like in :!<cmd> ! [args]
1129 * (:!! is indicated by the 'forceit' variable)
1130 */
1131 ins_prevcmd = forceit;
1132 trailarg = arg;
1133 do
1134 {
1135 len = (int)STRLEN(trailarg) + 1;
1136 if (newcmd != NULL)
1137 len += (int)STRLEN(newcmd);
1138 if (ins_prevcmd)
1139 {
1140 if (prevcmd == NULL)
1141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001142 emsg(_(e_noprev));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 vim_free(newcmd);
1144 return;
1145 }
1146 len += (int)STRLEN(prevcmd);
1147 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02001148 if ((t = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
1150 vim_free(newcmd);
1151 return;
1152 }
1153 *t = NUL;
1154 if (newcmd != NULL)
1155 STRCAT(t, newcmd);
1156 if (ins_prevcmd)
1157 STRCAT(t, prevcmd);
1158 p = t + STRLEN(t);
1159 STRCAT(t, trailarg);
1160 vim_free(newcmd);
1161 newcmd = t;
1162
1163 /*
1164 * Scan the rest of the argument for '!', which is replaced by the
1165 * previous command. "\!" is replaced by "!" (this is vi compatible).
1166 */
1167 trailarg = NULL;
1168 while (*p)
1169 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001170 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001173 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 else
1175 {
1176 trailarg = p;
1177 *trailarg++ = NUL;
1178 ins_prevcmd = TRUE;
1179 break;
1180 }
1181 }
1182 ++p;
1183 }
1184 } while (trailarg != NULL);
1185
1186 vim_free(prevcmd);
1187 prevcmd = newcmd;
1188
1189 if (bangredo) /* put cmd in redo buffer for ! command */
1190 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001191 /* If % or # appears in the command, it must have been escaped.
1192 * Reescape them, so that redoing them does not substitute them by the
1193 * buffername. */
1194 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1195
1196 if (cmd != NULL)
1197 {
1198 AppendToRedobuffLit(cmd, -1);
1199 vim_free(cmd);
1200 }
1201 else
1202 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 AppendToRedobuff((char_u *)"\n");
1204 bangredo = FALSE;
1205 }
1206 /*
1207 * Add quotes around the command, for shells that need them.
1208 */
1209 if (*p_shq != NUL)
1210 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001211 newcmd = alloc(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (newcmd == NULL)
1213 return;
1214 STRCPY(newcmd, p_shq);
1215 STRCAT(newcmd, prevcmd);
1216 STRCAT(newcmd, p_shq);
1217 free_newcmd = TRUE;
1218 }
1219 if (addr_count == 0) /* :! */
1220 {
1221 /* echo the command */
1222 msg_start();
1223 msg_putchar(':');
1224 msg_putchar('!');
1225 msg_outtrans(newcmd);
1226 msg_clr_eos();
1227 windgoto(msg_row, msg_col);
1228
1229 do_shell(newcmd, 0);
1230 }
1231 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /* Careful: This may recursively call do_bang() again! (because of
1234 * autocommands) */
1235 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001236 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarade00832006-03-10 21:46:58 +00001237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (free_newcmd)
1239 vim_free(newcmd);
1240}
1241
1242/*
1243 * do_filter: filter lines through a command given by the user
1244 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245 * We mostly use temp files and the call_shell() routine here. This would
1246 * normally be done using pipes on a UNIX machine, but this is more portable
1247 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 * to deal with redirection somehow, and should handle things like looking
1249 * at the PATH env. variable, and adding reasonable extensions to the
1250 * command name given by the user. All reasonable versions of call_shell()
1251 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252 * Alternatively, if on Unix and redirecting input or output, but not both,
1253 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * We use input redirection if do_in is TRUE.
1255 * We use output redirection if do_out is TRUE.
1256 */
1257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001258do_filter(
1259 linenr_T line1,
1260 linenr_T line2,
1261 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1262 char_u *cmd,
1263 int do_in,
1264 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 char_u *itmp = NULL;
1267 char_u *otmp = NULL;
1268 linenr_T linecount;
1269 linenr_T read_linecount;
1270 pos_T cursor_save;
1271 char_u *cmd_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 buf_T *old_curbuf = curbuf;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001273 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 if (*cmd == NUL) /* no filter command */
1276 return;
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 cursor_save = curwin->w_cursor;
1279 linecount = line2 - line1 + 1;
1280 curwin->w_cursor.lnum = line1;
1281 curwin->w_cursor.col = 0;
1282 changed_line_abv_curs();
1283 invalidate_botline();
1284
1285 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001286 * When using temp files:
1287 * 1. * Form temp file names
1288 * 2. * Write the lines to a temp file
1289 * 3. Run the filter command on the temp file
1290 * 4. * Read the output of the command into the buffer
1291 * 5. * Delete the original lines to be filtered
1292 * 6. * Remove the temp files
1293 *
1294 * When writing the input with a pipe or when catching the output with a
1295 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 */
1297
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001298 if (do_out)
1299 shell_flags |= SHELL_DOOUT;
1300
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001301#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001302 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001304 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1305 shell_flags |= SHELL_READ;
1306 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001308 else if (do_in && !do_out && !p_stmp)
1309 {
1310 /* Use a pipe to write stdin of the command, do not use a temp file. */
1311 shell_flags |= SHELL_WRITE;
1312 curbuf->b_op_start.lnum = line1;
1313 curbuf->b_op_end.lnum = line2;
1314 }
1315 else if (do_in && do_out && !p_stmp)
1316 {
1317 /* Use a pipe to write stdin and fetch stdout of the command, do not
1318 * use a temp file. */
1319 shell_flags |= SHELL_READ|SHELL_WRITE;
1320 curbuf->b_op_start.lnum = line1;
1321 curbuf->b_op_end.lnum = line2;
1322 curwin->w_cursor.lnum = line2;
1323 }
1324 else
1325#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001326 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1327 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001329 emsg(_(e_notmp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001330 goto filterend;
1331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333/*
1334 * The writing and reading of temp files will not be shown.
1335 * Vi also doesn't do this and the messages are not very informative.
1336 */
1337 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001338 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 FALSE, FALSE, FALSE, TRUE) == FAIL)
1340 {
1341 msg_putchar('\n'); /* keep message from buf_write() */
1342 --no_wait_return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001343#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (!aborting())
1345#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001346 (void)semsg(_(e_notcreate), itmp); /* will call wait_return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 goto filterend;
1348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (curbuf != old_curbuf)
1350 goto filterend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 if (!do_out)
1353 msg_putchar('\n');
1354
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001355 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1357 if (cmd_buf == NULL)
1358 goto filterend;
1359
1360 windgoto((int)Rows - 1, 0);
1361 cursor_on();
1362
1363 /*
1364 * When not redirecting the output the command can write anything to the
1365 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1366 * stderr output of external command. Clear the screen later.
1367 * If do_in is FALSE, this could be something like ":r !cat", which may
1368 * also mess up the screen, clear it later.
1369 */
1370 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1371 redraw_later_clear();
1372
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001373 if (do_out)
1374 {
1375 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001376 {
1377 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001378 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001379 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001380 redraw_curbuf_later(VALID);
1381 }
1382 read_linecount = curbuf->b_ml.ml_line_count;
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 /*
1385 * When call_shell() fails wait_return() is called to give the user a
1386 * chance to read the error messages. Otherwise errors are ignored, so you
1387 * can see the error messages from the command that appear on stdout; use
1388 * 'u' to fix the text
1389 * Switch to cooked mode when not redirecting stdin, avoids that something
1390 * like ":r !cat" hangs.
1391 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1392 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001393 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
1395 redraw_later_clear();
1396 wait_return(FALSE);
1397 }
1398 vim_free(cmd_buf);
1399
1400 did_check_timestamps = FALSE;
1401 need_check_timestamps = TRUE;
1402
1403 /* When interrupting the shell command, it may still have produced some
1404 * useful output. Reset got_int here, so that readfile() won't cancel
1405 * reading. */
1406 ui_breakcheck();
1407 got_int = FALSE;
1408
1409 if (do_out)
1410 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001411 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001413 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001414 eap, READ_FILTER) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001416#if defined(FEAT_EVAL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001417 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001419 {
1420 msg_putchar('\n');
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001421 semsg(_(e_notread), otmp);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001422 }
1423 goto error;
1424 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001425 if (curbuf != old_curbuf)
1426 goto filterend;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001427 }
1428
1429 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1430
1431 if (shell_flags & SHELL_READ)
1432 {
1433 curbuf->b_op_start.lnum = line2 + 1;
1434 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1435 appended_lines_mark(line2, read_linecount);
1436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 if (do_in)
1439 {
1440 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (read_linecount >= linecount)
1443 /* move all marks from old lines to new lines */
1444 mark_adjust(line1, line2, linecount, 0L);
1445 else
1446 {
1447 /* move marks from old lines to new lines, delete marks
1448 * that are in deleted lines */
1449 mark_adjust(line1, line1 + read_linecount - 1,
1450 linecount, 0L);
1451 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1452 }
1453 }
1454
1455 /*
1456 * Put cursor on first filtered line for ":range!cmd".
1457 * Adjust '[ and '] (set by buf_write()).
1458 */
1459 curwin->w_cursor.lnum = line1;
1460 del_lines(linecount, TRUE);
1461 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1462 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1463 write_lnum_adjust(-linecount); /* adjust last line
1464 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001465#ifdef FEAT_FOLDING
1466 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469 else
1470 {
1471 /*
1472 * Put cursor on last new line for ":r !cmd".
1473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001475 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1479 --no_wait_return;
1480
1481 if (linecount > p_report)
1482 {
1483 if (do_in)
1484 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001485 vim_snprintf(msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00001486 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /* save message to display it after redraw */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001489 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 else
1492 msgmore((long)linecount);
1493 }
1494 }
1495 else
1496 {
1497error:
1498 /* put cursor back in same position for ":w !cmd" */
1499 curwin->w_cursor = cursor_save;
1500 --no_wait_return;
1501 wait_return(FALSE);
1502 }
1503
1504filterend:
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (curbuf != old_curbuf)
1507 {
1508 --no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001509 emsg(_("E135: *Filter* Autocommands must not change current buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (itmp != NULL)
1512 mch_remove(itmp);
1513 if (otmp != NULL)
1514 mch_remove(otmp);
1515 vim_free(itmp);
1516 vim_free(otmp);
1517}
1518
1519/*
1520 * Call a shell to execute a command.
1521 * When "cmd" is NULL start an interactive shell.
1522 */
1523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001524do_shell(
1525 char_u *cmd,
1526 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 buf_T *buf;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001529#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 int save_nwr;
1531#endif
1532#ifdef MSWIN
1533 int winstart = FALSE;
Bram Moolenaar049ca592019-03-17 16:59:44 +01001534 int keep_termcap = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
1536
1537 /*
1538 * Disallow shell commands for "rvim".
1539 * Disallow shell commands from .exrc and .vimrc in current directory for
1540 * security reasons.
1541 */
1542 if (check_restricted() || check_secure())
1543 {
1544 msg_end();
1545 return;
1546 }
1547
1548#ifdef MSWIN
1549 /*
Bram Moolenaar049ca592019-03-17 16:59:44 +01001550 * Check if ":!start" is used. This implies not stopping termcap mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
1552 if (cmd != NULL)
Bram Moolenaar049ca592019-03-17 16:59:44 +01001553 keep_termcap = winstart = (STRNICMP(cmd, "start ", 6) == 0);
1554
1555# if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
1556 // Don't stop termcap mode when using a terminal window for the shell.
1557 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
1558 keep_termcap = TRUE;
1559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560#endif
1561
1562 /*
1563 * For autocommands we want to get the output on the current screen, to
1564 * avoid having to type return below.
1565 */
Bram Moolenaar049ca592019-03-17 16:59:44 +01001566 msg_putchar('\r'); // put cursor at start of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569#ifdef MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001570 if (!keep_termcap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#endif
1572 stoptermcap();
1573 }
1574#ifdef MSWIN
1575 if (!winstart)
1576#endif
Bram Moolenaar049ca592019-03-17 16:59:44 +01001577 msg_putchar('\n'); // may shift screen one line up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar049ca592019-03-17 16:59:44 +01001579 // warning message before calling the shell
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001580 if (p_warn && !autocmd_busy && msg_silent == 0)
Bram Moolenaar29323592016-07-24 22:04:11 +02001581 FOR_ALL_BUFFERS(buf)
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01001582 if (bufIsChangedNotTerm(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584#ifdef FEAT_GUI_MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001585 if (!keep_termcap)
1586 starttermcap(); // don't want a message box here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01001588 msg_puts(_("[No write since last change]\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#ifdef FEAT_GUI_MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001590 if (!keep_termcap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 stoptermcap();
1592#endif
1593 break;
1594 }
1595
Bram Moolenaar049ca592019-03-17 16:59:44 +01001596 // This windgoto is required for when the '\n' resulted in a "delete line
1597 // 1" command to the terminal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (!swapping_screen())
1599 windgoto(msg_row, msg_col);
1600 cursor_on();
1601 (void)call_shell(cmd, SHELL_COOKED | flags);
1602 did_check_timestamps = FALSE;
1603 need_check_timestamps = TRUE;
1604
1605 /*
1606 * put the message cursor at the end of the screen, avoids wait_return()
1607 * to overwrite the text that the external command showed
1608 */
1609 if (!swapping_screen())
1610 {
1611 msg_row = Rows - 1;
1612 msg_col = 0;
1613 }
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (autocmd_busy)
1616 {
1617 if (msg_silent == 0)
1618 redraw_later_clear();
1619 }
1620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 /*
1623 * For ":sh" there is no need to call wait_return(), just redraw.
1624 * Also for the Win32 GUI (the output is in a console window).
1625 * Otherwise there is probably text on the screen that the user wants
1626 * to read before redrawing, so call wait_return().
1627 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001628#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
1629# ifdef VIMDLL
1630 if (!gui.in_use)
1631# endif
1632 {
1633 if (cmd == NULL
Bram Moolenaar4f974752019-02-17 17:44:42 +01001634# ifdef MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001635 || (keep_termcap && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001637 )
1638 {
1639 if (msg_silent == 0)
1640 redraw_later_clear();
1641 need_wait_return = FALSE;
1642 }
1643 else
1644 {
1645 /*
1646 * If we switch screens when starttermcap() is called, we
1647 * really want to wait for "hit return to continue".
1648 */
1649 save_nwr = no_wait_return;
1650 if (swapping_screen())
1651 no_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652# ifdef AMIGA
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001653 wait_return(term_console ? -1 : msg_silent == 0); // see below
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654# else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001655 wait_return(msg_silent == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001657 no_wait_return = save_nwr;
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01001660#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
1662#ifdef MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001663 if (!keep_termcap) // if keep_termcap is TRUE didn't stop termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664#endif
Bram Moolenaar049ca592019-03-17 16:59:44 +01001665 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
1667 /*
1668 * In an Amiga window redrawing is caused by asking the window size.
1669 * If we got an interrupt this will not work. The chance that the
1670 * window size is wrong is very small, but we need to redraw the
1671 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1672 * but it saves an extra redraw.
1673 */
1674#ifdef AMIGA
1675 if (skip_redraw) /* ':' hit in wait_return() */
1676 {
1677 if (msg_silent == 0)
1678 redraw_later_clear();
1679 }
1680 else if (term_console)
1681 {
1682 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1683 if (got_int && msg_silent == 0)
1684 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1685 else
1686 must_redraw = 0; /* no extra redraw needed */
1687 }
1688#endif
1689 }
1690
1691 /* display any error messages now */
1692 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001693
Bram Moolenaarade00832006-03-10 21:46:58 +00001694 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
Bram Moolenaar06640892018-10-09 21:49:33 +02001697#if !defined(UNIX)
1698 static char_u *
1699find_pipe(char_u *cmd)
1700{
1701 char_u *p;
1702 int inquote = FALSE;
1703
1704 for (p = cmd; *p != NUL; ++p)
1705 {
1706 if (!inquote && *p == '|')
1707 return p;
1708 if (*p == '"')
1709 inquote = !inquote;
1710 else if (rem_backslash(p))
1711 ++p;
1712 }
1713 return NULL;
1714}
1715#endif
1716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717/*
1718 * Create a shell command from a command string, input redirection file and
1719 * output redirection file.
1720 * Returns an allocated string with the shell command, or NULL for failure.
1721 */
1722 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001723make_filter_cmd(
1724 char_u *cmd, /* command */
1725 char_u *itmp, /* NULL or name of input file */
1726 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 char_u *buf;
1729 long_u len;
1730
Bram Moolenaar53076832015-12-31 19:53:21 +01001731#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001732 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001733 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001734
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001735 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001736 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1737 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001738 if (is_fish_shell)
1739 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1740 else
1741#endif
1742 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (itmp != NULL)
1744 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1745 if (otmp != NULL)
1746 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001747 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (buf == NULL)
1749 return NULL;
1750
Bram Moolenaar53076832015-12-31 19:53:21 +01001751#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001753 * Put braces around the command (for concatenated commands) when
1754 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001756 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001757 {
1758 if (is_fish_shell)
1759 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1760 else
1761 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1762 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001763 else
1764 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (itmp != NULL)
1766 {
1767 STRCAT(buf, " < ");
1768 STRCAT(buf, itmp);
1769 }
1770#else
1771 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001772 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 * the use of commands in a pipe.
1774 */
1775 STRCPY(buf, cmd);
1776 if (itmp != NULL)
1777 {
1778 char_u *p;
1779
1780 /*
1781 * If there is a pipe, we have to put the '<' in front of it.
1782 * Don't do this when 'shellquote' is not empty, otherwise the
1783 * redirection would be inside the quotes.
1784 */
1785 if (*p_shq == NUL)
1786 {
Bram Moolenaar06640892018-10-09 21:49:33 +02001787 p = find_pipe(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (p != NULL)
1789 *p = NUL;
1790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1792 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 if (*p_shq == NUL)
1794 {
Bram Moolenaar06640892018-10-09 21:49:33 +02001795 p = find_pipe(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (p != NULL)
1797 {
1798 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1799 STRCAT(buf, p);
1800 }
1801 }
1802 }
1803#endif
1804 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001805 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 return buf;
1808}
1809
1810/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001811 * Append output redirection for file "fname" to the end of string buffer
1812 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 * Works with the 'shellredir' and 'shellpipe' options.
1814 * The caller should make sure that there is enough room:
1815 * STRLEN(opt) + STRLEN(fname) + 3
1816 */
1817 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001818append_redir(
1819 char_u *buf,
1820 int buflen,
1821 char_u *opt,
1822 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001825 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001827 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001828 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001830 {
1831 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001833 if (p[1] == '%') /* skip %% */
1834 ++p;
1835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (p != NULL)
1837 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001838 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1839 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1840 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001843 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 " %s %s",
1846#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848#endif
1849 (char *)opt, (char *)fname);
1850}
1851
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001852#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001854static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001855static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001856static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857static int viminfo_errcnt;
1858
1859 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001860no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 /* "vim -i NONE" does not read or write a viminfo file */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001863 return STRCMP(p_viminfofile, "NONE") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864}
1865
1866/*
1867 * Report an error for reading a viminfo file.
1868 * Count the number of errors. When there are more than 10, return TRUE.
1869 */
1870 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001871viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001873 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1874 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001875 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001876 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1877 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001878 emsg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (++viminfo_errcnt >= 10)
1880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 emsg(_("E136: viminfo: Too many errors, skipping rest of file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 return TRUE;
1883 }
1884 return FALSE;
1885}
1886
1887/*
1888 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001889 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001892read_viminfo(
1893 char_u *file, /* file name or NULL to use default name */
1894 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 FILE *fp;
1897 char_u *fname;
1898
1899 if (no_viminfo())
1900 return FAIL;
1901
Bram Moolenaard812df62008-11-09 12:46:09 +00001902 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (fname == NULL)
1904 return FAIL;
1905 fp = mch_fopen((char *)fname, READBIN);
1906
1907 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001908 {
1909 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001910 smsg(_("Reading viminfo file \"%s\"%s%s%s"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00001911 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001912 (flags & VIF_WANT_INFO) ? _(" info") : "",
1913 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1914 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001915 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001916 verbose_leave();
1917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 vim_free(fname);
1920 if (fp == NULL)
1921 return FAIL;
1922
1923 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001924 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 return OK;
1928}
1929
1930/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001931 * Write the viminfo file. The old one is read in first so that effectively a
1932 * merge of current info and old info is done. This allows multiple vims to
1933 * run simultaneously, without losing any marks etc.
1934 * If "forceit" is TRUE, then the old file is not read in, and only internal
1935 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
1937 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001938write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939{
1940 char_u *fname;
1941 FILE *fp_in = NULL; /* input viminfo file, if any */
1942 FILE *fp_out = NULL; /* output viminfo file */
1943 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001944 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945#if defined(UNIX) || defined(VMS)
1946 mode_t umask_save;
1947#endif
1948#ifdef UNIX
1949 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001950 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001952#ifdef MSWIN
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001953 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 if (no_viminfo())
1957 return;
1958
1959 fname = viminfo_filename(file); /* may set to default if NULL */
1960 if (fname == NULL)
1961 return;
1962
1963 fp_in = mch_fopen((char *)fname, READBIN);
1964 if (fp_in == NULL)
1965 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001966 int fd;
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 /* if it does exist, but we can't read it, don't try writing */
1969 if (mch_stat((char *)fname, &st_new) == 0)
1970 goto end;
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001971
1972 /* Create the new .viminfo non-accessible for others, because it may
1973 * contain text from non-accessible documents. It is up to the user to
1974 * widen access (e.g. to a group). This may also fail if there is a
1975 * race condition, then just give up. */
1976 fd = mch_open((char *)fname,
1977 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1978 if (fd < 0)
1979 goto end;
1980 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 else
1983 {
1984 /*
1985 * There is an existing viminfo file. Create a temporary file to
1986 * write the new viminfo into, in the same directory as the
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001987 * existing viminfo file, which will be renamed once all writing is
1988 * successful.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 */
1990#ifdef UNIX
1991 /*
1992 * For Unix we check the owner of the file. It's not very nice to
1993 * overwrite a user's viminfo file after a "su root", with a
1994 * viminfo file that the user can't read.
1995 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001996 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001997 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001999 if (mch_stat((char *)fname, &st_old) == 0
2000 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00002001 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 ? (st_old.st_mode & 0200)
2003 : (st_old.st_gid == getgid()
2004 ? (st_old.st_mode & 0020)
2005 : (st_old.st_mode & 0002))))
2006 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002007 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
2009 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002010 semsg(_("E137: Viminfo file is not writable: %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002012 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 goto end;
2014 }
2015#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002016#ifdef MSWIN
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002017 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002018 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002022 * Make tempname, find one that does not exist yet.
2023 * Beware of a race condition: If someone logs out and all Vim
2024 * instances exit at the same time a temp file might be created between
2025 * stat() and open(). Use mch_open() with O_EXCL to avoid that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * May try twice: Once normal and once with shortname set, just in
2027 * case somebody puts his viminfo file in an 8.3 filesystem.
2028 */
2029 for (;;)
2030 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002031 int next_char = 'z';
2032 char_u *wp;
2033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 tempname = buf_modname(
2035#ifdef UNIX
2036 shortname,
2037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
2040 fname,
2041#ifdef VMS
2042 (char_u *)"-tmp",
2043#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046 FALSE);
2047 if (tempname == NULL) /* out of memory */
2048 break;
2049
2050 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002051 * Try a series of names. Change one character, just before
2052 * the extension. This should also work for an 8.3
2053 * file name, when after adding the extension it still is
2054 * the same file as the original.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002056 wp = tempname + STRLEN(tempname) - 5;
2057 if (wp < gettail(tempname)) /* empty file name? */
2058 wp = gettail(tempname);
2059 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002061 /*
2062 * Check if tempfile already exists. Never overwrite an
2063 * existing file!
2064 */
2065 if (mch_stat((char *)tempname, &st_new) == 0)
2066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002069 * Check if tempfile is same as original file. May happen
2070 * when modname() gave the same file back. E.g. silly
2071 * link, or file name-length reached. Try again with
2072 * shortname set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002074 if (!shortname && st_new.st_dev == st_old.st_dev
2075 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002077 VIM_CLEAR(tempname);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002078 shortname = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 break;
2080 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002083 else
2084 {
2085 /* Try creating the file exclusively. This may fail if
2086 * another Vim tries to do it at the same time. */
2087#ifdef VMS
2088 /* fdopen() fails for some reason */
2089 umask_save = umask(077);
2090 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2091 (void)umask(umask_save);
2092#else
2093 int fd;
2094
2095 /* Use mch_open() to be able to use O_NOFOLLOW and set file
2096 * protection:
2097 * Unix: same as original file, but strip s-bit. Reset
2098 * umask to avoid it getting in the way.
2099 * Others: r&w for user only. */
2100# ifdef UNIX
2101 umask_save = umask(0);
2102 fd = mch_open((char *)tempname,
2103 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
2104 (int)((st_old.st_mode & 0777) | 0600));
2105 (void)umask(umask_save);
2106# else
2107 fd = mch_open((char *)tempname,
2108 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
2109# endif
2110 if (fd < 0)
2111 {
2112 fp_out = NULL;
2113# ifdef EEXIST
2114 /* Avoid trying lots of names while the problem is lack
Bram Moolenaar94747162019-02-10 21:55:26 +01002115 * of permission, only retry if the file already
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002116 * exists. */
2117 if (errno != EEXIST)
2118 break;
2119# endif
2120 }
2121 else
2122 fp_out = fdopen(fd, WRITEBIN);
2123#endif /* VMS */
2124 if (fp_out != NULL)
2125 break;
2126 }
2127
2128 /* Assume file exists, try again with another name. */
2129 if (next_char == 'a' - 1)
2130 {
2131 /* They all exist? Must be something wrong! Don't write
2132 * the viminfo file then. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002133 semsg(_("E929: Too many viminfo temp files, like %s!"),
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002134 tempname);
2135 break;
2136 }
2137 *wp = next_char;
2138 --next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002140
2141 if (tempname != NULL)
2142 break;
2143 /* continue if shortname was set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145
Bram Moolenaara5792f52005-11-23 21:25:05 +00002146#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002147 if (tempname != NULL && fp_out != NULL)
2148 {
2149 stat_T tmp_st;
2150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 /*
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002152 * Make sure the original owner can read/write the tempfile and
2153 * otherwise preserve permissions, making sure the group matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002155 if (mch_stat((char *)tempname, &tmp_st) >= 0)
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002156 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002157 if (st_old.st_uid != tmp_st.st_uid)
2158 /* Changing the owner might fail, in which case the
2159 * file will now owned by the current user, oh well. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002160 vim_ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002161 if (st_old.st_gid != tmp_st.st_gid
2162 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
2163 /* can't set the group to what it should be, remove
2164 * group permissions */
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002165 (void)mch_setperm(tempname, 0600);
2166 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002167 else
2168 /* can't stat the file, set conservative permissions */
2169 (void)mch_setperm(tempname, 0600);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002171#endif
Bram Moolenaare0aa23f2017-11-26 17:08:03 +01002172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
2174 /*
2175 * Check if the new viminfo file can be written to.
2176 */
2177 if (fp_out == NULL)
2178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002179 semsg(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002180 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (fp_in != NULL)
2182 fclose(fp_in);
2183 goto end;
2184 }
2185
2186 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002187 {
2188 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002189 smsg(_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002190 verbose_leave();
2191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002194 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
Bram Moolenaar927030a2016-03-15 18:23:55 +01002196 if (fclose(fp_out) == EOF)
2197 ++viminfo_errcnt;
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (fp_in != NULL)
2200 {
2201 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002202
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002203 /* In case of an error keep the original viminfo file. Otherwise
2204 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002205 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002206 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002207 if (vim_rename(tempname, fname) == -1)
2208 {
2209 ++viminfo_errcnt;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 semsg(_("E886: Can't rename viminfo file to %s!"), fname);
Bram Moolenaar927030a2016-03-15 18:23:55 +01002211 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002212# ifdef MSWIN
Bram Moolenaar927030a2016-03-15 18:23:55 +01002213 /* If the viminfo file was hidden then also hide the new file. */
2214 else if (hidden)
2215 mch_hide(fname);
2216# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002217 }
2218 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 mch_remove(tempname);
2220 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222end:
2223 vim_free(fname);
2224 vim_free(tempname);
2225}
2226
2227/*
2228 * Get the viminfo file name to use.
2229 * If "file" is given and not empty, use it (has already been expanded by
2230 * cmdline functions).
2231 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2232 * expand environment variables.
2233 * Returns an allocated string. NULL when out of memory.
2234 */
2235 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002236viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238 if (file == NULL || *file == NUL)
2239 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002240 if (*p_viminfofile != NUL)
2241 file = p_viminfofile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2243 {
2244#ifdef VIMINFO_FILE2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245# ifdef VMS
2246 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2247# else
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002248# ifdef MSWIN
2249 /* Use $VIM only if $HOME is the default "C:/". */
2250 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
2251 && mch_getenv((char_u *)"HOME") == NULL)
2252# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (mch_getenv((char_u *)"HOME") == NULL)
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255# endif
2256 {
2257 /* don't use $VIM when not available. */
2258 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2259 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2260 file = (char_u *)VIMINFO_FILE2;
2261 else
2262 file = (char_u *)VIMINFO_FILE;
2263 }
2264 else
2265#endif
2266 file = (char_u *)VIMINFO_FILE;
2267 }
2268 expand_env(file, NameBuff, MAXPATHL);
2269 file = NameBuff;
2270 }
2271 return vim_strsave(file);
2272}
2273
2274/*
2275 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2276 */
2277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002278do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 int eof = FALSE;
2281 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002282 int merge = FALSE;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002283 int do_copy_marks = FALSE;
2284 garray_T buflist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2287 return;
2288 vir.vir_fd = fp_in;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 vir.vir_conv.vc_type = CONV_NONE;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002290 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002291 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
2293 if (fp_in != NULL)
2294 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002295 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002296 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002297 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002298 {
2299 /* Registers and marks are read and kept separate from what
2300 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002301 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002302 prepare_viminfo_marks();
2303 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002304
Bram Moolenaard812df62008-11-09 12:46:09 +00002305 eof = read_viminfo_up_to_marks(&vir,
2306 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002307 merge = TRUE;
2308 }
2309 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 /* Skip info, find start of marks */
2311 while (!(eof = viminfo_readline(&vir))
2312 && vir.vir_line[0] != '>')
2313 ;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002314
2315 do_copy_marks = (flags &
2316 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002318
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (fp_out != NULL)
2320 {
2321 /* Write the info: */
2322 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2323 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002324 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002325 write_viminfo_version(fp_out);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002326 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 write_viminfo_search_pattern(fp_out);
2329 write_viminfo_sub_string(fp_out);
2330#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002331 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#endif
2333 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002334 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#ifdef FEAT_EVAL
2336 write_viminfo_varlist(fp_out);
2337#endif
2338 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002339 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002341 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002342
2343 if (do_copy_marks)
2344 ga_init2(&buflist, sizeof(buf_T *), 50);
2345 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002347
2348 if (do_copy_marks)
2349 {
2350 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2351 if (fp_out != NULL)
2352 ga_clear(&buflist);
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 vim_free(vir.vir_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (vir.vir_conv.vc_type != CONV_NONE)
2357 convert_setup(&vir.vir_conv, NULL, NULL);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002358 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359}
2360
2361/*
2362 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2363 * first part of the viminfo file which contains everything but the marks that
2364 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2365 */
2366 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002367read_viminfo_up_to_marks(
2368 vir_T *virp,
2369 int forceit,
2370 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 int eof;
2373 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002374 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002377 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 eof = viminfo_readline(virp);
2381 while (!eof && virp->vir_line[0] != '>')
2382 {
2383 switch (virp->vir_line[0])
2384 {
2385 /* Characters reserved for future expansion, ignored now */
2386 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 case '^': /* to be defined */
2388 case '<': /* long line - ignored */
2389 /* A comment or empty line. */
2390 case NUL:
2391 case '\r':
2392 case '\n':
2393 case '#':
2394 eof = viminfo_readline(virp);
2395 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002396 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002397 eof = read_viminfo_barline(virp, got_encoding,
2398 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002401 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 eof = viminfo_encoding(virp);
2403 break;
2404 case '!': /* global variable */
2405#ifdef FEAT_EVAL
2406 eof = read_viminfo_varlist(virp, writing);
2407#else
2408 eof = viminfo_readline(virp);
2409#endif
2410 break;
2411 case '%': /* entry for buffer list */
2412 eof = read_viminfo_bufferlist(virp, writing);
2413 break;
2414 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002415 /* When registers are in bar lines skip the old style register
2416 * lines. */
2417 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2418 eof = read_viminfo_register(virp, forceit);
2419 else
2420 do {
2421 eof = viminfo_readline(virp);
2422 } while (!eof && (virp->vir_line[0] == TAB
2423 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 break;
2425 case '/': /* Search string */
2426 case '&': /* Substitute search string */
2427 case '~': /* Last search string, followed by '/' or '&' */
2428 eof = read_viminfo_search_pattern(virp, forceit);
2429 break;
2430 case '$':
2431 eof = read_viminfo_sub_string(virp, forceit);
2432 break;
2433 case ':':
2434 case '?':
2435 case '=':
2436 case '@':
2437#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002438 /* When history is in bar lines skip the old style history
2439 * lines. */
2440 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2441 eof = read_viminfo_history(virp, writing);
2442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002444 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 break;
2446 case '-':
2447 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002448 /* When file marks are in bar lines skip the old style lines. */
2449 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2450 eof = read_viminfo_filemark(virp, forceit);
2451 else
2452 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 break;
2454 default:
2455 if (viminfo_error("E575: ", _("Illegal starting char"),
2456 virp->vir_line))
2457 eof = TRUE;
2458 else
2459 eof = viminfo_readline(virp);
2460 break;
2461 }
2462 }
2463
2464#ifdef FEAT_CMDHIST
2465 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002466 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002467 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#endif
2469
2470 /* Change file names to buffer numbers for fmarks. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002471 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 fmarks_check_names(buf);
2473
2474 return eof;
2475}
2476
2477/*
2478 * Compare the 'encoding' value in the viminfo file with the current value of
2479 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2480 * conversion of text with iconv() in viminfo_readstring().
2481 */
2482 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002483viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 char_u *p;
2486 int i;
2487
2488 if (get_viminfo_parameter('c') != 0)
2489 {
2490 p = vim_strchr(virp->vir_line, '=');
2491 if (p != NULL)
2492 {
2493 /* remove trailing newline */
2494 ++p;
2495 for (i = 0; vim_isprintc(p[i]); ++i)
2496 ;
2497 p[i] = NUL;
2498
2499 convert_setup(&virp->vir_conv, p, p_enc);
2500 }
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 return viminfo_readline(virp);
2503}
2504
2505/*
2506 * Read a line from the viminfo file.
2507 * Returns TRUE for end-of-file;
2508 */
2509 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002510viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2513}
2514
2515/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002516 * Check string read from viminfo file.
2517 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * - replace CTRL-V CTRL-V with CTRL-V
2519 * - replace CTRL-V 'n' with '\n'
2520 *
2521 * Check for a long line as written by viminfo_writestring().
2522 *
2523 * Return the string in allocated memory (NULL when out of memory).
2524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002526viminfo_readstring(
2527 vir_T *virp,
2528 int off, /* offset for virp->vir_line */
2529 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531 char_u *retval;
2532 char_u *s, *d;
2533 long len;
2534
2535 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2536 {
2537 len = atol((char *)virp->vir_line + off + 1);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002538 retval = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (retval == NULL)
2540 {
2541 /* Line too long? File messed up? Skip next line. */
2542 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2543 return NULL;
2544 }
2545 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2546 s = retval + 1; /* Skip the leading '<' */
2547 }
2548 else
2549 {
2550 retval = vim_strsave(virp->vir_line + off);
2551 if (retval == NULL)
2552 return NULL;
2553 s = retval;
2554 }
2555
2556 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2557 d = retval;
2558 while (*s != NUL && *s != '\n')
2559 {
2560 if (s[0] == Ctrl_V && s[1] != NUL)
2561 {
2562 if (s[1] == 'n')
2563 *d++ = '\n';
2564 else
2565 *d++ = Ctrl_V;
2566 s += 2;
2567 }
2568 else
2569 *d++ = *s++;
2570 }
2571 *d = NUL;
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2574 {
2575 d = string_convert(&virp->vir_conv, retval, NULL);
2576 if (d != NULL)
2577 {
2578 vim_free(retval);
2579 retval = d;
2580 }
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 return retval;
2584}
2585
2586/*
2587 * write string to viminfo file
2588 * - replace CTRL-V with CTRL-V CTRL-V
2589 * - replace '\n' with CTRL-V 'n'
2590 * - add a '\n' at the end
2591 *
2592 * For a long line:
2593 * - write " CTRL-V <length> \n " in first line
2594 * - write " < <string> \n " in second line
2595 */
2596 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002597viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 int c;
2600 char_u *s;
2601 int len = 0;
2602
2603 for (s = p; *s != NUL; ++s)
2604 {
2605 if (*s == Ctrl_V || *s == '\n')
2606 ++len;
2607 ++len;
2608 }
2609
2610 /* If the string will be too long, write its length and put it in the next
2611 * line. Take into account that some room is needed for what comes before
2612 * the string (e.g., variable name). Add something to the length for the
2613 * '<', NL and trailing NUL. */
2614 if (len > LSIZE / 2)
2615 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2616
2617 while ((c = *p++) != NUL)
2618 {
2619 if (c == Ctrl_V || c == '\n')
2620 {
2621 putc(Ctrl_V, fd);
2622 if (c == '\n')
2623 c = 'n';
2624 }
2625 putc(c, fd);
2626 }
2627 putc('\n', fd);
2628}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002629
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002630/*
2631 * Write a string in quotes that barline_parse() can read back.
2632 * Breaks the line in less than LSIZE pieces when needed.
2633 * Returns remaining characters in the line.
2634 */
2635 int
2636barline_writestring(FILE *fd, char_u *s, int remaining_start)
2637{
2638 char_u *p;
2639 int remaining = remaining_start;
2640 int len = 2;
2641
2642 /* Count the number of characters produced, including quotes. */
2643 for (p = s; *p != NUL; ++p)
2644 {
2645 if (*p == NL)
2646 len += 2;
2647 else if (*p == '"' || *p == '\\')
2648 len += 2;
2649 else
2650 ++len;
2651 }
Bram Moolenaar25709572016-08-26 20:41:16 +02002652 if (len > remaining - 2)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002653 {
2654 fprintf(fd, ">%d\n|<", len);
2655 remaining = LSIZE - 20;
2656 }
2657
2658 putc('"', fd);
2659 for (p = s; *p != NUL; ++p)
2660 {
2661 if (*p == NL)
2662 {
2663 putc('\\', fd);
2664 putc('n', fd);
2665 --remaining;
2666 }
2667 else if (*p == '"' || *p == '\\')
2668 {
2669 putc('\\', fd);
2670 putc(*p, fd);
2671 --remaining;
2672 }
2673 else
2674 putc(*p, fd);
2675 --remaining;
2676
2677 if (remaining < 3)
2678 {
2679 putc('\n', fd);
2680 putc('|', fd);
2681 putc('<', fd);
2682 /* Leave enough space for another continuation. */
2683 remaining = LSIZE - 20;
2684 }
2685 }
2686 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002687 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002688}
2689
2690/*
2691 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002692 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002693 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002694 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002695 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002696barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002697{
2698 char_u *p = text;
2699 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002700 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002701 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002702 int i;
2703 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002704 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002705 char_u *sconv;
2706 int converted;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002707
2708 while (*p == ',')
2709 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002710 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002711 if (ga_grow(values, 1) == FAIL)
2712 break;
2713 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002714
2715 if (*p == '>')
2716 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002717 /* Need to read a continuation line. Put strings in allocated
2718 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002719 if (!allocated)
2720 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002721 for (i = 0; i < values->ga_len; ++i)
2722 {
2723 bval_T *vp = (bval_T *)(values->ga_data) + i;
2724
2725 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002726 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002727 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2728 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002729 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002730 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002731 allocated = TRUE;
2732 }
2733
2734 if (vim_isdigit(p[1]))
2735 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002736 size_t len;
2737 size_t todo;
2738 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002739
2740 /* String value was split into lines that are each shorter
2741 * than LSIZE:
2742 * |{bartype},>{length of "{text}{text2}"}
2743 * |<"{text1}
2744 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002745 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002746 */
2747 ++p;
2748 len = getdigits(&p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002749 buf = alloc(len + 1);
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002750 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002751 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002752 p = buf;
2753 for (todo = len; todo > 0; todo -= n)
2754 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002755 eof = viminfo_readline(virp);
2756 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002757 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002758 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002759 /* File was truncated or garbled. Read another line if
2760 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002761 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002762 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002763 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002764 /* Get length of text, excluding |< and NL chars. */
2765 n = STRLEN(virp->vir_line);
2766 while (n > 0 && (virp->vir_line[n - 1] == NL
2767 || virp->vir_line[n - 1] == CAR))
2768 --n;
2769 n -= 2;
2770 if (n > todo)
2771 {
2772 /* more values follow after the string */
2773 nextp = virp->vir_line + 2 + todo;
2774 n = todo;
2775 }
2776 mch_memmove(p, virp->vir_line + 2, n);
2777 p += n;
2778 }
2779 *p = NUL;
2780 p = buf;
2781 }
2782 else
2783 {
2784 /* Line ending in ">" continues in the next line:
2785 * |{bartype},{lots of values},>
2786 * |<{value},{value}
2787 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002788 eof = viminfo_readline(virp);
2789 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002790 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002791 /* File was truncated or garbled. Read another line if
2792 * this one starts with '|'. */
2793 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002794 p = virp->vir_line + 2;
2795 }
2796 }
2797
2798 if (isdigit(*p))
2799 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002800 value->bv_type = BVAL_NR;
2801 value->bv_nr = getdigits(&p);
2802 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002803 }
2804 else if (*p == '"')
2805 {
2806 int len = 0;
2807 char_u *s = p;
2808
2809 /* Unescape special characters in-place. */
2810 ++p;
2811 while (*p != '"')
2812 {
2813 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002814 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002815 if (*p == '\\')
2816 {
2817 ++p;
2818 if (*p == 'n')
2819 s[len++] = '\n';
2820 else
2821 s[len++] = *p;
2822 ++p;
2823 }
2824 else
2825 s[len++] = *p++;
2826 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002827 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002828 s[len] = NUL;
2829
Bram Moolenaar01227092016-06-11 14:47:40 +02002830 converted = FALSE;
2831 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2832 {
2833 sconv = string_convert(&virp->vir_conv, s, NULL);
2834 if (sconv != NULL)
2835 {
2836 if (s == buf)
2837 vim_free(s);
2838 s = sconv;
2839 buf = s;
2840 converted = TRUE;
2841 }
2842 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002843
Bram Moolenaar01227092016-06-11 14:47:40 +02002844 /* Need to copy in allocated memory if the string wasn't allocated
2845 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002846 if (s != buf && allocated)
2847 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002848 value->bv_string = s;
2849 value->bv_type = BVAL_STRING;
2850 value->bv_len = len;
Bram Moolenaar13505972019-01-24 15:04:48 +01002851 value->bv_allocated = allocated || converted;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002852 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002853 if (nextp != NULL)
2854 {
2855 /* values following a long string */
2856 p = nextp;
2857 nextp = NULL;
2858 }
2859 }
2860 else if (*p == ',')
2861 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002862 value->bv_type = BVAL_EMPTY;
2863 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002864 }
2865 else
2866 break;
2867 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002868 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002869}
2870
2871 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002872read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002873{
2874 char_u *p = virp->vir_line + 1;
2875 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002876 garray_T values;
2877 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002878 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002879 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002880
2881 /* The format is: |{bartype},{value},...
2882 * For a very long string:
2883 * |{bartype},>{length of "{text}{text2}"}
2884 * |<{text1}
2885 * |<{text2},{value}
2886 * For a long line not using a string
2887 * |{bartype},{lots of values},>
2888 * |<{value},{value}
2889 */
2890 if (*p == '<')
2891 {
2892 /* Continuation line of an unrecognized item. */
2893 if (writing)
2894 ga_add_string(&virp->vir_barlines, virp->vir_line);
2895 }
2896 else
2897 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002898 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002899 bartype = getdigits(&p);
2900 switch (bartype)
2901 {
2902 case BARTYPE_VERSION:
2903 /* Only use the version when it comes before the encoding.
2904 * If it comes later it was copied by a Vim version that
2905 * doesn't understand the version. */
2906 if (!got_encoding)
2907 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002908 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002909 vp = (bval_T *)values.ga_data;
2910 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2911 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002912 }
2913 break;
2914
2915 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002916 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002917 handle_viminfo_history(&values, writing);
2918 break;
2919
2920 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002921 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002922 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002923 break;
2924
Bram Moolenaar2d358992016-06-12 21:20:54 +02002925 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002926 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002927 handle_viminfo_mark(&values, force);
2928 break;
2929
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002930 default:
2931 /* copy unrecognized line (for future use) */
2932 if (writing)
2933 ga_add_string(&virp->vir_barlines, virp->vir_line);
2934 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002935 for (i = 0; i < values.ga_len; ++i)
2936 {
2937 vp = (bval_T *)values.ga_data + i;
2938 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2939 vim_free(vp->bv_string);
2940 }
2941 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002942 }
2943
Bram Moolenaarecefe712016-06-20 12:50:17 +02002944 if (read_next)
2945 return viminfo_readline(virp);
2946 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002947}
2948
2949 static void
2950write_viminfo_version(FILE *fp_out)
2951{
2952 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2953 BARTYPE_VERSION, VIMINFO_VERSION);
2954}
2955
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002956 static void
2957write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2958{
2959 int i;
2960 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002961 int seen_useful = FALSE;
2962 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002963
2964 if (gap->ga_len > 0)
2965 {
2966 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2967
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002968 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002969 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002970 {
2971 line = ((char **)(gap->ga_data))[i];
2972 if (seen_useful || line[1] != '<')
2973 {
2974 fputs(line, fp_out);
2975 seen_useful = TRUE;
2976 }
2977 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002978 }
2979}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980#endif /* FEAT_VIMINFO */
2981
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002982/*
2983 * Return the current time in seconds. Calls time(), unless test_settime()
2984 * was used.
2985 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002986 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002987vim_time(void)
2988{
2989# ifdef FEAT_EVAL
2990 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2991# else
2992 return time(NULL);
2993# endif
2994}
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996/*
2997 * Implementation of ":fixdel", also used by get_stty().
2998 * <BS> resulting <Del>
2999 * ^? ^H
3000 * not ^? ^?
3001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003003do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 char_u *p;
3006
3007 p = find_termcode((char_u *)"kb");
3008 add_termcode((char_u *)"kD", p != NULL
3009 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
3010}
3011
3012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013print_line_no_prefix(
3014 linenr_T lnum,
3015 int use_number,
3016 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
Bram Moolenaar32526b32019-01-19 17:43:09 +01003018 char numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 if (curwin->w_p_nu || use_number)
3021 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003022 vim_snprintf(numbuf, sizeof(numbuf),
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003023 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003024 msg_puts_attr(numbuf, HL_ATTR(HLF_N)); /* Highlight line nrs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003026 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027}
3028
3029/*
3030 * Print a text line. Also in silent mode ("ex -s").
3031 */
3032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003033print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 int save_silent = silent_mode;
3036
Bram Moolenaard29459b2016-08-26 22:29:11 +02003037 /* apply :filter /pat/ */
3038 if (message_filtered(ml_get(lnum)))
3039 return;
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003042 silent_mode = FALSE;
3043 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
3044 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (save_silent)
3046 {
3047 msg_putchar('\n');
3048 cursor_on(); /* msg_start() switches it off */
3049 out_flush();
3050 silent_mode = save_silent;
3051 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00003052 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053}
3054
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003056rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003057{
3058 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02003059 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003060
Bram Moolenaar7e283842013-05-30 11:43:15 +02003061 buf = curbuf;
3062 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003063 /* buffer changed, don't change name now */
3064 if (buf != curbuf)
3065 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003066#ifdef FEAT_EVAL
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003067 if (aborting()) /* autocmds may abort script processing */
3068 return FAIL;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003069#endif
3070 /*
3071 * The name of the current buffer will be changed.
3072 * A new (unlisted) buffer entry needs to be made to hold the old file
3073 * name, which will become the alternate file name.
3074 * But don't set the alternate file name if the buffer didn't have a
3075 * name.
3076 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02003077 fname = curbuf->b_ffname;
3078 sfname = curbuf->b_sfname;
3079 xfname = curbuf->b_fname;
3080 curbuf->b_ffname = NULL;
3081 curbuf->b_sfname = NULL;
3082 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003083 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02003084 curbuf->b_ffname = fname;
3085 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003086 return FAIL;
3087 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02003088 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003089 if (xfname != NULL && *xfname != NUL)
3090 {
3091 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
3092 if (buf != NULL && !cmdmod.keepalt)
3093 curwin->w_alt_fnum = buf->b_fnum;
3094 }
3095 vim_free(fname);
3096 vim_free(sfname);
Bram Moolenaar7e283842013-05-30 11:43:15 +02003097 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003098
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003099 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02003100 DO_AUTOCHDIR;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003101 return OK;
3102}
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
3105 * ":file[!] [fname]".
3106 */
3107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003108ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003110 /* ":0file" removes the file name. Check for illegal uses ":3file",
3111 * "0file name", etc. */
3112 if (eap->addr_count > 0
3113 && (*eap->arg != NUL
3114 || eap->line2 > 0
3115 || eap->addr_count > 1))
3116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003117 emsg(_(e_invarg));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003118 return;
3119 }
3120
3121 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003123 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 return;
Bram Moolenaarfc089602018-06-24 16:53:35 +02003125 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
Bram Moolenaarfc089602018-06-24 16:53:35 +02003127
3128 // print file name if no argument or 'F' is not in 'shortmess'
3129 if (*eap->arg == NUL || !shortmess(SHM_FILEINFO))
Bram Moolenaar426dd022016-03-15 15:09:29 +01003130 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * ":update".
3135 */
3136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003137ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
3139 if (curbufIsChanged())
3140 (void)do_write(eap);
3141}
3142
3143/*
3144 * ":write" and ":saveas".
3145 */
3146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaarb7316892019-05-01 18:08:42 +02003149 if (eap->cmdidx == CMD_saveas)
3150 {
3151 // :saveas does not take a range, uses all lines.
3152 eap->line1 = 1;
3153 eap->line2 = curbuf->b_ml.ml_line_count;
3154 }
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (eap->usefilter) /* input lines to shell command */
3157 do_bang(1, eap, FALSE, TRUE, FALSE);
3158 else
3159 (void)do_write(eap);
3160}
3161
3162/*
3163 * write current buffer to file 'eap->arg'
3164 * if 'eap->append' is TRUE, append to the file
3165 *
3166 * if *eap->arg == NUL write to current file
3167 *
3168 * return FAIL for failure, OK otherwise
3169 */
3170 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 int other;
3174 char_u *fname = NULL; /* init to shut up gcc */
3175 char_u *ffname;
3176 int retval = FAIL;
3177 char_u *free_fname = NULL;
3178#ifdef FEAT_BROWSE
3179 char_u *browse_file = NULL;
3180#endif
3181 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003182 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184 if (not_writing()) /* check 'write' option */
3185 return FAIL;
3186
3187 ffname = eap->arg;
3188#ifdef FEAT_BROWSE
3189 if (cmdmod.browse)
3190 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003191 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 NULL, NULL, NULL, curbuf);
3193 if (browse_file == NULL)
3194 goto theend;
3195 ffname = browse_file;
3196 }
3197#endif
3198 if (*ffname == NUL)
3199 {
3200 if (eap->cmdidx == CMD_saveas)
3201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003202 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 goto theend;
3204 }
3205 other = FALSE;
3206 }
3207 else
3208 {
3209 fname = ffname;
3210 free_fname = fix_fname(ffname);
3211 /*
3212 * When out-of-memory, keep unexpanded file name, because we MUST be
3213 * able to write the file in this situation.
3214 */
3215 if (free_fname != NULL)
3216 ffname = free_fname;
3217 other = otherfile(ffname);
3218 }
3219
3220 /*
3221 * If we have a new file, put its name in the list of alternate file names.
3222 */
3223 if (other)
3224 {
3225 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3226 || eap->cmdidx == CMD_saveas)
3227 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3228 else
3229 alt_buf = buflist_findname(ffname);
3230 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3231 {
3232 /* Overwriting a file that is loaded in another buffer is not a
3233 * good idea. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003234 emsg(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 goto theend;
3236 }
3237 }
3238
3239 /*
3240 * Writing to the current file is not allowed in readonly mode
3241 * and a file name is required.
3242 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3243 */
3244 if (!other && (
3245#ifdef FEAT_QUICKFIX
3246 bt_dontwrite_msg(curbuf) ||
3247#endif
3248 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3249 goto theend;
3250
3251 if (!other)
3252 {
3253 ffname = curbuf->b_ffname;
3254 fname = curbuf->b_fname;
3255 /*
3256 * Not writing the whole file is only allowed with '!'.
3257 */
3258 if ( (eap->line1 != 1
3259 || eap->line2 != curbuf->b_ml.ml_line_count)
3260 && !eap->forceit
3261 && !eap->append
3262 && !p_wa)
3263 {
3264#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3265 if (p_confirm || cmdmod.confirm)
3266 {
3267 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3268 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3269 goto theend;
3270 eap->forceit = TRUE;
3271 }
3272 else
3273#endif
3274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003275 emsg(_("E140: Use ! to write partial buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 goto theend;
3277 }
3278 }
3279 }
3280
3281 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3282 {
3283 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 buf_T *was_curbuf = curbuf;
3286
3287 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003288 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003289#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003291#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
3295 /* buffer changed, don't change name now */
3296 retval = FAIL;
3297 goto theend;
3298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 /* Exchange the file names for the current and the alternate
3300 * buffer. This makes it look like we are now editing the buffer
3301 * under the new name. Must be done before buf_write(), because
3302 * if there is no file name and 'cpo' contains 'F', it will set
3303 * the file name. */
3304 fname = alt_buf->b_fname;
3305 alt_buf->b_fname = curbuf->b_fname;
3306 curbuf->b_fname = fname;
3307 fname = alt_buf->b_ffname;
3308 alt_buf->b_ffname = curbuf->b_ffname;
3309 curbuf->b_ffname = fname;
3310 fname = alt_buf->b_sfname;
3311 alt_buf->b_sfname = curbuf->b_sfname;
3312 curbuf->b_sfname = fname;
3313 buf_name_changed(curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003316 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (!alt_buf->b_p_bl)
3318 {
3319 alt_buf->b_p_bl = TRUE;
3320 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3321 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003322#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003324#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 /* buffer changed, don't write the file */
3329 retval = FAIL;
3330 goto theend;
3331 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003332
3333 /* If 'filetype' was empty try detecting it now. */
3334 if (*curbuf->b_p_ft == NUL)
3335 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003336 if (au_has_group((char_u *)"filetypedetect"))
3337 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003338 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003339 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003340 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003341
3342 /* Autocommands may have changed buffer names, esp. when
3343 * 'autochdir' is set. */
3344 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
Bram Moolenaar5c719942016-07-09 23:40:45 +02003347 name_was_missing = curbuf->b_ffname == NULL;
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3350 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003351
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003352 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003353 if (eap->cmdidx == CMD_saveas)
3354 {
3355 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003356 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003357 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003358 redraw_tabline = TRUE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003359 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003360 }
3361
3362 /* Change directories when the 'acd' option is set and the file name
3363 * got changed or set. */
3364 if (eap->cmdidx == CMD_saveas || name_was_missing)
Bram Moolenaar6f470022018-04-10 18:47:20 +02003365 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367
3368theend:
3369#ifdef FEAT_BROWSE
3370 vim_free(browse_file);
3371#endif
3372 vim_free(free_fname);
3373 return retval;
3374}
3375
3376/*
3377 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3378 * BF_NEW or BF_READERR, check for overwriting current file.
3379 * May set eap->forceit if a dialog says it's OK to overwrite.
3380 * Return OK if it's OK, FAIL if it is not.
3381 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003382 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003383check_overwrite(
3384 exarg_T *eap,
3385 buf_T *buf,
3386 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003388 char_u *ffname, /* full path version of fname */
3389 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 /*
3392 * write to other file or b_flags set or not writing the whole file:
3393 * overwriting only allowed with '!'
3394 */
3395 if ( (other
3396 || (buf->b_flags & BF_NOTEDITED)
3397 || ((buf->b_flags & BF_NEW)
3398 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3399 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003401#ifdef FEAT_QUICKFIX
3402 && !bt_nofile(buf)
3403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 && vim_fexists(ffname))
3405 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003406 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003408#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003409 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003410 if (mch_isdir(ffname))
3411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003412 semsg(_(e_isadir2), ffname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003413 return FAIL;
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#endif
3416#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003417 if (p_confirm || cmdmod.confirm)
3418 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003419 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003421 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3422 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3423 return FAIL;
3424 eap->forceit = TRUE;
3425 }
3426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003429 emsg(_(e_exists));
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003430 return FAIL;
3431 }
3432 }
3433
3434 /* For ":w! filename" check that no swap file exists for "filename". */
3435 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003437 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003438 char_u *p;
3439 int r;
3440 char_u *swapname;
3441
3442 /* We only try the first entry in 'directory', without checking if
3443 * it's writable. If the "." directory is not writable the write
3444 * will probably fail anyway.
3445 * Use 'shortname' of the current buffer, since there is no buffer
3446 * for the written file. */
3447 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003448 {
3449 dir = alloc(5);
3450 if (dir == NULL)
3451 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003452 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003453 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003454 else
3455 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003456 dir = alloc(MAXPATHL);
3457 if (dir == NULL)
3458 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003459 p = p_dir;
3460 copy_option_part(&p, dir, MAXPATHL, ",");
3461 }
3462 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003463 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003464 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003465 if (r)
3466 {
3467#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3468 if (p_confirm || cmdmod.confirm)
3469 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003470 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003471
3472 dialog_msg(buff,
3473 _("Swap file \"%s\" exists, overwrite anyway?"),
3474 swapname);
3475 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3476 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003477 {
3478 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003479 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003480 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003481 eap->forceit = TRUE;
3482 }
3483 else
3484#endif
3485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003486 semsg(_("E768: Swap file exists: %s (:silent! overrides)"),
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003487 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003488 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003489 return FAIL;
3490 }
3491 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003492 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 }
3495 return OK;
3496}
3497
3498/*
3499 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3500 */
3501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003502ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 int i;
3505
3506 if (eap->cmd[1] == 'n')
3507 i = curwin->w_arg_idx + (int)eap->line2;
3508 else
3509 i = curwin->w_arg_idx - (int)eap->line2;
3510 eap->line1 = 1;
3511 eap->line2 = curbuf->b_ml.ml_line_count;
3512 if (do_write(eap) != FAIL)
3513 do_argfile(eap, i);
3514}
3515
3516/*
3517 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3518 */
3519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003520do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 buf_T *buf;
3523 int error = 0;
3524 int save_forceit = eap->forceit;
3525
3526 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3527 exiting = TRUE;
3528
Bram Moolenaar29323592016-07-24 22:04:11 +02003529 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaar7a760922018-02-19 23:10:02 +01003531#ifdef FEAT_TERMINAL
3532 if (exiting && term_job_running(buf->b_term))
3533 {
3534 no_write_message_nobang(buf);
3535 ++error;
3536 }
3537 else
3538#endif
Bram Moolenaar059db5c2017-10-15 22:42:23 +02003539 if (bufIsChanged(buf) && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 /*
3542 * Check if there is a reason the buffer cannot be written:
3543 * 1. if the 'write' option is set
3544 * 2. if there is no file name (even after browsing)
3545 * 3. if the 'readonly' is set (even after a dialog)
3546 * 4. if overwriting is allowed (even after a dialog)
3547 */
3548 if (not_writing())
3549 {
3550 ++error;
3551 break;
3552 }
3553#ifdef FEAT_BROWSE
3554 /* ":browse wall": ask for file name if there isn't one */
3555 if (buf->b_ffname == NULL && cmdmod.browse)
3556 browse_save_fname(buf);
3557#endif
3558 if (buf->b_ffname == NULL)
3559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003560 semsg(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 ++error;
3562 }
3563 else if (check_readonly(&eap->forceit, buf)
3564 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3565 FALSE) == FAIL)
3566 {
3567 ++error;
3568 }
3569 else
3570 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003571 bufref_T bufref;
3572
3573 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (buf_write_all(buf, eap->forceit) == FAIL)
3575 ++error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003577 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580 eap->forceit = save_forceit; /* check_overwrite() may set it */
3581 }
3582 }
3583 if (exiting)
3584 {
3585 if (!error)
3586 getout(0); /* exit Vim */
3587 not_exiting();
3588 }
3589}
3590
3591/*
3592 * Check the 'write' option.
3593 * Return TRUE and give a message when it's not st.
3594 */
3595 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003596not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
3598 if (p_write)
3599 return FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003600 emsg(_("E142: File not written: Writing is disabled by 'write' option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return TRUE;
3602}
3603
3604/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003605 * Check if a buffer is read-only (either 'readonly' option is set or file is
3606 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3607 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 */
3609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003610check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003612 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003613
3614 /* Handle a file being readonly when the 'readonly' option is set or when
3615 * the file exists and permissions are read-only.
3616 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3617 * important for device checks but not here. */
3618 if (!*forceit && (buf->b_p_ro
3619 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3620 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3623 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3624 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003625 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaar5386a122007-06-28 20:02:32 +00003627 if (buf->b_p_ro)
3628 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3629 buf->b_fname);
3630 else
3631 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 +00003632 buf->b_fname);
3633
3634 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3635 {
3636 /* Set forceit, to force the writing of a readonly file */
3637 *forceit = TRUE;
3638 return FALSE;
3639 }
3640 else
3641 return TRUE;
3642 }
3643 else
3644#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003645 if (buf->b_p_ro)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003646 emsg(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003647 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 semsg(_("E505: \"%s\" is read-only (add ! to override)"),
Bram Moolenaar5386a122007-06-28 20:02:32 +00003649 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return TRUE;
3651 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return FALSE;
3654}
3655
3656/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003657 * Try to abandon the current file and edit a new or existing file.
3658 * "fnum" is the number of the file, if zero use "ffname_arg"/"sfname_arg".
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003659 * "lnum" is the line number for the cursor in the new file (if non-zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 *
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003661 * Return:
3662 * GETFILE_ERROR for "normal" error,
3663 * GETFILE_NOT_WRITTEN for "not written" error,
3664 * GETFILE_SAME_FILE for success
3665 * GETFILE_OPEN_OTHER for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003668getfile(
3669 int fnum,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003670 char_u *ffname_arg,
3671 char_u *sfname_arg,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003672 int setpm,
3673 linenr_T lnum,
3674 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003676 char_u *ffname = ffname_arg;
3677 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 int other;
3679 int retval;
3680 char_u *free_me = NULL;
3681
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003682 if (text_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003683 return GETFILE_ERROR;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003684 if (curbuf_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003685 return GETFILE_ERROR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687 if (fnum == 0)
3688 {
3689 /* make ffname full path, set sfname */
3690 fname_expand(curbuf, &ffname, &sfname);
3691 other = otherfile(ffname);
3692 free_me = ffname; /* has been allocated, free() later */
3693 }
3694 else
3695 other = (fnum != curbuf->b_fnum);
3696
3697 if (other)
3698 ++no_wait_return; /* don't wait for autowrite message */
Bram Moolenaareb44a682017-08-03 22:44:55 +02003699 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3701 {
3702#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3703 if (p_confirm && p_write)
3704 dialog_changed(curbuf, FALSE);
3705 if (curbufIsChanged())
3706#endif
3707 {
3708 if (other)
3709 --no_wait_return;
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02003710 no_write_message();
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003711 retval = GETFILE_NOT_WRITTEN; /* file has been changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 goto theend;
3713 }
3714 }
3715 if (other)
3716 --no_wait_return;
3717 if (setpm)
3718 setpcmark();
3719 if (!other)
3720 {
3721 if (lnum != 0)
3722 curwin->w_cursor.lnum = lnum;
3723 check_cursor_lnum();
3724 beginline(BL_SOL | BL_FIX);
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003725 retval = GETFILE_SAME_FILE; /* it's in the same file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02003728 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003729 curwin) == OK)
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003730 retval = GETFILE_OPEN_OTHER; /* opened another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 else
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003732 retval = GETFILE_ERROR; /* error encountered */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734theend:
3735 vim_free(free_me);
3736 return retval;
3737}
3738
3739/*
3740 * start editing a new file
3741 *
3742 * fnum: file number; if zero use ffname/sfname
3743 * ffname: the file name
3744 * - full path if sfname used,
3745 * - any file name if sfname is NULL
3746 * - empty string to re-edit with the same file name (but may be
3747 * in a different directory)
3748 * - NULL to start an empty buffer
3749 * sfname: the short file name (or NULL)
3750 * eap: contains the command to be executed after loading the file and
3751 * forced 'ff' and 'fenc'
3752 * newlnum: if > 0: put cursor on this line number (if possible)
3753 * if ECMD_LASTL: use last position in loaded file
3754 * if ECMD_LAST: use last position in all files
3755 * if ECMD_ONE: use first line
3756 * flags:
3757 * ECMD_HIDE: if TRUE don't free the current buffer
3758 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3759 * ECMD_OLDBUF: use existing buffer if it exists
3760 * ECMD_FORCEIT: ! used for Ex command
3761 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003762 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003763 * window, NULL when splitting the window first. When not NULL info
3764 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 *
3766 * return FAIL for failure, OK otherwise
3767 */
3768 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003769do_ecmd(
3770 int fnum,
3771 char_u *ffname,
3772 char_u *sfname,
3773 exarg_T *eap, /* can be NULL! */
3774 linenr_T newlnum,
3775 int flags,
3776 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
3778 int other_file; /* TRUE if editing another file */
3779 int oldbuf; /* TRUE if using existing buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 int auto_buf = FALSE; /* TRUE if autocommands brought us
3781 into the buffer unexpectedly */
3782 char_u *new_name = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003783#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003784 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785#endif
3786 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003787 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003788 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 char_u *free_fname = NULL;
3790#ifdef FEAT_BROWSE
3791 char_u *browse_file = NULL;
3792#endif
3793 int retval = FAIL;
3794 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003795 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 linenr_T topline = 0;
3797 int newcol = -1;
3798 int solcol = -1;
3799 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003801#ifdef FEAT_SPELL
3802 int did_get_winopts = FALSE;
3803#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003804 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003805 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +01003806 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
3808 if (eap != NULL)
3809 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003810 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 if (fnum != 0)
3813 {
3814 if (fnum == curbuf->b_fnum) /* file is already being edited */
3815 return OK; /* nothing to do */
3816 other_file = TRUE;
3817 }
3818 else
3819 {
3820#ifdef FEAT_BROWSE
3821 if (cmdmod.browse)
3822 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003823 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003824# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003825 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003826# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003827 au_has_group((char_u *)"FileExplorer"))
3828 {
3829 /* No browsing supported but we do have the file explorer:
3830 * Edit the directory. */
3831 if (ffname == NULL || !mch_isdir(ffname))
3832 ffname = (char_u *)".";
3833 }
3834 else
3835 {
3836 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003838 if (browse_file == NULL)
3839 goto theend;
3840 ffname = browse_file;
3841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843#endif
3844 /* if no short name given, use ffname for short name */
3845 if (sfname == NULL)
3846 sfname = ffname;
3847#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003848 if (sfname != NULL)
3849 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#endif
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3853 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if (ffname == NULL)
3856 other_file = TRUE;
3857 /* there is no file name */
3858 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3859 other_file = FALSE;
3860 else
3861 {
3862 if (*ffname == NUL) /* re-edit with same file name */
3863 {
3864 ffname = curbuf->b_ffname;
3865 sfname = curbuf->b_fname;
3866 }
3867 free_fname = fix_fname(ffname); /* may expand to full path name */
3868 if (free_fname != NULL)
3869 ffname = free_fname;
3870 other_file = otherfile(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872 }
3873
3874 /*
3875 * if the file was changed we may not be allowed to abandon it
3876 * - if we are going to re-edit the same file
3877 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3878 */
3879 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3880 || (curbuf->b_nwindows == 1
3881 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003882 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3883 | (other_file ? 0 : CCGD_MULTWIN)
3884 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3885 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 if (fnum == 0 && other_file && ffname != NULL)
3888 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3889 goto theend;
3890 }
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 /*
3893 * End Visual mode before switching to another buffer, so the text can be
3894 * copied into the GUI selection buffer.
3895 */
3896 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003898#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003899 if ((command != NULL || newlnum > (linenr_T)0)
3900 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3901 {
3902 int len;
3903 char_u *p;
3904
3905 /* Set v:swapcommand for the SwapExists autocommands. */
3906 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003907 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003908 else
3909 len = 30;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003910 p = alloc(len);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003911 if (p != NULL)
3912 {
3913 if (command != NULL)
3914 vim_snprintf((char *)p, len, ":%s\r", command);
3915 else
3916 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3917 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3918 did_set_swapcommand = TRUE;
3919 vim_free(p);
3920 }
3921 }
3922#endif
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 /*
3925 * If we are starting to edit another file, open a (new) buffer.
3926 * Otherwise we re-use the current buffer.
3927 */
3928 if (other_file)
3929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if (!(flags & ECMD_ADDBUF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003932 if (!cmdmod.keepalt)
3933 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003934 if (oldwin != NULL)
3935 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937
3938 if (fnum)
3939 buf = buflist_findnr(fnum);
3940 else
3941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (flags & ECMD_ADDBUF)
3943 {
3944 linenr_T tlnum = 1L;
3945
3946 if (command != NULL)
3947 {
3948 tlnum = atol((char *)command);
3949 if (tlnum <= 0)
3950 tlnum = 1L;
3951 }
3952 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3953 goto theend;
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 buf = buflist_new(ffname, sfname, 0L,
3956 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003957
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003958 /* autocommands may change curwin and curbuf */
3959 if (oldwin != NULL)
3960 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003961 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963 if (buf == NULL)
3964 goto theend;
3965 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3966 {
3967 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 else /* existing memfile */
3970 {
3971 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003972 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003974 /* Check if autocommands made the buffer invalid or changed the
3975 * current buffer. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003976 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 goto theend;
3978#ifdef FEAT_EVAL
3979 if (aborting()) /* autocmds may abort script processing */
3980 goto theend;
3981#endif
3982 }
3983
3984 /* May jump to last used line number for a loaded buffer or when asked
3985 * for explicitly */
3986 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3987 {
3988 pos = buflist_findfpos(buf);
3989 newlnum = pos->lnum;
3990 solcol = pos->col;
3991 }
3992
3993 /*
3994 * Make the (new) buffer the one used by the current window.
3995 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3996 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003997 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 */
3999 if (buf != curbuf)
4000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 /*
4002 * Be careful: The autocommands may delete any buffer and change
4003 * the current buffer.
4004 * - If the buffer we are going to edit is deleted, give up.
4005 * - If the current buffer is deleted, prefer to load the new
4006 * buffer when loading a buffer is required. This avoids
4007 * loading another buffer which then must be closed again.
4008 * - If we ended up in the new buffer already, need to skip a few
4009 * things, set auto_buf.
4010 */
4011 if (buf->b_fname != NULL)
4012 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004013 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004015 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004017 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 delbuf_msg(new_name); /* frees new_name */
4019 goto theend;
4020 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004021#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 if (aborting()) /* autocmds may abort script processing */
4023 {
4024 vim_free(new_name);
4025 goto theend;
4026 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (buf == curbuf) /* already in new buffer */
4029 auto_buf = TRUE;
4030 else
4031 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02004032 win_T *the_curwin = curwin;
4033
4034 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004035 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004036 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004037 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02004038
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004039 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 buf_copy_options(buf, BCO_ENTER);
4041
Bram Moolenaar5a497892016-09-03 16:29:04 +02004042 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02004043 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004044 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004045 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004046 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaar5a497892016-09-03 16:29:04 +02004048 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004049 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01004050
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004051#ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02004052 /* autocmds may abort script processing */
4053 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 vim_free(new_name);
4056 goto theend;
4057 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004060 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004062 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 delbuf_msg(new_name); /* frees new_name */
4064 goto theend;
4065 }
4066 if (buf == curbuf) /* already in new buffer */
4067 auto_buf = TRUE;
4068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004070#ifdef FEAT_SYN_HL
4071 /*
4072 * <VN> We could instead free the synblock
4073 * and re-attach to buffer, perhaps.
4074 */
Bram Moolenaarf1d13472017-07-11 18:28:46 +02004075 if (curwin->w_buffer == NULL
4076 || curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02004077 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 curwin->w_buffer = buf;
4080 curbuf = buf;
4081 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004082
4083 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
4084 if (!oldbuf && eap != NULL)
4085 {
4086 set_file_options(TRUE, eap);
4087 set_forced_fenc(eap);
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090
4091 /* May get the window options from the last time this buffer
4092 * was in this window (or another window). If not used
4093 * before, reset the local window options to the global
4094 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004095 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004096#ifdef FEAT_SPELL
4097 did_get_winopts = TRUE;
4098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004101 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004102 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105 curwin->w_pcmark.lnum = 1;
4106 curwin->w_pcmark.col = 0;
4107 }
4108 else /* !other_file */
4109 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02004110 if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 oldbuf = (flags & ECMD_OLDBUF);
4114 }
4115
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004116 /* Don't redraw until the cursor is in the right line, otherwise
4117 * autocommands may cause ml_get errors. */
4118 ++RedrawingDisabled;
4119 did_inc_redrawing_disabled = TRUE;
4120
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004121 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4123 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004124 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 else
4127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 /* Don't make a buffer listed if it's a help buffer. Useful when
4129 * using CTRL-O to go back to a help file. */
4130 if (!curbuf->b_help)
4131 set_buflisted(TRUE);
4132 }
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /* If autocommands change buffers under our fingers, forget about
4135 * editing the file. */
4136 if (buf != curbuf)
4137 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004138#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (aborting()) /* autocmds may abort script processing */
4140 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 /* Since we are starting to edit a file, consider the filetype to be
4144 * unset. Helps for when an autocommand changes files and expects syntax
4145 * highlighting to work in the other file. */
4146 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148/*
4149 * other_file oldbuf
4150 * FALSE FALSE re-edit same file, buffer is re-used
4151 * FALSE TRUE re-edit same file, nothing changes
4152 * TRUE FALSE start editing new file, new buffer
4153 * TRUE TRUE start editing in existing buffer (nothing to do)
4154 */
4155 if (!other_file && !oldbuf) /* re-use the buffer */
4156 {
4157 set_last_cursor(curwin); /* may set b_last_cursor */
4158 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4159 {
4160 newlnum = curwin->w_cursor.lnum;
4161 solcol = curwin->w_cursor.col;
4162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 buf = curbuf;
4164 if (buf->b_fname != NULL)
4165 new_name = vim_strsave(buf->b_fname);
4166 else
4167 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004168 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004169
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004170 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4171 {
4172 /* Save all the text, so that the reload can be undone.
4173 * Sync first so that this is a separate undo-able action. */
4174 u_sync(FALSE);
4175 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4176 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004177 {
4178 vim_free(new_name);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004179 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004180 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004181 u_unchanged(curbuf);
4182 buf_freeall(curbuf, BFA_KEEP_UNDO);
4183
4184 /* tell readfile() not to clear or reload undo info */
4185 readfile_flags = READ_KEEP_UNDO;
4186 }
4187 else
4188 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004189
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 /* If autocommands deleted the buffer we were going to re-edit, give
4191 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004192 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
4194 delbuf_msg(new_name); /* frees new_name */
4195 goto theend;
4196 }
4197 vim_free(new_name);
4198
4199 /* If autocommands change buffers under our fingers, forget about
4200 * re-editing the file. Should do the buf_clear_file(), but perhaps
4201 * the autocommands changed the buffer... */
4202 if (buf != curbuf)
4203 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004204#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (aborting()) /* autocmds may abort script processing */
4206 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#endif
4208 buf_clear_file(curbuf);
4209 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4210 curbuf->b_op_end.lnum = 0;
4211 }
4212
4213/*
4214 * If we get here we are sure to start editing
4215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /* Assume success now */
4217 retval = OK;
4218
4219 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * Check if we are editing the w_arg_idx file in the argument list.
4221 */
4222 check_arg_idx(curwin);
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (!auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 /*
4227 * Set cursor and init window before reading the file and executing
4228 * autocommands. This allows for the autocommands to position the
4229 * cursor.
4230 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004231 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004234 /* It's possible that all lines in the buffer changed. Need to update
4235 * automatic folding for all windows where it's used. */
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004236 {
4237 win_T *win;
4238 tabpage_T *tp;
4239
4240 FOR_ALL_TAB_WINDOWS(tp, win)
4241 if (win->w_buffer == curbuf)
4242 foldUpdateAll(win);
4243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#endif
4245
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004246 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004247 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 /*
4250 * Careful: open_buffer() and apply_autocmds() may change the current
4251 * buffer and window.
4252 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004253 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 topline = curwin->w_topline;
4255 if (!oldbuf) /* need to read the file */
4256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4259
4260 /*
4261 * Open the buffer and read the file.
4262 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004263#if defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004264 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 retval = FAIL;
4266#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004267 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#endif
4269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (swap_exists_action == SEA_QUIT)
4271 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004272 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else
4275 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004276 /* Read the modelines, but only to set window-local options. Any
4277 * buffer-local options have already been set and may have been
4278 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004279 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4282 &retval);
4283 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4284 &retval);
4285 }
4286 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Bram Moolenaareab316b2015-03-24 11:46:30 +01004288 /* If autocommands change the cursor position or topline, we should
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004289 * keep it. Also when it moves within a line. But not when it moves
4290 * to the first non-blank. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004291 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004293 char_u *text = ml_get_curline();
4294
4295 if (curwin->w_cursor.lnum != orig_pos.lnum
4296 || curwin->w_cursor.col != (int)(skipwhite(text) - text))
4297 {
4298 newlnum = curwin->w_cursor.lnum;
4299 newcol = curwin->w_cursor.col;
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 if (curwin->w_topline == topline)
4303 topline = 0;
4304
4305 /* Even when cursor didn't move we need to recompute topline. */
4306 changed_line_abv_curs();
4307
4308#ifdef FEAT_TITLE
4309 maketitle();
4310#endif
4311 }
4312
4313#ifdef FEAT_DIFF
4314 /* Tell the diff stuff that this buffer is new and/or needs updating.
4315 * Also needed when re-editing the same buffer, because unloading will
4316 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004317 if (curwin->w_p_diff)
4318 {
4319 diff_buf_add(curbuf);
4320 diff_invalidate(curbuf);
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322#endif
4323
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004324#ifdef FEAT_SPELL
4325 /* If the window options were changed may need to set the spell language.
4326 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004327 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4328 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004329#endif
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (command == NULL)
4332 {
4333 if (newcol >= 0) /* position set by autocommands */
4334 {
4335 curwin->w_cursor.lnum = newlnum;
4336 curwin->w_cursor.col = newcol;
4337 check_cursor();
4338 }
4339 else if (newlnum > 0) /* line number from caller or old position */
4340 {
4341 curwin->w_cursor.lnum = newlnum;
4342 check_cursor_lnum();
4343 if (solcol >= 0 && !p_sol)
4344 {
4345 /* 'sol' is off: Use last known column. */
4346 curwin->w_cursor.col = solcol;
4347 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 curwin->w_set_curswant = TRUE;
4350 }
4351 else
4352 beginline(BL_SOL | BL_FIX);
4353 }
4354 else /* no line number, go to last line in Ex mode */
4355 {
4356 if (exmode_active)
4357 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4358 beginline(BL_WHITE | BL_FIX);
4359 }
4360 }
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /* Check if cursors in other windows on the same buffer are still valid */
4363 check_lnums(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365 /*
4366 * Did not read the file, need to show some info about the file.
4367 * Do this after setting the cursor.
4368 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004369 if (oldbuf && !auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 {
4371 int msg_scroll_save = msg_scroll;
4372
4373 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4374 * message. */
4375 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4376 msg_scroll = FALSE;
4377 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4378 check_for_delay(FALSE);
4379 msg_start();
4380 msg_scroll = msg_scroll_save;
4381 msg_scrolled_ign = TRUE;
4382
Bram Moolenaar426dd022016-03-15 15:09:29 +01004383 if (!shortmess(SHM_FILEINFO))
4384 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 msg_scrolled_ign = FALSE;
4387 }
4388
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004389#ifdef FEAT_VIMINFO
4390 curbuf->b_last_used = vim_time();
4391#endif
4392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (command != NULL)
4394 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4395
4396#ifdef FEAT_KEYMAP
4397 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004398 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#endif
4400
4401 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004402 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (!skip_redraw)
4404 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004405 n = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 if (topline == 0 && command == NULL)
Bram Moolenaar375e3392019-01-31 18:26:10 +01004407 *so_ptr = 9999; // force cursor halfway the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 curwin->w_scbind_pos = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01004410 *so_ptr = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4412 }
4413
4414 if (p_im)
4415 need_start_insertmode = TRUE;
4416
Bram Moolenaar15833232018-02-03 18:33:17 +01004417#ifdef FEAT_AUTOCHDIR
4418 /* Change directories when the 'acd' option is set and we aren't already in
4419 * that directory (should already be done above). Expect getcwd() to be
4420 * faster than calling shorten_fnames() unnecessarily. */
4421 if (p_acd && curbuf->b_ffname != NULL)
4422 {
4423 char_u curdir[MAXPATHL];
4424 char_u filedir[MAXPATHL];
4425
4426 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1);
4427 *gettail_sep(filedir) = NUL;
4428 if (mch_dirname(curdir, MAXPATHL) != FAIL
4429 && vim_fnamecmp(curdir, filedir) != 0)
4430 do_autochdir();
4431 }
4432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01004434#if defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004435 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004438 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004439 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440# endif
4441 }
4442#endif
4443
4444theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004445 if (did_inc_redrawing_disabled)
4446 --RedrawingDisabled;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004447#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004448 if (did_set_swapcommand)
4449 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#ifdef FEAT_BROWSE
4452 vim_free(browse_file);
4453#endif
4454 vim_free(free_fname);
4455 return retval;
4456}
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004459delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004461 semsg(_("E143: Autocommands unexpectedly deleted new buffer %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 name == NULL ? (char_u *)"" : name);
4463 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004464 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004465 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004468static int append_indent = 0; /* autoindent for first line */
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470/*
4471 * ":insert" and ":append", also used by ":change"
4472 */
4473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004474ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 char_u *theline;
4477 int did_undo = FALSE;
4478 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004479 int indent = 0;
4480 char_u *p;
4481 int vcol;
4482 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004484 /* the ! flag toggles autoindent */
4485 if (eap->forceit)
4486 curbuf->b_p_ai = !curbuf->b_p_ai;
4487
4488 /* First autoindent comes from the line we start on */
4489 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4490 append_indent = get_indent_lnum(lnum);
4491
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (eap->cmdidx != CMD_append)
4493 --lnum;
4494
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004495 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004496 if (empty && lnum == 1)
4497 lnum = 0;
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 State = INSERT; /* behave like in Insert mode */
4500 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4501 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004502
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004503 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 msg_scroll = TRUE;
4506 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004507 if (curbuf->b_p_ai)
4508 {
4509 if (append_indent >= 0)
4510 {
4511 indent = append_indent;
4512 append_indent = -1;
4513 }
4514 else if (lnum > 0)
4515 indent = get_indent_lnum(lnum);
4516 }
4517 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004519 {
4520 /* No getline() function, use the lines that follow. This ends
4521 * when there is no more. */
4522 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4523 break;
4524 p = vim_strchr(eap->nextcmd, NL);
4525 if (p == NULL)
4526 p = eap->nextcmd + STRLEN(eap->nextcmd);
4527 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4528 if (*p != NUL)
4529 ++p;
4530 eap->nextcmd = p;
4531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004533 {
4534 int save_State = State;
4535
4536 /* Set State to avoid the cursor shape to be set to INSERT mode
4537 * when getline() returns. */
4538 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 theline = eap->getline(
4540#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004541 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004543 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004544 State = save_State;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004547 if (theline == NULL)
4548 break;
4549
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004550 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4551 if (ex_keep_indent)
4552 append_indent = indent;
4553
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004554 /* Look for the "." after automatic indent. */
4555 vcol = 0;
4556 for (p = theline; indent > vcol; ++p)
4557 {
4558 if (*p == ' ')
4559 ++vcol;
4560 else if (*p == TAB)
4561 vcol += 8 - vcol % 8;
4562 else
4563 break;
4564 }
4565 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004566 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4567 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
4569 vim_free(theline);
4570 break;
4571 }
4572
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004573 /* don't use autoindent if nothing was typed. */
4574 if (p[0] == NUL)
4575 theline[0] = NUL;
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 did_undo = TRUE;
4578 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004579 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581 vim_free(theline);
4582 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004583
4584 if (empty)
4585 {
4586 ml_delete(2L, FALSE);
4587 empty = FALSE;
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 State = NORMAL;
4591
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004592 if (eap->forceit)
4593 curbuf->b_p_ai = !curbuf->b_p_ai;
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004596 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 * "end" is set to lnum when something has been appended, otherwise
4598 * it is the same than "start" -- Acevedo */
4599 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4600 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4601 if (eap->cmdidx != CMD_append)
4602 --curbuf->b_op_start.lnum;
4603 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4604 ? lnum : curbuf->b_op_start.lnum;
4605 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4606 curwin->w_cursor.lnum = lnum;
4607 check_cursor_lnum();
4608 beginline(BL_SOL | BL_FIX);
4609
4610 need_wait_return = FALSE; /* don't use wait_return() now */
4611 ex_no_reprint = TRUE;
4612}
4613
4614/*
4615 * ":change"
4616 */
4617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004618ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
4620 linenr_T lnum;
4621
4622 if (eap->line2 >= eap->line1
4623 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4624 return;
4625
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004626 /* the ! flag toggles autoindent */
4627 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4628 append_indent = get_indent_lnum(eap->line1);
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4631 {
4632 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4633 break;
4634 ml_delete(eap->line1, FALSE);
4635 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004636
4637 /* make sure the cursor is not beyond the end of the file now */
4638 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4640
4641 /* ":append" on the line above the deleted lines. */
4642 eap->line2 = eap->line1;
4643 ex_append(eap);
4644}
4645
4646 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004647ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
4649 char_u *x;
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004650 long bigness;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004651 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 int minus = 0;
4653 linenr_T start, end, curs, i;
4654 int j;
4655 linenr_T lnum = eap->line2;
4656
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004657 /* Vi compatible: ":z!" uses display height, without a count uses
4658 * 'scroll' */
4659 if (eap->forceit)
4660 bigness = curwin->w_height;
Bram Moolenaar459ca562016-11-10 18:16:33 +01004661 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004662 bigness = curwin->w_height - 3;
Bram Moolenaar631abc32014-02-22 22:27:47 +01004663 else
4664 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (bigness < 1)
4666 bigness = 1;
4667
4668 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004669 kind = x;
4670 if (*kind == '-' || *kind == '+' || *kind == '='
4671 || *kind == '^' || *kind == '.')
4672 ++x;
4673 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 ++x;
4675
4676 if (*x != 0)
4677 {
4678 if (!VIM_ISDIGIT(*x))
4679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004680 emsg(_("E144: non-numeric argument to :z"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return;
4682 }
4683 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004684 {
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004685 bigness = atol((char *)x);
4686
4687 /* bigness could be < 0 if atol(x) overflows. */
4688 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
4689 bigness = 2 * curbuf->b_ml.ml_line_count;
4690
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004691 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004692 if (*kind == '=')
4693 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004697 /* the number of '-' and '+' multiplies the distance */
4698 if (*kind == '-' || *kind == '+')
4699 for (x = kind + 1; *x == *kind; ++x)
4700 ;
4701
4702 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004705 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4706 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004707 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 break;
4709
4710 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 start = lnum - (bigness + 1) / 2 + 1;
4712 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 curs = lnum;
4714 minus = 1;
4715 break;
4716
4717 case '^':
4718 start = lnum - bigness * 2;
4719 end = lnum - bigness;
4720 curs = lnum - bigness;
4721 break;
4722
4723 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 start = lnum - (bigness + 1) / 2 + 1;
4725 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 curs = end;
4727 break;
4728
4729 default: /* '+' */
4730 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004731 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004732 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004733 else if (eap->addr_count == 0)
4734 ++start;
4735 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 curs = end;
4737 break;
4738 }
4739
4740 if (start < 1)
4741 start = 1;
4742
4743 if (end > curbuf->b_ml.ml_line_count)
4744 end = curbuf->b_ml.ml_line_count;
4745
4746 if (curs > curbuf->b_ml.ml_line_count)
4747 curs = curbuf->b_ml.ml_line_count;
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004748 else if (curs < 1)
4749 curs = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 for (i = start; i <= end; i++)
4752 {
4753 if (minus && i == lnum)
4754 {
4755 msg_putchar('\n');
4756
4757 for (j = 1; j < Columns; j++)
4758 msg_putchar('-');
4759 }
4760
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004761 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 if (minus && i == lnum)
4764 {
4765 msg_putchar('\n');
4766
4767 for (j = 1; j < Columns; j++)
4768 msg_putchar('-');
4769 }
4770 }
4771
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004772 if (curwin->w_cursor.lnum != curs)
4773 {
4774 curwin->w_cursor.lnum = curs;
4775 curwin->w_cursor.col = 0;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 ex_no_reprint = TRUE;
4778}
4779
4780/*
4781 * Check if the restricted flag is set.
4782 * If so, give an error message and return TRUE.
4783 * Otherwise, return FALSE.
4784 */
4785 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
4788 if (restricted)
4789 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01004790 emsg(_("E145: Shell commands and some functionality not allowed in rvim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return TRUE;
4792 }
4793 return FALSE;
4794}
4795
4796/*
4797 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4798 * If so, give an error message and return TRUE.
4799 * Otherwise, return FALSE.
4800 */
4801 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004802check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 if (secure)
4805 {
4806 secure = 2;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004807 emsg(_(e_curdir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 return TRUE;
4809 }
4810#ifdef HAVE_SANDBOX
4811 /*
4812 * In the sandbox more things are not allowed, including the things
4813 * disallowed in secure mode.
4814 */
4815 if (sandbox != 0)
4816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004817 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return TRUE;
4819 }
4820#endif
4821 return FALSE;
4822}
4823
4824static char_u *old_sub = NULL; /* previous substitute pattern */
4825static int global_need_beginline; /* call beginline() after ":g" */
4826
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004827/*
4828 * Flags that are kept between calls to :substitute.
4829 */
4830typedef struct {
4831 int do_all; /* do multiple substitutions per line */
4832 int do_ask; /* ask for confirmation */
4833 int do_count; /* count only */
4834 int do_error; /* if false, ignore errors */
4835 int do_print; /* print last line with subs. */
4836 int do_list; /* list last line with subs. */
4837 int do_number; /* list last line with line nr*/
4838 int do_ic; /* ignore case flag */
4839} subflags_T;
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841/* do_sub()
4842 *
4843 * Perform a substitution from line eap->line1 to line eap->line2 using the
4844 * command pointed to by eap->arg which should be of the form:
4845 *
4846 * /pattern/substitution/{flags}
4847 *
4848 * The usual escapes are supported as described in the regexp docs.
4849 */
4850 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
4853 linenr_T lnum;
4854 long i = 0;
4855 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004856 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4857 FALSE, FALSE, 0};
4858#ifdef FEAT_EVAL
4859 subflags_T subflags_save;
4860#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004861 int save_do_all; /* remember user specified 'g' flag */
4862 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4864 int delimiter;
4865 int sublen;
4866 int got_quit = FALSE;
4867 int got_match = FALSE;
4868 int temp;
4869 int which_pat;
4870 char_u *cmd;
4871 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004872 linenr_T first_line = 0; /* first changed line */
4873 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 * change */
4875 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4876 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004877 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004878 char_u *sub_firstline; /* allocated copy of first sub line */
4879 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004880 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004881 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004882#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004883 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 cmd = eap->arg;
4887 if (!global_busy)
4888 {
4889 sub_nsubs = 0;
4890 sub_nlines = 0;
4891 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004892 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (eap->cmdidx == CMD_tilde)
4895 which_pat = RE_LAST; /* use last used regexp */
4896 else
4897 which_pat = RE_SUBST; /* use last substitute regexp */
4898
4899 /* new pattern and substitution */
Bram Moolenaar1c465442017-03-12 20:10:05 +01004900 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4902 {
4903 /* don't accept alphanumeric for separator */
4904 if (isalpha(*cmd))
4905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004906 emsg(_("E146: Regular expressions can't be delimited by letters"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return;
4908 }
4909 /*
4910 * undocumented vi feature:
4911 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4912 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4913 */
4914 if (*cmd == '\\')
4915 {
4916 ++cmd;
4917 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004919 emsg(_(e_backslash));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 return;
4921 }
4922 if (*cmd != '&')
4923 which_pat = RE_SEARCH; /* use last '/' pattern */
4924 pat = (char_u *)""; /* empty search pattern */
4925 delimiter = *cmd++; /* remember delimiter character */
4926 }
4927 else /* find the end of the regexp */
4928 {
4929 which_pat = RE_LAST; /* use last used regexp */
4930 delimiter = *cmd++; /* remember delimiter character */
4931 pat = cmd; /* remember start of search pat */
4932 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4933 if (cmd[0] == delimiter) /* end delimiter found */
4934 *cmd++ = NUL; /* replace it with a NUL */
4935 }
4936
4937 /*
4938 * Small incompatibility: vi sees '\n' as end of the command, but in
4939 * Vim we want to use '\n' to find/substitute a NUL.
4940 */
4941 sub = cmd; /* remember the start of the substitution */
4942
4943 while (cmd[0])
4944 {
4945 if (cmd[0] == delimiter) /* end delimiter found */
4946 {
4947 *cmd++ = NUL; /* replace it with a NUL */
4948 break;
4949 }
4950 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4951 ++cmd;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004952 MB_PTR_ADV(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954
4955 if (!eap->skip)
4956 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004957 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4958 if (STRCMP(sub, "%") == 0
4959 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4960 {
4961 if (old_sub == NULL) /* there is no previous command */
4962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004963 emsg(_(e_nopresub));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004964 return;
4965 }
4966 sub = old_sub;
4967 }
4968 else
4969 {
4970 vim_free(old_sub);
4971 old_sub = vim_strsave(sub);
4972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975 else if (!eap->skip) /* use previous pattern and substitution */
4976 {
4977 if (old_sub == NULL) /* there is no previous command */
4978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004979 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 return;
4981 }
4982 pat = NULL; /* search_regcomp() will use previous pattern */
4983 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004984
4985 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4986 * last column after using "$". */
4987 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004990 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4991 * more efficient.
4992 * TODO: find a generic solution to make line-joining operations more
4993 * efficient, avoid allocating a string that grows in size.
4994 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004995 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004996 && *sub == NUL
4997 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4998 || *cmd == 'p' || *cmd == '#'))))
4999 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005000 linenr_T joined_lines_count;
5001
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005002 curwin->w_cursor.lnum = eap->line1;
5003 if (*cmd == 'l')
5004 eap->flags = EXFLAG_LIST;
5005 else if (*cmd == '#')
5006 eap->flags = EXFLAG_NR;
5007 else if (*cmd == 'p')
5008 eap->flags = EXFLAG_PRINT;
5009
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005010 /* The number of lines joined is the number of lines in the range plus
5011 * one. One less when the last line is included. */
5012 joined_lines_count = eap->line2 - eap->line1 + 1;
5013 if (eap->line2 < curbuf->b_ml.ml_line_count)
5014 ++joined_lines_count;
5015 if (joined_lines_count > 1)
5016 {
5017 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
5018 sub_nsubs = joined_lines_count - 1;
5019 sub_nlines = 1;
5020 (void)do_sub_msg(FALSE);
5021 ex_may_print(eap);
5022 }
5023
5024 if (!cmdmod.keeppatterns)
5025 save_re_pat(RE_SUBST, pat, p_magic);
5026#ifdef FEAT_CMDHIST
5027 /* put pattern in history */
5028 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
5029#endif
5030
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005031 return;
5032 }
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /*
5035 * Find trailing options. When '&' is used, keep old options.
5036 */
5037 if (*cmd == '&')
5038 ++cmd;
5039 else
5040 {
5041 if (!p_ed)
5042 {
5043 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005044 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005046 subflags.do_all = FALSE;
5047 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005049 subflags.do_error = TRUE;
5050 subflags.do_print = FALSE;
Bram Moolenaar94747162019-02-10 21:55:26 +01005051 subflags.do_list = FALSE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005052 subflags.do_count = FALSE;
5053 subflags.do_number = FALSE;
5054 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 while (*cmd)
5057 {
5058 /*
5059 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
5060 * 'r' is never inverted.
5061 */
5062 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005063 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005065 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005066 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005067 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005069 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else if (*cmd == 'r') /* use last used regexp */
5071 which_pat = RE_LAST;
5072 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005073 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005074 else if (*cmd == '#')
5075 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005076 subflags.do_print = TRUE;
5077 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005078 }
5079 else if (*cmd == 'l')
5080 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005081 subflags.do_print = TRUE;
5082 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005085 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005087 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 else
5089 break;
5090 ++cmd;
5091 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005092 if (subflags.do_count)
5093 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005095 save_do_all = subflags.do_all;
5096 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 /*
5099 * check for a trailing count
5100 */
5101 cmd = skipwhite(cmd);
5102 if (VIM_ISDIGIT(*cmd))
5103 {
5104 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005105 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005107 emsg(_(e_zerocount));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 return;
5109 }
5110 eap->line1 = eap->line2;
5111 eap->line2 += i - 1;
5112 if (eap->line2 > curbuf->b_ml.ml_line_count)
5113 eap->line2 = curbuf->b_ml.ml_line_count;
5114 }
5115
5116 /*
5117 * check for trailing command or garbage
5118 */
5119 cmd = skipwhite(cmd);
5120 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5121 {
5122 eap->nextcmd = check_nextcmd(cmd);
5123 if (eap->nextcmd == NULL)
5124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005125 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 return;
5127 }
5128 }
5129
5130 if (eap->skip) /* not executing commands, only parsing */
5131 return;
5132
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005133 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005134 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005135 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005136 emsg(_(e_modifiable));
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005137 return;
5138 }
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5141 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005142 if (subflags.do_error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005143 emsg(_(e_invcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 return;
5145 }
5146
5147 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005148 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005150 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 regmatch.rmm_ic = FALSE;
5152
5153 sub_firstline = NULL;
5154
5155 /*
5156 * ~ in the substitute pattern is replaced with the old pattern.
5157 * We do it here once to avoid it to be replaced over and over again.
5158 * But don't do it when it starts with "\=", then it's an expression.
5159 */
5160 if (!(sub[0] == '\\' && sub[1] == '='))
5161 sub = regtilde(sub, p_magic);
5162
5163 /*
5164 * Check for a match on each line.
5165 */
5166 line2 = eap->line2;
5167 for (lnum = eap->line1; lnum <= line2 && !(got_quit
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005168#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 || aborting()
5170#endif
5171 ); ++lnum)
5172 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005173 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005174 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (nmatch)
5176 {
5177 colnr_T copycol;
5178 colnr_T matchcol;
5179 colnr_T prev_matchcol = MAXCOL;
5180 char_u *new_end, *new_start = NULL;
5181 unsigned new_start_len = 0;
5182 char_u *p1;
5183 int did_sub = FALSE;
5184 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005185 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 long nmatch_tl = 0; /* nr of lines matched below lnum */
5187 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005188 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005189 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005190#ifdef FEAT_TEXT_PROP
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005191 int apc_flags = APC_SAVE_FOR_UNDO | APC_SUBSTITUTE;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194 /*
5195 * The new text is build up step by step, to avoid too much
5196 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005197 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 * copycol Column in the old text where we started
5199 * looking for a match; from here old text still
5200 * needs to be copied to the new text.
5201 * matchcol Column number of the old text where to look
5202 * for the next match. It's just after the
5203 * previous match or one further.
5204 * prev_matchcol Column just after the previous match (if any).
5205 * Mostly equal to matchcol, except for the first
5206 * match and after skipping an empty match.
5207 * regmatch.*pos Where the pattern matched in the old text.
5208 * new_start The new text, all that has been produced so
5209 * far.
5210 * new_end The new text, where to append new text.
5211 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005212 * lnum The line number where we found the start of
5213 * the match. Can be below the line we searched
5214 * when there is a \n before a \zs in the
5215 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * sub_firstlnum The line number in the buffer where to look
5217 * for a match. Can be different from "lnum"
5218 * when the pattern or substitute string contains
5219 * line breaks.
5220 *
5221 * Special situations:
5222 * - When the substitute string contains a line break, the part up
5223 * to the line break is inserted in the text, but the copy of
5224 * the original line is kept. "sub_firstlnum" is adjusted for
5225 * the inserted lines.
5226 * - When the matched pattern contains a line break, the old line
5227 * is taken from the line at the end of the pattern. The lines
5228 * in the match are deleted later, "sub_firstlnum" is adjusted
5229 * accordingly.
5230 *
5231 * The new text is built up in new_start[]. It has some extra
5232 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005233 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 *
5235 * Make a copy of the old line, so it won't be taken away when
5236 * updating the screen or handling a multi-line match. The "old_"
5237 * pointers point into this copy.
5238 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005239 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 copycol = 0;
5241 matchcol = 0;
5242
5243 /* At first match, remember current cursor position. */
5244 if (!got_match)
5245 {
5246 setpcmark();
5247 got_match = TRUE;
5248 }
5249
5250 /*
5251 * Loop until nothing more to replace in this line.
5252 * 1. Handle match with empty string.
5253 * 2. If do_ask is set, ask for confirmation.
5254 * 3. substitute the string.
5255 * 4. if do_all is set, find next match
5256 * 5. break if there isn't another match in this line
5257 */
5258 for (;;)
5259 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005260 /* Advance "lnum" to the line where the match starts. The
5261 * match does not start in the first line when there is a line
5262 * break before \zs. */
5263 if (regmatch.startpos[0].lnum > 0)
5264 {
5265 lnum += regmatch.startpos[0].lnum;
5266 sub_firstlnum += regmatch.startpos[0].lnum;
5267 nmatch -= regmatch.startpos[0].lnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005268 VIM_CLEAR(sub_firstline);
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005269 }
5270
5271 if (sub_firstline == NULL)
5272 {
5273 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5274 if (sub_firstline == NULL)
5275 {
5276 vim_free(new_start);
5277 goto outofmem;
5278 }
5279 }
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /* Save the line number of the last change for the final
5282 * cursor position (just like Vi). */
5283 curwin->w_cursor.lnum = lnum;
5284 do_again = FALSE;
5285
5286 /*
5287 * 1. Match empty string does not count, except for first
5288 * match. This reproduces the strange vi behaviour.
5289 * This also catches endless loops.
5290 */
5291 if (matchcol == prev_matchcol
5292 && regmatch.endpos[0].lnum == 0
5293 && matchcol == regmatch.endpos[0].col)
5294 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005295 if (sub_firstline[matchcol] == NUL)
5296 /* We already were at the end of the line. Don't look
5297 * for a match in this line again. */
5298 skip_match = TRUE;
5299 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005300 {
5301 /* search for a match at next column */
Bram Moolenaar0df11022011-05-19 14:30:16 +02005302 if (has_mbyte)
5303 matchcol += mb_ptr2len(sub_firstline + matchcol);
5304 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005305 ++matchcol;
5306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 goto skip;
5308 }
5309
5310 /* Normally we continue searching for a match just after the
5311 * previous match. */
5312 matchcol = regmatch.endpos[0].col;
5313 prev_matchcol = matchcol;
5314
5315 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005316 * 2. If do_count is set only increase the counter.
5317 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005319 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005320 {
5321 /* For a multi-line match, put matchcol at the NUL at
5322 * the end of the line and set nmatch to one, so that
5323 * we continue looking for a match on the next line.
5324 * Avoids that ":s/\nB\@=//gc" get stuck. */
5325 if (nmatch > 1)
5326 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005327 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005328 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005329 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005330 }
5331 sub_nsubs++;
5332 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005333#ifdef FEAT_EVAL
5334 /* Skip the substitution, unless an expression is used,
5335 * then it is evaluated in the sandbox. */
5336 if (!(sub[0] == '\\' && sub[1] == '='))
5337#endif
5338 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005339 }
5340
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005341 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005343 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 /* change State to CONFIRM, so that the mouse works
5346 * properly */
5347 save_State = State;
5348 State = CONFIRM;
5349#ifdef FEAT_MOUSE
5350 setmouse(); /* disable mouse in xterm */
5351#endif
5352 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005353 if (curwin->w_p_crb)
5354 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 /* When 'cpoptions' contains "u" don't sync undo when
5357 * asking for confirmation. */
5358 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5359 ++no_u_sync;
5360
5361 /*
5362 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5363 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005364 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005366 if (exmode_active)
5367 {
5368 char_u *resp;
5369 colnr_T sc, ec;
5370
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005371 print_line_no_prefix(lnum,
5372 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005373
5374 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5375 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005376 if (curwin->w_cursor.col < 0)
5377 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005378 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005379 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005380 {
5381 int numw = number_width(curwin) + 1;
5382 sc += numw;
5383 ec += numw;
5384 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005385 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005386 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005387 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005388 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005389 msg_putchar('^');
5390
5391 resp = getexmodeline('?', NULL, 0);
5392 if (resp != NULL)
5393 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005394 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005395 vim_free(resp);
5396 }
5397 }
5398 else
5399 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005400 char_u *orig_line = NULL;
5401 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005403 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005405 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005407 /* Invert the matched string.
5408 * Remove the inversion afterwards. */
5409 temp = RedrawingDisabled;
5410 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005412 if (new_start != NULL)
5413 {
5414 /* There already was a substitution, we would
5415 * like to show this to the user. We cannot
5416 * really update the line, it would change
5417 * what matches. Temporarily replace the line
5418 * and change it back afterwards. */
5419 orig_line = vim_strsave(ml_get(lnum));
5420 if (orig_line != NULL)
5421 {
5422 char_u *new_line = concat_str(new_start,
5423 sub_firstline + copycol);
5424
5425 if (new_line == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005426 VIM_CLEAR(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005427 else
5428 {
5429 /* Position the cursor relative to the
5430 * end of the line, the previous
5431 * substitute may have inserted or
5432 * deleted characters before the
5433 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005434 len_change = (int)STRLEN(new_line)
5435 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005436 curwin->w_cursor.col += len_change;
5437 ml_replace(lnum, new_line, FALSE);
5438 }
5439 }
5440 }
5441
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005442 search_match_lines = regmatch.endpos[0].lnum
5443 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005444 search_match_endcol = regmatch.endpos[0].col
5445 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005446 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005448 update_topline();
5449 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005450 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005451 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005452 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005455 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005457 if (msg_row == Rows - 1)
5458 msg_didout = FALSE; /* avoid a scroll-up */
5459 msg_starthere();
5460 i = msg_scroll;
5461 msg_scroll = 0; /* truncate msg when
5462 needed */
5463 msg_no_more = TRUE;
5464 /* write message same highlighting as for
5465 * wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005466 smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005467 _("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005468 msg_no_more = FALSE;
5469 msg_scroll = i;
5470 showruler(TRUE);
5471 windgoto(msg_row, msg_col);
5472 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005475 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005477 ++no_mapping; /* don't map this key */
5478 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005479 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005480 --allow_keys;
5481 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005483 /* clear the question */
5484 msg_didout = FALSE; /* don't scroll up */
5485 msg_col = 0;
5486 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005487
5488 /* restore the line */
5489 if (orig_line != NULL)
5490 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005491 }
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005494 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005496 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#endif
5498 )
5499 {
5500 got_quit = TRUE;
5501 break;
5502 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005503 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005505 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005507 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005510 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 line2 = lnum;
5512 break;
5513 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005514 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005516 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 break;
5518 }
5519#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005520 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005522 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 scrolldown_clamp();
5524#endif
5525 }
5526 State = save_State;
5527#ifdef FEAT_MOUSE
5528 setmouse();
5529#endif
5530 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5531 --no_u_sync;
5532
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005533 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
5535 /* For a multi-line match, put matchcol at the NUL at
5536 * the end of the line and set nmatch to one, so that
5537 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005538 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5539 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (nmatch > 1)
5541 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005542 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005543 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545 goto skip;
5546 }
5547 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005548 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550
5551 /* Move the cursor to the start of the match, so that we can
5552 * use "\=col("."). */
5553 curwin->w_cursor.col = regmatch.startpos[0].col;
5554
5555 /*
5556 * 3. substitute the string.
5557 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005558#ifdef FEAT_EVAL
Bram Moolenaar80341bc2019-05-20 20:34:51 +02005559 save_ma = curbuf->b_p_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005560 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005561 {
Bram Moolenaar6349e942019-05-18 13:41:22 +02005562 // prevent accidentally changing the buffer by a function
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005563 curbuf->b_p_ma = FALSE;
5564 sandbox++;
5565 }
Bram Moolenaar6349e942019-05-18 13:41:22 +02005566 // Save flags for recursion. They can change for e.g.
5567 // :s/^/\=execute("s#^##gn")
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005568 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005569#endif
Bram Moolenaar6349e942019-05-18 13:41:22 +02005570 // get length of substitution part
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005571 sublen = vim_regsub_multi(&regmatch,
5572 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005574#ifdef FEAT_EVAL
Bram Moolenaar0e97b942019-03-27 22:53:53 +01005575 // If getting the substitute string caused an error, don't do
5576 // the replacement.
Bram Moolenaar0e97b942019-03-27 22:53:53 +01005577 // Don't keep flags set by a recursive call.
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005578 subflags = subflags_save;
Bram Moolenaar6349e942019-05-18 13:41:22 +02005579 if (aborting() || subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005580 {
5581 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005582 if (sandbox > 0)
5583 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005584 goto skip;
5585 }
5586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaar4463f292005-09-25 22:20:24 +00005588 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005589 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005590 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5591 {
5592 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5593 skip_match = TRUE;
5594 }
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 /* Need room for:
5597 * - result so far in new_start (not for first sub in line)
5598 * - original text up to match
5599 * - length of substituted part
5600 * - original text after match
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005601 * Adjust text properties here, since we have all information
5602 * needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 */
5604 if (nmatch == 1)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 p1 = sub_firstline;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005607#ifdef FEAT_TEXT_PROP
5608 if (curbuf->b_has_textprop)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005609 {
5610 // When text properties are changed, need to save for
5611 // undo first, unless done already.
5612 if (adjust_prop_columns(lnum, regmatch.startpos[0].col,
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005613 sublen - 1 - (regmatch.endpos[0].col
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005614 - regmatch.startpos[0].col),
5615 apc_flags))
5616 apc_flags &= ~APC_SAVE_FOR_UNDO;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005617 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005618#endif
5619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 else
5621 {
5622 p1 = ml_get(sub_firstlnum + nmatch - 1);
5623 nmatch_tl += nmatch - 1;
5624 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005625 copy_len = regmatch.startpos[0].col - copycol;
5626 needed_len = copy_len + ((unsigned)STRLEN(p1)
5627 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 if (new_start == NULL)
5629 {
5630 /*
5631 * Get some space for a temporary buffer to do the
5632 * substitution into (and some extra space to avoid
5633 * too many calls to alloc()/free()).
5634 */
5635 new_start_len = needed_len + 50;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005636 if ((new_start = alloc(new_start_len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 goto outofmem;
5638 *new_start = NUL;
5639 new_end = new_start;
5640 }
5641 else
5642 {
5643 /*
5644 * Check if the temporary buffer is long enough to do the
5645 * substitution into. If not, make it larger (with a bit
5646 * extra to avoid too many calls to alloc()/free()).
5647 */
5648 len = (unsigned)STRLEN(new_start);
5649 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005650 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
5652 new_start_len = needed_len + 50;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005653 if ((p1 = alloc(new_start_len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 vim_free(new_start);
5656 goto outofmem;
5657 }
5658 mch_memmove(p1, new_start, (size_t)(len + 1));
5659 vim_free(new_start);
5660 new_start = p1;
5661 }
5662 new_end = new_start + len;
5663 }
5664
5665 /*
5666 * copy the text up to the part that matched
5667 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005668 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5669 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005671 (void)vim_regsub_multi(&regmatch,
5672 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 sub, new_end, TRUE, p_magic, TRUE);
5674 sub_nsubs++;
5675 did_sub = TRUE;
5676
5677 /* Move the cursor to the start of the line, to avoid that it
5678 * is beyond the end of the line after the substitution. */
5679 curwin->w_cursor.col = 0;
5680
5681 /* For a multi-line match, make a copy of the last matched
5682 * line and continue in that one. */
5683 if (nmatch > 1)
5684 {
5685 sub_firstlnum += nmatch - 1;
5686 vim_free(sub_firstline);
5687 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5688 /* When going beyond the last line, stop substituting. */
5689 if (sub_firstlnum <= line2)
5690 do_again = TRUE;
5691 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005692 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694
5695 /* Remember next character to be copied. */
5696 copycol = regmatch.endpos[0].col;
5697
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005698 if (skip_match)
5699 {
5700 /* Already hit end of the buffer, sub_firstlnum is one
5701 * less than what it ought to be. */
5702 vim_free(sub_firstline);
5703 sub_firstline = vim_strsave((char_u *)"");
5704 copycol = 0;
5705 }
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 /*
5708 * Now the trick is to replace CTRL-M chars with a real line
5709 * break. This would make it impossible to insert a CTRL-M in
5710 * the text. The line break can be avoided by preceding the
5711 * CTRL-M with a backslash. To be able to insert a backslash,
5712 * they must be doubled in the string and are halved here.
5713 * That is Vi compatible.
5714 */
5715 for (p1 = new_end; *p1; ++p1)
5716 {
5717 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005718 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005719 STRMOVE(p1, p1 + 1);
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005720#ifdef FEAT_TEXT_PROP
5721 if (curbuf->b_has_textprop)
5722 {
5723 // When text properties are changed, need to save
5724 // for undo first, unless done already.
5725 if (adjust_prop_columns(lnum,
5726 (colnr_T)(p1 - new_start), -1,
5727 apc_flags))
5728 apc_flags &= ~APC_SAVE_FOR_UNDO;
5729 }
5730#endif
5731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 else if (*p1 == CAR)
5733 {
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005734 if (u_inssub(lnum) == OK) // prepare for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005736 colnr_T plen = (colnr_T)(p1 - new_start + 1);
5737
5738 *p1 = NUL; // truncate up to the CR
5739 ml_append(lnum - 1, new_start, plen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005741 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 appended_lines(lnum - 1, 1L);
5743 else
5744 {
5745 if (first_line == 0)
5746 first_line = lnum;
5747 last_line = lnum + 1;
5748 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005749#ifdef FEAT_TEXT_PROP
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02005750 adjust_props_for_split(lnum + 1, lnum, plen, 1);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005751#endif
5752 // all line numbers increase
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 ++sub_firstlnum;
5754 ++lnum;
5755 ++line2;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005756 // move the cursor to the new line, like Vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ++curwin->w_cursor.lnum;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005758 // copy the rest
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005759 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 p1 = new_start - 1;
5761 }
5762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005764 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766
5767 /*
5768 * 4. If do_all is set, find next match.
5769 * Prevent endless loop with patterns that match empty
5770 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5771 * But ":s/\n/#/" is OK.
5772 */
5773skip:
5774 /* We already know that we did the last subst when we are at
5775 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005776 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5777 * "line2" when there is a \zs in the pattern after a line
5778 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005779 lastone = (skip_match
5780 || got_int
5781 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005782 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005783 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005784 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5785 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 nmatch = -1;
5787
5788 /*
5789 * Replace the line in the buffer when needed. This is
5790 * skipped when there are more matches.
5791 * The check for nmatch_tl is needed for when multi-line
5792 * matching must replace the lines before trying to do another
5793 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005794 * When the match starts below where we start searching also
5795 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 */
5797 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 || nmatch_tl > 0
5799 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005800 curbuf, sub_firstlnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005801 matchcol, NULL, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005802 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
5804 if (new_start != NULL)
5805 {
5806 /*
5807 * Copy the rest of the line, that didn't match.
5808 * "matchcol" has to be adjusted, we use the end of
5809 * the line as reference, because the substitute may
5810 * have changed the number of characters. Same for
5811 * "prev_matchcol".
5812 */
5813 STRCAT(new_start, sub_firstline + copycol);
5814 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5815 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5816 - prev_matchcol;
5817
5818 if (u_savesub(lnum) != OK)
5819 break;
5820 ml_replace(lnum, new_start, TRUE);
5821
5822 if (nmatch_tl > 0)
5823 {
5824 /*
5825 * Matched lines have now been substituted and are
5826 * useless, delete them. The part after the match
5827 * has been appended to new_start, we don't need
5828 * it in the buffer.
5829 */
5830 ++lnum;
5831 if (u_savedel(lnum, nmatch_tl) != OK)
5832 break;
5833 for (i = 0; i < nmatch_tl; ++i)
5834 ml_delete(lnum, (int)FALSE);
5835 mark_adjust(lnum, lnum + nmatch_tl - 1,
5836 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005837 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 deleted_lines(lnum, nmatch_tl);
5839 --lnum;
5840 line2 -= nmatch_tl; /* nr of lines decreases */
5841 nmatch_tl = 0;
5842 }
5843
5844 /* When asking, undo is saved each time, must also set
5845 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005846 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 changed_bytes(lnum, 0);
5848 else
5849 {
5850 if (first_line == 0)
5851 first_line = lnum;
5852 last_line = lnum + 1;
5853 }
5854
5855 sub_firstlnum = lnum;
5856 vim_free(sub_firstline); /* free the temp buffer */
5857 sub_firstline = new_start;
5858 new_start = NULL;
5859 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5860 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5861 - prev_matchcol;
5862 copycol = 0;
5863 }
5864 if (nmatch == -1 && !lastone)
5865 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005866 sub_firstlnum, matchcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868 /*
5869 * 5. break if there isn't another match in this line
5870 */
5871 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005872 {
5873 /* If the match found didn't start where we were
5874 * searching, do the next search in the line where we
5875 * found the match. */
5876 if (nmatch == -1)
5877 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
5881
5882 line_breakcheck();
5883 }
5884
5885 if (did_sub)
5886 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005887 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaard23a8232018-02-10 18:45:26 +01005888 VIM_CLEAR(sub_firstline); /* free the copy of the original line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 }
5890
5891 line_breakcheck();
5892 }
5893
5894 if (first_line != 0)
5895 {
5896 /* Need to subtract the number of added lines from "last_line" to get
5897 * the line number before the change (same as adding the number of
5898 * deleted lines). */
5899 i = curbuf->b_ml.ml_line_count - old_line_count;
5900 changed_lines(first_line, 0, last_line - i, i);
5901 }
5902
5903outofmem:
5904 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005905
5906 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005907 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005908 curwin->w_cursor = old_cursor;
5909
Bram Moolenaar46475522010-03-23 17:36:29 +01005910 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
5912 /* Set the '[ and '] marks. */
5913 curbuf->b_op_start.lnum = eap->line1;
5914 curbuf->b_op_end.lnum = line2;
5915 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5916
5917 if (!global_busy)
5918 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005919 /* when interactive leave cursor on the match */
5920 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005921 {
5922 if (endcolumn)
5923 coladvance((colnr_T)MAXCOL);
5924 else
5925 beginline(BL_WHITE | BL_FIX);
5926 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005927 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005928 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
5930 else
5931 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005932 if (subflags.do_print)
5933 print_line(curwin->w_cursor.lnum,
5934 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
5936 else if (!global_busy)
5937 {
5938 if (got_int) /* interrupted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005939 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 else if (got_match) /* did find something but nothing substituted */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005941 msg("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005942 else if (subflags.do_error) /* nothing found */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005943 semsg(_(e_patnotf2), get_search_pat());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 }
5945
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005946#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005947 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005948 /* Cursor position may require updating */
5949 changed_window_setting();
5950#endif
5951
Bram Moolenaar473de612013-06-08 18:19:48 +02005952 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005953
5954 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005955 subflags.do_all = save_do_all;
5956 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957}
5958
5959/*
5960 * Give message for number of substitutions.
5961 * Can also be used after a ":global" command.
5962 * Return TRUE if a message was given.
5963 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005964 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965do_sub_msg(
5966 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
5968 /*
5969 * Only report substitutions when:
5970 * - more than 'report' substitutions
5971 * - command was typed by user, or number of changed lines > 'report'
5972 * - giving messages is not disabled by 'lazyredraw'
5973 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005974 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5975 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 && messaging())
5977 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005978 char *msg_single;
5979 char *msg_plural;
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (got_int)
5982 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005983 else
5984 *msg_buf = NUL;
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005985
5986 msg_single = count_only
5987 ? NGETTEXT("%ld match on %ld line",
5988 "%ld matches on %ld line", sub_nsubs)
5989 : NGETTEXT("%ld substitution on %ld line",
5990 "%ld substitutions on %ld line", sub_nsubs);
5991 msg_plural = count_only
5992 ? NGETTEXT("%ld match on %ld lines",
5993 "%ld matches on %ld lines", sub_nsubs)
5994 : NGETTEXT("%ld substitution on %ld lines",
5995 "%ld substitutions on %ld lines", sub_nsubs);
5996
Bram Moolenaar32526b32019-01-19 17:43:09 +01005997 vim_snprintf_add(msg_buf, sizeof(msg_buf),
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005998 NGETTEXT(msg_single, msg_plural, sub_nlines),
5999 sub_nsubs, (long)sub_nlines);
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 /* save message to display it after redraw */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006003 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 return TRUE;
6005 }
6006 if (got_int)
6007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006008 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 return TRUE;
6010 }
6011 return FALSE;
6012}
6013
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006014 static void
6015global_exe_one(char_u *cmd, linenr_T lnum)
6016{
6017 curwin->w_cursor.lnum = lnum;
6018 curwin->w_cursor.col = 0;
6019 if (*cmd == NUL || *cmd == '\n')
6020 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
6021 else
6022 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
6023}
6024
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025/*
6026 * Execute a global command of the form:
6027 *
6028 * g/pattern/X : execute X on all lines where pattern matches
6029 * v/pattern/X : execute X on all lines where pattern does not match
6030 *
6031 * where 'X' is an EX command
6032 *
6033 * The command character (as well as the trailing slash) is optional, and
6034 * is assumed to be 'p' if missing.
6035 *
6036 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006037 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * for each line that has a mark. This is required because after deleting
6039 * lines we do not know where to search for the next match.
6040 */
6041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006045 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 int type; /* first char of cmd: 'v' or 'g' */
6047 char_u *cmd; /* command argument */
6048
6049 char_u delim; /* delimiter, normally '/' */
6050 char_u *pat;
6051 regmmatch_T regmatch;
6052 int match;
6053 int which_pat;
6054
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006055 /* When nesting the command works on one line. This allows for
6056 * ":g/found/v/notfound/command". */
6057 if (global_busy && (eap->line1 != 1
6058 || eap->line2 != curbuf->b_ml.ml_line_count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006060 /* will increment global_busy to break out of the loop */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006061 emsg(_("E147: Cannot do :global recursive with a range"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 return;
6063 }
6064
6065 if (eap->forceit) /* ":global!" is like ":vglobal" */
6066 type = 'v';
6067 else
6068 type = *eap->cmd;
6069 cmd = eap->arg;
6070 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 /*
6073 * undocumented vi feature:
6074 * "\/" and "\?": use previous search pattern.
6075 * "\&": use previous substitute pattern.
6076 */
6077 if (*cmd == '\\')
6078 {
6079 ++cmd;
6080 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
6081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006082 emsg(_(e_backslash));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 return;
6084 }
6085 if (*cmd == '&')
6086 which_pat = RE_SUBST; /* use previous substitute pattern */
6087 else
6088 which_pat = RE_SEARCH; /* use previous search pattern */
6089 ++cmd;
6090 pat = (char_u *)"";
6091 }
6092 else if (*cmd == NUL)
6093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006094 emsg(_("E148: Regular expression missing from global"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 return;
6096 }
6097 else
6098 {
6099 delim = *cmd; /* get the delimiter */
6100 if (delim)
6101 ++cmd; /* skip delimiter if there is one */
6102 pat = cmd; /* remember start of pattern */
6103 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
6104 if (cmd[0] == delim) /* end delimiter found */
6105 *cmd++ = NUL; /* replace it with a NUL */
6106 }
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
6109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006110 emsg(_(e_invcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 return;
6112 }
6113
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006114 if (global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006116 lnum = curwin->w_cursor.lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006117 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006118 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if ((type == 'g' && match) || (type == 'v' && !match))
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006120 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 }
6122 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006123 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006124 /*
6125 * pass 1: set marks for each (not) matching line
6126 */
6127 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6128 {
6129 /* a match on this line? */
6130 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006131 (colnr_T)0, NULL, NULL);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006132 if ((type == 'g' && match) || (type == 'v' && !match))
6133 {
6134 ml_setmarked(lnum);
6135 ndone++;
6136 }
6137 line_breakcheck();
6138 }
6139
6140 /*
6141 * pass 2: execute the command for each line that has been marked
6142 */
6143 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006144 msg(_(e_interr));
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006145 else if (ndone == 0)
6146 {
6147 if (type == 'v')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006148 smsg(_("Pattern found in every line: %s"), pat);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006149 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006150 smsg(_("Pattern not found: %s"), pat);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006151 }
6152 else
6153 {
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006154#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006155 start_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006156#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006157 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006158#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006159 end_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006160#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006161 }
6162
6163 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Bram Moolenaar473de612013-06-08 18:19:48 +02006166 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167}
6168
6169/*
6170 * Execute "cmd" on lines marked with ml_setmarked().
6171 */
6172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006173global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006175 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6176 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6177 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 /*
6180 * Set current position only once for a global command.
6181 * If global_busy is set, setpcmark() will not do anything.
6182 * If there is an error, global_busy will be incremented.
6183 */
6184 setpcmark();
6185
6186 /* When the command writes a message, don't overwrite the command. */
6187 msg_didout = TRUE;
6188
Bram Moolenaard25bc232010-03-23 17:49:24 +01006189 sub_nsubs = 0;
6190 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 global_need_beginline = FALSE;
6192 global_busy = 1;
6193 old_lcount = curbuf->b_ml.ml_line_count;
6194 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6195 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006196 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 ui_breakcheck();
6198 }
6199
6200 global_busy = 0;
6201 if (global_need_beginline)
6202 beginline(BL_WHITE | BL_FIX);
6203 else
6204 check_cursor(); /* cursor may be beyond the end of the line */
6205
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006206 /* the cursor may not have moved in the text but a change in a previous
6207 * line may move it on the screen */
6208 changed_line_abv_curs();
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /* If it looks like no message was written, allow overwriting the
6211 * command with the report for number of changes. */
6212 if (msg_col == 0 && msg_scrolled == 0)
6213 msg_didout = FALSE;
6214
Bram Moolenaar45667512007-05-10 19:24:43 +00006215 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006216 * number of extra or deleted lines.
6217 * Don't report extra or deleted lines in the edge case where the buffer
6218 * we are in after execution is different from the buffer we started in. */
6219 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6221}
6222
6223#ifdef FEAT_VIMINFO
6224 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006225read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006227 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 vim_free(old_sub);
6229 if (force || old_sub == NULL)
6230 old_sub = viminfo_readstring(virp, 1, TRUE);
6231 return viminfo_readline(virp);
6232}
6233
6234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006235write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6238 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006239 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 viminfo_writestring(fp, old_sub);
6241 }
6242}
6243#endif /* FEAT_VIMINFO */
6244
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006245#if defined(EXITFREE) || defined(PROTO)
6246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006247free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006248{
6249 vim_free(old_sub);
6250}
6251#endif
6252
Bram Moolenaar4033c552017-09-16 20:54:51 +02006253#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254/*
6255 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006256 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006258 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259prepare_tagpreview(
6260 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 win_T *wp;
6263
6264# ifdef FEAT_GUI
6265 need_mouse_correct = TRUE;
6266# endif
6267
6268 /*
6269 * If there is already a preview window open, use that one.
6270 */
6271 if (!curwin->w_p_pvw)
6272 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006273 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (wp->w_p_pvw)
6275 break;
6276 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006277 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 else
6279 {
6280 /*
6281 * There is no preview window open yet. Create one.
6282 */
6283 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6284 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006285 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 curwin->w_p_pvw = TRUE;
6287 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006288 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6289 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290# ifdef FEAT_DIFF
6291 curwin->w_p_diff = FALSE; /* no 'diff' */
6292# endif
6293# ifdef FEAT_FOLDING
6294 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6295# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006296 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006299 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302#endif
6303
6304
6305/*
6306 * ":help": open a read-only window on a help file
6307 */
6308 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006309ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 char_u *arg;
6312 char_u *tag;
6313 FILE *helpfd; /* file descriptor of help file */
6314 int n;
6315 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 int num_matches;
6318 char_u **matches;
6319 char_u *p;
6320 int empty_fnum = 0;
6321 int alt_fnum = 0;
6322 buf_T *buf;
6323#ifdef FEAT_MULTI_LANG
6324 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006325 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006327#ifdef FEAT_FOLDING
6328 int old_KeyTyped = KeyTyped;
6329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 if (eap != NULL)
6332 {
6333 /*
6334 * A ":help" command ends at the first LF, or at a '|' that is
6335 * followed by some text. Set nextcmd to the following command.
6336 */
6337 for (arg = eap->arg; *arg; ++arg)
6338 {
6339 if (*arg == '\n' || *arg == '\r'
6340 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6341 {
6342 *arg++ = NUL;
6343 eap->nextcmd = arg;
6344 break;
6345 }
6346 }
6347 arg = eap->arg;
6348
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006349 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006351 emsg(_("E478: Don't panic!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 return;
6353 }
6354
6355 if (eap->skip) /* not executing commands */
6356 return;
6357 }
6358 else
6359 arg = (char_u *)"";
6360
6361 /* remove trailing blanks */
6362 p = arg + STRLEN(arg) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006363 while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 *p-- = NUL;
6365
6366#ifdef FEAT_MULTI_LANG
6367 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006368 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
6370
6371 /* When no argument given go to the index. */
6372 if (*arg == NUL)
6373 arg = (char_u *)"help.txt";
6374
6375 /*
6376 * Check if there is a match for the argument.
6377 */
6378 n = find_help_tags(arg, &num_matches, &matches,
6379 eap != NULL && eap->forceit);
6380
6381 i = 0;
6382#ifdef FEAT_MULTI_LANG
6383 if (n != FAIL && lang != NULL)
6384 /* Find first item with the requested language. */
6385 for (i = 0; i < num_matches; ++i)
6386 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006387 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (len > 3 && matches[i][len - 3] == '@'
6389 && STRICMP(matches[i] + len - 2, lang) == 0)
6390 break;
6391 }
6392#endif
6393 if (i >= num_matches || n == FAIL)
6394 {
6395#ifdef FEAT_MULTI_LANG
6396 if (lang != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006397 semsg(_("E661: Sorry, no '%s' help for %s"), lang, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 else
6399#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006400 semsg(_("E149: Sorry, no help for %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (n != FAIL)
6402 FreeWild(num_matches, matches);
6403 return;
6404 }
6405
6406 /* The first match (in the requested language) is the best match. */
6407 tag = vim_strsave(matches[i]);
6408 FreeWild(num_matches, matches);
6409
6410#ifdef FEAT_GUI
6411 need_mouse_correct = TRUE;
6412#endif
6413
6414 /*
6415 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006416 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006418 if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 {
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006420 if (cmdmod.tab != 0)
6421 wp = NULL;
6422 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006423 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006424 if (bt_help(wp->w_buffer))
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6427 win_enter(wp, TRUE);
6428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 /*
6431 * There is no help window yet.
6432 * Try to open the file specified by the "helpfile" option.
6433 */
6434 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006436 smsg(_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 goto erret;
6438 }
6439 fclose(helpfd);
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006442 * specified, the current window is vertically split and
6443 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006446 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006449 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (curwin->w_height < p_hh)
6452 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 /*
6455 * Open help file (do_ecmd() will set b_help flag, readfile() will
6456 * set b_p_ro flag).
6457 * Set the alternate file to the previously edited file.
6458 */
6459 alt_fnum = curbuf->b_fnum;
6460 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006461 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaar4033c552017-09-16 20:54:51 +02006462 NULL); /* buffer is still open, don't store info */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006463 if (!cmdmod.keepalt)
6464 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 empty_fnum = curbuf->b_fnum;
6466 }
6467 }
6468
6469 if (!p_im)
6470 restart_edit = 0; /* don't want insert mode in help file */
6471
Bram Moolenaar8f535582011-09-30 17:30:31 +02006472#ifdef FEAT_FOLDING
6473 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6474 * It is needed for do_tag top open folds under the cursor. */
6475 KeyTyped = old_KeyTyped;
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (tag != NULL)
6479 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6480
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006481 /* Delete the empty buffer if we're not using it. Careful: autocommands
6482 * may have jumped to another window, check that the buffer is not in a
6483 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6485 {
6486 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006487 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 wipe_buffer(buf, TRUE);
6489 }
6490
6491 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006492 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 curwin->w_alt_fnum = alt_fnum;
6494
6495erret:
6496 vim_free(tag);
6497}
6498
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006499/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006500 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006501 */
6502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006503ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006504{
6505 win_T *win;
6506
6507 FOR_ALL_WINDOWS(win)
6508 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006509 if (bt_help(win->w_buffer))
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006510 {
6511 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006512 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006513 }
6514 }
6515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006517#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6518/*
6519 * In an argument search for a language specifiers in the form "@xx".
6520 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6521 * Returns NULL if not found.
6522 */
6523 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006524check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006525{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006526 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006527
6528 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6529 && ASCII_ISALPHA(arg[len - 1]))
6530 {
6531 arg[len - 3] = NUL; /* remove the '@' */
6532 return arg + len - 2;
6533 }
6534 return NULL;
6535}
6536#endif
6537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538/*
6539 * Return a heuristic indicating how well the given string matches. The
6540 * smaller the number, the better the match. This is the order of priorities,
6541 * from best match to worst match:
6542 * - Match with least alpha-numeric characters is better.
6543 * - Match with least total characters is better.
6544 * - Match towards the start is better.
6545 * - Match starting with "+" is worse (feature instead of command)
6546 * Assumption is made that the matched_string passed has already been found to
6547 * match some string for which help is requested. webb.
6548 */
6549 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006550help_heuristic(
6551 char_u *matched_string,
6552 int offset, /* offset for match */
6553 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 int num_letters;
6556 char_u *p;
6557
6558 num_letters = 0;
6559 for (p = matched_string; *p; p++)
6560 if (ASCII_ISALNUM(*p))
6561 num_letters++;
6562
6563 /*
6564 * Multiply the number of letters by 100 to give it a much bigger
6565 * weighting than the number of characters.
6566 * If there only is a match while ignoring case, add 5000.
6567 * If the match starts in the middle of a word, add 10000 to put it
6568 * somewhere in the last half.
6569 * If the match is more than 2 chars from the start, multiply by 200 to
6570 * put it after matches at the start.
6571 */
6572 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6573 && ASCII_ISALNUM(matched_string[offset - 1]))
6574 offset += 10000;
6575 else if (offset > 2)
6576 offset *= 200;
6577 if (wrong_case)
6578 offset += 5000;
6579 /* Features are less interesting than the subjects themselves, but "+"
6580 * alone is not a feature. */
6581 if (matched_string[0] == '+' && matched_string[1] != NUL)
6582 offset += 100;
6583 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6584}
6585
6586/*
6587 * Compare functions for qsort() below, that checks the help heuristics number
6588 * that has been put after the tagname by find_tags().
6589 */
6590 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006591help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 char *p1;
6594 char *p2;
6595
6596 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6597 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6598 return strcmp(p1, p2);
6599}
6600
6601/*
6602 * Find all help tags matching "arg", sort them and return in matches[], with
6603 * the number of matches in num_matches.
6604 * The matches will be sorted with a "best" match algorithm.
6605 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6606 */
6607 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006608find_help_tags(
6609 char_u *arg,
6610 int *num_matches,
6611 char_u ***matches,
6612 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 char_u *s, *d;
6615 int i;
6616 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006617 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006618 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006619 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6620 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006621 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006622 "[count]", "[quotex]",
6623 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006624 "[pattern]", "\\|", "\\%$",
6625 "s/\\~", "s/\\U", "s/\\L",
6626 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006628 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006629 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006630 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6631 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006632 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006633 "\\[count]", "\\[quotex]",
6634 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006635 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006636 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006637 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006638 static char *(expr_table[]) = {"!=?", "!~?", "<=?", "<?", "==?", "=~?",
6639 ">=?", ">?", "is?", "isnot?"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 int flags;
6641
6642 d = IObuff; /* assume IObuff is long enough! */
6643
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006644 if (STRNICMP(arg, "expr-", 5) == 0)
6645 {
6646 // When the string starting with "expr-" and containing '?' and matches
6647 // the table, it is taken literally. Otherwise '?' is recognized as a
6648 // wildcard.
6649 for (i = (int)(sizeof(expr_table) / sizeof(char *)); --i >= 0; )
6650 if (STRCMP(arg + 5, expr_table[i]) == 0)
6651 {
6652 STRCPY(d, arg);
6653 break;
6654 }
6655 }
6656 else
6657 {
6658 // Recognize a few exceptions to the rule. Some strings that contain
6659 // '*' with "star". Otherwise '*' is recognized as a wildcard.
6660 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
6661 if (STRCMP(arg, mtable[i]) == 0)
6662 {
6663 STRCPY(d, rtable[i]);
6664 break;
6665 }
6666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
6668 if (i < 0) /* no match in table */
6669 {
6670 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6671 * Also replace "\%^" and "\%(", they match every tag too.
6672 * Also "\zs", "\z1", etc.
6673 * Also "\@<", "\@=", "\@<=", etc.
6674 * And also "\_$" and "\_^". */
6675 if (arg[0] == '\\'
6676 && ((arg[1] != NUL && arg[2] == NUL)
6677 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6678 && arg[2] != NUL)))
6679 {
6680 STRCPY(d, "/\\\\");
6681 STRCPY(d + 3, arg + 1);
6682 /* Check for "/\\_$", should be "/\\_\$" */
6683 if (d[3] == '_' && d[4] == '$')
6684 STRCPY(d + 4, "\\$");
6685 }
6686 else
6687 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006688 /* Replace:
6689 * "[:...:]" with "\[:...:]"
6690 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006691 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006692 */
6693 if ((arg[0] == '[' && (arg[1] == ':'
6694 || (arg[1] == '+' && arg[2] == '+')))
6695 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 *d++ = '\\';
6697
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006698 /*
6699 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6700 */
6701 if (*arg == '(' && arg[1] == '\'')
6702 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 for (s = arg; *s; ++s)
6704 {
6705 /*
6706 * Replace "|" with "bar" and '"' with "quote" to match the name of
6707 * the tags for these commands.
6708 * Replace "*" with ".*" and "?" with "." to match command line
6709 * completion.
6710 * Insert a backslash before '~', '$' and '.' to avoid their
6711 * special meaning.
6712 */
6713 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6714 break;
6715 switch (*s)
6716 {
6717 case '|': STRCPY(d, "bar");
6718 d += 3;
6719 continue;
6720 case '"': STRCPY(d, "quote");
6721 d += 5;
6722 continue;
6723 case '*': *d++ = '.';
6724 break;
6725 case '?': *d++ = '.';
6726 continue;
6727 case '$':
6728 case '.':
6729 case '~': *d++ = '\\';
6730 break;
6731 }
6732
6733 /*
6734 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6735 * ":help i_^_CTRL-D" work.
6736 * Insert '-' before and after "CTRL-X" when applicable.
6737 */
6738 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6739 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6740 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006741 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6742 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 STRCPY(d, "CTRL-");
6744 d += 5;
6745 if (*s < ' ')
6746 {
6747#ifdef EBCDIC
6748 *d++ = CtrlChar(*s);
6749#else
6750 *d++ = *s + '@';
6751#endif
6752 if (d[-1] == '\\')
6753 *d++ = '\\'; /* double a backslash */
6754 }
6755 else
6756 *d++ = *++s;
6757 if (s[1] != NUL && s[1] != '_')
6758 *d++ = '_'; /* append a '_' */
6759 continue;
6760 }
6761 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6762 *d++ = '\\';
6763
6764 /*
6765 * Insert a backslash before a backslash after a slash, for search
6766 * pattern tags: "/\|" --> "/\\|".
6767 */
6768 else if (s[0] == '\\' && s[1] != '\\'
6769 && *arg == '/' && s == arg + 1)
6770 *d++ = '\\';
6771
6772 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6773 * "CTRL-\_CTRL-N" */
6774 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6775 {
6776 STRCPY(d, "CTRL-\\\\");
6777 d += 7;
6778 s += 6;
6779 }
6780
6781 *d++ = *s;
6782
6783 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006784 * If tag contains "({" or "([", tag terminates at the "(".
6785 * This is for help on functions, e.g.: abs({expr}).
6786 */
6787 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6788 break;
6789
6790 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 * If tag starts with ', toss everything after a second '. Fixes
6792 * CTRL-] on 'option'. (would include the trailing '.').
6793 */
6794 if (*s == '\'' && s > arg && *arg == '\'')
6795 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006796 /* Also '{' and '}'. */
6797 if (*s == '}' && s > arg && *arg == '{')
6798 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
6800 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006801
6802 if (*IObuff == '`')
6803 {
6804 if (d > IObuff + 2 && d[-1] == '`')
6805 {
6806 /* remove the backticks from `command` */
6807 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6808 d[-2] = NUL;
6809 }
6810 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6811 {
6812 /* remove the backticks and comma from `command`, */
6813 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6814 d[-3] = NUL;
6815 }
6816 else if (d > IObuff + 4 && d[-3] == '`'
6817 && d[-2] == '\\' && d[-1] == '.')
6818 {
6819 /* remove the backticks and dot from `command`\. */
6820 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6821 d[-4] = NUL;
6822 }
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
6825 }
6826
6827 *matches = (char_u **)"";
6828 *num_matches = 0;
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006829 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE | TAG_NO_TAGFUNC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (keep_lang)
6831 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006832 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 /* Sort the matches found on the heuristic number that is after the
6836 * tag name. */
6837 qsort((void *)*matches, (size_t)*num_matches,
6838 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006839 /* Delete more than TAG_MANY to reduce the size of the listing. */
6840 while (*num_matches > TAG_MANY)
6841 vim_free((*matches)[--*num_matches]);
6842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 return OK;
6844}
6845
6846/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006847 * Called when starting to edit a buffer for a help file.
6848 */
6849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006850prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006851{
6852 char_u *p;
6853
6854 curbuf->b_help = TRUE;
6855#ifdef FEAT_QUICKFIX
6856 set_string_option_direct((char_u *)"buftype", -1,
6857 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6858#endif
6859
6860 /*
6861 * Always set these options after jumping to a help tag, because the
6862 * user may have an autocommand that gets in the way.
6863 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6864 * latin1 word characters (for translated help files).
6865 * Only set it when needed, buf_init_chartab() is some work.
6866 */
6867 p =
6868#ifdef EBCDIC
6869 (char_u *)"65-255,^*,^|,^\"";
6870#else
6871 (char_u *)"!-~,^*,^|,^\",192-255";
6872#endif
6873 if (STRCMP(curbuf->b_p_isk, p) != 0)
6874 {
6875 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6876 check_buf_options(curbuf);
6877 (void)buf_init_chartab(curbuf, FALSE);
6878 }
6879
Bram Moolenaar0b105412014-11-30 13:34:23 +01006880#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006881 /* Don't use the global foldmethod.*/
6882 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6883 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006884#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006885
6886 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6887 curwin->w_p_list = FALSE; /* no list mode */
6888
6889 curbuf->b_p_ma = FALSE; /* not modifiable */
6890 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6891 curwin->w_p_nu = 0; /* no line numbers */
6892 curwin->w_p_rnu = 0; /* no relative line numbers */
6893 RESET_BINDING(curwin); /* no scroll or cursor binding */
6894#ifdef FEAT_ARABIC
6895 curwin->w_p_arab = FALSE; /* no arabic mode */
6896#endif
6897#ifdef FEAT_RIGHTLEFT
6898 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6899#endif
6900#ifdef FEAT_FOLDING
6901 curwin->w_p_fen = FALSE; /* No folding in the help window */
6902#endif
6903#ifdef FEAT_DIFF
6904 curwin->w_p_diff = FALSE; /* No 'diff' */
6905#endif
6906#ifdef FEAT_SPELL
6907 curwin->w_p_spell = FALSE; /* No spell checking */
6908#endif
6909
6910 set_buflisted(FALSE);
6911}
6912
6913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 * After reading a help file: May cleanup a help buffer when syntax
6915 * highlighting is not used.
6916 */
6917 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006918fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 linenr_T lnum;
6921 char_u *line;
6922 int in_example = FALSE;
6923 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006924 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 char_u *p;
6926 char_u *rt;
6927 int mustfree;
6928
Bram Moolenaar90492982017-06-22 14:16:31 +02006929 /* Set filetype to "help" if still needed. */
6930 if (STRCMP(curbuf->b_p_ft, "help") != 0)
Bram Moolenaar18141832017-06-25 21:17:25 +02006931 {
6932 ++curbuf_lock;
Bram Moolenaar90492982017-06-22 14:16:31 +02006933 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
Bram Moolenaar18141832017-06-25 21:17:25 +02006934 --curbuf_lock;
6935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006938 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939#endif
6940 {
6941 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6942 {
6943 line = ml_get_buf(curbuf, lnum, FALSE);
6944 len = (int)STRLEN(line);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006945 if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
6947 /* End of example: non-white or '<' in first column. */
6948 if (line[0] == '<')
6949 {
6950 /* blank-out a '<' in the first column */
6951 line = ml_get_buf(curbuf, lnum, TRUE);
6952 line[0] = ' ';
6953 }
6954 in_example = FALSE;
6955 }
6956 if (!in_example && len > 0)
6957 {
6958 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6959 {
6960 /* blank-out a '>' in the last column (start of example) */
6961 line = ml_get_buf(curbuf, lnum, TRUE);
6962 line[len - 1] = ' ';
6963 in_example = TRUE;
6964 }
6965 else if (line[len - 1] == '~')
6966 {
6967 /* blank-out a '~' at the end of line (header marker) */
6968 line = ml_get_buf(curbuf, lnum, TRUE);
6969 line[len - 1] = ' ';
6970 }
6971 }
6972 }
6973 }
6974
6975 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006976 * In the "help.txt" and "help.abx" file, add the locally added help
6977 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006979 fname = gettail(curbuf->b_fname);
6980 if (fnamecmp(fname, "help.txt") == 0
6981#ifdef FEAT_MULTI_LANG
6982 || (fnamencmp(fname, "help.", 5) == 0
6983 && ASCII_ISALPHA(fname[5])
6984 && ASCII_ISALPHA(fname[6])
6985 && TOLOWER_ASC(fname[7]) == 'x'
6986 && fname[8] == NUL)
6987#endif
6988 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
6990 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6991 {
6992 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006993 if (strstr((char *)line, "*local-additions*") == NULL)
6994 continue;
6995
6996 /* Go through all directories in 'runtimepath', skipping
6997 * $VIMRUNTIME. */
6998 p = p_rtp;
6999 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007001 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7002 mustfree = FALSE;
7003 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
Bram Moolenaar99499b12019-05-23 21:35:48 +02007004 if (rt != NULL &&
7005 fullpathcmp(rt, NameBuff, FALSE, TRUE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007007 int fcount;
7008 char_u **fnames;
7009 FILE *fd;
7010 char_u *s;
7011 int fi;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007012 vimconv_T vc;
7013 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007015 /* Find all "doc/ *.txt" files in this directory. */
7016 add_pathsep(NameBuff);
7017#ifdef FEAT_MULTI_LANG
7018 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007020 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007022 if (gen_expand_wildcards(1, &NameBuff, &fcount,
7023 &fnames, EW_FILE|EW_SILENT) == OK
7024 && fcount > 0)
7025 {
7026#ifdef FEAT_MULTI_LANG
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007027 int i1, i2;
7028 char_u *f1, *f2;
7029 char_u *t1, *t2;
7030 char_u *e1, *e2;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007031
7032 /* If foo.abx is found use it instead of foo.txt in
7033 * the same directory. */
7034 for (i1 = 0; i1 < fcount; ++i1)
7035 {
7036 for (i2 = 0; i2 < fcount; ++i2)
7037 {
7038 if (i1 == i2)
7039 continue;
7040 if (fnames[i1] == NULL || fnames[i2] == NULL)
7041 continue;
7042 f1 = fnames[i1];
7043 f2 = fnames[i2];
7044 t1 = gettail(f1);
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007045 t2 = gettail(f2);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007046 e1 = vim_strrchr(t1, '.');
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007047 e2 = vim_strrchr(t2, '.');
Bram Moolenaar7756e742016-10-21 20:35:37 +02007048 if (e1 == NULL || e2 == NULL)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007049 continue;
7050 if (fnamecmp(e1, ".txt") != 0
7051 && fnamecmp(e1, fname + 4) != 0)
7052 {
7053 /* Not .txt and not .abx, remove it. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007054 VIM_CLEAR(fnames[i1]);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007055 continue;
7056 }
Bram Moolenaar35c5e812017-12-09 21:10:13 +01007057 if (e1 - f1 != e2 - f2
7058 || fnamencmp(f1, f2, e1 - f1) != 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007059 continue;
7060 if (fnamecmp(e1, ".txt") == 0
7061 && fnamecmp(e2, fname + 4) == 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007062 /* use .abx instead of .txt */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007063 VIM_CLEAR(fnames[i1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007066#endif
7067 for (fi = 0; fi < fcount; ++fi)
7068 {
7069 if (fnames[fi] == NULL)
7070 continue;
7071 fd = mch_fopen((char *)fnames[fi], "r");
7072 if (fd != NULL)
7073 {
7074 vim_fgets(IObuff, IOSIZE, fd);
7075 if (IObuff[0] == '*'
7076 && (s = vim_strchr(IObuff + 1, '*'))
7077 != NULL)
7078 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007079 int this_utf = MAYBE;
Bram Moolenaar13505972019-01-24 15:04:48 +01007080
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007081 /* Change tag definition to a
7082 * reference and remove <CR>/<NL>. */
7083 IObuff[0] = '|';
7084 *s = '|';
7085 while (*s != NUL)
7086 {
7087 if (*s == '\r' || *s == '\n')
7088 *s = NUL;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007089 /* The text is utf-8 when a byte
7090 * above 127 is found and no
7091 * illegal byte sequence is found.
7092 */
7093 if (*s >= 0x80 && this_utf != FALSE)
7094 {
7095 int l;
7096
7097 this_utf = TRUE;
7098 l = utf_ptr2len(s);
7099 if (l == 1)
7100 this_utf = FALSE;
7101 s += l - 1;
7102 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007103 ++s;
7104 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007105
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007106 /* The help file is latin1 or utf-8;
7107 * conversion to the current
7108 * 'encoding' may be required. */
7109 vc.vc_type = CONV_NONE;
7110 convert_setup(&vc, (char_u *)(
7111 this_utf == TRUE ? "utf-8"
7112 : "latin1"), p_enc);
7113 if (vc.vc_type == CONV_NONE)
7114 /* No conversion needed. */
7115 cp = IObuff;
7116 else
7117 {
7118 /* Do the conversion. If it fails
7119 * use the unconverted text. */
7120 cp = string_convert(&vc, IObuff,
7121 NULL);
7122 if (cp == NULL)
7123 cp = IObuff;
7124 }
7125 convert_setup(&vc, NULL, NULL);
7126
7127 ml_append(lnum, cp, (colnr_T)0, FALSE);
7128 if (cp != IObuff)
7129 vim_free(cp);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007130 ++lnum;
7131 }
7132 fclose(fd);
7133 }
7134 }
7135 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007138 if (mustfree)
7139 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
7143 }
7144}
7145
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007146/*
7147 * ":exusage"
7148 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007150ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007151{
7152 do_cmdline_cmd((char_u *)"help ex-cmd-index");
7153}
7154
7155/*
7156 * ":viusage"
7157 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007159ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007160{
7161 do_cmdline_cmd((char_u *)"help normal-index");
7162}
7163
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007165 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007168helptags_one(
7169 char_u *dir, /* doc directory */
7170 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
7171 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
7172 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 FILE *fd_tags;
7175 FILE *fd;
7176 garray_T ga;
7177 int filecount;
7178 char_u **files;
7179 char_u *p1, *p2;
7180 int fi;
7181 char_u *s;
7182 int i;
7183 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007184 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 int utf8 = MAYBE;
7186 int this_utf8;
7187 int firstline;
7188 int mix = FALSE; /* detected mixed encodings */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 /*
7191 * Find all *.txt files.
7192 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007193 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007195 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 STRCAT(NameBuff, ext);
7197 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7198 EW_FILE|EW_SILENT) == FAIL
7199 || filecount == 0)
7200 {
7201 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007202 semsg(_("E151: No match: %s"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 return;
7204 }
7205
7206 /*
7207 * Open the tags file for writing.
7208 * Do this before scanning through all the files.
7209 */
7210 STRCPY(NameBuff, dir);
7211 add_pathsep(NameBuff);
7212 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007213 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (fd_tags == NULL)
7215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007216 semsg(_("E152: Cannot open %s for writing"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 FreeWild(filecount, files);
7218 return;
7219 }
7220
7221 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007222 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7223 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 */
7225 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007226 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
Bram Moolenaar99499b12019-05-23 21:35:48 +02007227 dir, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
7229 if (ga_grow(&ga, 1) == FAIL)
7230 got_int = TRUE;
7231 else
7232 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007233 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 if (s == NULL)
7235 got_int = TRUE;
7236 else
7237 {
7238 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7239 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7240 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 }
7242 }
7243 }
7244
7245 /*
7246 * Go over all the files and extract the tags.
7247 */
7248 for (fi = 0; fi < filecount && !got_int; ++fi)
7249 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007250 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 if (fd == NULL)
7252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007253 semsg(_("E153: Unable to open %s for reading"), files[fi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 continue;
7255 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007256 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 firstline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 if (firstline)
7262 {
7263 /* Detect utf-8 file by a non-ASCII char in the first line. */
7264 this_utf8 = MAYBE;
7265 for (s = IObuff; *s != NUL; ++s)
7266 if (*s >= 0x80)
7267 {
7268 int l;
7269
7270 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007271 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (l == 1)
7273 {
7274 /* Illegal UTF-8 byte sequence. */
7275 this_utf8 = FALSE;
7276 break;
7277 }
7278 s += l - 1;
7279 }
7280 if (this_utf8 == MAYBE) /* only ASCII characters found */
7281 this_utf8 = FALSE;
7282 if (utf8 == MAYBE) /* first file */
7283 utf8 = this_utf8;
7284 else if (utf8 != this_utf8)
7285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007286 semsg(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 mix = !got_int;
7288 got_int = TRUE;
7289 }
7290 firstline = FALSE;
7291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7293 while (p1 != NULL)
7294 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007295 /* Use vim_strbyte() instead of vim_strchr() so that when
7296 * 'encoding' is dbcs it still works, don't find '*' in the
7297 * second byte. */
7298 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7300 {
7301 for (s = p1 + 1; s < p2; ++s)
7302 if (*s == ' ' || *s == '\t' || *s == '|')
7303 break;
7304
7305 /*
7306 * Only accept a *tag* when it consists of valid
7307 * characters, there is white space before it and is
7308 * followed by a white character or end-of-line.
7309 */
7310 if (s == p2
7311 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7312 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7313 || s[1] == '\0'))
7314 {
7315 *p2 = '\0';
7316 ++p1;
7317 if (ga_grow(&ga, 1) == FAIL)
7318 {
7319 got_int = TRUE;
7320 break;
7321 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02007322 s = alloc(p2 - p1 + STRLEN(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (s == NULL)
7324 {
7325 got_int = TRUE;
7326 break;
7327 }
7328 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7329 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 sprintf((char *)s, "%s\t%s", p1, fname);
7331
7332 /* find next '*' */
7333 p2 = vim_strchr(p2 + 1, '*');
7334 }
7335 }
7336 p1 = p2;
7337 }
7338 line_breakcheck();
7339 }
7340
7341 fclose(fd);
7342 }
7343
7344 FreeWild(filecount, files);
7345
7346 if (!got_int)
7347 {
7348 /*
7349 * Sort the tags.
7350 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007351 if (ga.ga_data != NULL)
7352 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 /*
7355 * Check for duplicates.
7356 */
7357 for (i = 1; i < ga.ga_len; ++i)
7358 {
7359 p1 = ((char_u **)ga.ga_data)[i - 1];
7360 p2 = ((char_u **)ga.ga_data)[i];
7361 while (*p1 == *p2)
7362 {
7363 if (*p2 == '\t')
7364 {
7365 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007366 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7368 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007369 emsg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 *p2 = '\t';
7371 break;
7372 }
7373 ++p1;
7374 ++p2;
7375 }
7376 }
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (utf8 == TRUE)
7379 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
7381 /*
7382 * Write the tags into the file.
7383 */
7384 for (i = 0; i < ga.ga_len; ++i)
7385 {
7386 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007387 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007389 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 else
7391 {
7392 fprintf(fd_tags, "%s\t/*", s);
7393 for (p1 = s; *p1 != '\t'; ++p1)
7394 {
7395 /* insert backslash before '\\' and '/' */
7396 if (*p1 == '\\' || *p1 == '/')
7397 putc('\\', fd_tags);
7398 putc(*p1, fd_tags);
7399 }
7400 fprintf(fd_tags, "*\n");
7401 }
7402 }
7403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (mix)
7405 got_int = FALSE; /* continue with other languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 for (i = 0; i < ga.ga_len; ++i)
7408 vim_free(((char_u **)ga.ga_data)[i]);
7409 ga_clear(&ga);
7410 fclose(fd_tags); /* there is no check for an error... */
7411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007413/*
7414 * Generate tags in one help directory, taking care of translations.
7415 */
7416 static void
7417do_helptags(char_u *dirname, int add_help_tags)
7418{
7419#ifdef FEAT_MULTI_LANG
7420 int len;
7421 int i, j;
7422 garray_T ga;
7423 char_u lang[2];
7424 char_u ext[5];
7425 char_u fname[8];
7426 int filecount;
7427 char_u **files;
7428
7429 /* Get a list of all files in the help directory and in subdirectories. */
7430 STRCPY(NameBuff, dirname);
7431 add_pathsep(NameBuff);
7432 STRCAT(NameBuff, "**");
7433 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7434 EW_FILE|EW_SILENT) == FAIL
7435 || filecount == 0)
7436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007437 semsg(_("E151: No match: %s"), NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007438 return;
7439 }
7440
7441 /* Go over all files in the directory to find out what languages are
7442 * present. */
7443 ga_init2(&ga, 1, 10);
7444 for (i = 0; i < filecount; ++i)
7445 {
7446 len = (int)STRLEN(files[i]);
7447 if (len > 4)
7448 {
7449 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7450 {
7451 /* ".txt" -> language "en" */
7452 lang[0] = 'e';
7453 lang[1] = 'n';
7454 }
7455 else if (files[i][len - 4] == '.'
7456 && ASCII_ISALPHA(files[i][len - 3])
7457 && ASCII_ISALPHA(files[i][len - 2])
7458 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7459 {
7460 /* ".abx" -> language "ab" */
7461 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7462 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7463 }
7464 else
7465 continue;
7466
7467 /* Did we find this language already? */
7468 for (j = 0; j < ga.ga_len; j += 2)
7469 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7470 break;
7471 if (j == ga.ga_len)
7472 {
7473 /* New language, add it. */
7474 if (ga_grow(&ga, 2) == FAIL)
7475 break;
7476 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7477 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7478 }
7479 }
7480 }
7481
7482 /*
7483 * Loop over the found languages to generate a tags file for each one.
7484 */
7485 for (j = 0; j < ga.ga_len; j += 2)
7486 {
7487 STRCPY(fname, "tags-xx");
7488 fname[5] = ((char_u *)ga.ga_data)[j];
7489 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7490 if (fname[5] == 'e' && fname[6] == 'n')
7491 {
7492 /* English is an exception: use ".txt" and "tags". */
7493 fname[4] = NUL;
7494 STRCPY(ext, ".txt");
7495 }
7496 else
7497 {
7498 /* Language "ab" uses ".abx" and "tags-ab". */
7499 STRCPY(ext, ".xxx");
7500 ext[1] = fname[5];
7501 ext[2] = fname[6];
7502 }
7503 helptags_one(dirname, ext, fname, add_help_tags);
7504 }
7505
7506 ga_clear(&ga);
7507 FreeWild(filecount, files);
7508
7509#else
7510 /* No language support, just use "*.txt" and "tags". */
7511 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7512#endif
7513}
7514
7515 static void
7516helptags_cb(char_u *fname, void *cookie)
7517{
7518 do_helptags(fname, *(int *)cookie);
7519}
7520
7521/*
7522 * ":helptags"
7523 */
7524 void
7525ex_helptags(exarg_T *eap)
7526{
7527 expand_T xpc;
7528 char_u *dirname;
7529 int add_help_tags = FALSE;
7530
7531 /* Check for ":helptags ++t {dir}". */
Bram Moolenaar1c465442017-03-12 20:10:05 +01007532 if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3]))
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007533 {
7534 add_help_tags = TRUE;
7535 eap->arg = skipwhite(eap->arg + 3);
7536 }
7537
7538 if (STRCMP(eap->arg, "ALL") == 0)
7539 {
7540 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7541 helptags_cb, &add_help_tags);
7542 }
7543 else
7544 {
7545 ExpandInit(&xpc);
7546 xpc.xp_context = EXPAND_DIRECTORIES;
7547 dirname = ExpandOne(&xpc, eap->arg, NULL,
7548 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7549 if (dirname == NULL || !mch_isdir(dirname))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 semsg(_("E150: Not a directory: %s"), eap->arg);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007551 else
7552 do_helptags(dirname, add_help_tags);
7553 vim_free(dirname);
7554 }
7555}
7556
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007557/*
7558 * Make the user happy.
7559 */
7560 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007561ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007562{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02007563 static char *code[] = {
7564 "\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$",
7565 "\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"
7566 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007567 char *p;
7568 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02007569 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007570
7571 msg_start();
7572 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02007573 for (i = 0; i < 2; ++i)
7574 for (p = code[i]; *p != NUL; ++p)
7575 if (*p == 'x')
7576 msg_putchar('\n');
7577 else
7578 for (n = *p++; n > 0; --n)
7579 if (*p == 'o' || *p == '$')
Bram Moolenaar8820b482017-03-16 17:23:31 +01007580 msg_putchar_attr(*p, HL_ATTR(HLF_L));
Bram Moolenaar700eefe2016-04-13 21:14:37 +02007581 else
7582 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007583 msg_clr_eos();
7584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586/*
7587 * ":drop"
7588 * Opens the first argument in a window. When there are two or more arguments
7589 * the argument list is redefined.
7590 */
7591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007592ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 win_T *wp;
7596 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007597 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
7599 /*
7600 * Check if the first argument is already being edited in a window. If
7601 * so, jump to that window.
7602 * We would actually need to check all arguments, but that's complicated
7603 * and mostly only one file is dropped.
7604 * This also ignores wildcards, since it is very unlikely the user is
7605 * editing a file name with a wildcard character.
7606 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007607 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00007609 /*
7610 * Expanding wildcards may result in an empty argument list. E.g. when
7611 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
7612 * already did an error message for this.
7613 */
7614 if (ARGCOUNT == 0)
7615 return;
7616
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007617 if (cmdmod.tab)
7618 {
7619 /* ":tab drop file ...": open a tab for each argument that isn't
7620 * edited in a window yet. It's like ":tab all" but without closing
7621 * windows or tabs. */
7622 ex_all(eap);
7623 }
7624 else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007625 {
7626 /* ":drop file ...": Edit the first argument. Jump to an existing
7627 * window if possible, edit in current window if the current buffer
7628 * can be abandoned, otherwise open a new window. */
7629 buf = buflist_findnr(ARGLIST[0].ae_fnum);
7630
7631 FOR_ALL_TAB_WINDOWS(tp, wp)
7632 {
7633 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007635 goto_tabpage_win(tp, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 return;
7638 }
7639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007641 /*
7642 * Check whether the current buffer is changed. If so, we will need
7643 * to split the current window or data could be lost.
7644 * Skip the check if the 'hidden' option is set, as in this case the
7645 * buffer won't be lost.
7646 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007647 if (!buf_hide(curbuf))
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007648 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007649 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007650 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007651 --emsg_off;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007654 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7655 if (split)
7656 {
7657 eap->cmdidx = CMD_sfirst;
7658 eap->cmd[0] = 's';
7659 }
7660 else
7661 eap->cmdidx = CMD_first;
7662 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007665
Bram Moolenaar9baf2972016-08-21 22:39:35 +02007666/*
7667 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
7668 * Put the start of the pattern in "*s", unless "s" is NULL.
7669 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
7670 * If "s" is not NULL terminate the pattern with a NUL.
7671 * Return a pointer to the char just past the pattern plus flags.
7672 */
7673 char_u *
7674skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
7675{
7676 int c;
7677
7678 if (vim_isIDc(*p))
7679 {
7680 /* ":vimgrep pattern fname" */
7681 if (s != NULL)
7682 *s = p;
7683 p = skiptowhite(p);
7684 if (s != NULL && *p != NUL)
7685 *p++ = NUL;
7686 }
7687 else
7688 {
7689 /* ":vimgrep /pattern/[g][j] fname" */
7690 if (s != NULL)
7691 *s = p + 1;
7692 c = *p;
7693 p = skip_regexp(p + 1, c, TRUE, NULL);
7694 if (*p != c)
7695 return NULL;
7696
7697 /* Truncate the pattern. */
7698 if (s != NULL)
7699 *p = NUL;
7700 ++p;
7701
7702 /* Find the flags */
7703 while (*p == 'g' || *p == 'j')
7704 {
7705 if (flags != NULL)
7706 {
7707 if (*p == 'g')
7708 *flags |= VGR_GLOBAL;
7709 else
7710 *flags |= VGR_NOJUMP;
7711 }
7712 ++p;
7713 }
7714 }
7715 return p;
7716}
Bram Moolenaar9baf2972016-08-21 22:39:35 +02007717
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007718#if defined(FEAT_EVAL) || defined(PROTO)
7719/*
7720 * List v:oldfiles in a nice way.
7721 */
7722 void
7723ex_oldfiles(exarg_T *eap UNUSED)
7724{
7725 list_T *l = get_vim_var_list(VV_OLDFILES);
7726 listitem_T *li;
7727 int nr = 0;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007728 char_u *fname;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007729
7730 if (l == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007731 msg(_("No old files"));
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007732 else
7733 {
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007734 msg_start();
7735 msg_scroll = TRUE;
7736 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
7737 {
7738 ++nr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007739 fname = tv_get_string(&li->li_tv);
Bram Moolenaard6f2ee32016-08-24 00:30:52 +02007740 if (!message_filtered(fname))
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007741 {
7742 msg_outnum((long)nr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007743 msg_puts(": ");
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007744 msg_outtrans(fname);
Bram Moolenaar885c00e2016-08-28 17:08:17 +02007745 msg_clr_eos();
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007746 msg_putchar('\n');
7747 out_flush(); /* output one line at a time */
7748 ui_breakcheck();
7749 }
7750 }
Bram Moolenaare11d61a2016-08-20 18:36:54 +02007751
7752 /* Assume "got_int" was set to truncate the listing. */
7753 got_int = FALSE;
7754
7755# ifdef FEAT_BROWSE_CMD
7756 if (cmdmod.browse)
7757 {
7758 quit_more = FALSE;
7759 nr = prompt_for_number(FALSE);
7760 msg_starthere();
7761 if (nr > 0)
7762 {
7763 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
7764 (long)nr);
7765
7766 if (p != NULL)
7767 {
7768 p = expand_env_save(p);
7769 eap->arg = p;
7770 eap->cmdidx = CMD_edit;
7771 cmdmod.browse = FALSE;
7772 do_exedit(eap, NULL);
7773 vim_free(p);
7774 }
7775 }
7776 }
7777# endif
7778 }
7779}
7780#endif