blob: 53334342c1571c83e981ca650f487ae71cf8071d [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 Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098 /* print full file name if :cd used */
Bram Moolenaar426dd022016-03-15 15:09:29 +01003099 if (!shortmess(SHM_FILEINFO))
3100 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar6ce65042017-10-24 22:32:59 +02003101 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103
3104/*
3105 * ":update".
3106 */
3107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003108ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 if (curbufIsChanged())
3111 (void)do_write(eap);
3112}
3113
3114/*
3115 * ":write" and ":saveas".
3116 */
3117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003118ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 if (eap->usefilter) /* input lines to shell command */
3121 do_bang(1, eap, FALSE, TRUE, FALSE);
3122 else
3123 (void)do_write(eap);
3124}
3125
3126/*
3127 * write current buffer to file 'eap->arg'
3128 * if 'eap->append' is TRUE, append to the file
3129 *
3130 * if *eap->arg == NUL write to current file
3131 *
3132 * return FAIL for failure, OK otherwise
3133 */
3134 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003135do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 int other;
3138 char_u *fname = NULL; /* init to shut up gcc */
3139 char_u *ffname;
3140 int retval = FAIL;
3141 char_u *free_fname = NULL;
3142#ifdef FEAT_BROWSE
3143 char_u *browse_file = NULL;
3144#endif
3145 buf_T *alt_buf = NULL;
Bram Moolenaar5c719942016-07-09 23:40:45 +02003146 int name_was_missing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 if (not_writing()) /* check 'write' option */
3149 return FAIL;
3150
3151 ffname = eap->arg;
3152#ifdef FEAT_BROWSE
3153 if (cmdmod.browse)
3154 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003155 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 NULL, NULL, NULL, curbuf);
3157 if (browse_file == NULL)
3158 goto theend;
3159 ffname = browse_file;
3160 }
3161#endif
3162 if (*ffname == NUL)
3163 {
3164 if (eap->cmdidx == CMD_saveas)
3165 {
3166 EMSG(_(e_argreq));
3167 goto theend;
3168 }
3169 other = FALSE;
3170 }
3171 else
3172 {
3173 fname = ffname;
3174 free_fname = fix_fname(ffname);
3175 /*
3176 * When out-of-memory, keep unexpanded file name, because we MUST be
3177 * able to write the file in this situation.
3178 */
3179 if (free_fname != NULL)
3180 ffname = free_fname;
3181 other = otherfile(ffname);
3182 }
3183
3184 /*
3185 * If we have a new file, put its name in the list of alternate file names.
3186 */
3187 if (other)
3188 {
3189 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3190 || eap->cmdidx == CMD_saveas)
3191 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3192 else
3193 alt_buf = buflist_findname(ffname);
3194 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3195 {
3196 /* Overwriting a file that is loaded in another buffer is not a
3197 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00003198 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 goto theend;
3200 }
3201 }
3202
3203 /*
3204 * Writing to the current file is not allowed in readonly mode
3205 * and a file name is required.
3206 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3207 */
3208 if (!other && (
3209#ifdef FEAT_QUICKFIX
3210 bt_dontwrite_msg(curbuf) ||
3211#endif
3212 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3213 goto theend;
3214
3215 if (!other)
3216 {
3217 ffname = curbuf->b_ffname;
3218 fname = curbuf->b_fname;
3219 /*
3220 * Not writing the whole file is only allowed with '!'.
3221 */
3222 if ( (eap->line1 != 1
3223 || eap->line2 != curbuf->b_ml.ml_line_count)
3224 && !eap->forceit
3225 && !eap->append
3226 && !p_wa)
3227 {
3228#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3229 if (p_confirm || cmdmod.confirm)
3230 {
3231 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3232 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3233 goto theend;
3234 eap->forceit = TRUE;
3235 }
3236 else
3237#endif
3238 {
3239 EMSG(_("E140: Use ! to write partial buffer"));
3240 goto theend;
3241 }
3242 }
3243 }
3244
3245 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3246 {
3247 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 buf_T *was_curbuf = curbuf;
3250
3251 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003252 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003253#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003255#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 /* buffer changed, don't change name now */
3260 retval = FAIL;
3261 goto theend;
3262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 /* Exchange the file names for the current and the alternate
3264 * buffer. This makes it look like we are now editing the buffer
3265 * under the new name. Must be done before buf_write(), because
3266 * if there is no file name and 'cpo' contains 'F', it will set
3267 * the file name. */
3268 fname = alt_buf->b_fname;
3269 alt_buf->b_fname = curbuf->b_fname;
3270 curbuf->b_fname = fname;
3271 fname = alt_buf->b_ffname;
3272 alt_buf->b_ffname = curbuf->b_ffname;
3273 curbuf->b_ffname = fname;
3274 fname = alt_buf->b_sfname;
3275 alt_buf->b_sfname = curbuf->b_sfname;
3276 curbuf->b_sfname = fname;
3277 buf_name_changed(curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003280 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (!alt_buf->b_p_bl)
3282 {
3283 alt_buf->b_p_bl = TRUE;
3284 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3285 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003286#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (curbuf != was_curbuf || aborting())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003288#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (curbuf != was_curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 /* buffer changed, don't write the file */
3293 retval = FAIL;
3294 goto theend;
3295 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003296
3297 /* If 'filetype' was empty try detecting it now. */
3298 if (*curbuf->b_p_ft == NUL)
3299 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003300 if (au_has_group((char_u *)"filetypedetect"))
3301 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003302 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003303 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003304 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003305
3306 /* Autocommands may have changed buffer names, esp. when
3307 * 'autochdir' is set. */
3308 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310
Bram Moolenaar5c719942016-07-09 23:40:45 +02003311 name_was_missing = curbuf->b_ffname == NULL;
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3314 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003315
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003316 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003317 if (eap->cmdidx == CMD_saveas)
3318 {
3319 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003320 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003321 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003322 redraw_tabline = TRUE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003323 }
Bram Moolenaar5c719942016-07-09 23:40:45 +02003324 }
3325
3326 /* Change directories when the 'acd' option is set and the file name
3327 * got changed or set. */
3328 if (eap->cmdidx == CMD_saveas || name_was_missing)
3329 {
Bram Moolenaar6f470022018-04-10 18:47:20 +02003330 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 }
3333
3334theend:
3335#ifdef FEAT_BROWSE
3336 vim_free(browse_file);
3337#endif
3338 vim_free(free_fname);
3339 return retval;
3340}
3341
3342/*
3343 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3344 * BF_NEW or BF_READERR, check for overwriting current file.
3345 * May set eap->forceit if a dialog says it's OK to overwrite.
3346 * Return OK if it's OK, FAIL if it is not.
3347 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003348 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003349check_overwrite(
3350 exarg_T *eap,
3351 buf_T *buf,
3352 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003354 char_u *ffname, /* full path version of fname */
3355 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 /*
3358 * write to other file or b_flags set or not writing the whole file:
3359 * overwriting only allowed with '!'
3360 */
3361 if ( (other
3362 || (buf->b_flags & BF_NOTEDITED)
3363 || ((buf->b_flags & BF_NEW)
3364 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3365 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003367#ifdef FEAT_QUICKFIX
3368 && !bt_nofile(buf)
3369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 && vim_fexists(ffname))
3371 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003372 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003374#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003375 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003376 if (mch_isdir(ffname))
3377 {
3378 EMSG2(_(e_isadir2), ffname);
3379 return FAIL;
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#endif
3382#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003383 if (p_confirm || cmdmod.confirm)
3384 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003385 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003387 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3388 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3389 return FAIL;
3390 eap->forceit = TRUE;
3391 }
3392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003394 {
3395 EMSG(_(e_exists));
3396 return FAIL;
3397 }
3398 }
3399
3400 /* For ":w! filename" check that no swap file exists for "filename". */
3401 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003403 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003404 char_u *p;
3405 int r;
3406 char_u *swapname;
3407
3408 /* We only try the first entry in 'directory', without checking if
3409 * it's writable. If the "." directory is not writable the write
3410 * will probably fail anyway.
3411 * Use 'shortname' of the current buffer, since there is no buffer
3412 * for the written file. */
3413 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003414 {
3415 dir = alloc(5);
3416 if (dir == NULL)
3417 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003418 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003419 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003420 else
3421 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003422 dir = alloc(MAXPATHL);
3423 if (dir == NULL)
3424 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003425 p = p_dir;
3426 copy_option_part(&p, dir, MAXPATHL, ",");
3427 }
3428 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003429 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003430 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003431 if (r)
3432 {
3433#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3434 if (p_confirm || cmdmod.confirm)
3435 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003436 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003437
3438 dialog_msg(buff,
3439 _("Swap file \"%s\" exists, overwrite anyway?"),
3440 swapname);
3441 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3442 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003443 {
3444 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003445 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003446 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003447 eap->forceit = TRUE;
3448 }
3449 else
3450#endif
3451 {
3452 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
3453 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003454 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003455 return FAIL;
3456 }
3457 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003458 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 }
3461 return OK;
3462}
3463
3464/*
3465 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3466 */
3467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003468ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 int i;
3471
3472 if (eap->cmd[1] == 'n')
3473 i = curwin->w_arg_idx + (int)eap->line2;
3474 else
3475 i = curwin->w_arg_idx - (int)eap->line2;
3476 eap->line1 = 1;
3477 eap->line2 = curbuf->b_ml.ml_line_count;
3478 if (do_write(eap) != FAIL)
3479 do_argfile(eap, i);
3480}
3481
3482/*
3483 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3484 */
3485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003486do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 buf_T *buf;
3489 int error = 0;
3490 int save_forceit = eap->forceit;
3491
3492 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3493 exiting = TRUE;
3494
Bram Moolenaar29323592016-07-24 22:04:11 +02003495 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
Bram Moolenaar7a760922018-02-19 23:10:02 +01003497#ifdef FEAT_TERMINAL
3498 if (exiting && term_job_running(buf->b_term))
3499 {
3500 no_write_message_nobang(buf);
3501 ++error;
3502 }
3503 else
3504#endif
Bram Moolenaar059db5c2017-10-15 22:42:23 +02003505 if (bufIsChanged(buf) && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 /*
3508 * Check if there is a reason the buffer cannot be written:
3509 * 1. if the 'write' option is set
3510 * 2. if there is no file name (even after browsing)
3511 * 3. if the 'readonly' is set (even after a dialog)
3512 * 4. if overwriting is allowed (even after a dialog)
3513 */
3514 if (not_writing())
3515 {
3516 ++error;
3517 break;
3518 }
3519#ifdef FEAT_BROWSE
3520 /* ":browse wall": ask for file name if there isn't one */
3521 if (buf->b_ffname == NULL && cmdmod.browse)
3522 browse_save_fname(buf);
3523#endif
3524 if (buf->b_ffname == NULL)
3525 {
3526 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3527 ++error;
3528 }
3529 else if (check_readonly(&eap->forceit, buf)
3530 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3531 FALSE) == FAIL)
3532 {
3533 ++error;
3534 }
3535 else
3536 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003537 bufref_T bufref;
3538
3539 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (buf_write_all(buf, eap->forceit) == FAIL)
3541 ++error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /* an autocommand may have deleted the buffer */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003543 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546 eap->forceit = save_forceit; /* check_overwrite() may set it */
3547 }
3548 }
3549 if (exiting)
3550 {
3551 if (!error)
3552 getout(0); /* exit Vim */
3553 not_exiting();
3554 }
3555}
3556
3557/*
3558 * Check the 'write' option.
3559 * Return TRUE and give a message when it's not st.
3560 */
3561 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003562not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
3564 if (p_write)
3565 return FALSE;
3566 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3567 return TRUE;
3568}
3569
3570/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003571 * Check if a buffer is read-only (either 'readonly' option is set or file is
3572 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3573 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 */
3575 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003576check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003578 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003579
3580 /* Handle a file being readonly when the 'readonly' option is set or when
3581 * the file exists and permissions are read-only.
3582 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3583 * important for device checks but not here. */
3584 if (!*forceit && (buf->b_p_ro
3585 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3586 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3589 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3590 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003591 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar5386a122007-06-28 20:02:32 +00003593 if (buf->b_p_ro)
3594 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3595 buf->b_fname);
3596 else
3597 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 +00003598 buf->b_fname);
3599
3600 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3601 {
3602 /* Set forceit, to force the writing of a readonly file */
3603 *forceit = TRUE;
3604 return FALSE;
3605 }
3606 else
3607 return TRUE;
3608 }
3609 else
3610#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003611 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003613 else
3614 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3615 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return TRUE;
3617 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 return FALSE;
3620}
3621
3622/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003623 * Try to abandon current file and edit a new or existing file.
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003624 * "fnum" is the number of the file, if zero use ffname/sfname.
3625 * "lnum" is the line number for the cursor in the new file (if non-zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 *
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003627 * Return:
3628 * GETFILE_ERROR for "normal" error,
3629 * GETFILE_NOT_WRITTEN for "not written" error,
3630 * GETFILE_SAME_FILE for success
3631 * GETFILE_OPEN_OTHER for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 */
3633 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003634getfile(
3635 int fnum,
3636 char_u *ffname,
3637 char_u *sfname,
3638 int setpm,
3639 linenr_T lnum,
3640 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 int other;
3643 int retval;
3644 char_u *free_me = NULL;
3645
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003646 if (text_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003647 return GETFILE_ERROR;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003648 if (curbuf_locked())
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003649 return GETFILE_ERROR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 if (fnum == 0)
3652 {
3653 /* make ffname full path, set sfname */
3654 fname_expand(curbuf, &ffname, &sfname);
3655 other = otherfile(ffname);
3656 free_me = ffname; /* has been allocated, free() later */
3657 }
3658 else
3659 other = (fnum != curbuf->b_fnum);
3660
3661 if (other)
3662 ++no_wait_return; /* don't wait for autowrite message */
Bram Moolenaareb44a682017-08-03 22:44:55 +02003663 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3665 {
3666#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3667 if (p_confirm && p_write)
3668 dialog_changed(curbuf, FALSE);
3669 if (curbufIsChanged())
3670#endif
3671 {
3672 if (other)
3673 --no_wait_return;
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02003674 no_write_message();
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003675 retval = GETFILE_NOT_WRITTEN; /* file has been changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 goto theend;
3677 }
3678 }
3679 if (other)
3680 --no_wait_return;
3681 if (setpm)
3682 setpcmark();
3683 if (!other)
3684 {
3685 if (lnum != 0)
3686 curwin->w_cursor.lnum = lnum;
3687 check_cursor_lnum();
3688 beginline(BL_SOL | BL_FIX);
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003689 retval = GETFILE_SAME_FILE; /* it's in the same file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02003692 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003693 curwin) == OK)
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003694 retval = GETFILE_OPEN_OTHER; /* opened another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 else
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003696 retval = GETFILE_ERROR; /* error encountered */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698theend:
3699 vim_free(free_me);
3700 return retval;
3701}
3702
3703/*
3704 * start editing a new file
3705 *
3706 * fnum: file number; if zero use ffname/sfname
3707 * ffname: the file name
3708 * - full path if sfname used,
3709 * - any file name if sfname is NULL
3710 * - empty string to re-edit with the same file name (but may be
3711 * in a different directory)
3712 * - NULL to start an empty buffer
3713 * sfname: the short file name (or NULL)
3714 * eap: contains the command to be executed after loading the file and
3715 * forced 'ff' and 'fenc'
3716 * newlnum: if > 0: put cursor on this line number (if possible)
3717 * if ECMD_LASTL: use last position in loaded file
3718 * if ECMD_LAST: use last position in all files
3719 * if ECMD_ONE: use first line
3720 * flags:
3721 * ECMD_HIDE: if TRUE don't free the current buffer
3722 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3723 * ECMD_OLDBUF: use existing buffer if it exists
3724 * ECMD_FORCEIT: ! used for Ex command
3725 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003726 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003727 * window, NULL when splitting the window first. When not NULL info
3728 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 *
3730 * return FAIL for failure, OK otherwise
3731 */
3732 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003733do_ecmd(
3734 int fnum,
3735 char_u *ffname,
3736 char_u *sfname,
3737 exarg_T *eap, /* can be NULL! */
3738 linenr_T newlnum,
3739 int flags,
3740 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 int other_file; /* TRUE if editing another file */
3743 int oldbuf; /* TRUE if using existing buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 int auto_buf = FALSE; /* TRUE if autocommands brought us
3745 into the buffer unexpectedly */
3746 char_u *new_name = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003747#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003748 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#endif
3750 buf_T *buf;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003751 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003752 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 char_u *free_fname = NULL;
3754#ifdef FEAT_BROWSE
3755 char_u *browse_file = NULL;
3756#endif
3757 int retval = FAIL;
3758 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003759 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 linenr_T topline = 0;
3761 int newcol = -1;
3762 int solcol = -1;
3763 pos_T *pos;
3764#ifdef FEAT_SUN_WORKSHOP
3765 char_u *cp;
3766#endif
3767 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003768#ifdef FEAT_SPELL
3769 int did_get_winopts = FALSE;
3770#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003771 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003772 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
3774 if (eap != NULL)
3775 command = eap->do_ecmd_cmd;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003776 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778 if (fnum != 0)
3779 {
3780 if (fnum == curbuf->b_fnum) /* file is already being edited */
3781 return OK; /* nothing to do */
3782 other_file = TRUE;
3783 }
3784 else
3785 {
3786#ifdef FEAT_BROWSE
3787 if (cmdmod.browse)
3788 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003789 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003790# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003791 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003792# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003793 au_has_group((char_u *)"FileExplorer"))
3794 {
3795 /* No browsing supported but we do have the file explorer:
3796 * Edit the directory. */
3797 if (ffname == NULL || !mch_isdir(ffname))
3798 ffname = (char_u *)".";
3799 }
3800 else
3801 {
3802 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003804 if (browse_file == NULL)
3805 goto theend;
3806 ffname = browse_file;
3807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809#endif
3810 /* if no short name given, use ffname for short name */
3811 if (sfname == NULL)
3812 sfname = ffname;
3813#ifdef USE_FNAME_CASE
3814# ifdef USE_LONG_FNAME
3815 if (USE_LONG_FNAME)
3816# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003817 if (sfname != NULL)
3818 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#endif
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3822 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
3824 if (ffname == NULL)
3825 other_file = TRUE;
3826 /* there is no file name */
3827 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3828 other_file = FALSE;
3829 else
3830 {
3831 if (*ffname == NUL) /* re-edit with same file name */
3832 {
3833 ffname = curbuf->b_ffname;
3834 sfname = curbuf->b_fname;
3835 }
3836 free_fname = fix_fname(ffname); /* may expand to full path name */
3837 if (free_fname != NULL)
3838 ffname = free_fname;
3839 other_file = otherfile(ffname);
3840#ifdef FEAT_SUN_WORKSHOP
3841 if (usingSunWorkShop && p_acd
3842 && (cp = vim_strrchr(sfname, '/')) != NULL)
3843 sfname = ++cp;
3844#endif
3845 }
3846 }
3847
3848 /*
3849 * if the file was changed we may not be allowed to abandon it
3850 * - if we are going to re-edit the same file
3851 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3852 */
3853 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3854 || (curbuf->b_nwindows == 1
3855 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003856 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3857 | (other_file ? 0 : CCGD_MULTWIN)
3858 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3859 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 if (fnum == 0 && other_file && ffname != NULL)
3862 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3863 goto theend;
3864 }
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 /*
3867 * End Visual mode before switching to another buffer, so the text can be
3868 * copied into the GUI selection buffer.
3869 */
3870 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003872#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003873 if ((command != NULL || newlnum > (linenr_T)0)
3874 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3875 {
3876 int len;
3877 char_u *p;
3878
3879 /* Set v:swapcommand for the SwapExists autocommands. */
3880 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003881 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003882 else
3883 len = 30;
3884 p = alloc((unsigned)len);
3885 if (p != NULL)
3886 {
3887 if (command != NULL)
3888 vim_snprintf((char *)p, len, ":%s\r", command);
3889 else
3890 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3891 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3892 did_set_swapcommand = TRUE;
3893 vim_free(p);
3894 }
3895 }
3896#endif
3897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 /*
3899 * If we are starting to edit another file, open a (new) buffer.
3900 * Otherwise we re-use the current buffer.
3901 */
3902 if (other_file)
3903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (!(flags & ECMD_ADDBUF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003906 if (!cmdmod.keepalt)
3907 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003908 if (oldwin != NULL)
3909 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911
3912 if (fnum)
3913 buf = buflist_findnr(fnum);
3914 else
3915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (flags & ECMD_ADDBUF)
3917 {
3918 linenr_T tlnum = 1L;
3919
3920 if (command != NULL)
3921 {
3922 tlnum = atol((char *)command);
3923 if (tlnum <= 0)
3924 tlnum = 1L;
3925 }
3926 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3927 goto theend;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 buf = buflist_new(ffname, sfname, 0L,
3930 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003931
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003932 /* autocommands may change curwin and curbuf */
3933 if (oldwin != NULL)
3934 oldwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003935 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937 if (buf == NULL)
3938 goto theend;
3939 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3940 {
3941 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 else /* existing memfile */
3944 {
3945 oldbuf = TRUE;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003946 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 (void)buf_check_timestamp(buf, FALSE);
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02003948 /* Check if autocommands made the buffer invalid or changed the
3949 * current buffer. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003950 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 goto theend;
3952#ifdef FEAT_EVAL
3953 if (aborting()) /* autocmds may abort script processing */
3954 goto theend;
3955#endif
3956 }
3957
3958 /* May jump to last used line number for a loaded buffer or when asked
3959 * for explicitly */
3960 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3961 {
3962 pos = buflist_findfpos(buf);
3963 newlnum = pos->lnum;
3964 solcol = pos->col;
3965 }
3966
3967 /*
3968 * Make the (new) buffer the one used by the current window.
3969 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3970 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003971 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 */
3973 if (buf != curbuf)
3974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 /*
3976 * Be careful: The autocommands may delete any buffer and change
3977 * the current buffer.
3978 * - If the buffer we are going to edit is deleted, give up.
3979 * - If the current buffer is deleted, prefer to load the new
3980 * buffer when loading a buffer is required. This avoids
3981 * loading another buffer which then must be closed again.
3982 * - If we ended up in the new buffer already, need to skip a few
3983 * things, set auto_buf.
3984 */
3985 if (buf->b_fname != NULL)
3986 new_name = vim_strsave(buf->b_fname);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003987 set_bufref(&au_new_curbuf, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003989 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02003991 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 delbuf_msg(new_name); /* frees new_name */
3993 goto theend;
3994 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003995#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (aborting()) /* autocmds may abort script processing */
3997 {
3998 vim_free(new_name);
3999 goto theend;
4000 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (buf == curbuf) /* already in new buffer */
4003 auto_buf = TRUE;
4004 else
4005 {
Bram Moolenaar5a497892016-09-03 16:29:04 +02004006 win_T *the_curwin = curwin;
4007
4008 /* Set the w_closing flag to avoid that autocommands close the
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004009 * window. And set b_locked for the same reason. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004010 the_curwin->w_closing = TRUE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004011 ++buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +02004012
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004013 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 buf_copy_options(buf, BCO_ENTER);
4015
Bram Moolenaar5a497892016-09-03 16:29:04 +02004016 /* Close the link to the current buffer. This will set
Bram Moolenaaraeac9002016-09-06 22:15:08 +02004017 * oldwin->w_buffer to NULL. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004018 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004019 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004020 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
Bram Moolenaar5a497892016-09-03 16:29:04 +02004022 the_curwin->w_closing = FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02004023 --buf->b_locked;
Bram Moolenaarfc573802011-12-30 15:01:59 +01004024
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004025#ifdef FEAT_EVAL
Bram Moolenaar5a497892016-09-03 16:29:04 +02004026 /* autocmds may abort script processing */
4027 if (aborting() && curwin->w_buffer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
4029 vim_free(new_name);
4030 goto theend;
4031 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 /* Be careful again, like above. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004034 if (!bufref_valid(&au_new_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004036 /* new buffer has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 delbuf_msg(new_name); /* frees new_name */
4038 goto theend;
4039 }
4040 if (buf == curbuf) /* already in new buffer */
4041 auto_buf = TRUE;
4042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004044#ifdef FEAT_SYN_HL
4045 /*
4046 * <VN> We could instead free the synblock
4047 * and re-attach to buffer, perhaps.
4048 */
Bram Moolenaarf1d13472017-07-11 18:28:46 +02004049 if (curwin->w_buffer == NULL
4050 || curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02004051 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 curwin->w_buffer = buf;
4054 curbuf = buf;
4055 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004056
4057 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
4058 if (!oldbuf && eap != NULL)
4059 {
4060 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004061#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004062 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02004063#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02004064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066
4067 /* May get the window options from the last time this buffer
4068 * was in this window (or another window). If not used
4069 * before, reset the local window options to the global
4070 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00004071 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004072#ifdef FEAT_SPELL
4073 did_get_winopts = TRUE;
4074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 vim_free(new_name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004077 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004078 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 curwin->w_pcmark.lnum = 1;
4082 curwin->w_pcmark.col = 0;
4083 }
4084 else /* !other_file */
4085 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02004086 if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01004088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 oldbuf = (flags & ECMD_OLDBUF);
4090 }
4091
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004092 /* Don't redraw until the cursor is in the right line, otherwise
4093 * autocommands may cause ml_get errors. */
4094 ++RedrawingDisabled;
4095 did_inc_redrawing_disabled = TRUE;
4096
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004097 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4099 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004100 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 else
4103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 /* Don't make a buffer listed if it's a help buffer. Useful when
4105 * using CTRL-O to go back to a help file. */
4106 if (!curbuf->b_help)
4107 set_buflisted(TRUE);
4108 }
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 /* If autocommands change buffers under our fingers, forget about
4111 * editing the file. */
4112 if (buf != curbuf)
4113 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004114#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (aborting()) /* autocmds may abort script processing */
4116 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 /* Since we are starting to edit a file, consider the filetype to be
4120 * unset. Helps for when an autocommand changes files and expects syntax
4121 * highlighting to work in the other file. */
4122 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124/*
4125 * other_file oldbuf
4126 * FALSE FALSE re-edit same file, buffer is re-used
4127 * FALSE TRUE re-edit same file, nothing changes
4128 * TRUE FALSE start editing new file, new buffer
4129 * TRUE TRUE start editing in existing buffer (nothing to do)
4130 */
4131 if (!other_file && !oldbuf) /* re-use the buffer */
4132 {
4133 set_last_cursor(curwin); /* may set b_last_cursor */
4134 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4135 {
4136 newlnum = curwin->w_cursor.lnum;
4137 solcol = curwin->w_cursor.col;
4138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 buf = curbuf;
4140 if (buf->b_fname != NULL)
4141 new_name = vim_strsave(buf->b_fname);
4142 else
4143 new_name = NULL;
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004144 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004145
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004146 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4147 {
4148 /* Save all the text, so that the reload can be undone.
4149 * Sync first so that this is a separate undo-able action. */
4150 u_sync(FALSE);
4151 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4152 == FAIL)
Bram Moolenaar1e225822016-07-30 21:48:59 +02004153 {
4154 vim_free(new_name);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004155 goto theend;
Bram Moolenaar1e225822016-07-30 21:48:59 +02004156 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004157 u_unchanged(curbuf);
4158 buf_freeall(curbuf, BFA_KEEP_UNDO);
4159
4160 /* tell readfile() not to clear or reload undo info */
4161 readfile_flags = READ_KEEP_UNDO;
4162 }
4163 else
4164 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 /* If autocommands deleted the buffer we were going to re-edit, give
4167 * up and jump to the end. */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004168 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 delbuf_msg(new_name); /* frees new_name */
4171 goto theend;
4172 }
4173 vim_free(new_name);
4174
4175 /* If autocommands change buffers under our fingers, forget about
4176 * re-editing the file. Should do the buf_clear_file(), but perhaps
4177 * the autocommands changed the buffer... */
4178 if (buf != curbuf)
4179 goto theend;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004180#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (aborting()) /* autocmds may abort script processing */
4182 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183#endif
4184 buf_clear_file(curbuf);
4185 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4186 curbuf->b_op_end.lnum = 0;
4187 }
4188
4189/*
4190 * If we get here we are sure to start editing
4191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 /* Assume success now */
4193 retval = OK;
4194
4195 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 * Check if we are editing the w_arg_idx file in the argument list.
4197 */
4198 check_arg_idx(curwin);
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (!auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
4202 /*
4203 * Set cursor and init window before reading the file and executing
4204 * autocommands. This allows for the autocommands to position the
4205 * cursor.
4206 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004207 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004210 /* It's possible that all lines in the buffer changed. Need to update
4211 * automatic folding for all windows where it's used. */
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004212 {
4213 win_T *win;
4214 tabpage_T *tp;
4215
4216 FOR_ALL_TAB_WINDOWS(tp, win)
4217 if (win->w_buffer == curbuf)
4218 foldUpdateAll(win);
4219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#endif
4221
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004222 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004223 DO_AUTOCHDIR;
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 /*
4226 * Careful: open_buffer() and apply_autocmds() may change the current
4227 * buffer and window.
4228 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004229 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 topline = curwin->w_topline;
4231 if (!oldbuf) /* need to read the file */
4232 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004233#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 swap_exists_action = SEA_DIALOG;
4235#endif
4236 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4237
4238 /*
4239 * Open the buffer and read the file.
4240 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004241#if defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004242 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 retval = FAIL;
4244#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004245 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#endif
4247
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004248#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (swap_exists_action == SEA_QUIT)
4250 retval = FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004251 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#endif
4253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 else
4255 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004256 /* Read the modelines, but only to set window-local options. Any
4257 * buffer-local options have already been set and may have been
4258 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004259 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4262 &retval);
4263 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4264 &retval);
4265 }
4266 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaareab316b2015-03-24 11:46:30 +01004268 /* If autocommands change the cursor position or topline, we should
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004269 * keep it. Also when it moves within a line. But not when it moves
4270 * to the first non-blank. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004271 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
Bram Moolenaaradb8fbe2018-06-04 20:34:23 +02004273 char_u *text = ml_get_curline();
4274
4275 if (curwin->w_cursor.lnum != orig_pos.lnum
4276 || curwin->w_cursor.col != (int)(skipwhite(text) - text))
4277 {
4278 newlnum = curwin->w_cursor.lnum;
4279 newcol = curwin->w_cursor.col;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 if (curwin->w_topline == topline)
4283 topline = 0;
4284
4285 /* Even when cursor didn't move we need to recompute topline. */
4286 changed_line_abv_curs();
4287
4288#ifdef FEAT_TITLE
4289 maketitle();
4290#endif
4291 }
4292
4293#ifdef FEAT_DIFF
4294 /* Tell the diff stuff that this buffer is new and/or needs updating.
4295 * Also needed when re-editing the same buffer, because unloading will
4296 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004297 if (curwin->w_p_diff)
4298 {
4299 diff_buf_add(curbuf);
4300 diff_invalidate(curbuf);
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif
4303
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004304#ifdef FEAT_SPELL
4305 /* If the window options were changed may need to set the spell language.
4306 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004307 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4308 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004309#endif
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (command == NULL)
4312 {
4313 if (newcol >= 0) /* position set by autocommands */
4314 {
4315 curwin->w_cursor.lnum = newlnum;
4316 curwin->w_cursor.col = newcol;
4317 check_cursor();
4318 }
4319 else if (newlnum > 0) /* line number from caller or old position */
4320 {
4321 curwin->w_cursor.lnum = newlnum;
4322 check_cursor_lnum();
4323 if (solcol >= 0 && !p_sol)
4324 {
4325 /* 'sol' is off: Use last known column. */
4326 curwin->w_cursor.col = solcol;
4327 check_cursor_col();
4328#ifdef FEAT_VIRTUALEDIT
4329 curwin->w_cursor.coladd = 0;
4330#endif
4331 curwin->w_set_curswant = TRUE;
4332 }
4333 else
4334 beginline(BL_SOL | BL_FIX);
4335 }
4336 else /* no line number, go to last line in Ex mode */
4337 {
4338 if (exmode_active)
4339 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4340 beginline(BL_WHITE | BL_FIX);
4341 }
4342 }
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 /* Check if cursors in other windows on the same buffer are still valid */
4345 check_lnums(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 /*
4348 * Did not read the file, need to show some info about the file.
4349 * Do this after setting the cursor.
4350 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004351 if (oldbuf && !auto_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 int msg_scroll_save = msg_scroll;
4354
4355 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4356 * message. */
4357 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4358 msg_scroll = FALSE;
4359 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4360 check_for_delay(FALSE);
4361 msg_start();
4362 msg_scroll = msg_scroll_save;
4363 msg_scrolled_ign = TRUE;
4364
Bram Moolenaar426dd022016-03-15 15:09:29 +01004365 if (!shortmess(SHM_FILEINFO))
4366 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 msg_scrolled_ign = FALSE;
4369 }
4370
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02004371#ifdef FEAT_VIMINFO
4372 curbuf->b_last_used = vim_time();
4373#endif
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (command != NULL)
4376 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4377
4378#ifdef FEAT_KEYMAP
4379 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004380 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif
4382
4383 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004384 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (!skip_redraw)
4386 {
4387 n = p_so;
4388 if (topline == 0 && command == NULL)
4389 p_so = 999; /* force cursor halfway the window */
4390 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 curwin->w_scbind_pos = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 p_so = n;
4393 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4394 }
4395
4396 if (p_im)
4397 need_start_insertmode = TRUE;
4398
Bram Moolenaar15833232018-02-03 18:33:17 +01004399#ifdef FEAT_AUTOCHDIR
4400 /* Change directories when the 'acd' option is set and we aren't already in
4401 * that directory (should already be done above). Expect getcwd() to be
4402 * faster than calling shorten_fnames() unnecessarily. */
4403 if (p_acd && curbuf->b_ffname != NULL)
4404 {
4405 char_u curdir[MAXPATHL];
4406 char_u filedir[MAXPATHL];
4407
4408 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1);
4409 *gettail_sep(filedir) = NUL;
4410 if (mch_dirname(curdir, MAXPATHL) != FAIL
4411 && vim_fnamecmp(curdir, filedir) != 0)
4412 do_autochdir();
4413 }
4414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00004416#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004417 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02004420 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
4422# endif
4423# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004424 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004425 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426# endif
4427 }
4428#endif
4429
4430theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004431 if (did_inc_redrawing_disabled)
4432 --RedrawingDisabled;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004433#if defined(FEAT_EVAL)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004434 if (did_set_swapcommand)
4435 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#ifdef FEAT_BROWSE
4438 vim_free(browse_file);
4439#endif
4440 vim_free(free_fname);
4441 return retval;
4442}
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004445delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4448 name == NULL ? (char_u *)"" : name);
4449 vim_free(name);
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02004450 au_new_curbuf.br_buf = NULL;
Bram Moolenaarac77aec2016-07-26 22:02:54 +02004451 au_new_curbuf.br_buf_free_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004454static int append_indent = 0; /* autoindent for first line */
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456/*
4457 * ":insert" and ":append", also used by ":change"
4458 */
4459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004460ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462 char_u *theline;
4463 int did_undo = FALSE;
4464 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004465 int indent = 0;
4466 char_u *p;
4467 int vcol;
4468 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004470 /* the ! flag toggles autoindent */
4471 if (eap->forceit)
4472 curbuf->b_p_ai = !curbuf->b_p_ai;
4473
4474 /* First autoindent comes from the line we start on */
4475 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4476 append_indent = get_indent_lnum(lnum);
4477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (eap->cmdidx != CMD_append)
4479 --lnum;
4480
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004481 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004482 if (empty && lnum == 1)
4483 lnum = 0;
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 State = INSERT; /* behave like in Insert mode */
4486 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4487 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004488
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004489 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 msg_scroll = TRUE;
4492 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004493 if (curbuf->b_p_ai)
4494 {
4495 if (append_indent >= 0)
4496 {
4497 indent = append_indent;
4498 append_indent = -1;
4499 }
4500 else if (lnum > 0)
4501 indent = get_indent_lnum(lnum);
4502 }
4503 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004505 {
4506 /* No getline() function, use the lines that follow. This ends
4507 * when there is no more. */
4508 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4509 break;
4510 p = vim_strchr(eap->nextcmd, NL);
4511 if (p == NULL)
4512 p = eap->nextcmd + STRLEN(eap->nextcmd);
4513 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4514 if (*p != NUL)
4515 ++p;
4516 eap->nextcmd = p;
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004519 {
4520 int save_State = State;
4521
4522 /* Set State to avoid the cursor shape to be set to INSERT mode
4523 * when getline() returns. */
4524 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 theline = eap->getline(
4526#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004527 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004529 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004530 State = save_State;
4531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004533 if (theline == NULL)
4534 break;
4535
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004536 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4537 if (ex_keep_indent)
4538 append_indent = indent;
4539
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004540 /* Look for the "." after automatic indent. */
4541 vcol = 0;
4542 for (p = theline; indent > vcol; ++p)
4543 {
4544 if (*p == ' ')
4545 ++vcol;
4546 else if (*p == TAB)
4547 vcol += 8 - vcol % 8;
4548 else
4549 break;
4550 }
4551 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004552 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4553 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
4555 vim_free(theline);
4556 break;
4557 }
4558
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004559 /* don't use autoindent if nothing was typed. */
4560 if (p[0] == NUL)
4561 theline[0] = NUL;
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 did_undo = TRUE;
4564 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004565 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 vim_free(theline);
4568 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004569
4570 if (empty)
4571 {
4572 ml_delete(2L, FALSE);
4573 empty = FALSE;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576 State = NORMAL;
4577
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004578 if (eap->forceit)
4579 curbuf->b_p_ai = !curbuf->b_p_ai;
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004582 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 * "end" is set to lnum when something has been appended, otherwise
4584 * it is the same than "start" -- Acevedo */
4585 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4586 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4587 if (eap->cmdidx != CMD_append)
4588 --curbuf->b_op_start.lnum;
4589 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4590 ? lnum : curbuf->b_op_start.lnum;
4591 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4592 curwin->w_cursor.lnum = lnum;
4593 check_cursor_lnum();
4594 beginline(BL_SOL | BL_FIX);
4595
4596 need_wait_return = FALSE; /* don't use wait_return() now */
4597 ex_no_reprint = TRUE;
4598}
4599
4600/*
4601 * ":change"
4602 */
4603 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 linenr_T lnum;
4607
4608 if (eap->line2 >= eap->line1
4609 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4610 return;
4611
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004612 /* the ! flag toggles autoindent */
4613 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4614 append_indent = get_indent_lnum(eap->line1);
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4617 {
4618 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4619 break;
4620 ml_delete(eap->line1, FALSE);
4621 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004622
4623 /* make sure the cursor is not beyond the end of the file now */
4624 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4626
4627 /* ":append" on the line above the deleted lines. */
4628 eap->line2 = eap->line1;
4629 ex_append(eap);
4630}
4631
4632 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004633ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 char_u *x;
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004636 long bigness;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004637 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 int minus = 0;
4639 linenr_T start, end, curs, i;
4640 int j;
4641 linenr_T lnum = eap->line2;
4642
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004643 /* Vi compatible: ":z!" uses display height, without a count uses
4644 * 'scroll' */
4645 if (eap->forceit)
4646 bigness = curwin->w_height;
Bram Moolenaar459ca562016-11-10 18:16:33 +01004647 else if (!ONE_WINDOW)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004648 bigness = curwin->w_height - 3;
Bram Moolenaar631abc32014-02-22 22:27:47 +01004649 else
4650 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (bigness < 1)
4652 bigness = 1;
4653
4654 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004655 kind = x;
4656 if (*kind == '-' || *kind == '+' || *kind == '='
4657 || *kind == '^' || *kind == '.')
4658 ++x;
4659 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 ++x;
4661
4662 if (*x != 0)
4663 {
4664 if (!VIM_ISDIGIT(*x))
4665 {
4666 EMSG(_("E144: non-numeric argument to :z"));
4667 return;
4668 }
4669 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004670 {
Bram Moolenaarfa0ad0b2017-04-02 15:45:17 +02004671 bigness = atol((char *)x);
4672
4673 /* bigness could be < 0 if atol(x) overflows. */
4674 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
4675 bigness = 2 * curbuf->b_ml.ml_line_count;
4676
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004677 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004678 if (*kind == '=')
4679 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004683 /* the number of '-' and '+' multiplies the distance */
4684 if (*kind == '-' || *kind == '+')
4685 for (x = kind + 1; *x == *kind; ++x)
4686 ;
4687
4688 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
4690 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004691 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4692 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004693 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 break;
4695
4696 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004697 start = lnum - (bigness + 1) / 2 + 1;
4698 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 curs = lnum;
4700 minus = 1;
4701 break;
4702
4703 case '^':
4704 start = lnum - bigness * 2;
4705 end = lnum - bigness;
4706 curs = lnum - bigness;
4707 break;
4708
4709 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004710 start = lnum - (bigness + 1) / 2 + 1;
4711 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 curs = end;
4713 break;
4714
4715 default: /* '+' */
4716 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004717 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004718 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004719 else if (eap->addr_count == 0)
4720 ++start;
4721 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 curs = end;
4723 break;
4724 }
4725
4726 if (start < 1)
4727 start = 1;
4728
4729 if (end > curbuf->b_ml.ml_line_count)
4730 end = curbuf->b_ml.ml_line_count;
4731
4732 if (curs > curbuf->b_ml.ml_line_count)
4733 curs = curbuf->b_ml.ml_line_count;
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004734 else if (curs < 1)
4735 curs = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 for (i = start; i <= end; i++)
4738 {
4739 if (minus && i == lnum)
4740 {
4741 msg_putchar('\n');
4742
4743 for (j = 1; j < Columns; j++)
4744 msg_putchar('-');
4745 }
4746
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004747 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 if (minus && i == lnum)
4750 {
4751 msg_putchar('\n');
4752
4753 for (j = 1; j < Columns; j++)
4754 msg_putchar('-');
4755 }
4756 }
4757
Bram Moolenaara364cdb2017-04-20 21:12:30 +02004758 if (curwin->w_cursor.lnum != curs)
4759 {
4760 curwin->w_cursor.lnum = curs;
4761 curwin->w_cursor.col = 0;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 ex_no_reprint = TRUE;
4764}
4765
4766/*
4767 * Check if the restricted flag is set.
4768 * If so, give an error message and return TRUE.
4769 * Otherwise, return FALSE.
4770 */
4771 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004772check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 if (restricted)
4775 {
4776 EMSG(_("E145: Shell commands not allowed in rvim"));
4777 return TRUE;
4778 }
4779 return FALSE;
4780}
4781
4782/*
4783 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4784 * If so, give an error message and return TRUE.
4785 * Otherwise, return FALSE.
4786 */
4787 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004788check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 if (secure)
4791 {
4792 secure = 2;
4793 EMSG(_(e_curdir));
4794 return TRUE;
4795 }
4796#ifdef HAVE_SANDBOX
4797 /*
4798 * In the sandbox more things are not allowed, including the things
4799 * disallowed in secure mode.
4800 */
4801 if (sandbox != 0)
4802 {
4803 EMSG(_(e_sandbox));
4804 return TRUE;
4805 }
4806#endif
4807 return FALSE;
4808}
4809
4810static char_u *old_sub = NULL; /* previous substitute pattern */
4811static int global_need_beginline; /* call beginline() after ":g" */
4812
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004813/*
4814 * Flags that are kept between calls to :substitute.
4815 */
4816typedef struct {
4817 int do_all; /* do multiple substitutions per line */
4818 int do_ask; /* ask for confirmation */
4819 int do_count; /* count only */
4820 int do_error; /* if false, ignore errors */
4821 int do_print; /* print last line with subs. */
4822 int do_list; /* list last line with subs. */
4823 int do_number; /* list last line with line nr*/
4824 int do_ic; /* ignore case flag */
4825} subflags_T;
4826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827/* do_sub()
4828 *
4829 * Perform a substitution from line eap->line1 to line eap->line2 using the
4830 * command pointed to by eap->arg which should be of the form:
4831 *
4832 * /pattern/substitution/{flags}
4833 *
4834 * The usual escapes are supported as described in the regexp docs.
4835 */
4836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004837do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
4839 linenr_T lnum;
4840 long i = 0;
4841 regmmatch_T regmatch;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02004842 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
4843 FALSE, FALSE, 0};
4844#ifdef FEAT_EVAL
4845 subflags_T subflags_save;
4846#endif
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004847 int save_do_all; /* remember user specified 'g' flag */
4848 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4850 int delimiter;
4851 int sublen;
4852 int got_quit = FALSE;
4853 int got_match = FALSE;
4854 int temp;
4855 int which_pat;
4856 char_u *cmd;
4857 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004858 linenr_T first_line = 0; /* first changed line */
4859 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 * change */
4861 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4862 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004863 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004864 char_u *sub_firstline; /* allocated copy of first sub line */
4865 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004866 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004867 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004868#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004869 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 cmd = eap->arg;
4873 if (!global_busy)
4874 {
4875 sub_nsubs = 0;
4876 sub_nlines = 0;
4877 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004878 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (eap->cmdidx == CMD_tilde)
4881 which_pat = RE_LAST; /* use last used regexp */
4882 else
4883 which_pat = RE_SUBST; /* use last substitute regexp */
4884
4885 /* new pattern and substitution */
Bram Moolenaar1c465442017-03-12 20:10:05 +01004886 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4888 {
4889 /* don't accept alphanumeric for separator */
4890 if (isalpha(*cmd))
4891 {
4892 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4893 return;
4894 }
4895 /*
4896 * undocumented vi feature:
4897 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4898 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4899 */
4900 if (*cmd == '\\')
4901 {
4902 ++cmd;
4903 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4904 {
4905 EMSG(_(e_backslash));
4906 return;
4907 }
4908 if (*cmd != '&')
4909 which_pat = RE_SEARCH; /* use last '/' pattern */
4910 pat = (char_u *)""; /* empty search pattern */
4911 delimiter = *cmd++; /* remember delimiter character */
4912 }
4913 else /* find the end of the regexp */
4914 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004915#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4916 if (p_altkeymap && curwin->w_p_rl)
4917 lrF_sub(cmd);
4918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 which_pat = RE_LAST; /* use last used regexp */
4920 delimiter = *cmd++; /* remember delimiter character */
4921 pat = cmd; /* remember start of search pat */
4922 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4923 if (cmd[0] == delimiter) /* end delimiter found */
4924 *cmd++ = NUL; /* replace it with a NUL */
4925 }
4926
4927 /*
4928 * Small incompatibility: vi sees '\n' as end of the command, but in
4929 * Vim we want to use '\n' to find/substitute a NUL.
4930 */
4931 sub = cmd; /* remember the start of the substitution */
4932
4933 while (cmd[0])
4934 {
4935 if (cmd[0] == delimiter) /* end delimiter found */
4936 {
4937 *cmd++ = NUL; /* replace it with a NUL */
4938 break;
4939 }
4940 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4941 ++cmd;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004942 MB_PTR_ADV(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944
4945 if (!eap->skip)
4946 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004947 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4948 if (STRCMP(sub, "%") == 0
4949 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4950 {
4951 if (old_sub == NULL) /* there is no previous command */
4952 {
4953 EMSG(_(e_nopresub));
4954 return;
4955 }
4956 sub = old_sub;
4957 }
4958 else
4959 {
4960 vim_free(old_sub);
4961 old_sub = vim_strsave(sub);
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 }
4965 else if (!eap->skip) /* use previous pattern and substitution */
4966 {
4967 if (old_sub == NULL) /* there is no previous command */
4968 {
4969 EMSG(_(e_nopresub));
4970 return;
4971 }
4972 pat = NULL; /* search_regcomp() will use previous pattern */
4973 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004974
4975 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4976 * last column after using "$". */
4977 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004980 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4981 * more efficient.
4982 * TODO: find a generic solution to make line-joining operations more
4983 * efficient, avoid allocating a string that grows in size.
4984 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004985 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004986 && *sub == NUL
4987 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4988 || *cmd == 'p' || *cmd == '#'))))
4989 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004990 linenr_T joined_lines_count;
4991
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004992 curwin->w_cursor.lnum = eap->line1;
4993 if (*cmd == 'l')
4994 eap->flags = EXFLAG_LIST;
4995 else if (*cmd == '#')
4996 eap->flags = EXFLAG_NR;
4997 else if (*cmd == 'p')
4998 eap->flags = EXFLAG_PRINT;
4999
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01005000 /* The number of lines joined is the number of lines in the range plus
5001 * one. One less when the last line is included. */
5002 joined_lines_count = eap->line2 - eap->line1 + 1;
5003 if (eap->line2 < curbuf->b_ml.ml_line_count)
5004 ++joined_lines_count;
5005 if (joined_lines_count > 1)
5006 {
5007 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
5008 sub_nsubs = joined_lines_count - 1;
5009 sub_nlines = 1;
5010 (void)do_sub_msg(FALSE);
5011 ex_may_print(eap);
5012 }
5013
5014 if (!cmdmod.keeppatterns)
5015 save_re_pat(RE_SUBST, pat, p_magic);
5016#ifdef FEAT_CMDHIST
5017 /* put pattern in history */
5018 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
5019#endif
5020
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02005021 return;
5022 }
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 /*
5025 * Find trailing options. When '&' is used, keep old options.
5026 */
5027 if (*cmd == '&')
5028 ++cmd;
5029 else
5030 {
5031 if (!p_ed)
5032 {
5033 if (p_gd) /* default is global on */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005034 subflags.do_all = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005036 subflags.do_all = FALSE;
5037 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005039 subflags.do_error = TRUE;
5040 subflags.do_print = FALSE;
5041 subflags.do_count = FALSE;
5042 subflags.do_number = FALSE;
5043 subflags.do_ic = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 while (*cmd)
5046 {
5047 /*
5048 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
5049 * 'r' is never inverted.
5050 */
5051 if (*cmd == 'g')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005052 subflags.do_all = !subflags.do_all;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 else if (*cmd == 'c')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005054 subflags.do_ask = !subflags.do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005055 else if (*cmd == 'n')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005056 subflags.do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else if (*cmd == 'e')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005058 subflags.do_error = !subflags.do_error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 else if (*cmd == 'r') /* use last used regexp */
5060 which_pat = RE_LAST;
5061 else if (*cmd == 'p')
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005062 subflags.do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005063 else if (*cmd == '#')
5064 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005065 subflags.do_print = TRUE;
5066 subflags.do_number = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005067 }
5068 else if (*cmd == 'l')
5069 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005070 subflags.do_print = TRUE;
5071 subflags.do_list = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 else if (*cmd == 'i') /* ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005074 subflags.do_ic = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 else if (*cmd == 'I') /* don't ignore case */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005076 subflags.do_ic = 'I';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 else
5078 break;
5079 ++cmd;
5080 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005081 if (subflags.do_count)
5082 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005084 save_do_all = subflags.do_all;
5085 save_do_ask = subflags.do_ask;
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /*
5088 * check for a trailing count
5089 */
5090 cmd = skipwhite(cmd);
5091 if (VIM_ISDIGIT(*cmd))
5092 {
5093 i = getdigits(&cmd);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005094 if (i <= 0 && !eap->skip && subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 EMSG(_(e_zerocount));
5097 return;
5098 }
5099 eap->line1 = eap->line2;
5100 eap->line2 += i - 1;
5101 if (eap->line2 > curbuf->b_ml.ml_line_count)
5102 eap->line2 = curbuf->b_ml.ml_line_count;
5103 }
5104
5105 /*
5106 * check for trailing command or garbage
5107 */
5108 cmd = skipwhite(cmd);
5109 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
5110 {
5111 eap->nextcmd = check_nextcmd(cmd);
5112 if (eap->nextcmd == NULL)
5113 {
5114 EMSG(_(e_trailing));
5115 return;
5116 }
5117 }
5118
5119 if (eap->skip) /* not executing commands, only parsing */
5120 return;
5121
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005122 if (!subflags.do_count && !curbuf->b_p_ma)
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005123 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005124 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005125 EMSG(_(e_modifiable));
5126 return;
5127 }
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5130 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005131 if (subflags.do_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 EMSG(_(e_invcmd));
5133 return;
5134 }
5135
5136 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005137 if (subflags.do_ic == 'i')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 regmatch.rmm_ic = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005139 else if (subflags.do_ic == 'I')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 regmatch.rmm_ic = FALSE;
5141
5142 sub_firstline = NULL;
5143
5144 /*
5145 * ~ in the substitute pattern is replaced with the old pattern.
5146 * We do it here once to avoid it to be replaced over and over again.
5147 * But don't do it when it starts with "\=", then it's an expression.
5148 */
5149 if (!(sub[0] == '\\' && sub[1] == '='))
5150 sub = regtilde(sub, p_magic);
5151
5152 /*
5153 * Check for a match on each line.
5154 */
5155 line2 = eap->line2;
5156 for (lnum = eap->line1; lnum <= line2 && !(got_quit
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005157#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 || aborting()
5159#endif
5160 ); ++lnum)
5161 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005162 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005163 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (nmatch)
5165 {
5166 colnr_T copycol;
5167 colnr_T matchcol;
5168 colnr_T prev_matchcol = MAXCOL;
5169 char_u *new_end, *new_start = NULL;
5170 unsigned new_start_len = 0;
5171 char_u *p1;
5172 int did_sub = FALSE;
5173 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005174 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 long nmatch_tl = 0; /* nr of lines matched below lnum */
5176 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005177 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005178 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 /*
5181 * The new text is build up step by step, to avoid too much
5182 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005183 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 * copycol Column in the old text where we started
5185 * looking for a match; from here old text still
5186 * needs to be copied to the new text.
5187 * matchcol Column number of the old text where to look
5188 * for the next match. It's just after the
5189 * previous match or one further.
5190 * prev_matchcol Column just after the previous match (if any).
5191 * Mostly equal to matchcol, except for the first
5192 * match and after skipping an empty match.
5193 * regmatch.*pos Where the pattern matched in the old text.
5194 * new_start The new text, all that has been produced so
5195 * far.
5196 * new_end The new text, where to append new text.
5197 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005198 * lnum The line number where we found the start of
5199 * the match. Can be below the line we searched
5200 * when there is a \n before a \zs in the
5201 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 * sub_firstlnum The line number in the buffer where to look
5203 * for a match. Can be different from "lnum"
5204 * when the pattern or substitute string contains
5205 * line breaks.
5206 *
5207 * Special situations:
5208 * - When the substitute string contains a line break, the part up
5209 * to the line break is inserted in the text, but the copy of
5210 * the original line is kept. "sub_firstlnum" is adjusted for
5211 * the inserted lines.
5212 * - When the matched pattern contains a line break, the old line
5213 * is taken from the line at the end of the pattern. The lines
5214 * in the match are deleted later, "sub_firstlnum" is adjusted
5215 * accordingly.
5216 *
5217 * The new text is built up in new_start[]. It has some extra
5218 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005219 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 *
5221 * Make a copy of the old line, so it won't be taken away when
5222 * updating the screen or handling a multi-line match. The "old_"
5223 * pointers point into this copy.
5224 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005225 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 copycol = 0;
5227 matchcol = 0;
5228
5229 /* At first match, remember current cursor position. */
5230 if (!got_match)
5231 {
5232 setpcmark();
5233 got_match = TRUE;
5234 }
5235
5236 /*
5237 * Loop until nothing more to replace in this line.
5238 * 1. Handle match with empty string.
5239 * 2. If do_ask is set, ask for confirmation.
5240 * 3. substitute the string.
5241 * 4. if do_all is set, find next match
5242 * 5. break if there isn't another match in this line
5243 */
5244 for (;;)
5245 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005246 /* Advance "lnum" to the line where the match starts. The
5247 * match does not start in the first line when there is a line
5248 * break before \zs. */
5249 if (regmatch.startpos[0].lnum > 0)
5250 {
5251 lnum += regmatch.startpos[0].lnum;
5252 sub_firstlnum += regmatch.startpos[0].lnum;
5253 nmatch -= regmatch.startpos[0].lnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +01005254 VIM_CLEAR(sub_firstline);
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005255 }
5256
5257 if (sub_firstline == NULL)
5258 {
5259 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5260 if (sub_firstline == NULL)
5261 {
5262 vim_free(new_start);
5263 goto outofmem;
5264 }
5265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 /* Save the line number of the last change for the final
5268 * cursor position (just like Vi). */
5269 curwin->w_cursor.lnum = lnum;
5270 do_again = FALSE;
5271
5272 /*
5273 * 1. Match empty string does not count, except for first
5274 * match. This reproduces the strange vi behaviour.
5275 * This also catches endless loops.
5276 */
5277 if (matchcol == prev_matchcol
5278 && regmatch.endpos[0].lnum == 0
5279 && matchcol == regmatch.endpos[0].col)
5280 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005281 if (sub_firstline[matchcol] == NUL)
5282 /* We already were at the end of the line. Don't look
5283 * for a match in this line again. */
5284 skip_match = TRUE;
5285 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005286 {
5287 /* search for a match at next column */
5288#ifdef FEAT_MBYTE
5289 if (has_mbyte)
5290 matchcol += mb_ptr2len(sub_firstline + matchcol);
5291 else
5292#endif
5293 ++matchcol;
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 goto skip;
5296 }
5297
5298 /* Normally we continue searching for a match just after the
5299 * previous match. */
5300 matchcol = regmatch.endpos[0].col;
5301 prev_matchcol = matchcol;
5302
5303 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005304 * 2. If do_count is set only increase the counter.
5305 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005307 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005308 {
5309 /* For a multi-line match, put matchcol at the NUL at
5310 * the end of the line and set nmatch to one, so that
5311 * we continue looking for a match on the next line.
5312 * Avoids that ":s/\nB\@=//gc" get stuck. */
5313 if (nmatch > 1)
5314 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005315 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005316 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005317 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005318 }
5319 sub_nsubs++;
5320 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005321#ifdef FEAT_EVAL
5322 /* Skip the substitution, unless an expression is used,
5323 * then it is evaluated in the sandbox. */
5324 if (!(sub[0] == '\\' && sub[1] == '='))
5325#endif
5326 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005327 }
5328
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005329 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005331 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 /* change State to CONFIRM, so that the mouse works
5334 * properly */
5335 save_State = State;
5336 State = CONFIRM;
5337#ifdef FEAT_MOUSE
5338 setmouse(); /* disable mouse in xterm */
5339#endif
5340 curwin->w_cursor.col = regmatch.startpos[0].col;
Bram Moolenaar41baa792017-01-21 14:45:09 +01005341 if (curwin->w_p_crb)
5342 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 /* When 'cpoptions' contains "u" don't sync undo when
5345 * asking for confirmation. */
5346 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5347 ++no_u_sync;
5348
5349 /*
5350 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5351 */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005352 while (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005354 if (exmode_active)
5355 {
5356 char_u *resp;
5357 colnr_T sc, ec;
5358
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005359 print_line_no_prefix(lnum,
5360 subflags.do_number, subflags.do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005361
5362 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5363 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01005364 if (curwin->w_cursor.col < 0)
5365 curwin->w_cursor.col = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005366 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005367 if (subflags.do_number || curwin->w_p_nu)
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005368 {
5369 int numw = number_width(curwin) + 1;
5370 sc += numw;
5371 ec += numw;
5372 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005373 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005374 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005375 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005376 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005377 msg_putchar('^');
5378
5379 resp = getexmodeline('?', NULL, 0);
5380 if (resp != NULL)
5381 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005382 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005383 vim_free(resp);
5384 }
5385 }
5386 else
5387 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005388 char_u *orig_line = NULL;
5389 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005391 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005393 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005395 /* Invert the matched string.
5396 * Remove the inversion afterwards. */
5397 temp = RedrawingDisabled;
5398 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005400 if (new_start != NULL)
5401 {
5402 /* There already was a substitution, we would
5403 * like to show this to the user. We cannot
5404 * really update the line, it would change
5405 * what matches. Temporarily replace the line
5406 * and change it back afterwards. */
5407 orig_line = vim_strsave(ml_get(lnum));
5408 if (orig_line != NULL)
5409 {
5410 char_u *new_line = concat_str(new_start,
5411 sub_firstline + copycol);
5412
5413 if (new_line == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005414 VIM_CLEAR(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005415 else
5416 {
5417 /* Position the cursor relative to the
5418 * end of the line, the previous
5419 * substitute may have inserted or
5420 * deleted characters before the
5421 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005422 len_change = (int)STRLEN(new_line)
5423 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005424 curwin->w_cursor.col += len_change;
5425 ml_replace(lnum, new_line, FALSE);
5426 }
5427 }
5428 }
5429
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005430 search_match_lines = regmatch.endpos[0].lnum
5431 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005432 search_match_endcol = regmatch.endpos[0].col
5433 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005434 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005436 update_topline();
5437 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005438 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005439 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005440 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005443 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005445 if (msg_row == Rows - 1)
5446 msg_didout = FALSE; /* avoid a scroll-up */
5447 msg_starthere();
5448 i = msg_scroll;
5449 msg_scroll = 0; /* truncate msg when
5450 needed */
5451 msg_no_more = TRUE;
5452 /* write message same highlighting as for
5453 * wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005454 smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005455 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
5456 msg_no_more = FALSE;
5457 msg_scroll = i;
5458 showruler(TRUE);
5459 windgoto(msg_row, msg_col);
5460 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005463 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005465 ++no_mapping; /* don't map this key */
5466 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005467 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005468 --allow_keys;
5469 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005471 /* clear the question */
5472 msg_didout = FALSE; /* don't scroll up */
5473 msg_col = 0;
5474 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005475
5476 /* restore the line */
5477 if (orig_line != NULL)
5478 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005479 }
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005482 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005484 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485#endif
5486 )
5487 {
5488 got_quit = TRUE;
5489 break;
5490 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005491 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005493 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005495 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 /* last: replace and then stop */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005498 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 line2 = lnum;
5500 break;
5501 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005502 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005504 subflags.do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 break;
5506 }
5507#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005508 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005510 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 scrolldown_clamp();
5512#endif
5513 }
5514 State = save_State;
5515#ifdef FEAT_MOUSE
5516 setmouse();
5517#endif
5518 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5519 --no_u_sync;
5520
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005521 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
5523 /* For a multi-line match, put matchcol at the NUL at
5524 * the end of the line and set nmatch to one, so that
5525 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005526 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5527 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (nmatch > 1)
5529 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005530 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005531 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533 goto skip;
5534 }
5535 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005536 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538
5539 /* Move the cursor to the start of the match, so that we can
5540 * use "\=col("."). */
5541 curwin->w_cursor.col = regmatch.startpos[0].col;
5542
5543 /*
5544 * 3. substitute the string.
5545 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005546#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005547 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005548 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005549 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005550 save_ma = curbuf->b_p_ma;
5551 curbuf->b_p_ma = FALSE;
5552 sandbox++;
5553 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005554 /* Save flags for recursion. They can change for e.g.
5555 * :s/^/\=execute("s#^##gn") */
5556 subflags_save = subflags;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005559 sublen = vim_regsub_multi(&regmatch,
5560 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005562#ifdef FEAT_EVAL
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005563 /* Don't keep flags set by a recursive call. */
5564 subflags = subflags_save;
5565 if (subflags.do_count)
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005566 {
5567 curbuf->b_p_ma = save_ma;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005568 if (sandbox > 0)
5569 sandbox--;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005570 goto skip;
5571 }
5572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
Bram Moolenaar4463f292005-09-25 22:20:24 +00005574 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005575 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005576 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5577 {
5578 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5579 skip_match = TRUE;
5580 }
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 /* Need room for:
5583 * - result so far in new_start (not for first sub in line)
5584 * - original text up to match
5585 * - length of substituted part
5586 * - original text after match
5587 */
5588 if (nmatch == 1)
5589 p1 = sub_firstline;
5590 else
5591 {
5592 p1 = ml_get(sub_firstlnum + nmatch - 1);
5593 nmatch_tl += nmatch - 1;
5594 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005595 copy_len = regmatch.startpos[0].col - copycol;
5596 needed_len = copy_len + ((unsigned)STRLEN(p1)
5597 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (new_start == NULL)
5599 {
5600 /*
5601 * Get some space for a temporary buffer to do the
5602 * substitution into (and some extra space to avoid
5603 * too many calls to alloc()/free()).
5604 */
5605 new_start_len = needed_len + 50;
5606 if ((new_start = alloc_check(new_start_len)) == NULL)
5607 goto outofmem;
5608 *new_start = NUL;
5609 new_end = new_start;
5610 }
5611 else
5612 {
5613 /*
5614 * Check if the temporary buffer is long enough to do the
5615 * substitution into. If not, make it larger (with a bit
5616 * extra to avoid too many calls to alloc()/free()).
5617 */
5618 len = (unsigned)STRLEN(new_start);
5619 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005620 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 new_start_len = needed_len + 50;
5623 if ((p1 = alloc_check(new_start_len)) == NULL)
5624 {
5625 vim_free(new_start);
5626 goto outofmem;
5627 }
5628 mch_memmove(p1, new_start, (size_t)(len + 1));
5629 vim_free(new_start);
5630 new_start = p1;
5631 }
5632 new_end = new_start + len;
5633 }
5634
5635 /*
5636 * copy the text up to the part that matched
5637 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005638 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5639 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005641 (void)vim_regsub_multi(&regmatch,
5642 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 sub, new_end, TRUE, p_magic, TRUE);
5644 sub_nsubs++;
5645 did_sub = TRUE;
5646
5647 /* Move the cursor to the start of the line, to avoid that it
5648 * is beyond the end of the line after the substitution. */
5649 curwin->w_cursor.col = 0;
5650
5651 /* For a multi-line match, make a copy of the last matched
5652 * line and continue in that one. */
5653 if (nmatch > 1)
5654 {
5655 sub_firstlnum += nmatch - 1;
5656 vim_free(sub_firstline);
5657 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5658 /* When going beyond the last line, stop substituting. */
5659 if (sub_firstlnum <= line2)
5660 do_again = TRUE;
5661 else
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005662 subflags.do_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664
5665 /* Remember next character to be copied. */
5666 copycol = regmatch.endpos[0].col;
5667
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005668 if (skip_match)
5669 {
5670 /* Already hit end of the buffer, sub_firstlnum is one
5671 * less than what it ought to be. */
5672 vim_free(sub_firstline);
5673 sub_firstline = vim_strsave((char_u *)"");
5674 copycol = 0;
5675 }
5676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 /*
5678 * Now the trick is to replace CTRL-M chars with a real line
5679 * break. This would make it impossible to insert a CTRL-M in
5680 * the text. The line break can be avoided by preceding the
5681 * CTRL-M with a backslash. To be able to insert a backslash,
5682 * they must be doubled in the string and are halved here.
5683 * That is Vi compatible.
5684 */
5685 for (p1 = new_end; *p1; ++p1)
5686 {
5687 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005688 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 else if (*p1 == CAR)
5690 {
5691 if (u_inssub(lnum) == OK) /* prepare for undo */
5692 {
5693 *p1 = NUL; /* truncate up to the CR */
5694 ml_append(lnum - 1, new_start,
5695 (colnr_T)(p1 - new_start + 1), FALSE);
5696 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005697 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 appended_lines(lnum - 1, 1L);
5699 else
5700 {
5701 if (first_line == 0)
5702 first_line = lnum;
5703 last_line = lnum + 1;
5704 }
5705 /* All line numbers increase. */
5706 ++sub_firstlnum;
5707 ++lnum;
5708 ++line2;
5709 /* move the cursor to the new line, like Vi */
5710 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005711 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005712 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 p1 = new_start - 1;
5714 }
5715 }
5716#ifdef FEAT_MBYTE
5717 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005718 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#endif
5720 }
5721
5722 /*
5723 * 4. If do_all is set, find next match.
5724 * Prevent endless loop with patterns that match empty
5725 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5726 * But ":s/\n/#/" is OK.
5727 */
5728skip:
5729 /* We already know that we did the last subst when we are at
5730 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005731 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5732 * "line2" when there is a \zs in the pattern after a line
5733 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005734 lastone = (skip_match
5735 || got_int
5736 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005737 || lnum > line2
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005738 || !(subflags.do_all || do_again)
Bram Moolenaar8299df92004-07-10 09:47:34 +00005739 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5740 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 nmatch = -1;
5742
5743 /*
5744 * Replace the line in the buffer when needed. This is
5745 * skipped when there are more matches.
5746 * The check for nmatch_tl is needed for when multi-line
5747 * matching must replace the lines before trying to do another
5748 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005749 * When the match starts below where we start searching also
5750 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 */
5752 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 || nmatch_tl > 0
5754 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005755 curbuf, sub_firstlnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005756 matchcol, NULL, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005757 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
5759 if (new_start != NULL)
5760 {
5761 /*
5762 * Copy the rest of the line, that didn't match.
5763 * "matchcol" has to be adjusted, we use the end of
5764 * the line as reference, because the substitute may
5765 * have changed the number of characters. Same for
5766 * "prev_matchcol".
5767 */
5768 STRCAT(new_start, sub_firstline + copycol);
5769 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5770 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5771 - prev_matchcol;
5772
5773 if (u_savesub(lnum) != OK)
5774 break;
5775 ml_replace(lnum, new_start, TRUE);
5776
5777 if (nmatch_tl > 0)
5778 {
5779 /*
5780 * Matched lines have now been substituted and are
5781 * useless, delete them. The part after the match
5782 * has been appended to new_start, we don't need
5783 * it in the buffer.
5784 */
5785 ++lnum;
5786 if (u_savedel(lnum, nmatch_tl) != OK)
5787 break;
5788 for (i = 0; i < nmatch_tl; ++i)
5789 ml_delete(lnum, (int)FALSE);
5790 mark_adjust(lnum, lnum + nmatch_tl - 1,
5791 (long)MAXLNUM, -nmatch_tl);
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005792 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 deleted_lines(lnum, nmatch_tl);
5794 --lnum;
5795 line2 -= nmatch_tl; /* nr of lines decreases */
5796 nmatch_tl = 0;
5797 }
5798
5799 /* When asking, undo is saved each time, must also set
5800 * changed flag each time. */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005801 if (subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 changed_bytes(lnum, 0);
5803 else
5804 {
5805 if (first_line == 0)
5806 first_line = lnum;
5807 last_line = lnum + 1;
5808 }
5809
5810 sub_firstlnum = lnum;
5811 vim_free(sub_firstline); /* free the temp buffer */
5812 sub_firstline = new_start;
5813 new_start = NULL;
5814 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5815 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5816 - prev_matchcol;
5817 copycol = 0;
5818 }
5819 if (nmatch == -1 && !lastone)
5820 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005821 sub_firstlnum, matchcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822
5823 /*
5824 * 5. break if there isn't another match in this line
5825 */
5826 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005827 {
5828 /* If the match found didn't start where we were
5829 * searching, do the next search in the line where we
5830 * found the match. */
5831 if (nmatch == -1)
5832 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
5836
5837 line_breakcheck();
5838 }
5839
5840 if (did_sub)
5841 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005842 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaard23a8232018-02-10 18:45:26 +01005843 VIM_CLEAR(sub_firstline); /* free the copy of the original line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 }
5845
5846 line_breakcheck();
5847 }
5848
5849 if (first_line != 0)
5850 {
5851 /* Need to subtract the number of added lines from "last_line" to get
5852 * the line number before the change (same as adding the number of
5853 * deleted lines). */
5854 i = curbuf->b_ml.ml_line_count - old_line_count;
5855 changed_lines(first_line, 0, last_line - i, i);
5856 }
5857
5858outofmem:
5859 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005860
5861 /* ":s/pat//n" doesn't move the cursor */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005862 if (subflags.do_count)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005863 curwin->w_cursor = old_cursor;
5864
Bram Moolenaar46475522010-03-23 17:36:29 +01005865 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {
5867 /* Set the '[ and '] marks. */
5868 curbuf->b_op_start.lnum = eap->line1;
5869 curbuf->b_op_end.lnum = line2;
5870 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5871
5872 if (!global_busy)
5873 {
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005874 /* when interactive leave cursor on the match */
5875 if (!subflags.do_ask)
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005876 {
5877 if (endcolumn)
5878 coladvance((colnr_T)MAXCOL);
5879 else
5880 beginline(BL_WHITE | BL_FIX);
5881 }
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005882 if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 MSG("");
5884 }
5885 else
5886 global_need_beginline = TRUE;
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005887 if (subflags.do_print)
5888 print_line(curwin->w_cursor.lnum,
5889 subflags.do_number, subflags.do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891 else if (!global_busy)
5892 {
5893 if (got_int) /* interrupted */
5894 EMSG(_(e_interr));
5895 else if (got_match) /* did find something but nothing substituted */
5896 MSG("");
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005897 else if (subflags.do_error) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 EMSG2(_(e_patnotf2), get_search_pat());
5899 }
5900
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005901#ifdef FEAT_FOLDING
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005902 if (subflags.do_ask && hasAnyFolding(curwin))
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005903 /* Cursor position may require updating */
5904 changed_window_setting();
5905#endif
5906
Bram Moolenaar473de612013-06-08 18:19:48 +02005907 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005908
5909 /* Restore the flag values, they can be used for ":&&". */
Bram Moolenaarf5a39442016-08-16 21:04:41 +02005910 subflags.do_all = save_do_all;
5911 subflags.do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912}
5913
5914/*
5915 * Give message for number of substitutions.
5916 * Can also be used after a ":global" command.
5917 * Return TRUE if a message was given.
5918 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005919 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005920do_sub_msg(
5921 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 /*
5924 * Only report substitutions when:
5925 * - more than 'report' substitutions
5926 * - command was typed by user, or number of changed lines > 'report'
5927 * - giving messages is not disabled by 'lazyredraw'
5928 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005929 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5930 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 && messaging())
5932 {
5933 if (got_int)
5934 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005935 else
5936 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005938 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005939 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005941 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005942 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 sub_nsubs);
5944 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005945 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005946 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005948 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005949 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005952 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 return TRUE;
5954 }
5955 if (got_int)
5956 {
5957 EMSG(_(e_interr));
5958 return TRUE;
5959 }
5960 return FALSE;
5961}
5962
Bram Moolenaarf84b1222017-06-10 14:29:52 +02005963 static void
5964global_exe_one(char_u *cmd, linenr_T lnum)
5965{
5966 curwin->w_cursor.lnum = lnum;
5967 curwin->w_cursor.col = 0;
5968 if (*cmd == NUL || *cmd == '\n')
5969 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5970 else
5971 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5972}
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974/*
5975 * Execute a global command of the form:
5976 *
5977 * g/pattern/X : execute X on all lines where pattern matches
5978 * v/pattern/X : execute X on all lines where pattern does not match
5979 *
5980 * where 'X' is an EX command
5981 *
5982 * The command character (as well as the trailing slash) is optional, and
5983 * is assumed to be 'p' if missing.
5984 *
5985 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005986 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 * for each line that has a mark. This is required because after deleting
5988 * lines we do not know where to search for the next match.
5989 */
5990 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005991ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005994 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 int type; /* first char of cmd: 'v' or 'g' */
5996 char_u *cmd; /* command argument */
5997
5998 char_u delim; /* delimiter, normally '/' */
5999 char_u *pat;
6000 regmmatch_T regmatch;
6001 int match;
6002 int which_pat;
6003
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006004 /* When nesting the command works on one line. This allows for
6005 * ":g/found/v/notfound/command". */
6006 if (global_busy && (eap->line1 != 1
6007 || eap->line2 != curbuf->b_ml.ml_line_count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006009 /* will increment global_busy to break out of the loop */
6010 EMSG(_("E147: Cannot do :global recursive with a range"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 return;
6012 }
6013
6014 if (eap->forceit) /* ":global!" is like ":vglobal" */
6015 type = 'v';
6016 else
6017 type = *eap->cmd;
6018 cmd = eap->arg;
6019 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021 /*
6022 * undocumented vi feature:
6023 * "\/" and "\?": use previous search pattern.
6024 * "\&": use previous substitute pattern.
6025 */
6026 if (*cmd == '\\')
6027 {
6028 ++cmd;
6029 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
6030 {
6031 EMSG(_(e_backslash));
6032 return;
6033 }
6034 if (*cmd == '&')
6035 which_pat = RE_SUBST; /* use previous substitute pattern */
6036 else
6037 which_pat = RE_SEARCH; /* use previous search pattern */
6038 ++cmd;
6039 pat = (char_u *)"";
6040 }
6041 else if (*cmd == NUL)
6042 {
6043 EMSG(_("E148: Regular expression missing from global"));
6044 return;
6045 }
6046 else
6047 {
6048 delim = *cmd; /* get the delimiter */
6049 if (delim)
6050 ++cmd; /* skip delimiter if there is one */
6051 pat = cmd; /* remember start of pattern */
6052 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
6053 if (cmd[0] == delim) /* end delimiter found */
6054 *cmd++ = NUL; /* replace it with a NUL */
6055 }
6056
6057#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
6058 if (p_altkeymap && curwin->w_p_rl)
6059 lrFswap(pat,0);
6060#endif
6061
6062 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
6063 {
6064 EMSG(_(e_invcmd));
6065 return;
6066 }
6067
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006068 if (global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006070 lnum = curwin->w_cursor.lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006071 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006072 (colnr_T)0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if ((type == 'g' && match) || (type == 'v' && !match))
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006074 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006077 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006078 /*
6079 * pass 1: set marks for each (not) matching line
6080 */
6081 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
6082 {
6083 /* a match on this line? */
6084 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006085 (colnr_T)0, NULL, NULL);
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006086 if ((type == 'g' && match) || (type == 'v' && !match))
6087 {
6088 ml_setmarked(lnum);
6089 ndone++;
6090 }
6091 line_breakcheck();
6092 }
6093
6094 /*
6095 * pass 2: execute the command for each line that has been marked
6096 */
6097 if (got_int)
6098 MSG(_(e_interr));
6099 else if (ndone == 0)
6100 {
6101 if (type == 'v')
6102 smsg((char_u *)_("Pattern found in every line: %s"), pat);
6103 else
6104 smsg((char_u *)_("Pattern not found: %s"), pat);
6105 }
6106 else
6107 {
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006108#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006109 start_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006110#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006111 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006112#ifdef FEAT_CLIPBOARD
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006113 end_global_changes();
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006114#endif
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006115 }
6116
6117 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02006118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaar473de612013-06-08 18:19:48 +02006120 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121}
6122
6123/*
6124 * Execute "cmd" on lines marked with ml_setmarked().
6125 */
6126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
Bram Moolenaarbb993222011-05-10 16:00:47 +02006129 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
6130 buf_T *old_buf = curbuf; /* remember what buffer we started in */
6131 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 /*
6134 * Set current position only once for a global command.
6135 * If global_busy is set, setpcmark() will not do anything.
6136 * If there is an error, global_busy will be incremented.
6137 */
6138 setpcmark();
6139
6140 /* When the command writes a message, don't overwrite the command. */
6141 msg_didout = TRUE;
6142
Bram Moolenaard25bc232010-03-23 17:49:24 +01006143 sub_nsubs = 0;
6144 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 global_need_beginline = FALSE;
6146 global_busy = 1;
6147 old_lcount = curbuf->b_ml.ml_line_count;
6148 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
6149 {
Bram Moolenaarf84b1222017-06-10 14:29:52 +02006150 global_exe_one(cmd, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 ui_breakcheck();
6152 }
6153
6154 global_busy = 0;
6155 if (global_need_beginline)
6156 beginline(BL_WHITE | BL_FIX);
6157 else
6158 check_cursor(); /* cursor may be beyond the end of the line */
6159
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006160 /* the cursor may not have moved in the text but a change in a previous
6161 * line may move it on the screen */
6162 changed_line_abv_curs();
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 /* If it looks like no message was written, allow overwriting the
6165 * command with the report for number of changes. */
6166 if (msg_col == 0 && msg_scrolled == 0)
6167 msg_didout = FALSE;
6168
Bram Moolenaar45667512007-05-10 19:24:43 +00006169 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006170 * number of extra or deleted lines.
6171 * Don't report extra or deleted lines in the edge case where the buffer
6172 * we are in after execution is different from the buffer we started in. */
6173 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6175}
6176
6177#ifdef FEAT_VIMINFO
6178 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006181 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 vim_free(old_sub);
6183 if (force || old_sub == NULL)
6184 old_sub = viminfo_readstring(virp, 1, TRUE);
6185 return viminfo_readline(virp);
6186}
6187
6188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6192 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006193 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 viminfo_writestring(fp, old_sub);
6195 }
6196}
6197#endif /* FEAT_VIMINFO */
6198
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006199#if defined(EXITFREE) || defined(PROTO)
6200 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006201free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006202{
6203 vim_free(old_sub);
6204}
6205#endif
6206
Bram Moolenaar4033c552017-09-16 20:54:51 +02006207#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208/*
6209 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006210 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006212 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006213prepare_tagpreview(
6214 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 win_T *wp;
6217
6218# ifdef FEAT_GUI
6219 need_mouse_correct = TRUE;
6220# endif
6221
6222 /*
6223 * If there is already a preview window open, use that one.
6224 */
6225 if (!curwin->w_p_pvw)
6226 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006227 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (wp->w_p_pvw)
6229 break;
6230 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006231 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 else
6233 {
6234 /*
6235 * There is no preview window open yet. Create one.
6236 */
6237 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6238 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006239 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 curwin->w_p_pvw = TRUE;
6241 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006242 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6243 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244# ifdef FEAT_DIFF
6245 curwin->w_p_diff = FALSE; /* no 'diff' */
6246# endif
6247# ifdef FEAT_FOLDING
6248 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6249# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006250 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006253 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254}
6255
6256#endif
6257
6258
6259/*
6260 * ":help": open a read-only window on a help file
6261 */
6262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006263ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 char_u *arg;
6266 char_u *tag;
6267 FILE *helpfd; /* file descriptor of help file */
6268 int n;
6269 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 int num_matches;
6272 char_u **matches;
6273 char_u *p;
6274 int empty_fnum = 0;
6275 int alt_fnum = 0;
6276 buf_T *buf;
6277#ifdef FEAT_MULTI_LANG
6278 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006279 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006281#ifdef FEAT_FOLDING
6282 int old_KeyTyped = KeyTyped;
6283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 if (eap != NULL)
6286 {
6287 /*
6288 * A ":help" command ends at the first LF, or at a '|' that is
6289 * followed by some text. Set nextcmd to the following command.
6290 */
6291 for (arg = eap->arg; *arg; ++arg)
6292 {
6293 if (*arg == '\n' || *arg == '\r'
6294 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6295 {
6296 *arg++ = NUL;
6297 eap->nextcmd = arg;
6298 break;
6299 }
6300 }
6301 arg = eap->arg;
6302
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006303 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
6305 EMSG(_("E478: Don't panic!"));
6306 return;
6307 }
6308
6309 if (eap->skip) /* not executing commands */
6310 return;
6311 }
6312 else
6313 arg = (char_u *)"";
6314
6315 /* remove trailing blanks */
6316 p = arg + STRLEN(arg) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006317 while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 *p-- = NUL;
6319
6320#ifdef FEAT_MULTI_LANG
6321 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006322 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323#endif
6324
6325 /* When no argument given go to the index. */
6326 if (*arg == NUL)
6327 arg = (char_u *)"help.txt";
6328
6329 /*
6330 * Check if there is a match for the argument.
6331 */
6332 n = find_help_tags(arg, &num_matches, &matches,
6333 eap != NULL && eap->forceit);
6334
6335 i = 0;
6336#ifdef FEAT_MULTI_LANG
6337 if (n != FAIL && lang != NULL)
6338 /* Find first item with the requested language. */
6339 for (i = 0; i < num_matches; ++i)
6340 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006341 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (len > 3 && matches[i][len - 3] == '@'
6343 && STRICMP(matches[i] + len - 2, lang) == 0)
6344 break;
6345 }
6346#endif
6347 if (i >= num_matches || n == FAIL)
6348 {
6349#ifdef FEAT_MULTI_LANG
6350 if (lang != NULL)
6351 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
6352 else
6353#endif
6354 EMSG2(_("E149: Sorry, no help for %s"), arg);
6355 if (n != FAIL)
6356 FreeWild(num_matches, matches);
6357 return;
6358 }
6359
6360 /* The first match (in the requested language) is the best match. */
6361 tag = vim_strsave(matches[i]);
6362 FreeWild(num_matches, matches);
6363
6364#ifdef FEAT_GUI
6365 need_mouse_correct = TRUE;
6366#endif
6367
6368 /*
6369 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006370 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006372 if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006374 if (cmdmod.tab != 0)
6375 wp = NULL;
6376 else
Bram Moolenaar29323592016-07-24 22:04:11 +02006377 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006378 if (bt_help(wp->w_buffer))
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6381 win_enter(wp, TRUE);
6382 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
6384 /*
6385 * There is no help window yet.
6386 * Try to open the file specified by the "helpfile" option.
6387 */
6388 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6389 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006390 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 goto erret;
6392 }
6393 fclose(helpfd);
6394
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006396 * specified, the current window is vertically split and
6397 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006400 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006403 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (curwin->w_height < p_hh)
6406 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408 /*
6409 * Open help file (do_ecmd() will set b_help flag, readfile() will
6410 * set b_p_ro flag).
6411 * Set the alternate file to the previously edited file.
6412 */
6413 alt_fnum = curbuf->b_fnum;
6414 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006415 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaar4033c552017-09-16 20:54:51 +02006416 NULL); /* buffer is still open, don't store info */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006417 if (!cmdmod.keepalt)
6418 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 empty_fnum = curbuf->b_fnum;
6420 }
6421 }
6422
6423 if (!p_im)
6424 restart_edit = 0; /* don't want insert mode in help file */
6425
Bram Moolenaar8f535582011-09-30 17:30:31 +02006426#ifdef FEAT_FOLDING
6427 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6428 * It is needed for do_tag top open folds under the cursor. */
6429 KeyTyped = old_KeyTyped;
6430#endif
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (tag != NULL)
6433 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6434
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006435 /* Delete the empty buffer if we're not using it. Careful: autocommands
6436 * may have jumped to another window, check that the buffer is not in a
6437 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6439 {
6440 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006441 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 wipe_buffer(buf, TRUE);
6443 }
6444
6445 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006446 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 curwin->w_alt_fnum = alt_fnum;
6448
6449erret:
6450 vim_free(tag);
6451}
6452
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006453/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006454 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006455 */
6456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006457ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006458{
6459 win_T *win;
6460
6461 FOR_ALL_WINDOWS(win)
6462 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006463 if (bt_help(win->w_buffer))
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006464 {
6465 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006466 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006467 }
6468 }
6469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006471#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6472/*
6473 * In an argument search for a language specifiers in the form "@xx".
6474 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6475 * Returns NULL if not found.
6476 */
6477 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006478check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006479{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006480 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006481
6482 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6483 && ASCII_ISALPHA(arg[len - 1]))
6484 {
6485 arg[len - 3] = NUL; /* remove the '@' */
6486 return arg + len - 2;
6487 }
6488 return NULL;
6489}
6490#endif
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492/*
6493 * Return a heuristic indicating how well the given string matches. The
6494 * smaller the number, the better the match. This is the order of priorities,
6495 * from best match to worst match:
6496 * - Match with least alpha-numeric characters is better.
6497 * - Match with least total characters is better.
6498 * - Match towards the start is better.
6499 * - Match starting with "+" is worse (feature instead of command)
6500 * Assumption is made that the matched_string passed has already been found to
6501 * match some string for which help is requested. webb.
6502 */
6503 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006504help_heuristic(
6505 char_u *matched_string,
6506 int offset, /* offset for match */
6507 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 int num_letters;
6510 char_u *p;
6511
6512 num_letters = 0;
6513 for (p = matched_string; *p; p++)
6514 if (ASCII_ISALNUM(*p))
6515 num_letters++;
6516
6517 /*
6518 * Multiply the number of letters by 100 to give it a much bigger
6519 * weighting than the number of characters.
6520 * If there only is a match while ignoring case, add 5000.
6521 * If the match starts in the middle of a word, add 10000 to put it
6522 * somewhere in the last half.
6523 * If the match is more than 2 chars from the start, multiply by 200 to
6524 * put it after matches at the start.
6525 */
6526 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6527 && ASCII_ISALNUM(matched_string[offset - 1]))
6528 offset += 10000;
6529 else if (offset > 2)
6530 offset *= 200;
6531 if (wrong_case)
6532 offset += 5000;
6533 /* Features are less interesting than the subjects themselves, but "+"
6534 * alone is not a feature. */
6535 if (matched_string[0] == '+' && matched_string[1] != NUL)
6536 offset += 100;
6537 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6538}
6539
6540/*
6541 * Compare functions for qsort() below, that checks the help heuristics number
6542 * that has been put after the tagname by find_tags().
6543 */
6544 static int
6545#ifdef __BORLANDC__
6546_RTLENTRYF
6547#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006548help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 char *p1;
6551 char *p2;
6552
6553 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6554 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6555 return strcmp(p1, p2);
6556}
6557
6558/*
6559 * Find all help tags matching "arg", sort them and return in matches[], with
6560 * the number of matches in num_matches.
6561 * The matches will be sorted with a "best" match algorithm.
6562 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6563 */
6564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565find_help_tags(
6566 char_u *arg,
6567 int *num_matches,
6568 char_u ***matches,
6569 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 char_u *s, *d;
6572 int i;
6573 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006574 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006575 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaardad73092017-02-09 11:54:50 +01006576 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006577 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006578 "[count]", "[quotex]",
6579 "[range]", ":[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006580 "[pattern]", "\\|", "\\%$",
6581 "s/\\~", "s/\\U", "s/\\L",
6582 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006584 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006585 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaardad73092017-02-09 11:54:50 +01006586 "?", ":?", "?<CR>", "g?", "g?g?", "g??",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006587 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaara76f59d2017-02-09 11:41:01 +01006588 "\\[count]", "\\[quotex]",
6589 "\\[range]", ":\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006590 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006591 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006592 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 int flags;
6594
6595 d = IObuff; /* assume IObuff is long enough! */
6596
6597 /*
6598 * Recognize a few exceptions to the rule. Some strings that contain '*'
6599 * with "star". Otherwise '*' is recognized as a wildcard.
6600 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006601 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 if (STRCMP(arg, mtable[i]) == 0)
6603 {
6604 STRCPY(d, rtable[i]);
6605 break;
6606 }
6607
6608 if (i < 0) /* no match in table */
6609 {
6610 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6611 * Also replace "\%^" and "\%(", they match every tag too.
6612 * Also "\zs", "\z1", etc.
6613 * Also "\@<", "\@=", "\@<=", etc.
6614 * And also "\_$" and "\_^". */
6615 if (arg[0] == '\\'
6616 && ((arg[1] != NUL && arg[2] == NUL)
6617 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6618 && arg[2] != NUL)))
6619 {
6620 STRCPY(d, "/\\\\");
6621 STRCPY(d + 3, arg + 1);
6622 /* Check for "/\\_$", should be "/\\_\$" */
6623 if (d[3] == '_' && d[4] == '$')
6624 STRCPY(d + 4, "\\$");
6625 }
6626 else
6627 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006628 /* Replace:
6629 * "[:...:]" with "\[:...:]"
6630 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006631 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006632 */
6633 if ((arg[0] == '[' && (arg[1] == ':'
6634 || (arg[1] == '+' && arg[2] == '+')))
6635 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 *d++ = '\\';
6637
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006638 /*
6639 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6640 */
6641 if (*arg == '(' && arg[1] == '\'')
6642 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 for (s = arg; *s; ++s)
6644 {
6645 /*
6646 * Replace "|" with "bar" and '"' with "quote" to match the name of
6647 * the tags for these commands.
6648 * Replace "*" with ".*" and "?" with "." to match command line
6649 * completion.
6650 * Insert a backslash before '~', '$' and '.' to avoid their
6651 * special meaning.
6652 */
6653 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6654 break;
6655 switch (*s)
6656 {
6657 case '|': STRCPY(d, "bar");
6658 d += 3;
6659 continue;
6660 case '"': STRCPY(d, "quote");
6661 d += 5;
6662 continue;
6663 case '*': *d++ = '.';
6664 break;
6665 case '?': *d++ = '.';
6666 continue;
6667 case '$':
6668 case '.':
6669 case '~': *d++ = '\\';
6670 break;
6671 }
6672
6673 /*
6674 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6675 * ":help i_^_CTRL-D" work.
6676 * Insert '-' before and after "CTRL-X" when applicable.
6677 */
6678 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6679 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6680 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006681 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6682 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 STRCPY(d, "CTRL-");
6684 d += 5;
6685 if (*s < ' ')
6686 {
6687#ifdef EBCDIC
6688 *d++ = CtrlChar(*s);
6689#else
6690 *d++ = *s + '@';
6691#endif
6692 if (d[-1] == '\\')
6693 *d++ = '\\'; /* double a backslash */
6694 }
6695 else
6696 *d++ = *++s;
6697 if (s[1] != NUL && s[1] != '_')
6698 *d++ = '_'; /* append a '_' */
6699 continue;
6700 }
6701 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6702 *d++ = '\\';
6703
6704 /*
6705 * Insert a backslash before a backslash after a slash, for search
6706 * pattern tags: "/\|" --> "/\\|".
6707 */
6708 else if (s[0] == '\\' && s[1] != '\\'
6709 && *arg == '/' && s == arg + 1)
6710 *d++ = '\\';
6711
6712 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6713 * "CTRL-\_CTRL-N" */
6714 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6715 {
6716 STRCPY(d, "CTRL-\\\\");
6717 d += 7;
6718 s += 6;
6719 }
6720
6721 *d++ = *s;
6722
6723 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006724 * If tag contains "({" or "([", tag terminates at the "(".
6725 * This is for help on functions, e.g.: abs({expr}).
6726 */
6727 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6728 break;
6729
6730 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 * If tag starts with ', toss everything after a second '. Fixes
6732 * CTRL-] on 'option'. (would include the trailing '.').
6733 */
6734 if (*s == '\'' && s > arg && *arg == '\'')
6735 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006736 /* Also '{' and '}'. */
6737 if (*s == '}' && s > arg && *arg == '{')
6738 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
6740 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006741
6742 if (*IObuff == '`')
6743 {
6744 if (d > IObuff + 2 && d[-1] == '`')
6745 {
6746 /* remove the backticks from `command` */
6747 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6748 d[-2] = NUL;
6749 }
6750 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6751 {
6752 /* remove the backticks and comma from `command`, */
6753 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6754 d[-3] = NUL;
6755 }
6756 else if (d > IObuff + 4 && d[-3] == '`'
6757 && d[-2] == '\\' && d[-1] == '.')
6758 {
6759 /* remove the backticks and dot from `command`\. */
6760 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6761 d[-4] = NUL;
6762 }
6763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 }
6765 }
6766
6767 *matches = (char_u **)"";
6768 *num_matches = 0;
6769 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6770 if (keep_lang)
6771 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006772 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 /* Sort the matches found on the heuristic number that is after the
6776 * tag name. */
6777 qsort((void *)*matches, (size_t)*num_matches,
6778 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006779 /* Delete more than TAG_MANY to reduce the size of the listing. */
6780 while (*num_matches > TAG_MANY)
6781 vim_free((*matches)[--*num_matches]);
6782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 return OK;
6784}
6785
6786/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006787 * Called when starting to edit a buffer for a help file.
6788 */
6789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006790prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006791{
6792 char_u *p;
6793
6794 curbuf->b_help = TRUE;
6795#ifdef FEAT_QUICKFIX
6796 set_string_option_direct((char_u *)"buftype", -1,
6797 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6798#endif
6799
6800 /*
6801 * Always set these options after jumping to a help tag, because the
6802 * user may have an autocommand that gets in the way.
6803 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6804 * latin1 word characters (for translated help files).
6805 * Only set it when needed, buf_init_chartab() is some work.
6806 */
6807 p =
6808#ifdef EBCDIC
6809 (char_u *)"65-255,^*,^|,^\"";
6810#else
6811 (char_u *)"!-~,^*,^|,^\",192-255";
6812#endif
6813 if (STRCMP(curbuf->b_p_isk, p) != 0)
6814 {
6815 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6816 check_buf_options(curbuf);
6817 (void)buf_init_chartab(curbuf, FALSE);
6818 }
6819
Bram Moolenaar0b105412014-11-30 13:34:23 +01006820#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006821 /* Don't use the global foldmethod.*/
6822 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6823 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006824#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006825
6826 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6827 curwin->w_p_list = FALSE; /* no list mode */
6828
6829 curbuf->b_p_ma = FALSE; /* not modifiable */
6830 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6831 curwin->w_p_nu = 0; /* no line numbers */
6832 curwin->w_p_rnu = 0; /* no relative line numbers */
6833 RESET_BINDING(curwin); /* no scroll or cursor binding */
6834#ifdef FEAT_ARABIC
6835 curwin->w_p_arab = FALSE; /* no arabic mode */
6836#endif
6837#ifdef FEAT_RIGHTLEFT
6838 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6839#endif
6840#ifdef FEAT_FOLDING
6841 curwin->w_p_fen = FALSE; /* No folding in the help window */
6842#endif
6843#ifdef FEAT_DIFF
6844 curwin->w_p_diff = FALSE; /* No 'diff' */
6845#endif
6846#ifdef FEAT_SPELL
6847 curwin->w_p_spell = FALSE; /* No spell checking */
6848#endif
6849
6850 set_buflisted(FALSE);
6851}
6852
6853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 * After reading a help file: May cleanup a help buffer when syntax
6855 * highlighting is not used.
6856 */
6857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006858fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 linenr_T lnum;
6861 char_u *line;
6862 int in_example = FALSE;
6863 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006864 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 char_u *p;
6866 char_u *rt;
6867 int mustfree;
6868
Bram Moolenaar90492982017-06-22 14:16:31 +02006869 /* Set filetype to "help" if still needed. */
6870 if (STRCMP(curbuf->b_p_ft, "help") != 0)
Bram Moolenaar18141832017-06-25 21:17:25 +02006871 {
6872 ++curbuf_lock;
Bram Moolenaar90492982017-06-22 14:16:31 +02006873 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
Bram Moolenaar18141832017-06-25 21:17:25 +02006874 --curbuf_lock;
6875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006878 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#endif
6880 {
6881 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6882 {
6883 line = ml_get_buf(curbuf, lnum, FALSE);
6884 len = (int)STRLEN(line);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006885 if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 /* End of example: non-white or '<' in first column. */
6888 if (line[0] == '<')
6889 {
6890 /* blank-out a '<' in the first column */
6891 line = ml_get_buf(curbuf, lnum, TRUE);
6892 line[0] = ' ';
6893 }
6894 in_example = FALSE;
6895 }
6896 if (!in_example && len > 0)
6897 {
6898 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6899 {
6900 /* blank-out a '>' in the last column (start of example) */
6901 line = ml_get_buf(curbuf, lnum, TRUE);
6902 line[len - 1] = ' ';
6903 in_example = TRUE;
6904 }
6905 else if (line[len - 1] == '~')
6906 {
6907 /* blank-out a '~' at the end of line (header marker) */
6908 line = ml_get_buf(curbuf, lnum, TRUE);
6909 line[len - 1] = ' ';
6910 }
6911 }
6912 }
6913 }
6914
6915 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006916 * In the "help.txt" and "help.abx" file, add the locally added help
6917 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006919 fname = gettail(curbuf->b_fname);
6920 if (fnamecmp(fname, "help.txt") == 0
6921#ifdef FEAT_MULTI_LANG
6922 || (fnamencmp(fname, "help.", 5) == 0
6923 && ASCII_ISALPHA(fname[5])
6924 && ASCII_ISALPHA(fname[6])
6925 && TOLOWER_ASC(fname[7]) == 'x'
6926 && fname[8] == NUL)
6927#endif
6928 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
6930 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6931 {
6932 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006933 if (strstr((char *)line, "*local-additions*") == NULL)
6934 continue;
6935
6936 /* Go through all directories in 'runtimepath', skipping
6937 * $VIMRUNTIME. */
6938 p = p_rtp;
6939 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006941 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6942 mustfree = FALSE;
6943 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
Bram Moolenaare4db7ae2018-02-13 13:12:11 +01006944 if (rt != NULL && fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006946 int fcount;
6947 char_u **fnames;
6948 FILE *fd;
6949 char_u *s;
6950 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006952 vimconv_T vc;
6953 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954#endif
6955
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006956 /* Find all "doc/ *.txt" files in this directory. */
6957 add_pathsep(NameBuff);
6958#ifdef FEAT_MULTI_LANG
6959 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006961 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006963 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6964 &fnames, EW_FILE|EW_SILENT) == OK
6965 && fcount > 0)
6966 {
6967#ifdef FEAT_MULTI_LANG
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006968 int i1, i2;
6969 char_u *f1, *f2;
6970 char_u *t1, *t2;
6971 char_u *e1, *e2;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006972
6973 /* If foo.abx is found use it instead of foo.txt in
6974 * the same directory. */
6975 for (i1 = 0; i1 < fcount; ++i1)
6976 {
6977 for (i2 = 0; i2 < fcount; ++i2)
6978 {
6979 if (i1 == i2)
6980 continue;
6981 if (fnames[i1] == NULL || fnames[i2] == NULL)
6982 continue;
6983 f1 = fnames[i1];
6984 f2 = fnames[i2];
6985 t1 = gettail(f1);
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006986 t2 = gettail(f2);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006987 e1 = vim_strrchr(t1, '.');
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006988 e2 = vim_strrchr(t2, '.');
Bram Moolenaar7756e742016-10-21 20:35:37 +02006989 if (e1 == NULL || e2 == NULL)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006990 continue;
6991 if (fnamecmp(e1, ".txt") != 0
6992 && fnamecmp(e1, fname + 4) != 0)
6993 {
6994 /* Not .txt and not .abx, remove it. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01006995 VIM_CLEAR(fnames[i1]);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006996 continue;
6997 }
Bram Moolenaar35c5e812017-12-09 21:10:13 +01006998 if (e1 - f1 != e2 - f2
6999 || fnamencmp(f1, f2, e1 - f1) != 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007000 continue;
7001 if (fnamecmp(e1, ".txt") == 0
7002 && fnamecmp(e2, fname + 4) == 0)
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007003 /* use .abx instead of .txt */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007004 VIM_CLEAR(fnames[i1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007007#endif
7008 for (fi = 0; fi < fcount; ++fi)
7009 {
7010 if (fnames[fi] == NULL)
7011 continue;
7012 fd = mch_fopen((char *)fnames[fi], "r");
7013 if (fd != NULL)
7014 {
7015 vim_fgets(IObuff, IOSIZE, fd);
7016 if (IObuff[0] == '*'
7017 && (s = vim_strchr(IObuff + 1, '*'))
7018 != NULL)
7019 {
7020#ifdef FEAT_MBYTE
7021 int this_utf = MAYBE;
7022#endif
7023 /* Change tag definition to a
7024 * reference and remove <CR>/<NL>. */
7025 IObuff[0] = '|';
7026 *s = '|';
7027 while (*s != NUL)
7028 {
7029 if (*s == '\r' || *s == '\n')
7030 *s = NUL;
7031#ifdef FEAT_MBYTE
7032 /* The text is utf-8 when a byte
7033 * above 127 is found and no
7034 * illegal byte sequence is found.
7035 */
7036 if (*s >= 0x80 && this_utf != FALSE)
7037 {
7038 int l;
7039
7040 this_utf = TRUE;
7041 l = utf_ptr2len(s);
7042 if (l == 1)
7043 this_utf = FALSE;
7044 s += l - 1;
7045 }
7046#endif
7047 ++s;
7048 }
7049#ifdef FEAT_MBYTE
7050 /* The help file is latin1 or utf-8;
7051 * conversion to the current
7052 * 'encoding' may be required. */
7053 vc.vc_type = CONV_NONE;
7054 convert_setup(&vc, (char_u *)(
7055 this_utf == TRUE ? "utf-8"
7056 : "latin1"), p_enc);
7057 if (vc.vc_type == CONV_NONE)
7058 /* No conversion needed. */
7059 cp = IObuff;
7060 else
7061 {
7062 /* Do the conversion. If it fails
7063 * use the unconverted text. */
7064 cp = string_convert(&vc, IObuff,
7065 NULL);
7066 if (cp == NULL)
7067 cp = IObuff;
7068 }
7069 convert_setup(&vc, NULL, NULL);
7070
7071 ml_append(lnum, cp, (colnr_T)0, FALSE);
7072 if (cp != IObuff)
7073 vim_free(cp);
7074#else
7075 ml_append(lnum, IObuff, (colnr_T)0,
7076 FALSE);
7077#endif
7078 ++lnum;
7079 }
7080 fclose(fd);
7081 }
7082 }
7083 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007086 if (mustfree)
7087 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02007089 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 }
7091 }
7092}
7093
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007094/*
7095 * ":exusage"
7096 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007099{
7100 do_cmdline_cmd((char_u *)"help ex-cmd-index");
7101}
7102
7103/*
7104 * ":viusage"
7105 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007107ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007108{
7109 do_cmdline_cmd((char_u *)"help normal-index");
7110}
7111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007113 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007116helptags_one(
7117 char_u *dir, /* doc directory */
7118 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
7119 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
7120 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 FILE *fd_tags;
7123 FILE *fd;
7124 garray_T ga;
7125 int filecount;
7126 char_u **files;
7127 char_u *p1, *p2;
7128 int fi;
7129 char_u *s;
7130 int i;
7131 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007132 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133# ifdef FEAT_MBYTE
7134 int utf8 = MAYBE;
7135 int this_utf8;
7136 int firstline;
7137 int mix = FALSE; /* detected mixed encodings */
7138# endif
7139
7140 /*
7141 * Find all *.txt files.
7142 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007143 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007145 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 STRCAT(NameBuff, ext);
7147 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7148 EW_FILE|EW_SILENT) == FAIL
7149 || filecount == 0)
7150 {
7151 if (!got_int)
Bram Moolenaar5b302912016-08-24 22:11:55 +02007152 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 return;
7154 }
7155
7156 /*
7157 * Open the tags file for writing.
7158 * Do this before scanning through all the files.
7159 */
7160 STRCPY(NameBuff, dir);
7161 add_pathsep(NameBuff);
7162 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 if (fd_tags == NULL)
7165 {
7166 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
7167 FreeWild(filecount, files);
7168 return;
7169 }
7170
7171 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007172 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7173 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
7175 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007176 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
7177 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
7179 if (ga_grow(&ga, 1) == FAIL)
7180 got_int = TRUE;
7181 else
7182 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007183 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (s == NULL)
7185 got_int = TRUE;
7186 else
7187 {
7188 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7189 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7190 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192 }
7193 }
7194
7195 /*
7196 * Go over all the files and extract the tags.
7197 */
7198 for (fi = 0; fi < filecount && !got_int; ++fi)
7199 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007200 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if (fd == NULL)
7202 {
7203 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
7204 continue;
7205 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007206 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
7208# ifdef FEAT_MBYTE
7209 firstline = TRUE;
7210# endif
7211 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7212 {
7213# ifdef FEAT_MBYTE
7214 if (firstline)
7215 {
7216 /* Detect utf-8 file by a non-ASCII char in the first line. */
7217 this_utf8 = MAYBE;
7218 for (s = IObuff; *s != NUL; ++s)
7219 if (*s >= 0x80)
7220 {
7221 int l;
7222
7223 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007224 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (l == 1)
7226 {
7227 /* Illegal UTF-8 byte sequence. */
7228 this_utf8 = FALSE;
7229 break;
7230 }
7231 s += l - 1;
7232 }
7233 if (this_utf8 == MAYBE) /* only ASCII characters found */
7234 this_utf8 = FALSE;
7235 if (utf8 == MAYBE) /* first file */
7236 utf8 = this_utf8;
7237 else if (utf8 != this_utf8)
7238 {
7239 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
7240 mix = !got_int;
7241 got_int = TRUE;
7242 }
7243 firstline = FALSE;
7244 }
7245# endif
7246 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7247 while (p1 != NULL)
7248 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007249 /* Use vim_strbyte() instead of vim_strchr() so that when
7250 * 'encoding' is dbcs it still works, don't find '*' in the
7251 * second byte. */
7252 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7254 {
7255 for (s = p1 + 1; s < p2; ++s)
7256 if (*s == ' ' || *s == '\t' || *s == '|')
7257 break;
7258
7259 /*
7260 * Only accept a *tag* when it consists of valid
7261 * characters, there is white space before it and is
7262 * followed by a white character or end-of-line.
7263 */
7264 if (s == p2
7265 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7266 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7267 || s[1] == '\0'))
7268 {
7269 *p2 = '\0';
7270 ++p1;
7271 if (ga_grow(&ga, 1) == FAIL)
7272 {
7273 got_int = TRUE;
7274 break;
7275 }
7276 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7277 if (s == NULL)
7278 {
7279 got_int = TRUE;
7280 break;
7281 }
7282 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7283 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 sprintf((char *)s, "%s\t%s", p1, fname);
7285
7286 /* find next '*' */
7287 p2 = vim_strchr(p2 + 1, '*');
7288 }
7289 }
7290 p1 = p2;
7291 }
7292 line_breakcheck();
7293 }
7294
7295 fclose(fd);
7296 }
7297
7298 FreeWild(filecount, files);
7299
7300 if (!got_int)
7301 {
7302 /*
7303 * Sort the tags.
7304 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007305 if (ga.ga_data != NULL)
7306 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307
7308 /*
7309 * Check for duplicates.
7310 */
7311 for (i = 1; i < ga.ga_len; ++i)
7312 {
7313 p1 = ((char_u **)ga.ga_data)[i - 1];
7314 p2 = ((char_u **)ga.ga_data)[i];
7315 while (*p1 == *p2)
7316 {
7317 if (*p2 == '\t')
7318 {
7319 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007320 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7322 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
7323 EMSG(NameBuff);
7324 *p2 = '\t';
7325 break;
7326 }
7327 ++p1;
7328 ++p2;
7329 }
7330 }
7331
7332# ifdef FEAT_MBYTE
7333 if (utf8 == TRUE)
7334 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
7335# endif
7336
7337 /*
7338 * Write the tags into the file.
7339 */
7340 for (i = 0; i < ga.ga_len; ++i)
7341 {
7342 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007343 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007345 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
7347 {
7348 fprintf(fd_tags, "%s\t/*", s);
7349 for (p1 = s; *p1 != '\t'; ++p1)
7350 {
7351 /* insert backslash before '\\' and '/' */
7352 if (*p1 == '\\' || *p1 == '/')
7353 putc('\\', fd_tags);
7354 putc(*p1, fd_tags);
7355 }
7356 fprintf(fd_tags, "*\n");
7357 }
7358 }
7359 }
7360#ifdef FEAT_MBYTE
7361 if (mix)
7362 got_int = FALSE; /* continue with other languages */
7363#endif
7364
7365 for (i = 0; i < ga.ga_len; ++i)
7366 vim_free(((char_u **)ga.ga_data)[i]);
7367 ga_clear(&ga);
7368 fclose(fd_tags); /* there is no check for an error... */
7369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007371/*
7372 * Generate tags in one help directory, taking care of translations.
7373 */
7374 static void
7375do_helptags(char_u *dirname, int add_help_tags)
7376{
7377#ifdef FEAT_MULTI_LANG
7378 int len;
7379 int i, j;
7380 garray_T ga;
7381 char_u lang[2];
7382 char_u ext[5];
7383 char_u fname[8];
7384 int filecount;
7385 char_u **files;
7386
7387 /* Get a list of all files in the help directory and in subdirectories. */
7388 STRCPY(NameBuff, dirname);
7389 add_pathsep(NameBuff);
7390 STRCAT(NameBuff, "**");
7391 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7392 EW_FILE|EW_SILENT) == FAIL
7393 || filecount == 0)
7394 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007395 EMSG2(_("E151: No match: %s"), NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007396 return;
7397 }
7398
7399 /* Go over all files in the directory to find out what languages are
7400 * present. */
7401 ga_init2(&ga, 1, 10);
7402 for (i = 0; i < filecount; ++i)
7403 {
7404 len = (int)STRLEN(files[i]);
7405 if (len > 4)
7406 {
7407 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7408 {
7409 /* ".txt" -> language "en" */
7410 lang[0] = 'e';
7411 lang[1] = 'n';
7412 }
7413 else if (files[i][len - 4] == '.'
7414 && ASCII_ISALPHA(files[i][len - 3])
7415 && ASCII_ISALPHA(files[i][len - 2])
7416 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7417 {
7418 /* ".abx" -> language "ab" */
7419 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7420 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7421 }
7422 else
7423 continue;
7424
7425 /* Did we find this language already? */
7426 for (j = 0; j < ga.ga_len; j += 2)
7427 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7428 break;
7429 if (j == ga.ga_len)
7430 {
7431 /* New language, add it. */
7432 if (ga_grow(&ga, 2) == FAIL)
7433 break;
7434 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7435 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7436 }
7437 }
7438 }
7439
7440 /*
7441 * Loop over the found languages to generate a tags file for each one.
7442 */
7443 for (j = 0; j < ga.ga_len; j += 2)
7444 {
7445 STRCPY(fname, "tags-xx");
7446 fname[5] = ((char_u *)ga.ga_data)[j];
7447 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7448 if (fname[5] == 'e' && fname[6] == 'n')
7449 {
7450 /* English is an exception: use ".txt" and "tags". */
7451 fname[4] = NUL;
7452 STRCPY(ext, ".txt");
7453 }
7454 else
7455 {
7456 /* Language "ab" uses ".abx" and "tags-ab". */
7457 STRCPY(ext, ".xxx");
7458 ext[1] = fname[5];
7459 ext[2] = fname[6];
7460 }
7461 helptags_one(dirname, ext, fname, add_help_tags);
7462 }
7463
7464 ga_clear(&ga);
7465 FreeWild(filecount, files);
7466
7467#else
7468 /* No language support, just use "*.txt" and "tags". */
7469 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7470#endif
7471}
7472
7473 static void
7474helptags_cb(char_u *fname, void *cookie)
7475{
7476 do_helptags(fname, *(int *)cookie);
7477}
7478
7479/*
7480 * ":helptags"
7481 */
7482 void
7483ex_helptags(exarg_T *eap)
7484{
7485 expand_T xpc;
7486 char_u *dirname;
7487 int add_help_tags = FALSE;
7488
7489 /* Check for ":helptags ++t {dir}". */
Bram Moolenaar1c465442017-03-12 20:10:05 +01007490 if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3]))
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007491 {
7492 add_help_tags = TRUE;
7493 eap->arg = skipwhite(eap->arg + 3);
7494 }
7495
7496 if (STRCMP(eap->arg, "ALL") == 0)
7497 {
7498 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7499 helptags_cb, &add_help_tags);
7500 }
7501 else
7502 {
7503 ExpandInit(&xpc);
7504 xpc.xp_context = EXPAND_DIRECTORIES;
7505 dirname = ExpandOne(&xpc, eap->arg, NULL,
7506 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7507 if (dirname == NULL || !mch_isdir(dirname))
7508 EMSG2(_("E150: Not a directory: %s"), eap->arg);
7509 else
7510 do_helptags(dirname, add_help_tags);
7511 vim_free(dirname);
7512 }
7513}
7514
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#if defined(FEAT_SIGNS) || defined(PROTO)
7516
7517/*
7518 * Struct to hold the sign properties.
7519 */
7520typedef struct sign sign_T;
7521
7522struct sign
7523{
7524 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007525 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 char_u *sn_name; /* name of sign */
7527 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007528# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 char_u *sn_text; /* text used instead of pixmap */
7532 int sn_line_hl; /* highlight ID for line */
7533 int sn_text_hl; /* highlight ID for text */
7534};
7535
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007537static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007539static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
7540static void sign_list_defined(sign_T *sp);
7541static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007543static char *cmds[] = {
7544 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007545# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007546 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007547# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007548 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007549# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007550 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007551# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007552 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007553# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007554 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007555# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007556 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007557# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007558};
7559
7560/*
7561 * Find index of a ":sign" subcmd from its name.
7562 * "*end_cmd" must be writable.
7563 */
7564 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007565sign_cmd_idx(
7566 char_u *begin_cmd, /* begin of sign subcmd */
7567 char_u *end_cmd) /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007568{
7569 int idx;
7570 char save = *end_cmd;
7571
7572 *end_cmd = NUL;
7573 for (idx = 0; ; ++idx)
7574 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7575 break;
7576 *end_cmd = save;
7577 return idx;
7578}
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580/*
7581 * ":sign" command
7582 */
7583 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007584ex_sign(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 char_u *arg = eap->arg;
7587 char_u *p;
7588 int idx;
7589 sign_T *sp;
7590 sign_T *sp_prev;
Bram Moolenaaraeeb6882017-11-11 16:45:19 +01007591 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
7593 /* Parse the subcommand. */
7594 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007595 idx = sign_cmd_idx(arg, p);
7596 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007598 EMSG2(_("E160: Unknown sign command: %s"), arg);
7599 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601 arg = skipwhite(p);
7602
7603 if (idx <= SIGNCMD_LIST)
7604 {
7605 /*
7606 * Define, undefine or list signs.
7607 */
7608 if (idx == SIGNCMD_LIST && *arg == NUL)
7609 {
7610 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007611 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 sign_list_defined(sp);
7613 }
7614 else if (*arg == NUL)
7615 EMSG(_("E156: Missing sign name"));
7616 else
7617 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007618 /* Isolate the sign name. If it's a number skip leading zeroes,
7619 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 p = skiptowhite(arg);
7621 if (*p != NUL)
7622 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007623 while (arg[0] == '0' && arg[1] != NUL)
7624 ++arg;
7625
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 sp_prev = NULL;
7627 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7628 {
7629 if (STRCMP(sp->sn_name, arg) == 0)
7630 break;
7631 sp_prev = sp;
7632 }
7633 if (idx == SIGNCMD_DEFINE)
7634 {
7635 /* ":sign define {name} ...": define a sign */
7636 if (sp == NULL)
7637 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007638 sign_T *lp;
7639 int start = next_sign_typenr;
7640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 /* Allocate a new sign. */
7642 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7643 if (sp == NULL)
7644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007646 /* Check that next_sign_typenr is not already being used.
7647 * This only happens after wrapping around. Hopefully
7648 * another one got deleted and we can use its number. */
7649 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007651 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007653 ++next_sign_typenr;
7654 if (next_sign_typenr == MAX_TYPENR)
7655 next_sign_typenr = 1;
7656 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007658 vim_free(sp);
7659 EMSG(_("E612: Too many signs defined"));
7660 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007662 lp = first_sign; /* start all over */
7663 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007665 lp = lp->sn_next;
7666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007668 sp->sn_typenr = next_sign_typenr;
7669 if (++next_sign_typenr == MAX_TYPENR)
7670 next_sign_typenr = 1; /* wrap around */
7671
7672 sp->sn_name = vim_strsave(arg);
7673 if (sp->sn_name == NULL) /* out of memory */
7674 {
7675 vim_free(sp);
7676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007678
7679 /* add the new sign to the list of signs */
7680 if (sp_prev == NULL)
7681 first_sign = sp;
7682 else
7683 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685
7686 /* set values for a defined sign. */
7687 for (;;)
7688 {
7689 arg = skipwhite(p);
7690 if (*arg == NUL)
7691 break;
7692 p = skiptowhite_esc(arg);
7693 if (STRNCMP(arg, "icon=", 5) == 0)
7694 {
7695 arg += 5;
7696 vim_free(sp->sn_icon);
7697 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7698 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007699# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (gui.in_use)
7701 {
7702 out_flush();
7703 if (sp->sn_image != NULL)
7704 gui_mch_destroy_sign(sp->sn_image);
7705 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7706 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709 else if (STRNCMP(arg, "text=", 5) == 0)
7710 {
7711 char_u *s;
7712 int cells;
7713 int len;
7714
7715 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007716# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 /* Count cells and check for non-printable chars */
7718 if (has_mbyte)
7719 {
7720 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007721 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
7723 if (!vim_isprintc((*mb_ptr2char)(s)))
7724 break;
7725 cells += (*mb_ptr2cells)(s);
7726 }
7727 }
7728 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
7731 for (s = arg; s < p; ++s)
7732 if (!vim_isprintc(*s))
7733 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007734 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736 /* Currently must be one or two display cells */
7737 if (s != p || cells < 1 || cells > 2)
7738 {
7739 *p = NUL;
7740 EMSG2(_("E239: Invalid sign text: %s"), arg);
7741 return;
7742 }
7743
7744 vim_free(sp->sn_text);
7745 /* Allocate one byte more if we need to pad up
7746 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007747 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 sp->sn_text = vim_strnsave(arg, len);
7749
7750 if (sp->sn_text != NULL && cells == 1)
7751 STRCPY(sp->sn_text + len - 1, " ");
7752 }
7753 else if (STRNCMP(arg, "linehl=", 7) == 0)
7754 {
7755 arg += 7;
7756 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7757 }
7758 else if (STRNCMP(arg, "texthl=", 7) == 0)
7759 {
7760 arg += 7;
7761 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7762 }
7763 else
7764 {
7765 EMSG2(_(e_invarg2), arg);
7766 return;
7767 }
7768 }
7769 }
7770 else if (sp == NULL)
7771 EMSG2(_("E155: Unknown sign: %s"), arg);
7772 else if (idx == SIGNCMD_LIST)
7773 /* ":sign list {name}" */
7774 sign_list_defined(sp);
7775 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007777 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779 }
7780 else
7781 {
7782 int id = -1;
7783 linenr_T lnum = -1;
7784 char_u *sign_name = NULL;
7785 char_u *arg1;
7786
7787 if (*arg == NUL)
7788 {
7789 if (idx == SIGNCMD_PLACE)
7790 {
7791 /* ":sign place": list placed signs in all buffers */
7792 sign_list_placed(NULL);
7793 }
7794 else if (idx == SIGNCMD_UNPLACE)
7795 {
7796 /* ":sign unplace": remove placed sign at cursor */
7797 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7798 if (id > 0)
7799 {
7800 buf_delsign(curwin->w_buffer, id);
7801 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7802 }
7803 else
7804 EMSG(_("E159: Missing sign number"));
7805 }
7806 else
7807 EMSG(_(e_argreq));
7808 return;
7809 }
7810
7811 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7812 {
7813 /* ":sign unplace *": remove all placed signs */
7814 buf_delete_all_signs();
7815 return;
7816 }
7817
7818 /* first arg could be placed sign id */
7819 arg1 = arg;
7820 if (VIM_ISDIGIT(*arg))
7821 {
7822 id = getdigits(&arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007823 if (!VIM_ISWHITE(*arg) && *arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
7825 id = -1;
7826 arg = arg1;
7827 }
7828 else
7829 {
7830 arg = skipwhite(arg);
7831 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7832 {
7833 /* ":sign unplace {id}": remove placed sign by number */
Bram Moolenaar29323592016-07-24 22:04:11 +02007834 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 if ((lnum = buf_delsign(buf, id)) != 0)
7836 update_debug_sign(buf, lnum);
7837 return;
7838 }
7839 }
7840 }
7841
7842 /*
7843 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7844 * Leave "arg" pointing to {fname}.
7845 */
7846 for (;;)
7847 {
7848 if (STRNCMP(arg, "line=", 5) == 0)
7849 {
7850 arg += 5;
7851 lnum = atoi((char *)arg);
7852 arg = skiptowhite(arg);
7853 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007854 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7855 {
7856 if (id != -1)
7857 {
7858 EMSG(_(e_invarg));
7859 return;
7860 }
7861 id = -2;
7862 arg = skiptowhite(arg + 1);
7863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 else if (STRNCMP(arg, "name=", 5) == 0)
7865 {
7866 arg += 5;
7867 sign_name = arg;
7868 arg = skiptowhite(arg);
7869 if (*arg != NUL)
7870 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007871 while (sign_name[0] == '0' && sign_name[1] != NUL)
7872 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874 else if (STRNCMP(arg, "file=", 5) == 0)
7875 {
7876 arg += 5;
7877 buf = buflist_findname(arg);
7878 break;
7879 }
7880 else if (STRNCMP(arg, "buffer=", 7) == 0)
7881 {
7882 arg += 7;
7883 buf = buflist_findnr((int)getdigits(&arg));
7884 if (*skipwhite(arg) != NUL)
7885 EMSG(_(e_trailing));
7886 break;
7887 }
7888 else
7889 {
7890 EMSG(_(e_invarg));
7891 return;
7892 }
7893 arg = skipwhite(arg);
7894 }
7895
7896 if (buf == NULL)
7897 {
7898 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7899 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007900 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
7902 if (lnum >= 0 || sign_name != NULL)
7903 EMSG(_(e_invarg));
7904 else
7905 /* ":sign place file={fname}": list placed signs in one file */
7906 sign_list_placed(buf);
7907 }
7908 else if (idx == SIGNCMD_JUMP)
7909 {
7910 /* ":sign jump {id} file={fname}" */
7911 if (lnum >= 0 || sign_name != NULL)
7912 EMSG(_(e_invarg));
7913 else if ((lnum = buf_findsign(buf, id)) > 0)
7914 { /* goto a sign ... */
7915 if (buf_jump_open_win(buf) != NULL)
7916 { /* ... in a current window */
7917 curwin->w_cursor.lnum = lnum;
7918 check_cursor_lnum();
7919 beginline(BL_WHITE);
7920 }
7921 else
7922 { /* ... not currently in a window */
7923 char_u *cmd;
7924
Bram Moolenaarbfd096d2016-08-17 22:29:09 +02007925 if (buf->b_fname == NULL)
7926 {
7927 EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
7928 return;
7929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7931 if (cmd == NULL)
7932 return;
7933 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7934 do_cmdline_cmd(cmd);
7935 vim_free(cmd);
7936 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007937# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 }
7941 else
7942 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7943 }
7944 else if (idx == SIGNCMD_UNPLACE)
7945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (lnum >= 0 || sign_name != NULL)
7947 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007948 else if (id == -2)
7949 {
7950 /* ":sign unplace * file={fname}" */
7951 redraw_buf_later(buf, NOT_VALID);
7952 buf_delete_signs(buf);
7953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 else
7955 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007956 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 lnum = buf_delsign(buf, id);
7958 update_debug_sign(buf, lnum);
7959 }
7960 }
7961 /* idx == SIGNCMD_PLACE */
7962 else if (sign_name != NULL)
7963 {
7964 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7965 if (STRCMP(sp->sn_name, sign_name) == 0)
7966 break;
7967 if (sp == NULL)
7968 {
7969 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7970 return;
7971 }
7972 if (lnum > 0)
7973 /* ":sign place {id} line={lnum} name={name} file={fname}":
7974 * place a sign */
7975 buf_addsign(buf, id, lnum, sp->sn_typenr);
7976 else
7977 /* ":sign place {id} file={fname}": change sign type */
7978 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007979 if (lnum > 0)
7980 update_debug_sign(buf, lnum);
7981 else
7982 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984 else
7985 EMSG(_(e_invarg));
7986 }
7987}
7988
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007989# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990/*
7991 * Allocate the icons. Called when the GUI has started. Allows defining
7992 * signs before it starts.
7993 */
7994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007995sign_gui_started(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
7997 sign_T *sp;
7998
7999 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8000 if (sp->sn_icon != NULL)
8001 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
8002}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005/*
8006 * List one sign.
8007 */
8008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008009sign_list_defined(sign_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011 char_u *p;
8012
Bram Moolenaar555b2802005-05-19 21:08:39 +00008013 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if (sp->sn_icon != NULL)
8015 {
8016 MSG_PUTS(" icon=");
8017 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008018# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 if (sp->sn_image == NULL)
8020 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008021# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 if (sp->sn_text != NULL)
8026 {
8027 MSG_PUTS(" text=");
8028 msg_outtrans(sp->sn_text);
8029 }
8030 if (sp->sn_line_hl > 0)
8031 {
8032 MSG_PUTS(" linehl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008033 p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (p == NULL)
8035 MSG_PUTS("NONE");
8036 else
8037 msg_puts(p);
8038 }
8039 if (sp->sn_text_hl > 0)
8040 {
8041 MSG_PUTS(" texthl=");
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008042 p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (p == NULL)
8044 MSG_PUTS("NONE");
8045 else
8046 msg_puts(p);
8047 }
8048}
8049
8050/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008051 * Undefine a sign and free its memory.
8052 */
8053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008054sign_undefine(sign_T *sp, sign_T *sp_prev)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008055{
8056 vim_free(sp->sn_name);
8057 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008058# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008059 if (sp->sn_image != NULL)
8060 {
8061 out_flush();
8062 gui_mch_destroy_sign(sp->sn_image);
8063 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008064# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008065 vim_free(sp->sn_text);
8066 if (sp_prev == NULL)
8067 first_sign = sp->sn_next;
8068 else
8069 sp_prev->sn_next = sp->sn_next;
8070 vim_free(sp);
8071}
8072
8073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 * Get highlighting attribute for sign "typenr".
8075 * If "line" is TRUE: line highl, if FALSE: text highl.
8076 */
8077 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008078sign_get_attr(int typenr, int line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080 sign_T *sp;
8081
8082 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8083 if (sp->sn_typenr == typenr)
8084 {
8085 if (line)
8086 {
8087 if (sp->sn_line_hl > 0)
8088 return syn_id2attr(sp->sn_line_hl);
8089 }
8090 else
8091 {
8092 if (sp->sn_text_hl > 0)
8093 return syn_id2attr(sp->sn_text_hl);
8094 }
8095 break;
8096 }
8097 return 0;
8098}
8099
8100/*
8101 * Get text mark for sign "typenr".
8102 * Returns NULL if there isn't one.
8103 */
8104 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008105sign_get_text(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 sign_T *sp;
8108
8109 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8110 if (sp->sn_typenr == typenr)
8111 return sp->sn_text;
8112 return NULL;
8113}
8114
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008115# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008117sign_get_image(
8118 int typenr) /* the attribute which may have a sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
8120 sign_T *sp;
8121
8122 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8123 if (sp->sn_typenr == typenr)
8124 return sp->sn_image;
8125 return NULL;
8126}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128
8129/*
8130 * Get the name of a sign by its typenr.
8131 */
8132 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008133sign_typenr2name(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
8135 sign_T *sp;
8136
8137 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8138 if (sp->sn_typenr == typenr)
8139 return sp->sn_name;
8140 return (char_u *)_("[Deleted]");
8141}
8142
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008143# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008144/*
8145 * Undefine/free all signs.
8146 */
8147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008148free_signs(void)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008149{
8150 while (first_sign != NULL)
8151 sign_undefine(first_sign, NULL);
8152}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008153# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008154
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008155# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008156static enum
8157{
8158 EXP_SUBCMD, /* expand :sign sub-commands */
8159 EXP_DEFINE, /* expand :sign define {name} args */
8160 EXP_PLACE, /* expand :sign place {id} args */
8161 EXP_UNPLACE, /* expand :sign unplace" */
8162 EXP_SIGN_NAMES /* expand with name of placed signs */
8163} expand_what;
8164
8165/*
8166 * Function given to ExpandGeneric() to obtain the sign command
8167 * expansion.
8168 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008169 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008170get_sign_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008171{
8172 sign_T *sp;
8173 int current_idx;
8174
8175 switch (expand_what)
8176 {
8177 case EXP_SUBCMD:
8178 return (char_u *)cmds[idx];
8179 case EXP_DEFINE:
8180 {
8181 char *define_arg[] =
8182 {
8183 "icon=", "linehl=", "text=", "texthl=", NULL
8184 };
8185 return (char_u *)define_arg[idx];
8186 }
8187 case EXP_PLACE:
8188 {
8189 char *place_arg[] =
8190 {
8191 "line=", "name=", "file=", "buffer=", NULL
8192 };
8193 return (char_u *)place_arg[idx];
8194 }
8195 case EXP_UNPLACE:
8196 {
8197 char *unplace_arg[] = { "file=", "buffer=", NULL };
8198 return (char_u *)unplace_arg[idx];
8199 }
8200 case EXP_SIGN_NAMES:
8201 /* Complete with name of signs already defined */
8202 current_idx = 0;
8203 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8204 if (current_idx++ == idx)
8205 return sp->sn_name;
8206 return NULL;
8207 default:
8208 return NULL;
8209 }
8210}
8211
8212/*
8213 * Handle command line completion for :sign command.
8214 */
8215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008216set_context_in_sign_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008217{
8218 char_u *p;
8219 char_u *end_subcmd;
8220 char_u *last;
8221 int cmd_idx;
8222 char_u *begin_subcmd_args;
8223
8224 /* Default: expand subcommands. */
8225 xp->xp_context = EXPAND_SIGN;
8226 expand_what = EXP_SUBCMD;
8227 xp->xp_pattern = arg;
8228
8229 end_subcmd = skiptowhite(arg);
8230 if (*end_subcmd == NUL)
8231 /* expand subcmd name
8232 * :sign {subcmd}<CTRL-D>*/
8233 return;
8234
8235 cmd_idx = sign_cmd_idx(arg, end_subcmd);
8236
8237 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008238 * |
8239 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008240 begin_subcmd_args = skipwhite(end_subcmd);
8241 p = skiptowhite(begin_subcmd_args);
8242 if (*p == NUL)
8243 {
8244 /*
8245 * Expand first argument of subcmd when possible.
8246 * For ":jump {id}" and ":unplace {id}", we could
8247 * possibly expand the ids of all signs already placed.
8248 */
8249 xp->xp_pattern = begin_subcmd_args;
8250 switch (cmd_idx)
8251 {
8252 case SIGNCMD_LIST:
8253 case SIGNCMD_UNDEFINE:
8254 /* :sign list <CTRL-D>
8255 * :sign undefine <CTRL-D> */
8256 expand_what = EXP_SIGN_NAMES;
8257 break;
8258 default:
8259 xp->xp_context = EXPAND_NOTHING;
8260 }
8261 return;
8262 }
8263
8264 /* expand last argument of subcmd */
8265
8266 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008267 * |
8268 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008269
8270 /* Loop until reaching last argument. */
8271 do
8272 {
8273 p = skipwhite(p);
8274 last = p;
8275 p = skiptowhite(p);
8276 } while (*p != NUL);
8277
8278 p = vim_strchr(last, '=');
8279
8280 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008281 * | |
8282 * last p */
Bram Moolenaar7756e742016-10-21 20:35:37 +02008283 if (p == NULL)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008284 {
8285 /* Expand last argument name (before equal sign). */
8286 xp->xp_pattern = last;
8287 switch (cmd_idx)
8288 {
8289 case SIGNCMD_DEFINE:
8290 expand_what = EXP_DEFINE;
8291 break;
8292 case SIGNCMD_PLACE:
8293 expand_what = EXP_PLACE;
8294 break;
8295 case SIGNCMD_JUMP:
8296 case SIGNCMD_UNPLACE:
8297 expand_what = EXP_UNPLACE;
8298 break;
8299 default:
8300 xp->xp_context = EXPAND_NOTHING;
8301 }
8302 }
8303 else
8304 {
8305 /* Expand last argument value (after equal sign). */
8306 xp->xp_pattern = p + 1;
8307 switch (cmd_idx)
8308 {
8309 case SIGNCMD_DEFINE:
8310 if (STRNCMP(last, "texthl", p - last) == 0 ||
8311 STRNCMP(last, "linehl", p - last) == 0)
8312 xp->xp_context = EXPAND_HIGHLIGHT;
8313 else if (STRNCMP(last, "icon", p - last) == 0)
8314 xp->xp_context = EXPAND_FILES;
8315 else
8316 xp->xp_context = EXPAND_NOTHING;
8317 break;
8318 case SIGNCMD_PLACE:
8319 if (STRNCMP(last, "name", p - last) == 0)
8320 expand_what = EXP_SIGN_NAMES;
8321 else
8322 xp->xp_context = EXPAND_NOTHING;
8323 break;
8324 default:
8325 xp->xp_context = EXPAND_NOTHING;
8326 }
8327 }
8328}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008329# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008330#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008331
8332/*
8333 * Make the user happy.
8334 */
8335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008336ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008337{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008338 static char *code[] = {
8339 "\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$",
8340 "\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"
8341 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008342 char *p;
8343 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008344 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008345
8346 msg_start();
8347 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008348 for (i = 0; i < 2; ++i)
8349 for (p = code[i]; *p != NUL; ++p)
8350 if (*p == 'x')
8351 msg_putchar('\n');
8352 else
8353 for (n = *p++; n > 0; --n)
8354 if (*p == 'o' || *p == '$')
Bram Moolenaar8820b482017-03-16 17:23:31 +01008355 msg_putchar_attr(*p, HL_ATTR(HLF_L));
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008356 else
8357 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008358 msg_clr_eos();
8359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361/*
8362 * ":drop"
8363 * Opens the first argument in a window. When there are two or more arguments
8364 * the argument list is redefined.
8365 */
8366 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008367ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368{
8369 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 win_T *wp;
8371 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008372 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 /*
8375 * Check if the first argument is already being edited in a window. If
8376 * so, jump to that window.
8377 * We would actually need to check all arguments, but that's complicated
8378 * and mostly only one file is dropped.
8379 * This also ignores wildcards, since it is very unlikely the user is
8380 * editing a file name with a wildcard character.
8381 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008382 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00008384 /*
8385 * Expanding wildcards may result in an empty argument list. E.g. when
8386 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
8387 * already did an error message for this.
8388 */
8389 if (ARGCOUNT == 0)
8390 return;
8391
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008392 if (cmdmod.tab)
8393 {
8394 /* ":tab drop file ...": open a tab for each argument that isn't
8395 * edited in a window yet. It's like ":tab all" but without closing
8396 * windows or tabs. */
8397 ex_all(eap);
8398 }
8399 else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008400 {
8401 /* ":drop file ...": Edit the first argument. Jump to an existing
8402 * window if possible, edit in current window if the current buffer
8403 * can be abandoned, otherwise open a new window. */
8404 buf = buflist_findnr(ARGLIST[0].ae_fnum);
8405
8406 FOR_ALL_TAB_WINDOWS(tp, wp)
8407 {
8408 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008410 goto_tabpage_win(tp, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 return;
8413 }
8414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008416 /*
8417 * Check whether the current buffer is changed. If so, we will need
8418 * to split the current window or data could be lost.
8419 * Skip the check if the 'hidden' option is set, as in this case the
8420 * buffer won't be lost.
8421 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02008422 if (!buf_hide(curbuf))
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008423 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008424 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008425 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008426 --emsg_off;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008429 /* Fake a ":sfirst" or ":first" command edit the first argument. */
8430 if (split)
8431 {
8432 eap->cmdidx = CMD_sfirst;
8433 eap->cmd[0] = 's';
8434 }
8435 else
8436 eap->cmdidx = CMD_first;
8437 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439}
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008440
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008441/*
8442 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
8443 * Put the start of the pattern in "*s", unless "s" is NULL.
8444 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
8445 * If "s" is not NULL terminate the pattern with a NUL.
8446 * Return a pointer to the char just past the pattern plus flags.
8447 */
8448 char_u *
8449skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
8450{
8451 int c;
8452
8453 if (vim_isIDc(*p))
8454 {
8455 /* ":vimgrep pattern fname" */
8456 if (s != NULL)
8457 *s = p;
8458 p = skiptowhite(p);
8459 if (s != NULL && *p != NUL)
8460 *p++ = NUL;
8461 }
8462 else
8463 {
8464 /* ":vimgrep /pattern/[g][j] fname" */
8465 if (s != NULL)
8466 *s = p + 1;
8467 c = *p;
8468 p = skip_regexp(p + 1, c, TRUE, NULL);
8469 if (*p != c)
8470 return NULL;
8471
8472 /* Truncate the pattern. */
8473 if (s != NULL)
8474 *p = NUL;
8475 ++p;
8476
8477 /* Find the flags */
8478 while (*p == 'g' || *p == 'j')
8479 {
8480 if (flags != NULL)
8481 {
8482 if (*p == 'g')
8483 *flags |= VGR_GLOBAL;
8484 else
8485 *flags |= VGR_NOJUMP;
8486 }
8487 ++p;
8488 }
8489 }
8490 return p;
8491}
Bram Moolenaar9baf2972016-08-21 22:39:35 +02008492
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008493#if defined(FEAT_EVAL) || defined(PROTO)
8494/*
8495 * List v:oldfiles in a nice way.
8496 */
8497 void
8498ex_oldfiles(exarg_T *eap UNUSED)
8499{
8500 list_T *l = get_vim_var_list(VV_OLDFILES);
8501 listitem_T *li;
8502 int nr = 0;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008503 char_u *fname;
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008504
8505 if (l == NULL)
8506 msg((char_u *)_("No old files"));
8507 else
8508 {
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008509 msg_start();
8510 msg_scroll = TRUE;
8511 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8512 {
8513 ++nr;
8514 fname = get_tv_string(&li->li_tv);
Bram Moolenaard6f2ee32016-08-24 00:30:52 +02008515 if (!message_filtered(fname))
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008516 {
8517 msg_outnum((long)nr);
8518 MSG_PUTS(": ");
8519 msg_outtrans(fname);
Bram Moolenaar885c00e2016-08-28 17:08:17 +02008520 msg_clr_eos();
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008521 msg_putchar('\n');
8522 out_flush(); /* output one line at a time */
8523 ui_breakcheck();
8524 }
8525 }
Bram Moolenaare11d61a2016-08-20 18:36:54 +02008526
8527 /* Assume "got_int" was set to truncate the listing. */
8528 got_int = FALSE;
8529
8530# ifdef FEAT_BROWSE_CMD
8531 if (cmdmod.browse)
8532 {
8533 quit_more = FALSE;
8534 nr = prompt_for_number(FALSE);
8535 msg_starthere();
8536 if (nr > 0)
8537 {
8538 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8539 (long)nr);
8540
8541 if (p != NULL)
8542 {
8543 p = expand_env_save(p);
8544 eap->arg = p;
8545 eap->cmdidx = CMD_edit;
8546 cmdmod.browse = FALSE;
8547 do_exedit(eap, NULL);
8548 vim_free(p);
8549 }
8550 }
8551 }
8552# endif
8553 }
8554}
8555#endif