blob: 0174fd6b012f927e1b5e6f25bf67753affac738f [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 Moolenaar54ee7752005-05-31 22:22:17 +0000400 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
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 Moolenaar4c3f5362006-04-11 21:38:50 +0000598 sortbuf1 = alloc((unsigned)maxlen + 1);
599 if (sortbuf1 == NULL)
600 goto sortend;
601 sortbuf2 = alloc((unsigned)maxlen + 1);
602 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);
796 new_line = lalloc(old_len - col + start_col + len + 1,
797 TRUE);
798 if (new_line == NULL)
799 break;
800 if (start_col > 0)
801 mch_memmove(new_line, ptr, (size_t)start_col);
802 mch_memmove(new_line + start_col + len,
803 ptr + col, (size_t)(old_len - col + 1));
804 ptr = new_line + start_col;
805 for (col = 0; col < len; col++)
806 ptr[col] = (col < num_tabs) ? '\t' : ' ';
807 ml_replace(lnum, new_line, FALSE);
808 if (first_line == 0)
809 first_line = lnum;
810 last_line = lnum;
811 ptr = new_line;
812 col = start_col + len;
813 }
814 }
815 got_tab = FALSE;
816 num_spaces = 0;
817 }
818 if (ptr[col] == NUL)
819 break;
820 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000822 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ++col;
825 }
826 if (new_line == NULL) /* out of memory */
827 break;
828 line_breakcheck();
829 }
830 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200833#ifdef FEAT_VARTABS
834 // If a single value was given then it can be considered equal to
835 // either the value of 'tabstop' or the value of 'vartabstop'.
836 if (tabstop_count(curbuf->b_p_vts_array) == 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200837 && tabstop_count(new_vts_array) == 1
838 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200839 ; /* not changed */
840 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar0119a592018-06-24 23:53:28 +0200841 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200842 ; /* not changed */
843 else
844 redraw_curbuf_later(NOT_VALID);
845#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (curbuf->b_p_ts != new_ts)
847 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (first_line != 0)
850 changed_lines(first_line, 0, last_line + 1, 0L);
851
852 curwin->w_p_list = save_list; /* restore 'list' */
853
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200854#ifdef FEAT_VARTABS
855 if (new_ts_str != NULL) /* set the new tabstop */
856 {
857 // If 'vartabstop' is in use or if the value given to retab has more
858 // than one tabstop then update 'vartabstop'.
859 int *old_vts_ary = curbuf->b_p_vts_array;
860
Bram Moolenaar0119a592018-06-24 23:53:28 +0200861 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200862 {
863 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
864 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0119a592018-06-24 23:53:28 +0200865 curbuf->b_p_vts_array = new_vts_array;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200866 vim_free(old_vts_ary);
867 }
868 else
869 {
870 // 'vartabstop' wasn't in use and a single value was given to
871 // retab then update 'tabstop'.
Bram Moolenaar0119a592018-06-24 23:53:28 +0200872 curbuf->b_p_ts = tabstop_first(new_vts_array);
873 vim_free(new_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200874 }
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200875 vim_free(new_ts_str);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200876 }
877#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 curbuf->b_p_ts = new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 coladvance(curwin->w_curswant);
881
882 u_clearline();
883}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
885/*
886 * :move command - move lines line1-line2 to line dest
887 *
888 * return FAIL for failure, OK otherwise
889 */
890 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100891do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 char_u *str;
894 linenr_T l;
Bram Moolenaarddd1f912018-11-10 19:19:36 +0100895 linenr_T extra; // Num lines added before line1
896 linenr_T num_lines; // Num lines moved
897 linenr_T last_line; // Last line in file after adding new text
Bram Moolenaard5f69332015-04-15 12:43:50 +0200898#ifdef FEAT_FOLDING
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100899 win_T *win;
900 tabpage_T *tp;
Bram Moolenaard5f69332015-04-15 12:43:50 +0200901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
903 if (dest >= line1 && dest < line2)
904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100905 emsg(_("E134: Cannot move a range of lines into itself"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 return FAIL;
907 }
908
Bram Moolenaarddd1f912018-11-10 19:19:36 +0100909 // Do nothing if we are not actually moving any lines. This will prevent
910 // the 'modified' flag from being set without cause.
911 if (dest == line1 - 1 || dest == line2)
912 {
913 // Move the cursor as if lines were moved (see below) to be backwards
914 // compatible.
915 if (dest >= line1)
916 curwin->w_cursor.lnum = dest;
917 else
918 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
919
920 return OK;
921 }
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 num_lines = line2 - line1 + 1;
924
925 /*
926 * First we copy the old text to its new location -- webb
927 * Also copy the flag that ":global" command uses.
928 */
929 if (u_save(dest, dest + 1) == FAIL)
930 return FAIL;
931 for (extra = 0, l = line1; l <= line2; l++)
932 {
933 str = vim_strsave(ml_get(l + extra));
934 if (str != NULL)
935 {
936 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
937 vim_free(str);
938 if (dest < line1)
939 extra++;
940 }
941 }
942
943 /*
944 * Now we must be careful adjusting our marks so that we don't overlap our
945 * mark_adjust() calls.
946 *
947 * We adjust the marks within the old text so that they refer to the
948 * last lines of the file (temporarily), because we know no other marks
949 * will be set there since these line numbers did not exist until we added
950 * our new lines.
951 *
952 * Then we adjust the marks on lines between the old and new text positions
953 * (either forwards or backwards).
954 *
955 * And Finally we adjust the marks we put at the end of the file back to
956 * their final destination at the new text position -- webb
957 */
958 last_line = curbuf->b_ml.ml_line_count;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100959 mark_adjust_nofold(line1, line2, last_line - line2, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (dest >= line2)
961 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100962 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
963#ifdef FEAT_FOLDING
964 FOR_ALL_TAB_WINDOWS(tp, win) {
965 if (win->w_buffer == curbuf)
966 foldMoveRange(&win->w_folds, line1, line2, dest);
967 }
968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 curbuf->b_op_start.lnum = dest - num_lines + 1;
970 curbuf->b_op_end.lnum = dest;
971 }
972 else
973 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100974 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
975#ifdef FEAT_FOLDING
976 FOR_ALL_TAB_WINDOWS(tp, win) {
977 if (win->w_buffer == curbuf)
978 foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
979 }
980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 curbuf->b_op_start.lnum = dest + 1;
982 curbuf->b_op_end.lnum = dest + num_lines;
983 }
984 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100985 mark_adjust_nofold(last_line - num_lines + 1, last_line,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 -(last_line - dest - extra), 0L);
987
988 /*
989 * Now we delete the original text -- webb
990 */
991 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
992 return FAIL;
993
994 for (l = line1; l <= line2; l++)
995 ml_delete(line1 + extra, TRUE);
996
997 if (!global_busy && num_lines > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100998 smsg(NGETTEXT("%ld line moved", "%ld lines moved", num_lines),
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200999 (long)num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 /*
1002 * Leave the cursor on the last of the moved lines.
1003 */
1004 if (dest >= line1)
1005 curwin->w_cursor.lnum = dest;
1006 else
1007 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
1008
1009 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +01001010 {
1011 dest += num_lines + 1;
1012 last_line = curbuf->b_ml.ml_line_count;
1013 if (dest > last_line + 1)
1014 dest = last_line + 1;
1015 changed_lines(line1, 0, dest, 0L);
1016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 else
1018 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
1019
1020 return OK;
1021}
1022
1023/*
1024 * ":copy"
1025 */
1026 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001027ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 linenr_T count;
1030 char_u *p;
1031
1032 count = line2 - line1 + 1;
1033 curbuf->b_op_start.lnum = n + 1;
1034 curbuf->b_op_end.lnum = n + count;
1035 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
1036
1037 /*
1038 * there are three situations:
1039 * 1. destination is above line1
1040 * 2. destination is between line1 and line2
1041 * 3. destination is below line2
1042 *
1043 * n = destination (when starting)
1044 * curwin->w_cursor.lnum = destination (while copying)
1045 * line1 = start of source (while copying)
1046 * line2 = end of source (while copying)
1047 */
1048 if (u_save(n, n + 1) == FAIL)
1049 return;
1050
1051 curwin->w_cursor.lnum = n;
1052 while (line1 <= line2)
1053 {
1054 /* need to use vim_strsave() because the line will be unlocked within
1055 * ml_append() */
1056 p = vim_strsave(ml_get(line1));
1057 if (p != NULL)
1058 {
1059 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
1060 vim_free(p);
1061 }
1062 /* situation 2: skip already copied lines */
1063 if (line1 == n)
1064 line1 = curwin->w_cursor.lnum;
1065 ++line1;
1066 if (curwin->w_cursor.lnum < line1)
1067 ++line1;
1068 if (curwin->w_cursor.lnum < line2)
1069 ++line2;
1070 ++curwin->w_cursor.lnum;
1071 }
1072
1073 appended_lines_mark(n, count);
1074
1075 msgmore((long)count);
1076}
1077
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001078static char_u *prevcmd = NULL; /* the previous command */
1079
1080#if defined(EXITFREE) || defined(PROTO)
1081 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001082free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001083{
1084 vim_free(prevcmd);
1085}
1086#endif
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088/*
1089 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
1090 * Bangs in the argument are replaced with the previously entered command.
1091 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
1093 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001094do_bang(
1095 int addr_count,
1096 exarg_T *eap,
1097 int forceit,
1098 int do_in,
1099 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 char_u *arg = eap->arg; /* command */
1102 linenr_T line1 = eap->line1; /* start of range */
1103 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 char_u *newcmd = NULL; /* the new command */
1105 int free_newcmd = FALSE; /* need to free() newcmd */
1106 int ins_prevcmd;
1107 char_u *t;
1108 char_u *p;
1109 char_u *trailarg;
1110 int len;
1111 int scroll_save = msg_scroll;
1112
1113 /*
1114 * Disallow shell commands for "rvim".
1115 * Disallow shell commands from .exrc and .vimrc in current directory for
1116 * security reasons.
1117 */
1118 if (check_restricted() || check_secure())
1119 return;
1120
1121 if (addr_count == 0) /* :! */
1122 {
1123 msg_scroll = FALSE; /* don't scroll here */
1124 autowrite_all();
1125 msg_scroll = scroll_save;
1126 }
1127
1128 /*
1129 * Try to find an embedded bang, like in :!<cmd> ! [args]
1130 * (:!! is indicated by the 'forceit' variable)
1131 */
1132 ins_prevcmd = forceit;
1133 trailarg = arg;
1134 do
1135 {
1136 len = (int)STRLEN(trailarg) + 1;
1137 if (newcmd != NULL)
1138 len += (int)STRLEN(newcmd);
1139 if (ins_prevcmd)
1140 {
1141 if (prevcmd == NULL)
1142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001143 emsg(_(e_noprev));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 vim_free(newcmd);
1145 return;
1146 }
1147 len += (int)STRLEN(prevcmd);
1148 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001149 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 vim_free(newcmd);
1152 return;
1153 }
1154 *t = NUL;
1155 if (newcmd != NULL)
1156 STRCAT(t, newcmd);
1157 if (ins_prevcmd)
1158 STRCAT(t, prevcmd);
1159 p = t + STRLEN(t);
1160 STRCAT(t, trailarg);
1161 vim_free(newcmd);
1162 newcmd = t;
1163
1164 /*
1165 * Scan the rest of the argument for '!', which is replaced by the
1166 * previous command. "\!" is replaced by "!" (this is vi compatible).
1167 */
1168 trailarg = NULL;
1169 while (*p)
1170 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001171 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001174 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else
1176 {
1177 trailarg = p;
1178 *trailarg++ = NUL;
1179 ins_prevcmd = TRUE;
1180 break;
1181 }
1182 }
1183 ++p;
1184 }
1185 } while (trailarg != NULL);
1186
1187 vim_free(prevcmd);
1188 prevcmd = newcmd;
1189
1190 if (bangredo) /* put cmd in redo buffer for ! command */
1191 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001192 /* If % or # appears in the command, it must have been escaped.
1193 * Reescape them, so that redoing them does not substitute them by the
1194 * buffername. */
1195 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1196
1197 if (cmd != NULL)
1198 {
1199 AppendToRedobuffLit(cmd, -1);
1200 vim_free(cmd);
1201 }
1202 else
1203 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 AppendToRedobuff((char_u *)"\n");
1205 bangredo = FALSE;
1206 }
1207 /*
1208 * Add quotes around the command, for shells that need them.
1209 */
1210 if (*p_shq != NUL)
1211 {
1212 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1213 if (newcmd == NULL)
1214 return;
1215 STRCPY(newcmd, p_shq);
1216 STRCAT(newcmd, prevcmd);
1217 STRCAT(newcmd, p_shq);
1218 free_newcmd = TRUE;
1219 }
1220 if (addr_count == 0) /* :! */
1221 {
1222 /* echo the command */
1223 msg_start();
1224 msg_putchar(':');
1225 msg_putchar('!');
1226 msg_outtrans(newcmd);
1227 msg_clr_eos();
1228 windgoto(msg_row, msg_col);
1229
1230 do_shell(newcmd, 0);
1231 }
1232 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 /* Careful: This may recursively call do_bang() again! (because of
1235 * autocommands) */
1236 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001237 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarade00832006-03-10 21:46:58 +00001238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (free_newcmd)
1240 vim_free(newcmd);
1241}
1242
1243/*
1244 * do_filter: filter lines through a command given by the user
1245 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246 * We mostly use temp files and the call_shell() routine here. This would
1247 * normally be done using pipes on a UNIX machine, but this is more portable
1248 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 * to deal with redirection somehow, and should handle things like looking
1250 * at the PATH env. variable, and adding reasonable extensions to the
1251 * command name given by the user. All reasonable versions of call_shell()
1252 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001253 * Alternatively, if on Unix and redirecting input or output, but not both,
1254 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * We use input redirection if do_in is TRUE.
1256 * We use output redirection if do_out is TRUE.
1257 */
1258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001259do_filter(
1260 linenr_T line1,
1261 linenr_T line2,
1262 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1263 char_u *cmd,
1264 int do_in,
1265 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 char_u *itmp = NULL;
1268 char_u *otmp = NULL;
1269 linenr_T linecount;
1270 linenr_T read_linecount;
1271 pos_T cursor_save;
1272 char_u *cmd_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 buf_T *old_curbuf = curbuf;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001274 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276 if (*cmd == NUL) /* no filter command */
1277 return;
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 cursor_save = curwin->w_cursor;
1280 linecount = line2 - line1 + 1;
1281 curwin->w_cursor.lnum = line1;
1282 curwin->w_cursor.col = 0;
1283 changed_line_abv_curs();
1284 invalidate_botline();
1285
1286 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001287 * When using temp files:
1288 * 1. * Form temp file names
1289 * 2. * Write the lines to a temp file
1290 * 3. Run the filter command on the temp file
1291 * 4. * Read the output of the command into the buffer
1292 * 5. * Delete the original lines to be filtered
1293 * 6. * Remove the temp files
1294 *
1295 * When writing the input with a pipe or when catching the output with a
1296 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001299 if (do_out)
1300 shell_flags |= SHELL_DOOUT;
1301
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001302#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001303 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001305 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1306 shell_flags |= SHELL_READ;
1307 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001309 else if (do_in && !do_out && !p_stmp)
1310 {
1311 /* Use a pipe to write stdin of the command, do not use a temp file. */
1312 shell_flags |= SHELL_WRITE;
1313 curbuf->b_op_start.lnum = line1;
1314 curbuf->b_op_end.lnum = line2;
1315 }
1316 else if (do_in && do_out && !p_stmp)
1317 {
1318 /* Use a pipe to write stdin and fetch stdout of the command, do not
1319 * use a temp file. */
1320 shell_flags |= SHELL_READ|SHELL_WRITE;
1321 curbuf->b_op_start.lnum = line1;
1322 curbuf->b_op_end.lnum = line2;
1323 curwin->w_cursor.lnum = line2;
1324 }
1325 else
1326#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001327 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1328 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001330 emsg(_(e_notmp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001331 goto filterend;
1332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334/*
1335 * The writing and reading of temp files will not be shown.
1336 * Vi also doesn't do this and the messages are not very informative.
1337 */
1338 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001339 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 FALSE, FALSE, FALSE, TRUE) == FAIL)
1341 {
1342 msg_putchar('\n'); /* keep message from buf_write() */
1343 --no_wait_return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001344#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (!aborting())
1346#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001347 (void)semsg(_(e_notcreate), itmp); /* will call wait_return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 goto filterend;
1349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (curbuf != old_curbuf)
1351 goto filterend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 if (!do_out)
1354 msg_putchar('\n');
1355
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001356 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1358 if (cmd_buf == NULL)
1359 goto filterend;
1360
1361 windgoto((int)Rows - 1, 0);
1362 cursor_on();
1363
1364 /*
1365 * When not redirecting the output the command can write anything to the
1366 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1367 * stderr output of external command. Clear the screen later.
1368 * If do_in is FALSE, this could be something like ":r !cat", which may
1369 * also mess up the screen, clear it later.
1370 */
1371 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1372 redraw_later_clear();
1373
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001374 if (do_out)
1375 {
1376 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001377 {
1378 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001379 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001380 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001381 redraw_curbuf_later(VALID);
1382 }
1383 read_linecount = curbuf->b_ml.ml_line_count;
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /*
1386 * When call_shell() fails wait_return() is called to give the user a
1387 * chance to read the error messages. Otherwise errors are ignored, so you
1388 * can see the error messages from the command that appear on stdout; use
1389 * 'u' to fix the text
1390 * Switch to cooked mode when not redirecting stdin, avoids that something
1391 * like ":r !cat" hangs.
1392 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1393 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001394 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 redraw_later_clear();
1397 wait_return(FALSE);
1398 }
1399 vim_free(cmd_buf);
1400
1401 did_check_timestamps = FALSE;
1402 need_check_timestamps = TRUE;
1403
1404 /* When interrupting the shell command, it may still have produced some
1405 * useful output. Reset got_int here, so that readfile() won't cancel
1406 * reading. */
1407 ui_breakcheck();
1408 got_int = FALSE;
1409
1410 if (do_out)
1411 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001412 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001414 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001415 eap, READ_FILTER) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001417#if defined(FEAT_EVAL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001418 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001420 {
1421 msg_putchar('\n');
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001422 semsg(_(e_notread), otmp);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001423 }
1424 goto error;
1425 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001426 if (curbuf != old_curbuf)
1427 goto filterend;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001428 }
1429
1430 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1431
1432 if (shell_flags & SHELL_READ)
1433 {
1434 curbuf->b_op_start.lnum = line2 + 1;
1435 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1436 appended_lines_mark(line2, read_linecount);
1437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 if (do_in)
1440 {
1441 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (read_linecount >= linecount)
1444 /* move all marks from old lines to new lines */
1445 mark_adjust(line1, line2, linecount, 0L);
1446 else
1447 {
1448 /* move marks from old lines to new lines, delete marks
1449 * that are in deleted lines */
1450 mark_adjust(line1, line1 + read_linecount - 1,
1451 linecount, 0L);
1452 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1453 }
1454 }
1455
1456 /*
1457 * Put cursor on first filtered line for ":range!cmd".
1458 * Adjust '[ and '] (set by buf_write()).
1459 */
1460 curwin->w_cursor.lnum = line1;
1461 del_lines(linecount, TRUE);
1462 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1463 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1464 write_lnum_adjust(-linecount); /* adjust last line
1465 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001466#ifdef FEAT_FOLDING
1467 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 else
1471 {
1472 /*
1473 * Put cursor on last new line for ":r !cmd".
1474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001476 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1480 --no_wait_return;
1481
1482 if (linecount > p_report)
1483 {
1484 if (do_in)
1485 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001486 vim_snprintf(msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00001487 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 /* save message to display it after redraw */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001490 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 else
1493 msgmore((long)linecount);
1494 }
1495 }
1496 else
1497 {
1498error:
1499 /* put cursor back in same position for ":w !cmd" */
1500 curwin->w_cursor = cursor_save;
1501 --no_wait_return;
1502 wait_return(FALSE);
1503 }
1504
1505filterend:
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (curbuf != old_curbuf)
1508 {
1509 --no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001510 emsg(_("E135: *Filter* Autocommands must not change current buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (itmp != NULL)
1513 mch_remove(itmp);
1514 if (otmp != NULL)
1515 mch_remove(otmp);
1516 vim_free(itmp);
1517 vim_free(otmp);
1518}
1519
1520/*
1521 * Call a shell to execute a command.
1522 * When "cmd" is NULL start an interactive shell.
1523 */
1524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001525do_shell(
1526 char_u *cmd,
1527 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 buf_T *buf;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001530#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 int save_nwr;
1532#endif
1533#ifdef MSWIN
1534 int winstart = FALSE;
Bram Moolenaar049ca592019-03-17 16:59:44 +01001535 int keep_termcap = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
1537
1538 /*
1539 * Disallow shell commands for "rvim".
1540 * Disallow shell commands from .exrc and .vimrc in current directory for
1541 * security reasons.
1542 */
1543 if (check_restricted() || check_secure())
1544 {
1545 msg_end();
1546 return;
1547 }
1548
1549#ifdef MSWIN
1550 /*
Bram Moolenaar049ca592019-03-17 16:59:44 +01001551 * Check if ":!start" is used. This implies not stopping termcap mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
1553 if (cmd != NULL)
Bram Moolenaar049ca592019-03-17 16:59:44 +01001554 keep_termcap = winstart = (STRNICMP(cmd, "start ", 6) == 0);
1555
1556# if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
1557 // Don't stop termcap mode when using a terminal window for the shell.
1558 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
1559 keep_termcap = TRUE;
1560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#endif
1562
1563 /*
1564 * For autocommands we want to get the output on the current screen, to
1565 * avoid having to type return below.
1566 */
Bram Moolenaar049ca592019-03-17 16:59:44 +01001567 msg_putchar('\r'); // put cursor at start of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570#ifdef MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001571 if (!keep_termcap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#endif
1573 stoptermcap();
1574 }
1575#ifdef MSWIN
1576 if (!winstart)
1577#endif
Bram Moolenaar049ca592019-03-17 16:59:44 +01001578 msg_putchar('\n'); // may shift screen one line up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaar049ca592019-03-17 16:59:44 +01001580 // warning message before calling the shell
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001581 if (p_warn && !autocmd_busy && msg_silent == 0)
Bram Moolenaar29323592016-07-24 22:04:11 +02001582 FOR_ALL_BUFFERS(buf)
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01001583 if (bufIsChangedNotTerm(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585#ifdef FEAT_GUI_MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001586 if (!keep_termcap)
1587 starttermcap(); // don't want a message box here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01001589 msg_puts(_("[No write since last change]\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#ifdef FEAT_GUI_MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001591 if (!keep_termcap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 stoptermcap();
1593#endif
1594 break;
1595 }
1596
Bram Moolenaar049ca592019-03-17 16:59:44 +01001597 // This windgoto is required for when the '\n' resulted in a "delete line
1598 // 1" command to the terminal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (!swapping_screen())
1600 windgoto(msg_row, msg_col);
1601 cursor_on();
1602 (void)call_shell(cmd, SHELL_COOKED | flags);
1603 did_check_timestamps = FALSE;
1604 need_check_timestamps = TRUE;
1605
1606 /*
1607 * put the message cursor at the end of the screen, avoids wait_return()
1608 * to overwrite the text that the external command showed
1609 */
1610 if (!swapping_screen())
1611 {
1612 msg_row = Rows - 1;
1613 msg_col = 0;
1614 }
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (autocmd_busy)
1617 {
1618 if (msg_silent == 0)
1619 redraw_later_clear();
1620 }
1621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 /*
1624 * For ":sh" there is no need to call wait_return(), just redraw.
1625 * Also for the Win32 GUI (the output is in a console window).
1626 * Otherwise there is probably text on the screen that the user wants
1627 * to read before redrawing, so call wait_return().
1628 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001629#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
1630# ifdef VIMDLL
1631 if (!gui.in_use)
1632# endif
1633 {
1634 if (cmd == NULL
Bram Moolenaar4f974752019-02-17 17:44:42 +01001635# ifdef MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001636 || (keep_termcap && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001638 )
1639 {
1640 if (msg_silent == 0)
1641 redraw_later_clear();
1642 need_wait_return = FALSE;
1643 }
1644 else
1645 {
1646 /*
1647 * If we switch screens when starttermcap() is called, we
1648 * really want to wait for "hit return to continue".
1649 */
1650 save_nwr = no_wait_return;
1651 if (swapping_screen())
1652 no_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653# ifdef AMIGA
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001654 wait_return(term_console ? -1 : msg_silent == 0); // see below
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655# else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001656 wait_return(msg_silent == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001658 no_wait_return = save_nwr;
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01001661#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663#ifdef MSWIN
Bram Moolenaar049ca592019-03-17 16:59:44 +01001664 if (!keep_termcap) // if keep_termcap is TRUE didn't stop termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#endif
Bram Moolenaar049ca592019-03-17 16:59:44 +01001666 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 /*
1669 * In an Amiga window redrawing is caused by asking the window size.
1670 * If we got an interrupt this will not work. The chance that the
1671 * window size is wrong is very small, but we need to redraw the
1672 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1673 * but it saves an extra redraw.
1674 */
1675#ifdef AMIGA
1676 if (skip_redraw) /* ':' hit in wait_return() */
1677 {
1678 if (msg_silent == 0)
1679 redraw_later_clear();
1680 }
1681 else if (term_console)
1682 {
1683 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1684 if (got_int && msg_silent == 0)
1685 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1686 else
1687 must_redraw = 0; /* no extra redraw needed */
1688 }
1689#endif
1690 }
1691
1692 /* display any error messages now */
1693 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001694
Bram Moolenaarade00832006-03-10 21:46:58 +00001695 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696}
1697
Bram Moolenaar06640892018-10-09 21:49:33 +02001698#if !defined(UNIX)
1699 static char_u *
1700find_pipe(char_u *cmd)
1701{
1702 char_u *p;
1703 int inquote = FALSE;
1704
1705 for (p = cmd; *p != NUL; ++p)
1706 {
1707 if (!inquote && *p == '|')
1708 return p;
1709 if (*p == '"')
1710 inquote = !inquote;
1711 else if (rem_backslash(p))
1712 ++p;
1713 }
1714 return NULL;
1715}
1716#endif
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718/*
1719 * Create a shell command from a command string, input redirection file and
1720 * output redirection file.
1721 * Returns an allocated string with the shell command, or NULL for failure.
1722 */
1723 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001724make_filter_cmd(
1725 char_u *cmd, /* command */
1726 char_u *itmp, /* NULL or name of input file */
1727 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 char_u *buf;
1730 long_u len;
1731
Bram Moolenaar53076832015-12-31 19:53:21 +01001732#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001733 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001734 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001735
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001736 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001737 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1738 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001739 if (is_fish_shell)
1740 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1741 else
1742#endif
1743 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (itmp != NULL)
1745 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1746 if (otmp != NULL)
1747 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1748 buf = lalloc(len, TRUE);
1749 if (buf == NULL)
1750 return NULL;
1751
Bram Moolenaar53076832015-12-31 19:53:21 +01001752#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001754 * Put braces around the command (for concatenated commands) when
1755 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001757 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001758 {
1759 if (is_fish_shell)
1760 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1761 else
1762 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1763 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001764 else
1765 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (itmp != NULL)
1767 {
1768 STRCAT(buf, " < ");
1769 STRCAT(buf, itmp);
1770 }
1771#else
1772 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001773 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 * the use of commands in a pipe.
1775 */
1776 STRCPY(buf, cmd);
1777 if (itmp != NULL)
1778 {
1779 char_u *p;
1780
1781 /*
1782 * If there is a pipe, we have to put the '<' in front of it.
1783 * Don't do this when 'shellquote' is not empty, otherwise the
1784 * redirection would be inside the quotes.
1785 */
1786 if (*p_shq == NUL)
1787 {
Bram Moolenaar06640892018-10-09 21:49:33 +02001788 p = find_pipe(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (p != NULL)
1790 *p = NUL;
1791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1793 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (*p_shq == NUL)
1795 {
Bram Moolenaar06640892018-10-09 21:49:33 +02001796 p = find_pipe(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (p != NULL)
1798 {
1799 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1800 STRCAT(buf, p);
1801 }
1802 }
1803 }
1804#endif
1805 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001806 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 return buf;
1809}
1810
1811/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001812 * Append output redirection for file "fname" to the end of string buffer
1813 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 * Works with the 'shellredir' and 'shellpipe' options.
1815 * The caller should make sure that there is enough room:
1816 * STRLEN(opt) + STRLEN(fname) + 3
1817 */
1818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001819append_redir(
1820 char_u *buf,
1821 int buflen,
1822 char_u *opt,
1823 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001826 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001828 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001829 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001831 {
1832 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001834 if (p[1] == '%') /* skip %% */
1835 ++p;
1836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (p != NULL)
1838 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001839 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1840 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1841 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001844 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 " %s %s",
1847#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#endif
1850 (char *)opt, (char *)fname);
1851}
1852
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001853#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001855static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001856static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001857static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858static int viminfo_errcnt;
1859
1860 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001861no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862{
1863 /* "vim -i NONE" does not read or write a viminfo file */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001864 return STRCMP(p_viminfofile, "NONE") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865}
1866
1867/*
1868 * Report an error for reading a viminfo file.
1869 * Count the number of errors. When there are more than 10, return TRUE.
1870 */
1871 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001872viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001874 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1875 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001876 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001877 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1878 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001879 emsg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (++viminfo_errcnt >= 10)
1881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 emsg(_("E136: viminfo: Too many errors, skipping rest of file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 return TRUE;
1884 }
1885 return FALSE;
1886}
1887
1888/*
1889 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001890 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 */
1892 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001893read_viminfo(
1894 char_u *file, /* file name or NULL to use default name */
1895 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 FILE *fp;
1898 char_u *fname;
1899
1900 if (no_viminfo())
1901 return FAIL;
1902
Bram Moolenaard812df62008-11-09 12:46:09 +00001903 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (fname == NULL)
1905 return FAIL;
1906 fp = mch_fopen((char *)fname, READBIN);
1907
1908 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001909 {
1910 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001911 smsg(_("Reading viminfo file \"%s\"%s%s%s"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00001912 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001913 (flags & VIF_WANT_INFO) ? _(" info") : "",
1914 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1915 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001916 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001917 verbose_leave();
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 vim_free(fname);
1921 if (fp == NULL)
1922 return FAIL;
1923
1924 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001925 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 return OK;
1929}
1930
1931/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001932 * Write the viminfo file. The old one is read in first so that effectively a
1933 * merge of current info and old info is done. This allows multiple vims to
1934 * run simultaneously, without losing any marks etc.
1935 * If "forceit" is TRUE, then the old file is not read in, and only internal
1936 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 */
1938 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001939write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
1941 char_u *fname;
1942 FILE *fp_in = NULL; /* input viminfo file, if any */
1943 FILE *fp_out = NULL; /* output viminfo file */
1944 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001945 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946#if defined(UNIX) || defined(VMS)
1947 mode_t umask_save;
1948#endif
1949#ifdef UNIX
1950 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001951 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001953#ifdef MSWIN
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001954 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
1957 if (no_viminfo())
1958 return;
1959
1960 fname = viminfo_filename(file); /* may set to default if NULL */
1961 if (fname == NULL)
1962 return;
1963
1964 fp_in = mch_fopen((char *)fname, READBIN);
1965 if (fp_in == NULL)
1966 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001967 int fd;
1968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /* if it does exist, but we can't read it, don't try writing */
1970 if (mch_stat((char *)fname, &st_new) == 0)
1971 goto end;
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001972
1973 /* Create the new .viminfo non-accessible for others, because it may
1974 * contain text from non-accessible documents. It is up to the user to
1975 * widen access (e.g. to a group). This may also fail if there is a
1976 * race condition, then just give up. */
1977 fd = mch_open((char *)fname,
1978 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1979 if (fd < 0)
1980 goto end;
1981 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983 else
1984 {
1985 /*
1986 * There is an existing viminfo file. Create a temporary file to
1987 * write the new viminfo into, in the same directory as the
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001988 * existing viminfo file, which will be renamed once all writing is
1989 * successful.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
1991#ifdef UNIX
1992 /*
1993 * For Unix we check the owner of the file. It's not very nice to
1994 * overwrite a user's viminfo file after a "su root", with a
1995 * viminfo file that the user can't read.
1996 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001997 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001998 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00002000 if (mch_stat((char *)fname, &st_old) == 0
2001 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00002002 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 ? (st_old.st_mode & 0200)
2004 : (st_old.st_gid == getgid()
2005 ? (st_old.st_mode & 0020)
2006 : (st_old.st_mode & 0002))))
2007 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002008 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 semsg(_("E137: Viminfo file is not writable: %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002013 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 goto end;
2015 }
2016#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002017#ifdef MSWIN
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002018 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002019 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002023 * Make tempname, find one that does not exist yet.
2024 * Beware of a race condition: If someone logs out and all Vim
2025 * instances exit at the same time a temp file might be created between
2026 * stat() and open(). Use mch_open() with O_EXCL to avoid that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * May try twice: Once normal and once with shortname set, just in
2028 * case somebody puts his viminfo file in an 8.3 filesystem.
2029 */
2030 for (;;)
2031 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002032 int next_char = 'z';
2033 char_u *wp;
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 tempname = buf_modname(
2036#ifdef UNIX
2037 shortname,
2038#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#endif
2041 fname,
2042#ifdef VMS
2043 (char_u *)"-tmp",
2044#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046#endif
2047 FALSE);
2048 if (tempname == NULL) /* out of memory */
2049 break;
2050
2051 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002052 * Try a series of names. Change one character, just before
2053 * the extension. This should also work for an 8.3
2054 * file name, when after adding the extension it still is
2055 * the same file as the original.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002057 wp = tempname + STRLEN(tempname) - 5;
2058 if (wp < gettail(tempname)) /* empty file name? */
2059 wp = gettail(tempname);
2060 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002062 /*
2063 * Check if tempfile already exists. Never overwrite an
2064 * existing file!
2065 */
2066 if (mch_stat((char *)tempname, &st_new) == 0)
2067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002070 * Check if tempfile is same as original file. May happen
2071 * when modname() gave the same file back. E.g. silly
2072 * link, or file name-length reached. Try again with
2073 * shortname set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002075 if (!shortname && st_new.st_dev == st_old.st_dev
2076 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002078 VIM_CLEAR(tempname);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002079 shortname = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 break;
2081 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002084 else
2085 {
2086 /* Try creating the file exclusively. This may fail if
2087 * another Vim tries to do it at the same time. */
2088#ifdef VMS
2089 /* fdopen() fails for some reason */
2090 umask_save = umask(077);
2091 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2092 (void)umask(umask_save);
2093#else
2094 int fd;
2095
2096 /* Use mch_open() to be able to use O_NOFOLLOW and set file
2097 * protection:
2098 * Unix: same as original file, but strip s-bit. Reset
2099 * umask to avoid it getting in the way.
2100 * Others: r&w for user only. */
2101# ifdef UNIX
2102 umask_save = umask(0);
2103 fd = mch_open((char *)tempname,
2104 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
2105 (int)((st_old.st_mode & 0777) | 0600));
2106 (void)umask(umask_save);
2107# else
2108 fd = mch_open((char *)tempname,
2109 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
2110# endif
2111 if (fd < 0)
2112 {
2113 fp_out = NULL;
2114# ifdef EEXIST
2115 /* Avoid trying lots of names while the problem is lack
Bram Moolenaar94747162019-02-10 21:55:26 +01002116 * of permission, only retry if the file already
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002117 * exists. */
2118 if (errno != EEXIST)
2119 break;
2120# endif
2121 }
2122 else
2123 fp_out = fdopen(fd, WRITEBIN);
2124#endif /* VMS */
2125 if (fp_out != NULL)
2126 break;
2127 }
2128
2129 /* Assume file exists, try again with another name. */
2130 if (next_char == 'a' - 1)
2131 {
2132 /* They all exist? Must be something wrong! Don't write
2133 * the viminfo file then. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002134 semsg(_("E929: Too many viminfo temp files, like %s!"),
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002135 tempname);
2136 break;
2137 }
2138 *wp = next_char;
2139 --next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002141
2142 if (tempname != NULL)
2143 break;
2144 /* continue if shortname was set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146
Bram Moolenaara5792f52005-11-23 21:25:05 +00002147#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002148 if (tempname != NULL && fp_out != NULL)
2149 {
2150 stat_T tmp_st;
2151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 /*
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002153 * Make sure the original owner can read/write the tempfile and
2154 * otherwise preserve permissions, making sure the group matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002156 if (mch_stat((char *)tempname, &tmp_st) >= 0)
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002157 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002158 if (st_old.st_uid != tmp_st.st_uid)
2159 /* Changing the owner might fail, in which case the
2160 * file will now owned by the current user, oh well. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002161 vim_ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002162 if (st_old.st_gid != tmp_st.st_gid
2163 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
2164 /* can't set the group to what it should be, remove
2165 * group permissions */
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002166 (void)mch_setperm(tempname, 0600);
2167 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002168 else
2169 /* can't stat the file, set conservative permissions */
2170 (void)mch_setperm(tempname, 0600);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002172#endif
Bram Moolenaare0aa23f2017-11-26 17:08:03 +01002173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175 /*
2176 * Check if the new viminfo file can be written to.
2177 */
2178 if (fp_out == NULL)
2179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002180 semsg(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002181 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (fp_in != NULL)
2183 fclose(fp_in);
2184 goto end;
2185 }
2186
2187 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002188 {
2189 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002190 smsg(_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002191 verbose_leave();
2192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002195 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar927030a2016-03-15 18:23:55 +01002197 if (fclose(fp_out) == EOF)
2198 ++viminfo_errcnt;
2199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (fp_in != NULL)
2201 {
2202 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002203
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002204 /* In case of an error keep the original viminfo file. Otherwise
2205 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002206 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002207 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002208 if (vim_rename(tempname, fname) == -1)
2209 {
2210 ++viminfo_errcnt;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 semsg(_("E886: Can't rename viminfo file to %s!"), fname);
Bram Moolenaar927030a2016-03-15 18:23:55 +01002212 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002213# ifdef MSWIN
Bram Moolenaar927030a2016-03-15 18:23:55 +01002214 /* If the viminfo file was hidden then also hide the new file. */
2215 else if (hidden)
2216 mch_hide(fname);
2217# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002218 }
2219 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 mch_remove(tempname);
2221 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223end:
2224 vim_free(fname);
2225 vim_free(tempname);
2226}
2227
2228/*
2229 * Get the viminfo file name to use.
2230 * If "file" is given and not empty, use it (has already been expanded by
2231 * cmdline functions).
2232 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2233 * expand environment variables.
2234 * Returns an allocated string. NULL when out of memory.
2235 */
2236 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002237viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238{
2239 if (file == NULL || *file == NUL)
2240 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002241 if (*p_viminfofile != NUL)
2242 file = p_viminfofile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2244 {
2245#ifdef VIMINFO_FILE2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246# ifdef VMS
2247 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2248# else
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002249# ifdef MSWIN
2250 /* Use $VIM only if $HOME is the default "C:/". */
2251 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
2252 && mch_getenv((char_u *)"HOME") == NULL)
2253# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (mch_getenv((char_u *)"HOME") == NULL)
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256# endif
2257 {
2258 /* don't use $VIM when not available. */
2259 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2260 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2261 file = (char_u *)VIMINFO_FILE2;
2262 else
2263 file = (char_u *)VIMINFO_FILE;
2264 }
2265 else
2266#endif
2267 file = (char_u *)VIMINFO_FILE;
2268 }
2269 expand_env(file, NameBuff, MAXPATHL);
2270 file = NameBuff;
2271 }
2272 return vim_strsave(file);
2273}
2274
2275/*
2276 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2277 */
2278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002279do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int eof = FALSE;
2282 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002283 int merge = FALSE;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002284 int do_copy_marks = FALSE;
2285 garray_T buflist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2288 return;
2289 vir.vir_fd = fp_in;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 vir.vir_conv.vc_type = CONV_NONE;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002291 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002292 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294 if (fp_in != NULL)
2295 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002296 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002297 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002298 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002299 {
2300 /* Registers and marks are read and kept separate from what
2301 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002302 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002303 prepare_viminfo_marks();
2304 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002305
Bram Moolenaard812df62008-11-09 12:46:09 +00002306 eof = read_viminfo_up_to_marks(&vir,
2307 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002308 merge = TRUE;
2309 }
2310 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 /* Skip info, find start of marks */
2312 while (!(eof = viminfo_readline(&vir))
2313 && vir.vir_line[0] != '>')
2314 ;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002315
2316 do_copy_marks = (flags &
2317 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (fp_out != NULL)
2321 {
2322 /* Write the info: */
2323 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2324 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002325 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002326 write_viminfo_version(fp_out);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002327 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 write_viminfo_search_pattern(fp_out);
2330 write_viminfo_sub_string(fp_out);
2331#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002332 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#endif
2334 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002335 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#ifdef FEAT_EVAL
2337 write_viminfo_varlist(fp_out);
2338#endif
2339 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002340 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002342 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002343
2344 if (do_copy_marks)
2345 ga_init2(&buflist, sizeof(buf_T *), 50);
2346 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002348
2349 if (do_copy_marks)
2350 {
2351 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2352 if (fp_out != NULL)
2353 ga_clear(&buflist);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 vim_free(vir.vir_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 if (vir.vir_conv.vc_type != CONV_NONE)
2358 convert_setup(&vir.vir_conv, NULL, NULL);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002359 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360}
2361
2362/*
2363 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2364 * first part of the viminfo file which contains everything but the marks that
2365 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2366 */
2367 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002368read_viminfo_up_to_marks(
2369 vir_T *virp,
2370 int forceit,
2371 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 int eof;
2374 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002375 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002378 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 eof = viminfo_readline(virp);
2382 while (!eof && virp->vir_line[0] != '>')
2383 {
2384 switch (virp->vir_line[0])
2385 {
2386 /* Characters reserved for future expansion, ignored now */
2387 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case '^': /* to be defined */
2389 case '<': /* long line - ignored */
2390 /* A comment or empty line. */
2391 case NUL:
2392 case '\r':
2393 case '\n':
2394 case '#':
2395 eof = viminfo_readline(virp);
2396 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002397 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002398 eof = read_viminfo_barline(virp, got_encoding,
2399 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002402 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 eof = viminfo_encoding(virp);
2404 break;
2405 case '!': /* global variable */
2406#ifdef FEAT_EVAL
2407 eof = read_viminfo_varlist(virp, writing);
2408#else
2409 eof = viminfo_readline(virp);
2410#endif
2411 break;
2412 case '%': /* entry for buffer list */
2413 eof = read_viminfo_bufferlist(virp, writing);
2414 break;
2415 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002416 /* When registers are in bar lines skip the old style register
2417 * lines. */
2418 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2419 eof = read_viminfo_register(virp, forceit);
2420 else
2421 do {
2422 eof = viminfo_readline(virp);
2423 } while (!eof && (virp->vir_line[0] == TAB
2424 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 break;
2426 case '/': /* Search string */
2427 case '&': /* Substitute search string */
2428 case '~': /* Last search string, followed by '/' or '&' */
2429 eof = read_viminfo_search_pattern(virp, forceit);
2430 break;
2431 case '$':
2432 eof = read_viminfo_sub_string(virp, forceit);
2433 break;
2434 case ':':
2435 case '?':
2436 case '=':
2437 case '@':
2438#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002439 /* When history is in bar lines skip the old style history
2440 * lines. */
2441 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2442 eof = read_viminfo_history(virp, writing);
2443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002445 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 break;
2447 case '-':
2448 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002449 /* When file marks are in bar lines skip the old style lines. */
2450 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2451 eof = read_viminfo_filemark(virp, forceit);
2452 else
2453 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 break;
2455 default:
2456 if (viminfo_error("E575: ", _("Illegal starting char"),
2457 virp->vir_line))
2458 eof = TRUE;
2459 else
2460 eof = viminfo_readline(virp);
2461 break;
2462 }
2463 }
2464
2465#ifdef FEAT_CMDHIST
2466 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002467 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002468 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469#endif
2470
2471 /* Change file names to buffer numbers for fmarks. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002472 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 fmarks_check_names(buf);
2474
2475 return eof;
2476}
2477
2478/*
2479 * Compare the 'encoding' value in the viminfo file with the current value of
2480 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2481 * conversion of text with iconv() in viminfo_readstring().
2482 */
2483 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002484viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 char_u *p;
2487 int i;
2488
2489 if (get_viminfo_parameter('c') != 0)
2490 {
2491 p = vim_strchr(virp->vir_line, '=');
2492 if (p != NULL)
2493 {
2494 /* remove trailing newline */
2495 ++p;
2496 for (i = 0; vim_isprintc(p[i]); ++i)
2497 ;
2498 p[i] = NUL;
2499
2500 convert_setup(&virp->vir_conv, p, p_enc);
2501 }
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return viminfo_readline(virp);
2504}
2505
2506/*
2507 * Read a line from the viminfo file.
2508 * Returns TRUE for end-of-file;
2509 */
2510 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002511viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2514}
2515
2516/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002517 * Check string read from viminfo file.
2518 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * - replace CTRL-V CTRL-V with CTRL-V
2520 * - replace CTRL-V 'n' with '\n'
2521 *
2522 * Check for a long line as written by viminfo_writestring().
2523 *
2524 * Return the string in allocated memory (NULL when out of memory).
2525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002527viminfo_readstring(
2528 vir_T *virp,
2529 int off, /* offset for virp->vir_line */
2530 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 char_u *retval;
2533 char_u *s, *d;
2534 long len;
2535
2536 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2537 {
2538 len = atol((char *)virp->vir_line + off + 1);
2539 retval = lalloc(len, TRUE);
2540 if (retval == NULL)
2541 {
2542 /* Line too long? File messed up? Skip next line. */
2543 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2544 return NULL;
2545 }
2546 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2547 s = retval + 1; /* Skip the leading '<' */
2548 }
2549 else
2550 {
2551 retval = vim_strsave(virp->vir_line + off);
2552 if (retval == NULL)
2553 return NULL;
2554 s = retval;
2555 }
2556
2557 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2558 d = retval;
2559 while (*s != NUL && *s != '\n')
2560 {
2561 if (s[0] == Ctrl_V && s[1] != NUL)
2562 {
2563 if (s[1] == 'n')
2564 *d++ = '\n';
2565 else
2566 *d++ = Ctrl_V;
2567 s += 2;
2568 }
2569 else
2570 *d++ = *s++;
2571 }
2572 *d = NUL;
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2575 {
2576 d = string_convert(&virp->vir_conv, retval, NULL);
2577 if (d != NULL)
2578 {
2579 vim_free(retval);
2580 retval = d;
2581 }
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 return retval;
2585}
2586
2587/*
2588 * write string to viminfo file
2589 * - replace CTRL-V with CTRL-V CTRL-V
2590 * - replace '\n' with CTRL-V 'n'
2591 * - add a '\n' at the end
2592 *
2593 * For a long line:
2594 * - write " CTRL-V <length> \n " in first line
2595 * - write " < <string> \n " in second line
2596 */
2597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002598viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 int c;
2601 char_u *s;
2602 int len = 0;
2603
2604 for (s = p; *s != NUL; ++s)
2605 {
2606 if (*s == Ctrl_V || *s == '\n')
2607 ++len;
2608 ++len;
2609 }
2610
2611 /* If the string will be too long, write its length and put it in the next
2612 * line. Take into account that some room is needed for what comes before
2613 * the string (e.g., variable name). Add something to the length for the
2614 * '<', NL and trailing NUL. */
2615 if (len > LSIZE / 2)
2616 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2617
2618 while ((c = *p++) != NUL)
2619 {
2620 if (c == Ctrl_V || c == '\n')
2621 {
2622 putc(Ctrl_V, fd);
2623 if (c == '\n')
2624 c = 'n';
2625 }
2626 putc(c, fd);
2627 }
2628 putc('\n', fd);
2629}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002630
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002631/*
2632 * Write a string in quotes that barline_parse() can read back.
2633 * Breaks the line in less than LSIZE pieces when needed.
2634 * Returns remaining characters in the line.
2635 */
2636 int
2637barline_writestring(FILE *fd, char_u *s, int remaining_start)
2638{
2639 char_u *p;
2640 int remaining = remaining_start;
2641 int len = 2;
2642
2643 /* Count the number of characters produced, including quotes. */
2644 for (p = s; *p != NUL; ++p)
2645 {
2646 if (*p == NL)
2647 len += 2;
2648 else if (*p == '"' || *p == '\\')
2649 len += 2;
2650 else
2651 ++len;
2652 }
Bram Moolenaar25709572016-08-26 20:41:16 +02002653 if (len > remaining - 2)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002654 {
2655 fprintf(fd, ">%d\n|<", len);
2656 remaining = LSIZE - 20;
2657 }
2658
2659 putc('"', fd);
2660 for (p = s; *p != NUL; ++p)
2661 {
2662 if (*p == NL)
2663 {
2664 putc('\\', fd);
2665 putc('n', fd);
2666 --remaining;
2667 }
2668 else if (*p == '"' || *p == '\\')
2669 {
2670 putc('\\', fd);
2671 putc(*p, fd);
2672 --remaining;
2673 }
2674 else
2675 putc(*p, fd);
2676 --remaining;
2677
2678 if (remaining < 3)
2679 {
2680 putc('\n', fd);
2681 putc('|', fd);
2682 putc('<', fd);
2683 /* Leave enough space for another continuation. */
2684 remaining = LSIZE - 20;
2685 }
2686 }
2687 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002688 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002689}
2690
2691/*
2692 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002693 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002694 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002695 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002696 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002697barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002698{
2699 char_u *p = text;
2700 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002701 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002702 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002703 int i;
2704 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002705 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002706 char_u *sconv;
2707 int converted;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002708
2709 while (*p == ',')
2710 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002711 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002712 if (ga_grow(values, 1) == FAIL)
2713 break;
2714 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002715
2716 if (*p == '>')
2717 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002718 /* Need to read a continuation line. Put strings in allocated
2719 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002720 if (!allocated)
2721 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002722 for (i = 0; i < values->ga_len; ++i)
2723 {
2724 bval_T *vp = (bval_T *)(values->ga_data) + i;
2725
2726 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002727 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002728 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2729 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002730 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002731 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002732 allocated = TRUE;
2733 }
2734
2735 if (vim_isdigit(p[1]))
2736 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002737 size_t len;
2738 size_t todo;
2739 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002740
2741 /* String value was split into lines that are each shorter
2742 * than LSIZE:
2743 * |{bartype},>{length of "{text}{text2}"}
2744 * |<"{text1}
2745 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002746 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002747 */
2748 ++p;
2749 len = getdigits(&p);
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002750 buf = alloc((int)(len + 1));
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002751 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002752 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002753 p = buf;
2754 for (todo = len; todo > 0; todo -= n)
2755 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002756 eof = viminfo_readline(virp);
2757 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002758 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002759 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002760 /* File was truncated or garbled. Read another line if
2761 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002762 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002763 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002764 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002765 /* Get length of text, excluding |< and NL chars. */
2766 n = STRLEN(virp->vir_line);
2767 while (n > 0 && (virp->vir_line[n - 1] == NL
2768 || virp->vir_line[n - 1] == CAR))
2769 --n;
2770 n -= 2;
2771 if (n > todo)
2772 {
2773 /* more values follow after the string */
2774 nextp = virp->vir_line + 2 + todo;
2775 n = todo;
2776 }
2777 mch_memmove(p, virp->vir_line + 2, n);
2778 p += n;
2779 }
2780 *p = NUL;
2781 p = buf;
2782 }
2783 else
2784 {
2785 /* Line ending in ">" continues in the next line:
2786 * |{bartype},{lots of values},>
2787 * |<{value},{value}
2788 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002789 eof = viminfo_readline(virp);
2790 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002791 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002792 /* File was truncated or garbled. Read another line if
2793 * this one starts with '|'. */
2794 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002795 p = virp->vir_line + 2;
2796 }
2797 }
2798
2799 if (isdigit(*p))
2800 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002801 value->bv_type = BVAL_NR;
2802 value->bv_nr = getdigits(&p);
2803 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002804 }
2805 else if (*p == '"')
2806 {
2807 int len = 0;
2808 char_u *s = p;
2809
2810 /* Unescape special characters in-place. */
2811 ++p;
2812 while (*p != '"')
2813 {
2814 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002815 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002816 if (*p == '\\')
2817 {
2818 ++p;
2819 if (*p == 'n')
2820 s[len++] = '\n';
2821 else
2822 s[len++] = *p;
2823 ++p;
2824 }
2825 else
2826 s[len++] = *p++;
2827 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002828 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002829 s[len] = NUL;
2830
Bram Moolenaar01227092016-06-11 14:47:40 +02002831 converted = FALSE;
2832 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2833 {
2834 sconv = string_convert(&virp->vir_conv, s, NULL);
2835 if (sconv != NULL)
2836 {
2837 if (s == buf)
2838 vim_free(s);
2839 s = sconv;
2840 buf = s;
2841 converted = TRUE;
2842 }
2843 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002844
Bram Moolenaar01227092016-06-11 14:47:40 +02002845 /* Need to copy in allocated memory if the string wasn't allocated
2846 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002847 if (s != buf && allocated)
2848 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002849 value->bv_string = s;
2850 value->bv_type = BVAL_STRING;
2851 value->bv_len = len;
Bram Moolenaar13505972019-01-24 15:04:48 +01002852 value->bv_allocated = allocated || converted;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002853 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002854 if (nextp != NULL)
2855 {
2856 /* values following a long string */
2857 p = nextp;
2858 nextp = NULL;
2859 }
2860 }
2861 else if (*p == ',')
2862 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002863 value->bv_type = BVAL_EMPTY;
2864 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002865 }
2866 else
2867 break;
2868 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002869 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002870}
2871
2872 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002873read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002874{
2875 char_u *p = virp->vir_line + 1;
2876 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002877 garray_T values;
2878 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002879 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002880 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002881
2882 /* The format is: |{bartype},{value},...
2883 * For a very long string:
2884 * |{bartype},>{length of "{text}{text2}"}
2885 * |<{text1}
2886 * |<{text2},{value}
2887 * For a long line not using a string
2888 * |{bartype},{lots of values},>
2889 * |<{value},{value}
2890 */
2891 if (*p == '<')
2892 {
2893 /* Continuation line of an unrecognized item. */
2894 if (writing)
2895 ga_add_string(&virp->vir_barlines, virp->vir_line);
2896 }
2897 else
2898 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002899 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002900 bartype = getdigits(&p);
2901 switch (bartype)
2902 {
2903 case BARTYPE_VERSION:
2904 /* Only use the version when it comes before the encoding.
2905 * If it comes later it was copied by a Vim version that
2906 * doesn't understand the version. */
2907 if (!got_encoding)
2908 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002909 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002910 vp = (bval_T *)values.ga_data;
2911 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2912 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002913 }
2914 break;
2915
2916 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002917 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002918 handle_viminfo_history(&values, writing);
2919 break;
2920
2921 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002922 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002923 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002924 break;
2925
Bram Moolenaar2d358992016-06-12 21:20:54 +02002926 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002927 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002928 handle_viminfo_mark(&values, force);
2929 break;
2930
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002931 default:
2932 /* copy unrecognized line (for future use) */
2933 if (writing)
2934 ga_add_string(&virp->vir_barlines, virp->vir_line);
2935 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002936 for (i = 0; i < values.ga_len; ++i)
2937 {
2938 vp = (bval_T *)values.ga_data + i;
2939 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2940 vim_free(vp->bv_string);
2941 }
2942 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002943 }
2944
Bram Moolenaarecefe712016-06-20 12:50:17 +02002945 if (read_next)
2946 return viminfo_readline(virp);
2947 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002948}
2949
2950 static void
2951write_viminfo_version(FILE *fp_out)
2952{
2953 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2954 BARTYPE_VERSION, VIMINFO_VERSION);
2955}
2956
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002957 static void
2958write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2959{
2960 int i;
2961 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002962 int seen_useful = FALSE;
2963 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002964
2965 if (gap->ga_len > 0)
2966 {
2967 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2968
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002969 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002970 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002971 {
2972 line = ((char **)(gap->ga_data))[i];
2973 if (seen_useful || line[1] != '<')
2974 {
2975 fputs(line, fp_out);
2976 seen_useful = TRUE;
2977 }
2978 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002979 }
2980}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#endif /* FEAT_VIMINFO */
2982
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002983/*
2984 * Return the current time in seconds. Calls time(), unless test_settime()
2985 * was used.
2986 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002987 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002988vim_time(void)
2989{
2990# ifdef FEAT_EVAL
2991 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2992# else
2993 return time(NULL);
2994# endif
2995}
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997/*
2998 * Implementation of ":fixdel", also used by get_stty().
2999 * <BS> resulting <Del>
3000 * ^? ^H
3001 * not ^? ^?
3002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003004do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 char_u *p;
3007
3008 p = find_termcode((char_u *)"kb");
3009 add_termcode((char_u *)"kD", p != NULL
3010 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
3011}
3012
3013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003014print_line_no_prefix(
3015 linenr_T lnum,
3016 int use_number,
3017 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
Bram Moolenaar32526b32019-01-19 17:43:09 +01003019 char numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 if (curwin->w_p_nu || use_number)
3022 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003023 vim_snprintf(numbuf, sizeof(numbuf),
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003024 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003025 msg_puts_attr(numbuf, HL_ATTR(HLF_N)); /* Highlight line nrs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003027 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028}
3029
3030/*
3031 * Print a text line. Also in silent mode ("ex -s").
3032 */
3033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003034print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 int save_silent = silent_mode;
3037
Bram Moolenaard29459b2016-08-26 22:29:11 +02003038 /* apply :filter /pat/ */
3039 if (message_filtered(ml_get(lnum)))
3040 return;
3041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003043 silent_mode = FALSE;
3044 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
3045 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (save_silent)
3047 {
3048 msg_putchar('\n');
3049 cursor_on(); /* msg_start() switches it off */
3050 out_flush();
3051 silent_mode = save_silent;
3052 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00003053 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003057rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003058{
3059 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02003060 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003061
Bram Moolenaar7e283842013-05-30 11:43:15 +02003062 buf = curbuf;
3063 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003064 /* buffer changed, don't change name now */
3065 if (buf != curbuf)
3066 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003067#ifdef FEAT_EVAL
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003068 if (aborting()) /* autocmds may abort script processing */
3069 return FAIL;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003070#endif
3071 /*
3072 * The name of the current buffer will be changed.
3073 * A new (unlisted) buffer entry needs to be made to hold the old file
3074 * name, which will become the alternate file name.
3075 * But don't set the alternate file name if the buffer didn't have a
3076 * name.
3077 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02003078 fname = curbuf->b_ffname;
3079 sfname = curbuf->b_sfname;
3080 xfname = curbuf->b_fname;
3081 curbuf->b_ffname = NULL;
3082 curbuf->b_sfname = NULL;
3083 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003084 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02003085 curbuf->b_ffname = fname;
3086 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003087 return FAIL;
3088 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02003089 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003090 if (xfname != NULL && *xfname != NUL)
3091 {
3092 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
3093 if (buf != NULL && !cmdmod.keepalt)
3094 curwin->w_alt_fnum = buf->b_fnum;
3095 }
3096 vim_free(fname);
3097 vim_free(sfname);
Bram Moolenaar7e283842013-05-30 11:43:15 +02003098 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003099
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003100 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02003101 DO_AUTOCHDIR;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003102 return OK;
3103}
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
3106 * ":file[!] [fname]".
3107 */
3108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003111 /* ":0file" removes the file name. Check for illegal uses ":3file",
3112 * "0file name", etc. */
3113 if (eap->addr_count > 0
3114 && (*eap->arg != NUL
3115 || eap->line2 > 0
3116 || eap->addr_count > 1))
3117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003118 emsg(_(e_invarg));
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003119 return;
3120 }
3121
3122 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003124 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 return;
Bram Moolenaarfc089602018-06-24 16:53:35 +02003126 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
Bram Moolenaarfc089602018-06-24 16:53:35 +02003128
3129 // print file name if no argument or 'F' is not in 'shortmess'
3130 if (*eap->arg == NUL || !shortmess(SHM_FILEINFO))
Bram Moolenaar426dd022016-03-15 15:09:29 +01003131 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
3134/*
3135 * ":update".
3136 */
3137 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003138ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 if (curbufIsChanged())
3141 (void)do_write(eap);
3142}
3143
3144/*
3145 * ":write" and ":saveas".
3146 */
3147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaarb7316892019-05-01 18:08:42 +02003150 if (eap->cmdidx == CMD_saveas)
3151 {
3152 // :saveas does not take a range, uses all lines.
3153 eap->line1 = 1;
3154 eap->line2 = curbuf->b_ml.ml_line_count;
3155 }
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (eap->usefilter) /* input lines to shell command */
3158 do_bang(1, eap, FALSE, TRUE, FALSE);
3159 else
3160 (void)do_write(eap);
3161}
3162
3163/*
3164 * write current buffer to file 'eap->arg'
3165 * if 'eap->append' is TRUE, append to the file
3166 *
3167 * if *eap->arg == NUL write to current file
3168 *
3169 * return FAIL for failure, OK otherwise
3170 */
3171 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003172do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 int other;
3175 char_u *fname = NULL; /* init to shut up gcc */
3176 char_u *ffname;
3177 int retval = FAIL;
3178 char_u *free_fname = NULL;
3179#ifdef FEAT_BROWSE
3180 char_u *browse_file = NULL;
3181#endif
3182 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003183 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 if (not_writing()) /* check 'write' option */
3186 return FAIL;
3187
3188 ffname = eap->arg;
3189#ifdef FEAT_BROWSE
3190 if (cmdmod.browse)
3191 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003192 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 NULL, NULL, NULL, curbuf);
3194 if (browse_file == NULL)
3195 goto theend;
3196 ffname = browse_file;
3197 }
3198#endif
3199 if (*ffname == NUL)
3200 {
3201 if (eap->cmdidx == CMD_saveas)
3202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003203 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 goto theend;
3205 }
3206 other = FALSE;
3207 }
3208 else
3209 {
3210 fname = ffname;
3211 free_fname = fix_fname(ffname);
3212 /*
3213 * When out-of-memory, keep unexpanded file name, because we MUST be
3214 * able to write the file in this situation.
3215 */
3216 if (free_fname != NULL)
3217 ffname = free_fname;
3218 other = otherfile(ffname);
3219 }
3220
3221 /*
3222 * If we have a new file, put its name in the list of alternate file names.
3223 */
3224 if (other)
3225 {
3226 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3227 || eap->cmdidx == CMD_saveas)
3228 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3229 else
3230 alt_buf = buflist_findname(ffname);
3231 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3232 {
3233 /* Overwriting a file that is loaded in another buffer is not a
3234 * good idea. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003235 emsg(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 goto theend;
3237 }
3238 }
3239
3240 /*
3241 * Writing to the current file is not allowed in readonly mode
3242 * and a file name is required.
3243 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3244 */
3245 if (!other && (
3246#ifdef FEAT_QUICKFIX
3247 bt_dontwrite_msg(curbuf) ||
3248#endif
3249 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3250 goto theend;
3251
3252 if (!other)
3253 {
3254 ffname = curbuf->b_ffname;
3255 fname = curbuf->b_fname;
3256 /*
3257 * Not writing the whole file is only allowed with '!'.
3258 */
3259 if ( (eap->line1 != 1
3260 || eap->line2 != curbuf->b_ml.ml_line_count)
3261 && !eap->forceit
3262 && !eap->append
3263 && !p_wa)
3264 {
3265#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3266 if (p_confirm || cmdmod.confirm)
3267 {
3268 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3269 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3270 goto theend;
3271 eap->forceit = TRUE;
3272 }
3273 else
3274#endif
3275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003276 emsg(_("E140: Use ! to write partial buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 goto theend;
3278 }
3279 }
3280 }
3281
3282 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3283 {
3284 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 buf_T *was_curbuf = curbuf;
3287
3288 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003289 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003290#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003292#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
3296 /* buffer changed, don't change name now */
3297 retval = FAIL;
3298 goto theend;
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 /* Exchange the file names for the current and the alternate
3301 * buffer. This makes it look like we are now editing the buffer
3302 * under the new name. Must be done before buf_write(), because
3303 * if there is no file name and 'cpo' contains 'F', it will set
3304 * the file name. */
3305 fname = alt_buf->b_fname;
3306 alt_buf->b_fname = curbuf->b_fname;
3307 curbuf->b_fname = fname;
3308 fname = alt_buf->b_ffname;
3309 alt_buf->b_ffname = curbuf->b_ffname;
3310 curbuf->b_ffname = fname;
3311 fname = alt_buf->b_sfname;
3312 alt_buf->b_sfname = curbuf->b_sfname;
3313 curbuf->b_sfname = fname;
3314 buf_name_changed(curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003317 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (!alt_buf->b_p_bl)
3319 {
3320 alt_buf->b_p_bl = TRUE;
3321 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3322 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003323#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003325#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 /* buffer changed, don't write the file */
3330 retval = FAIL;
3331 goto theend;
3332 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003333
3334 /* If 'filetype' was empty try detecting it now. */
3335 if (*curbuf->b_p_ft == NUL)
3336 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003337 if (au_has_group((char_u *)"filetypedetect"))
3338 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003339 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003340 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003341 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003342
3343 /* Autocommands may have changed buffer names, esp. when
3344 * 'autochdir' is set. */
3345 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
3347
Bram Moolenaar5c719942016-07-09 23:40:45 +02003348 name_was_missing = curbuf->b_ffname == NULL;
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3351 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003352
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003353 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003354 if (eap->cmdidx == CMD_saveas)
3355 {
3356 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003357 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003358 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003359 redraw_tabline = TRUE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003360 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003361 }
3362
3363 /* Change directories when the 'acd' option is set and the file name
3364 * got changed or set. */
3365 if (eap->cmdidx == CMD_saveas || name_was_missing)
Bram Moolenaar6f470022018-04-10 18:47:20 +02003366 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368
3369theend:
3370#ifdef FEAT_BROWSE
3371 vim_free(browse_file);
3372#endif
3373 vim_free(free_fname);
3374 return retval;
3375}
3376
3377/*
3378 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3379 * BF_NEW or BF_READERR, check for overwriting current file.
3380 * May set eap->forceit if a dialog says it's OK to overwrite.
3381 * Return OK if it's OK, FAIL if it is not.
3382 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003383 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003384check_overwrite(
3385 exarg_T *eap,
3386 buf_T *buf,
3387 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003389 char_u *ffname, /* full path version of fname */
3390 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 /*
3393 * write to other file or b_flags set or not writing the whole file:
3394 * overwriting only allowed with '!'
3395 */
3396 if ( (other
3397 || (buf->b_flags & BF_NOTEDITED)
3398 || ((buf->b_flags & BF_NEW)
3399 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3400 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003402#ifdef FEAT_QUICKFIX
3403 && !bt_nofile(buf)
3404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 && vim_fexists(ffname))
3406 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003407 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003409#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003410 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003411 if (mch_isdir(ffname))
3412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003413 semsg(_(e_isadir2), ffname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003414 return FAIL;
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
3417#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003418 if (p_confirm || cmdmod.confirm)
3419 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003420 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003422 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3423 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3424 return FAIL;
3425 eap->forceit = TRUE;
3426 }
3427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003430 emsg(_(e_exists));
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003431 return FAIL;
3432 }
3433 }
3434
3435 /* For ":w! filename" check that no swap file exists for "filename". */
3436 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003438 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003439 char_u *p;
3440 int r;
3441 char_u *swapname;
3442
3443 /* We only try the first entry in 'directory', without checking if
3444 * it's writable. If the "." directory is not writable the write
3445 * will probably fail anyway.
3446 * Use 'shortname' of the current buffer, since there is no buffer
3447 * for the written file. */
3448 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003449 {
3450 dir = alloc(5);
3451 if (dir == NULL)
3452 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003453 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003454 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003455 else
3456 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003457 dir = alloc(MAXPATHL);
3458 if (dir == NULL)
3459 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003460 p = p_dir;
3461 copy_option_part(&p, dir, MAXPATHL, ",");
3462 }
3463 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003464 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003465 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003466 if (r)
3467 {
3468#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3469 if (p_confirm || cmdmod.confirm)
3470 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003471 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003472
3473 dialog_msg(buff,
3474 _("Swap file \"%s\" exists, overwrite anyway?"),
3475 swapname);
3476 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3477 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003478 {
3479 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003480 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003481 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003482 eap->forceit = TRUE;
3483 }
3484 else
3485#endif
3486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003487 semsg(_("E768: Swap file exists: %s (:silent! overrides)"),
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003488 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003489 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003490 return FAIL;
3491 }
3492 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003493 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495 }
3496 return OK;
3497}
3498
3499/*
3500 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3501 */
3502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003503ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 int i;
3506
3507 if (eap->cmd[1] == 'n')
3508 i = curwin->w_arg_idx + (int)eap->line2;
3509 else
3510 i = curwin->w_arg_idx - (int)eap->line2;
3511 eap->line1 = 1;
3512 eap->line2 = curbuf->b_ml.ml_line_count;
3513 if (do_write(eap) != FAIL)
3514 do_argfile(eap, i);
3515}
3516
3517/*
3518 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3519 */
3520 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003521do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 buf_T *buf;
3524 int error = 0;
3525 int save_forceit = eap->forceit;
3526
3527 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3528 exiting = TRUE;
3529
Bram Moolenaar29323592016-07-24 22:04:11 +02003530 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaar7a760922018-02-19 23:10:02 +01003532#ifdef FEAT_TERMINAL
3533 if (exiting && term_job_running(buf->b_term))
3534 {
3535 no_write_message_nobang(buf);
3536 ++error;
3537 }
3538 else
3539#endif
Bram Moolenaar059db5c2017-10-15 22:42:23 +02003540 if (bufIsChanged(buf) && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 /*
3543 * Check if there is a reason the buffer cannot be written:
3544 * 1. if the 'write' option is set
3545 * 2. if there is no file name (even after browsing)
3546 * 3. if the 'readonly' is set (even after a dialog)
3547 * 4. if overwriting is allowed (even after a dialog)
3548 */
3549 if (not_writing())
3550 {
3551 ++error;
3552 break;
3553 }
3554#ifdef FEAT_BROWSE
3555 /* ":browse wall": ask for file name if there isn't one */
3556 if (buf->b_ffname == NULL && cmdmod.browse)
3557 browse_save_fname(buf);
3558#endif
3559 if (buf->b_ffname == NULL)
3560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003561 semsg(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 ++error;
3563 }
3564 else if (check_readonly(&eap->forceit, buf)
3565 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3566 FALSE) == FAIL)
3567 {
3568 ++error;
3569 }
3570 else
3571 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003572 bufref_T bufref;
3573
3574 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (buf_write_all(buf, eap->forceit) == FAIL)
3576 ++error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003578 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581 eap->forceit = save_forceit; /* check_overwrite() may set it */
3582 }
3583 }
3584 if (exiting)
3585 {
3586 if (!error)
3587 getout(0); /* exit Vim */
3588 not_exiting();
3589 }
3590}
3591
3592/*
3593 * Check the 'write' option.
3594 * Return TRUE and give a message when it's not st.
3595 */
3596 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003597not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 if (p_write)
3600 return FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003601 emsg(_("E142: File not written: Writing is disabled by 'write' option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 return TRUE;
3603}
3604
3605/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003606 * Check if a buffer is read-only (either 'readonly' option is set or file is
3607 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3608 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 */
3610 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003611check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003613 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003614
3615 /* Handle a file being readonly when the 'readonly' option is set or when
3616 * the file exists and permissions are read-only.
3617 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3618 * important for device checks but not here. */
3619 if (!*forceit && (buf->b_p_ro
3620 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3621 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3624 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3625 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003626 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
Bram Moolenaar5386a122007-06-28 20:02:32 +00003628 if (buf->b_p_ro)
3629 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3630 buf->b_fname);
3631 else
3632 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 +00003633 buf->b_fname);
3634
3635 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3636 {
3637 /* Set forceit, to force the writing of a readonly file */
3638 *forceit = TRUE;
3639 return FALSE;
3640 }
3641 else
3642 return TRUE;
3643 }
3644 else
3645#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003646 if (buf->b_p_ro)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003647 emsg(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003648 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003649 semsg(_("E505: \"%s\" is read-only (add ! to override)"),
Bram Moolenaar5386a122007-06-28 20:02:32 +00003650 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 return TRUE;
3652 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return FALSE;
3655}
3656
3657/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003658 * Try to abandon the current file and edit a new or existing file.
3659 * "fnum" is the number of the file, if zero use "ffname_arg"/"sfname_arg".
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003660 * "lnum" is the line number for the cursor in the new file (if non-zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 *
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003662 * Return:
3663 * GETFILE_ERROR for "normal" error,
3664 * GETFILE_NOT_WRITTEN for "not written" error,
3665 * GETFILE_SAME_FILE for success
3666 * GETFILE_OPEN_OTHER for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003669getfile(
3670 int fnum,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003671 char_u *ffname_arg,
3672 char_u *sfname_arg,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003673 int setpm,
3674 linenr_T lnum,
3675 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003677 char_u *ffname = ffname_arg;
3678 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 int other;
3680 int retval;
3681 char_u *free_me = NULL;
3682
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003683 if (text_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003684 return GETFILE_ERROR;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003685 if (curbuf_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003686 return GETFILE_ERROR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
3688 if (fnum == 0)
3689 {
3690 /* make ffname full path, set sfname */
3691 fname_expand(curbuf, &ffname, &sfname);
3692 other = otherfile(ffname);
3693 free_me = ffname; /* has been allocated, free() later */
3694 }
3695 else
3696 other = (fnum != curbuf->b_fnum);
3697
3698 if (other)
3699 ++no_wait_return; /* don't wait for autowrite message */
Bram Moolenaareb44a682017-08-03 22:44:55 +02003700 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3702 {
3703#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3704 if (p_confirm && p_write)
3705 dialog_changed(curbuf, FALSE);
3706 if (curbufIsChanged())
3707#endif
3708 {
3709 if (other)
3710 --no_wait_return;
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02003711 no_write_message();
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003712 retval = GETFILE_NOT_WRITTEN; /* file has been changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 goto theend;
3714 }
3715 }
3716 if (other)
3717 --no_wait_return;
3718 if (setpm)
3719 setpcmark();
3720 if (!other)
3721 {
3722 if (lnum != 0)
3723 curwin->w_cursor.lnum = lnum;
3724 check_cursor_lnum();
3725 beginline(BL_SOL | BL_FIX);
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003726 retval = GETFILE_SAME_FILE; /* it's in the same file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02003729 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003730 curwin) == OK)
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003731 retval = GETFILE_OPEN_OTHER; /* opened another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 else
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003733 retval = GETFILE_ERROR; /* error encountered */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735theend:
3736 vim_free(free_me);
3737 return retval;
3738}
3739
3740/*
3741 * start editing a new file
3742 *
3743 * fnum: file number; if zero use ffname/sfname
3744 * ffname: the file name
3745 * - full path if sfname used,
3746 * - any file name if sfname is NULL
3747 * - empty string to re-edit with the same file name (but may be
3748 * in a different directory)
3749 * - NULL to start an empty buffer
3750 * sfname: the short file name (or NULL)
3751 * eap: contains the command to be executed after loading the file and
3752 * forced 'ff' and 'fenc'
3753 * newlnum: if > 0: put cursor on this line number (if possible)
3754 * if ECMD_LASTL: use last position in loaded file
3755 * if ECMD_LAST: use last position in all files
3756 * if ECMD_ONE: use first line
3757 * flags:
3758 * ECMD_HIDE: if TRUE don't free the current buffer
3759 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3760 * ECMD_OLDBUF: use existing buffer if it exists
3761 * ECMD_FORCEIT: ! used for Ex command
3762 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003763 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003764 * window, NULL when splitting the window first. When not NULL info
3765 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 *
3767 * return FAIL for failure, OK otherwise
3768 */
3769 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003770do_ecmd(
3771 int fnum,
3772 char_u *ffname,
3773 char_u *sfname,
3774 exarg_T *eap, /* can be NULL! */
3775 linenr_T newlnum,
3776 int flags,
3777 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 int other_file; /* TRUE if editing another file */
3780 int oldbuf; /* TRUE if using existing buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 int auto_buf = FALSE; /* TRUE if autocommands brought us
3782 into the buffer unexpectedly */
3783 char_u *new_name = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003784#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003785 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786#endif
3787 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003788 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003789 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 char_u *free_fname = NULL;
3791#ifdef FEAT_BROWSE
3792 char_u *browse_file = NULL;
3793#endif
3794 int retval = FAIL;
3795 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003796 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 linenr_T topline = 0;
3798 int newcol = -1;
3799 int solcol = -1;
3800 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003802#ifdef FEAT_SPELL
3803 int did_get_winopts = FALSE;
3804#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003805 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003806 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +01003807 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 if (eap != NULL)
3810 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003811 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 if (fnum != 0)
3814 {
3815 if (fnum == curbuf->b_fnum) /* file is already being edited */
3816 return OK; /* nothing to do */
3817 other_file = TRUE;
3818 }
3819 else
3820 {
3821#ifdef FEAT_BROWSE
3822 if (cmdmod.browse)
3823 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003824 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003825# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003826 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003827# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003828 au_has_group((char_u *)"FileExplorer"))
3829 {
3830 /* No browsing supported but we do have the file explorer:
3831 * Edit the directory. */
3832 if (ffname == NULL || !mch_isdir(ffname))
3833 ffname = (char_u *)".";
3834 }
3835 else
3836 {
3837 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003839 if (browse_file == NULL)
3840 goto theend;
3841 ffname = browse_file;
3842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844#endif
3845 /* if no short name given, use ffname for short name */
3846 if (sfname == NULL)
3847 sfname = ffname;
3848#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003849 if (sfname != NULL)
3850 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851#endif
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3854 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 if (ffname == NULL)
3857 other_file = TRUE;
3858 /* there is no file name */
3859 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3860 other_file = FALSE;
3861 else
3862 {
3863 if (*ffname == NUL) /* re-edit with same file name */
3864 {
3865 ffname = curbuf->b_ffname;
3866 sfname = curbuf->b_fname;
3867 }
3868 free_fname = fix_fname(ffname); /* may expand to full path name */
3869 if (free_fname != NULL)
3870 ffname = free_fname;
3871 other_file = otherfile(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873 }
3874
3875 /*
3876 * if the file was changed we may not be allowed to abandon it
3877 * - if we are going to re-edit the same file
3878 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3879 */
3880 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3881 || (curbuf->b_nwindows == 1
3882 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003883 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3884 | (other_file ? 0 : CCGD_MULTWIN)
3885 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3886 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 if (fnum == 0 && other_file && ffname != NULL)
3889 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3890 goto theend;
3891 }
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 /*
3894 * End Visual mode before switching to another buffer, so the text can be
3895 * copied into the GUI selection buffer.
3896 */
3897 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003899#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003900 if ((command != NULL || newlnum > (linenr_T)0)
3901 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3902 {
3903 int len;
3904 char_u *p;
3905
3906 /* Set v:swapcommand for the SwapExists autocommands. */
3907 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003908 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003909 else
3910 len = 30;
3911 p = alloc((unsigned)len);
3912 if (p != NULL)
3913 {
3914 if (command != NULL)
3915 vim_snprintf((char *)p, len, ":%s\r", command);
3916 else
3917 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3918 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3919 did_set_swapcommand = TRUE;
3920 vim_free(p);
3921 }
3922 }
3923#endif
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 /*
3926 * If we are starting to edit another file, open a (new) buffer.
3927 * Otherwise we re-use the current buffer.
3928 */
3929 if (other_file)
3930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (!(flags & ECMD_ADDBUF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003933 if (!cmdmod.keepalt)
3934 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003935 if (oldwin != NULL)
3936 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938
3939 if (fnum)
3940 buf = buflist_findnr(fnum);
3941 else
3942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (flags & ECMD_ADDBUF)
3944 {
3945 linenr_T tlnum = 1L;
3946
3947 if (command != NULL)
3948 {
3949 tlnum = atol((char *)command);
3950 if (tlnum <= 0)
3951 tlnum = 1L;
3952 }
3953 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3954 goto theend;
3955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 buf = buflist_new(ffname, sfname, 0L,
3957 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003958
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003959 /* autocommands may change curwin and curbuf */
3960 if (oldwin != NULL)
3961 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003962 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 if (buf == NULL)
3965 goto theend;
3966 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3967 {
3968 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 else /* existing memfile */
3971 {
3972 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003973 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003975 /* Check if autocommands made the buffer invalid or changed the
3976 * current buffer. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003977 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 goto theend;
3979#ifdef FEAT_EVAL
3980 if (aborting()) /* autocmds may abort script processing */
3981 goto theend;
3982#endif
3983 }
3984
3985 /* May jump to last used line number for a loaded buffer or when asked
3986 * for explicitly */
3987 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3988 {
3989 pos = buflist_findfpos(buf);
3990 newlnum = pos->lnum;
3991 solcol = pos->col;
3992 }
3993
3994 /*
3995 * Make the (new) buffer the one used by the current window.
3996 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3997 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003998 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 */
4000 if (buf != curbuf)
4001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 /*
4003 * Be careful: The autocommands may delete any buffer and change
4004 * the current buffer.
4005 * - If the buffer we are going to edit is deleted, give up.
4006 * - If the current buffer is deleted, prefer to load the new
4007 * buffer when loading a buffer is required. This avoids
4008 * loading another buffer which then must be closed again.
4009 * - If we ended up in the new buffer already, need to skip a few
4010 * things, set auto_buf.
4011 */
4012 if (buf->b_fname != NULL)
4013 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004014 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004016 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004018 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 delbuf_msg(new_name); /* frees new_name */
4020 goto theend;
4021 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004022#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (aborting()) /* autocmds may abort script processing */
4024 {
4025 vim_free(new_name);
4026 goto theend;
4027 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if (buf == curbuf) /* already in new buffer */
4030 auto_buf = TRUE;
4031 else
4032 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02004033 win_T *the_curwin = curwin;
4034
4035 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004036 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004037 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004038 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02004039
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004040 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 buf_copy_options(buf, BCO_ENTER);
4042
Bram Moolenaar5a497892016-09-03 16:29:04 +02004043 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02004044 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004045 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004046 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004047 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaar5a497892016-09-03 16:29:04 +02004049 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004050 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01004051
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004052#ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02004053 /* autocmds may abort script processing */
4054 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 vim_free(new_name);
4057 goto theend;
4058 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004061 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004063 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 delbuf_msg(new_name); /* frees new_name */
4065 goto theend;
4066 }
4067 if (buf == curbuf) /* already in new buffer */
4068 auto_buf = TRUE;
4069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004071#ifdef FEAT_SYN_HL
4072 /*
4073 * <VN> We could instead free the synblock
4074 * and re-attach to buffer, perhaps.
4075 */
Bram Moolenaarf1d13472017-07-11 18:28:46 +02004076 if (curwin->w_buffer == NULL
4077 || curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02004078 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 curwin->w_buffer = buf;
4081 curbuf = buf;
4082 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004083
4084 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
4085 if (!oldbuf && eap != NULL)
4086 {
4087 set_file_options(TRUE, eap);
4088 set_forced_fenc(eap);
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
4092 /* May get the window options from the last time this buffer
4093 * was in this window (or another window). If not used
4094 * before, reset the local window options to the global
4095 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004096 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004097#ifdef FEAT_SPELL
4098 did_get_winopts = TRUE;
4099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004102 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004103 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105
4106 curwin->w_pcmark.lnum = 1;
4107 curwin->w_pcmark.col = 0;
4108 }
4109 else /* !other_file */
4110 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02004111 if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 oldbuf = (flags & ECMD_OLDBUF);
4115 }
4116
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004117 /* Don't redraw until the cursor is in the right line, otherwise
4118 * autocommands may cause ml_get errors. */
4119 ++RedrawingDisabled;
4120 did_inc_redrawing_disabled = TRUE;
4121
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004122 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4124 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004125 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 else
4128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 /* Don't make a buffer listed if it's a help buffer. Useful when
4130 * using CTRL-O to go back to a help file. */
4131 if (!curbuf->b_help)
4132 set_buflisted(TRUE);
4133 }
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /* If autocommands change buffers under our fingers, forget about
4136 * editing the file. */
4137 if (buf != curbuf)
4138 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004139#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 if (aborting()) /* autocmds may abort script processing */
4141 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 /* Since we are starting to edit a file, consider the filetype to be
4145 * unset. Helps for when an autocommand changes files and expects syntax
4146 * highlighting to work in the other file. */
4147 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149/*
4150 * other_file oldbuf
4151 * FALSE FALSE re-edit same file, buffer is re-used
4152 * FALSE TRUE re-edit same file, nothing changes
4153 * TRUE FALSE start editing new file, new buffer
4154 * TRUE TRUE start editing in existing buffer (nothing to do)
4155 */
4156 if (!other_file && !oldbuf) /* re-use the buffer */
4157 {
4158 set_last_cursor(curwin); /* may set b_last_cursor */
4159 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4160 {
4161 newlnum = curwin->w_cursor.lnum;
4162 solcol = curwin->w_cursor.col;
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 buf = curbuf;
4165 if (buf->b_fname != NULL)
4166 new_name = vim_strsave(buf->b_fname);
4167 else
4168 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004169 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004170
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004171 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4172 {
4173 /* Save all the text, so that the reload can be undone.
4174 * Sync first so that this is a separate undo-able action. */
4175 u_sync(FALSE);
4176 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4177 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004178 {
4179 vim_free(new_name);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004180 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004181 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004182 u_unchanged(curbuf);
4183 buf_freeall(curbuf, BFA_KEEP_UNDO);
4184
4185 /* tell readfile() not to clear or reload undo info */
4186 readfile_flags = READ_KEEP_UNDO;
4187 }
4188 else
4189 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 /* If autocommands deleted the buffer we were going to re-edit, give
4192 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004193 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {
4195 delbuf_msg(new_name); /* frees new_name */
4196 goto theend;
4197 }
4198 vim_free(new_name);
4199
4200 /* If autocommands change buffers under our fingers, forget about
4201 * re-editing the file. Should do the buf_clear_file(), but perhaps
4202 * the autocommands changed the buffer... */
4203 if (buf != curbuf)
4204 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004205#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (aborting()) /* autocmds may abort script processing */
4207 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#endif
4209 buf_clear_file(curbuf);
4210 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4211 curbuf->b_op_end.lnum = 0;
4212 }
4213
4214/*
4215 * If we get here we are sure to start editing
4216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /* Assume success now */
4218 retval = OK;
4219
4220 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 * Check if we are editing the w_arg_idx file in the argument list.
4222 */
4223 check_arg_idx(curwin);
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 if (!auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 /*
4228 * Set cursor and init window before reading the file and executing
4229 * autocommands. This allows for the autocommands to position the
4230 * cursor.
4231 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004232 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004235 /* It's possible that all lines in the buffer changed. Need to update
4236 * automatic folding for all windows where it's used. */
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004237 {
4238 win_T *win;
4239 tabpage_T *tp;
4240
4241 FOR_ALL_TAB_WINDOWS(tp, win)
4242 if (win->w_buffer == curbuf)
4243 foldUpdateAll(win);
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#endif
4246
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004247 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004248 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 /*
4251 * Careful: open_buffer() and apply_autocmds() may change the current
4252 * buffer and window.
4253 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004254 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 topline = curwin->w_topline;
4256 if (!oldbuf) /* need to read the file */
4257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4260
4261 /*
4262 * Open the buffer and read the file.
4263 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004264#if defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004265 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 retval = FAIL;
4267#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004268 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#endif
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (swap_exists_action == SEA_QUIT)
4272 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004273 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 else
4276 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004277 /* Read the modelines, but only to set window-local options. Any
4278 * buffer-local options have already been set and may have been
4279 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004280 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4283 &retval);
4284 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4285 &retval);
4286 }
4287 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaareab316b2015-03-24 11:46:30 +01004289 /* If autocommands change the cursor position or topline, we should
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004290 * keep it. Also when it moves within a line. But not when it moves
4291 * to the first non-blank. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004292 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004294 char_u *text = ml_get_curline();
4295
4296 if (curwin->w_cursor.lnum != orig_pos.lnum
4297 || curwin->w_cursor.col != (int)(skipwhite(text) - text))
4298 {
4299 newlnum = curwin->w_cursor.lnum;
4300 newcol = curwin->w_cursor.col;
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 if (curwin->w_topline == topline)
4304 topline = 0;
4305
4306 /* Even when cursor didn't move we need to recompute topline. */
4307 changed_line_abv_curs();
4308
4309#ifdef FEAT_TITLE
4310 maketitle();
4311#endif
4312 }
4313
4314#ifdef FEAT_DIFF
4315 /* Tell the diff stuff that this buffer is new and/or needs updating.
4316 * Also needed when re-editing the same buffer, because unloading will
4317 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004318 if (curwin->w_p_diff)
4319 {
4320 diff_buf_add(curbuf);
4321 diff_invalidate(curbuf);
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#endif
4324
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004325#ifdef FEAT_SPELL
4326 /* If the window options were changed may need to set the spell language.
4327 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004328 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4329 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004330#endif
4331
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (command == NULL)
4333 {
4334 if (newcol >= 0) /* position set by autocommands */
4335 {
4336 curwin->w_cursor.lnum = newlnum;
4337 curwin->w_cursor.col = newcol;
4338 check_cursor();
4339 }
4340 else if (newlnum > 0) /* line number from caller or old position */
4341 {
4342 curwin->w_cursor.lnum = newlnum;
4343 check_cursor_lnum();
4344 if (solcol >= 0 && !p_sol)
4345 {
4346 /* 'sol' is off: Use last known column. */
4347 curwin->w_cursor.col = solcol;
4348 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 curwin->w_set_curswant = TRUE;
4351 }
4352 else
4353 beginline(BL_SOL | BL_FIX);
4354 }
4355 else /* no line number, go to last line in Ex mode */
4356 {
4357 if (exmode_active)
4358 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4359 beginline(BL_WHITE | BL_FIX);
4360 }
4361 }
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 /* Check if cursors in other windows on the same buffer are still valid */
4364 check_lnums(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 /*
4367 * Did not read the file, need to show some info about the file.
4368 * Do this after setting the cursor.
4369 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004370 if (oldbuf && !auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
4372 int msg_scroll_save = msg_scroll;
4373
4374 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4375 * message. */
4376 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4377 msg_scroll = FALSE;
4378 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4379 check_for_delay(FALSE);
4380 msg_start();
4381 msg_scroll = msg_scroll_save;
4382 msg_scrolled_ign = TRUE;
4383
Bram Moolenaar426dd022016-03-15 15:09:29 +01004384 if (!shortmess(SHM_FILEINFO))
4385 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 msg_scrolled_ign = FALSE;
4388 }
4389
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004390#ifdef FEAT_VIMINFO
4391 curbuf->b_last_used = vim_time();
4392#endif
4393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (command != NULL)
4395 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4396
4397#ifdef FEAT_KEYMAP
4398 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004399 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#endif
4401
4402 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004403 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (!skip_redraw)
4405 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004406 n = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (topline == 0 && command == NULL)
Bram Moolenaar375e3392019-01-31 18:26:10 +01004408 *so_ptr = 9999; // force cursor halfway the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 curwin->w_scbind_pos = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01004411 *so_ptr = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4413 }
4414
4415 if (p_im)
4416 need_start_insertmode = TRUE;
4417
Bram Moolenaar15833232018-02-03 18:33:17 +01004418#ifdef FEAT_AUTOCHDIR
4419 /* Change directories when the 'acd' option is set and we aren't already in
4420 * that directory (should already be done above). Expect getcwd() to be
4421 * faster than calling shorten_fnames() unnecessarily. */
4422 if (p_acd && curbuf->b_ffname != NULL)
4423 {
4424 char_u curdir[MAXPATHL];
4425 char_u filedir[MAXPATHL];
4426
4427 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1);
4428 *gettail_sep(filedir) = NUL;
4429 if (mch_dirname(curdir, MAXPATHL) != FAIL
4430 && vim_fnamecmp(curdir, filedir) != 0)
4431 do_autochdir();
4432 }
4433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01004435#if defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004436 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004439 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004440 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441# endif
4442 }
4443#endif
4444
4445theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004446 if (did_inc_redrawing_disabled)
4447 --RedrawingDisabled;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004448#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004449 if (did_set_swapcommand)
4450 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452#ifdef FEAT_BROWSE
4453 vim_free(browse_file);
4454#endif
4455 vim_free(free_fname);
4456 return retval;
4457}
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004460delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004462 semsg(_("E143: Autocommands unexpectedly deleted new buffer %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 name == NULL ? (char_u *)"" : name);
4464 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004465 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004466 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004469static int append_indent = 0; /* autoindent for first line */
4470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471/*
4472 * ":insert" and ":append", also used by ":change"
4473 */
4474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004475ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
4477 char_u *theline;
4478 int did_undo = FALSE;
4479 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004480 int indent = 0;
4481 char_u *p;
4482 int vcol;
4483 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004485 /* the ! flag toggles autoindent */
4486 if (eap->forceit)
4487 curbuf->b_p_ai = !curbuf->b_p_ai;
4488
4489 /* First autoindent comes from the line we start on */
4490 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4491 append_indent = get_indent_lnum(lnum);
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (eap->cmdidx != CMD_append)
4494 --lnum;
4495
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004496 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004497 if (empty && lnum == 1)
4498 lnum = 0;
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 State = INSERT; /* behave like in Insert mode */
4501 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4502 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004503
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004504 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 msg_scroll = TRUE;
4507 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004508 if (curbuf->b_p_ai)
4509 {
4510 if (append_indent >= 0)
4511 {
4512 indent = append_indent;
4513 append_indent = -1;
4514 }
4515 else if (lnum > 0)
4516 indent = get_indent_lnum(lnum);
4517 }
4518 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004520 {
4521 /* No getline() function, use the lines that follow. This ends
4522 * when there is no more. */
4523 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4524 break;
4525 p = vim_strchr(eap->nextcmd, NL);
4526 if (p == NULL)
4527 p = eap->nextcmd + STRLEN(eap->nextcmd);
4528 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4529 if (*p != NUL)
4530 ++p;
4531 eap->nextcmd = p;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004534 {
4535 int save_State = State;
4536
4537 /* Set State to avoid the cursor shape to be set to INSERT mode
4538 * when getline() returns. */
4539 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 theline = eap->getline(
4541#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004542 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004544 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004545 State = save_State;
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004548 if (theline == NULL)
4549 break;
4550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004551 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4552 if (ex_keep_indent)
4553 append_indent = indent;
4554
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004555 /* Look for the "." after automatic indent. */
4556 vcol = 0;
4557 for (p = theline; indent > vcol; ++p)
4558 {
4559 if (*p == ' ')
4560 ++vcol;
4561 else if (*p == TAB)
4562 vcol += 8 - vcol % 8;
4563 else
4564 break;
4565 }
4566 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004567 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4568 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570 vim_free(theline);
4571 break;
4572 }
4573
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004574 /* don't use autoindent if nothing was typed. */
4575 if (p[0] == NUL)
4576 theline[0] = NUL;
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 did_undo = TRUE;
4579 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004580 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582 vim_free(theline);
4583 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004584
4585 if (empty)
4586 {
4587 ml_delete(2L, FALSE);
4588 empty = FALSE;
4589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 State = NORMAL;
4592
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004593 if (eap->forceit)
4594 curbuf->b_p_ai = !curbuf->b_p_ai;
4595
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004597 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 * "end" is set to lnum when something has been appended, otherwise
4599 * it is the same than "start" -- Acevedo */
4600 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4601 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4602 if (eap->cmdidx != CMD_append)
4603 --curbuf->b_op_start.lnum;
4604 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4605 ? lnum : curbuf->b_op_start.lnum;
4606 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4607 curwin->w_cursor.lnum = lnum;
4608 check_cursor_lnum();
4609 beginline(BL_SOL | BL_FIX);
4610
4611 need_wait_return = FALSE; /* don't use wait_return() now */
4612 ex_no_reprint = TRUE;
4613}
4614
4615/*
4616 * ":change"
4617 */
4618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004619ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
4621 linenr_T lnum;
4622
4623 if (eap->line2 >= eap->line1
4624 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4625 return;
4626
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004627 /* the ! flag toggles autoindent */
4628 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4629 append_indent = get_indent_lnum(eap->line1);
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4632 {
4633 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4634 break;
4635 ml_delete(eap->line1, FALSE);
4636 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004637
4638 /* make sure the cursor is not beyond the end of the file now */
4639 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4641
4642 /* ":append" on the line above the deleted lines. */
4643 eap->line2 = eap->line1;
4644 ex_append(eap);
4645}
4646
4647 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004648ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
4650 char_u *x;
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004651 long bigness;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004652 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 int minus = 0;
4654 linenr_T start, end, curs, i;
4655 int j;
4656 linenr_T lnum = eap->line2;
4657
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004658 /* Vi compatible: ":z!" uses display height, without a count uses
4659 * 'scroll' */
4660 if (eap->forceit)
4661 bigness = curwin->w_height;
Bram Moolenaar459ca562016-11-10 18:16:33 +01004662 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004663 bigness = curwin->w_height - 3;
Bram Moolenaar631abc32014-02-22 22:27:47 +01004664 else
4665 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (bigness < 1)
4667 bigness = 1;
4668
4669 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004670 kind = x;
4671 if (*kind == '-' || *kind == '+' || *kind == '='
4672 || *kind == '^' || *kind == '.')
4673 ++x;
4674 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 ++x;
4676
4677 if (*x != 0)
4678 {
4679 if (!VIM_ISDIGIT(*x))
4680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004681 emsg(_("E144: non-numeric argument to :z"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 return;
4683 }
4684 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004685 {
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004686 bigness = atol((char *)x);
4687
4688 /* bigness could be < 0 if atol(x) overflows. */
4689 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
4690 bigness = 2 * curbuf->b_ml.ml_line_count;
4691
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004692 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004693 if (*kind == '=')
4694 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
4697
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004698 /* the number of '-' and '+' multiplies the distance */
4699 if (*kind == '-' || *kind == '+')
4700 for (x = kind + 1; *x == *kind; ++x)
4701 ;
4702
4703 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004706 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4707 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004708 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 break;
4710
4711 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004712 start = lnum - (bigness + 1) / 2 + 1;
4713 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 curs = lnum;
4715 minus = 1;
4716 break;
4717
4718 case '^':
4719 start = lnum - bigness * 2;
4720 end = lnum - bigness;
4721 curs = lnum - bigness;
4722 break;
4723
4724 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004725 start = lnum - (bigness + 1) / 2 + 1;
4726 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 curs = end;
4728 break;
4729
4730 default: /* '+' */
4731 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004732 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004733 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004734 else if (eap->addr_count == 0)
4735 ++start;
4736 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 curs = end;
4738 break;
4739 }
4740
4741 if (start < 1)
4742 start = 1;
4743
4744 if (end > curbuf->b_ml.ml_line_count)
4745 end = curbuf->b_ml.ml_line_count;
4746
4747 if (curs > curbuf->b_ml.ml_line_count)
4748 curs = curbuf->b_ml.ml_line_count;
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004749 else if (curs < 1)
4750 curs = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 for (i = start; i <= end; i++)
4753 {
4754 if (minus && i == lnum)
4755 {
4756 msg_putchar('\n');
4757
4758 for (j = 1; j < Columns; j++)
4759 msg_putchar('-');
4760 }
4761
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004762 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 if (minus && i == lnum)
4765 {
4766 msg_putchar('\n');
4767
4768 for (j = 1; j < Columns; j++)
4769 msg_putchar('-');
4770 }
4771 }
4772
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004773 if (curwin->w_cursor.lnum != curs)
4774 {
4775 curwin->w_cursor.lnum = curs;
4776 curwin->w_cursor.col = 0;
4777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 ex_no_reprint = TRUE;
4779}
4780
4781/*
4782 * Check if the restricted flag is set.
4783 * If so, give an error message and return TRUE.
4784 * Otherwise, return FALSE.
4785 */
4786 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004787check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 if (restricted)
4790 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01004791 emsg(_("E145: Shell commands and some functionality not allowed in rvim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 return TRUE;
4793 }
4794 return FALSE;
4795}
4796
4797/*
4798 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4799 * If so, give an error message and return TRUE.
4800 * Otherwise, return FALSE.
4801 */
4802 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 if (secure)
4806 {
4807 secure = 2;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004808 emsg(_(e_curdir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return TRUE;
4810 }
4811#ifdef HAVE_SANDBOX
4812 /*
4813 * In the sandbox more things are not allowed, including the things
4814 * disallowed in secure mode.
4815 */
4816 if (sandbox != 0)
4817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return TRUE;
4820 }
4821#endif
4822 return FALSE;
4823}
4824
4825static char_u *old_sub = NULL; /* previous substitute pattern */
4826static int global_need_beginline; /* call beginline() after ":g" */
4827
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004828/*
4829 * Flags that are kept between calls to :substitute.
4830 */
4831typedef struct {
4832 int do_all; /* do multiple substitutions per line */
4833 int do_ask; /* ask for confirmation */
4834 int do_count; /* count only */
4835 int do_error; /* if false, ignore errors */
4836 int do_print; /* print last line with subs. */
4837 int do_list; /* list last line with subs. */
4838 int do_number; /* list last line with line nr*/
4839 int do_ic; /* ignore case flag */
4840} subflags_T;
4841
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842/* do_sub()
4843 *
4844 * Perform a substitution from line eap->line1 to line eap->line2 using the
4845 * command pointed to by eap->arg which should be of the form:
4846 *
4847 * /pattern/substitution/{flags}
4848 *
4849 * The usual escapes are supported as described in the regexp docs.
4850 */
4851 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 linenr_T lnum;
4855 long i = 0;
4856 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004857 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4858 FALSE, FALSE, 0};
4859#ifdef FEAT_EVAL
4860 subflags_T subflags_save;
4861#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004862 int save_do_all; /* remember user specified 'g' flag */
4863 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4865 int delimiter;
4866 int sublen;
4867 int got_quit = FALSE;
4868 int got_match = FALSE;
4869 int temp;
4870 int which_pat;
4871 char_u *cmd;
4872 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004873 linenr_T first_line = 0; /* first changed line */
4874 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 * change */
4876 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4877 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004878 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004879 char_u *sub_firstline; /* allocated copy of first sub line */
4880 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004881 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004882 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004883#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004884 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 cmd = eap->arg;
4888 if (!global_busy)
4889 {
4890 sub_nsubs = 0;
4891 sub_nlines = 0;
4892 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004893 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (eap->cmdidx == CMD_tilde)
4896 which_pat = RE_LAST; /* use last used regexp */
4897 else
4898 which_pat = RE_SUBST; /* use last substitute regexp */
4899
4900 /* new pattern and substitution */
Bram Moolenaar1c465442017-03-12 20:10:05 +01004901 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4903 {
4904 /* don't accept alphanumeric for separator */
4905 if (isalpha(*cmd))
4906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004907 emsg(_("E146: Regular expressions can't be delimited by letters"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 return;
4909 }
4910 /*
4911 * undocumented vi feature:
4912 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4913 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4914 */
4915 if (*cmd == '\\')
4916 {
4917 ++cmd;
4918 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004920 emsg(_(e_backslash));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return;
4922 }
4923 if (*cmd != '&')
4924 which_pat = RE_SEARCH; /* use last '/' pattern */
4925 pat = (char_u *)""; /* empty search pattern */
4926 delimiter = *cmd++; /* remember delimiter character */
4927 }
4928 else /* find the end of the regexp */
4929 {
4930 which_pat = RE_LAST; /* use last used regexp */
4931 delimiter = *cmd++; /* remember delimiter character */
4932 pat = cmd; /* remember start of search pat */
4933 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4934 if (cmd[0] == delimiter) /* end delimiter found */
4935 *cmd++ = NUL; /* replace it with a NUL */
4936 }
4937
4938 /*
4939 * Small incompatibility: vi sees '\n' as end of the command, but in
4940 * Vim we want to use '\n' to find/substitute a NUL.
4941 */
4942 sub = cmd; /* remember the start of the substitution */
4943
4944 while (cmd[0])
4945 {
4946 if (cmd[0] == delimiter) /* end delimiter found */
4947 {
4948 *cmd++ = NUL; /* replace it with a NUL */
4949 break;
4950 }
4951 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4952 ++cmd;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004953 MB_PTR_ADV(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955
4956 if (!eap->skip)
4957 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004958 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4959 if (STRCMP(sub, "%") == 0
4960 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4961 {
4962 if (old_sub == NULL) /* there is no previous command */
4963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004964 emsg(_(e_nopresub));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004965 return;
4966 }
4967 sub = old_sub;
4968 }
4969 else
4970 {
4971 vim_free(old_sub);
4972 old_sub = vim_strsave(sub);
4973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975 }
4976 else if (!eap->skip) /* use previous pattern and substitution */
4977 {
4978 if (old_sub == NULL) /* there is no previous command */
4979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004980 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return;
4982 }
4983 pat = NULL; /* search_regcomp() will use previous pattern */
4984 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004985
4986 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4987 * last column after using "$". */
4988 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004991 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4992 * more efficient.
4993 * TODO: find a generic solution to make line-joining operations more
4994 * efficient, avoid allocating a string that grows in size.
4995 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004996 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004997 && *sub == NUL
4998 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4999 || *cmd == 'p' || *cmd == '#'))))
5000 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005001 linenr_T joined_lines_count;
5002
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005003 curwin->w_cursor.lnum = eap->line1;
5004 if (*cmd == 'l')
5005 eap->flags = EXFLAG_LIST;
5006 else if (*cmd == '#')
5007 eap->flags = EXFLAG_NR;
5008 else if (*cmd == 'p')
5009 eap->flags = EXFLAG_PRINT;
5010
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005011 /* The number of lines joined is the number of lines in the range plus
5012 * one. One less when the last line is included. */
5013 joined_lines_count = eap->line2 - eap->line1 + 1;
5014 if (eap->line2 < curbuf->b_ml.ml_line_count)
5015 ++joined_lines_count;
5016 if (joined_lines_count > 1)
5017 {
5018 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
5019 sub_nsubs = joined_lines_count - 1;
5020 sub_nlines = 1;
5021 (void)do_sub_msg(FALSE);
5022 ex_may_print(eap);
5023 }
5024
5025 if (!cmdmod.keeppatterns)
5026 save_re_pat(RE_SUBST, pat, p_magic);
5027#ifdef FEAT_CMDHIST
5028 /* put pattern in history */
5029 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
5030#endif
5031
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005032 return;
5033 }
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /*
5036 * Find trailing options. When '&' is used, keep old options.
5037 */
5038 if (*cmd == '&')
5039 ++cmd;
5040 else
5041 {
5042 if (!p_ed)
5043 {
5044 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005045 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005047 subflags.do_all = FALSE;
5048 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005050 subflags.do_error = TRUE;
5051 subflags.do_print = FALSE;
Bram Moolenaar94747162019-02-10 21:55:26 +01005052 subflags.do_list = FALSE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005053 subflags.do_count = FALSE;
5054 subflags.do_number = FALSE;
5055 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 while (*cmd)
5058 {
5059 /*
5060 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
5061 * 'r' is never inverted.
5062 */
5063 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005064 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005066 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005067 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005068 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005070 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else if (*cmd == 'r') /* use last used regexp */
5072 which_pat = RE_LAST;
5073 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005074 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005075 else if (*cmd == '#')
5076 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005077 subflags.do_print = TRUE;
5078 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005079 }
5080 else if (*cmd == 'l')
5081 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005082 subflags.do_print = TRUE;
5083 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005086 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005088 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 else
5090 break;
5091 ++cmd;
5092 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005093 if (subflags.do_count)
5094 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005096 save_do_all = subflags.do_all;
5097 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
5100 * check for a trailing count
5101 */
5102 cmd = skipwhite(cmd);
5103 if (VIM_ISDIGIT(*cmd))
5104 {
5105 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005106 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005108 emsg(_(e_zerocount));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return;
5110 }
5111 eap->line1 = eap->line2;
5112 eap->line2 += i - 1;
5113 if (eap->line2 > curbuf->b_ml.ml_line_count)
5114 eap->line2 = curbuf->b_ml.ml_line_count;
5115 }
5116
5117 /*
5118 * check for trailing command or garbage
5119 */
5120 cmd = skipwhite(cmd);
5121 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5122 {
5123 eap->nextcmd = check_nextcmd(cmd);
5124 if (eap->nextcmd == NULL)
5125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005126 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 return;
5128 }
5129 }
5130
5131 if (eap->skip) /* not executing commands, only parsing */
5132 return;
5133
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005134 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005135 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005136 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005137 emsg(_(e_modifiable));
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005138 return;
5139 }
5140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5142 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005143 if (subflags.do_error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005144 emsg(_(e_invcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 return;
5146 }
5147
5148 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005149 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005151 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 regmatch.rmm_ic = FALSE;
5153
5154 sub_firstline = NULL;
5155
5156 /*
5157 * ~ in the substitute pattern is replaced with the old pattern.
5158 * We do it here once to avoid it to be replaced over and over again.
5159 * But don't do it when it starts with "\=", then it's an expression.
5160 */
5161 if (!(sub[0] == '\\' && sub[1] == '='))
5162 sub = regtilde(sub, p_magic);
5163
5164 /*
5165 * Check for a match on each line.
5166 */
5167 line2 = eap->line2;
5168 for (lnum = eap->line1; lnum <= line2 && !(got_quit
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005169#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 || aborting()
5171#endif
5172 ); ++lnum)
5173 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005174 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005175 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (nmatch)
5177 {
5178 colnr_T copycol;
5179 colnr_T matchcol;
5180 colnr_T prev_matchcol = MAXCOL;
5181 char_u *new_end, *new_start = NULL;
5182 unsigned new_start_len = 0;
5183 char_u *p1;
5184 int did_sub = FALSE;
5185 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005186 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 long nmatch_tl = 0; /* nr of lines matched below lnum */
5188 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005189 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005190 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005191#ifdef FEAT_TEXT_PROP
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005192 int apc_flags = APC_SAVE_FOR_UNDO | APC_SUBSTITUTE;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 /*
5196 * The new text is build up step by step, to avoid too much
5197 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005198 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 * copycol Column in the old text where we started
5200 * looking for a match; from here old text still
5201 * needs to be copied to the new text.
5202 * matchcol Column number of the old text where to look
5203 * for the next match. It's just after the
5204 * previous match or one further.
5205 * prev_matchcol Column just after the previous match (if any).
5206 * Mostly equal to matchcol, except for the first
5207 * match and after skipping an empty match.
5208 * regmatch.*pos Where the pattern matched in the old text.
5209 * new_start The new text, all that has been produced so
5210 * far.
5211 * new_end The new text, where to append new text.
5212 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005213 * lnum The line number where we found the start of
5214 * the match. Can be below the line we searched
5215 * when there is a \n before a \zs in the
5216 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 * sub_firstlnum The line number in the buffer where to look
5218 * for a match. Can be different from "lnum"
5219 * when the pattern or substitute string contains
5220 * line breaks.
5221 *
5222 * Special situations:
5223 * - When the substitute string contains a line break, the part up
5224 * to the line break is inserted in the text, but the copy of
5225 * the original line is kept. "sub_firstlnum" is adjusted for
5226 * the inserted lines.
5227 * - When the matched pattern contains a line break, the old line
5228 * is taken from the line at the end of the pattern. The lines
5229 * in the match are deleted later, "sub_firstlnum" is adjusted
5230 * accordingly.
5231 *
5232 * The new text is built up in new_start[]. It has some extra
5233 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005234 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 *
5236 * Make a copy of the old line, so it won't be taken away when
5237 * updating the screen or handling a multi-line match. The "old_"
5238 * pointers point into this copy.
5239 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005240 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 copycol = 0;
5242 matchcol = 0;
5243
5244 /* At first match, remember current cursor position. */
5245 if (!got_match)
5246 {
5247 setpcmark();
5248 got_match = TRUE;
5249 }
5250
5251 /*
5252 * Loop until nothing more to replace in this line.
5253 * 1. Handle match with empty string.
5254 * 2. If do_ask is set, ask for confirmation.
5255 * 3. substitute the string.
5256 * 4. if do_all is set, find next match
5257 * 5. break if there isn't another match in this line
5258 */
5259 for (;;)
5260 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005261 /* Advance "lnum" to the line where the match starts. The
5262 * match does not start in the first line when there is a line
5263 * break before \zs. */
5264 if (regmatch.startpos[0].lnum > 0)
5265 {
5266 lnum += regmatch.startpos[0].lnum;
5267 sub_firstlnum += regmatch.startpos[0].lnum;
5268 nmatch -= regmatch.startpos[0].lnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005269 VIM_CLEAR(sub_firstline);
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005270 }
5271
5272 if (sub_firstline == NULL)
5273 {
5274 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5275 if (sub_firstline == NULL)
5276 {
5277 vim_free(new_start);
5278 goto outofmem;
5279 }
5280 }
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 /* Save the line number of the last change for the final
5283 * cursor position (just like Vi). */
5284 curwin->w_cursor.lnum = lnum;
5285 do_again = FALSE;
5286
5287 /*
5288 * 1. Match empty string does not count, except for first
5289 * match. This reproduces the strange vi behaviour.
5290 * This also catches endless loops.
5291 */
5292 if (matchcol == prev_matchcol
5293 && regmatch.endpos[0].lnum == 0
5294 && matchcol == regmatch.endpos[0].col)
5295 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005296 if (sub_firstline[matchcol] == NUL)
5297 /* We already were at the end of the line. Don't look
5298 * for a match in this line again. */
5299 skip_match = TRUE;
5300 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005301 {
5302 /* search for a match at next column */
Bram Moolenaar0df11022011-05-19 14:30:16 +02005303 if (has_mbyte)
5304 matchcol += mb_ptr2len(sub_firstline + matchcol);
5305 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005306 ++matchcol;
5307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 goto skip;
5309 }
5310
5311 /* Normally we continue searching for a match just after the
5312 * previous match. */
5313 matchcol = regmatch.endpos[0].col;
5314 prev_matchcol = matchcol;
5315
5316 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005317 * 2. If do_count is set only increase the counter.
5318 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005320 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005321 {
5322 /* For a multi-line match, put matchcol at the NUL at
5323 * the end of the line and set nmatch to one, so that
5324 * we continue looking for a match on the next line.
5325 * Avoids that ":s/\nB\@=//gc" get stuck. */
5326 if (nmatch > 1)
5327 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005328 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005329 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005330 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005331 }
5332 sub_nsubs++;
5333 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005334#ifdef FEAT_EVAL
5335 /* Skip the substitution, unless an expression is used,
5336 * then it is evaluated in the sandbox. */
5337 if (!(sub[0] == '\\' && sub[1] == '='))
5338#endif
5339 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005340 }
5341
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005342 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005344 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 /* change State to CONFIRM, so that the mouse works
5347 * properly */
5348 save_State = State;
5349 State = CONFIRM;
5350#ifdef FEAT_MOUSE
5351 setmouse(); /* disable mouse in xterm */
5352#endif
5353 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005354 if (curwin->w_p_crb)
5355 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 /* When 'cpoptions' contains "u" don't sync undo when
5358 * asking for confirmation. */
5359 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5360 ++no_u_sync;
5361
5362 /*
5363 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5364 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005365 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005367 if (exmode_active)
5368 {
5369 char_u *resp;
5370 colnr_T sc, ec;
5371
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005372 print_line_no_prefix(lnum,
5373 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005374
5375 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5376 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005377 if (curwin->w_cursor.col < 0)
5378 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005379 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005380 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005381 {
5382 int numw = number_width(curwin) + 1;
5383 sc += numw;
5384 ec += numw;
5385 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005386 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005387 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005388 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005389 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005390 msg_putchar('^');
5391
5392 resp = getexmodeline('?', NULL, 0);
5393 if (resp != NULL)
5394 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005395 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005396 vim_free(resp);
5397 }
5398 }
5399 else
5400 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005401 char_u *orig_line = NULL;
5402 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005404 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005406 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005408 /* Invert the matched string.
5409 * Remove the inversion afterwards. */
5410 temp = RedrawingDisabled;
5411 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005413 if (new_start != NULL)
5414 {
5415 /* There already was a substitution, we would
5416 * like to show this to the user. We cannot
5417 * really update the line, it would change
5418 * what matches. Temporarily replace the line
5419 * and change it back afterwards. */
5420 orig_line = vim_strsave(ml_get(lnum));
5421 if (orig_line != NULL)
5422 {
5423 char_u *new_line = concat_str(new_start,
5424 sub_firstline + copycol);
5425
5426 if (new_line == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005427 VIM_CLEAR(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005428 else
5429 {
5430 /* Position the cursor relative to the
5431 * end of the line, the previous
5432 * substitute may have inserted or
5433 * deleted characters before the
5434 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005435 len_change = (int)STRLEN(new_line)
5436 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005437 curwin->w_cursor.col += len_change;
5438 ml_replace(lnum, new_line, FALSE);
5439 }
5440 }
5441 }
5442
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005443 search_match_lines = regmatch.endpos[0].lnum
5444 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005445 search_match_endcol = regmatch.endpos[0].col
5446 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005447 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005449 update_topline();
5450 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005451 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005452 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005453 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005456 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005458 if (msg_row == Rows - 1)
5459 msg_didout = FALSE; /* avoid a scroll-up */
5460 msg_starthere();
5461 i = msg_scroll;
5462 msg_scroll = 0; /* truncate msg when
5463 needed */
5464 msg_no_more = TRUE;
5465 /* write message same highlighting as for
5466 * wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005467 smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005468 _("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005469 msg_no_more = FALSE;
5470 msg_scroll = i;
5471 showruler(TRUE);
5472 windgoto(msg_row, msg_col);
5473 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005476 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005478 ++no_mapping; /* don't map this key */
5479 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005480 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005481 --allow_keys;
5482 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005484 /* clear the question */
5485 msg_didout = FALSE; /* don't scroll up */
5486 msg_col = 0;
5487 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005488
5489 /* restore the line */
5490 if (orig_line != NULL)
5491 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005492 }
5493
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005495 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005497 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498#endif
5499 )
5500 {
5501 got_quit = TRUE;
5502 break;
5503 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005504 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005506 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005508 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005511 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 line2 = lnum;
5513 break;
5514 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005515 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005517 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 break;
5519 }
5520#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005521 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005523 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 scrolldown_clamp();
5525#endif
5526 }
5527 State = save_State;
5528#ifdef FEAT_MOUSE
5529 setmouse();
5530#endif
5531 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5532 --no_u_sync;
5533
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005534 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 /* For a multi-line match, put matchcol at the NUL at
5537 * the end of the line and set nmatch to one, so that
5538 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005539 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5540 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (nmatch > 1)
5542 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005543 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005544 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 goto skip;
5547 }
5548 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005549 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551
5552 /* Move the cursor to the start of the match, so that we can
5553 * use "\=col("."). */
5554 curwin->w_cursor.col = regmatch.startpos[0].col;
5555
5556 /*
5557 * 3. substitute the string.
5558 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005559#ifdef FEAT_EVAL
Bram Moolenaar80341bc2019-05-20 20:34:51 +02005560 save_ma = curbuf->b_p_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005561 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005562 {
Bram Moolenaar6349e942019-05-18 13:41:22 +02005563 // prevent accidentally changing the buffer by a function
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005564 curbuf->b_p_ma = FALSE;
5565 sandbox++;
5566 }
Bram Moolenaar6349e942019-05-18 13:41:22 +02005567 // Save flags for recursion. They can change for e.g.
5568 // :s/^/\=execute("s#^##gn")
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005569 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005570#endif
Bram Moolenaar6349e942019-05-18 13:41:22 +02005571 // get length of substitution part
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005572 sublen = vim_regsub_multi(&regmatch,
5573 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005575#ifdef FEAT_EVAL
Bram Moolenaar0e97b942019-03-27 22:53:53 +01005576 // If getting the substitute string caused an error, don't do
5577 // the replacement.
Bram Moolenaar0e97b942019-03-27 22:53:53 +01005578 // Don't keep flags set by a recursive call.
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005579 subflags = subflags_save;
Bram Moolenaar6349e942019-05-18 13:41:22 +02005580 if (aborting() || subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005581 {
5582 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005583 if (sandbox > 0)
5584 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005585 goto skip;
5586 }
5587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar4463f292005-09-25 22:20:24 +00005589 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005590 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005591 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5592 {
5593 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5594 skip_match = TRUE;
5595 }
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 /* Need room for:
5598 * - result so far in new_start (not for first sub in line)
5599 * - original text up to match
5600 * - length of substituted part
5601 * - original text after match
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005602 * Adjust text properties here, since we have all information
5603 * needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 if (nmatch == 1)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 p1 = sub_firstline;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005608#ifdef FEAT_TEXT_PROP
5609 if (curbuf->b_has_textprop)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005610 {
5611 // When text properties are changed, need to save for
5612 // undo first, unless done already.
5613 if (adjust_prop_columns(lnum, regmatch.startpos[0].col,
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005614 sublen - 1 - (regmatch.endpos[0].col
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005615 - regmatch.startpos[0].col),
5616 apc_flags))
5617 apc_flags &= ~APC_SAVE_FOR_UNDO;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02005618 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005619#endif
5620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 else
5622 {
5623 p1 = ml_get(sub_firstlnum + nmatch - 1);
5624 nmatch_tl += nmatch - 1;
5625 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005626 copy_len = regmatch.startpos[0].col - copycol;
5627 needed_len = copy_len + ((unsigned)STRLEN(p1)
5628 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (new_start == NULL)
5630 {
5631 /*
5632 * Get some space for a temporary buffer to do the
5633 * substitution into (and some extra space to avoid
5634 * too many calls to alloc()/free()).
5635 */
5636 new_start_len = needed_len + 50;
5637 if ((new_start = alloc_check(new_start_len)) == NULL)
5638 goto outofmem;
5639 *new_start = NUL;
5640 new_end = new_start;
5641 }
5642 else
5643 {
5644 /*
5645 * Check if the temporary buffer is long enough to do the
5646 * substitution into. If not, make it larger (with a bit
5647 * extra to avoid too many calls to alloc()/free()).
5648 */
5649 len = (unsigned)STRLEN(new_start);
5650 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005651 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 new_start_len = needed_len + 50;
5654 if ((p1 = alloc_check(new_start_len)) == NULL)
5655 {
5656 vim_free(new_start);
5657 goto outofmem;
5658 }
5659 mch_memmove(p1, new_start, (size_t)(len + 1));
5660 vim_free(new_start);
5661 new_start = p1;
5662 }
5663 new_end = new_start + len;
5664 }
5665
5666 /*
5667 * copy the text up to the part that matched
5668 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005669 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5670 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005672 (void)vim_regsub_multi(&regmatch,
5673 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 sub, new_end, TRUE, p_magic, TRUE);
5675 sub_nsubs++;
5676 did_sub = TRUE;
5677
5678 /* Move the cursor to the start of the line, to avoid that it
5679 * is beyond the end of the line after the substitution. */
5680 curwin->w_cursor.col = 0;
5681
5682 /* For a multi-line match, make a copy of the last matched
5683 * line and continue in that one. */
5684 if (nmatch > 1)
5685 {
5686 sub_firstlnum += nmatch - 1;
5687 vim_free(sub_firstline);
5688 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5689 /* When going beyond the last line, stop substituting. */
5690 if (sub_firstlnum <= line2)
5691 do_again = TRUE;
5692 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005693 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695
5696 /* Remember next character to be copied. */
5697 copycol = regmatch.endpos[0].col;
5698
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005699 if (skip_match)
5700 {
5701 /* Already hit end of the buffer, sub_firstlnum is one
5702 * less than what it ought to be. */
5703 vim_free(sub_firstline);
5704 sub_firstline = vim_strsave((char_u *)"");
5705 copycol = 0;
5706 }
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 /*
5709 * Now the trick is to replace CTRL-M chars with a real line
5710 * break. This would make it impossible to insert a CTRL-M in
5711 * the text. The line break can be avoided by preceding the
5712 * CTRL-M with a backslash. To be able to insert a backslash,
5713 * they must be doubled in the string and are halved here.
5714 * That is Vi compatible.
5715 */
5716 for (p1 = new_end; *p1; ++p1)
5717 {
5718 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005720 STRMOVE(p1, p1 + 1);
Bram Moolenaarf3333b02019-05-19 22:53:40 +02005721#ifdef FEAT_TEXT_PROP
5722 if (curbuf->b_has_textprop)
5723 {
5724 // When text properties are changed, need to save
5725 // for undo first, unless done already.
5726 if (adjust_prop_columns(lnum,
5727 (colnr_T)(p1 - new_start), -1,
5728 apc_flags))
5729 apc_flags &= ~APC_SAVE_FOR_UNDO;
5730 }
5731#endif
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 else if (*p1 == CAR)
5734 {
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005735 if (u_inssub(lnum) == OK) // prepare for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005737 colnr_T plen = (colnr_T)(p1 - new_start + 1);
5738
5739 *p1 = NUL; // truncate up to the CR
5740 ml_append(lnum - 1, new_start, plen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005742 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 appended_lines(lnum - 1, 1L);
5744 else
5745 {
5746 if (first_line == 0)
5747 first_line = lnum;
5748 last_line = lnum + 1;
5749 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005750#ifdef FEAT_TEXT_PROP
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02005751 adjust_props_for_split(lnum + 1, lnum, plen, 1);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005752#endif
5753 // all line numbers increase
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 ++sub_firstlnum;
5755 ++lnum;
5756 ++line2;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005757 // move the cursor to the new line, like Vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 ++curwin->w_cursor.lnum;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01005759 // copy the rest
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005760 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 p1 = new_start - 1;
5762 }
5763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005765 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767
5768 /*
5769 * 4. If do_all is set, find next match.
5770 * Prevent endless loop with patterns that match empty
5771 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5772 * But ":s/\n/#/" is OK.
5773 */
5774skip:
5775 /* We already know that we did the last subst when we are at
5776 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005777 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5778 * "line2" when there is a \zs in the pattern after a line
5779 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005780 lastone = (skip_match
5781 || got_int
5782 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005783 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005784 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005785 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5786 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 nmatch = -1;
5788
5789 /*
5790 * Replace the line in the buffer when needed. This is
5791 * skipped when there are more matches.
5792 * The check for nmatch_tl is needed for when multi-line
5793 * matching must replace the lines before trying to do another
5794 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005795 * When the match starts below where we start searching also
5796 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 */
5798 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 || nmatch_tl > 0
5800 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005801 curbuf, sub_firstlnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005802 matchcol, NULL, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005803 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
5805 if (new_start != NULL)
5806 {
5807 /*
5808 * Copy the rest of the line, that didn't match.
5809 * "matchcol" has to be adjusted, we use the end of
5810 * the line as reference, because the substitute may
5811 * have changed the number of characters. Same for
5812 * "prev_matchcol".
5813 */
5814 STRCAT(new_start, sub_firstline + copycol);
5815 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5816 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5817 - prev_matchcol;
5818
5819 if (u_savesub(lnum) != OK)
5820 break;
5821 ml_replace(lnum, new_start, TRUE);
5822
5823 if (nmatch_tl > 0)
5824 {
5825 /*
5826 * Matched lines have now been substituted and are
5827 * useless, delete them. The part after the match
5828 * has been appended to new_start, we don't need
5829 * it in the buffer.
5830 */
5831 ++lnum;
5832 if (u_savedel(lnum, nmatch_tl) != OK)
5833 break;
5834 for (i = 0; i < nmatch_tl; ++i)
5835 ml_delete(lnum, (int)FALSE);
5836 mark_adjust(lnum, lnum + nmatch_tl - 1,
5837 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005838 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 deleted_lines(lnum, nmatch_tl);
5840 --lnum;
5841 line2 -= nmatch_tl; /* nr of lines decreases */
5842 nmatch_tl = 0;
5843 }
5844
5845 /* When asking, undo is saved each time, must also set
5846 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005847 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 changed_bytes(lnum, 0);
5849 else
5850 {
5851 if (first_line == 0)
5852 first_line = lnum;
5853 last_line = lnum + 1;
5854 }
5855
5856 sub_firstlnum = lnum;
5857 vim_free(sub_firstline); /* free the temp buffer */
5858 sub_firstline = new_start;
5859 new_start = NULL;
5860 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5861 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5862 - prev_matchcol;
5863 copycol = 0;
5864 }
5865 if (nmatch == -1 && !lastone)
5866 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005867 sub_firstlnum, matchcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869 /*
5870 * 5. break if there isn't another match in this line
5871 */
5872 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005873 {
5874 /* If the match found didn't start where we were
5875 * searching, do the next search in the line where we
5876 * found the match. */
5877 if (nmatch == -1)
5878 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 }
5882
5883 line_breakcheck();
5884 }
5885
5886 if (did_sub)
5887 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005888 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaard23a8232018-02-10 18:45:26 +01005889 VIM_CLEAR(sub_firstline); /* free the copy of the original line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891
5892 line_breakcheck();
5893 }
5894
5895 if (first_line != 0)
5896 {
5897 /* Need to subtract the number of added lines from "last_line" to get
5898 * the line number before the change (same as adding the number of
5899 * deleted lines). */
5900 i = curbuf->b_ml.ml_line_count - old_line_count;
5901 changed_lines(first_line, 0, last_line - i, i);
5902 }
5903
5904outofmem:
5905 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005906
5907 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005908 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005909 curwin->w_cursor = old_cursor;
5910
Bram Moolenaar46475522010-03-23 17:36:29 +01005911 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
5913 /* Set the '[ and '] marks. */
5914 curbuf->b_op_start.lnum = eap->line1;
5915 curbuf->b_op_end.lnum = line2;
5916 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5917
5918 if (!global_busy)
5919 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005920 /* when interactive leave cursor on the match */
5921 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005922 {
5923 if (endcolumn)
5924 coladvance((colnr_T)MAXCOL);
5925 else
5926 beginline(BL_WHITE | BL_FIX);
5927 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005928 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005929 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931 else
5932 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005933 if (subflags.do_print)
5934 print_line(curwin->w_cursor.lnum,
5935 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 else if (!global_busy)
5938 {
5939 if (got_int) /* interrupted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005940 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 else if (got_match) /* did find something but nothing substituted */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005942 msg("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005943 else if (subflags.do_error) /* nothing found */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005944 semsg(_(e_patnotf2), get_search_pat());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005947#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005948 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005949 /* Cursor position may require updating */
5950 changed_window_setting();
5951#endif
5952
Bram Moolenaar473de612013-06-08 18:19:48 +02005953 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005954
5955 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005956 subflags.do_all = save_do_all;
5957 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958}
5959
5960/*
5961 * Give message for number of substitutions.
5962 * Can also be used after a ":global" command.
5963 * Return TRUE if a message was given.
5964 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005965 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005966do_sub_msg(
5967 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 /*
5970 * Only report substitutions when:
5971 * - more than 'report' substitutions
5972 * - command was typed by user, or number of changed lines > 'report'
5973 * - giving messages is not disabled by 'lazyredraw'
5974 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005975 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5976 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 && messaging())
5978 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005979 char *msg_single;
5980 char *msg_plural;
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 if (got_int)
5983 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005984 else
5985 *msg_buf = NUL;
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005986
5987 msg_single = count_only
5988 ? NGETTEXT("%ld match on %ld line",
5989 "%ld matches on %ld line", sub_nsubs)
5990 : NGETTEXT("%ld substitution on %ld line",
5991 "%ld substitutions on %ld line", sub_nsubs);
5992 msg_plural = count_only
5993 ? NGETTEXT("%ld match on %ld lines",
5994 "%ld matches on %ld lines", sub_nsubs)
5995 : NGETTEXT("%ld substitution on %ld lines",
5996 "%ld substitutions on %ld lines", sub_nsubs);
5997
Bram Moolenaar32526b32019-01-19 17:43:09 +01005998 vim_snprintf_add(msg_buf, sizeof(msg_buf),
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005999 NGETTEXT(msg_single, msg_plural, sub_nlines),
6000 sub_nsubs, (long)sub_nlines);
6001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 /* save message to display it after redraw */
Bram Moolenaar32526b32019-01-19 17:43:09 +01006004 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 return TRUE;
6006 }
6007 if (got_int)
6008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006009 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 return TRUE;
6011 }
6012 return FALSE;
6013}
6014
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006015 static void
6016global_exe_one(char_u *cmd, linenr_T lnum)
6017{
6018 curwin->w_cursor.lnum = lnum;
6019 curwin->w_cursor.col = 0;
6020 if (*cmd == NUL || *cmd == '\n')
6021 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
6022 else
6023 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
6024}
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026/*
6027 * Execute a global command of the form:
6028 *
6029 * g/pattern/X : execute X on all lines where pattern matches
6030 * v/pattern/X : execute X on all lines where pattern does not match
6031 *
6032 * where 'X' is an EX command
6033 *
6034 * The command character (as well as the trailing slash) is optional, and
6035 * is assumed to be 'p' if missing.
6036 *
6037 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006038 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * for each line that has a mark. This is required because after deleting
6040 * lines we do not know where to search for the next match.
6041 */
6042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006043ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006046 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 int type; /* first char of cmd: 'v' or 'g' */
6048 char_u *cmd; /* command argument */
6049
6050 char_u delim; /* delimiter, normally '/' */
6051 char_u *pat;
6052 regmmatch_T regmatch;
6053 int match;
6054 int which_pat;
6055
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006056 /* When nesting the command works on one line. This allows for
6057 * ":g/found/v/notfound/command". */
6058 if (global_busy && (eap->line1 != 1
6059 || eap->line2 != curbuf->b_ml.ml_line_count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006061 /* will increment global_busy to break out of the loop */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006062 emsg(_("E147: Cannot do :global recursive with a range"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 return;
6064 }
6065
6066 if (eap->forceit) /* ":global!" is like ":vglobal" */
6067 type = 'v';
6068 else
6069 type = *eap->cmd;
6070 cmd = eap->arg;
6071 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
6073 /*
6074 * undocumented vi feature:
6075 * "\/" and "\?": use previous search pattern.
6076 * "\&": use previous substitute pattern.
6077 */
6078 if (*cmd == '\\')
6079 {
6080 ++cmd;
6081 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
6082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006083 emsg(_(e_backslash));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 return;
6085 }
6086 if (*cmd == '&')
6087 which_pat = RE_SUBST; /* use previous substitute pattern */
6088 else
6089 which_pat = RE_SEARCH; /* use previous search pattern */
6090 ++cmd;
6091 pat = (char_u *)"";
6092 }
6093 else if (*cmd == NUL)
6094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006095 emsg(_("E148: Regular expression missing from global"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 return;
6097 }
6098 else
6099 {
6100 delim = *cmd; /* get the delimiter */
6101 if (delim)
6102 ++cmd; /* skip delimiter if there is one */
6103 pat = cmd; /* remember start of pattern */
6104 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
6105 if (cmd[0] == delim) /* end delimiter found */
6106 *cmd++ = NUL; /* replace it with a NUL */
6107 }
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
6110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006111 emsg(_(e_invcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 return;
6113 }
6114
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006115 if (global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006117 lnum = curwin->w_cursor.lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006118 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006119 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if ((type == 'g' && match) || (type == 'v' && !match))
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006121 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 }
6123 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006124 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006125 /*
6126 * pass 1: set marks for each (not) matching line
6127 */
6128 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6129 {
6130 /* a match on this line? */
6131 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006132 (colnr_T)0, NULL, NULL);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006133 if ((type == 'g' && match) || (type == 'v' && !match))
6134 {
6135 ml_setmarked(lnum);
6136 ndone++;
6137 }
6138 line_breakcheck();
6139 }
6140
6141 /*
6142 * pass 2: execute the command for each line that has been marked
6143 */
6144 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006145 msg(_(e_interr));
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006146 else if (ndone == 0)
6147 {
6148 if (type == 'v')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006149 smsg(_("Pattern found in every line: %s"), pat);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006150 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006151 smsg(_("Pattern not found: %s"), pat);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006152 }
6153 else
6154 {
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006155#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006156 start_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006157#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006158 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006159#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006160 end_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006161#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006162 }
6163
6164 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
Bram Moolenaar473de612013-06-08 18:19:48 +02006167 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168}
6169
6170/*
6171 * Execute "cmd" on lines marked with ml_setmarked().
6172 */
6173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006174global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006176 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6177 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6178 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 /*
6181 * Set current position only once for a global command.
6182 * If global_busy is set, setpcmark() will not do anything.
6183 * If there is an error, global_busy will be incremented.
6184 */
6185 setpcmark();
6186
6187 /* When the command writes a message, don't overwrite the command. */
6188 msg_didout = TRUE;
6189
Bram Moolenaard25bc232010-03-23 17:49:24 +01006190 sub_nsubs = 0;
6191 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 global_need_beginline = FALSE;
6193 global_busy = 1;
6194 old_lcount = curbuf->b_ml.ml_line_count;
6195 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6196 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006197 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 ui_breakcheck();
6199 }
6200
6201 global_busy = 0;
6202 if (global_need_beginline)
6203 beginline(BL_WHITE | BL_FIX);
6204 else
6205 check_cursor(); /* cursor may be beyond the end of the line */
6206
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006207 /* the cursor may not have moved in the text but a change in a previous
6208 * line may move it on the screen */
6209 changed_line_abv_curs();
6210
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 /* If it looks like no message was written, allow overwriting the
6212 * command with the report for number of changes. */
6213 if (msg_col == 0 && msg_scrolled == 0)
6214 msg_didout = FALSE;
6215
Bram Moolenaar45667512007-05-10 19:24:43 +00006216 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006217 * number of extra or deleted lines.
6218 * Don't report extra or deleted lines in the edge case where the buffer
6219 * we are in after execution is different from the buffer we started in. */
6220 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6222}
6223
6224#ifdef FEAT_VIMINFO
6225 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006226read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006228 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 vim_free(old_sub);
6230 if (force || old_sub == NULL)
6231 old_sub = viminfo_readstring(virp, 1, TRUE);
6232 return viminfo_readline(virp);
6233}
6234
6235 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006236write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6239 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006240 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 viminfo_writestring(fp, old_sub);
6242 }
6243}
6244#endif /* FEAT_VIMINFO */
6245
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006246#if defined(EXITFREE) || defined(PROTO)
6247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006248free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006249{
6250 vim_free(old_sub);
6251}
6252#endif
6253
Bram Moolenaar4033c552017-09-16 20:54:51 +02006254#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255/*
6256 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006257 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260prepare_tagpreview(
6261 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 win_T *wp;
6264
6265# ifdef FEAT_GUI
6266 need_mouse_correct = TRUE;
6267# endif
6268
6269 /*
6270 * If there is already a preview window open, use that one.
6271 */
6272 if (!curwin->w_p_pvw)
6273 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006274 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (wp->w_p_pvw)
6276 break;
6277 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006278 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 else
6280 {
6281 /*
6282 * There is no preview window open yet. Create one.
6283 */
6284 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6285 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006286 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 curwin->w_p_pvw = TRUE;
6288 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006289 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6290 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291# ifdef FEAT_DIFF
6292 curwin->w_p_diff = FALSE; /* no 'diff' */
6293# endif
6294# ifdef FEAT_FOLDING
6295 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6296# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006297 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006300 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301}
6302
6303#endif
6304
6305
6306/*
6307 * ":help": open a read-only window on a help file
6308 */
6309 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006310ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311{
6312 char_u *arg;
6313 char_u *tag;
6314 FILE *helpfd; /* file descriptor of help file */
6315 int n;
6316 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 int num_matches;
6319 char_u **matches;
6320 char_u *p;
6321 int empty_fnum = 0;
6322 int alt_fnum = 0;
6323 buf_T *buf;
6324#ifdef FEAT_MULTI_LANG
6325 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006326 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006328#ifdef FEAT_FOLDING
6329 int old_KeyTyped = KeyTyped;
6330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332 if (eap != NULL)
6333 {
6334 /*
6335 * A ":help" command ends at the first LF, or at a '|' that is
6336 * followed by some text. Set nextcmd to the following command.
6337 */
6338 for (arg = eap->arg; *arg; ++arg)
6339 {
6340 if (*arg == '\n' || *arg == '\r'
6341 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6342 {
6343 *arg++ = NUL;
6344 eap->nextcmd = arg;
6345 break;
6346 }
6347 }
6348 arg = eap->arg;
6349
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006350 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006352 emsg(_("E478: Don't panic!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return;
6354 }
6355
6356 if (eap->skip) /* not executing commands */
6357 return;
6358 }
6359 else
6360 arg = (char_u *)"";
6361
6362 /* remove trailing blanks */
6363 p = arg + STRLEN(arg) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006364 while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 *p-- = NUL;
6366
6367#ifdef FEAT_MULTI_LANG
6368 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006369 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370#endif
6371
6372 /* When no argument given go to the index. */
6373 if (*arg == NUL)
6374 arg = (char_u *)"help.txt";
6375
6376 /*
6377 * Check if there is a match for the argument.
6378 */
6379 n = find_help_tags(arg, &num_matches, &matches,
6380 eap != NULL && eap->forceit);
6381
6382 i = 0;
6383#ifdef FEAT_MULTI_LANG
6384 if (n != FAIL && lang != NULL)
6385 /* Find first item with the requested language. */
6386 for (i = 0; i < num_matches; ++i)
6387 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006388 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (len > 3 && matches[i][len - 3] == '@'
6390 && STRICMP(matches[i] + len - 2, lang) == 0)
6391 break;
6392 }
6393#endif
6394 if (i >= num_matches || n == FAIL)
6395 {
6396#ifdef FEAT_MULTI_LANG
6397 if (lang != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006398 semsg(_("E661: Sorry, no '%s' help for %s"), lang, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 else
6400#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006401 semsg(_("E149: Sorry, no help for %s"), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (n != FAIL)
6403 FreeWild(num_matches, matches);
6404 return;
6405 }
6406
6407 /* The first match (in the requested language) is the best match. */
6408 tag = vim_strsave(matches[i]);
6409 FreeWild(num_matches, matches);
6410
6411#ifdef FEAT_GUI
6412 need_mouse_correct = TRUE;
6413#endif
6414
6415 /*
6416 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006417 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006419 if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006421 if (cmdmod.tab != 0)
6422 wp = NULL;
6423 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006424 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006425 if (bt_help(wp->w_buffer))
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6428 win_enter(wp, TRUE);
6429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
6431 /*
6432 * There is no help window yet.
6433 * Try to open the file specified by the "helpfile" option.
6434 */
6435 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006437 smsg(_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 goto erret;
6439 }
6440 fclose(helpfd);
6441
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006443 * specified, the current window is vertically split and
6444 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006447 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006450 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (curwin->w_height < p_hh)
6453 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 /*
6456 * Open help file (do_ecmd() will set b_help flag, readfile() will
6457 * set b_p_ro flag).
6458 * Set the alternate file to the previously edited file.
6459 */
6460 alt_fnum = curbuf->b_fnum;
6461 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006462 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaar4033c552017-09-16 20:54:51 +02006463 NULL); /* buffer is still open, don't store info */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006464 if (!cmdmod.keepalt)
6465 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 empty_fnum = curbuf->b_fnum;
6467 }
6468 }
6469
6470 if (!p_im)
6471 restart_edit = 0; /* don't want insert mode in help file */
6472
Bram Moolenaar8f535582011-09-30 17:30:31 +02006473#ifdef FEAT_FOLDING
6474 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6475 * It is needed for do_tag top open folds under the cursor. */
6476 KeyTyped = old_KeyTyped;
6477#endif
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (tag != NULL)
6480 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6481
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006482 /* Delete the empty buffer if we're not using it. Careful: autocommands
6483 * may have jumped to another window, check that the buffer is not in a
6484 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6486 {
6487 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006488 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 wipe_buffer(buf, TRUE);
6490 }
6491
6492 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006493 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 curwin->w_alt_fnum = alt_fnum;
6495
6496erret:
6497 vim_free(tag);
6498}
6499
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006500/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006501 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006502 */
6503 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006504ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006505{
6506 win_T *win;
6507
6508 FOR_ALL_WINDOWS(win)
6509 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006510 if (bt_help(win->w_buffer))
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006511 {
6512 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006513 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006514 }
6515 }
6516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006518#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6519/*
6520 * In an argument search for a language specifiers in the form "@xx".
6521 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6522 * Returns NULL if not found.
6523 */
6524 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006525check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006526{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006527 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006528
6529 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6530 && ASCII_ISALPHA(arg[len - 1]))
6531 {
6532 arg[len - 3] = NUL; /* remove the '@' */
6533 return arg + len - 2;
6534 }
6535 return NULL;
6536}
6537#endif
6538
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539/*
6540 * Return a heuristic indicating how well the given string matches. The
6541 * smaller the number, the better the match. This is the order of priorities,
6542 * from best match to worst match:
6543 * - Match with least alpha-numeric characters is better.
6544 * - Match with least total characters is better.
6545 * - Match towards the start is better.
6546 * - Match starting with "+" is worse (feature instead of command)
6547 * Assumption is made that the matched_string passed has already been found to
6548 * match some string for which help is requested. webb.
6549 */
6550 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006551help_heuristic(
6552 char_u *matched_string,
6553 int offset, /* offset for match */
6554 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 int num_letters;
6557 char_u *p;
6558
6559 num_letters = 0;
6560 for (p = matched_string; *p; p++)
6561 if (ASCII_ISALNUM(*p))
6562 num_letters++;
6563
6564 /*
6565 * Multiply the number of letters by 100 to give it a much bigger
6566 * weighting than the number of characters.
6567 * If there only is a match while ignoring case, add 5000.
6568 * If the match starts in the middle of a word, add 10000 to put it
6569 * somewhere in the last half.
6570 * If the match is more than 2 chars from the start, multiply by 200 to
6571 * put it after matches at the start.
6572 */
6573 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6574 && ASCII_ISALNUM(matched_string[offset - 1]))
6575 offset += 10000;
6576 else if (offset > 2)
6577 offset *= 200;
6578 if (wrong_case)
6579 offset += 5000;
6580 /* Features are less interesting than the subjects themselves, but "+"
6581 * alone is not a feature. */
6582 if (matched_string[0] == '+' && matched_string[1] != NUL)
6583 offset += 100;
6584 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6585}
6586
6587/*
6588 * Compare functions for qsort() below, that checks the help heuristics number
6589 * that has been put after the tagname by find_tags().
6590 */
6591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006592help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 char *p1;
6595 char *p2;
6596
6597 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6598 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6599 return strcmp(p1, p2);
6600}
6601
6602/*
6603 * Find all help tags matching "arg", sort them and return in matches[], with
6604 * the number of matches in num_matches.
6605 * The matches will be sorted with a "best" match algorithm.
6606 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6607 */
6608 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609find_help_tags(
6610 char_u *arg,
6611 int *num_matches,
6612 char_u ***matches,
6613 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 char_u *s, *d;
6616 int i;
6617 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006618 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006619 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006620 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6621 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006622 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006623 "[count]", "[quotex]",
6624 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006625 "[pattern]", "\\|", "\\%$",
6626 "s/\\~", "s/\\U", "s/\\L",
6627 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006629 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006630 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006631 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
6632 "-?", "q?", "v_g?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006633 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006634 "\\[count]", "\\[quotex]",
6635 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006636 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006637 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006638 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006639 static char *(expr_table[]) = {"!=?", "!~?", "<=?", "<?", "==?", "=~?",
6640 ">=?", ">?", "is?", "isnot?"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 int flags;
6642
6643 d = IObuff; /* assume IObuff is long enough! */
6644
Bram Moolenaar3bf5e6a2018-08-02 22:23:57 +02006645 if (STRNICMP(arg, "expr-", 5) == 0)
6646 {
6647 // When the string starting with "expr-" and containing '?' and matches
6648 // the table, it is taken literally. Otherwise '?' is recognized as a
6649 // wildcard.
6650 for (i = (int)(sizeof(expr_table) / sizeof(char *)); --i >= 0; )
6651 if (STRCMP(arg + 5, expr_table[i]) == 0)
6652 {
6653 STRCPY(d, arg);
6654 break;
6655 }
6656 }
6657 else
6658 {
6659 // Recognize a few exceptions to the rule. Some strings that contain
6660 // '*' with "star". Otherwise '*' is recognized as a wildcard.
6661 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
6662 if (STRCMP(arg, mtable[i]) == 0)
6663 {
6664 STRCPY(d, rtable[i]);
6665 break;
6666 }
6667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 if (i < 0) /* no match in table */
6670 {
6671 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6672 * Also replace "\%^" and "\%(", they match every tag too.
6673 * Also "\zs", "\z1", etc.
6674 * Also "\@<", "\@=", "\@<=", etc.
6675 * And also "\_$" and "\_^". */
6676 if (arg[0] == '\\'
6677 && ((arg[1] != NUL && arg[2] == NUL)
6678 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6679 && arg[2] != NUL)))
6680 {
6681 STRCPY(d, "/\\\\");
6682 STRCPY(d + 3, arg + 1);
6683 /* Check for "/\\_$", should be "/\\_\$" */
6684 if (d[3] == '_' && d[4] == '$')
6685 STRCPY(d + 4, "\\$");
6686 }
6687 else
6688 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006689 /* Replace:
6690 * "[:...:]" with "\[:...:]"
6691 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006692 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006693 */
6694 if ((arg[0] == '[' && (arg[1] == ':'
6695 || (arg[1] == '+' && arg[2] == '+')))
6696 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 *d++ = '\\';
6698
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006699 /*
6700 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6701 */
6702 if (*arg == '(' && arg[1] == '\'')
6703 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 for (s = arg; *s; ++s)
6705 {
6706 /*
6707 * Replace "|" with "bar" and '"' with "quote" to match the name of
6708 * the tags for these commands.
6709 * Replace "*" with ".*" and "?" with "." to match command line
6710 * completion.
6711 * Insert a backslash before '~', '$' and '.' to avoid their
6712 * special meaning.
6713 */
6714 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6715 break;
6716 switch (*s)
6717 {
6718 case '|': STRCPY(d, "bar");
6719 d += 3;
6720 continue;
6721 case '"': STRCPY(d, "quote");
6722 d += 5;
6723 continue;
6724 case '*': *d++ = '.';
6725 break;
6726 case '?': *d++ = '.';
6727 continue;
6728 case '$':
6729 case '.':
6730 case '~': *d++ = '\\';
6731 break;
6732 }
6733
6734 /*
6735 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6736 * ":help i_^_CTRL-D" work.
6737 * Insert '-' before and after "CTRL-X" when applicable.
6738 */
6739 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6740 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6741 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006742 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6743 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 STRCPY(d, "CTRL-");
6745 d += 5;
6746 if (*s < ' ')
6747 {
6748#ifdef EBCDIC
6749 *d++ = CtrlChar(*s);
6750#else
6751 *d++ = *s + '@';
6752#endif
6753 if (d[-1] == '\\')
6754 *d++ = '\\'; /* double a backslash */
6755 }
6756 else
6757 *d++ = *++s;
6758 if (s[1] != NUL && s[1] != '_')
6759 *d++ = '_'; /* append a '_' */
6760 continue;
6761 }
6762 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6763 *d++ = '\\';
6764
6765 /*
6766 * Insert a backslash before a backslash after a slash, for search
6767 * pattern tags: "/\|" --> "/\\|".
6768 */
6769 else if (s[0] == '\\' && s[1] != '\\'
6770 && *arg == '/' && s == arg + 1)
6771 *d++ = '\\';
6772
6773 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6774 * "CTRL-\_CTRL-N" */
6775 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6776 {
6777 STRCPY(d, "CTRL-\\\\");
6778 d += 7;
6779 s += 6;
6780 }
6781
6782 *d++ = *s;
6783
6784 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006785 * If tag contains "({" or "([", tag terminates at the "(".
6786 * This is for help on functions, e.g.: abs({expr}).
6787 */
6788 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6789 break;
6790
6791 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 * If tag starts with ', toss everything after a second '. Fixes
6793 * CTRL-] on 'option'. (would include the trailing '.').
6794 */
6795 if (*s == '\'' && s > arg && *arg == '\'')
6796 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006797 /* Also '{' and '}'. */
6798 if (*s == '}' && s > arg && *arg == '{')
6799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 }
6801 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006802
6803 if (*IObuff == '`')
6804 {
6805 if (d > IObuff + 2 && d[-1] == '`')
6806 {
6807 /* remove the backticks from `command` */
6808 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6809 d[-2] = NUL;
6810 }
6811 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6812 {
6813 /* remove the backticks and comma from `command`, */
6814 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6815 d[-3] = NUL;
6816 }
6817 else if (d > IObuff + 4 && d[-3] == '`'
6818 && d[-2] == '\\' && d[-1] == '.')
6819 {
6820 /* remove the backticks and dot from `command`\. */
6821 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6822 d[-4] = NUL;
6823 }
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826 }
6827
6828 *matches = (char_u **)"";
6829 *num_matches = 0;
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006830 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE | TAG_NO_TAGFUNC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 if (keep_lang)
6832 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006833 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 /* Sort the matches found on the heuristic number that is after the
6837 * tag name. */
6838 qsort((void *)*matches, (size_t)*num_matches,
6839 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006840 /* Delete more than TAG_MANY to reduce the size of the listing. */
6841 while (*num_matches > TAG_MANY)
6842 vim_free((*matches)[--*num_matches]);
6843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 return OK;
6845}
6846
6847/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006848 * Called when starting to edit a buffer for a help file.
6849 */
6850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006851prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006852{
6853 char_u *p;
6854
6855 curbuf->b_help = TRUE;
6856#ifdef FEAT_QUICKFIX
6857 set_string_option_direct((char_u *)"buftype", -1,
6858 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6859#endif
6860
6861 /*
6862 * Always set these options after jumping to a help tag, because the
6863 * user may have an autocommand that gets in the way.
6864 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6865 * latin1 word characters (for translated help files).
6866 * Only set it when needed, buf_init_chartab() is some work.
6867 */
6868 p =
6869#ifdef EBCDIC
6870 (char_u *)"65-255,^*,^|,^\"";
6871#else
6872 (char_u *)"!-~,^*,^|,^\",192-255";
6873#endif
6874 if (STRCMP(curbuf->b_p_isk, p) != 0)
6875 {
6876 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6877 check_buf_options(curbuf);
6878 (void)buf_init_chartab(curbuf, FALSE);
6879 }
6880
Bram Moolenaar0b105412014-11-30 13:34:23 +01006881#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006882 /* Don't use the global foldmethod.*/
6883 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6884 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006885#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006886
6887 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6888 curwin->w_p_list = FALSE; /* no list mode */
6889
6890 curbuf->b_p_ma = FALSE; /* not modifiable */
6891 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6892 curwin->w_p_nu = 0; /* no line numbers */
6893 curwin->w_p_rnu = 0; /* no relative line numbers */
6894 RESET_BINDING(curwin); /* no scroll or cursor binding */
6895#ifdef FEAT_ARABIC
6896 curwin->w_p_arab = FALSE; /* no arabic mode */
6897#endif
6898#ifdef FEAT_RIGHTLEFT
6899 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6900#endif
6901#ifdef FEAT_FOLDING
6902 curwin->w_p_fen = FALSE; /* No folding in the help window */
6903#endif
6904#ifdef FEAT_DIFF
6905 curwin->w_p_diff = FALSE; /* No 'diff' */
6906#endif
6907#ifdef FEAT_SPELL
6908 curwin->w_p_spell = FALSE; /* No spell checking */
6909#endif
6910
6911 set_buflisted(FALSE);
6912}
6913
6914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 * After reading a help file: May cleanup a help buffer when syntax
6916 * highlighting is not used.
6917 */
6918 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006919fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 linenr_T lnum;
6922 char_u *line;
6923 int in_example = FALSE;
6924 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006925 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 char_u *p;
6927 char_u *rt;
6928 int mustfree;
6929
Bram Moolenaar90492982017-06-22 14:16:31 +02006930 /* Set filetype to "help" if still needed. */
6931 if (STRCMP(curbuf->b_p_ft, "help") != 0)
Bram Moolenaar18141832017-06-25 21:17:25 +02006932 {
6933 ++curbuf_lock;
Bram Moolenaar90492982017-06-22 14:16:31 +02006934 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
Bram Moolenaar18141832017-06-25 21:17:25 +02006935 --curbuf_lock;
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
6938#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006939 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#endif
6941 {
6942 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6943 {
6944 line = ml_get_buf(curbuf, lnum, FALSE);
6945 len = (int)STRLEN(line);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006946 if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
6948 /* End of example: non-white or '<' in first column. */
6949 if (line[0] == '<')
6950 {
6951 /* blank-out a '<' in the first column */
6952 line = ml_get_buf(curbuf, lnum, TRUE);
6953 line[0] = ' ';
6954 }
6955 in_example = FALSE;
6956 }
6957 if (!in_example && len > 0)
6958 {
6959 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6960 {
6961 /* blank-out a '>' in the last column (start of example) */
6962 line = ml_get_buf(curbuf, lnum, TRUE);
6963 line[len - 1] = ' ';
6964 in_example = TRUE;
6965 }
6966 else if (line[len - 1] == '~')
6967 {
6968 /* blank-out a '~' at the end of line (header marker) */
6969 line = ml_get_buf(curbuf, lnum, TRUE);
6970 line[len - 1] = ' ';
6971 }
6972 }
6973 }
6974 }
6975
6976 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006977 * In the "help.txt" and "help.abx" file, add the locally added help
6978 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006980 fname = gettail(curbuf->b_fname);
6981 if (fnamecmp(fname, "help.txt") == 0
6982#ifdef FEAT_MULTI_LANG
6983 || (fnamencmp(fname, "help.", 5) == 0
6984 && ASCII_ISALPHA(fname[5])
6985 && ASCII_ISALPHA(fname[6])
6986 && TOLOWER_ASC(fname[7]) == 'x'
6987 && fname[8] == NUL)
6988#endif
6989 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
6991 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6992 {
6993 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006994 if (strstr((char *)line, "*local-additions*") == NULL)
6995 continue;
6996
6997 /* Go through all directories in 'runtimepath', skipping
6998 * $VIMRUNTIME. */
6999 p = p_rtp;
7000 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007002 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7003 mustfree = FALSE;
7004 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
Bram Moolenaare4db7ae2018-02-13 13:12:11 +01007005 if (rt != NULL && fullpathcmp(rt, NameBuff, FALSE) != 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",
7227 dir, FALSE) == 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 }
7322 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7323 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