blob: 2ed2bdb4d31b65ba5f72893e8509fda76ab724a0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_cmds.c: some functions for command line commands
12 */
13
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014#include "vim.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#include "version.h"
16
Bram Moolenaar17576a12016-01-20 20:05:44 +010017#ifdef FEAT_FLOAT
18# include <float.h>
19#endif
20
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010021static int linelen(int *has_tab);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010022static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010024static char_u *viminfo_filename(char_u *);
25static void do_viminfo(FILE *fp_in, FILE *fp_out, int flags);
26static int viminfo_encoding(vir_T *virp);
27static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#endif
29
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static int check_readonly(int *forceit, buf_T *buf);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static void delbuf_msg(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static int
33#ifdef __BORLANDC__
34 _RTLENTRYF
35#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036 help_compare(const void *s1, const void *s2);
37static void prepare_help_buffer(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
40 * ":ascii" and "ga".
41 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010043do_ascii(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000046 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 char buf1[20];
48 char buf2[20];
49 char_u buf3[7];
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010050#ifdef FEAT_DIGRAPHS
51 char_u *dig;
52#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000054 int cc[MAX_MCO];
55 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 int len;
57
58 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000059 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 else
61#endif
62 c = gchar_cursor();
63 if (c == NUL)
64 {
65 MSG("NUL");
66 return;
67 }
68
69#ifdef FEAT_MBYTE
70 IObuff[0] = NUL;
71 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
72#endif
73 {
74 if (c == NL) /* NUL is stored as NL */
75 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000076 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
77 cval = NL; /* NL is stored as CR */
78 else
79 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 if (vim_isprintc_strict(c) && (c < ' '
81#ifndef EBCDIC
82 || c > '~'
83#endif
84 ))
85 {
86 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000087 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 }
89 else
90 buf1[0] = NUL;
91#ifndef EBCDIC
92 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000093 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
94 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 else
96#endif
97 buf2[0] = NUL;
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010098#ifdef FEAT_DIGRAPHS
99 dig = get_digraph_for_char(cval);
100 if (dig != NULL)
101 vim_snprintf((char *)IObuff, IOSIZE,
102 _("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"),
103 transchar(c), buf1, buf2, cval, cval, cval, dig);
104 else
105#endif
106 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar555b2802005-05-19 21:08:39 +0000107 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +0000108 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#ifdef FEAT_MBYTE
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000110 if (enc_utf8)
111 c = cc[ci++];
112 else
113 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
115 }
116
117#ifdef FEAT_MBYTE
118 /* Repeat for combining characters. */
119 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
120 {
121 len = (int)STRLEN(IObuff);
122 /* This assumes every multi-byte char is printable... */
123 if (len > 0)
124 IObuff[len++] = ' ';
125 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000126 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000127# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 )
131 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000132 len += (*mb_char2bytes)(c, IObuff + len);
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100133#ifdef FEAT_DIGRAPHS
134 dig = get_digraph_for_char(c);
135 if (dig != NULL)
136 vim_snprintf((char *)IObuff + len, IOSIZE - len,
137 c < 0x10000 ? _("> %d, Hex %04x, Oct %o, Digr %s")
138 : _("> %d, Hex %08x, Oct %o, Digr %s"),
139 c, c, c, dig);
140 else
141#endif
142 vim_snprintf((char *)IObuff + len, IOSIZE - len,
143 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
144 : _("> %d, Hex %08x, Octal %o"),
145 c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000146 if (ci == MAX_MCO)
147 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000148 if (enc_utf8)
149 c = cc[ci++];
150 else
151 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 }
153#endif
154
155 msg(IObuff);
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * ":left", ":center" and ":right": align text.
160 */
161 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100162ex_align(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
164 pos_T save_curpos;
165 int len;
166 int indent = 0;
167 int new_indent;
168 int has_tab;
169 int width;
170
171#ifdef FEAT_RIGHTLEFT
172 if (curwin->w_p_rl)
173 {
174 /* switch left and right aligning */
175 if (eap->cmdidx == CMD_right)
176 eap->cmdidx = CMD_left;
177 else if (eap->cmdidx == CMD_left)
178 eap->cmdidx = CMD_right;
179 }
180#endif
181
182 width = atoi((char *)eap->arg);
183 save_curpos = curwin->w_cursor;
184 if (eap->cmdidx == CMD_left) /* width is used for new indent */
185 {
186 if (width >= 0)
187 indent = width;
188 }
189 else
190 {
191 /*
192 * if 'textwidth' set, use it
193 * else if 'wrapmargin' set, use it
194 * if invalid value, use 80
195 */
196 if (width <= 0)
197 width = curbuf->b_p_tw;
198 if (width == 0 && curbuf->b_p_wm > 0)
Bram Moolenaar02631462017-09-22 15:20:32 +0200199 width = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 if (width <= 0)
201 width = 80;
202 }
203
204 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
205 return;
206
207 for (curwin->w_cursor.lnum = eap->line1;
208 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
209 {
210 if (eap->cmdidx == CMD_left) /* left align */
211 new_indent = indent;
212 else
213 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000214 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 len = linelen(eap->cmdidx == CMD_right ? &has_tab
216 : NULL) - get_indent();
217
218 if (len <= 0) /* skip blank lines */
219 continue;
220
221 if (eap->cmdidx == CMD_center)
222 new_indent = (width - len) / 2;
223 else
224 {
225 new_indent = width - len; /* right align */
226
227 /*
228 * Make sure that embedded TABs don't make the text go too far
229 * to the right.
230 */
231 if (has_tab)
232 while (new_indent > 0)
233 {
234 (void)set_indent(new_indent, 0);
235 if (linelen(NULL) <= width)
236 {
237 /*
238 * Now try to move the line as much as possible to
239 * the right. Stop when it moves too far.
240 */
241 do
242 (void)set_indent(++new_indent, 0);
243 while (linelen(NULL) <= width);
244 --new_indent;
245 break;
246 }
247 --new_indent;
248 }
249 }
250 }
251 if (new_indent < 0)
252 new_indent = 0;
253 (void)set_indent(new_indent, 0); /* set indent */
254 }
255 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
256 curwin->w_cursor = save_curpos;
257 beginline(BL_WHITE | BL_FIX);
258}
259
260/*
261 * Get the length of the current line, excluding trailing white space.
262 */
263 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100264linelen(int *has_tab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 char_u *line;
267 char_u *first;
268 char_u *last;
269 int save;
270 int len;
271
272 /* find the first non-blank character */
273 line = ml_get_curline();
274 first = skipwhite(line);
275
276 /* find the character after the last non-blank character */
277 for (last = first + STRLEN(first);
Bram Moolenaar1c465442017-03-12 20:10:05 +0100278 last > first && VIM_ISWHITE(last[-1]); --last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 ;
280 save = *last;
281 *last = NUL;
282 len = linetabsize(line); /* get line length */
283 if (has_tab != NULL) /* check for embedded TAB */
284 *has_tab = (vim_strrchr(first, TAB) != NULL);
285 *last = save;
286
287 return len;
288}
289
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000290/* Buffer for two lines used during sorting. They are allocated to
291 * contain the longest line being sorted. */
292static char_u *sortbuf1;
293static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000294
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100295static int sort_ic; /* ignore case */
296static int sort_nr; /* sort on number */
297static int sort_rx; /* sort on regex instead of skipping it */
298#ifdef FEAT_FLOAT
299static int sort_flt; /* sort on floating number */
300#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000301
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100302static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000303
304/* Struct to store info to be sorted. */
305typedef struct
306{
307 linenr_T lnum; /* line number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100308 union {
309 struct
310 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200311 varnumber_T start_col_nr; /* starting column number */
312 varnumber_T end_col_nr; /* ending column number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100313 } line;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200314 varnumber_T value; /* value if sorting by integer */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100315#ifdef FEAT_FLOAT
316 float_T value_flt; /* value if sorting by float */
317#endif
318 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000319} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000320
321static int
322#ifdef __BORLANDC__
323_RTLENTRYF
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325sort_compare(const void *s1, const void *s2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000326
327 static int
328#ifdef __BORLANDC__
329_RTLENTRYF
330#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100331sort_compare(const void *s1, const void *s2)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000332{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000333 sorti_T l1 = *(sorti_T *)s1;
334 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000335 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000336
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000338 * if we return 0 every time, qsort will assume it's done sorting and
339 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000340 if (sort_abort)
341 return 0;
342 fast_breakcheck();
343 if (got_int)
344 sort_abort = TRUE;
345
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000346 /* When sorting numbers "start_col_nr" is the number, not the column
347 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000348 if (sort_nr)
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100349 result = l1.st_u.value == l2.st_u.value ? 0
350 : l1.st_u.value > l2.st_u.value ? 1 : -1;
351#ifdef FEAT_FLOAT
352 else if (sort_flt)
353 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
354 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
355#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000356 else
357 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000358 /* We need to copy one line into "sortbuf1", because there is no
359 * guarantee that the first pointer becomes invalid when obtaining the
360 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100361 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
362 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
363 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
364 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
365 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
366 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000367
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000368 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
369 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000370 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000371
372 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000373 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000374 return (int)(l1.lnum - l2.lnum);
375 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000376}
377
378/*
379 * ":sort".
380 */
381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100382ex_sort(exarg_T *eap)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000383{
384 regmatch_T regmatch;
385 int len;
386 linenr_T lnum;
387 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000388 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000389 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000390 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000391 char_u *p;
392 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000393 char_u *s2;
394 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000395 int unique = FALSE;
396 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000397 colnr_T start_col;
398 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100399 int sort_what = 0;
400 int format_found = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000401
Bram Moolenaar48c99162008-02-18 18:42:52 +0000402 /* Sorting one line is really quick! */
403 if (count <= 1)
404 return;
405
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000406 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
407 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000408 sortbuf1 = NULL;
409 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000410 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000411 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000412 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000413 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000414
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100415 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100416#ifdef FEAT_FLOAT
417 sort_flt = 0;
418#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000419
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000420 for (p = eap->arg; *p != NUL; ++p)
421 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100422 if (VIM_ISWHITE(*p))
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000423 ;
424 else if (*p == 'i')
425 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000426 else if (*p == 'r')
427 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000428 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100429 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100430 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100431 ++format_found;
432 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100433#ifdef FEAT_FLOAT
434 else if (*p == 'f')
435 {
436 sort_flt = 1;
437 ++format_found;
438 }
439#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100440 else if (*p == 'b')
441 {
442 sort_what = STR2NR_BIN + STR2NR_FORCE;
443 ++format_found;
444 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000445 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100446 {
447 sort_what = STR2NR_OCT + STR2NR_FORCE;
448 ++format_found;
449 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000450 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100451 {
452 sort_what = STR2NR_HEX + STR2NR_FORCE;
453 ++format_found;
454 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000455 else if (*p == 'u')
456 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000457 else if (*p == '"') /* comment start */
458 break;
459 else if (check_nextcmd(p) != NULL)
460 {
461 eap->nextcmd = check_nextcmd(p);
462 break;
463 }
464 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000465 {
466 s = skip_regexp(p + 1, *p, TRUE, NULL);
467 if (*s != *p)
468 {
469 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000470 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000471 }
472 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000473 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100474 if (s == p + 1)
475 {
476 if (last_search_pat() == NULL)
477 {
478 EMSG(_(e_noprevre));
479 goto sortend;
480 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000481 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100482 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000483 else
484 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000485 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000486 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000487 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000488 regmatch.rm_ic = p_ic;
489 }
490 else
491 {
492 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000493 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000494 }
495 }
496
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100497 /* Can only have one of 'n', 'b', 'o' and 'x'. */
498 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000499 {
500 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000501 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000502 }
503
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100504 /* From here on "sort_nr" is used as a flag for any integer number
505 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100506 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000507
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000508 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000509 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000510 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000511 * When sorting on strings "start_col_nr" is the offset in the line, for
512 * numbers sorting it's the number to sort on. This means the pattern
513 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000514 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000515 */
516 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
517 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000518 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000519 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000520 if (maxlen < len)
521 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000522
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000523 start_col = 0;
524 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000525 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000526 {
527 if (sort_rx)
528 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000529 start_col = (colnr_T)(regmatch.startp[0] - s);
530 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000531 }
532 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000533 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000534 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000535 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000536 if (regmatch.regprog != NULL)
537 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000538
Bram Moolenaar937204a2016-01-30 23:37:38 +0100539 if (sort_nr
540#ifdef FEAT_FLOAT
541 || sort_flt
542#endif
543 )
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000544 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000545 /* Make sure vim_str2nr doesn't read any digits past the end
546 * of the match, by temporarily terminating the string there */
547 s2 = s + end_col;
548 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200549 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000550 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000551 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100552 if (sort_nr)
553 {
554 if (sort_what & STR2NR_HEX)
555 s = skiptohex(p);
556 else if (sort_what & STR2NR_BIN)
557 s = skiptobin(p);
558 else
559 s = skiptodigit(p);
560 if (s > p && s[-1] == '-')
561 --s; /* include preceding negative sign */
562 if (*s == NUL)
563 /* empty line should sort before any number */
564 nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
565 else
566 vim_str2nr(s, NULL, NULL, sort_what,
567 &nrs[lnum - eap->line1].st_u.value, NULL, 0);
568 }
569#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000570 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100571 {
572 s = skipwhite(p);
573 if (*s == '+')
574 s = skipwhite(s + 1);
575
576 if (*s == NUL)
577 /* empty line should sort before any number */
578 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
579 else
580 nrs[lnum - eap->line1].st_u.value_flt =
581 strtod((char *)s, NULL);
582 }
583#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200584 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000585 }
586 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000587 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000588 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100589 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
590 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000591 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000592
593 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000594
595 if (regmatch.regprog != NULL)
596 fast_breakcheck();
597 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000598 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000599 }
600
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000601 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000602 sortbuf1 = alloc((unsigned)maxlen + 1);
603 if (sortbuf1 == NULL)
604 goto sortend;
605 sortbuf2 = alloc((unsigned)maxlen + 1);
606 if (sortbuf2 == NULL)
607 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000608
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000609 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000610 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000611
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000612 if (sort_abort)
613 goto sortend;
614
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000615 /* Insert the lines in the sorted order below the last one. */
616 lnum = eap->line2;
617 for (i = 0; i < count; ++i)
618 {
619 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
620 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000621 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000622 {
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100623 /* Copy the line into a buffer, it may become invalid in
624 * ml_append(). And it's needed for "unique". */
625 STRCPY(sortbuf1, s);
626 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000627 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000628 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000629 fast_breakcheck();
630 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000631 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000632 }
633
634 /* delete the original lines if appending worked */
635 if (i == count)
636 for (i = 0; i < count; ++i)
637 ml_delete(eap->line1, FALSE);
638 else
639 count = 0;
640
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000641 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000642 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000643 if (deleted > 0)
644 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
645 else if (deleted < 0)
646 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
647 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000648
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000649 curwin->w_cursor.lnum = eap->line1;
650 beginline(BL_WHITE | BL_FIX);
651
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000652sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000653 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000654 vim_free(sortbuf1);
655 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200656 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000657 if (got_int)
658 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000659}
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661/*
662 * ":retab".
663 */
664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100665ex_retab(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
667 linenr_T lnum;
668 int got_tab = FALSE;
669 long num_spaces = 0;
670 long num_tabs;
671 long len;
672 long col;
673 long vcol;
674 long start_col = 0; /* For start of white-space string */
675 long start_vcol = 0; /* For start of white-space string */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 long old_len;
677 char_u *ptr;
678 char_u *new_line = (char_u *)1; /* init to non-NULL */
679 int did_undo; /* called u_save for current line */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200680#ifdef FEAT_VARTABS
681 int *new_ts = 0;
682 char_u *new_ts_str; /* string value of tab argument */
683#else
684 int temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 int new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 int save_list;
688 linenr_T first_line = 0; /* first changed line */
689 linenr_T last_line = 0; /* last changed line */
690
691 save_list = curwin->w_p_list;
692 curwin->w_p_list = 0; /* don't want list mode here */
693
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200694#ifdef FEAT_VARTABS
695 new_ts_str = eap->arg;
696 if (!tabstop_set(eap->arg, &new_ts))
697 return;
698 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
699 ++(eap->arg);
700
701 // This ensures that either new_ts and new_ts_str are freshly allocated,
702 // or new_ts points to an existing array and new_ts_str is null.
703 if (new_ts == 0)
704 {
705 new_ts = curbuf->b_p_vts_array;
706 new_ts_str = NULL;
707 }
708 else
709 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
710#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 new_ts = getdigits(&(eap->arg));
712 if (new_ts < 0)
713 {
714 EMSG(_(e_positive));
715 return;
716 }
717 if (new_ts == 0)
718 new_ts = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
721 {
722 ptr = ml_get(lnum);
723 col = 0;
724 vcol = 0;
725 did_undo = FALSE;
726 for (;;)
727 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100728 if (VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
730 if (!got_tab && num_spaces == 0)
731 {
732 /* First consecutive white-space */
733 start_vcol = vcol;
734 start_col = col;
735 }
736 if (ptr[col] == ' ')
737 num_spaces++;
738 else
739 got_tab = TRUE;
740 }
741 else
742 {
743 if (got_tab || (eap->forceit && num_spaces > 1))
744 {
745 /* Retabulate this string of white-space */
746
747 /* len is virtual length of white string */
748 len = num_spaces = vcol - start_vcol;
749 num_tabs = 0;
750 if (!curbuf->b_p_et)
751 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200752#ifdef FEAT_VARTABS
753 int t, s;
754
755 tabstop_fromto(start_vcol, vcol,
756 tabstop_count(new_ts)? 0: curbuf->b_p_ts,
757 new_ts,
758 &t, &s);
759 num_tabs = t;
760 num_spaces = s;
761#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 temp = new_ts - (start_vcol % new_ts);
763 if (num_spaces >= temp)
764 {
765 num_spaces -= temp;
766 num_tabs++;
767 }
768 num_tabs += num_spaces / new_ts;
769 num_spaces -= (num_spaces / new_ts) * new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772 if (curbuf->b_p_et || got_tab ||
773 (num_spaces + num_tabs < len))
774 {
775 if (did_undo == FALSE)
776 {
777 did_undo = TRUE;
778 if (u_save((linenr_T)(lnum - 1),
779 (linenr_T)(lnum + 1)) == FAIL)
780 {
781 new_line = NULL; /* flag out-of-memory */
782 break;
783 }
784 }
785
786 /* len is actual number of white characters used */
787 len = num_spaces + num_tabs;
788 old_len = (long)STRLEN(ptr);
789 new_line = lalloc(old_len - col + start_col + len + 1,
790 TRUE);
791 if (new_line == NULL)
792 break;
793 if (start_col > 0)
794 mch_memmove(new_line, ptr, (size_t)start_col);
795 mch_memmove(new_line + start_col + len,
796 ptr + col, (size_t)(old_len - col + 1));
797 ptr = new_line + start_col;
798 for (col = 0; col < len; col++)
799 ptr[col] = (col < num_tabs) ? '\t' : ' ';
800 ml_replace(lnum, new_line, FALSE);
801 if (first_line == 0)
802 first_line = lnum;
803 last_line = lnum;
804 ptr = new_line;
805 col = start_col + len;
806 }
807 }
808 got_tab = FALSE;
809 num_spaces = 0;
810 }
811 if (ptr[col] == NUL)
812 break;
813 vcol += chartabsize(ptr + col, (colnr_T)vcol);
814#ifdef FEAT_MBYTE
815 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000816 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 else
818#endif
819 ++col;
820 }
821 if (new_line == NULL) /* out of memory */
822 break;
823 line_breakcheck();
824 }
825 if (got_int)
826 EMSG(_(e_interr));
827
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200828#ifdef FEAT_VARTABS
829 // If a single value was given then it can be considered equal to
830 // either the value of 'tabstop' or the value of 'vartabstop'.
831 if (tabstop_count(curbuf->b_p_vts_array) == 0
832 && tabstop_count(new_ts) == 1
833 && curbuf->b_p_ts == tabstop_first(new_ts))
834 ; /* not changed */
835 else if (tabstop_count(curbuf->b_p_vts_array) > 0
836 && tabstop_eq(curbuf->b_p_vts_array, new_ts))
837 ; /* not changed */
838 else
839 redraw_curbuf_later(NOT_VALID);
840#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (curbuf->b_p_ts != new_ts)
842 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (first_line != 0)
845 changed_lines(first_line, 0, last_line + 1, 0L);
846
847 curwin->w_p_list = save_list; /* restore 'list' */
848
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200849#ifdef FEAT_VARTABS
850 if (new_ts_str != NULL) /* set the new tabstop */
851 {
852 // If 'vartabstop' is in use or if the value given to retab has more
853 // than one tabstop then update 'vartabstop'.
854 int *old_vts_ary = curbuf->b_p_vts_array;
855
856 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_ts) > 1)
857 {
858 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
859 OPT_FREE|OPT_LOCAL, 0);
860 vim_free(new_ts_str);
861 curbuf->b_p_vts_array = new_ts;
862 vim_free(old_vts_ary);
863 }
864 else
865 {
866 // 'vartabstop' wasn't in use and a single value was given to
867 // retab then update 'tabstop'.
868 curbuf->b_p_ts = tabstop_first(new_ts);
869 vim_free(new_ts);
870 }
871 }
872#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 curbuf->b_p_ts = new_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 coladvance(curwin->w_curswant);
876
877 u_clearline();
878}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
880/*
881 * :move command - move lines line1-line2 to line dest
882 *
883 * return FAIL for failure, OK otherwise
884 */
885 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100886do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 char_u *str;
889 linenr_T l;
890 linenr_T extra; /* Num lines added before line1 */
891 linenr_T num_lines; /* Num lines moved */
892 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200893#ifdef FEAT_FOLDING
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100894 win_T *win;
895 tabpage_T *tp;
Bram Moolenaard5f69332015-04-15 12:43:50 +0200896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
898 if (dest >= line1 && dest < line2)
899 {
900 EMSG(_("E134: Move lines into themselves"));
901 return FAIL;
902 }
903
904 num_lines = line2 - line1 + 1;
905
906 /*
907 * First we copy the old text to its new location -- webb
908 * Also copy the flag that ":global" command uses.
909 */
910 if (u_save(dest, dest + 1) == FAIL)
911 return FAIL;
912 for (extra = 0, l = line1; l <= line2; l++)
913 {
914 str = vim_strsave(ml_get(l + extra));
915 if (str != NULL)
916 {
917 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
918 vim_free(str);
919 if (dest < line1)
920 extra++;
921 }
922 }
923
924 /*
925 * Now we must be careful adjusting our marks so that we don't overlap our
926 * mark_adjust() calls.
927 *
928 * We adjust the marks within the old text so that they refer to the
929 * last lines of the file (temporarily), because we know no other marks
930 * will be set there since these line numbers did not exist until we added
931 * our new lines.
932 *
933 * Then we adjust the marks on lines between the old and new text positions
934 * (either forwards or backwards).
935 *
936 * And Finally we adjust the marks we put at the end of the file back to
937 * their final destination at the new text position -- webb
938 */
939 last_line = curbuf->b_ml.ml_line_count;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100940 mark_adjust_nofold(line1, line2, last_line - line2, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (dest >= line2)
942 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100943 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
944#ifdef FEAT_FOLDING
945 FOR_ALL_TAB_WINDOWS(tp, win) {
946 if (win->w_buffer == curbuf)
947 foldMoveRange(&win->w_folds, line1, line2, dest);
948 }
949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 curbuf->b_op_start.lnum = dest - num_lines + 1;
951 curbuf->b_op_end.lnum = dest;
952 }
953 else
954 {
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100955 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
956#ifdef FEAT_FOLDING
957 FOR_ALL_TAB_WINDOWS(tp, win) {
958 if (win->w_buffer == curbuf)
959 foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
960 }
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 curbuf->b_op_start.lnum = dest + 1;
963 curbuf->b_op_end.lnum = dest + num_lines;
964 }
965 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100966 mark_adjust_nofold(last_line - num_lines + 1, last_line,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 -(last_line - dest - extra), 0L);
968
969 /*
970 * Now we delete the original text -- webb
971 */
972 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
973 return FAIL;
974
975 for (l = line1; l <= line2; l++)
976 ml_delete(line1 + extra, TRUE);
977
978 if (!global_busy && num_lines > p_report)
979 {
980 if (num_lines == 1)
981 MSG(_("1 line moved"));
982 else
983 smsg((char_u *)_("%ld lines moved"), num_lines);
984 }
985
986 /*
987 * Leave the cursor on the last of the moved lines.
988 */
989 if (dest >= line1)
990 curwin->w_cursor.lnum = dest;
991 else
992 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
993
994 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +0100995 {
996 dest += num_lines + 1;
997 last_line = curbuf->b_ml.ml_line_count;
998 if (dest > last_line + 1)
999 dest = last_line + 1;
1000 changed_lines(line1, 0, dest, 0L);
1001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else
1003 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
1004
1005 return OK;
1006}
1007
1008/*
1009 * ":copy"
1010 */
1011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001012ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 linenr_T count;
1015 char_u *p;
1016
1017 count = line2 - line1 + 1;
1018 curbuf->b_op_start.lnum = n + 1;
1019 curbuf->b_op_end.lnum = n + count;
1020 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
1021
1022 /*
1023 * there are three situations:
1024 * 1. destination is above line1
1025 * 2. destination is between line1 and line2
1026 * 3. destination is below line2
1027 *
1028 * n = destination (when starting)
1029 * curwin->w_cursor.lnum = destination (while copying)
1030 * line1 = start of source (while copying)
1031 * line2 = end of source (while copying)
1032 */
1033 if (u_save(n, n + 1) == FAIL)
1034 return;
1035
1036 curwin->w_cursor.lnum = n;
1037 while (line1 <= line2)
1038 {
1039 /* need to use vim_strsave() because the line will be unlocked within
1040 * ml_append() */
1041 p = vim_strsave(ml_get(line1));
1042 if (p != NULL)
1043 {
1044 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
1045 vim_free(p);
1046 }
1047 /* situation 2: skip already copied lines */
1048 if (line1 == n)
1049 line1 = curwin->w_cursor.lnum;
1050 ++line1;
1051 if (curwin->w_cursor.lnum < line1)
1052 ++line1;
1053 if (curwin->w_cursor.lnum < line2)
1054 ++line2;
1055 ++curwin->w_cursor.lnum;
1056 }
1057
1058 appended_lines_mark(n, count);
1059
1060 msgmore((long)count);
1061}
1062
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001063static char_u *prevcmd = NULL; /* the previous command */
1064
1065#if defined(EXITFREE) || defined(PROTO)
1066 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001067free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001068{
1069 vim_free(prevcmd);
1070}
1071#endif
1072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073/*
1074 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
1075 * Bangs in the argument are replaced with the previously entered command.
1076 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 */
1078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001079do_bang(
1080 int addr_count,
1081 exarg_T *eap,
1082 int forceit,
1083 int do_in,
1084 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 char_u *arg = eap->arg; /* command */
1087 linenr_T line1 = eap->line1; /* start of range */
1088 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 char_u *newcmd = NULL; /* the new command */
1090 int free_newcmd = FALSE; /* need to free() newcmd */
1091 int ins_prevcmd;
1092 char_u *t;
1093 char_u *p;
1094 char_u *trailarg;
1095 int len;
1096 int scroll_save = msg_scroll;
1097
1098 /*
1099 * Disallow shell commands for "rvim".
1100 * Disallow shell commands from .exrc and .vimrc in current directory for
1101 * security reasons.
1102 */
1103 if (check_restricted() || check_secure())
1104 return;
1105
1106 if (addr_count == 0) /* :! */
1107 {
1108 msg_scroll = FALSE; /* don't scroll here */
1109 autowrite_all();
1110 msg_scroll = scroll_save;
1111 }
1112
1113 /*
1114 * Try to find an embedded bang, like in :!<cmd> ! [args]
1115 * (:!! is indicated by the 'forceit' variable)
1116 */
1117 ins_prevcmd = forceit;
1118 trailarg = arg;
1119 do
1120 {
1121 len = (int)STRLEN(trailarg) + 1;
1122 if (newcmd != NULL)
1123 len += (int)STRLEN(newcmd);
1124 if (ins_prevcmd)
1125 {
1126 if (prevcmd == NULL)
1127 {
1128 EMSG(_(e_noprev));
1129 vim_free(newcmd);
1130 return;
1131 }
1132 len += (int)STRLEN(prevcmd);
1133 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001134 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
1136 vim_free(newcmd);
1137 return;
1138 }
1139 *t = NUL;
1140 if (newcmd != NULL)
1141 STRCAT(t, newcmd);
1142 if (ins_prevcmd)
1143 STRCAT(t, prevcmd);
1144 p = t + STRLEN(t);
1145 STRCAT(t, trailarg);
1146 vim_free(newcmd);
1147 newcmd = t;
1148
1149 /*
1150 * Scan the rest of the argument for '!', which is replaced by the
1151 * previous command. "\!" is replaced by "!" (this is vi compatible).
1152 */
1153 trailarg = NULL;
1154 while (*p)
1155 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001156 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001159 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 else
1161 {
1162 trailarg = p;
1163 *trailarg++ = NUL;
1164 ins_prevcmd = TRUE;
1165 break;
1166 }
1167 }
1168 ++p;
1169 }
1170 } while (trailarg != NULL);
1171
1172 vim_free(prevcmd);
1173 prevcmd = newcmd;
1174
1175 if (bangredo) /* put cmd in redo buffer for ! command */
1176 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001177 /* If % or # appears in the command, it must have been escaped.
1178 * Reescape them, so that redoing them does not substitute them by the
1179 * buffername. */
1180 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1181
1182 if (cmd != NULL)
1183 {
1184 AppendToRedobuffLit(cmd, -1);
1185 vim_free(cmd);
1186 }
1187 else
1188 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 AppendToRedobuff((char_u *)"\n");
1190 bangredo = FALSE;
1191 }
1192 /*
1193 * Add quotes around the command, for shells that need them.
1194 */
1195 if (*p_shq != NUL)
1196 {
1197 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1198 if (newcmd == NULL)
1199 return;
1200 STRCPY(newcmd, p_shq);
1201 STRCAT(newcmd, prevcmd);
1202 STRCAT(newcmd, p_shq);
1203 free_newcmd = TRUE;
1204 }
1205 if (addr_count == 0) /* :! */
1206 {
1207 /* echo the command */
1208 msg_start();
1209 msg_putchar(':');
1210 msg_putchar('!');
1211 msg_outtrans(newcmd);
1212 msg_clr_eos();
1213 windgoto(msg_row, msg_col);
1214
1215 do_shell(newcmd, 0);
1216 }
1217 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 /* Careful: This may recursively call do_bang() again! (because of
1220 * autocommands) */
1221 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001222 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarade00832006-03-10 21:46:58 +00001223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (free_newcmd)
1225 vim_free(newcmd);
1226}
1227
1228/*
1229 * do_filter: filter lines through a command given by the user
1230 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231 * We mostly use temp files and the call_shell() routine here. This would
1232 * normally be done using pipes on a UNIX machine, but this is more portable
1233 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * to deal with redirection somehow, and should handle things like looking
1235 * at the PATH env. variable, and adding reasonable extensions to the
1236 * command name given by the user. All reasonable versions of call_shell()
1237 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 * Alternatively, if on Unix and redirecting input or output, but not both,
1239 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 * We use input redirection if do_in is TRUE.
1241 * We use output redirection if do_out is TRUE.
1242 */
1243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001244do_filter(
1245 linenr_T line1,
1246 linenr_T line2,
1247 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1248 char_u *cmd,
1249 int do_in,
1250 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252 char_u *itmp = NULL;
1253 char_u *otmp = NULL;
1254 linenr_T linecount;
1255 linenr_T read_linecount;
1256 pos_T cursor_save;
1257 char_u *cmd_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 buf_T *old_curbuf = curbuf;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001259 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261 if (*cmd == NUL) /* no filter command */
1262 return;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 cursor_save = curwin->w_cursor;
1265 linecount = line2 - line1 + 1;
1266 curwin->w_cursor.lnum = line1;
1267 curwin->w_cursor.col = 0;
1268 changed_line_abv_curs();
1269 invalidate_botline();
1270
1271 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001272 * When using temp files:
1273 * 1. * Form temp file names
1274 * 2. * Write the lines to a temp file
1275 * 3. Run the filter command on the temp file
1276 * 4. * Read the output of the command into the buffer
1277 * 5. * Delete the original lines to be filtered
1278 * 6. * Remove the temp files
1279 *
1280 * When writing the input with a pipe or when catching the output with a
1281 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 */
1283
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001284 if (do_out)
1285 shell_flags |= SHELL_DOOUT;
1286
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001287#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001288 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001290 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1291 shell_flags |= SHELL_READ;
1292 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001294 else if (do_in && !do_out && !p_stmp)
1295 {
1296 /* Use a pipe to write stdin of the command, do not use a temp file. */
1297 shell_flags |= SHELL_WRITE;
1298 curbuf->b_op_start.lnum = line1;
1299 curbuf->b_op_end.lnum = line2;
1300 }
1301 else if (do_in && do_out && !p_stmp)
1302 {
1303 /* Use a pipe to write stdin and fetch stdout of the command, do not
1304 * use a temp file. */
1305 shell_flags |= SHELL_READ|SHELL_WRITE;
1306 curbuf->b_op_start.lnum = line1;
1307 curbuf->b_op_end.lnum = line2;
1308 curwin->w_cursor.lnum = line2;
1309 }
1310 else
1311#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001312 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1313 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001314 {
1315 EMSG(_(e_notmp));
1316 goto filterend;
1317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319/*
1320 * The writing and reading of temp files will not be shown.
1321 * Vi also doesn't do this and the messages are not very informative.
1322 */
1323 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001324 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 FALSE, FALSE, FALSE, TRUE) == FAIL)
1326 {
1327 msg_putchar('\n'); /* keep message from buf_write() */
1328 --no_wait_return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001329#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (!aborting())
1331#endif
1332 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1333 goto filterend;
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (curbuf != old_curbuf)
1336 goto filterend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338 if (!do_out)
1339 msg_putchar('\n');
1340
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001341 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1343 if (cmd_buf == NULL)
1344 goto filterend;
1345
1346 windgoto((int)Rows - 1, 0);
1347 cursor_on();
1348
1349 /*
1350 * When not redirecting the output the command can write anything to the
1351 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1352 * stderr output of external command. Clear the screen later.
1353 * If do_in is FALSE, this could be something like ":r !cat", which may
1354 * also mess up the screen, clear it later.
1355 */
1356 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1357 redraw_later_clear();
1358
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001359 if (do_out)
1360 {
1361 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001362 {
1363 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001364 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001365 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001366 redraw_curbuf_later(VALID);
1367 }
1368 read_linecount = curbuf->b_ml.ml_line_count;
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /*
1371 * When call_shell() fails wait_return() is called to give the user a
1372 * chance to read the error messages. Otherwise errors are ignored, so you
1373 * can see the error messages from the command that appear on stdout; use
1374 * 'u' to fix the text
1375 * Switch to cooked mode when not redirecting stdin, avoids that something
1376 * like ":r !cat" hangs.
1377 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1378 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001379 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
1381 redraw_later_clear();
1382 wait_return(FALSE);
1383 }
1384 vim_free(cmd_buf);
1385
1386 did_check_timestamps = FALSE;
1387 need_check_timestamps = TRUE;
1388
1389 /* When interrupting the shell command, it may still have produced some
1390 * useful output. Reset got_int here, so that readfile() won't cancel
1391 * reading. */
1392 ui_breakcheck();
1393 got_int = FALSE;
1394
1395 if (do_out)
1396 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001397 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001399 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001400 eap, READ_FILTER) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001402#if defined(FEAT_EVAL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001403 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001405 {
1406 msg_putchar('\n');
1407 EMSG2(_(e_notread), otmp);
1408 }
1409 goto error;
1410 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001411 if (curbuf != old_curbuf)
1412 goto filterend;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001413 }
1414
1415 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1416
1417 if (shell_flags & SHELL_READ)
1418 {
1419 curbuf->b_op_start.lnum = line2 + 1;
1420 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1421 appended_lines_mark(line2, read_linecount);
1422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 if (do_in)
1425 {
1426 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (read_linecount >= linecount)
1429 /* move all marks from old lines to new lines */
1430 mark_adjust(line1, line2, linecount, 0L);
1431 else
1432 {
1433 /* move marks from old lines to new lines, delete marks
1434 * that are in deleted lines */
1435 mark_adjust(line1, line1 + read_linecount - 1,
1436 linecount, 0L);
1437 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1438 }
1439 }
1440
1441 /*
1442 * Put cursor on first filtered line for ":range!cmd".
1443 * Adjust '[ and '] (set by buf_write()).
1444 */
1445 curwin->w_cursor.lnum = line1;
1446 del_lines(linecount, TRUE);
1447 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1448 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1449 write_lnum_adjust(-linecount); /* adjust last line
1450 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001451#ifdef FEAT_FOLDING
1452 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455 else
1456 {
1457 /*
1458 * Put cursor on last new line for ":r !cmd".
1459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001461 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1465 --no_wait_return;
1466
1467 if (linecount > p_report)
1468 {
1469 if (do_in)
1470 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001471 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1472 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001475 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477 else
1478 msgmore((long)linecount);
1479 }
1480 }
1481 else
1482 {
1483error:
1484 /* put cursor back in same position for ":w !cmd" */
1485 curwin->w_cursor = cursor_save;
1486 --no_wait_return;
1487 wait_return(FALSE);
1488 }
1489
1490filterend:
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (curbuf != old_curbuf)
1493 {
1494 --no_wait_return;
1495 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (itmp != NULL)
1498 mch_remove(itmp);
1499 if (otmp != NULL)
1500 mch_remove(otmp);
1501 vim_free(itmp);
1502 vim_free(otmp);
1503}
1504
1505/*
1506 * Call a shell to execute a command.
1507 * When "cmd" is NULL start an interactive shell.
1508 */
1509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510do_shell(
1511 char_u *cmd,
1512 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 buf_T *buf;
1515#ifndef FEAT_GUI_MSWIN
1516 int save_nwr;
1517#endif
1518#ifdef MSWIN
1519 int winstart = FALSE;
1520#endif
1521
1522 /*
1523 * Disallow shell commands for "rvim".
1524 * Disallow shell commands from .exrc and .vimrc in current directory for
1525 * security reasons.
1526 */
1527 if (check_restricted() || check_secure())
1528 {
1529 msg_end();
1530 return;
1531 }
1532
1533#ifdef MSWIN
1534 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 * Check if ":!start" is used.
1536 */
1537 if (cmd != NULL)
1538 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1539#endif
1540
1541 /*
1542 * For autocommands we want to get the output on the current screen, to
1543 * avoid having to type return below.
1544 */
1545 msg_putchar('\r'); /* put cursor at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
1548#ifdef MSWIN
1549 if (!winstart)
1550#endif
1551 stoptermcap();
1552 }
1553#ifdef MSWIN
1554 if (!winstart)
1555#endif
1556 msg_putchar('\n'); /* may shift screen one line up */
1557
1558 /* warning message before calling the shell */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001559 if (p_warn && !autocmd_busy && msg_silent == 0)
Bram Moolenaar29323592016-07-24 22:04:11 +02001560 FOR_ALL_BUFFERS(buf)
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01001561 if (bufIsChangedNotTerm(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563#ifdef FEAT_GUI_MSWIN
1564 if (!winstart)
1565 starttermcap(); /* don't want a message box here */
1566#endif
1567 MSG_PUTS(_("[No write since last change]\n"));
1568#ifdef FEAT_GUI_MSWIN
1569 if (!winstart)
1570 stoptermcap();
1571#endif
1572 break;
1573 }
1574
1575 /* This windgoto is required for when the '\n' resulted in a "delete line
1576 * 1" command to the terminal. */
1577 if (!swapping_screen())
1578 windgoto(msg_row, msg_col);
1579 cursor_on();
1580 (void)call_shell(cmd, SHELL_COOKED | flags);
1581 did_check_timestamps = FALSE;
1582 need_check_timestamps = TRUE;
1583
1584 /*
1585 * put the message cursor at the end of the screen, avoids wait_return()
1586 * to overwrite the text that the external command showed
1587 */
1588 if (!swapping_screen())
1589 {
1590 msg_row = Rows - 1;
1591 msg_col = 0;
1592 }
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (autocmd_busy)
1595 {
1596 if (msg_silent == 0)
1597 redraw_later_clear();
1598 }
1599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 /*
1602 * For ":sh" there is no need to call wait_return(), just redraw.
1603 * Also for the Win32 GUI (the output is in a console window).
1604 * Otherwise there is probably text on the screen that the user wants
1605 * to read before redrawing, so call wait_return().
1606 */
1607#ifndef FEAT_GUI_MSWIN
1608 if (cmd == NULL
1609# ifdef WIN3264
1610 || (winstart && !need_wait_return)
1611# endif
1612 )
1613 {
1614 if (msg_silent == 0)
1615 redraw_later_clear();
1616 need_wait_return = FALSE;
1617 }
1618 else
1619 {
1620 /*
1621 * If we switch screens when starttermcap() is called, we really
1622 * want to wait for "hit return to continue".
1623 */
1624 save_nwr = no_wait_return;
1625 if (swapping_screen())
1626 no_wait_return = FALSE;
1627# ifdef AMIGA
1628 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1629# else
1630 wait_return(msg_silent == 0);
1631# endif
1632 no_wait_return = save_nwr;
1633 }
1634#endif /* FEAT_GUI_W32 */
1635
1636#ifdef MSWIN
1637 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1638#endif
1639 starttermcap(); /* start termcap if not done by wait_return() */
1640
1641 /*
1642 * In an Amiga window redrawing is caused by asking the window size.
1643 * If we got an interrupt this will not work. The chance that the
1644 * window size is wrong is very small, but we need to redraw the
1645 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1646 * but it saves an extra redraw.
1647 */
1648#ifdef AMIGA
1649 if (skip_redraw) /* ':' hit in wait_return() */
1650 {
1651 if (msg_silent == 0)
1652 redraw_later_clear();
1653 }
1654 else if (term_console)
1655 {
1656 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1657 if (got_int && msg_silent == 0)
1658 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1659 else
1660 must_redraw = 0; /* no extra redraw needed */
1661 }
1662#endif
1663 }
1664
1665 /* display any error messages now */
1666 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001667
Bram Moolenaarade00832006-03-10 21:46:58 +00001668 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669}
1670
1671/*
1672 * Create a shell command from a command string, input redirection file and
1673 * output redirection file.
1674 * Returns an allocated string with the shell command, or NULL for failure.
1675 */
1676 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001677make_filter_cmd(
1678 char_u *cmd, /* command */
1679 char_u *itmp, /* NULL or name of input file */
1680 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 char_u *buf;
1683 long_u len;
1684
Bram Moolenaar53076832015-12-31 19:53:21 +01001685#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001686 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001687 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001688
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001689 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001690 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1691 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001692 if (is_fish_shell)
1693 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1694 else
1695#endif
1696 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (itmp != NULL)
1698 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1699 if (otmp != NULL)
1700 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1701 buf = lalloc(len, TRUE);
1702 if (buf == NULL)
1703 return NULL;
1704
Bram Moolenaar53076832015-12-31 19:53:21 +01001705#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001707 * Put braces around the command (for concatenated commands) when
1708 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001710 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001711 {
1712 if (is_fish_shell)
1713 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1714 else
1715 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1716 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001717 else
1718 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (itmp != NULL)
1720 {
1721 STRCAT(buf, " < ");
1722 STRCAT(buf, itmp);
1723 }
1724#else
1725 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001726 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 * the use of commands in a pipe.
1728 */
1729 STRCPY(buf, cmd);
1730 if (itmp != NULL)
1731 {
1732 char_u *p;
1733
1734 /*
1735 * If there is a pipe, we have to put the '<' in front of it.
1736 * Don't do this when 'shellquote' is not empty, otherwise the
1737 * redirection would be inside the quotes.
1738 */
1739 if (*p_shq == NUL)
1740 {
1741 p = vim_strchr(buf, '|');
1742 if (p != NULL)
1743 *p = NUL;
1744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1746 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (*p_shq == NUL)
1748 {
1749 p = vim_strchr(cmd, '|');
1750 if (p != NULL)
1751 {
1752 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1753 STRCAT(buf, p);
1754 }
1755 }
1756 }
1757#endif
1758 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001759 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761 return buf;
1762}
1763
1764/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001765 * Append output redirection for file "fname" to the end of string buffer
1766 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 * Works with the 'shellredir' and 'shellpipe' options.
1768 * The caller should make sure that there is enough room:
1769 * STRLEN(opt) + STRLEN(fname) + 3
1770 */
1771 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001772append_redir(
1773 char_u *buf,
1774 int buflen,
1775 char_u *opt,
1776 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001779 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001781 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001782 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001784 {
1785 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001787 if (p[1] == '%') /* skip %% */
1788 ++p;
1789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (p != NULL)
1791 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001792 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1793 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1794 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
1796 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001797 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 " %s %s",
1800#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#endif
1803 (char *)opt, (char *)fname);
1804}
1805
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001806#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001808static int no_viminfo(void);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001809static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001810static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001811static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812static int viminfo_errcnt;
1813
1814 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001815no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 /* "vim -i NONE" does not read or write a viminfo file */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001818 return STRCMP(p_viminfofile, "NONE") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
1821/*
1822 * Report an error for reading a viminfo file.
1823 * Count the number of errors. When there are more than 10, return TRUE.
1824 */
1825 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001826viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001828 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1829 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001830 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001831 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1832 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 emsg(IObuff);
1834 if (++viminfo_errcnt >= 10)
1835 {
1836 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1837 return TRUE;
1838 }
1839 return FALSE;
1840}
1841
1842/*
1843 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001844 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001847read_viminfo(
1848 char_u *file, /* file name or NULL to use default name */
1849 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
1851 FILE *fp;
1852 char_u *fname;
1853
1854 if (no_viminfo())
1855 return FAIL;
1856
Bram Moolenaard812df62008-11-09 12:46:09 +00001857 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (fname == NULL)
1859 return FAIL;
1860 fp = mch_fopen((char *)fname, READBIN);
1861
1862 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001863 {
1864 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001865 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1866 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001867 (flags & VIF_WANT_INFO) ? _(" info") : "",
1868 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1869 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001870 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001871 verbose_leave();
1872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 vim_free(fname);
1875 if (fp == NULL)
1876 return FAIL;
1877
1878 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001879 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 return OK;
1883}
1884
1885/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001886 * Write the viminfo file. The old one is read in first so that effectively a
1887 * merge of current info and old info is done. This allows multiple vims to
1888 * run simultaneously, without losing any marks etc.
1889 * If "forceit" is TRUE, then the old file is not read in, and only internal
1890 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 */
1892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001893write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 char_u *fname;
1896 FILE *fp_in = NULL; /* input viminfo file, if any */
1897 FILE *fp_out = NULL; /* output viminfo file */
1898 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001899 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#if defined(UNIX) || defined(VMS)
1901 mode_t umask_save;
1902#endif
1903#ifdef UNIX
1904 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001905 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001907#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001908 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
1911 if (no_viminfo())
1912 return;
1913
1914 fname = viminfo_filename(file); /* may set to default if NULL */
1915 if (fname == NULL)
1916 return;
1917
1918 fp_in = mch_fopen((char *)fname, READBIN);
1919 if (fp_in == NULL)
1920 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001921 int fd;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 /* if it does exist, but we can't read it, don't try writing */
1924 if (mch_stat((char *)fname, &st_new) == 0)
1925 goto end;
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001926
1927 /* Create the new .viminfo non-accessible for others, because it may
1928 * contain text from non-accessible documents. It is up to the user to
1929 * widen access (e.g. to a group). This may also fail if there is a
1930 * race condition, then just give up. */
1931 fd = mch_open((char *)fname,
1932 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
1933 if (fd < 0)
1934 goto end;
1935 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 else
1938 {
1939 /*
1940 * There is an existing viminfo file. Create a temporary file to
1941 * write the new viminfo into, in the same directory as the
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001942 * existing viminfo file, which will be renamed once all writing is
1943 * successful.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 */
1945#ifdef UNIX
1946 /*
1947 * For Unix we check the owner of the file. It's not very nice to
1948 * overwrite a user's viminfo file after a "su root", with a
1949 * viminfo file that the user can't read.
1950 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001951 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001952 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001954 if (mch_stat((char *)fname, &st_old) == 0
1955 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001956 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 ? (st_old.st_mode & 0200)
1958 : (st_old.st_gid == getgid()
1959 ? (st_old.st_mode & 0020)
1960 : (st_old.st_mode & 0002))))
1961 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001962 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1966 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001967 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto end;
1969 }
1970#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001971#ifdef WIN3264
1972 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001973 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001977 * Make tempname, find one that does not exist yet.
1978 * Beware of a race condition: If someone logs out and all Vim
1979 * instances exit at the same time a temp file might be created between
1980 * stat() and open(). Use mch_open() with O_EXCL to avoid that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 * May try twice: Once normal and once with shortname set, just in
1982 * case somebody puts his viminfo file in an 8.3 filesystem.
1983 */
1984 for (;;)
1985 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01001986 int next_char = 'z';
1987 char_u *wp;
1988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 tempname = buf_modname(
1990#ifdef UNIX
1991 shortname,
1992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994#endif
1995 fname,
1996#ifdef VMS
1997 (char_u *)"-tmp",
1998#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#endif
2001 FALSE);
2002 if (tempname == NULL) /* out of memory */
2003 break;
2004
2005 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002006 * Try a series of names. Change one character, just before
2007 * the extension. This should also work for an 8.3
2008 * file name, when after adding the extension it still is
2009 * the same file as the original.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002011 wp = tempname + STRLEN(tempname) - 5;
2012 if (wp < gettail(tempname)) /* empty file name? */
2013 wp = gettail(tempname);
2014 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002016 /*
2017 * Check if tempfile already exists. Never overwrite an
2018 * existing file!
2019 */
2020 if (mch_stat((char *)tempname, &st_new) == 0)
2021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 /*
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002024 * Check if tempfile is same as original file. May happen
2025 * when modname() gave the same file back. E.g. silly
2026 * link, or file name-length reached. Try again with
2027 * shortname set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002029 if (!shortname && st_new.st_dev == st_old.st_dev
2030 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002032 VIM_CLEAR(tempname);
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002033 shortname = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 break;
2035 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002038 else
2039 {
2040 /* Try creating the file exclusively. This may fail if
2041 * another Vim tries to do it at the same time. */
2042#ifdef VMS
2043 /* fdopen() fails for some reason */
2044 umask_save = umask(077);
2045 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2046 (void)umask(umask_save);
2047#else
2048 int fd;
2049
2050 /* Use mch_open() to be able to use O_NOFOLLOW and set file
2051 * protection:
2052 * Unix: same as original file, but strip s-bit. Reset
2053 * umask to avoid it getting in the way.
2054 * Others: r&w for user only. */
2055# ifdef UNIX
2056 umask_save = umask(0);
2057 fd = mch_open((char *)tempname,
2058 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
2059 (int)((st_old.st_mode & 0777) | 0600));
2060 (void)umask(umask_save);
2061# else
2062 fd = mch_open((char *)tempname,
2063 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
2064# endif
2065 if (fd < 0)
2066 {
2067 fp_out = NULL;
2068# ifdef EEXIST
2069 /* Avoid trying lots of names while the problem is lack
2070 * of premission, only retry if the file already
2071 * exists. */
2072 if (errno != EEXIST)
2073 break;
2074# endif
2075 }
2076 else
2077 fp_out = fdopen(fd, WRITEBIN);
2078#endif /* VMS */
2079 if (fp_out != NULL)
2080 break;
2081 }
2082
2083 /* Assume file exists, try again with another name. */
2084 if (next_char == 'a' - 1)
2085 {
2086 /* They all exist? Must be something wrong! Don't write
2087 * the viminfo file then. */
2088 EMSG2(_("E929: Too many viminfo temp files, like %s!"),
2089 tempname);
2090 break;
2091 }
2092 *wp = next_char;
2093 --next_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002095
2096 if (tempname != NULL)
2097 break;
2098 /* continue if shortname was set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 }
2100
Bram Moolenaara5792f52005-11-23 21:25:05 +00002101#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002102 if (tempname != NULL && fp_out != NULL)
2103 {
2104 stat_T tmp_st;
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 /*
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002107 * Make sure the original owner can read/write the tempfile and
2108 * otherwise preserve permissions, making sure the group matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 */
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002110 if (mch_stat((char *)tempname, &tmp_st) >= 0)
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002111 {
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002112 if (st_old.st_uid != tmp_st.st_uid)
2113 /* Changing the owner might fail, in which case the
2114 * file will now owned by the current user, oh well. */
2115 ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
2116 if (st_old.st_gid != tmp_st.st_gid
2117 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
2118 /* can't set the group to what it should be, remove
2119 * group permissions */
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01002120 (void)mch_setperm(tempname, 0600);
2121 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002122 else
2123 /* can't stat the file, set conservative permissions */
2124 (void)mch_setperm(tempname, 0600);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
Bram Moolenaarc41838a2017-11-26 16:50:41 +01002126#endif
Bram Moolenaare0aa23f2017-11-26 17:08:03 +01002127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /*
2130 * Check if the new viminfo file can be written to.
2131 */
2132 if (fp_out == NULL)
2133 {
2134 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002135 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (fp_in != NULL)
2137 fclose(fp_in);
2138 goto end;
2139 }
2140
2141 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002142 {
2143 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002144 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002145 verbose_leave();
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002149 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar927030a2016-03-15 18:23:55 +01002151 if (fclose(fp_out) == EOF)
2152 ++viminfo_errcnt;
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (fp_in != NULL)
2155 {
2156 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002157
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002158 /* In case of an error keep the original viminfo file. Otherwise
2159 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002160 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002161 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002162 if (vim_rename(tempname, fname) == -1)
2163 {
2164 ++viminfo_errcnt;
2165 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2166 }
2167# ifdef WIN3264
2168 /* If the viminfo file was hidden then also hide the new file. */
2169 else if (hidden)
2170 mch_hide(fname);
2171# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002172 }
2173 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 mch_remove(tempname);
2175 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177end:
2178 vim_free(fname);
2179 vim_free(tempname);
2180}
2181
2182/*
2183 * Get the viminfo file name to use.
2184 * If "file" is given and not empty, use it (has already been expanded by
2185 * cmdline functions).
2186 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2187 * expand environment variables.
2188 * Returns an allocated string. NULL when out of memory.
2189 */
2190 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002191viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 if (file == NULL || *file == NUL)
2194 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002195 if (*p_viminfofile != NUL)
2196 file = p_viminfofile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2198 {
2199#ifdef VIMINFO_FILE2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200# ifdef VMS
2201 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2202# else
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002203# ifdef MSWIN
2204 /* Use $VIM only if $HOME is the default "C:/". */
2205 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
2206 && mch_getenv((char_u *)"HOME") == NULL)
2207# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (mch_getenv((char_u *)"HOME") == NULL)
Bram Moolenaar3d593c22017-08-31 20:42:18 +02002209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# endif
2211 {
2212 /* don't use $VIM when not available. */
2213 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2214 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2215 file = (char_u *)VIMINFO_FILE2;
2216 else
2217 file = (char_u *)VIMINFO_FILE;
2218 }
2219 else
2220#endif
2221 file = (char_u *)VIMINFO_FILE;
2222 }
2223 expand_env(file, NameBuff, MAXPATHL);
2224 file = NameBuff;
2225 }
2226 return vim_strsave(file);
2227}
2228
2229/*
2230 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2231 */
2232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002233do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 int eof = FALSE;
2236 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002237 int merge = FALSE;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002238 int do_copy_marks = FALSE;
2239 garray_T buflist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2242 return;
2243 vir.vir_fd = fp_in;
2244#ifdef FEAT_MBYTE
2245 vir.vir_conv.vc_type = CONV_NONE;
2246#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002247 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002248 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 if (fp_in != NULL)
2251 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002252 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002253 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002254 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002255 {
2256 /* Registers and marks are read and kept separate from what
2257 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002258 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002259 prepare_viminfo_marks();
2260 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002261
Bram Moolenaard812df62008-11-09 12:46:09 +00002262 eof = read_viminfo_up_to_marks(&vir,
2263 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002264 merge = TRUE;
2265 }
2266 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 /* Skip info, find start of marks */
2268 while (!(eof = viminfo_readline(&vir))
2269 && vir.vir_line[0] != '>')
2270 ;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002271
2272 do_copy_marks = (flags &
2273 (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (fp_out != NULL)
2277 {
2278 /* Write the info: */
2279 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2280 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002281 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002282 write_viminfo_version(fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002284 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2286#endif
2287 write_viminfo_search_pattern(fp_out);
2288 write_viminfo_sub_string(fp_out);
2289#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002290 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#endif
2292 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002293 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#ifdef FEAT_EVAL
2295 write_viminfo_varlist(fp_out);
2296#endif
2297 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002298 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002300 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002301
2302 if (do_copy_marks)
2303 ga_init2(&buflist, sizeof(buf_T *), 50);
2304 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002306
2307 if (do_copy_marks)
2308 {
2309 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2310 if (fp_out != NULL)
2311 ga_clear(&buflist);
2312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
2314 vim_free(vir.vir_line);
2315#ifdef FEAT_MBYTE
2316 if (vir.vir_conv.vc_type != CONV_NONE)
2317 convert_setup(&vir.vir_conv, NULL, NULL);
2318#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002319 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320}
2321
2322/*
2323 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2324 * first part of the viminfo file which contains everything but the marks that
2325 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2326 */
2327 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002328read_viminfo_up_to_marks(
2329 vir_T *virp,
2330 int forceit,
2331 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 int eof;
2334 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002335 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002338 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 eof = viminfo_readline(virp);
2342 while (!eof && virp->vir_line[0] != '>')
2343 {
2344 switch (virp->vir_line[0])
2345 {
2346 /* Characters reserved for future expansion, ignored now */
2347 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 case '^': /* to be defined */
2349 case '<': /* long line - ignored */
2350 /* A comment or empty line. */
2351 case NUL:
2352 case '\r':
2353 case '\n':
2354 case '#':
2355 eof = viminfo_readline(virp);
2356 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002357 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002358 eof = read_viminfo_barline(virp, got_encoding,
2359 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002362 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 eof = viminfo_encoding(virp);
2364 break;
2365 case '!': /* global variable */
2366#ifdef FEAT_EVAL
2367 eof = read_viminfo_varlist(virp, writing);
2368#else
2369 eof = viminfo_readline(virp);
2370#endif
2371 break;
2372 case '%': /* entry for buffer list */
2373 eof = read_viminfo_bufferlist(virp, writing);
2374 break;
2375 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002376 /* When registers are in bar lines skip the old style register
2377 * lines. */
2378 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2379 eof = read_viminfo_register(virp, forceit);
2380 else
2381 do {
2382 eof = viminfo_readline(virp);
2383 } while (!eof && (virp->vir_line[0] == TAB
2384 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 break;
2386 case '/': /* Search string */
2387 case '&': /* Substitute search string */
2388 case '~': /* Last search string, followed by '/' or '&' */
2389 eof = read_viminfo_search_pattern(virp, forceit);
2390 break;
2391 case '$':
2392 eof = read_viminfo_sub_string(virp, forceit);
2393 break;
2394 case ':':
2395 case '?':
2396 case '=':
2397 case '@':
2398#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002399 /* When history is in bar lines skip the old style history
2400 * lines. */
2401 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2402 eof = read_viminfo_history(virp, writing);
2403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002405 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 break;
2407 case '-':
2408 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002409 /* When file marks are in bar lines skip the old style lines. */
2410 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2411 eof = read_viminfo_filemark(virp, forceit);
2412 else
2413 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 break;
2415 default:
2416 if (viminfo_error("E575: ", _("Illegal starting char"),
2417 virp->vir_line))
2418 eof = TRUE;
2419 else
2420 eof = viminfo_readline(virp);
2421 break;
2422 }
2423 }
2424
2425#ifdef FEAT_CMDHIST
2426 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002427 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002428 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
2430
2431 /* Change file names to buffer numbers for fmarks. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002432 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 fmarks_check_names(buf);
2434
2435 return eof;
2436}
2437
2438/*
2439 * Compare the 'encoding' value in the viminfo file with the current value of
2440 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2441 * conversion of text with iconv() in viminfo_readstring().
2442 */
2443 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002444viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446#ifdef FEAT_MBYTE
2447 char_u *p;
2448 int i;
2449
2450 if (get_viminfo_parameter('c') != 0)
2451 {
2452 p = vim_strchr(virp->vir_line, '=');
2453 if (p != NULL)
2454 {
2455 /* remove trailing newline */
2456 ++p;
2457 for (i = 0; vim_isprintc(p[i]); ++i)
2458 ;
2459 p[i] = NUL;
2460
2461 convert_setup(&virp->vir_conv, p, p_enc);
2462 }
2463 }
2464#endif
2465 return viminfo_readline(virp);
2466}
2467
2468/*
2469 * Read a line from the viminfo file.
2470 * Returns TRUE for end-of-file;
2471 */
2472 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002473viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2476}
2477
2478/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002479 * Check string read from viminfo file.
2480 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 * - replace CTRL-V CTRL-V with CTRL-V
2482 * - replace CTRL-V 'n' with '\n'
2483 *
2484 * Check for a long line as written by viminfo_writestring().
2485 *
2486 * Return the string in allocated memory (NULL when out of memory).
2487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002489viminfo_readstring(
2490 vir_T *virp,
2491 int off, /* offset for virp->vir_line */
2492 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 char_u *retval;
2495 char_u *s, *d;
2496 long len;
2497
2498 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2499 {
2500 len = atol((char *)virp->vir_line + off + 1);
2501 retval = lalloc(len, TRUE);
2502 if (retval == NULL)
2503 {
2504 /* Line too long? File messed up? Skip next line. */
2505 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2506 return NULL;
2507 }
2508 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2509 s = retval + 1; /* Skip the leading '<' */
2510 }
2511 else
2512 {
2513 retval = vim_strsave(virp->vir_line + off);
2514 if (retval == NULL)
2515 return NULL;
2516 s = retval;
2517 }
2518
2519 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2520 d = retval;
2521 while (*s != NUL && *s != '\n')
2522 {
2523 if (s[0] == Ctrl_V && s[1] != NUL)
2524 {
2525 if (s[1] == 'n')
2526 *d++ = '\n';
2527 else
2528 *d++ = Ctrl_V;
2529 s += 2;
2530 }
2531 else
2532 *d++ = *s++;
2533 }
2534 *d = NUL;
2535
2536#ifdef FEAT_MBYTE
2537 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2538 {
2539 d = string_convert(&virp->vir_conv, retval, NULL);
2540 if (d != NULL)
2541 {
2542 vim_free(retval);
2543 retval = d;
2544 }
2545 }
2546#endif
2547
2548 return retval;
2549}
2550
2551/*
2552 * write string to viminfo file
2553 * - replace CTRL-V with CTRL-V CTRL-V
2554 * - replace '\n' with CTRL-V 'n'
2555 * - add a '\n' at the end
2556 *
2557 * For a long line:
2558 * - write " CTRL-V <length> \n " in first line
2559 * - write " < <string> \n " in second line
2560 */
2561 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002562viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int c;
2565 char_u *s;
2566 int len = 0;
2567
2568 for (s = p; *s != NUL; ++s)
2569 {
2570 if (*s == Ctrl_V || *s == '\n')
2571 ++len;
2572 ++len;
2573 }
2574
2575 /* If the string will be too long, write its length and put it in the next
2576 * line. Take into account that some room is needed for what comes before
2577 * the string (e.g., variable name). Add something to the length for the
2578 * '<', NL and trailing NUL. */
2579 if (len > LSIZE / 2)
2580 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2581
2582 while ((c = *p++) != NUL)
2583 {
2584 if (c == Ctrl_V || c == '\n')
2585 {
2586 putc(Ctrl_V, fd);
2587 if (c == '\n')
2588 c = 'n';
2589 }
2590 putc(c, fd);
2591 }
2592 putc('\n', fd);
2593}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002594
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002595/*
2596 * Write a string in quotes that barline_parse() can read back.
2597 * Breaks the line in less than LSIZE pieces when needed.
2598 * Returns remaining characters in the line.
2599 */
2600 int
2601barline_writestring(FILE *fd, char_u *s, int remaining_start)
2602{
2603 char_u *p;
2604 int remaining = remaining_start;
2605 int len = 2;
2606
2607 /* Count the number of characters produced, including quotes. */
2608 for (p = s; *p != NUL; ++p)
2609 {
2610 if (*p == NL)
2611 len += 2;
2612 else if (*p == '"' || *p == '\\')
2613 len += 2;
2614 else
2615 ++len;
2616 }
Bram Moolenaar25709572016-08-26 20:41:16 +02002617 if (len > remaining - 2)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002618 {
2619 fprintf(fd, ">%d\n|<", len);
2620 remaining = LSIZE - 20;
2621 }
2622
2623 putc('"', fd);
2624 for (p = s; *p != NUL; ++p)
2625 {
2626 if (*p == NL)
2627 {
2628 putc('\\', fd);
2629 putc('n', fd);
2630 --remaining;
2631 }
2632 else if (*p == '"' || *p == '\\')
2633 {
2634 putc('\\', fd);
2635 putc(*p, fd);
2636 --remaining;
2637 }
2638 else
2639 putc(*p, fd);
2640 --remaining;
2641
2642 if (remaining < 3)
2643 {
2644 putc('\n', fd);
2645 putc('|', fd);
2646 putc('<', fd);
2647 /* Leave enough space for another continuation. */
2648 remaining = LSIZE - 20;
2649 }
2650 }
2651 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002652 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002653}
2654
2655/*
2656 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002657 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002658 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002659 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002660 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002661barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002662{
2663 char_u *p = text;
2664 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002665 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002666 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002667 int i;
2668 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002669 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002670#ifdef FEAT_MBYTE
2671 char_u *sconv;
2672 int converted;
2673#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002674
2675 while (*p == ',')
2676 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002677 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002678 if (ga_grow(values, 1) == FAIL)
2679 break;
2680 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002681
2682 if (*p == '>')
2683 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002684 /* Need to read a continuation line. Put strings in allocated
2685 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002686 if (!allocated)
2687 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002688 for (i = 0; i < values->ga_len; ++i)
2689 {
2690 bval_T *vp = (bval_T *)(values->ga_data) + i;
2691
2692 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002693 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002694 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2695 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002696 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002697 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002698 allocated = TRUE;
2699 }
2700
2701 if (vim_isdigit(p[1]))
2702 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002703 size_t len;
2704 size_t todo;
2705 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002706
2707 /* String value was split into lines that are each shorter
2708 * than LSIZE:
2709 * |{bartype},>{length of "{text}{text2}"}
2710 * |<"{text1}
2711 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002712 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002713 */
2714 ++p;
2715 len = getdigits(&p);
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002716 buf = alloc((int)(len + 1));
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002717 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002718 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002719 p = buf;
2720 for (todo = len; todo > 0; todo -= n)
2721 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002722 eof = viminfo_readline(virp);
2723 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002724 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002725 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002726 /* File was truncated or garbled. Read another line if
2727 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002728 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002729 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002730 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002731 /* Get length of text, excluding |< and NL chars. */
2732 n = STRLEN(virp->vir_line);
2733 while (n > 0 && (virp->vir_line[n - 1] == NL
2734 || virp->vir_line[n - 1] == CAR))
2735 --n;
2736 n -= 2;
2737 if (n > todo)
2738 {
2739 /* more values follow after the string */
2740 nextp = virp->vir_line + 2 + todo;
2741 n = todo;
2742 }
2743 mch_memmove(p, virp->vir_line + 2, n);
2744 p += n;
2745 }
2746 *p = NUL;
2747 p = buf;
2748 }
2749 else
2750 {
2751 /* Line ending in ">" continues in the next line:
2752 * |{bartype},{lots of values},>
2753 * |<{value},{value}
2754 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002755 eof = viminfo_readline(virp);
2756 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002757 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002758 /* File was truncated or garbled. Read another line if
2759 * this one starts with '|'. */
2760 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002761 p = virp->vir_line + 2;
2762 }
2763 }
2764
2765 if (isdigit(*p))
2766 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002767 value->bv_type = BVAL_NR;
2768 value->bv_nr = getdigits(&p);
2769 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002770 }
2771 else if (*p == '"')
2772 {
2773 int len = 0;
2774 char_u *s = p;
2775
2776 /* Unescape special characters in-place. */
2777 ++p;
2778 while (*p != '"')
2779 {
2780 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002781 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002782 if (*p == '\\')
2783 {
2784 ++p;
2785 if (*p == 'n')
2786 s[len++] = '\n';
2787 else
2788 s[len++] = *p;
2789 ++p;
2790 }
2791 else
2792 s[len++] = *p++;
2793 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002794 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002795 s[len] = NUL;
2796
Bram Moolenaar01227092016-06-11 14:47:40 +02002797#ifdef FEAT_MBYTE
2798 converted = FALSE;
2799 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2800 {
2801 sconv = string_convert(&virp->vir_conv, s, NULL);
2802 if (sconv != NULL)
2803 {
2804 if (s == buf)
2805 vim_free(s);
2806 s = sconv;
2807 buf = s;
2808 converted = TRUE;
2809 }
2810 }
2811#endif
2812 /* Need to copy in allocated memory if the string wasn't allocated
2813 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002814 if (s != buf && allocated)
2815 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002816 value->bv_string = s;
2817 value->bv_type = BVAL_STRING;
2818 value->bv_len = len;
2819 value->bv_allocated = allocated
Bram Moolenaar01227092016-06-11 14:47:40 +02002820#ifdef FEAT_MBYTE
2821 || converted
2822#endif
2823 ;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002824 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002825 if (nextp != NULL)
2826 {
2827 /* values following a long string */
2828 p = nextp;
2829 nextp = NULL;
2830 }
2831 }
2832 else if (*p == ',')
2833 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002834 value->bv_type = BVAL_EMPTY;
2835 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002836 }
2837 else
2838 break;
2839 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002840 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002841}
2842
2843 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002844read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002845{
2846 char_u *p = virp->vir_line + 1;
2847 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002848 garray_T values;
2849 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002850 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002851 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002852
2853 /* The format is: |{bartype},{value},...
2854 * For a very long string:
2855 * |{bartype},>{length of "{text}{text2}"}
2856 * |<{text1}
2857 * |<{text2},{value}
2858 * For a long line not using a string
2859 * |{bartype},{lots of values},>
2860 * |<{value},{value}
2861 */
2862 if (*p == '<')
2863 {
2864 /* Continuation line of an unrecognized item. */
2865 if (writing)
2866 ga_add_string(&virp->vir_barlines, virp->vir_line);
2867 }
2868 else
2869 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002870 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002871 bartype = getdigits(&p);
2872 switch (bartype)
2873 {
2874 case BARTYPE_VERSION:
2875 /* Only use the version when it comes before the encoding.
2876 * If it comes later it was copied by a Vim version that
2877 * doesn't understand the version. */
2878 if (!got_encoding)
2879 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002880 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002881 vp = (bval_T *)values.ga_data;
2882 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2883 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002884 }
2885 break;
2886
2887 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002888 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002889 handle_viminfo_history(&values, writing);
2890 break;
2891
2892 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002893 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002894 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002895 break;
2896
Bram Moolenaar2d358992016-06-12 21:20:54 +02002897 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002898 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002899 handle_viminfo_mark(&values, force);
2900 break;
2901
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002902 default:
2903 /* copy unrecognized line (for future use) */
2904 if (writing)
2905 ga_add_string(&virp->vir_barlines, virp->vir_line);
2906 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002907 for (i = 0; i < values.ga_len; ++i)
2908 {
2909 vp = (bval_T *)values.ga_data + i;
2910 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2911 vim_free(vp->bv_string);
2912 }
2913 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002914 }
2915
Bram Moolenaarecefe712016-06-20 12:50:17 +02002916 if (read_next)
2917 return viminfo_readline(virp);
2918 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002919}
2920
2921 static void
2922write_viminfo_version(FILE *fp_out)
2923{
2924 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2925 BARTYPE_VERSION, VIMINFO_VERSION);
2926}
2927
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002928 static void
2929write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2930{
2931 int i;
2932 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002933 int seen_useful = FALSE;
2934 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002935
2936 if (gap->ga_len > 0)
2937 {
2938 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2939
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002940 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002941 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002942 {
2943 line = ((char **)(gap->ga_data))[i];
2944 if (seen_useful || line[1] != '<')
2945 {
2946 fputs(line, fp_out);
2947 seen_useful = TRUE;
2948 }
2949 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002950 }
2951}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952#endif /* FEAT_VIMINFO */
2953
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002954/*
2955 * Return the current time in seconds. Calls time(), unless test_settime()
2956 * was used.
2957 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002958 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002959vim_time(void)
2960{
2961# ifdef FEAT_EVAL
2962 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2963# else
2964 return time(NULL);
2965# endif
2966}
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968/*
2969 * Implementation of ":fixdel", also used by get_stty().
2970 * <BS> resulting <Del>
2971 * ^? ^H
2972 * not ^? ^?
2973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002975do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 char_u *p;
2978
2979 p = find_termcode((char_u *)"kb");
2980 add_termcode((char_u *)"kD", p != NULL
2981 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2982}
2983
2984 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002985print_line_no_prefix(
2986 linenr_T lnum,
2987 int use_number,
2988 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002990 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992 if (curwin->w_p_nu || use_number)
2993 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002994 vim_snprintf((char *)numbuf, sizeof(numbuf),
2995 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002996 msg_puts_attr(numbuf, HL_ATTR(HLF_N)); /* Highlight line nrs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002998 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000
3001/*
3002 * Print a text line. Also in silent mode ("ex -s").
3003 */
3004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003005print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 int save_silent = silent_mode;
3008
Bram Moolenaard29459b2016-08-26 22:29:11 +02003009 /* apply :filter /pat/ */
3010 if (message_filtered(ml_get(lnum)))
3011 return;
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003014 silent_mode = FALSE;
3015 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
3016 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (save_silent)
3018 {
3019 msg_putchar('\n');
3020 cursor_on(); /* msg_start() switches it off */
3021 out_flush();
3022 silent_mode = save_silent;
3023 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00003024 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003028rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003029{
3030 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02003031 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003032
Bram Moolenaar7e283842013-05-30 11:43:15 +02003033 buf = curbuf;
3034 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003035 /* buffer changed, don't change name now */
3036 if (buf != curbuf)
3037 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003038#ifdef FEAT_EVAL
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003039 if (aborting()) /* autocmds may abort script processing */
3040 return FAIL;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003041#endif
3042 /*
3043 * The name of the current buffer will be changed.
3044 * A new (unlisted) buffer entry needs to be made to hold the old file
3045 * name, which will become the alternate file name.
3046 * But don't set the alternate file name if the buffer didn't have a
3047 * name.
3048 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02003049 fname = curbuf->b_ffname;
3050 sfname = curbuf->b_sfname;
3051 xfname = curbuf->b_fname;
3052 curbuf->b_ffname = NULL;
3053 curbuf->b_sfname = NULL;
3054 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003055 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02003056 curbuf->b_ffname = fname;
3057 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003058 return FAIL;
3059 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02003060 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003061 if (xfname != NULL && *xfname != NUL)
3062 {
3063 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
3064 if (buf != NULL && !cmdmod.keepalt)
3065 curwin->w_alt_fnum = buf->b_fnum;
3066 }
3067 vim_free(fname);
3068 vim_free(sfname);
Bram Moolenaar7e283842013-05-30 11:43:15 +02003069 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003070
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003071 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02003072 DO_AUTOCHDIR;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003073 return OK;
3074}
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076/*
3077 * ":file[!] [fname]".
3078 */
3079 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003080ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003082 /* ":0file" removes the file name. Check for illegal uses ":3file",
3083 * "0file name", etc. */
3084 if (eap->addr_count > 0
3085 && (*eap->arg != NUL
3086 || eap->line2 > 0
3087 || eap->addr_count > 1))
3088 {
3089 EMSG(_(e_invarg));
3090 return;
3091 }
3092
3093 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003095 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 return;
Bram Moolenaarfc089602018-06-24 16:53:35 +02003097 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaarfc089602018-06-24 16:53:35 +02003099
3100 // print file name if no argument or 'F' is not in 'shortmess'
3101 if (*eap->arg == NUL || !shortmess(SHM_FILEINFO))
Bram Moolenaar426dd022016-03-15 15:09:29 +01003102 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105/*
3106 * ":update".
3107 */
3108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 if (curbufIsChanged())
3112 (void)do_write(eap);
3113}
3114
3115/*
3116 * ":write" and ":saveas".
3117 */
3118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003119ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 if (eap->usefilter) /* input lines to shell command */
3122 do_bang(1, eap, FALSE, TRUE, FALSE);
3123 else
3124 (void)do_write(eap);
3125}
3126
3127/*
3128 * write current buffer to file 'eap->arg'
3129 * if 'eap->append' is TRUE, append to the file
3130 *
3131 * if *eap->arg == NUL write to current file
3132 *
3133 * return FAIL for failure, OK otherwise
3134 */
3135 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 int other;
3139 char_u *fname = NULL; /* init to shut up gcc */
3140 char_u *ffname;
3141 int retval = FAIL;
3142 char_u *free_fname = NULL;
3143#ifdef FEAT_BROWSE
3144 char_u *browse_file = NULL;
3145#endif
3146 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003147 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 if (not_writing()) /* check 'write' option */
3150 return FAIL;
3151
3152 ffname = eap->arg;
3153#ifdef FEAT_BROWSE
3154 if (cmdmod.browse)
3155 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003156 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 NULL, NULL, NULL, curbuf);
3158 if (browse_file == NULL)
3159 goto theend;
3160 ffname = browse_file;
3161 }
3162#endif
3163 if (*ffname == NUL)
3164 {
3165 if (eap->cmdidx == CMD_saveas)
3166 {
3167 EMSG(_(e_argreq));
3168 goto theend;
3169 }
3170 other = FALSE;
3171 }
3172 else
3173 {
3174 fname = ffname;
3175 free_fname = fix_fname(ffname);
3176 /*
3177 * When out-of-memory, keep unexpanded file name, because we MUST be
3178 * able to write the file in this situation.
3179 */
3180 if (free_fname != NULL)
3181 ffname = free_fname;
3182 other = otherfile(ffname);
3183 }
3184
3185 /*
3186 * If we have a new file, put its name in the list of alternate file names.
3187 */
3188 if (other)
3189 {
3190 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3191 || eap->cmdidx == CMD_saveas)
3192 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3193 else
3194 alt_buf = buflist_findname(ffname);
3195 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3196 {
3197 /* Overwriting a file that is loaded in another buffer is not a
3198 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00003199 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 goto theend;
3201 }
3202 }
3203
3204 /*
3205 * Writing to the current file is not allowed in readonly mode
3206 * and a file name is required.
3207 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3208 */
3209 if (!other && (
3210#ifdef FEAT_QUICKFIX
3211 bt_dontwrite_msg(curbuf) ||
3212#endif
3213 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3214 goto theend;
3215
3216 if (!other)
3217 {
3218 ffname = curbuf->b_ffname;
3219 fname = curbuf->b_fname;
3220 /*
3221 * Not writing the whole file is only allowed with '!'.
3222 */
3223 if ( (eap->line1 != 1
3224 || eap->line2 != curbuf->b_ml.ml_line_count)
3225 && !eap->forceit
3226 && !eap->append
3227 && !p_wa)
3228 {
3229#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3230 if (p_confirm || cmdmod.confirm)
3231 {
3232 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3233 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3234 goto theend;
3235 eap->forceit = TRUE;
3236 }
3237 else
3238#endif
3239 {
3240 EMSG(_("E140: Use ! to write partial buffer"));
3241 goto theend;
3242 }
3243 }
3244 }
3245
3246 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3247 {
3248 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 buf_T *was_curbuf = curbuf;
3251
3252 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003253 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003254#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003256#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
3260 /* buffer changed, don't change name now */
3261 retval = FAIL;
3262 goto theend;
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 /* Exchange the file names for the current and the alternate
3265 * buffer. This makes it look like we are now editing the buffer
3266 * under the new name. Must be done before buf_write(), because
3267 * if there is no file name and 'cpo' contains 'F', it will set
3268 * the file name. */
3269 fname = alt_buf->b_fname;
3270 alt_buf->b_fname = curbuf->b_fname;
3271 curbuf->b_fname = fname;
3272 fname = alt_buf->b_ffname;
3273 alt_buf->b_ffname = curbuf->b_ffname;
3274 curbuf->b_ffname = fname;
3275 fname = alt_buf->b_sfname;
3276 alt_buf->b_sfname = curbuf->b_sfname;
3277 curbuf->b_sfname = fname;
3278 buf_name_changed(curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003281 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (!alt_buf->b_p_bl)
3283 {
3284 alt_buf->b_p_bl = TRUE;
3285 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3286 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003287#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003289#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 /* buffer changed, don't write the file */
3294 retval = FAIL;
3295 goto theend;
3296 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003297
3298 /* If 'filetype' was empty try detecting it now. */
3299 if (*curbuf->b_p_ft == NUL)
3300 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003301 if (au_has_group((char_u *)"filetypedetect"))
3302 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003303 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003304 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003305 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003306
3307 /* Autocommands may have changed buffer names, esp. when
3308 * 'autochdir' is set. */
3309 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311
Bram Moolenaar5c719942016-07-09 23:40:45 +02003312 name_was_missing = curbuf->b_ffname == NULL;
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3315 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003316
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003317 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003318 if (eap->cmdidx == CMD_saveas)
3319 {
3320 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003321 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003322 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003323 redraw_tabline = TRUE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003324 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003325 }
3326
3327 /* Change directories when the 'acd' option is set and the file name
3328 * got changed or set. */
3329 if (eap->cmdidx == CMD_saveas || name_was_missing)
3330 {
Bram Moolenaar6f470022018-04-10 18:47:20 +02003331 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334
3335theend:
3336#ifdef FEAT_BROWSE
3337 vim_free(browse_file);
3338#endif
3339 vim_free(free_fname);
3340 return retval;
3341}
3342
3343/*
3344 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3345 * BF_NEW or BF_READERR, check for overwriting current file.
3346 * May set eap->forceit if a dialog says it's OK to overwrite.
3347 * Return OK if it's OK, FAIL if it is not.
3348 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003349 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003350check_overwrite(
3351 exarg_T *eap,
3352 buf_T *buf,
3353 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003355 char_u *ffname, /* full path version of fname */
3356 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 /*
3359 * write to other file or b_flags set or not writing the whole file:
3360 * overwriting only allowed with '!'
3361 */
3362 if ( (other
3363 || (buf->b_flags & BF_NOTEDITED)
3364 || ((buf->b_flags & BF_NEW)
3365 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3366 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003368#ifdef FEAT_QUICKFIX
3369 && !bt_nofile(buf)
3370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 && vim_fexists(ffname))
3372 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003373 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003375#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003376 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003377 if (mch_isdir(ffname))
3378 {
3379 EMSG2(_(e_isadir2), ffname);
3380 return FAIL;
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382#endif
3383#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003384 if (p_confirm || cmdmod.confirm)
3385 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003386 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003388 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3389 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3390 return FAIL;
3391 eap->forceit = TRUE;
3392 }
3393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003395 {
3396 EMSG(_(e_exists));
3397 return FAIL;
3398 }
3399 }
3400
3401 /* For ":w! filename" check that no swap file exists for "filename". */
3402 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003404 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003405 char_u *p;
3406 int r;
3407 char_u *swapname;
3408
3409 /* We only try the first entry in 'directory', without checking if
3410 * it's writable. If the "." directory is not writable the write
3411 * will probably fail anyway.
3412 * Use 'shortname' of the current buffer, since there is no buffer
3413 * for the written file. */
3414 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003415 {
3416 dir = alloc(5);
3417 if (dir == NULL)
3418 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003419 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003420 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003421 else
3422 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003423 dir = alloc(MAXPATHL);
3424 if (dir == NULL)
3425 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003426 p = p_dir;
3427 copy_option_part(&p, dir, MAXPATHL, ",");
3428 }
3429 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003430 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003431 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003432 if (r)
3433 {
3434#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3435 if (p_confirm || cmdmod.confirm)
3436 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003437 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003438
3439 dialog_msg(buff,
3440 _("Swap file \"%s\" exists, overwrite anyway?"),
3441 swapname);
3442 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3443 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003444 {
3445 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003446 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003447 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003448 eap->forceit = TRUE;
3449 }
3450 else
3451#endif
3452 {
3453 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
3454 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003455 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003456 return FAIL;
3457 }
3458 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003459 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461 }
3462 return OK;
3463}
3464
3465/*
3466 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3467 */
3468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003469ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 int i;
3472
3473 if (eap->cmd[1] == 'n')
3474 i = curwin->w_arg_idx + (int)eap->line2;
3475 else
3476 i = curwin->w_arg_idx - (int)eap->line2;
3477 eap->line1 = 1;
3478 eap->line2 = curbuf->b_ml.ml_line_count;
3479 if (do_write(eap) != FAIL)
3480 do_argfile(eap, i);
3481}
3482
3483/*
3484 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3485 */
3486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003487do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 buf_T *buf;
3490 int error = 0;
3491 int save_forceit = eap->forceit;
3492
3493 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3494 exiting = TRUE;
3495
Bram Moolenaar29323592016-07-24 22:04:11 +02003496 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
Bram Moolenaar7a760922018-02-19 23:10:02 +01003498#ifdef FEAT_TERMINAL
3499 if (exiting && term_job_running(buf->b_term))
3500 {
3501 no_write_message_nobang(buf);
3502 ++error;
3503 }
3504 else
3505#endif
Bram Moolenaar059db5c2017-10-15 22:42:23 +02003506 if (bufIsChanged(buf) && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 /*
3509 * Check if there is a reason the buffer cannot be written:
3510 * 1. if the 'write' option is set
3511 * 2. if there is no file name (even after browsing)
3512 * 3. if the 'readonly' is set (even after a dialog)
3513 * 4. if overwriting is allowed (even after a dialog)
3514 */
3515 if (not_writing())
3516 {
3517 ++error;
3518 break;
3519 }
3520#ifdef FEAT_BROWSE
3521 /* ":browse wall": ask for file name if there isn't one */
3522 if (buf->b_ffname == NULL && cmdmod.browse)
3523 browse_save_fname(buf);
3524#endif
3525 if (buf->b_ffname == NULL)
3526 {
3527 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3528 ++error;
3529 }
3530 else if (check_readonly(&eap->forceit, buf)
3531 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3532 FALSE) == FAIL)
3533 {
3534 ++error;
3535 }
3536 else
3537 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003538 bufref_T bufref;
3539
3540 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (buf_write_all(buf, eap->forceit) == FAIL)
3542 ++error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003544 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547 eap->forceit = save_forceit; /* check_overwrite() may set it */
3548 }
3549 }
3550 if (exiting)
3551 {
3552 if (!error)
3553 getout(0); /* exit Vim */
3554 not_exiting();
3555 }
3556}
3557
3558/*
3559 * Check the 'write' option.
3560 * Return TRUE and give a message when it's not st.
3561 */
3562 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003563not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
3565 if (p_write)
3566 return FALSE;
3567 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3568 return TRUE;
3569}
3570
3571/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003572 * Check if a buffer is read-only (either 'readonly' option is set or file is
3573 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3574 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 */
3576 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003577check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003579 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003580
3581 /* Handle a file being readonly when the 'readonly' option is set or when
3582 * the file exists and permissions are read-only.
3583 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3584 * important for device checks but not here. */
3585 if (!*forceit && (buf->b_p_ro
3586 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3587 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3590 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3591 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003592 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar5386a122007-06-28 20:02:32 +00003594 if (buf->b_p_ro)
3595 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3596 buf->b_fname);
3597 else
3598 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 +00003599 buf->b_fname);
3600
3601 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3602 {
3603 /* Set forceit, to force the writing of a readonly file */
3604 *forceit = TRUE;
3605 return FALSE;
3606 }
3607 else
3608 return TRUE;
3609 }
3610 else
3611#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003612 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003614 else
3615 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3616 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return TRUE;
3618 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 return FALSE;
3621}
3622
3623/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003624 * Try to abandon current file and edit a new or existing file.
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003625 * "fnum" is the number of the file, if zero use ffname/sfname.
3626 * "lnum" is the line number for the cursor in the new file (if non-zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 *
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003628 * Return:
3629 * GETFILE_ERROR for "normal" error,
3630 * GETFILE_NOT_WRITTEN for "not written" error,
3631 * GETFILE_SAME_FILE for success
3632 * GETFILE_OPEN_OTHER for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
3634 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003635getfile(
3636 int fnum,
3637 char_u *ffname,
3638 char_u *sfname,
3639 int setpm,
3640 linenr_T lnum,
3641 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 int other;
3644 int retval;
3645 char_u *free_me = NULL;
3646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003647 if (text_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003648 return GETFILE_ERROR;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003649 if (curbuf_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003650 return GETFILE_ERROR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 if (fnum == 0)
3653 {
3654 /* make ffname full path, set sfname */
3655 fname_expand(curbuf, &ffname, &sfname);
3656 other = otherfile(ffname);
3657 free_me = ffname; /* has been allocated, free() later */
3658 }
3659 else
3660 other = (fnum != curbuf->b_fnum);
3661
3662 if (other)
3663 ++no_wait_return; /* don't wait for autowrite message */
Bram Moolenaareb44a682017-08-03 22:44:55 +02003664 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3666 {
3667#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3668 if (p_confirm && p_write)
3669 dialog_changed(curbuf, FALSE);
3670 if (curbufIsChanged())
3671#endif
3672 {
3673 if (other)
3674 --no_wait_return;
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02003675 no_write_message();
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003676 retval = GETFILE_NOT_WRITTEN; /* file has been changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 goto theend;
3678 }
3679 }
3680 if (other)
3681 --no_wait_return;
3682 if (setpm)
3683 setpcmark();
3684 if (!other)
3685 {
3686 if (lnum != 0)
3687 curwin->w_cursor.lnum = lnum;
3688 check_cursor_lnum();
3689 beginline(BL_SOL | BL_FIX);
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003690 retval = GETFILE_SAME_FILE; /* it's in the same file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02003693 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003694 curwin) == OK)
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003695 retval = GETFILE_OPEN_OTHER; /* opened another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 else
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003697 retval = GETFILE_ERROR; /* error encountered */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
3699theend:
3700 vim_free(free_me);
3701 return retval;
3702}
3703
3704/*
3705 * start editing a new file
3706 *
3707 * fnum: file number; if zero use ffname/sfname
3708 * ffname: the file name
3709 * - full path if sfname used,
3710 * - any file name if sfname is NULL
3711 * - empty string to re-edit with the same file name (but may be
3712 * in a different directory)
3713 * - NULL to start an empty buffer
3714 * sfname: the short file name (or NULL)
3715 * eap: contains the command to be executed after loading the file and
3716 * forced 'ff' and 'fenc'
3717 * newlnum: if > 0: put cursor on this line number (if possible)
3718 * if ECMD_LASTL: use last position in loaded file
3719 * if ECMD_LAST: use last position in all files
3720 * if ECMD_ONE: use first line
3721 * flags:
3722 * ECMD_HIDE: if TRUE don't free the current buffer
3723 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3724 * ECMD_OLDBUF: use existing buffer if it exists
3725 * ECMD_FORCEIT: ! used for Ex command
3726 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003727 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003728 * window, NULL when splitting the window first. When not NULL info
3729 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 *
3731 * return FAIL for failure, OK otherwise
3732 */
3733 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003734do_ecmd(
3735 int fnum,
3736 char_u *ffname,
3737 char_u *sfname,
3738 exarg_T *eap, /* can be NULL! */
3739 linenr_T newlnum,
3740 int flags,
3741 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 int other_file; /* TRUE if editing another file */
3744 int oldbuf; /* TRUE if using existing buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 int auto_buf = FALSE; /* TRUE if autocommands brought us
3746 into the buffer unexpectedly */
3747 char_u *new_name = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003748#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003749 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#endif
3751 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003752 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003753 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 char_u *free_fname = NULL;
3755#ifdef FEAT_BROWSE
3756 char_u *browse_file = NULL;
3757#endif
3758 int retval = FAIL;
3759 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003760 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 linenr_T topline = 0;
3762 int newcol = -1;
3763 int solcol = -1;
3764 pos_T *pos;
3765#ifdef FEAT_SUN_WORKSHOP
3766 char_u *cp;
3767#endif
3768 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003769#ifdef FEAT_SPELL
3770 int did_get_winopts = FALSE;
3771#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003772 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003773 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 if (eap != NULL)
3776 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003777 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 if (fnum != 0)
3780 {
3781 if (fnum == curbuf->b_fnum) /* file is already being edited */
3782 return OK; /* nothing to do */
3783 other_file = TRUE;
3784 }
3785 else
3786 {
3787#ifdef FEAT_BROWSE
3788 if (cmdmod.browse)
3789 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003790 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003791# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003792 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003793# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003794 au_has_group((char_u *)"FileExplorer"))
3795 {
3796 /* No browsing supported but we do have the file explorer:
3797 * Edit the directory. */
3798 if (ffname == NULL || !mch_isdir(ffname))
3799 ffname = (char_u *)".";
3800 }
3801 else
3802 {
3803 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003805 if (browse_file == NULL)
3806 goto theend;
3807 ffname = browse_file;
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810#endif
3811 /* if no short name given, use ffname for short name */
3812 if (sfname == NULL)
3813 sfname = ffname;
3814#ifdef USE_FNAME_CASE
3815# ifdef USE_LONG_FNAME
3816 if (USE_LONG_FNAME)
3817# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003818 if (sfname != NULL)
3819 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820#endif
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3823 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 if (ffname == NULL)
3826 other_file = TRUE;
3827 /* there is no file name */
3828 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3829 other_file = FALSE;
3830 else
3831 {
3832 if (*ffname == NUL) /* re-edit with same file name */
3833 {
3834 ffname = curbuf->b_ffname;
3835 sfname = curbuf->b_fname;
3836 }
3837 free_fname = fix_fname(ffname); /* may expand to full path name */
3838 if (free_fname != NULL)
3839 ffname = free_fname;
3840 other_file = otherfile(ffname);
3841#ifdef FEAT_SUN_WORKSHOP
3842 if (usingSunWorkShop && p_acd
3843 && (cp = vim_strrchr(sfname, '/')) != NULL)
3844 sfname = ++cp;
3845#endif
3846 }
3847 }
3848
3849 /*
3850 * if the file was changed we may not be allowed to abandon it
3851 * - if we are going to re-edit the same file
3852 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3853 */
3854 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3855 || (curbuf->b_nwindows == 1
3856 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003857 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3858 | (other_file ? 0 : CCGD_MULTWIN)
3859 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3860 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
3862 if (fnum == 0 && other_file && ffname != NULL)
3863 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3864 goto theend;
3865 }
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 /*
3868 * End Visual mode before switching to another buffer, so the text can be
3869 * copied into the GUI selection buffer.
3870 */
3871 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003873#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003874 if ((command != NULL || newlnum > (linenr_T)0)
3875 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3876 {
3877 int len;
3878 char_u *p;
3879
3880 /* Set v:swapcommand for the SwapExists autocommands. */
3881 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003882 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003883 else
3884 len = 30;
3885 p = alloc((unsigned)len);
3886 if (p != NULL)
3887 {
3888 if (command != NULL)
3889 vim_snprintf((char *)p, len, ":%s\r", command);
3890 else
3891 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3892 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3893 did_set_swapcommand = TRUE;
3894 vim_free(p);
3895 }
3896 }
3897#endif
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 /*
3900 * If we are starting to edit another file, open a (new) buffer.
3901 * Otherwise we re-use the current buffer.
3902 */
3903 if (other_file)
3904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if (!(flags & ECMD_ADDBUF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003907 if (!cmdmod.keepalt)
3908 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003909 if (oldwin != NULL)
3910 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912
3913 if (fnum)
3914 buf = buflist_findnr(fnum);
3915 else
3916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if (flags & ECMD_ADDBUF)
3918 {
3919 linenr_T tlnum = 1L;
3920
3921 if (command != NULL)
3922 {
3923 tlnum = atol((char *)command);
3924 if (tlnum <= 0)
3925 tlnum = 1L;
3926 }
3927 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3928 goto theend;
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 buf = buflist_new(ffname, sfname, 0L,
3931 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003932
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003933 /* autocommands may change curwin and curbuf */
3934 if (oldwin != NULL)
3935 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003936 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 if (buf == NULL)
3939 goto theend;
3940 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3941 {
3942 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 else /* existing memfile */
3945 {
3946 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003947 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003949 /* Check if autocommands made the buffer invalid or changed the
3950 * current buffer. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003951 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 goto theend;
3953#ifdef FEAT_EVAL
3954 if (aborting()) /* autocmds may abort script processing */
3955 goto theend;
3956#endif
3957 }
3958
3959 /* May jump to last used line number for a loaded buffer or when asked
3960 * for explicitly */
3961 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3962 {
3963 pos = buflist_findfpos(buf);
3964 newlnum = pos->lnum;
3965 solcol = pos->col;
3966 }
3967
3968 /*
3969 * Make the (new) buffer the one used by the current window.
3970 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3971 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003972 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 */
3974 if (buf != curbuf)
3975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 /*
3977 * Be careful: The autocommands may delete any buffer and change
3978 * the current buffer.
3979 * - If the buffer we are going to edit is deleted, give up.
3980 * - If the current buffer is deleted, prefer to load the new
3981 * buffer when loading a buffer is required. This avoids
3982 * loading another buffer which then must be closed again.
3983 * - If we ended up in the new buffer already, need to skip a few
3984 * things, set auto_buf.
3985 */
3986 if (buf->b_fname != NULL)
3987 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003988 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003990 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003992 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 delbuf_msg(new_name); /* frees new_name */
3994 goto theend;
3995 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003996#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (aborting()) /* autocmds may abort script processing */
3998 {
3999 vim_free(new_name);
4000 goto theend;
4001 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (buf == curbuf) /* already in new buffer */
4004 auto_buf = TRUE;
4005 else
4006 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02004007 win_T *the_curwin = curwin;
4008
4009 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004010 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004011 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004012 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02004013
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004014 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 buf_copy_options(buf, BCO_ENTER);
4016
Bram Moolenaar5a497892016-09-03 16:29:04 +02004017 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02004018 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004019 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004020 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004021 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaar5a497892016-09-03 16:29:04 +02004023 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004024 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01004025
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004026#ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02004027 /* autocmds may abort script processing */
4028 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 vim_free(new_name);
4031 goto theend;
4032 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004035 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004037 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 delbuf_msg(new_name); /* frees new_name */
4039 goto theend;
4040 }
4041 if (buf == curbuf) /* already in new buffer */
4042 auto_buf = TRUE;
4043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004045#ifdef FEAT_SYN_HL
4046 /*
4047 * <VN> We could instead free the synblock
4048 * and re-attach to buffer, perhaps.
4049 */
Bram Moolenaarf1d13472017-07-11 18:28:46 +02004050 if (curwin->w_buffer == NULL
4051 || curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02004052 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 curwin->w_buffer = buf;
4055 curbuf = buf;
4056 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004057
4058 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
4059 if (!oldbuf && eap != NULL)
4060 {
4061 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004062#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004063 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004064#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067
4068 /* May get the window options from the last time this buffer
4069 * was in this window (or another window). If not used
4070 * before, reset the local window options to the global
4071 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004072 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004073#ifdef FEAT_SPELL
4074 did_get_winopts = TRUE;
4075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004078 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004079 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 curwin->w_pcmark.lnum = 1;
4083 curwin->w_pcmark.col = 0;
4084 }
4085 else /* !other_file */
4086 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02004087 if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 oldbuf = (flags & ECMD_OLDBUF);
4091 }
4092
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004093 /* Don't redraw until the cursor is in the right line, otherwise
4094 * autocommands may cause ml_get errors. */
4095 ++RedrawingDisabled;
4096 did_inc_redrawing_disabled = TRUE;
4097
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004098 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4100 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004101 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 else
4104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /* Don't make a buffer listed if it's a help buffer. Useful when
4106 * using CTRL-O to go back to a help file. */
4107 if (!curbuf->b_help)
4108 set_buflisted(TRUE);
4109 }
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 /* If autocommands change buffers under our fingers, forget about
4112 * editing the file. */
4113 if (buf != curbuf)
4114 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004115#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (aborting()) /* autocmds may abort script processing */
4117 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 /* Since we are starting to edit a file, consider the filetype to be
4121 * unset. Helps for when an autocommand changes files and expects syntax
4122 * highlighting to work in the other file. */
4123 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125/*
4126 * other_file oldbuf
4127 * FALSE FALSE re-edit same file, buffer is re-used
4128 * FALSE TRUE re-edit same file, nothing changes
4129 * TRUE FALSE start editing new file, new buffer
4130 * TRUE TRUE start editing in existing buffer (nothing to do)
4131 */
4132 if (!other_file && !oldbuf) /* re-use the buffer */
4133 {
4134 set_last_cursor(curwin); /* may set b_last_cursor */
4135 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4136 {
4137 newlnum = curwin->w_cursor.lnum;
4138 solcol = curwin->w_cursor.col;
4139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 buf = curbuf;
4141 if (buf->b_fname != NULL)
4142 new_name = vim_strsave(buf->b_fname);
4143 else
4144 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004145 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004146
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004147 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4148 {
4149 /* Save all the text, so that the reload can be undone.
4150 * Sync first so that this is a separate undo-able action. */
4151 u_sync(FALSE);
4152 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4153 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004154 {
4155 vim_free(new_name);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004156 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004157 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004158 u_unchanged(curbuf);
4159 buf_freeall(curbuf, BFA_KEEP_UNDO);
4160
4161 /* tell readfile() not to clear or reload undo info */
4162 readfile_flags = READ_KEEP_UNDO;
4163 }
4164 else
4165 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /* If autocommands deleted the buffer we were going to re-edit, give
4168 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004169 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
4171 delbuf_msg(new_name); /* frees new_name */
4172 goto theend;
4173 }
4174 vim_free(new_name);
4175
4176 /* If autocommands change buffers under our fingers, forget about
4177 * re-editing the file. Should do the buf_clear_file(), but perhaps
4178 * the autocommands changed the buffer... */
4179 if (buf != curbuf)
4180 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004181#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (aborting()) /* autocmds may abort script processing */
4183 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#endif
4185 buf_clear_file(curbuf);
4186 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4187 curbuf->b_op_end.lnum = 0;
4188 }
4189
4190/*
4191 * If we get here we are sure to start editing
4192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 /* Assume success now */
4194 retval = OK;
4195
4196 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * Check if we are editing the w_arg_idx file in the argument list.
4198 */
4199 check_arg_idx(curwin);
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 if (!auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /*
4204 * Set cursor and init window before reading the file and executing
4205 * autocommands. This allows for the autocommands to position the
4206 * cursor.
4207 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004208 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004211 /* It's possible that all lines in the buffer changed. Need to update
4212 * automatic folding for all windows where it's used. */
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004213 {
4214 win_T *win;
4215 tabpage_T *tp;
4216
4217 FOR_ALL_TAB_WINDOWS(tp, win)
4218 if (win->w_buffer == curbuf)
4219 foldUpdateAll(win);
4220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#endif
4222
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004223 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004224 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 /*
4227 * Careful: open_buffer() and apply_autocmds() may change the current
4228 * buffer and window.
4229 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004230 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 topline = curwin->w_topline;
4232 if (!oldbuf) /* need to read the file */
4233 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004234#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 swap_exists_action = SEA_DIALOG;
4236#endif
4237 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4238
4239 /*
4240 * Open the buffer and read the file.
4241 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004242#if defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004243 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 retval = FAIL;
4245#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004246 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#endif
4248
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004249#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (swap_exists_action == SEA_QUIT)
4251 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004252 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253#endif
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
4256 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004257 /* Read the modelines, but only to set window-local options. Any
4258 * buffer-local options have already been set and may have been
4259 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004260 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4263 &retval);
4264 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4265 &retval);
4266 }
4267 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaareab316b2015-03-24 11:46:30 +01004269 /* If autocommands change the cursor position or topline, we should
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004270 * keep it. Also when it moves within a line. But not when it moves
4271 * to the first non-blank. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004272 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004274 char_u *text = ml_get_curline();
4275
4276 if (curwin->w_cursor.lnum != orig_pos.lnum
4277 || curwin->w_cursor.col != (int)(skipwhite(text) - text))
4278 {
4279 newlnum = curwin->w_cursor.lnum;
4280 newcol = curwin->w_cursor.col;
4281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 if (curwin->w_topline == topline)
4284 topline = 0;
4285
4286 /* Even when cursor didn't move we need to recompute topline. */
4287 changed_line_abv_curs();
4288
4289#ifdef FEAT_TITLE
4290 maketitle();
4291#endif
4292 }
4293
4294#ifdef FEAT_DIFF
4295 /* Tell the diff stuff that this buffer is new and/or needs updating.
4296 * Also needed when re-editing the same buffer, because unloading will
4297 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004298 if (curwin->w_p_diff)
4299 {
4300 diff_buf_add(curbuf);
4301 diff_invalidate(curbuf);
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#endif
4304
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004305#ifdef FEAT_SPELL
4306 /* If the window options were changed may need to set the spell language.
4307 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004308 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4309 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004310#endif
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (command == NULL)
4313 {
4314 if (newcol >= 0) /* position set by autocommands */
4315 {
4316 curwin->w_cursor.lnum = newlnum;
4317 curwin->w_cursor.col = newcol;
4318 check_cursor();
4319 }
4320 else if (newlnum > 0) /* line number from caller or old position */
4321 {
4322 curwin->w_cursor.lnum = newlnum;
4323 check_cursor_lnum();
4324 if (solcol >= 0 && !p_sol)
4325 {
4326 /* 'sol' is off: Use last known column. */
4327 curwin->w_cursor.col = solcol;
4328 check_cursor_col();
4329#ifdef FEAT_VIRTUALEDIT
4330 curwin->w_cursor.coladd = 0;
4331#endif
4332 curwin->w_set_curswant = TRUE;
4333 }
4334 else
4335 beginline(BL_SOL | BL_FIX);
4336 }
4337 else /* no line number, go to last line in Ex mode */
4338 {
4339 if (exmode_active)
4340 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4341 beginline(BL_WHITE | BL_FIX);
4342 }
4343 }
4344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 /* Check if cursors in other windows on the same buffer are still valid */
4346 check_lnums(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 /*
4349 * Did not read the file, need to show some info about the file.
4350 * Do this after setting the cursor.
4351 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004352 if (oldbuf && !auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
4354 int msg_scroll_save = msg_scroll;
4355
4356 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4357 * message. */
4358 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4359 msg_scroll = FALSE;
4360 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4361 check_for_delay(FALSE);
4362 msg_start();
4363 msg_scroll = msg_scroll_save;
4364 msg_scrolled_ign = TRUE;
4365
Bram Moolenaar426dd022016-03-15 15:09:29 +01004366 if (!shortmess(SHM_FILEINFO))
4367 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 msg_scrolled_ign = FALSE;
4370 }
4371
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004372#ifdef FEAT_VIMINFO
4373 curbuf->b_last_used = vim_time();
4374#endif
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (command != NULL)
4377 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4378
4379#ifdef FEAT_KEYMAP
4380 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004381 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382#endif
4383
4384 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004385 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (!skip_redraw)
4387 {
4388 n = p_so;
4389 if (topline == 0 && command == NULL)
4390 p_so = 999; /* force cursor halfway the window */
4391 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 curwin->w_scbind_pos = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 p_so = n;
4394 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4395 }
4396
4397 if (p_im)
4398 need_start_insertmode = TRUE;
4399
Bram Moolenaar15833232018-02-03 18:33:17 +01004400#ifdef FEAT_AUTOCHDIR
4401 /* Change directories when the 'acd' option is set and we aren't already in
4402 * that directory (should already be done above). Expect getcwd() to be
4403 * faster than calling shorten_fnames() unnecessarily. */
4404 if (p_acd && curbuf->b_ffname != NULL)
4405 {
4406 char_u curdir[MAXPATHL];
4407 char_u filedir[MAXPATHL];
4408
4409 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1);
4410 *gettail_sep(filedir) = NUL;
4411 if (mch_dirname(curdir, MAXPATHL) != FAIL
4412 && vim_fnamecmp(curdir, filedir) != 0)
4413 do_autochdir();
4414 }
4415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00004417#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004418 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02004421 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
4423# endif
4424# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004425 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004426 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427# endif
4428 }
4429#endif
4430
4431theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004432 if (did_inc_redrawing_disabled)
4433 --RedrawingDisabled;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004434#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004435 if (did_set_swapcommand)
4436 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438#ifdef FEAT_BROWSE
4439 vim_free(browse_file);
4440#endif
4441 vim_free(free_fname);
4442 return retval;
4443}
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004446delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
4448 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4449 name == NULL ? (char_u *)"" : name);
4450 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004451 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004452 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004455static int append_indent = 0; /* autoindent for first line */
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457/*
4458 * ":insert" and ":append", also used by ":change"
4459 */
4460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004461ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 char_u *theline;
4464 int did_undo = FALSE;
4465 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004466 int indent = 0;
4467 char_u *p;
4468 int vcol;
4469 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004471 /* the ! flag toggles autoindent */
4472 if (eap->forceit)
4473 curbuf->b_p_ai = !curbuf->b_p_ai;
4474
4475 /* First autoindent comes from the line we start on */
4476 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4477 append_indent = get_indent_lnum(lnum);
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (eap->cmdidx != CMD_append)
4480 --lnum;
4481
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004482 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004483 if (empty && lnum == 1)
4484 lnum = 0;
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 State = INSERT; /* behave like in Insert mode */
4487 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4488 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004489
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004490 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 msg_scroll = TRUE;
4493 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004494 if (curbuf->b_p_ai)
4495 {
4496 if (append_indent >= 0)
4497 {
4498 indent = append_indent;
4499 append_indent = -1;
4500 }
4501 else if (lnum > 0)
4502 indent = get_indent_lnum(lnum);
4503 }
4504 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004506 {
4507 /* No getline() function, use the lines that follow. This ends
4508 * when there is no more. */
4509 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4510 break;
4511 p = vim_strchr(eap->nextcmd, NL);
4512 if (p == NULL)
4513 p = eap->nextcmd + STRLEN(eap->nextcmd);
4514 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4515 if (*p != NUL)
4516 ++p;
4517 eap->nextcmd = p;
4518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004520 {
4521 int save_State = State;
4522
4523 /* Set State to avoid the cursor shape to be set to INSERT mode
4524 * when getline() returns. */
4525 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 theline = eap->getline(
4527#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004528 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004530 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004531 State = save_State;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004534 if (theline == NULL)
4535 break;
4536
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004537 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4538 if (ex_keep_indent)
4539 append_indent = indent;
4540
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004541 /* Look for the "." after automatic indent. */
4542 vcol = 0;
4543 for (p = theline; indent > vcol; ++p)
4544 {
4545 if (*p == ' ')
4546 ++vcol;
4547 else if (*p == TAB)
4548 vcol += 8 - vcol % 8;
4549 else
4550 break;
4551 }
4552 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004553 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4554 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556 vim_free(theline);
4557 break;
4558 }
4559
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004560 /* don't use autoindent if nothing was typed. */
4561 if (p[0] == NUL)
4562 theline[0] = NUL;
4563
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 did_undo = TRUE;
4565 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004566 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 vim_free(theline);
4569 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004570
4571 if (empty)
4572 {
4573 ml_delete(2L, FALSE);
4574 empty = FALSE;
4575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577 State = NORMAL;
4578
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004579 if (eap->forceit)
4580 curbuf->b_p_ai = !curbuf->b_p_ai;
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004583 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 * "end" is set to lnum when something has been appended, otherwise
4585 * it is the same than "start" -- Acevedo */
4586 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4587 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4588 if (eap->cmdidx != CMD_append)
4589 --curbuf->b_op_start.lnum;
4590 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4591 ? lnum : curbuf->b_op_start.lnum;
4592 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4593 curwin->w_cursor.lnum = lnum;
4594 check_cursor_lnum();
4595 beginline(BL_SOL | BL_FIX);
4596
4597 need_wait_return = FALSE; /* don't use wait_return() now */
4598 ex_no_reprint = TRUE;
4599}
4600
4601/*
4602 * ":change"
4603 */
4604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004605ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 linenr_T lnum;
4608
4609 if (eap->line2 >= eap->line1
4610 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4611 return;
4612
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004613 /* the ! flag toggles autoindent */
4614 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4615 append_indent = get_indent_lnum(eap->line1);
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4618 {
4619 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4620 break;
4621 ml_delete(eap->line1, FALSE);
4622 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004623
4624 /* make sure the cursor is not beyond the end of the file now */
4625 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4627
4628 /* ":append" on the line above the deleted lines. */
4629 eap->line2 = eap->line1;
4630 ex_append(eap);
4631}
4632
4633 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 char_u *x;
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004637 long bigness;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004638 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 int minus = 0;
4640 linenr_T start, end, curs, i;
4641 int j;
4642 linenr_T lnum = eap->line2;
4643
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004644 /* Vi compatible: ":z!" uses display height, without a count uses
4645 * 'scroll' */
4646 if (eap->forceit)
4647 bigness = curwin->w_height;
Bram Moolenaar459ca562016-11-10 18:16:33 +01004648 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004649 bigness = curwin->w_height - 3;
Bram Moolenaar631abc32014-02-22 22:27:47 +01004650 else
4651 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (bigness < 1)
4653 bigness = 1;
4654
4655 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004656 kind = x;
4657 if (*kind == '-' || *kind == '+' || *kind == '='
4658 || *kind == '^' || *kind == '.')
4659 ++x;
4660 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 ++x;
4662
4663 if (*x != 0)
4664 {
4665 if (!VIM_ISDIGIT(*x))
4666 {
4667 EMSG(_("E144: non-numeric argument to :z"));
4668 return;
4669 }
4670 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004671 {
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004672 bigness = atol((char *)x);
4673
4674 /* bigness could be < 0 if atol(x) overflows. */
4675 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
4676 bigness = 2 * curbuf->b_ml.ml_line_count;
4677
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004678 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004679 if (*kind == '=')
4680 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004684 /* the number of '-' and '+' multiplies the distance */
4685 if (*kind == '-' || *kind == '+')
4686 for (x = kind + 1; *x == *kind; ++x)
4687 ;
4688
4689 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004692 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4693 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004694 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 break;
4696
4697 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004698 start = lnum - (bigness + 1) / 2 + 1;
4699 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 curs = lnum;
4701 minus = 1;
4702 break;
4703
4704 case '^':
4705 start = lnum - bigness * 2;
4706 end = lnum - bigness;
4707 curs = lnum - bigness;
4708 break;
4709
4710 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 start = lnum - (bigness + 1) / 2 + 1;
4712 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 curs = end;
4714 break;
4715
4716 default: /* '+' */
4717 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004718 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004719 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004720 else if (eap->addr_count == 0)
4721 ++start;
4722 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 curs = end;
4724 break;
4725 }
4726
4727 if (start < 1)
4728 start = 1;
4729
4730 if (end > curbuf->b_ml.ml_line_count)
4731 end = curbuf->b_ml.ml_line_count;
4732
4733 if (curs > curbuf->b_ml.ml_line_count)
4734 curs = curbuf->b_ml.ml_line_count;
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004735 else if (curs < 1)
4736 curs = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 for (i = start; i <= end; i++)
4739 {
4740 if (minus && i == lnum)
4741 {
4742 msg_putchar('\n');
4743
4744 for (j = 1; j < Columns; j++)
4745 msg_putchar('-');
4746 }
4747
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004748 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
4750 if (minus && i == lnum)
4751 {
4752 msg_putchar('\n');
4753
4754 for (j = 1; j < Columns; j++)
4755 msg_putchar('-');
4756 }
4757 }
4758
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004759 if (curwin->w_cursor.lnum != curs)
4760 {
4761 curwin->w_cursor.lnum = curs;
4762 curwin->w_cursor.col = 0;
4763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 ex_no_reprint = TRUE;
4765}
4766
4767/*
4768 * Check if the restricted flag is set.
4769 * If so, give an error message and return TRUE.
4770 * Otherwise, return FALSE.
4771 */
4772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004773check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 if (restricted)
4776 {
4777 EMSG(_("E145: Shell commands not allowed in rvim"));
4778 return TRUE;
4779 }
4780 return FALSE;
4781}
4782
4783/*
4784 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4785 * If so, give an error message and return TRUE.
4786 * Otherwise, return FALSE.
4787 */
4788 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004789check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 if (secure)
4792 {
4793 secure = 2;
4794 EMSG(_(e_curdir));
4795 return TRUE;
4796 }
4797#ifdef HAVE_SANDBOX
4798 /*
4799 * In the sandbox more things are not allowed, including the things
4800 * disallowed in secure mode.
4801 */
4802 if (sandbox != 0)
4803 {
4804 EMSG(_(e_sandbox));
4805 return TRUE;
4806 }
4807#endif
4808 return FALSE;
4809}
4810
4811static char_u *old_sub = NULL; /* previous substitute pattern */
4812static int global_need_beginline; /* call beginline() after ":g" */
4813
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004814/*
4815 * Flags that are kept between calls to :substitute.
4816 */
4817typedef struct {
4818 int do_all; /* do multiple substitutions per line */
4819 int do_ask; /* ask for confirmation */
4820 int do_count; /* count only */
4821 int do_error; /* if false, ignore errors */
4822 int do_print; /* print last line with subs. */
4823 int do_list; /* list last line with subs. */
4824 int do_number; /* list last line with line nr*/
4825 int do_ic; /* ignore case flag */
4826} subflags_T;
4827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828/* do_sub()
4829 *
4830 * Perform a substitution from line eap->line1 to line eap->line2 using the
4831 * command pointed to by eap->arg which should be of the form:
4832 *
4833 * /pattern/substitution/{flags}
4834 *
4835 * The usual escapes are supported as described in the regexp docs.
4836 */
4837 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 linenr_T lnum;
4841 long i = 0;
4842 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004843 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4844 FALSE, FALSE, 0};
4845#ifdef FEAT_EVAL
4846 subflags_T subflags_save;
4847#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004848 int save_do_all; /* remember user specified 'g' flag */
4849 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4851 int delimiter;
4852 int sublen;
4853 int got_quit = FALSE;
4854 int got_match = FALSE;
4855 int temp;
4856 int which_pat;
4857 char_u *cmd;
4858 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004859 linenr_T first_line = 0; /* first changed line */
4860 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 * change */
4862 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4863 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004864 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004865 char_u *sub_firstline; /* allocated copy of first sub line */
4866 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004867 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004868 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004869#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004870 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 cmd = eap->arg;
4874 if (!global_busy)
4875 {
4876 sub_nsubs = 0;
4877 sub_nlines = 0;
4878 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004879 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (eap->cmdidx == CMD_tilde)
4882 which_pat = RE_LAST; /* use last used regexp */
4883 else
4884 which_pat = RE_SUBST; /* use last substitute regexp */
4885
4886 /* new pattern and substitution */
Bram Moolenaar1c465442017-03-12 20:10:05 +01004887 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4889 {
4890 /* don't accept alphanumeric for separator */
4891 if (isalpha(*cmd))
4892 {
4893 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4894 return;
4895 }
4896 /*
4897 * undocumented vi feature:
4898 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4899 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4900 */
4901 if (*cmd == '\\')
4902 {
4903 ++cmd;
4904 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4905 {
4906 EMSG(_(e_backslash));
4907 return;
4908 }
4909 if (*cmd != '&')
4910 which_pat = RE_SEARCH; /* use last '/' pattern */
4911 pat = (char_u *)""; /* empty search pattern */
4912 delimiter = *cmd++; /* remember delimiter character */
4913 }
4914 else /* find the end of the regexp */
4915 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004916#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4917 if (p_altkeymap && curwin->w_p_rl)
4918 lrF_sub(cmd);
4919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 which_pat = RE_LAST; /* use last used regexp */
4921 delimiter = *cmd++; /* remember delimiter character */
4922 pat = cmd; /* remember start of search pat */
4923 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4924 if (cmd[0] == delimiter) /* end delimiter found */
4925 *cmd++ = NUL; /* replace it with a NUL */
4926 }
4927
4928 /*
4929 * Small incompatibility: vi sees '\n' as end of the command, but in
4930 * Vim we want to use '\n' to find/substitute a NUL.
4931 */
4932 sub = cmd; /* remember the start of the substitution */
4933
4934 while (cmd[0])
4935 {
4936 if (cmd[0] == delimiter) /* end delimiter found */
4937 {
4938 *cmd++ = NUL; /* replace it with a NUL */
4939 break;
4940 }
4941 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4942 ++cmd;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004943 MB_PTR_ADV(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945
4946 if (!eap->skip)
4947 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004948 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4949 if (STRCMP(sub, "%") == 0
4950 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4951 {
4952 if (old_sub == NULL) /* there is no previous command */
4953 {
4954 EMSG(_(e_nopresub));
4955 return;
4956 }
4957 sub = old_sub;
4958 }
4959 else
4960 {
4961 vim_free(old_sub);
4962 old_sub = vim_strsave(sub);
4963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 }
4966 else if (!eap->skip) /* use previous pattern and substitution */
4967 {
4968 if (old_sub == NULL) /* there is no previous command */
4969 {
4970 EMSG(_(e_nopresub));
4971 return;
4972 }
4973 pat = NULL; /* search_regcomp() will use previous pattern */
4974 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004975
4976 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4977 * last column after using "$". */
4978 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004981 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4982 * more efficient.
4983 * TODO: find a generic solution to make line-joining operations more
4984 * efficient, avoid allocating a string that grows in size.
4985 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004986 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004987 && *sub == NUL
4988 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4989 || *cmd == 'p' || *cmd == '#'))))
4990 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004991 linenr_T joined_lines_count;
4992
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004993 curwin->w_cursor.lnum = eap->line1;
4994 if (*cmd == 'l')
4995 eap->flags = EXFLAG_LIST;
4996 else if (*cmd == '#')
4997 eap->flags = EXFLAG_NR;
4998 else if (*cmd == 'p')
4999 eap->flags = EXFLAG_PRINT;
5000
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005001 /* The number of lines joined is the number of lines in the range plus
5002 * one. One less when the last line is included. */
5003 joined_lines_count = eap->line2 - eap->line1 + 1;
5004 if (eap->line2 < curbuf->b_ml.ml_line_count)
5005 ++joined_lines_count;
5006 if (joined_lines_count > 1)
5007 {
5008 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
5009 sub_nsubs = joined_lines_count - 1;
5010 sub_nlines = 1;
5011 (void)do_sub_msg(FALSE);
5012 ex_may_print(eap);
5013 }
5014
5015 if (!cmdmod.keeppatterns)
5016 save_re_pat(RE_SUBST, pat, p_magic);
5017#ifdef FEAT_CMDHIST
5018 /* put pattern in history */
5019 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
5020#endif
5021
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005022 return;
5023 }
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 /*
5026 * Find trailing options. When '&' is used, keep old options.
5027 */
5028 if (*cmd == '&')
5029 ++cmd;
5030 else
5031 {
5032 if (!p_ed)
5033 {
5034 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005035 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005037 subflags.do_all = FALSE;
5038 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005040 subflags.do_error = TRUE;
5041 subflags.do_print = FALSE;
5042 subflags.do_count = FALSE;
5043 subflags.do_number = FALSE;
5044 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 while (*cmd)
5047 {
5048 /*
5049 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
5050 * 'r' is never inverted.
5051 */
5052 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005053 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005055 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005056 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005057 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005059 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else if (*cmd == 'r') /* use last used regexp */
5061 which_pat = RE_LAST;
5062 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005063 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005064 else if (*cmd == '#')
5065 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005066 subflags.do_print = TRUE;
5067 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005068 }
5069 else if (*cmd == 'l')
5070 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005071 subflags.do_print = TRUE;
5072 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005075 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005077 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else
5079 break;
5080 ++cmd;
5081 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005082 if (subflags.do_count)
5083 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005085 save_do_all = subflags.do_all;
5086 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 /*
5089 * check for a trailing count
5090 */
5091 cmd = skipwhite(cmd);
5092 if (VIM_ISDIGIT(*cmd))
5093 {
5094 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005095 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 EMSG(_(e_zerocount));
5098 return;
5099 }
5100 eap->line1 = eap->line2;
5101 eap->line2 += i - 1;
5102 if (eap->line2 > curbuf->b_ml.ml_line_count)
5103 eap->line2 = curbuf->b_ml.ml_line_count;
5104 }
5105
5106 /*
5107 * check for trailing command or garbage
5108 */
5109 cmd = skipwhite(cmd);
5110 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5111 {
5112 eap->nextcmd = check_nextcmd(cmd);
5113 if (eap->nextcmd == NULL)
5114 {
5115 EMSG(_(e_trailing));
5116 return;
5117 }
5118 }
5119
5120 if (eap->skip) /* not executing commands, only parsing */
5121 return;
5122
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005123 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005124 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005125 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005126 EMSG(_(e_modifiable));
5127 return;
5128 }
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5131 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005132 if (subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 EMSG(_(e_invcmd));
5134 return;
5135 }
5136
5137 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005138 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005140 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 regmatch.rmm_ic = FALSE;
5142
5143 sub_firstline = NULL;
5144
5145 /*
5146 * ~ in the substitute pattern is replaced with the old pattern.
5147 * We do it here once to avoid it to be replaced over and over again.
5148 * But don't do it when it starts with "\=", then it's an expression.
5149 */
5150 if (!(sub[0] == '\\' && sub[1] == '='))
5151 sub = regtilde(sub, p_magic);
5152
5153 /*
5154 * Check for a match on each line.
5155 */
5156 line2 = eap->line2;
5157 for (lnum = eap->line1; lnum <= line2 && !(got_quit
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005158#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 || aborting()
5160#endif
5161 ); ++lnum)
5162 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005163 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005164 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (nmatch)
5166 {
5167 colnr_T copycol;
5168 colnr_T matchcol;
5169 colnr_T prev_matchcol = MAXCOL;
5170 char_u *new_end, *new_start = NULL;
5171 unsigned new_start_len = 0;
5172 char_u *p1;
5173 int did_sub = FALSE;
5174 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005175 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 long nmatch_tl = 0; /* nr of lines matched below lnum */
5177 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005178 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005179 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 /*
5182 * The new text is build up step by step, to avoid too much
5183 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005184 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 * copycol Column in the old text where we started
5186 * looking for a match; from here old text still
5187 * needs to be copied to the new text.
5188 * matchcol Column number of the old text where to look
5189 * for the next match. It's just after the
5190 * previous match or one further.
5191 * prev_matchcol Column just after the previous match (if any).
5192 * Mostly equal to matchcol, except for the first
5193 * match and after skipping an empty match.
5194 * regmatch.*pos Where the pattern matched in the old text.
5195 * new_start The new text, all that has been produced so
5196 * far.
5197 * new_end The new text, where to append new text.
5198 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005199 * lnum The line number where we found the start of
5200 * the match. Can be below the line we searched
5201 * when there is a \n before a \zs in the
5202 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 * sub_firstlnum The line number in the buffer where to look
5204 * for a match. Can be different from "lnum"
5205 * when the pattern or substitute string contains
5206 * line breaks.
5207 *
5208 * Special situations:
5209 * - When the substitute string contains a line break, the part up
5210 * to the line break is inserted in the text, but the copy of
5211 * the original line is kept. "sub_firstlnum" is adjusted for
5212 * the inserted lines.
5213 * - When the matched pattern contains a line break, the old line
5214 * is taken from the line at the end of the pattern. The lines
5215 * in the match are deleted later, "sub_firstlnum" is adjusted
5216 * accordingly.
5217 *
5218 * The new text is built up in new_start[]. It has some extra
5219 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005220 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 *
5222 * Make a copy of the old line, so it won't be taken away when
5223 * updating the screen or handling a multi-line match. The "old_"
5224 * pointers point into this copy.
5225 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005226 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 copycol = 0;
5228 matchcol = 0;
5229
5230 /* At first match, remember current cursor position. */
5231 if (!got_match)
5232 {
5233 setpcmark();
5234 got_match = TRUE;
5235 }
5236
5237 /*
5238 * Loop until nothing more to replace in this line.
5239 * 1. Handle match with empty string.
5240 * 2. If do_ask is set, ask for confirmation.
5241 * 3. substitute the string.
5242 * 4. if do_all is set, find next match
5243 * 5. break if there isn't another match in this line
5244 */
5245 for (;;)
5246 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005247 /* Advance "lnum" to the line where the match starts. The
5248 * match does not start in the first line when there is a line
5249 * break before \zs. */
5250 if (regmatch.startpos[0].lnum > 0)
5251 {
5252 lnum += regmatch.startpos[0].lnum;
5253 sub_firstlnum += regmatch.startpos[0].lnum;
5254 nmatch -= regmatch.startpos[0].lnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005255 VIM_CLEAR(sub_firstline);
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005256 }
5257
5258 if (sub_firstline == NULL)
5259 {
5260 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5261 if (sub_firstline == NULL)
5262 {
5263 vim_free(new_start);
5264 goto outofmem;
5265 }
5266 }
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 /* Save the line number of the last change for the final
5269 * cursor position (just like Vi). */
5270 curwin->w_cursor.lnum = lnum;
5271 do_again = FALSE;
5272
5273 /*
5274 * 1. Match empty string does not count, except for first
5275 * match. This reproduces the strange vi behaviour.
5276 * This also catches endless loops.
5277 */
5278 if (matchcol == prev_matchcol
5279 && regmatch.endpos[0].lnum == 0
5280 && matchcol == regmatch.endpos[0].col)
5281 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005282 if (sub_firstline[matchcol] == NUL)
5283 /* We already were at the end of the line. Don't look
5284 * for a match in this line again. */
5285 skip_match = TRUE;
5286 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005287 {
5288 /* search for a match at next column */
5289#ifdef FEAT_MBYTE
5290 if (has_mbyte)
5291 matchcol += mb_ptr2len(sub_firstline + matchcol);
5292 else
5293#endif
5294 ++matchcol;
5295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 goto skip;
5297 }
5298
5299 /* Normally we continue searching for a match just after the
5300 * previous match. */
5301 matchcol = regmatch.endpos[0].col;
5302 prev_matchcol = matchcol;
5303
5304 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005305 * 2. If do_count is set only increase the counter.
5306 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005308 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005309 {
5310 /* For a multi-line match, put matchcol at the NUL at
5311 * the end of the line and set nmatch to one, so that
5312 * we continue looking for a match on the next line.
5313 * Avoids that ":s/\nB\@=//gc" get stuck. */
5314 if (nmatch > 1)
5315 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005316 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005317 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005318 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005319 }
5320 sub_nsubs++;
5321 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005322#ifdef FEAT_EVAL
5323 /* Skip the substitution, unless an expression is used,
5324 * then it is evaluated in the sandbox. */
5325 if (!(sub[0] == '\\' && sub[1] == '='))
5326#endif
5327 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005328 }
5329
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005330 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005332 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 /* change State to CONFIRM, so that the mouse works
5335 * properly */
5336 save_State = State;
5337 State = CONFIRM;
5338#ifdef FEAT_MOUSE
5339 setmouse(); /* disable mouse in xterm */
5340#endif
5341 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005342 if (curwin->w_p_crb)
5343 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345 /* When 'cpoptions' contains "u" don't sync undo when
5346 * asking for confirmation. */
5347 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5348 ++no_u_sync;
5349
5350 /*
5351 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5352 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005353 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005355 if (exmode_active)
5356 {
5357 char_u *resp;
5358 colnr_T sc, ec;
5359
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005360 print_line_no_prefix(lnum,
5361 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005362
5363 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5364 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005365 if (curwin->w_cursor.col < 0)
5366 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005367 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005368 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005369 {
5370 int numw = number_width(curwin) + 1;
5371 sc += numw;
5372 ec += numw;
5373 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005374 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005375 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005376 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005377 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005378 msg_putchar('^');
5379
5380 resp = getexmodeline('?', NULL, 0);
5381 if (resp != NULL)
5382 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005383 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005384 vim_free(resp);
5385 }
5386 }
5387 else
5388 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005389 char_u *orig_line = NULL;
5390 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005392 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005394 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005396 /* Invert the matched string.
5397 * Remove the inversion afterwards. */
5398 temp = RedrawingDisabled;
5399 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005401 if (new_start != NULL)
5402 {
5403 /* There already was a substitution, we would
5404 * like to show this to the user. We cannot
5405 * really update the line, it would change
5406 * what matches. Temporarily replace the line
5407 * and change it back afterwards. */
5408 orig_line = vim_strsave(ml_get(lnum));
5409 if (orig_line != NULL)
5410 {
5411 char_u *new_line = concat_str(new_start,
5412 sub_firstline + copycol);
5413
5414 if (new_line == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005415 VIM_CLEAR(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005416 else
5417 {
5418 /* Position the cursor relative to the
5419 * end of the line, the previous
5420 * substitute may have inserted or
5421 * deleted characters before the
5422 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005423 len_change = (int)STRLEN(new_line)
5424 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005425 curwin->w_cursor.col += len_change;
5426 ml_replace(lnum, new_line, FALSE);
5427 }
5428 }
5429 }
5430
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005431 search_match_lines = regmatch.endpos[0].lnum
5432 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005433 search_match_endcol = regmatch.endpos[0].col
5434 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005435 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005437 update_topline();
5438 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005439 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005440 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005441 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005444 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005446 if (msg_row == Rows - 1)
5447 msg_didout = FALSE; /* avoid a scroll-up */
5448 msg_starthere();
5449 i = msg_scroll;
5450 msg_scroll = 0; /* truncate msg when
5451 needed */
5452 msg_no_more = TRUE;
5453 /* write message same highlighting as for
5454 * wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005455 smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005456 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
5457 msg_no_more = FALSE;
5458 msg_scroll = i;
5459 showruler(TRUE);
5460 windgoto(msg_row, msg_col);
5461 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005464 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005466 ++no_mapping; /* don't map this key */
5467 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005468 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005469 --allow_keys;
5470 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005472 /* clear the question */
5473 msg_didout = FALSE; /* don't scroll up */
5474 msg_col = 0;
5475 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005476
5477 /* restore the line */
5478 if (orig_line != NULL)
5479 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005480 }
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005483 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005485 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486#endif
5487 )
5488 {
5489 got_quit = TRUE;
5490 break;
5491 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005492 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005494 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005496 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
5498 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005499 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 line2 = lnum;
5501 break;
5502 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005503 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005505 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 break;
5507 }
5508#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005509 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005511 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 scrolldown_clamp();
5513#endif
5514 }
5515 State = save_State;
5516#ifdef FEAT_MOUSE
5517 setmouse();
5518#endif
5519 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5520 --no_u_sync;
5521
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005522 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
5524 /* For a multi-line match, put matchcol at the NUL at
5525 * the end of the line and set nmatch to one, so that
5526 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005527 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5528 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (nmatch > 1)
5530 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005531 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005532 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 goto skip;
5535 }
5536 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005537 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539
5540 /* Move the cursor to the start of the match, so that we can
5541 * use "\=col("."). */
5542 curwin->w_cursor.col = regmatch.startpos[0].col;
5543
5544 /*
5545 * 3. substitute the string.
5546 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005547#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005548 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005549 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005550 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005551 save_ma = curbuf->b_p_ma;
5552 curbuf->b_p_ma = FALSE;
5553 sandbox++;
5554 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005555 /* Save flags for recursion. They can change for e.g.
5556 * :s/^/\=execute("s#^##gn") */
5557 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005560 sublen = vim_regsub_multi(&regmatch,
5561 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005563#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005564 /* Don't keep flags set by a recursive call. */
5565 subflags = subflags_save;
5566 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005567 {
5568 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005569 if (sandbox > 0)
5570 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005571 goto skip;
5572 }
5573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
Bram Moolenaar4463f292005-09-25 22:20:24 +00005575 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005576 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005577 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5578 {
5579 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5580 skip_match = TRUE;
5581 }
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /* Need room for:
5584 * - result so far in new_start (not for first sub in line)
5585 * - original text up to match
5586 * - length of substituted part
5587 * - original text after match
5588 */
5589 if (nmatch == 1)
5590 p1 = sub_firstline;
5591 else
5592 {
5593 p1 = ml_get(sub_firstlnum + nmatch - 1);
5594 nmatch_tl += nmatch - 1;
5595 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005596 copy_len = regmatch.startpos[0].col - copycol;
5597 needed_len = copy_len + ((unsigned)STRLEN(p1)
5598 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (new_start == NULL)
5600 {
5601 /*
5602 * Get some space for a temporary buffer to do the
5603 * substitution into (and some extra space to avoid
5604 * too many calls to alloc()/free()).
5605 */
5606 new_start_len = needed_len + 50;
5607 if ((new_start = alloc_check(new_start_len)) == NULL)
5608 goto outofmem;
5609 *new_start = NUL;
5610 new_end = new_start;
5611 }
5612 else
5613 {
5614 /*
5615 * Check if the temporary buffer is long enough to do the
5616 * substitution into. If not, make it larger (with a bit
5617 * extra to avoid too many calls to alloc()/free()).
5618 */
5619 len = (unsigned)STRLEN(new_start);
5620 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005621 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {
5623 new_start_len = needed_len + 50;
5624 if ((p1 = alloc_check(new_start_len)) == NULL)
5625 {
5626 vim_free(new_start);
5627 goto outofmem;
5628 }
5629 mch_memmove(p1, new_start, (size_t)(len + 1));
5630 vim_free(new_start);
5631 new_start = p1;
5632 }
5633 new_end = new_start + len;
5634 }
5635
5636 /*
5637 * copy the text up to the part that matched
5638 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005639 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5640 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005642 (void)vim_regsub_multi(&regmatch,
5643 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 sub, new_end, TRUE, p_magic, TRUE);
5645 sub_nsubs++;
5646 did_sub = TRUE;
5647
5648 /* Move the cursor to the start of the line, to avoid that it
5649 * is beyond the end of the line after the substitution. */
5650 curwin->w_cursor.col = 0;
5651
5652 /* For a multi-line match, make a copy of the last matched
5653 * line and continue in that one. */
5654 if (nmatch > 1)
5655 {
5656 sub_firstlnum += nmatch - 1;
5657 vim_free(sub_firstline);
5658 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5659 /* When going beyond the last line, stop substituting. */
5660 if (sub_firstlnum <= line2)
5661 do_again = TRUE;
5662 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005663 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665
5666 /* Remember next character to be copied. */
5667 copycol = regmatch.endpos[0].col;
5668
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005669 if (skip_match)
5670 {
5671 /* Already hit end of the buffer, sub_firstlnum is one
5672 * less than what it ought to be. */
5673 vim_free(sub_firstline);
5674 sub_firstline = vim_strsave((char_u *)"");
5675 copycol = 0;
5676 }
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 /*
5679 * Now the trick is to replace CTRL-M chars with a real line
5680 * break. This would make it impossible to insert a CTRL-M in
5681 * the text. The line break can be avoided by preceding the
5682 * CTRL-M with a backslash. To be able to insert a backslash,
5683 * they must be doubled in the string and are halved here.
5684 * That is Vi compatible.
5685 */
5686 for (p1 = new_end; *p1; ++p1)
5687 {
5688 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005689 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 else if (*p1 == CAR)
5691 {
5692 if (u_inssub(lnum) == OK) /* prepare for undo */
5693 {
5694 *p1 = NUL; /* truncate up to the CR */
5695 ml_append(lnum - 1, new_start,
5696 (colnr_T)(p1 - new_start + 1), FALSE);
5697 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005698 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 appended_lines(lnum - 1, 1L);
5700 else
5701 {
5702 if (first_line == 0)
5703 first_line = lnum;
5704 last_line = lnum + 1;
5705 }
5706 /* All line numbers increase. */
5707 ++sub_firstlnum;
5708 ++lnum;
5709 ++line2;
5710 /* move the cursor to the new line, like Vi */
5711 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005712 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005713 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 p1 = new_start - 1;
5715 }
5716 }
5717#ifdef FEAT_MBYTE
5718 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005719 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720#endif
5721 }
5722
5723 /*
5724 * 4. If do_all is set, find next match.
5725 * Prevent endless loop with patterns that match empty
5726 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5727 * But ":s/\n/#/" is OK.
5728 */
5729skip:
5730 /* We already know that we did the last subst when we are at
5731 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005732 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5733 * "line2" when there is a \zs in the pattern after a line
5734 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005735 lastone = (skip_match
5736 || got_int
5737 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005738 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005739 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005740 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5741 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 nmatch = -1;
5743
5744 /*
5745 * Replace the line in the buffer when needed. This is
5746 * skipped when there are more matches.
5747 * The check for nmatch_tl is needed for when multi-line
5748 * matching must replace the lines before trying to do another
5749 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005750 * When the match starts below where we start searching also
5751 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 */
5753 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 || nmatch_tl > 0
5755 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005756 curbuf, sub_firstlnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005757 matchcol, NULL, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005758 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
5760 if (new_start != NULL)
5761 {
5762 /*
5763 * Copy the rest of the line, that didn't match.
5764 * "matchcol" has to be adjusted, we use the end of
5765 * the line as reference, because the substitute may
5766 * have changed the number of characters. Same for
5767 * "prev_matchcol".
5768 */
5769 STRCAT(new_start, sub_firstline + copycol);
5770 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5771 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5772 - prev_matchcol;
5773
5774 if (u_savesub(lnum) != OK)
5775 break;
5776 ml_replace(lnum, new_start, TRUE);
5777
5778 if (nmatch_tl > 0)
5779 {
5780 /*
5781 * Matched lines have now been substituted and are
5782 * useless, delete them. The part after the match
5783 * has been appended to new_start, we don't need
5784 * it in the buffer.
5785 */
5786 ++lnum;
5787 if (u_savedel(lnum, nmatch_tl) != OK)
5788 break;
5789 for (i = 0; i < nmatch_tl; ++i)
5790 ml_delete(lnum, (int)FALSE);
5791 mark_adjust(lnum, lnum + nmatch_tl - 1,
5792 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005793 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 deleted_lines(lnum, nmatch_tl);
5795 --lnum;
5796 line2 -= nmatch_tl; /* nr of lines decreases */
5797 nmatch_tl = 0;
5798 }
5799
5800 /* When asking, undo is saved each time, must also set
5801 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005802 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 changed_bytes(lnum, 0);
5804 else
5805 {
5806 if (first_line == 0)
5807 first_line = lnum;
5808 last_line = lnum + 1;
5809 }
5810
5811 sub_firstlnum = lnum;
5812 vim_free(sub_firstline); /* free the temp buffer */
5813 sub_firstline = new_start;
5814 new_start = NULL;
5815 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5816 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5817 - prev_matchcol;
5818 copycol = 0;
5819 }
5820 if (nmatch == -1 && !lastone)
5821 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005822 sub_firstlnum, matchcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
5824 /*
5825 * 5. break if there isn't another match in this line
5826 */
5827 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005828 {
5829 /* If the match found didn't start where we were
5830 * searching, do the next search in the line where we
5831 * found the match. */
5832 if (nmatch == -1)
5833 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
5837
5838 line_breakcheck();
5839 }
5840
5841 if (did_sub)
5842 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005843 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaard23a8232018-02-10 18:45:26 +01005844 VIM_CLEAR(sub_firstline); /* free the copy of the original line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
5846
5847 line_breakcheck();
5848 }
5849
5850 if (first_line != 0)
5851 {
5852 /* Need to subtract the number of added lines from "last_line" to get
5853 * the line number before the change (same as adding the number of
5854 * deleted lines). */
5855 i = curbuf->b_ml.ml_line_count - old_line_count;
5856 changed_lines(first_line, 0, last_line - i, i);
5857 }
5858
5859outofmem:
5860 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005861
5862 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005863 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005864 curwin->w_cursor = old_cursor;
5865
Bram Moolenaar46475522010-03-23 17:36:29 +01005866 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
5868 /* Set the '[ and '] marks. */
5869 curbuf->b_op_start.lnum = eap->line1;
5870 curbuf->b_op_end.lnum = line2;
5871 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5872
5873 if (!global_busy)
5874 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005875 /* when interactive leave cursor on the match */
5876 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005877 {
5878 if (endcolumn)
5879 coladvance((colnr_T)MAXCOL);
5880 else
5881 beginline(BL_WHITE | BL_FIX);
5882 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005883 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 MSG("");
5885 }
5886 else
5887 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005888 if (subflags.do_print)
5889 print_line(curwin->w_cursor.lnum,
5890 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
5892 else if (!global_busy)
5893 {
5894 if (got_int) /* interrupted */
5895 EMSG(_(e_interr));
5896 else if (got_match) /* did find something but nothing substituted */
5897 MSG("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005898 else if (subflags.do_error) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 EMSG2(_(e_patnotf2), get_search_pat());
5900 }
5901
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005902#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005903 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005904 /* Cursor position may require updating */
5905 changed_window_setting();
5906#endif
5907
Bram Moolenaar473de612013-06-08 18:19:48 +02005908 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005909
5910 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005911 subflags.do_all = save_do_all;
5912 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913}
5914
5915/*
5916 * Give message for number of substitutions.
5917 * Can also be used after a ":global" command.
5918 * Return TRUE if a message was given.
5919 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005921do_sub_msg(
5922 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 /*
5925 * Only report substitutions when:
5926 * - more than 'report' substitutions
5927 * - command was typed by user, or number of changed lines > 'report'
5928 * - giving messages is not disabled by 'lazyredraw'
5929 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005930 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5931 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 && messaging())
5933 {
5934 if (got_int)
5935 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005936 else
5937 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005939 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005940 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005942 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005943 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 sub_nsubs);
5945 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005946 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005947 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005949 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005950 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005953 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 return TRUE;
5955 }
5956 if (got_int)
5957 {
5958 EMSG(_(e_interr));
5959 return TRUE;
5960 }
5961 return FALSE;
5962}
5963
Bram Moolenaarf84b1222017-06-10 14:29:52 +02005964 static void
5965global_exe_one(char_u *cmd, linenr_T lnum)
5966{
5967 curwin->w_cursor.lnum = lnum;
5968 curwin->w_cursor.col = 0;
5969 if (*cmd == NUL || *cmd == '\n')
5970 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5971 else
5972 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5973}
5974
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975/*
5976 * Execute a global command of the form:
5977 *
5978 * g/pattern/X : execute X on all lines where pattern matches
5979 * v/pattern/X : execute X on all lines where pattern does not match
5980 *
5981 * where 'X' is an EX command
5982 *
5983 * The command character (as well as the trailing slash) is optional, and
5984 * is assumed to be 'p' if missing.
5985 *
5986 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005987 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 * for each line that has a mark. This is required because after deleting
5989 * lines we do not know where to search for the next match.
5990 */
5991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005995 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 int type; /* first char of cmd: 'v' or 'g' */
5997 char_u *cmd; /* command argument */
5998
5999 char_u delim; /* delimiter, normally '/' */
6000 char_u *pat;
6001 regmmatch_T regmatch;
6002 int match;
6003 int which_pat;
6004
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006005 /* When nesting the command works on one line. This allows for
6006 * ":g/found/v/notfound/command". */
6007 if (global_busy && (eap->line1 != 1
6008 || eap->line2 != curbuf->b_ml.ml_line_count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006010 /* will increment global_busy to break out of the loop */
6011 EMSG(_("E147: Cannot do :global recursive with a range"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 return;
6013 }
6014
6015 if (eap->forceit) /* ":global!" is like ":vglobal" */
6016 type = 'v';
6017 else
6018 type = *eap->cmd;
6019 cmd = eap->arg;
6020 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
6022 /*
6023 * undocumented vi feature:
6024 * "\/" and "\?": use previous search pattern.
6025 * "\&": use previous substitute pattern.
6026 */
6027 if (*cmd == '\\')
6028 {
6029 ++cmd;
6030 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
6031 {
6032 EMSG(_(e_backslash));
6033 return;
6034 }
6035 if (*cmd == '&')
6036 which_pat = RE_SUBST; /* use previous substitute pattern */
6037 else
6038 which_pat = RE_SEARCH; /* use previous search pattern */
6039 ++cmd;
6040 pat = (char_u *)"";
6041 }
6042 else if (*cmd == NUL)
6043 {
6044 EMSG(_("E148: Regular expression missing from global"));
6045 return;
6046 }
6047 else
6048 {
6049 delim = *cmd; /* get the delimiter */
6050 if (delim)
6051 ++cmd; /* skip delimiter if there is one */
6052 pat = cmd; /* remember start of pattern */
6053 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
6054 if (cmd[0] == delim) /* end delimiter found */
6055 *cmd++ = NUL; /* replace it with a NUL */
6056 }
6057
6058#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
6059 if (p_altkeymap && curwin->w_p_rl)
6060 lrFswap(pat,0);
6061#endif
6062
6063 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
6064 {
6065 EMSG(_(e_invcmd));
6066 return;
6067 }
6068
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006069 if (global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006071 lnum = curwin->w_cursor.lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006072 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006073 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if ((type == 'g' && match) || (type == 'v' && !match))
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006075 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006078 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006079 /*
6080 * pass 1: set marks for each (not) matching line
6081 */
6082 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6083 {
6084 /* a match on this line? */
6085 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006086 (colnr_T)0, NULL, NULL);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006087 if ((type == 'g' && match) || (type == 'v' && !match))
6088 {
6089 ml_setmarked(lnum);
6090 ndone++;
6091 }
6092 line_breakcheck();
6093 }
6094
6095 /*
6096 * pass 2: execute the command for each line that has been marked
6097 */
6098 if (got_int)
6099 MSG(_(e_interr));
6100 else if (ndone == 0)
6101 {
6102 if (type == 'v')
6103 smsg((char_u *)_("Pattern found in every line: %s"), pat);
6104 else
6105 smsg((char_u *)_("Pattern not found: %s"), pat);
6106 }
6107 else
6108 {
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006109#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006110 start_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006111#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006112 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006113#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006114 end_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006115#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006116 }
6117
6118 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
Bram Moolenaar473de612013-06-08 18:19:48 +02006121 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122}
6123
6124/*
6125 * Execute "cmd" on lines marked with ml_setmarked().
6126 */
6127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006130 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6131 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6132 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 /*
6135 * Set current position only once for a global command.
6136 * If global_busy is set, setpcmark() will not do anything.
6137 * If there is an error, global_busy will be incremented.
6138 */
6139 setpcmark();
6140
6141 /* When the command writes a message, don't overwrite the command. */
6142 msg_didout = TRUE;
6143
Bram Moolenaard25bc232010-03-23 17:49:24 +01006144 sub_nsubs = 0;
6145 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 global_need_beginline = FALSE;
6147 global_busy = 1;
6148 old_lcount = curbuf->b_ml.ml_line_count;
6149 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6150 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006151 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 ui_breakcheck();
6153 }
6154
6155 global_busy = 0;
6156 if (global_need_beginline)
6157 beginline(BL_WHITE | BL_FIX);
6158 else
6159 check_cursor(); /* cursor may be beyond the end of the line */
6160
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006161 /* the cursor may not have moved in the text but a change in a previous
6162 * line may move it on the screen */
6163 changed_line_abv_curs();
6164
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 /* If it looks like no message was written, allow overwriting the
6166 * command with the report for number of changes. */
6167 if (msg_col == 0 && msg_scrolled == 0)
6168 msg_didout = FALSE;
6169
Bram Moolenaar45667512007-05-10 19:24:43 +00006170 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006171 * number of extra or deleted lines.
6172 * Don't report extra or deleted lines in the edge case where the buffer
6173 * we are in after execution is different from the buffer we started in. */
6174 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6176}
6177
6178#ifdef FEAT_VIMINFO
6179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006182 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 vim_free(old_sub);
6184 if (force || old_sub == NULL)
6185 old_sub = viminfo_readstring(virp, 1, TRUE);
6186 return viminfo_readline(virp);
6187}
6188
6189 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006190write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6193 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006194 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 viminfo_writestring(fp, old_sub);
6196 }
6197}
6198#endif /* FEAT_VIMINFO */
6199
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006200#if defined(EXITFREE) || defined(PROTO)
6201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006202free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006203{
6204 vim_free(old_sub);
6205}
6206#endif
6207
Bram Moolenaar4033c552017-09-16 20:54:51 +02006208#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209/*
6210 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006211 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006213 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006214prepare_tagpreview(
6215 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 win_T *wp;
6218
6219# ifdef FEAT_GUI
6220 need_mouse_correct = TRUE;
6221# endif
6222
6223 /*
6224 * If there is already a preview window open, use that one.
6225 */
6226 if (!curwin->w_p_pvw)
6227 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006228 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 if (wp->w_p_pvw)
6230 break;
6231 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006232 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 else
6234 {
6235 /*
6236 * There is no preview window open yet. Create one.
6237 */
6238 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6239 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006240 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 curwin->w_p_pvw = TRUE;
6242 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006243 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6244 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245# ifdef FEAT_DIFF
6246 curwin->w_p_diff = FALSE; /* no 'diff' */
6247# endif
6248# ifdef FEAT_FOLDING
6249 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6250# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006251 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
6253 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006254 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255}
6256
6257#endif
6258
6259
6260/*
6261 * ":help": open a read-only window on a help file
6262 */
6263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006264ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 char_u *arg;
6267 char_u *tag;
6268 FILE *helpfd; /* file descriptor of help file */
6269 int n;
6270 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 int num_matches;
6273 char_u **matches;
6274 char_u *p;
6275 int empty_fnum = 0;
6276 int alt_fnum = 0;
6277 buf_T *buf;
6278#ifdef FEAT_MULTI_LANG
6279 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006280 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006282#ifdef FEAT_FOLDING
6283 int old_KeyTyped = KeyTyped;
6284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
6286 if (eap != NULL)
6287 {
6288 /*
6289 * A ":help" command ends at the first LF, or at a '|' that is
6290 * followed by some text. Set nextcmd to the following command.
6291 */
6292 for (arg = eap->arg; *arg; ++arg)
6293 {
6294 if (*arg == '\n' || *arg == '\r'
6295 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6296 {
6297 *arg++ = NUL;
6298 eap->nextcmd = arg;
6299 break;
6300 }
6301 }
6302 arg = eap->arg;
6303
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006304 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
6306 EMSG(_("E478: Don't panic!"));
6307 return;
6308 }
6309
6310 if (eap->skip) /* not executing commands */
6311 return;
6312 }
6313 else
6314 arg = (char_u *)"";
6315
6316 /* remove trailing blanks */
6317 p = arg + STRLEN(arg) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006318 while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 *p-- = NUL;
6320
6321#ifdef FEAT_MULTI_LANG
6322 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006323 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324#endif
6325
6326 /* When no argument given go to the index. */
6327 if (*arg == NUL)
6328 arg = (char_u *)"help.txt";
6329
6330 /*
6331 * Check if there is a match for the argument.
6332 */
6333 n = find_help_tags(arg, &num_matches, &matches,
6334 eap != NULL && eap->forceit);
6335
6336 i = 0;
6337#ifdef FEAT_MULTI_LANG
6338 if (n != FAIL && lang != NULL)
6339 /* Find first item with the requested language. */
6340 for (i = 0; i < num_matches; ++i)
6341 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006342 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (len > 3 && matches[i][len - 3] == '@'
6344 && STRICMP(matches[i] + len - 2, lang) == 0)
6345 break;
6346 }
6347#endif
6348 if (i >= num_matches || n == FAIL)
6349 {
6350#ifdef FEAT_MULTI_LANG
6351 if (lang != NULL)
6352 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
6353 else
6354#endif
6355 EMSG2(_("E149: Sorry, no help for %s"), arg);
6356 if (n != FAIL)
6357 FreeWild(num_matches, matches);
6358 return;
6359 }
6360
6361 /* The first match (in the requested language) is the best match. */
6362 tag = vim_strsave(matches[i]);
6363 FreeWild(num_matches, matches);
6364
6365#ifdef FEAT_GUI
6366 need_mouse_correct = TRUE;
6367#endif
6368
6369 /*
6370 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006371 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006373 if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006375 if (cmdmod.tab != 0)
6376 wp = NULL;
6377 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006378 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006379 if (bt_help(wp->w_buffer))
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006380 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6382 win_enter(wp, TRUE);
6383 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
6385 /*
6386 * There is no help window yet.
6387 * Try to open the file specified by the "helpfile" option.
6388 */
6389 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6390 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006391 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 goto erret;
6393 }
6394 fclose(helpfd);
6395
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006397 * specified, the current window is vertically split and
6398 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006401 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006404 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (curwin->w_height < p_hh)
6407 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
6409 /*
6410 * Open help file (do_ecmd() will set b_help flag, readfile() will
6411 * set b_p_ro flag).
6412 * Set the alternate file to the previously edited file.
6413 */
6414 alt_fnum = curbuf->b_fnum;
6415 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006416 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaar4033c552017-09-16 20:54:51 +02006417 NULL); /* buffer is still open, don't store info */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006418 if (!cmdmod.keepalt)
6419 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 empty_fnum = curbuf->b_fnum;
6421 }
6422 }
6423
6424 if (!p_im)
6425 restart_edit = 0; /* don't want insert mode in help file */
6426
Bram Moolenaar8f535582011-09-30 17:30:31 +02006427#ifdef FEAT_FOLDING
6428 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6429 * It is needed for do_tag top open folds under the cursor. */
6430 KeyTyped = old_KeyTyped;
6431#endif
6432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (tag != NULL)
6434 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6435
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006436 /* Delete the empty buffer if we're not using it. Careful: autocommands
6437 * may have jumped to another window, check that the buffer is not in a
6438 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6440 {
6441 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006442 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 wipe_buffer(buf, TRUE);
6444 }
6445
6446 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006447 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 curwin->w_alt_fnum = alt_fnum;
6449
6450erret:
6451 vim_free(tag);
6452}
6453
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006454/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006455 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006456 */
6457 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006458ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006459{
6460 win_T *win;
6461
6462 FOR_ALL_WINDOWS(win)
6463 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006464 if (bt_help(win->w_buffer))
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006465 {
6466 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006467 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006468 }
6469 }
6470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006472#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6473/*
6474 * In an argument search for a language specifiers in the form "@xx".
6475 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6476 * Returns NULL if not found.
6477 */
6478 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006479check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006480{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006481 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006482
6483 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6484 && ASCII_ISALPHA(arg[len - 1]))
6485 {
6486 arg[len - 3] = NUL; /* remove the '@' */
6487 return arg + len - 2;
6488 }
6489 return NULL;
6490}
6491#endif
6492
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493/*
6494 * Return a heuristic indicating how well the given string matches. The
6495 * smaller the number, the better the match. This is the order of priorities,
6496 * from best match to worst match:
6497 * - Match with least alpha-numeric characters is better.
6498 * - Match with least total characters is better.
6499 * - Match towards the start is better.
6500 * - Match starting with "+" is worse (feature instead of command)
6501 * Assumption is made that the matched_string passed has already been found to
6502 * match some string for which help is requested. webb.
6503 */
6504 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006505help_heuristic(
6506 char_u *matched_string,
6507 int offset, /* offset for match */
6508 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 int num_letters;
6511 char_u *p;
6512
6513 num_letters = 0;
6514 for (p = matched_string; *p; p++)
6515 if (ASCII_ISALNUM(*p))
6516 num_letters++;
6517
6518 /*
6519 * Multiply the number of letters by 100 to give it a much bigger
6520 * weighting than the number of characters.
6521 * If there only is a match while ignoring case, add 5000.
6522 * If the match starts in the middle of a word, add 10000 to put it
6523 * somewhere in the last half.
6524 * If the match is more than 2 chars from the start, multiply by 200 to
6525 * put it after matches at the start.
6526 */
6527 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6528 && ASCII_ISALNUM(matched_string[offset - 1]))
6529 offset += 10000;
6530 else if (offset > 2)
6531 offset *= 200;
6532 if (wrong_case)
6533 offset += 5000;
6534 /* Features are less interesting than the subjects themselves, but "+"
6535 * alone is not a feature. */
6536 if (matched_string[0] == '+' && matched_string[1] != NUL)
6537 offset += 100;
6538 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6539}
6540
6541/*
6542 * Compare functions for qsort() below, that checks the help heuristics number
6543 * that has been put after the tagname by find_tags().
6544 */
6545 static int
6546#ifdef __BORLANDC__
6547_RTLENTRYF
6548#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006549help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 char *p1;
6552 char *p2;
6553
6554 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6555 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6556 return strcmp(p1, p2);
6557}
6558
6559/*
6560 * Find all help tags matching "arg", sort them and return in matches[], with
6561 * the number of matches in num_matches.
6562 * The matches will be sorted with a "best" match algorithm.
6563 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6564 */
6565 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006566find_help_tags(
6567 char_u *arg,
6568 int *num_matches,
6569 char_u ***matches,
6570 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 char_u *s, *d;
6573 int i;
6574 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006575 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006576 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaardad73092017-02-09 11:54:50 +01006577 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006578 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006579 "[count]", "[quotex]",
6580 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006581 "[pattern]", "\\|", "\\%$",
6582 "s/\\~", "s/\\U", "s/\\L",
6583 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006585 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006586 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaardad73092017-02-09 11:54:50 +01006587 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006588 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006589 "\\[count]", "\\[quotex]",
6590 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006591 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006592 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006593 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 int flags;
6595
6596 d = IObuff; /* assume IObuff is long enough! */
6597
6598 /*
6599 * Recognize a few exceptions to the rule. Some strings that contain '*'
6600 * with "star". Otherwise '*' is recognized as a wildcard.
6601 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006602 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (STRCMP(arg, mtable[i]) == 0)
6604 {
6605 STRCPY(d, rtable[i]);
6606 break;
6607 }
6608
6609 if (i < 0) /* no match in table */
6610 {
6611 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6612 * Also replace "\%^" and "\%(", they match every tag too.
6613 * Also "\zs", "\z1", etc.
6614 * Also "\@<", "\@=", "\@<=", etc.
6615 * And also "\_$" and "\_^". */
6616 if (arg[0] == '\\'
6617 && ((arg[1] != NUL && arg[2] == NUL)
6618 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6619 && arg[2] != NUL)))
6620 {
6621 STRCPY(d, "/\\\\");
6622 STRCPY(d + 3, arg + 1);
6623 /* Check for "/\\_$", should be "/\\_\$" */
6624 if (d[3] == '_' && d[4] == '$')
6625 STRCPY(d + 4, "\\$");
6626 }
6627 else
6628 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006629 /* Replace:
6630 * "[:...:]" with "\[:...:]"
6631 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006632 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006633 */
6634 if ((arg[0] == '[' && (arg[1] == ':'
6635 || (arg[1] == '+' && arg[2] == '+')))
6636 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 *d++ = '\\';
6638
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006639 /*
6640 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6641 */
6642 if (*arg == '(' && arg[1] == '\'')
6643 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 for (s = arg; *s; ++s)
6645 {
6646 /*
6647 * Replace "|" with "bar" and '"' with "quote" to match the name of
6648 * the tags for these commands.
6649 * Replace "*" with ".*" and "?" with "." to match command line
6650 * completion.
6651 * Insert a backslash before '~', '$' and '.' to avoid their
6652 * special meaning.
6653 */
6654 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6655 break;
6656 switch (*s)
6657 {
6658 case '|': STRCPY(d, "bar");
6659 d += 3;
6660 continue;
6661 case '"': STRCPY(d, "quote");
6662 d += 5;
6663 continue;
6664 case '*': *d++ = '.';
6665 break;
6666 case '?': *d++ = '.';
6667 continue;
6668 case '$':
6669 case '.':
6670 case '~': *d++ = '\\';
6671 break;
6672 }
6673
6674 /*
6675 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6676 * ":help i_^_CTRL-D" work.
6677 * Insert '-' before and after "CTRL-X" when applicable.
6678 */
6679 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6680 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6681 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006682 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6683 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 STRCPY(d, "CTRL-");
6685 d += 5;
6686 if (*s < ' ')
6687 {
6688#ifdef EBCDIC
6689 *d++ = CtrlChar(*s);
6690#else
6691 *d++ = *s + '@';
6692#endif
6693 if (d[-1] == '\\')
6694 *d++ = '\\'; /* double a backslash */
6695 }
6696 else
6697 *d++ = *++s;
6698 if (s[1] != NUL && s[1] != '_')
6699 *d++ = '_'; /* append a '_' */
6700 continue;
6701 }
6702 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6703 *d++ = '\\';
6704
6705 /*
6706 * Insert a backslash before a backslash after a slash, for search
6707 * pattern tags: "/\|" --> "/\\|".
6708 */
6709 else if (s[0] == '\\' && s[1] != '\\'
6710 && *arg == '/' && s == arg + 1)
6711 *d++ = '\\';
6712
6713 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6714 * "CTRL-\_CTRL-N" */
6715 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6716 {
6717 STRCPY(d, "CTRL-\\\\");
6718 d += 7;
6719 s += 6;
6720 }
6721
6722 *d++ = *s;
6723
6724 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006725 * If tag contains "({" or "([", tag terminates at the "(".
6726 * This is for help on functions, e.g.: abs({expr}).
6727 */
6728 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6729 break;
6730
6731 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * If tag starts with ', toss everything after a second '. Fixes
6733 * CTRL-] on 'option'. (would include the trailing '.').
6734 */
6735 if (*s == '\'' && s > arg && *arg == '\'')
6736 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006737 /* Also '{' and '}'. */
6738 if (*s == '}' && s > arg && *arg == '{')
6739 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006742
6743 if (*IObuff == '`')
6744 {
6745 if (d > IObuff + 2 && d[-1] == '`')
6746 {
6747 /* remove the backticks from `command` */
6748 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6749 d[-2] = NUL;
6750 }
6751 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6752 {
6753 /* remove the backticks and comma from `command`, */
6754 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6755 d[-3] = NUL;
6756 }
6757 else if (d > IObuff + 4 && d[-3] == '`'
6758 && d[-2] == '\\' && d[-1] == '.')
6759 {
6760 /* remove the backticks and dot from `command`\. */
6761 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6762 d[-4] = NUL;
6763 }
6764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
6766 }
6767
6768 *matches = (char_u **)"";
6769 *num_matches = 0;
6770 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6771 if (keep_lang)
6772 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006773 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 /* Sort the matches found on the heuristic number that is after the
6777 * tag name. */
6778 qsort((void *)*matches, (size_t)*num_matches,
6779 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006780 /* Delete more than TAG_MANY to reduce the size of the listing. */
6781 while (*num_matches > TAG_MANY)
6782 vim_free((*matches)[--*num_matches]);
6783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 return OK;
6785}
6786
6787/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006788 * Called when starting to edit a buffer for a help file.
6789 */
6790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006791prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006792{
6793 char_u *p;
6794
6795 curbuf->b_help = TRUE;
6796#ifdef FEAT_QUICKFIX
6797 set_string_option_direct((char_u *)"buftype", -1,
6798 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6799#endif
6800
6801 /*
6802 * Always set these options after jumping to a help tag, because the
6803 * user may have an autocommand that gets in the way.
6804 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6805 * latin1 word characters (for translated help files).
6806 * Only set it when needed, buf_init_chartab() is some work.
6807 */
6808 p =
6809#ifdef EBCDIC
6810 (char_u *)"65-255,^*,^|,^\"";
6811#else
6812 (char_u *)"!-~,^*,^|,^\",192-255";
6813#endif
6814 if (STRCMP(curbuf->b_p_isk, p) != 0)
6815 {
6816 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6817 check_buf_options(curbuf);
6818 (void)buf_init_chartab(curbuf, FALSE);
6819 }
6820
Bram Moolenaar0b105412014-11-30 13:34:23 +01006821#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006822 /* Don't use the global foldmethod.*/
6823 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6824 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006825#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006826
6827 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6828 curwin->w_p_list = FALSE; /* no list mode */
6829
6830 curbuf->b_p_ma = FALSE; /* not modifiable */
6831 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6832 curwin->w_p_nu = 0; /* no line numbers */
6833 curwin->w_p_rnu = 0; /* no relative line numbers */
6834 RESET_BINDING(curwin); /* no scroll or cursor binding */
6835#ifdef FEAT_ARABIC
6836 curwin->w_p_arab = FALSE; /* no arabic mode */
6837#endif
6838#ifdef FEAT_RIGHTLEFT
6839 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6840#endif
6841#ifdef FEAT_FOLDING
6842 curwin->w_p_fen = FALSE; /* No folding in the help window */
6843#endif
6844#ifdef FEAT_DIFF
6845 curwin->w_p_diff = FALSE; /* No 'diff' */
6846#endif
6847#ifdef FEAT_SPELL
6848 curwin->w_p_spell = FALSE; /* No spell checking */
6849#endif
6850
6851 set_buflisted(FALSE);
6852}
6853
6854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 * After reading a help file: May cleanup a help buffer when syntax
6856 * highlighting is not used.
6857 */
6858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006859fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
6861 linenr_T lnum;
6862 char_u *line;
6863 int in_example = FALSE;
6864 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006865 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 char_u *p;
6867 char_u *rt;
6868 int mustfree;
6869
Bram Moolenaar90492982017-06-22 14:16:31 +02006870 /* Set filetype to "help" if still needed. */
6871 if (STRCMP(curbuf->b_p_ft, "help") != 0)
Bram Moolenaar18141832017-06-25 21:17:25 +02006872 {
6873 ++curbuf_lock;
Bram Moolenaar90492982017-06-22 14:16:31 +02006874 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
Bram Moolenaar18141832017-06-25 21:17:25 +02006875 --curbuf_lock;
6876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006879 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880#endif
6881 {
6882 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6883 {
6884 line = ml_get_buf(curbuf, lnum, FALSE);
6885 len = (int)STRLEN(line);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006886 if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
6888 /* End of example: non-white or '<' in first column. */
6889 if (line[0] == '<')
6890 {
6891 /* blank-out a '<' in the first column */
6892 line = ml_get_buf(curbuf, lnum, TRUE);
6893 line[0] = ' ';
6894 }
6895 in_example = FALSE;
6896 }
6897 if (!in_example && len > 0)
6898 {
6899 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6900 {
6901 /* blank-out a '>' in the last column (start of example) */
6902 line = ml_get_buf(curbuf, lnum, TRUE);
6903 line[len - 1] = ' ';
6904 in_example = TRUE;
6905 }
6906 else if (line[len - 1] == '~')
6907 {
6908 /* blank-out a '~' at the end of line (header marker) */
6909 line = ml_get_buf(curbuf, lnum, TRUE);
6910 line[len - 1] = ' ';
6911 }
6912 }
6913 }
6914 }
6915
6916 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006917 * In the "help.txt" and "help.abx" file, add the locally added help
6918 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006920 fname = gettail(curbuf->b_fname);
6921 if (fnamecmp(fname, "help.txt") == 0
6922#ifdef FEAT_MULTI_LANG
6923 || (fnamencmp(fname, "help.", 5) == 0
6924 && ASCII_ISALPHA(fname[5])
6925 && ASCII_ISALPHA(fname[6])
6926 && TOLOWER_ASC(fname[7]) == 'x'
6927 && fname[8] == NUL)
6928#endif
6929 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
6931 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6932 {
6933 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006934 if (strstr((char *)line, "*local-additions*") == NULL)
6935 continue;
6936
6937 /* Go through all directories in 'runtimepath', skipping
6938 * $VIMRUNTIME. */
6939 p = p_rtp;
6940 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006942 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6943 mustfree = FALSE;
6944 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
Bram Moolenaare4db7ae2018-02-13 13:12:11 +01006945 if (rt != NULL && fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006947 int fcount;
6948 char_u **fnames;
6949 FILE *fd;
6950 char_u *s;
6951 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006953 vimconv_T vc;
6954 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955#endif
6956
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006957 /* Find all "doc/ *.txt" files in this directory. */
6958 add_pathsep(NameBuff);
6959#ifdef FEAT_MULTI_LANG
6960 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006962 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006964 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6965 &fnames, EW_FILE|EW_SILENT) == OK
6966 && fcount > 0)
6967 {
6968#ifdef FEAT_MULTI_LANG
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006969 int i1, i2;
6970 char_u *f1, *f2;
6971 char_u *t1, *t2;
6972 char_u *e1, *e2;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006973
6974 /* If foo.abx is found use it instead of foo.txt in
6975 * the same directory. */
6976 for (i1 = 0; i1 < fcount; ++i1)
6977 {
6978 for (i2 = 0; i2 < fcount; ++i2)
6979 {
6980 if (i1 == i2)
6981 continue;
6982 if (fnames[i1] == NULL || fnames[i2] == NULL)
6983 continue;
6984 f1 = fnames[i1];
6985 f2 = fnames[i2];
6986 t1 = gettail(f1);
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006987 t2 = gettail(f2);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006988 e1 = vim_strrchr(t1, '.');
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006989 e2 = vim_strrchr(t2, '.');
Bram Moolenaar7756e742016-10-21 20:35:37 +02006990 if (e1 == NULL || e2 == NULL)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006991 continue;
6992 if (fnamecmp(e1, ".txt") != 0
6993 && fnamecmp(e1, fname + 4) != 0)
6994 {
6995 /* Not .txt and not .abx, remove it. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01006996 VIM_CLEAR(fnames[i1]);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006997 continue;
6998 }
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006999 if (e1 - f1 != e2 - f2
7000 || fnamencmp(f1, f2, e1 - f1) != 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007001 continue;
7002 if (fnamecmp(e1, ".txt") == 0
7003 && fnamecmp(e2, fname + 4) == 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007004 /* use .abx instead of .txt */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007005 VIM_CLEAR(fnames[i1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007008#endif
7009 for (fi = 0; fi < fcount; ++fi)
7010 {
7011 if (fnames[fi] == NULL)
7012 continue;
7013 fd = mch_fopen((char *)fnames[fi], "r");
7014 if (fd != NULL)
7015 {
7016 vim_fgets(IObuff, IOSIZE, fd);
7017 if (IObuff[0] == '*'
7018 && (s = vim_strchr(IObuff + 1, '*'))
7019 != NULL)
7020 {
7021#ifdef FEAT_MBYTE
7022 int this_utf = MAYBE;
7023#endif
7024 /* Change tag definition to a
7025 * reference and remove <CR>/<NL>. */
7026 IObuff[0] = '|';
7027 *s = '|';
7028 while (*s != NUL)
7029 {
7030 if (*s == '\r' || *s == '\n')
7031 *s = NUL;
7032#ifdef FEAT_MBYTE
7033 /* The text is utf-8 when a byte
7034 * above 127 is found and no
7035 * illegal byte sequence is found.
7036 */
7037 if (*s >= 0x80 && this_utf != FALSE)
7038 {
7039 int l;
7040
7041 this_utf = TRUE;
7042 l = utf_ptr2len(s);
7043 if (l == 1)
7044 this_utf = FALSE;
7045 s += l - 1;
7046 }
7047#endif
7048 ++s;
7049 }
7050#ifdef FEAT_MBYTE
7051 /* The help file is latin1 or utf-8;
7052 * conversion to the current
7053 * 'encoding' may be required. */
7054 vc.vc_type = CONV_NONE;
7055 convert_setup(&vc, (char_u *)(
7056 this_utf == TRUE ? "utf-8"
7057 : "latin1"), p_enc);
7058 if (vc.vc_type == CONV_NONE)
7059 /* No conversion needed. */
7060 cp = IObuff;
7061 else
7062 {
7063 /* Do the conversion. If it fails
7064 * use the unconverted text. */
7065 cp = string_convert(&vc, IObuff,
7066 NULL);
7067 if (cp == NULL)
7068 cp = IObuff;
7069 }
7070 convert_setup(&vc, NULL, NULL);
7071
7072 ml_append(lnum, cp, (colnr_T)0, FALSE);
7073 if (cp != IObuff)
7074 vim_free(cp);
7075#else
7076 ml_append(lnum, IObuff, (colnr_T)0,
7077 FALSE);
7078#endif
7079 ++lnum;
7080 }
7081 fclose(fd);
7082 }
7083 }
7084 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007087 if (mustfree)
7088 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007090 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 }
7092 }
7093}
7094
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007095/*
7096 * ":exusage"
7097 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007099ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007100{
7101 do_cmdline_cmd((char_u *)"help ex-cmd-index");
7102}
7103
7104/*
7105 * ":viusage"
7106 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007109{
7110 do_cmdline_cmd((char_u *)"help normal-index");
7111}
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007114 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007117helptags_one(
7118 char_u *dir, /* doc directory */
7119 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
7120 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
7121 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 FILE *fd_tags;
7124 FILE *fd;
7125 garray_T ga;
7126 int filecount;
7127 char_u **files;
7128 char_u *p1, *p2;
7129 int fi;
7130 char_u *s;
7131 int i;
7132 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007133 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134# ifdef FEAT_MBYTE
7135 int utf8 = MAYBE;
7136 int this_utf8;
7137 int firstline;
7138 int mix = FALSE; /* detected mixed encodings */
7139# endif
7140
7141 /*
7142 * Find all *.txt files.
7143 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007144 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007146 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 STRCAT(NameBuff, ext);
7148 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7149 EW_FILE|EW_SILENT) == FAIL
7150 || filecount == 0)
7151 {
7152 if (!got_int)
Bram Moolenaar5b302912016-08-24 22:11:55 +02007153 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 return;
7155 }
7156
7157 /*
7158 * Open the tags file for writing.
7159 * Do this before scanning through all the files.
7160 */
7161 STRCPY(NameBuff, dir);
7162 add_pathsep(NameBuff);
7163 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007164 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 if (fd_tags == NULL)
7166 {
7167 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
7168 FreeWild(filecount, files);
7169 return;
7170 }
7171
7172 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007173 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7174 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 */
7176 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007177 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
7178 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 if (ga_grow(&ga, 1) == FAIL)
7181 got_int = TRUE;
7182 else
7183 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007184 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (s == NULL)
7186 got_int = TRUE;
7187 else
7188 {
7189 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7190 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7191 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193 }
7194 }
7195
7196 /*
7197 * Go over all the files and extract the tags.
7198 */
7199 for (fi = 0; fi < filecount && !got_int; ++fi)
7200 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007201 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 if (fd == NULL)
7203 {
7204 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
7205 continue;
7206 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007207 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
7209# ifdef FEAT_MBYTE
7210 firstline = TRUE;
7211# endif
7212 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7213 {
7214# ifdef FEAT_MBYTE
7215 if (firstline)
7216 {
7217 /* Detect utf-8 file by a non-ASCII char in the first line. */
7218 this_utf8 = MAYBE;
7219 for (s = IObuff; *s != NUL; ++s)
7220 if (*s >= 0x80)
7221 {
7222 int l;
7223
7224 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007225 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (l == 1)
7227 {
7228 /* Illegal UTF-8 byte sequence. */
7229 this_utf8 = FALSE;
7230 break;
7231 }
7232 s += l - 1;
7233 }
7234 if (this_utf8 == MAYBE) /* only ASCII characters found */
7235 this_utf8 = FALSE;
7236 if (utf8 == MAYBE) /* first file */
7237 utf8 = this_utf8;
7238 else if (utf8 != this_utf8)
7239 {
7240 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
7241 mix = !got_int;
7242 got_int = TRUE;
7243 }
7244 firstline = FALSE;
7245 }
7246# endif
7247 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7248 while (p1 != NULL)
7249 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007250 /* Use vim_strbyte() instead of vim_strchr() so that when
7251 * 'encoding' is dbcs it still works, don't find '*' in the
7252 * second byte. */
7253 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7255 {
7256 for (s = p1 + 1; s < p2; ++s)
7257 if (*s == ' ' || *s == '\t' || *s == '|')
7258 break;
7259
7260 /*
7261 * Only accept a *tag* when it consists of valid
7262 * characters, there is white space before it and is
7263 * followed by a white character or end-of-line.
7264 */
7265 if (s == p2
7266 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7267 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7268 || s[1] == '\0'))
7269 {
7270 *p2 = '\0';
7271 ++p1;
7272 if (ga_grow(&ga, 1) == FAIL)
7273 {
7274 got_int = TRUE;
7275 break;
7276 }
7277 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7278 if (s == NULL)
7279 {
7280 got_int = TRUE;
7281 break;
7282 }
7283 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7284 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 sprintf((char *)s, "%s\t%s", p1, fname);
7286
7287 /* find next '*' */
7288 p2 = vim_strchr(p2 + 1, '*');
7289 }
7290 }
7291 p1 = p2;
7292 }
7293 line_breakcheck();
7294 }
7295
7296 fclose(fd);
7297 }
7298
7299 FreeWild(filecount, files);
7300
7301 if (!got_int)
7302 {
7303 /*
7304 * Sort the tags.
7305 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007306 if (ga.ga_data != NULL)
7307 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 /*
7310 * Check for duplicates.
7311 */
7312 for (i = 1; i < ga.ga_len; ++i)
7313 {
7314 p1 = ((char_u **)ga.ga_data)[i - 1];
7315 p2 = ((char_u **)ga.ga_data)[i];
7316 while (*p1 == *p2)
7317 {
7318 if (*p2 == '\t')
7319 {
7320 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007321 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7323 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
7324 EMSG(NameBuff);
7325 *p2 = '\t';
7326 break;
7327 }
7328 ++p1;
7329 ++p2;
7330 }
7331 }
7332
7333# ifdef FEAT_MBYTE
7334 if (utf8 == TRUE)
7335 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
7336# endif
7337
7338 /*
7339 * Write the tags into the file.
7340 */
7341 for (i = 0; i < ga.ga_len; ++i)
7342 {
7343 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007344 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007346 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 else
7348 {
7349 fprintf(fd_tags, "%s\t/*", s);
7350 for (p1 = s; *p1 != '\t'; ++p1)
7351 {
7352 /* insert backslash before '\\' and '/' */
7353 if (*p1 == '\\' || *p1 == '/')
7354 putc('\\', fd_tags);
7355 putc(*p1, fd_tags);
7356 }
7357 fprintf(fd_tags, "*\n");
7358 }
7359 }
7360 }
7361#ifdef FEAT_MBYTE
7362 if (mix)
7363 got_int = FALSE; /* continue with other languages */
7364#endif
7365
7366 for (i = 0; i < ga.ga_len; ++i)
7367 vim_free(((char_u **)ga.ga_data)[i]);
7368 ga_clear(&ga);
7369 fclose(fd_tags); /* there is no check for an error... */
7370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007372/*
7373 * Generate tags in one help directory, taking care of translations.
7374 */
7375 static void
7376do_helptags(char_u *dirname, int add_help_tags)
7377{
7378#ifdef FEAT_MULTI_LANG
7379 int len;
7380 int i, j;
7381 garray_T ga;
7382 char_u lang[2];
7383 char_u ext[5];
7384 char_u fname[8];
7385 int filecount;
7386 char_u **files;
7387
7388 /* Get a list of all files in the help directory and in subdirectories. */
7389 STRCPY(NameBuff, dirname);
7390 add_pathsep(NameBuff);
7391 STRCAT(NameBuff, "**");
7392 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7393 EW_FILE|EW_SILENT) == FAIL
7394 || filecount == 0)
7395 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007396 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007397 return;
7398 }
7399
7400 /* Go over all files in the directory to find out what languages are
7401 * present. */
7402 ga_init2(&ga, 1, 10);
7403 for (i = 0; i < filecount; ++i)
7404 {
7405 len = (int)STRLEN(files[i]);
7406 if (len > 4)
7407 {
7408 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7409 {
7410 /* ".txt" -> language "en" */
7411 lang[0] = 'e';
7412 lang[1] = 'n';
7413 }
7414 else if (files[i][len - 4] == '.'
7415 && ASCII_ISALPHA(files[i][len - 3])
7416 && ASCII_ISALPHA(files[i][len - 2])
7417 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7418 {
7419 /* ".abx" -> language "ab" */
7420 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7421 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7422 }
7423 else
7424 continue;
7425
7426 /* Did we find this language already? */
7427 for (j = 0; j < ga.ga_len; j += 2)
7428 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7429 break;
7430 if (j == ga.ga_len)
7431 {
7432 /* New language, add it. */
7433 if (ga_grow(&ga, 2) == FAIL)
7434 break;
7435 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7436 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7437 }
7438 }
7439 }
7440
7441 /*
7442 * Loop over the found languages to generate a tags file for each one.
7443 */
7444 for (j = 0; j < ga.ga_len; j += 2)
7445 {
7446 STRCPY(fname, "tags-xx");
7447 fname[5] = ((char_u *)ga.ga_data)[j];
7448 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7449 if (fname[5] == 'e' && fname[6] == 'n')
7450 {
7451 /* English is an exception: use ".txt" and "tags". */
7452 fname[4] = NUL;
7453 STRCPY(ext, ".txt");
7454 }
7455 else
7456 {
7457 /* Language "ab" uses ".abx" and "tags-ab". */
7458 STRCPY(ext, ".xxx");
7459 ext[1] = fname[5];
7460 ext[2] = fname[6];
7461 }
7462 helptags_one(dirname, ext, fname, add_help_tags);
7463 }
7464
7465 ga_clear(&ga);
7466 FreeWild(filecount, files);
7467
7468#else
7469 /* No language support, just use "*.txt" and "tags". */
7470 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7471#endif
7472}
7473
7474 static void
7475helptags_cb(char_u *fname, void *cookie)
7476{
7477 do_helptags(fname, *(int *)cookie);
7478}
7479
7480/*
7481 * ":helptags"
7482 */
7483 void
7484ex_helptags(exarg_T *eap)
7485{
7486 expand_T xpc;
7487 char_u *dirname;
7488 int add_help_tags = FALSE;
7489
7490 /* Check for ":helptags ++t {dir}". */
Bram Moolenaar1c465442017-03-12 20:10:05 +01007491 if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3]))
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007492 {
7493 add_help_tags = TRUE;
7494 eap->arg = skipwhite(eap->arg + 3);
7495 }
7496
7497 if (STRCMP(eap->arg, "ALL") == 0)
7498 {
7499 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7500 helptags_cb, &add_help_tags);
7501 }
7502 else
7503 {
7504 ExpandInit(&xpc);
7505 xpc.xp_context = EXPAND_DIRECTORIES;
7506 dirname = ExpandOne(&xpc, eap->arg, NULL,
7507 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7508 if (dirname == NULL || !mch_isdir(dirname))
7509 EMSG2(_("E150: Not a directory: %s"), eap->arg);
7510 else
7511 do_helptags(dirname, add_help_tags);
7512 vim_free(dirname);
7513 }
7514}
7515
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516#if defined(FEAT_SIGNS) || defined(PROTO)
7517
7518/*
7519 * Struct to hold the sign properties.
7520 */
7521typedef struct sign sign_T;
7522
7523struct sign
7524{
7525 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007526 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 char_u *sn_name; /* name of sign */
7528 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007529# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 char_u *sn_text; /* text used instead of pixmap */
7533 int sn_line_hl; /* highlight ID for line */
7534 int sn_text_hl; /* highlight ID for text */
7535};
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007538static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007540static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
7541static void sign_list_defined(sign_T *sp);
7542static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007544static char *cmds[] = {
7545 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007546# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007547 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007548# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007549 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007550# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007551 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007552# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007553 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007554# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007555 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007556# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007557 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007558# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007559};
7560
7561/*
7562 * Find index of a ":sign" subcmd from its name.
7563 * "*end_cmd" must be writable.
7564 */
7565 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566sign_cmd_idx(
7567 char_u *begin_cmd, /* begin of sign subcmd */
7568 char_u *end_cmd) /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007569{
7570 int idx;
7571 char save = *end_cmd;
7572
7573 *end_cmd = NUL;
7574 for (idx = 0; ; ++idx)
7575 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7576 break;
7577 *end_cmd = save;
7578 return idx;
7579}
7580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581/*
7582 * ":sign" command
7583 */
7584 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007585ex_sign(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
7587 char_u *arg = eap->arg;
7588 char_u *p;
7589 int idx;
7590 sign_T *sp;
7591 sign_T *sp_prev;
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01007592 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
7594 /* Parse the subcommand. */
7595 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007596 idx = sign_cmd_idx(arg, p);
7597 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007599 EMSG2(_("E160: Unknown sign command: %s"), arg);
7600 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 }
7602 arg = skipwhite(p);
7603
7604 if (idx <= SIGNCMD_LIST)
7605 {
7606 /*
7607 * Define, undefine or list signs.
7608 */
7609 if (idx == SIGNCMD_LIST && *arg == NUL)
7610 {
7611 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007612 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 sign_list_defined(sp);
7614 }
7615 else if (*arg == NUL)
7616 EMSG(_("E156: Missing sign name"));
7617 else
7618 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007619 /* Isolate the sign name. If it's a number skip leading zeroes,
7620 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 p = skiptowhite(arg);
7622 if (*p != NUL)
7623 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007624 while (arg[0] == '0' && arg[1] != NUL)
7625 ++arg;
7626
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 sp_prev = NULL;
7628 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7629 {
7630 if (STRCMP(sp->sn_name, arg) == 0)
7631 break;
7632 sp_prev = sp;
7633 }
7634 if (idx == SIGNCMD_DEFINE)
7635 {
7636 /* ":sign define {name} ...": define a sign */
7637 if (sp == NULL)
7638 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007639 sign_T *lp;
7640 int start = next_sign_typenr;
7641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 /* Allocate a new sign. */
7643 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7644 if (sp == NULL)
7645 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007647 /* Check that next_sign_typenr is not already being used.
7648 * This only happens after wrapping around. Hopefully
7649 * another one got deleted and we can use its number. */
7650 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007652 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007654 ++next_sign_typenr;
7655 if (next_sign_typenr == MAX_TYPENR)
7656 next_sign_typenr = 1;
7657 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007659 vim_free(sp);
7660 EMSG(_("E612: Too many signs defined"));
7661 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007663 lp = first_sign; /* start all over */
7664 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007666 lp = lp->sn_next;
7667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007669 sp->sn_typenr = next_sign_typenr;
7670 if (++next_sign_typenr == MAX_TYPENR)
7671 next_sign_typenr = 1; /* wrap around */
7672
7673 sp->sn_name = vim_strsave(arg);
7674 if (sp->sn_name == NULL) /* out of memory */
7675 {
7676 vim_free(sp);
7677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007679
7680 /* add the new sign to the list of signs */
7681 if (sp_prev == NULL)
7682 first_sign = sp;
7683 else
7684 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686
7687 /* set values for a defined sign. */
7688 for (;;)
7689 {
7690 arg = skipwhite(p);
7691 if (*arg == NUL)
7692 break;
7693 p = skiptowhite_esc(arg);
7694 if (STRNCMP(arg, "icon=", 5) == 0)
7695 {
7696 arg += 5;
7697 vim_free(sp->sn_icon);
7698 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7699 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007700# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (gui.in_use)
7702 {
7703 out_flush();
7704 if (sp->sn_image != NULL)
7705 gui_mch_destroy_sign(sp->sn_image);
7706 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7707 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 else if (STRNCMP(arg, "text=", 5) == 0)
7711 {
7712 char_u *s;
7713 int cells;
7714 int len;
7715
7716 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007717# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 /* Count cells and check for non-printable chars */
7719 if (has_mbyte)
7720 {
7721 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007722 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
7724 if (!vim_isprintc((*mb_ptr2char)(s)))
7725 break;
7726 cells += (*mb_ptr2cells)(s);
7727 }
7728 }
7729 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {
7732 for (s = arg; s < p; ++s)
7733 if (!vim_isprintc(*s))
7734 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007735 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
7737 /* Currently must be one or two display cells */
7738 if (s != p || cells < 1 || cells > 2)
7739 {
7740 *p = NUL;
7741 EMSG2(_("E239: Invalid sign text: %s"), arg);
7742 return;
7743 }
7744
7745 vim_free(sp->sn_text);
7746 /* Allocate one byte more if we need to pad up
7747 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007748 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 sp->sn_text = vim_strnsave(arg, len);
7750
7751 if (sp->sn_text != NULL && cells == 1)
7752 STRCPY(sp->sn_text + len - 1, " ");
7753 }
7754 else if (STRNCMP(arg, "linehl=", 7) == 0)
7755 {
7756 arg += 7;
7757 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7758 }
7759 else if (STRNCMP(arg, "texthl=", 7) == 0)
7760 {
7761 arg += 7;
7762 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7763 }
7764 else
7765 {
7766 EMSG2(_(e_invarg2), arg);
7767 return;
7768 }
7769 }
7770 }
7771 else if (sp == NULL)
7772 EMSG2(_("E155: Unknown sign: %s"), arg);
7773 else if (idx == SIGNCMD_LIST)
7774 /* ":sign list {name}" */
7775 sign_list_defined(sp);
7776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007778 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780 }
7781 else
7782 {
7783 int id = -1;
7784 linenr_T lnum = -1;
7785 char_u *sign_name = NULL;
7786 char_u *arg1;
7787
7788 if (*arg == NUL)
7789 {
7790 if (idx == SIGNCMD_PLACE)
7791 {
7792 /* ":sign place": list placed signs in all buffers */
7793 sign_list_placed(NULL);
7794 }
7795 else if (idx == SIGNCMD_UNPLACE)
7796 {
7797 /* ":sign unplace": remove placed sign at cursor */
7798 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7799 if (id > 0)
7800 {
7801 buf_delsign(curwin->w_buffer, id);
7802 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7803 }
7804 else
7805 EMSG(_("E159: Missing sign number"));
7806 }
7807 else
7808 EMSG(_(e_argreq));
7809 return;
7810 }
7811
7812 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7813 {
7814 /* ":sign unplace *": remove all placed signs */
7815 buf_delete_all_signs();
7816 return;
7817 }
7818
7819 /* first arg could be placed sign id */
7820 arg1 = arg;
7821 if (VIM_ISDIGIT(*arg))
7822 {
7823 id = getdigits(&arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007824 if (!VIM_ISWHITE(*arg) && *arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
7826 id = -1;
7827 arg = arg1;
7828 }
7829 else
7830 {
7831 arg = skipwhite(arg);
7832 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7833 {
7834 /* ":sign unplace {id}": remove placed sign by number */
Bram Moolenaar29323592016-07-24 22:04:11 +02007835 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if ((lnum = buf_delsign(buf, id)) != 0)
7837 update_debug_sign(buf, lnum);
7838 return;
7839 }
7840 }
7841 }
7842
7843 /*
7844 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7845 * Leave "arg" pointing to {fname}.
7846 */
7847 for (;;)
7848 {
7849 if (STRNCMP(arg, "line=", 5) == 0)
7850 {
7851 arg += 5;
7852 lnum = atoi((char *)arg);
7853 arg = skiptowhite(arg);
7854 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007855 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7856 {
7857 if (id != -1)
7858 {
7859 EMSG(_(e_invarg));
7860 return;
7861 }
7862 id = -2;
7863 arg = skiptowhite(arg + 1);
7864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 else if (STRNCMP(arg, "name=", 5) == 0)
7866 {
7867 arg += 5;
7868 sign_name = arg;
7869 arg = skiptowhite(arg);
7870 if (*arg != NUL)
7871 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007872 while (sign_name[0] == '0' && sign_name[1] != NUL)
7873 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875 else if (STRNCMP(arg, "file=", 5) == 0)
7876 {
7877 arg += 5;
7878 buf = buflist_findname(arg);
7879 break;
7880 }
7881 else if (STRNCMP(arg, "buffer=", 7) == 0)
7882 {
7883 arg += 7;
7884 buf = buflist_findnr((int)getdigits(&arg));
7885 if (*skipwhite(arg) != NUL)
7886 EMSG(_(e_trailing));
7887 break;
7888 }
7889 else
7890 {
7891 EMSG(_(e_invarg));
7892 return;
7893 }
7894 arg = skipwhite(arg);
7895 }
7896
7897 if (buf == NULL)
7898 {
7899 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7900 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007901 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
7903 if (lnum >= 0 || sign_name != NULL)
7904 EMSG(_(e_invarg));
7905 else
7906 /* ":sign place file={fname}": list placed signs in one file */
7907 sign_list_placed(buf);
7908 }
7909 else if (idx == SIGNCMD_JUMP)
7910 {
7911 /* ":sign jump {id} file={fname}" */
7912 if (lnum >= 0 || sign_name != NULL)
7913 EMSG(_(e_invarg));
7914 else if ((lnum = buf_findsign(buf, id)) > 0)
7915 { /* goto a sign ... */
7916 if (buf_jump_open_win(buf) != NULL)
7917 { /* ... in a current window */
7918 curwin->w_cursor.lnum = lnum;
7919 check_cursor_lnum();
7920 beginline(BL_WHITE);
7921 }
7922 else
7923 { /* ... not currently in a window */
7924 char_u *cmd;
7925
Bram Moolenaarbfd096d2016-08-17 22:29:09 +02007926 if (buf->b_fname == NULL)
7927 {
7928 EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
7929 return;
7930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7932 if (cmd == NULL)
7933 return;
7934 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7935 do_cmdline_cmd(cmd);
7936 vim_free(cmd);
7937 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007938# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 }
7942 else
7943 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7944 }
7945 else if (idx == SIGNCMD_UNPLACE)
7946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 if (lnum >= 0 || sign_name != NULL)
7948 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007949 else if (id == -2)
7950 {
7951 /* ":sign unplace * file={fname}" */
7952 redraw_buf_later(buf, NOT_VALID);
7953 buf_delete_signs(buf);
7954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 else
7956 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007957 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 lnum = buf_delsign(buf, id);
7959 update_debug_sign(buf, lnum);
7960 }
7961 }
7962 /* idx == SIGNCMD_PLACE */
7963 else if (sign_name != NULL)
7964 {
7965 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7966 if (STRCMP(sp->sn_name, sign_name) == 0)
7967 break;
7968 if (sp == NULL)
7969 {
7970 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7971 return;
7972 }
7973 if (lnum > 0)
7974 /* ":sign place {id} line={lnum} name={name} file={fname}":
7975 * place a sign */
7976 buf_addsign(buf, id, lnum, sp->sn_typenr);
7977 else
7978 /* ":sign place {id} file={fname}": change sign type */
7979 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007980 if (lnum > 0)
7981 update_debug_sign(buf, lnum);
7982 else
7983 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 }
7985 else
7986 EMSG(_(e_invarg));
7987 }
7988}
7989
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007990# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991/*
7992 * Allocate the icons. Called when the GUI has started. Allows defining
7993 * signs before it starts.
7994 */
7995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996sign_gui_started(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 sign_T *sp;
7999
8000 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8001 if (sp->sn_icon != NULL)
8002 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
8003}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
8006/*
8007 * List one sign.
8008 */
8009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008010sign_list_defined(sign_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 char_u *p;
8013
Bram Moolenaar555b2802005-05-19 21:08:39 +00008014 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (sp->sn_icon != NULL)
8016 {
8017 MSG_PUTS(" icon=");
8018 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008019# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 if (sp->sn_image == NULL)
8021 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008022# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026 if (sp->sn_text != NULL)
8027 {
8028 MSG_PUTS(" text=");
8029 msg_outtrans(sp->sn_text);
8030 }
8031 if (sp->sn_line_hl > 0)
8032 {
8033 MSG_PUTS(" linehl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008034 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (p == NULL)
8036 MSG_PUTS("NONE");
8037 else
8038 msg_puts(p);
8039 }
8040 if (sp->sn_text_hl > 0)
8041 {
8042 MSG_PUTS(" texthl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008043 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (p == NULL)
8045 MSG_PUTS("NONE");
8046 else
8047 msg_puts(p);
8048 }
8049}
8050
8051/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008052 * Undefine a sign and free its memory.
8053 */
8054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055sign_undefine(sign_T *sp, sign_T *sp_prev)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008056{
8057 vim_free(sp->sn_name);
8058 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008059# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008060 if (sp->sn_image != NULL)
8061 {
8062 out_flush();
8063 gui_mch_destroy_sign(sp->sn_image);
8064 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008065# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008066 vim_free(sp->sn_text);
8067 if (sp_prev == NULL)
8068 first_sign = sp->sn_next;
8069 else
8070 sp_prev->sn_next = sp->sn_next;
8071 vim_free(sp);
8072}
8073
8074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 * Get highlighting attribute for sign "typenr".
8076 * If "line" is TRUE: line highl, if FALSE: text highl.
8077 */
8078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079sign_get_attr(int typenr, int line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 sign_T *sp;
8082
8083 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8084 if (sp->sn_typenr == typenr)
8085 {
8086 if (line)
8087 {
8088 if (sp->sn_line_hl > 0)
8089 return syn_id2attr(sp->sn_line_hl);
8090 }
8091 else
8092 {
8093 if (sp->sn_text_hl > 0)
8094 return syn_id2attr(sp->sn_text_hl);
8095 }
8096 break;
8097 }
8098 return 0;
8099}
8100
8101/*
8102 * Get text mark for sign "typenr".
8103 * Returns NULL if there isn't one.
8104 */
8105 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008106sign_get_text(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 sign_T *sp;
8109
8110 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8111 if (sp->sn_typenr == typenr)
8112 return sp->sn_text;
8113 return NULL;
8114}
8115
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008116# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008118sign_get_image(
8119 int typenr) /* the attribute which may have a sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 sign_T *sp;
8122
8123 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8124 if (sp->sn_typenr == typenr)
8125 return sp->sn_image;
8126 return NULL;
8127}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129
8130/*
8131 * Get the name of a sign by its typenr.
8132 */
8133 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134sign_typenr2name(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 sign_T *sp;
8137
8138 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8139 if (sp->sn_typenr == typenr)
8140 return sp->sn_name;
8141 return (char_u *)_("[Deleted]");
8142}
8143
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008144# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008145/*
8146 * Undefine/free all signs.
8147 */
8148 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149free_signs(void)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008150{
8151 while (first_sign != NULL)
8152 sign_undefine(first_sign, NULL);
8153}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008154# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008155
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008156# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008157static enum
8158{
8159 EXP_SUBCMD, /* expand :sign sub-commands */
8160 EXP_DEFINE, /* expand :sign define {name} args */
8161 EXP_PLACE, /* expand :sign place {id} args */
8162 EXP_UNPLACE, /* expand :sign unplace" */
8163 EXP_SIGN_NAMES /* expand with name of placed signs */
8164} expand_what;
8165
8166/*
8167 * Function given to ExpandGeneric() to obtain the sign command
8168 * expansion.
8169 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008170 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171get_sign_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008172{
8173 sign_T *sp;
8174 int current_idx;
8175
8176 switch (expand_what)
8177 {
8178 case EXP_SUBCMD:
8179 return (char_u *)cmds[idx];
8180 case EXP_DEFINE:
8181 {
8182 char *define_arg[] =
8183 {
8184 "icon=", "linehl=", "text=", "texthl=", NULL
8185 };
8186 return (char_u *)define_arg[idx];
8187 }
8188 case EXP_PLACE:
8189 {
8190 char *place_arg[] =
8191 {
8192 "line=", "name=", "file=", "buffer=", NULL
8193 };
8194 return (char_u *)place_arg[idx];
8195 }
8196 case EXP_UNPLACE:
8197 {
8198 char *unplace_arg[] = { "file=", "buffer=", NULL };
8199 return (char_u *)unplace_arg[idx];
8200 }
8201 case EXP_SIGN_NAMES:
8202 /* Complete with name of signs already defined */
8203 current_idx = 0;
8204 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8205 if (current_idx++ == idx)
8206 return sp->sn_name;
8207 return NULL;
8208 default:
8209 return NULL;
8210 }
8211}
8212
8213/*
8214 * Handle command line completion for :sign command.
8215 */
8216 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008217set_context_in_sign_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008218{
8219 char_u *p;
8220 char_u *end_subcmd;
8221 char_u *last;
8222 int cmd_idx;
8223 char_u *begin_subcmd_args;
8224
8225 /* Default: expand subcommands. */
8226 xp->xp_context = EXPAND_SIGN;
8227 expand_what = EXP_SUBCMD;
8228 xp->xp_pattern = arg;
8229
8230 end_subcmd = skiptowhite(arg);
8231 if (*end_subcmd == NUL)
8232 /* expand subcmd name
8233 * :sign {subcmd}<CTRL-D>*/
8234 return;
8235
8236 cmd_idx = sign_cmd_idx(arg, end_subcmd);
8237
8238 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008239 * |
8240 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008241 begin_subcmd_args = skipwhite(end_subcmd);
8242 p = skiptowhite(begin_subcmd_args);
8243 if (*p == NUL)
8244 {
8245 /*
8246 * Expand first argument of subcmd when possible.
8247 * For ":jump {id}" and ":unplace {id}", we could
8248 * possibly expand the ids of all signs already placed.
8249 */
8250 xp->xp_pattern = begin_subcmd_args;
8251 switch (cmd_idx)
8252 {
8253 case SIGNCMD_LIST:
8254 case SIGNCMD_UNDEFINE:
8255 /* :sign list <CTRL-D>
8256 * :sign undefine <CTRL-D> */
8257 expand_what = EXP_SIGN_NAMES;
8258 break;
8259 default:
8260 xp->xp_context = EXPAND_NOTHING;
8261 }
8262 return;
8263 }
8264
8265 /* expand last argument of subcmd */
8266
8267 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008268 * |
8269 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008270
8271 /* Loop until reaching last argument. */
8272 do
8273 {
8274 p = skipwhite(p);
8275 last = p;
8276 p = skiptowhite(p);
8277 } while (*p != NUL);
8278
8279 p = vim_strchr(last, '=');
8280
8281 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008282 * | |
8283 * last p */
Bram Moolenaar7756e742016-10-21 20:35:37 +02008284 if (p == NULL)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008285 {
8286 /* Expand last argument name (before equal sign). */
8287 xp->xp_pattern = last;
8288 switch (cmd_idx)
8289 {
8290 case SIGNCMD_DEFINE:
8291 expand_what = EXP_DEFINE;
8292 break;
8293 case SIGNCMD_PLACE:
8294 expand_what = EXP_PLACE;
8295 break;
8296 case SIGNCMD_JUMP:
8297 case SIGNCMD_UNPLACE:
8298 expand_what = EXP_UNPLACE;
8299 break;
8300 default:
8301 xp->xp_context = EXPAND_NOTHING;
8302 }
8303 }
8304 else
8305 {
8306 /* Expand last argument value (after equal sign). */
8307 xp->xp_pattern = p + 1;
8308 switch (cmd_idx)
8309 {
8310 case SIGNCMD_DEFINE:
8311 if (STRNCMP(last, "texthl", p - last) == 0 ||
8312 STRNCMP(last, "linehl", p - last) == 0)
8313 xp->xp_context = EXPAND_HIGHLIGHT;
8314 else if (STRNCMP(last, "icon", p - last) == 0)
8315 xp->xp_context = EXPAND_FILES;
8316 else
8317 xp->xp_context = EXPAND_NOTHING;
8318 break;
8319 case SIGNCMD_PLACE:
8320 if (STRNCMP(last, "name", p - last) == 0)
8321 expand_what = EXP_SIGN_NAMES;
8322 else
8323 xp->xp_context = EXPAND_NOTHING;
8324 break;
8325 default:
8326 xp->xp_context = EXPAND_NOTHING;
8327 }
8328 }
8329}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008330# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008331#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008332
8333/*
8334 * Make the user happy.
8335 */
8336 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008337ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008338{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008339 static char *code[] = {
8340 "\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$",
8341 "\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"
8342 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008343 char *p;
8344 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008345 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008346
8347 msg_start();
8348 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008349 for (i = 0; i < 2; ++i)
8350 for (p = code[i]; *p != NUL; ++p)
8351 if (*p == 'x')
8352 msg_putchar('\n');
8353 else
8354 for (n = *p++; n > 0; --n)
8355 if (*p == 'o' || *p == '$')
Bram Moolenaar8820b482017-03-16 17:23:31 +01008356 msg_putchar_attr(*p, HL_ATTR(HLF_L));
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008357 else
8358 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008359 msg_clr_eos();
8360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362/*
8363 * ":drop"
8364 * Opens the first argument in a window. When there are two or more arguments
8365 * the argument list is redefined.
8366 */
8367 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008368ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 win_T *wp;
8372 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008373 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374
8375 /*
8376 * Check if the first argument is already being edited in a window. If
8377 * so, jump to that window.
8378 * We would actually need to check all arguments, but that's complicated
8379 * and mostly only one file is dropped.
8380 * This also ignores wildcards, since it is very unlikely the user is
8381 * editing a file name with a wildcard character.
8382 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008383 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00008385 /*
8386 * Expanding wildcards may result in an empty argument list. E.g. when
8387 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
8388 * already did an error message for this.
8389 */
8390 if (ARGCOUNT == 0)
8391 return;
8392
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008393 if (cmdmod.tab)
8394 {
8395 /* ":tab drop file ...": open a tab for each argument that isn't
8396 * edited in a window yet. It's like ":tab all" but without closing
8397 * windows or tabs. */
8398 ex_all(eap);
8399 }
8400 else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008401 {
8402 /* ":drop file ...": Edit the first argument. Jump to an existing
8403 * window if possible, edit in current window if the current buffer
8404 * can be abandoned, otherwise open a new window. */
8405 buf = buflist_findnr(ARGLIST[0].ae_fnum);
8406
8407 FOR_ALL_TAB_WINDOWS(tp, wp)
8408 {
8409 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008411 goto_tabpage_win(tp, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 return;
8414 }
8415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008417 /*
8418 * Check whether the current buffer is changed. If so, we will need
8419 * to split the current window or data could be lost.
8420 * Skip the check if the 'hidden' option is set, as in this case the
8421 * buffer won't be lost.
8422 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02008423 if (!buf_hide(curbuf))
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008424 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008425 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008426 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008427 --emsg_off;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008430 /* Fake a ":sfirst" or ":first" command edit the first argument. */
8431 if (split)
8432 {
8433 eap->cmdidx = CMD_sfirst;
8434 eap->cmd[0] = 's';
8435 }
8436 else
8437 eap->cmdidx = CMD_first;
8438 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440}
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008441
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008442/*
8443 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
8444 * Put the start of the pattern in "*s", unless "s" is NULL.
8445 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
8446 * If "s" is not NULL terminate the pattern with a NUL.
8447 * Return a pointer to the char just past the pattern plus flags.
8448 */
8449 char_u *
8450skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
8451{
8452 int c;
8453
8454 if (vim_isIDc(*p))
8455 {
8456 /* ":vimgrep pattern fname" */
8457 if (s != NULL)
8458 *s = p;
8459 p = skiptowhite(p);
8460 if (s != NULL && *p != NUL)
8461 *p++ = NUL;
8462 }
8463 else
8464 {
8465 /* ":vimgrep /pattern/[g][j] fname" */
8466 if (s != NULL)
8467 *s = p + 1;
8468 c = *p;
8469 p = skip_regexp(p + 1, c, TRUE, NULL);
8470 if (*p != c)
8471 return NULL;
8472
8473 /* Truncate the pattern. */
8474 if (s != NULL)
8475 *p = NUL;
8476 ++p;
8477
8478 /* Find the flags */
8479 while (*p == 'g' || *p == 'j')
8480 {
8481 if (flags != NULL)
8482 {
8483 if (*p == 'g')
8484 *flags |= VGR_GLOBAL;
8485 else
8486 *flags |= VGR_NOJUMP;
8487 }
8488 ++p;
8489 }
8490 }
8491 return p;
8492}
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008493
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008494#if defined(FEAT_EVAL) || defined(PROTO)
8495/*
8496 * List v:oldfiles in a nice way.
8497 */
8498 void
8499ex_oldfiles(exarg_T *eap UNUSED)
8500{
8501 list_T *l = get_vim_var_list(VV_OLDFILES);
8502 listitem_T *li;
8503 int nr = 0;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008504 char_u *fname;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008505
8506 if (l == NULL)
8507 msg((char_u *)_("No old files"));
8508 else
8509 {
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008510 msg_start();
8511 msg_scroll = TRUE;
8512 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8513 {
8514 ++nr;
8515 fname = get_tv_string(&li->li_tv);
Bram Moolenaard6f2ee32016-08-24 00:30:52 +02008516 if (!message_filtered(fname))
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008517 {
8518 msg_outnum((long)nr);
8519 MSG_PUTS(": ");
8520 msg_outtrans(fname);
Bram Moolenaar885c00e2016-08-28 17:08:17 +02008521 msg_clr_eos();
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008522 msg_putchar('\n');
8523 out_flush(); /* output one line at a time */
8524 ui_breakcheck();
8525 }
8526 }
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008527
8528 /* Assume "got_int" was set to truncate the listing. */
8529 got_int = FALSE;
8530
8531# ifdef FEAT_BROWSE_CMD
8532 if (cmdmod.browse)
8533 {
8534 quit_more = FALSE;
8535 nr = prompt_for_number(FALSE);
8536 msg_starthere();
8537 if (nr > 0)
8538 {
8539 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8540 (long)nr);
8541
8542 if (p != NULL)
8543 {
8544 p = expand_env_save(p);
8545 eap->arg = p;
8546 eap->cmdidx = CMD_edit;
8547 cmdmod.browse = FALSE;
8548 do_exedit(eap, NULL);
8549 vim_free(p);
8550 }
8551 }
8552 }
8553# endif
8554 }
8555}
8556#endif