blob: 72603d1bd2aab90a85894b5a31450e0499e804c5 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void delbuf_msg(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static int
35#ifdef __BORLANDC__
36 _RTLENTRYF
37#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038 help_compare(const void *s1, const void *s2);
39static void prepare_help_buffer(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
42 * ":ascii" and "ga".
43 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010045do_ascii(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046{
47 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000048 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 char buf1[20];
50 char buf2[20];
51 char_u buf3[7];
52#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000053 int cc[MAX_MCO];
54 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 int len;
56
57 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000058 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 else
60#endif
61 c = gchar_cursor();
62 if (c == NUL)
63 {
64 MSG("NUL");
65 return;
66 }
67
68#ifdef FEAT_MBYTE
69 IObuff[0] = NUL;
70 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
71#endif
72 {
73 if (c == NL) /* NUL is stored as NL */
74 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000075 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
76 cval = NL; /* NL is stored as CR */
77 else
78 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 if (vim_isprintc_strict(c) && (c < ' '
80#ifndef EBCDIC
81 || c > '~'
82#endif
83 ))
84 {
85 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000086 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 }
88 else
89 buf1[0] = NUL;
90#ifndef EBCDIC
91 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000092 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
93 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 else
95#endif
96 buf2[0] = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +000097 vim_snprintf((char *)IObuff, IOSIZE,
98 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000099 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000101 if (enc_utf8)
102 c = cc[ci++];
103 else
104 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106 }
107
108#ifdef FEAT_MBYTE
109 /* Repeat for combining characters. */
110 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
111 {
112 len = (int)STRLEN(IObuff);
113 /* This assumes every multi-byte char is printable... */
114 if (len > 0)
115 IObuff[len++] = ' ';
116 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000117 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000118# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 )
122 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000123 len += (*mb_char2bytes)(c, IObuff + len);
124 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
126 : _("> %d, Hex %08x, Octal %o"), c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000127 if (ci == MAX_MCO)
128 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000129 if (enc_utf8)
130 c = cc[ci++];
131 else
132 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 }
134#endif
135
136 msg(IObuff);
137}
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * ":left", ":center" and ":right": align text.
141 */
142 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100143ex_align(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 pos_T save_curpos;
146 int len;
147 int indent = 0;
148 int new_indent;
149 int has_tab;
150 int width;
151
152#ifdef FEAT_RIGHTLEFT
153 if (curwin->w_p_rl)
154 {
155 /* switch left and right aligning */
156 if (eap->cmdidx == CMD_right)
157 eap->cmdidx = CMD_left;
158 else if (eap->cmdidx == CMD_left)
159 eap->cmdidx = CMD_right;
160 }
161#endif
162
163 width = atoi((char *)eap->arg);
164 save_curpos = curwin->w_cursor;
165 if (eap->cmdidx == CMD_left) /* width is used for new indent */
166 {
167 if (width >= 0)
168 indent = width;
169 }
170 else
171 {
172 /*
173 * if 'textwidth' set, use it
174 * else if 'wrapmargin' set, use it
175 * if invalid value, use 80
176 */
177 if (width <= 0)
178 width = curbuf->b_p_tw;
179 if (width == 0 && curbuf->b_p_wm > 0)
180 width = W_WIDTH(curwin) - curbuf->b_p_wm;
181 if (width <= 0)
182 width = 80;
183 }
184
185 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
186 return;
187
188 for (curwin->w_cursor.lnum = eap->line1;
189 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
190 {
191 if (eap->cmdidx == CMD_left) /* left align */
192 new_indent = indent;
193 else
194 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000195 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 len = linelen(eap->cmdidx == CMD_right ? &has_tab
197 : NULL) - get_indent();
198
199 if (len <= 0) /* skip blank lines */
200 continue;
201
202 if (eap->cmdidx == CMD_center)
203 new_indent = (width - len) / 2;
204 else
205 {
206 new_indent = width - len; /* right align */
207
208 /*
209 * Make sure that embedded TABs don't make the text go too far
210 * to the right.
211 */
212 if (has_tab)
213 while (new_indent > 0)
214 {
215 (void)set_indent(new_indent, 0);
216 if (linelen(NULL) <= width)
217 {
218 /*
219 * Now try to move the line as much as possible to
220 * the right. Stop when it moves too far.
221 */
222 do
223 (void)set_indent(++new_indent, 0);
224 while (linelen(NULL) <= width);
225 --new_indent;
226 break;
227 }
228 --new_indent;
229 }
230 }
231 }
232 if (new_indent < 0)
233 new_indent = 0;
234 (void)set_indent(new_indent, 0); /* set indent */
235 }
236 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
237 curwin->w_cursor = save_curpos;
238 beginline(BL_WHITE | BL_FIX);
239}
240
241/*
242 * Get the length of the current line, excluding trailing white space.
243 */
244 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100245linelen(int *has_tab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 char_u *line;
248 char_u *first;
249 char_u *last;
250 int save;
251 int len;
252
253 /* find the first non-blank character */
254 line = ml_get_curline();
255 first = skipwhite(line);
256
257 /* find the character after the last non-blank character */
258 for (last = first + STRLEN(first);
259 last > first && vim_iswhite(last[-1]); --last)
260 ;
261 save = *last;
262 *last = NUL;
263 len = linetabsize(line); /* get line length */
264 if (has_tab != NULL) /* check for embedded TAB */
265 *has_tab = (vim_strrchr(first, TAB) != NULL);
266 *last = save;
267
268 return len;
269}
270
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000271/* Buffer for two lines used during sorting. They are allocated to
272 * contain the longest line being sorted. */
273static char_u *sortbuf1;
274static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000275
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100276static int sort_ic; /* ignore case */
277static int sort_nr; /* sort on number */
278static int sort_rx; /* sort on regex instead of skipping it */
279#ifdef FEAT_FLOAT
280static int sort_flt; /* sort on floating number */
281#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000282
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100283static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000284
285/* Struct to store info to be sorted. */
286typedef struct
287{
288 linenr_T lnum; /* line number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100289 union {
290 struct
291 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200292 varnumber_T start_col_nr; /* starting column number */
293 varnumber_T end_col_nr; /* ending column number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100294 } line;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200295 varnumber_T value; /* value if sorting by integer */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100296#ifdef FEAT_FLOAT
297 float_T value_flt; /* value if sorting by float */
298#endif
299 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000300} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000301
302static int
303#ifdef __BORLANDC__
304_RTLENTRYF
305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306sort_compare(const void *s1, const void *s2);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000307
308 static int
309#ifdef __BORLANDC__
310_RTLENTRYF
311#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100312sort_compare(const void *s1, const void *s2)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000313{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000314 sorti_T l1 = *(sorti_T *)s1;
315 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000316 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000317
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000318 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000319 * if we return 0 every time, qsort will assume it's done sorting and
320 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000321 if (sort_abort)
322 return 0;
323 fast_breakcheck();
324 if (got_int)
325 sort_abort = TRUE;
326
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000327 /* When sorting numbers "start_col_nr" is the number, not the column
328 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000329 if (sort_nr)
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100330 result = l1.st_u.value == l2.st_u.value ? 0
331 : l1.st_u.value > l2.st_u.value ? 1 : -1;
332#ifdef FEAT_FLOAT
333 else if (sort_flt)
334 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
335 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
336#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337 else
338 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000339 /* We need to copy one line into "sortbuf1", because there is no
340 * guarantee that the first pointer becomes invalid when obtaining the
341 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100342 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
343 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
344 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
345 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
346 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
347 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000348
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000349 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
350 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000351 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000352
353 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000354 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000355 return (int)(l1.lnum - l2.lnum);
356 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000357}
358
359/*
360 * ":sort".
361 */
362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100363ex_sort(exarg_T *eap)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000364{
365 regmatch_T regmatch;
366 int len;
367 linenr_T lnum;
368 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000369 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000370 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000371 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000372 char_u *p;
373 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000374 char_u *s2;
375 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000376 int unique = FALSE;
377 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000378 colnr_T start_col;
379 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100380 int sort_what = 0;
381 int format_found = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000382
Bram Moolenaar48c99162008-02-18 18:42:52 +0000383 /* Sorting one line is really quick! */
384 if (count <= 1)
385 return;
386
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000387 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
388 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000389 sortbuf1 = NULL;
390 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000391 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000392 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000393 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000394 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000395
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100396 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100397#ifdef FEAT_FLOAT
398 sort_flt = 0;
399#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000400
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000401 for (p = eap->arg; *p != NUL; ++p)
402 {
403 if (vim_iswhite(*p))
404 ;
405 else if (*p == 'i')
406 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000407 else if (*p == 'r')
408 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000409 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100410 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100411 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100412 ++format_found;
413 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100414#ifdef FEAT_FLOAT
415 else if (*p == 'f')
416 {
417 sort_flt = 1;
418 ++format_found;
419 }
420#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100421 else if (*p == 'b')
422 {
423 sort_what = STR2NR_BIN + STR2NR_FORCE;
424 ++format_found;
425 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000426 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100427 {
428 sort_what = STR2NR_OCT + STR2NR_FORCE;
429 ++format_found;
430 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000431 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100432 {
433 sort_what = STR2NR_HEX + STR2NR_FORCE;
434 ++format_found;
435 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000436 else if (*p == 'u')
437 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000438 else if (*p == '"') /* comment start */
439 break;
440 else if (check_nextcmd(p) != NULL)
441 {
442 eap->nextcmd = check_nextcmd(p);
443 break;
444 }
445 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000446 {
447 s = skip_regexp(p + 1, *p, TRUE, NULL);
448 if (*s != *p)
449 {
450 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000451 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000452 }
453 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000454 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100455 if (s == p + 1)
456 {
457 if (last_search_pat() == NULL)
458 {
459 EMSG(_(e_noprevre));
460 goto sortend;
461 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000462 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100463 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000464 else
465 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000466 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000467 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000468 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000469 regmatch.rm_ic = p_ic;
470 }
471 else
472 {
473 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000474 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000475 }
476 }
477
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100478 /* Can only have one of 'n', 'b', 'o' and 'x'. */
479 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000480 {
481 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000482 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000483 }
484
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100485 /* From here on "sort_nr" is used as a flag for any integer number
486 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100487 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000488
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000489 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000490 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000491 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000492 * When sorting on strings "start_col_nr" is the offset in the line, for
493 * numbers sorting it's the number to sort on. This means the pattern
494 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000495 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000496 */
497 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
498 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000499 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000500 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000501 if (maxlen < len)
502 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000503
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000504 start_col = 0;
505 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000506 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000507 {
508 if (sort_rx)
509 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000510 start_col = (colnr_T)(regmatch.startp[0] - s);
511 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000512 }
513 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000514 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000515 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000516 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000517 if (regmatch.regprog != NULL)
518 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000519
Bram Moolenaar937204a2016-01-30 23:37:38 +0100520 if (sort_nr
521#ifdef FEAT_FLOAT
522 || sort_flt
523#endif
524 )
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000525 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000526 /* Make sure vim_str2nr doesn't read any digits past the end
527 * of the match, by temporarily terminating the string there */
528 s2 = s + end_col;
529 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200530 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000531 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000532 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100533 if (sort_nr)
534 {
535 if (sort_what & STR2NR_HEX)
536 s = skiptohex(p);
537 else if (sort_what & STR2NR_BIN)
538 s = skiptobin(p);
539 else
540 s = skiptodigit(p);
541 if (s > p && s[-1] == '-')
542 --s; /* include preceding negative sign */
543 if (*s == NUL)
544 /* empty line should sort before any number */
545 nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
546 else
547 vim_str2nr(s, NULL, NULL, sort_what,
548 &nrs[lnum - eap->line1].st_u.value, NULL, 0);
549 }
550#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000551 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100552 {
553 s = skipwhite(p);
554 if (*s == '+')
555 s = skipwhite(s + 1);
556
557 if (*s == NUL)
558 /* empty line should sort before any number */
559 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
560 else
561 nrs[lnum - eap->line1].st_u.value_flt =
562 strtod((char *)s, NULL);
563 }
564#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200565 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000566 }
567 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000568 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000569 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100570 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
571 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000572 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000573
574 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000575
576 if (regmatch.regprog != NULL)
577 fast_breakcheck();
578 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000579 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000580 }
581
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000582 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000583 sortbuf1 = alloc((unsigned)maxlen + 1);
584 if (sortbuf1 == NULL)
585 goto sortend;
586 sortbuf2 = alloc((unsigned)maxlen + 1);
587 if (sortbuf2 == NULL)
588 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000589
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000590 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000591 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000592
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000593 if (sort_abort)
594 goto sortend;
595
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000596 /* Insert the lines in the sorted order below the last one. */
597 lnum = eap->line2;
598 for (i = 0; i < count; ++i)
599 {
600 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
601 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000602 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000603 {
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100604 /* Copy the line into a buffer, it may become invalid in
605 * ml_append(). And it's needed for "unique". */
606 STRCPY(sortbuf1, s);
607 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000608 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000609 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000610 fast_breakcheck();
611 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000612 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000613 }
614
615 /* delete the original lines if appending worked */
616 if (i == count)
617 for (i = 0; i < count; ++i)
618 ml_delete(eap->line1, FALSE);
619 else
620 count = 0;
621
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000622 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000623 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000624 if (deleted > 0)
625 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
626 else if (deleted < 0)
627 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
628 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000629
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000630 curwin->w_cursor.lnum = eap->line1;
631 beginline(BL_WHITE | BL_FIX);
632
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000633sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000634 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000635 vim_free(sortbuf1);
636 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200637 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000638 if (got_int)
639 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000640}
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642/*
643 * ":retab".
644 */
645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100646ex_retab(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 linenr_T lnum;
649 int got_tab = FALSE;
650 long num_spaces = 0;
651 long num_tabs;
652 long len;
653 long col;
654 long vcol;
655 long start_col = 0; /* For start of white-space string */
656 long start_vcol = 0; /* For start of white-space string */
657 int temp;
658 long old_len;
659 char_u *ptr;
660 char_u *new_line = (char_u *)1; /* init to non-NULL */
661 int did_undo; /* called u_save for current line */
662 int new_ts;
663 int save_list;
664 linenr_T first_line = 0; /* first changed line */
665 linenr_T last_line = 0; /* last changed line */
666
667 save_list = curwin->w_p_list;
668 curwin->w_p_list = 0; /* don't want list mode here */
669
670 new_ts = getdigits(&(eap->arg));
671 if (new_ts < 0)
672 {
673 EMSG(_(e_positive));
674 return;
675 }
676 if (new_ts == 0)
677 new_ts = curbuf->b_p_ts;
678 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
679 {
680 ptr = ml_get(lnum);
681 col = 0;
682 vcol = 0;
683 did_undo = FALSE;
684 for (;;)
685 {
686 if (vim_iswhite(ptr[col]))
687 {
688 if (!got_tab && num_spaces == 0)
689 {
690 /* First consecutive white-space */
691 start_vcol = vcol;
692 start_col = col;
693 }
694 if (ptr[col] == ' ')
695 num_spaces++;
696 else
697 got_tab = TRUE;
698 }
699 else
700 {
701 if (got_tab || (eap->forceit && num_spaces > 1))
702 {
703 /* Retabulate this string of white-space */
704
705 /* len is virtual length of white string */
706 len = num_spaces = vcol - start_vcol;
707 num_tabs = 0;
708 if (!curbuf->b_p_et)
709 {
710 temp = new_ts - (start_vcol % new_ts);
711 if (num_spaces >= temp)
712 {
713 num_spaces -= temp;
714 num_tabs++;
715 }
716 num_tabs += num_spaces / new_ts;
717 num_spaces -= (num_spaces / new_ts) * new_ts;
718 }
719 if (curbuf->b_p_et || got_tab ||
720 (num_spaces + num_tabs < len))
721 {
722 if (did_undo == FALSE)
723 {
724 did_undo = TRUE;
725 if (u_save((linenr_T)(lnum - 1),
726 (linenr_T)(lnum + 1)) == FAIL)
727 {
728 new_line = NULL; /* flag out-of-memory */
729 break;
730 }
731 }
732
733 /* len is actual number of white characters used */
734 len = num_spaces + num_tabs;
735 old_len = (long)STRLEN(ptr);
736 new_line = lalloc(old_len - col + start_col + len + 1,
737 TRUE);
738 if (new_line == NULL)
739 break;
740 if (start_col > 0)
741 mch_memmove(new_line, ptr, (size_t)start_col);
742 mch_memmove(new_line + start_col + len,
743 ptr + col, (size_t)(old_len - col + 1));
744 ptr = new_line + start_col;
745 for (col = 0; col < len; col++)
746 ptr[col] = (col < num_tabs) ? '\t' : ' ';
747 ml_replace(lnum, new_line, FALSE);
748 if (first_line == 0)
749 first_line = lnum;
750 last_line = lnum;
751 ptr = new_line;
752 col = start_col + len;
753 }
754 }
755 got_tab = FALSE;
756 num_spaces = 0;
757 }
758 if (ptr[col] == NUL)
759 break;
760 vcol += chartabsize(ptr + col, (colnr_T)vcol);
761#ifdef FEAT_MBYTE
762 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000763 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 else
765#endif
766 ++col;
767 }
768 if (new_line == NULL) /* out of memory */
769 break;
770 line_breakcheck();
771 }
772 if (got_int)
773 EMSG(_(e_interr));
774
775 if (curbuf->b_p_ts != new_ts)
776 redraw_curbuf_later(NOT_VALID);
777 if (first_line != 0)
778 changed_lines(first_line, 0, last_line + 1, 0L);
779
780 curwin->w_p_list = save_list; /* restore 'list' */
781
782 curbuf->b_p_ts = new_ts;
783 coladvance(curwin->w_curswant);
784
785 u_clearline();
786}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788/*
789 * :move command - move lines line1-line2 to line dest
790 *
791 * return FAIL for failure, OK otherwise
792 */
793 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100794do_move(linenr_T line1, linenr_T line2, linenr_T dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 char_u *str;
797 linenr_T l;
798 linenr_T extra; /* Num lines added before line1 */
799 linenr_T num_lines; /* Num lines moved */
800 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200801#ifdef FEAT_FOLDING
802 int isFolded;
803
804 /* Moving lines seems to corrupt the folds, delete folding info now
805 * and recreate it when finished. Don't do this for manual folding, it
806 * would delete all folds. */
807 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin);
808 if (isFolded)
809 deleteFoldRecurse(&curwin->w_folds);
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 if (dest >= line1 && dest < line2)
813 {
814 EMSG(_("E134: Move lines into themselves"));
815 return FAIL;
816 }
817
818 num_lines = line2 - line1 + 1;
819
820 /*
821 * First we copy the old text to its new location -- webb
822 * Also copy the flag that ":global" command uses.
823 */
824 if (u_save(dest, dest + 1) == FAIL)
825 return FAIL;
826 for (extra = 0, l = line1; l <= line2; l++)
827 {
828 str = vim_strsave(ml_get(l + extra));
829 if (str != NULL)
830 {
831 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
832 vim_free(str);
833 if (dest < line1)
834 extra++;
835 }
836 }
837
838 /*
839 * Now we must be careful adjusting our marks so that we don't overlap our
840 * mark_adjust() calls.
841 *
842 * We adjust the marks within the old text so that they refer to the
843 * last lines of the file (temporarily), because we know no other marks
844 * will be set there since these line numbers did not exist until we added
845 * our new lines.
846 *
847 * Then we adjust the marks on lines between the old and new text positions
848 * (either forwards or backwards).
849 *
850 * And Finally we adjust the marks we put at the end of the file back to
851 * their final destination at the new text position -- webb
852 */
853 last_line = curbuf->b_ml.ml_line_count;
854 mark_adjust(line1, line2, last_line - line2, 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200855 changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (dest >= line2)
857 {
858 mark_adjust(line2 + 1, dest, -num_lines, 0L);
859 curbuf->b_op_start.lnum = dest - num_lines + 1;
860 curbuf->b_op_end.lnum = dest;
861 }
862 else
863 {
864 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
865 curbuf->b_op_start.lnum = dest + 1;
866 curbuf->b_op_end.lnum = dest + num_lines;
867 }
868 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
869 mark_adjust(last_line - num_lines + 1, last_line,
870 -(last_line - dest - extra), 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200871 changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /*
874 * Now we delete the original text -- webb
875 */
876 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
877 return FAIL;
878
879 for (l = line1; l <= line2; l++)
880 ml_delete(line1 + extra, TRUE);
881
882 if (!global_busy && num_lines > p_report)
883 {
884 if (num_lines == 1)
885 MSG(_("1 line moved"));
886 else
887 smsg((char_u *)_("%ld lines moved"), num_lines);
888 }
889
890 /*
891 * Leave the cursor on the last of the moved lines.
892 */
893 if (dest >= line1)
894 curwin->w_cursor.lnum = dest;
895 else
896 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
897
898 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +0100899 {
900 dest += num_lines + 1;
901 last_line = curbuf->b_ml.ml_line_count;
902 if (dest > last_line + 1)
903 dest = last_line + 1;
904 changed_lines(line1, 0, dest, 0L);
905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else
907 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
908
Bram Moolenaard5f69332015-04-15 12:43:50 +0200909#ifdef FEAT_FOLDING
910 /* recreate folds */
911 if (isFolded)
912 foldUpdateAll(curwin);
913#endif
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return OK;
916}
917
918/*
919 * ":copy"
920 */
921 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100922ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 linenr_T count;
925 char_u *p;
926
927 count = line2 - line1 + 1;
928 curbuf->b_op_start.lnum = n + 1;
929 curbuf->b_op_end.lnum = n + count;
930 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
931
932 /*
933 * there are three situations:
934 * 1. destination is above line1
935 * 2. destination is between line1 and line2
936 * 3. destination is below line2
937 *
938 * n = destination (when starting)
939 * curwin->w_cursor.lnum = destination (while copying)
940 * line1 = start of source (while copying)
941 * line2 = end of source (while copying)
942 */
943 if (u_save(n, n + 1) == FAIL)
944 return;
945
946 curwin->w_cursor.lnum = n;
947 while (line1 <= line2)
948 {
949 /* need to use vim_strsave() because the line will be unlocked within
950 * ml_append() */
951 p = vim_strsave(ml_get(line1));
952 if (p != NULL)
953 {
954 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
955 vim_free(p);
956 }
957 /* situation 2: skip already copied lines */
958 if (line1 == n)
959 line1 = curwin->w_cursor.lnum;
960 ++line1;
961 if (curwin->w_cursor.lnum < line1)
962 ++line1;
963 if (curwin->w_cursor.lnum < line2)
964 ++line2;
965 ++curwin->w_cursor.lnum;
966 }
967
968 appended_lines_mark(n, count);
969
970 msgmore((long)count);
971}
972
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000973static char_u *prevcmd = NULL; /* the previous command */
974
975#if defined(EXITFREE) || defined(PROTO)
976 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100977free_prev_shellcmd(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000978{
979 vim_free(prevcmd);
980}
981#endif
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
985 * Bangs in the argument are replaced with the previously entered command.
986 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 */
988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100989do_bang(
990 int addr_count,
991 exarg_T *eap,
992 int forceit,
993 int do_in,
994 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 char_u *arg = eap->arg; /* command */
997 linenr_T line1 = eap->line1; /* start of range */
998 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 char_u *newcmd = NULL; /* the new command */
1000 int free_newcmd = FALSE; /* need to free() newcmd */
1001 int ins_prevcmd;
1002 char_u *t;
1003 char_u *p;
1004 char_u *trailarg;
1005 int len;
1006 int scroll_save = msg_scroll;
1007
1008 /*
1009 * Disallow shell commands for "rvim".
1010 * Disallow shell commands from .exrc and .vimrc in current directory for
1011 * security reasons.
1012 */
1013 if (check_restricted() || check_secure())
1014 return;
1015
1016 if (addr_count == 0) /* :! */
1017 {
1018 msg_scroll = FALSE; /* don't scroll here */
1019 autowrite_all();
1020 msg_scroll = scroll_save;
1021 }
1022
1023 /*
1024 * Try to find an embedded bang, like in :!<cmd> ! [args]
1025 * (:!! is indicated by the 'forceit' variable)
1026 */
1027 ins_prevcmd = forceit;
1028 trailarg = arg;
1029 do
1030 {
1031 len = (int)STRLEN(trailarg) + 1;
1032 if (newcmd != NULL)
1033 len += (int)STRLEN(newcmd);
1034 if (ins_prevcmd)
1035 {
1036 if (prevcmd == NULL)
1037 {
1038 EMSG(_(e_noprev));
1039 vim_free(newcmd);
1040 return;
1041 }
1042 len += (int)STRLEN(prevcmd);
1043 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001044 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 vim_free(newcmd);
1047 return;
1048 }
1049 *t = NUL;
1050 if (newcmd != NULL)
1051 STRCAT(t, newcmd);
1052 if (ins_prevcmd)
1053 STRCAT(t, prevcmd);
1054 p = t + STRLEN(t);
1055 STRCAT(t, trailarg);
1056 vim_free(newcmd);
1057 newcmd = t;
1058
1059 /*
1060 * Scan the rest of the argument for '!', which is replaced by the
1061 * previous command. "\!" is replaced by "!" (this is vi compatible).
1062 */
1063 trailarg = NULL;
1064 while (*p)
1065 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001066 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001069 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 else
1071 {
1072 trailarg = p;
1073 *trailarg++ = NUL;
1074 ins_prevcmd = TRUE;
1075 break;
1076 }
1077 }
1078 ++p;
1079 }
1080 } while (trailarg != NULL);
1081
1082 vim_free(prevcmd);
1083 prevcmd = newcmd;
1084
1085 if (bangredo) /* put cmd in redo buffer for ! command */
1086 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001087 /* If % or # appears in the command, it must have been escaped.
1088 * Reescape them, so that redoing them does not substitute them by the
1089 * buffername. */
1090 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1091
1092 if (cmd != NULL)
1093 {
1094 AppendToRedobuffLit(cmd, -1);
1095 vim_free(cmd);
1096 }
1097 else
1098 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 AppendToRedobuff((char_u *)"\n");
1100 bangredo = FALSE;
1101 }
1102 /*
1103 * Add quotes around the command, for shells that need them.
1104 */
1105 if (*p_shq != NUL)
1106 {
1107 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1108 if (newcmd == NULL)
1109 return;
1110 STRCPY(newcmd, p_shq);
1111 STRCAT(newcmd, prevcmd);
1112 STRCAT(newcmd, p_shq);
1113 free_newcmd = TRUE;
1114 }
1115 if (addr_count == 0) /* :! */
1116 {
1117 /* echo the command */
1118 msg_start();
1119 msg_putchar(':');
1120 msg_putchar('!');
1121 msg_outtrans(newcmd);
1122 msg_clr_eos();
1123 windgoto(msg_row, msg_col);
1124
1125 do_shell(newcmd, 0);
1126 }
1127 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 /* Careful: This may recursively call do_bang() again! (because of
1130 * autocommands) */
1131 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001132#ifdef FEAT_AUTOCMD
1133 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1134#endif
1135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (free_newcmd)
1137 vim_free(newcmd);
1138}
1139
1140/*
1141 * do_filter: filter lines through a command given by the user
1142 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 * We mostly use temp files and the call_shell() routine here. This would
1144 * normally be done using pipes on a UNIX machine, but this is more portable
1145 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * to deal with redirection somehow, and should handle things like looking
1147 * at the PATH env. variable, and adding reasonable extensions to the
1148 * command name given by the user. All reasonable versions of call_shell()
1149 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 * Alternatively, if on Unix and redirecting input or output, but not both,
1151 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * We use input redirection if do_in is TRUE.
1153 * We use output redirection if do_out is TRUE.
1154 */
1155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001156do_filter(
1157 linenr_T line1,
1158 linenr_T line2,
1159 exarg_T *eap, /* for forced 'ff' and 'fenc' */
1160 char_u *cmd,
1161 int do_in,
1162 int do_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
1164 char_u *itmp = NULL;
1165 char_u *otmp = NULL;
1166 linenr_T linecount;
1167 linenr_T read_linecount;
1168 pos_T cursor_save;
1169 char_u *cmd_buf;
1170#ifdef FEAT_AUTOCMD
1171 buf_T *old_curbuf = curbuf;
1172#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
1175 if (*cmd == NUL) /* no filter command */
1176 return;
1177
1178#ifdef WIN3264
1179 /*
1180 * Check if external commands are allowed now.
1181 */
1182 if (can_end_termcap_mode(TRUE) == FALSE)
1183 return;
1184#endif
1185
1186 cursor_save = curwin->w_cursor;
1187 linecount = line2 - line1 + 1;
1188 curwin->w_cursor.lnum = line1;
1189 curwin->w_cursor.col = 0;
1190 changed_line_abv_curs();
1191 invalidate_botline();
1192
1193 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 * When using temp files:
1195 * 1. * Form temp file names
1196 * 2. * Write the lines to a temp file
1197 * 3. Run the filter command on the temp file
1198 * 4. * Read the output of the command into the buffer
1199 * 5. * Delete the original lines to be filtered
1200 * 6. * Remove the temp files
1201 *
1202 * When writing the input with a pipe or when catching the output with a
1203 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 */
1205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 if (do_out)
1207 shell_flags |= SHELL_DOOUT;
1208
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001209#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1213 shell_flags |= SHELL_READ;
1214 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 else if (do_in && !do_out && !p_stmp)
1217 {
1218 /* Use a pipe to write stdin of the command, do not use a temp file. */
1219 shell_flags |= SHELL_WRITE;
1220 curbuf->b_op_start.lnum = line1;
1221 curbuf->b_op_end.lnum = line2;
1222 }
1223 else if (do_in && do_out && !p_stmp)
1224 {
1225 /* Use a pipe to write stdin and fetch stdout of the command, do not
1226 * use a temp file. */
1227 shell_flags |= SHELL_READ|SHELL_WRITE;
1228 curbuf->b_op_start.lnum = line1;
1229 curbuf->b_op_end.lnum = line2;
1230 curwin->w_cursor.lnum = line2;
1231 }
1232 else
1233#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001234 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1235 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 {
1237 EMSG(_(e_notmp));
1238 goto filterend;
1239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241/*
1242 * The writing and reading of temp files will not be shown.
1243 * Vi also doesn't do this and the messages are not very informative.
1244 */
1245 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 FALSE, FALSE, FALSE, TRUE) == FAIL)
1248 {
1249 msg_putchar('\n'); /* keep message from buf_write() */
1250 --no_wait_return;
1251#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1252 if (!aborting())
1253#endif
1254 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1255 goto filterend;
1256 }
1257#ifdef FEAT_AUTOCMD
1258 if (curbuf != old_curbuf)
1259 goto filterend;
1260#endif
1261
1262 if (!do_out)
1263 msg_putchar('\n');
1264
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001265 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1267 if (cmd_buf == NULL)
1268 goto filterend;
1269
1270 windgoto((int)Rows - 1, 0);
1271 cursor_on();
1272
1273 /*
1274 * When not redirecting the output the command can write anything to the
1275 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1276 * stderr output of external command. Clear the screen later.
1277 * If do_in is FALSE, this could be something like ":r !cat", which may
1278 * also mess up the screen, clear it later.
1279 */
1280 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1281 redraw_later_clear();
1282
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001283 if (do_out)
1284 {
1285 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001286 {
1287 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001288 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001289 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001290 redraw_curbuf_later(VALID);
1291 }
1292 read_linecount = curbuf->b_ml.ml_line_count;
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 /*
1295 * When call_shell() fails wait_return() is called to give the user a
1296 * chance to read the error messages. Otherwise errors are ignored, so you
1297 * can see the error messages from the command that appear on stdout; use
1298 * 'u' to fix the text
1299 * Switch to cooked mode when not redirecting stdin, avoids that something
1300 * like ":r !cat" hangs.
1301 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1302 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001303 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 redraw_later_clear();
1306 wait_return(FALSE);
1307 }
1308 vim_free(cmd_buf);
1309
1310 did_check_timestamps = FALSE;
1311 need_check_timestamps = TRUE;
1312
1313 /* When interrupting the shell command, it may still have produced some
1314 * useful output. Reset got_int here, so that readfile() won't cancel
1315 * reading. */
1316 ui_breakcheck();
1317 got_int = FALSE;
1318
1319 if (do_out)
1320 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001321 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001323 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1324 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001326#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1327 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001329 {
1330 msg_putchar('\n');
1331 EMSG2(_(e_notread), otmp);
1332 }
1333 goto error;
1334 }
1335#ifdef FEAT_AUTOCMD
1336 if (curbuf != old_curbuf)
1337 goto filterend;
1338#endif
1339 }
1340
1341 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1342
1343 if (shell_flags & SHELL_READ)
1344 {
1345 curbuf->b_op_start.lnum = line2 + 1;
1346 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1347 appended_lines_mark(line2, read_linecount);
1348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
1350 if (do_in)
1351 {
1352 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (read_linecount >= linecount)
1355 /* move all marks from old lines to new lines */
1356 mark_adjust(line1, line2, linecount, 0L);
1357 else
1358 {
1359 /* move marks from old lines to new lines, delete marks
1360 * that are in deleted lines */
1361 mark_adjust(line1, line1 + read_linecount - 1,
1362 linecount, 0L);
1363 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1364 }
1365 }
1366
1367 /*
1368 * Put cursor on first filtered line for ":range!cmd".
1369 * Adjust '[ and '] (set by buf_write()).
1370 */
1371 curwin->w_cursor.lnum = line1;
1372 del_lines(linecount, TRUE);
1373 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1374 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1375 write_lnum_adjust(-linecount); /* adjust last line
1376 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001377#ifdef FEAT_FOLDING
1378 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 else
1382 {
1383 /*
1384 * Put cursor on last new line for ":r !cmd".
1385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001387 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1391 --no_wait_return;
1392
1393 if (linecount > p_report)
1394 {
1395 if (do_in)
1396 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001397 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1398 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001401 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 else
1404 msgmore((long)linecount);
1405 }
1406 }
1407 else
1408 {
1409error:
1410 /* put cursor back in same position for ":w !cmd" */
1411 curwin->w_cursor = cursor_save;
1412 --no_wait_return;
1413 wait_return(FALSE);
1414 }
1415
1416filterend:
1417
1418#ifdef FEAT_AUTOCMD
1419 if (curbuf != old_curbuf)
1420 {
1421 --no_wait_return;
1422 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1423 }
1424#endif
1425 if (itmp != NULL)
1426 mch_remove(itmp);
1427 if (otmp != NULL)
1428 mch_remove(otmp);
1429 vim_free(itmp);
1430 vim_free(otmp);
1431}
1432
1433/*
1434 * Call a shell to execute a command.
1435 * When "cmd" is NULL start an interactive shell.
1436 */
1437 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001438do_shell(
1439 char_u *cmd,
1440 int flags) /* may be SHELL_DOOUT when output is redirected */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 buf_T *buf;
1443#ifndef FEAT_GUI_MSWIN
1444 int save_nwr;
1445#endif
1446#ifdef MSWIN
1447 int winstart = FALSE;
1448#endif
1449
1450 /*
1451 * Disallow shell commands for "rvim".
1452 * Disallow shell commands from .exrc and .vimrc in current directory for
1453 * security reasons.
1454 */
1455 if (check_restricted() || check_secure())
1456 {
1457 msg_end();
1458 return;
1459 }
1460
1461#ifdef MSWIN
1462 /*
1463 * Check if external commands are allowed now.
1464 */
1465 if (can_end_termcap_mode(TRUE) == FALSE)
1466 return;
1467
1468 /*
1469 * Check if ":!start" is used.
1470 */
1471 if (cmd != NULL)
1472 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1473#endif
1474
1475 /*
1476 * For autocommands we want to get the output on the current screen, to
1477 * avoid having to type return below.
1478 */
1479 msg_putchar('\r'); /* put cursor at start of line */
1480#ifdef FEAT_AUTOCMD
1481 if (!autocmd_busy)
1482#endif
1483 {
1484#ifdef MSWIN
1485 if (!winstart)
1486#endif
1487 stoptermcap();
1488 }
1489#ifdef MSWIN
1490 if (!winstart)
1491#endif
1492 msg_putchar('\n'); /* may shift screen one line up */
1493
1494 /* warning message before calling the shell */
1495 if (p_warn
1496#ifdef FEAT_AUTOCMD
1497 && !autocmd_busy
1498#endif
1499 && msg_silent == 0)
1500 for (buf = firstbuf; buf; buf = buf->b_next)
1501 if (bufIsChanged(buf))
1502 {
1503#ifdef FEAT_GUI_MSWIN
1504 if (!winstart)
1505 starttermcap(); /* don't want a message box here */
1506#endif
1507 MSG_PUTS(_("[No write since last change]\n"));
1508#ifdef FEAT_GUI_MSWIN
1509 if (!winstart)
1510 stoptermcap();
1511#endif
1512 break;
1513 }
1514
1515 /* This windgoto is required for when the '\n' resulted in a "delete line
1516 * 1" command to the terminal. */
1517 if (!swapping_screen())
1518 windgoto(msg_row, msg_col);
1519 cursor_on();
1520 (void)call_shell(cmd, SHELL_COOKED | flags);
1521 did_check_timestamps = FALSE;
1522 need_check_timestamps = TRUE;
1523
1524 /*
1525 * put the message cursor at the end of the screen, avoids wait_return()
1526 * to overwrite the text that the external command showed
1527 */
1528 if (!swapping_screen())
1529 {
1530 msg_row = Rows - 1;
1531 msg_col = 0;
1532 }
1533
1534#ifdef FEAT_AUTOCMD
1535 if (autocmd_busy)
1536 {
1537 if (msg_silent == 0)
1538 redraw_later_clear();
1539 }
1540 else
1541#endif
1542 {
1543 /*
1544 * For ":sh" there is no need to call wait_return(), just redraw.
1545 * Also for the Win32 GUI (the output is in a console window).
1546 * Otherwise there is probably text on the screen that the user wants
1547 * to read before redrawing, so call wait_return().
1548 */
1549#ifndef FEAT_GUI_MSWIN
1550 if (cmd == NULL
1551# ifdef WIN3264
1552 || (winstart && !need_wait_return)
1553# endif
1554 )
1555 {
1556 if (msg_silent == 0)
1557 redraw_later_clear();
1558 need_wait_return = FALSE;
1559 }
1560 else
1561 {
1562 /*
1563 * If we switch screens when starttermcap() is called, we really
1564 * want to wait for "hit return to continue".
1565 */
1566 save_nwr = no_wait_return;
1567 if (swapping_screen())
1568 no_wait_return = FALSE;
1569# ifdef AMIGA
1570 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1571# else
1572 wait_return(msg_silent == 0);
1573# endif
1574 no_wait_return = save_nwr;
1575 }
1576#endif /* FEAT_GUI_W32 */
1577
1578#ifdef MSWIN
1579 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1580#endif
1581 starttermcap(); /* start termcap if not done by wait_return() */
1582
1583 /*
1584 * In an Amiga window redrawing is caused by asking the window size.
1585 * If we got an interrupt this will not work. The chance that the
1586 * window size is wrong is very small, but we need to redraw the
1587 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1588 * but it saves an extra redraw.
1589 */
1590#ifdef AMIGA
1591 if (skip_redraw) /* ':' hit in wait_return() */
1592 {
1593 if (msg_silent == 0)
1594 redraw_later_clear();
1595 }
1596 else if (term_console)
1597 {
1598 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1599 if (got_int && msg_silent == 0)
1600 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1601 else
1602 must_redraw = 0; /* no extra redraw needed */
1603 }
1604#endif
1605 }
1606
1607 /* display any error messages now */
1608 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001609
1610#ifdef FEAT_AUTOCMD
1611 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613}
1614
1615/*
1616 * Create a shell command from a command string, input redirection file and
1617 * output redirection file.
1618 * Returns an allocated string with the shell command, or NULL for failure.
1619 */
1620 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001621make_filter_cmd(
1622 char_u *cmd, /* command */
1623 char_u *itmp, /* NULL or name of input file */
1624 char_u *otmp) /* NULL or name of output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 char_u *buf;
1627 long_u len;
1628
Bram Moolenaar53076832015-12-31 19:53:21 +01001629#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001630 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001631 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001632
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001633 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001634 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1635 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001636 if (is_fish_shell)
1637 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1638 else
1639#endif
1640 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (itmp != NULL)
1642 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1643 if (otmp != NULL)
1644 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1645 buf = lalloc(len, TRUE);
1646 if (buf == NULL)
1647 return NULL;
1648
Bram Moolenaar53076832015-12-31 19:53:21 +01001649#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001651 * Put braces around the command (for concatenated commands) when
1652 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001654 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001655 {
1656 if (is_fish_shell)
1657 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1658 else
1659 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1660 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001661 else
1662 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (itmp != NULL)
1664 {
1665 STRCAT(buf, " < ");
1666 STRCAT(buf, itmp);
1667 }
1668#else
1669 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001670 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 * the use of commands in a pipe.
1672 */
1673 STRCPY(buf, cmd);
1674 if (itmp != NULL)
1675 {
1676 char_u *p;
1677
1678 /*
1679 * If there is a pipe, we have to put the '<' in front of it.
1680 * Don't do this when 'shellquote' is not empty, otherwise the
1681 * redirection would be inside the quotes.
1682 */
1683 if (*p_shq == NUL)
1684 {
1685 p = vim_strchr(buf, '|');
1686 if (p != NULL)
1687 *p = NUL;
1688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1690 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (*p_shq == NUL)
1692 {
1693 p = vim_strchr(cmd, '|');
1694 if (p != NULL)
1695 {
1696 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1697 STRCAT(buf, p);
1698 }
1699 }
1700 }
1701#endif
1702 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001703 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705 return buf;
1706}
1707
1708/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001709 * Append output redirection for file "fname" to the end of string buffer
1710 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 * Works with the 'shellredir' and 'shellpipe' options.
1712 * The caller should make sure that there is enough room:
1713 * STRLEN(opt) + STRLEN(fname) + 3
1714 */
1715 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001716append_redir(
1717 char_u *buf,
1718 int buflen,
1719 char_u *opt,
1720 char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001723 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001725 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001726 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001728 {
1729 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001731 if (p[1] == '%') /* skip %% */
1732 ++p;
1733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (p != NULL)
1735 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001736 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1737 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1738 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001741 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 " %s %s",
1744#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747 (char *)opt, (char *)fname);
1748}
1749
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001750#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001752static int no_viminfo(void);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001753static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02001754static void write_viminfo_version(FILE *fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001755static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756static int viminfo_errcnt;
1757
1758 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001759no_viminfo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 /* "vim -i NONE" does not read or write a viminfo file */
1762 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1763}
1764
1765/*
1766 * Report an error for reading a viminfo file.
1767 * Count the number of errors. When there are more than 10, return TRUE.
1768 */
1769 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001770viminfo_error(char *errnum, char *message, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001772 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1773 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001774 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001775 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1776 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 emsg(IObuff);
1778 if (++viminfo_errcnt >= 10)
1779 {
1780 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1781 return TRUE;
1782 }
1783 return FALSE;
1784}
1785
1786/*
1787 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001788 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 */
1790 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001791read_viminfo(
1792 char_u *file, /* file name or NULL to use default name */
1793 int flags) /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 FILE *fp;
1796 char_u *fname;
1797
1798 if (no_viminfo())
1799 return FAIL;
1800
Bram Moolenaard812df62008-11-09 12:46:09 +00001801 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (fname == NULL)
1803 return FAIL;
1804 fp = mch_fopen((char *)fname, READBIN);
1805
1806 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001807 {
1808 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001809 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1810 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001811 (flags & VIF_WANT_INFO) ? _(" info") : "",
1812 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1813 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001814 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001815 verbose_leave();
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 vim_free(fname);
1819 if (fp == NULL)
1820 return FAIL;
1821
1822 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001823 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 return OK;
1827}
1828
1829/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001830 * Write the viminfo file. The old one is read in first so that effectively a
1831 * merge of current info and old info is done. This allows multiple vims to
1832 * run simultaneously, without losing any marks etc.
1833 * If "forceit" is TRUE, then the old file is not read in, and only internal
1834 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001837write_viminfo(char_u *file, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 char_u *fname;
1840 FILE *fp_in = NULL; /* input viminfo file, if any */
1841 FILE *fp_out = NULL; /* output viminfo file */
1842 char_u *tempname = NULL; /* name of temp viminfo file */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001843 stat_T st_new; /* mch_stat() of potential new file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 char_u *wp;
1845#if defined(UNIX) || defined(VMS)
1846 mode_t umask_save;
1847#endif
1848#ifdef UNIX
1849 int shortname = FALSE; /* use 8.3 file name */
Bram Moolenaar8767f522016-07-01 17:17:39 +02001850 stat_T st_old; /* mch_stat() of existing viminfo file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001852#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001853 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856 if (no_viminfo())
1857 return;
1858
1859 fname = viminfo_filename(file); /* may set to default if NULL */
1860 if (fname == NULL)
1861 return;
1862
1863 fp_in = mch_fopen((char *)fname, READBIN);
1864 if (fp_in == NULL)
1865 {
1866 /* if it does exist, but we can't read it, don't try writing */
1867 if (mch_stat((char *)fname, &st_new) == 0)
1868 goto end;
1869#if defined(UNIX) || defined(VMS)
1870 /*
1871 * For Unix we create the .viminfo non-accessible for others,
1872 * because it may contain text from non-accessible documents.
1873 */
1874 umask_save = umask(077);
1875#endif
1876 fp_out = mch_fopen((char *)fname, WRITEBIN);
1877#if defined(UNIX) || defined(VMS)
1878 (void)umask(umask_save);
1879#endif
1880 }
1881 else
1882 {
1883 /*
1884 * There is an existing viminfo file. Create a temporary file to
1885 * write the new viminfo into, in the same directory as the
1886 * existing viminfo file, which will be renamed later.
1887 */
1888#ifdef UNIX
1889 /*
1890 * For Unix we check the owner of the file. It's not very nice to
1891 * overwrite a user's viminfo file after a "su root", with a
1892 * viminfo file that the user can't read.
1893 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001894 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001895 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001897 if (mch_stat((char *)fname, &st_old) == 0
1898 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001899 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 ? (st_old.st_mode & 0200)
1901 : (st_old.st_gid == getgid()
1902 ? (st_old.st_mode & 0020)
1903 : (st_old.st_mode & 0002))))
1904 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001905 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
1907 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1909 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001910 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 goto end;
1912 }
1913#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001914#ifdef WIN3264
1915 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001916 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 /*
1920 * Make tempname.
1921 * May try twice: Once normal and once with shortname set, just in
1922 * case somebody puts his viminfo file in an 8.3 filesystem.
1923 */
1924 for (;;)
1925 {
1926 tempname = buf_modname(
1927#ifdef UNIX
1928 shortname,
1929#else
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001930# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 gui_is_win32s(),
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001932# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934# endif
1935#endif
1936 fname,
1937#ifdef VMS
1938 (char_u *)"-tmp",
1939#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#endif
1942 FALSE);
1943 if (tempname == NULL) /* out of memory */
1944 break;
1945
1946 /*
1947 * Check if tempfile already exists. Never overwrite an
1948 * existing file!
1949 */
1950 if (mch_stat((char *)tempname, &st_new) == 0)
1951 {
1952#ifdef UNIX
1953 /*
1954 * Check if tempfile is same as original file. May happen
1955 * when modname() gave the same file back. E.g. silly
1956 * link, or file name-length reached. Try again with
1957 * shortname set.
1958 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001959 if (!shortname && st_new.st_dev == st_old.st_dev
1960 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 vim_free(tempname);
1963 tempname = NULL;
1964 shortname = TRUE;
1965 continue;
1966 }
1967#endif
1968 /*
1969 * Try another name. Change one character, just before
1970 * the extension. This should also work for an 8.3
1971 * file name, when after adding the extension it still is
1972 * the same file as the original.
1973 */
1974 wp = tempname + STRLEN(tempname) - 5;
1975 if (wp < gettail(tempname)) /* empty file name? */
1976 wp = gettail(tempname);
1977 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1978 --*wp)
1979 {
1980 /*
1981 * They all exist? Must be something wrong! Don't
1982 * write the viminfo file then.
1983 */
1984 if (*wp == 'a')
1985 {
Bram Moolenaar2d358992016-06-12 21:20:54 +02001986 EMSG2(_("E929: Too many viminfo temp files, like %s!"),
1987 tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 vim_free(tempname);
1989 tempname = NULL;
1990 break;
1991 }
1992 }
1993 }
1994 break;
1995 }
1996
1997 if (tempname != NULL)
1998 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00001999#ifdef VMS
2000 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00002001 umask_save = umask(077);
2002 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2003 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002004#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00002005 int fd;
2006
2007 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002008 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002009 * Unix: same as original file, but strip s-bit. Reset umask to
2010 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002011 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00002012# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002013 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002014 fd = mch_open((char *)tempname,
2015 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002016 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002017 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002018# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002019 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002020 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002021# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00002022 if (fd < 0)
2023 fp_out = NULL;
2024 else
2025 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002026#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 /*
2029 * If we can't create in the same directory, try creating a
2030 * "normal" temp file.
2031 */
2032 if (fp_out == NULL)
2033 {
2034 vim_free(tempname);
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002035 if ((tempname = vim_tempname('o', TRUE)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2037 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038
2039#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00002041 * Make sure the owner can read/write it. This only works for
2042 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 */
2044 if (fp_out != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002045 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046#endif
2047 }
2048 }
2049
2050 /*
2051 * Check if the new viminfo file can be written to.
2052 */
2053 if (fp_out == NULL)
2054 {
2055 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002056 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (fp_in != NULL)
2058 fclose(fp_in);
2059 goto end;
2060 }
2061
2062 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002063 {
2064 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002065 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002066 verbose_leave();
2067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
2069 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002070 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar927030a2016-03-15 18:23:55 +01002072 if (fclose(fp_out) == EOF)
2073 ++viminfo_errcnt;
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (fp_in != NULL)
2076 {
2077 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002078
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002079 /* In case of an error keep the original viminfo file. Otherwise
2080 * rename the newly written file. Give an error if that fails. */
Bram Moolenaar927030a2016-03-15 18:23:55 +01002081 if (viminfo_errcnt == 0)
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002082 {
Bram Moolenaar927030a2016-03-15 18:23:55 +01002083 if (vim_rename(tempname, fname) == -1)
2084 {
2085 ++viminfo_errcnt;
2086 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2087 }
2088# ifdef WIN3264
2089 /* If the viminfo file was hidden then also hide the new file. */
2090 else if (hidden)
2091 mch_hide(fname);
2092# endif
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002093 }
2094 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 mch_remove(tempname);
2096 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098end:
2099 vim_free(fname);
2100 vim_free(tempname);
2101}
2102
2103/*
2104 * Get the viminfo file name to use.
2105 * If "file" is given and not empty, use it (has already been expanded by
2106 * cmdline functions).
2107 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2108 * expand environment variables.
2109 * Returns an allocated string. NULL when out of memory.
2110 */
2111 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002112viminfo_filename(char_u *file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 if (file == NULL || *file == NUL)
2115 {
2116 if (use_viminfo != NULL)
2117 file = use_viminfo;
2118 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2119 {
2120#ifdef VIMINFO_FILE2
2121 /* don't use $HOME when not defined (turned into "c:/"!). */
2122# ifdef VMS
2123 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2124# else
2125 if (mch_getenv((char_u *)"HOME") == NULL)
2126# endif
2127 {
2128 /* don't use $VIM when not available. */
2129 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2130 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2131 file = (char_u *)VIMINFO_FILE2;
2132 else
2133 file = (char_u *)VIMINFO_FILE;
2134 }
2135 else
2136#endif
2137 file = (char_u *)VIMINFO_FILE;
2138 }
2139 expand_env(file, NameBuff, MAXPATHL);
2140 file = NameBuff;
2141 }
2142 return vim_strsave(file);
2143}
2144
2145/*
2146 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2147 */
2148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002149do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
2151 int count = 0;
2152 int eof = FALSE;
2153 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002154 int merge = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2157 return;
2158 vir.vir_fd = fp_in;
2159#ifdef FEAT_MBYTE
2160 vir.vir_conv.vc_type = CONV_NONE;
2161#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002162 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002163 vir.vir_version = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 if (fp_in != NULL)
2166 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002167 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002168 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002169 if (fp_out != NULL)
Bram Moolenaar2d358992016-06-12 21:20:54 +02002170 {
2171 /* Registers and marks are read and kept separate from what
2172 * this Vim is using. They are merged when writing. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002173 prepare_viminfo_registers();
Bram Moolenaar2d358992016-06-12 21:20:54 +02002174 prepare_viminfo_marks();
2175 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002176
Bram Moolenaard812df62008-11-09 12:46:09 +00002177 eof = read_viminfo_up_to_marks(&vir,
2178 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002179 merge = TRUE;
2180 }
2181 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 /* Skip info, find start of marks */
2183 while (!(eof = viminfo_readline(&vir))
2184 && vir.vir_line[0] != '>')
2185 ;
2186 }
2187 if (fp_out != NULL)
2188 {
2189 /* Write the info: */
2190 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2191 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002192 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002193 write_viminfo_version(fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002195 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2197#endif
2198 write_viminfo_search_pattern(fp_out);
2199 write_viminfo_sub_string(fp_out);
2200#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002201 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#endif
2203 write_viminfo_registers(fp_out);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002204 finish_viminfo_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#ifdef FEAT_EVAL
2206 write_viminfo_varlist(fp_out);
2207#endif
2208 write_viminfo_filemarks(fp_out);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002209 finish_viminfo_marks();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002211 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 count = write_viminfo_marks(fp_out);
2213 }
Bram Moolenaard812df62008-11-09 12:46:09 +00002214 if (fp_in != NULL
2215 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT)))
2216 copy_viminfo_marks(&vir, fp_out, count, eof, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 vim_free(vir.vir_line);
2219#ifdef FEAT_MBYTE
2220 if (vir.vir_conv.vc_type != CONV_NONE)
2221 convert_setup(&vir.vir_conv, NULL, NULL);
2222#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002223 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224}
2225
2226/*
2227 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2228 * first part of the viminfo file which contains everything but the marks that
2229 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2230 */
2231 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002232read_viminfo_up_to_marks(
2233 vir_T *virp,
2234 int forceit,
2235 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236{
2237 int eof;
2238 buf_T *buf;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002239 int got_encoding = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002242 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 eof = viminfo_readline(virp);
2246 while (!eof && virp->vir_line[0] != '>')
2247 {
2248 switch (virp->vir_line[0])
2249 {
2250 /* Characters reserved for future expansion, ignored now */
2251 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 case '^': /* to be defined */
2253 case '<': /* long line - ignored */
2254 /* A comment or empty line. */
2255 case NUL:
2256 case '\r':
2257 case '\n':
2258 case '#':
2259 eof = viminfo_readline(virp);
2260 break;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002261 case '|':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002262 eof = read_viminfo_barline(virp, got_encoding,
2263 forceit, writing);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 case '*': /* "*encoding=value" */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002266 got_encoding = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 eof = viminfo_encoding(virp);
2268 break;
2269 case '!': /* global variable */
2270#ifdef FEAT_EVAL
2271 eof = read_viminfo_varlist(virp, writing);
2272#else
2273 eof = viminfo_readline(virp);
2274#endif
2275 break;
2276 case '%': /* entry for buffer list */
2277 eof = read_viminfo_bufferlist(virp, writing);
2278 break;
2279 case '"':
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002280 /* When registers are in bar lines skip the old style register
2281 * lines. */
2282 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2283 eof = read_viminfo_register(virp, forceit);
2284 else
2285 do {
2286 eof = viminfo_readline(virp);
2287 } while (!eof && (virp->vir_line[0] == TAB
2288 || virp->vir_line[0] == '<'));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 break;
2290 case '/': /* Search string */
2291 case '&': /* Substitute search string */
2292 case '~': /* Last search string, followed by '/' or '&' */
2293 eof = read_viminfo_search_pattern(virp, forceit);
2294 break;
2295 case '$':
2296 eof = read_viminfo_sub_string(virp, forceit);
2297 break;
2298 case ':':
2299 case '?':
2300 case '=':
2301 case '@':
2302#ifdef FEAT_CMDHIST
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002303 /* When history is in bar lines skip the old style history
2304 * lines. */
2305 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2306 eof = read_viminfo_history(virp, writing);
2307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002309 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 break;
2311 case '-':
2312 case '\'':
Bram Moolenaara641e1d2016-06-13 21:16:03 +02002313 /* When file marks are in bar lines skip the old style lines. */
2314 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2315 eof = read_viminfo_filemark(virp, forceit);
2316 else
2317 eof = viminfo_readline(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 break;
2319 default:
2320 if (viminfo_error("E575: ", _("Illegal starting char"),
2321 virp->vir_line))
2322 eof = TRUE;
2323 else
2324 eof = viminfo_readline(virp);
2325 break;
2326 }
2327 }
2328
2329#ifdef FEAT_CMDHIST
2330 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002331 if (!writing)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02002332 finish_viminfo_history(virp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#endif
2334
2335 /* Change file names to buffer numbers for fmarks. */
2336 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2337 fmarks_check_names(buf);
2338
2339 return eof;
2340}
2341
2342/*
2343 * Compare the 'encoding' value in the viminfo file with the current value of
2344 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2345 * conversion of text with iconv() in viminfo_readstring().
2346 */
2347 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002348viminfo_encoding(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
2350#ifdef FEAT_MBYTE
2351 char_u *p;
2352 int i;
2353
2354 if (get_viminfo_parameter('c') != 0)
2355 {
2356 p = vim_strchr(virp->vir_line, '=');
2357 if (p != NULL)
2358 {
2359 /* remove trailing newline */
2360 ++p;
2361 for (i = 0; vim_isprintc(p[i]); ++i)
2362 ;
2363 p[i] = NUL;
2364
2365 convert_setup(&virp->vir_conv, p, p_enc);
2366 }
2367 }
2368#endif
2369 return viminfo_readline(virp);
2370}
2371
2372/*
2373 * Read a line from the viminfo file.
2374 * Returns TRUE for end-of-file;
2375 */
2376 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002377viminfo_readline(vir_T *virp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2380}
2381
2382/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002383 * Check string read from viminfo file.
2384 * Remove '\n' at the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 * - replace CTRL-V CTRL-V with CTRL-V
2386 * - replace CTRL-V 'n' with '\n'
2387 *
2388 * Check for a long line as written by viminfo_writestring().
2389 *
2390 * Return the string in allocated memory (NULL when out of memory).
2391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002393viminfo_readstring(
2394 vir_T *virp,
2395 int off, /* offset for virp->vir_line */
2396 int convert UNUSED) /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 char_u *retval;
2399 char_u *s, *d;
2400 long len;
2401
2402 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2403 {
2404 len = atol((char *)virp->vir_line + off + 1);
2405 retval = lalloc(len, TRUE);
2406 if (retval == NULL)
2407 {
2408 /* Line too long? File messed up? Skip next line. */
2409 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2410 return NULL;
2411 }
2412 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2413 s = retval + 1; /* Skip the leading '<' */
2414 }
2415 else
2416 {
2417 retval = vim_strsave(virp->vir_line + off);
2418 if (retval == NULL)
2419 return NULL;
2420 s = retval;
2421 }
2422
2423 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2424 d = retval;
2425 while (*s != NUL && *s != '\n')
2426 {
2427 if (s[0] == Ctrl_V && s[1] != NUL)
2428 {
2429 if (s[1] == 'n')
2430 *d++ = '\n';
2431 else
2432 *d++ = Ctrl_V;
2433 s += 2;
2434 }
2435 else
2436 *d++ = *s++;
2437 }
2438 *d = NUL;
2439
2440#ifdef FEAT_MBYTE
2441 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2442 {
2443 d = string_convert(&virp->vir_conv, retval, NULL);
2444 if (d != NULL)
2445 {
2446 vim_free(retval);
2447 retval = d;
2448 }
2449 }
2450#endif
2451
2452 return retval;
2453}
2454
2455/*
2456 * write string to viminfo file
2457 * - replace CTRL-V with CTRL-V CTRL-V
2458 * - replace '\n' with CTRL-V 'n'
2459 * - add a '\n' at the end
2460 *
2461 * For a long line:
2462 * - write " CTRL-V <length> \n " in first line
2463 * - write " < <string> \n " in second line
2464 */
2465 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002466viminfo_writestring(FILE *fd, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 int c;
2469 char_u *s;
2470 int len = 0;
2471
2472 for (s = p; *s != NUL; ++s)
2473 {
2474 if (*s == Ctrl_V || *s == '\n')
2475 ++len;
2476 ++len;
2477 }
2478
2479 /* If the string will be too long, write its length and put it in the next
2480 * line. Take into account that some room is needed for what comes before
2481 * the string (e.g., variable name). Add something to the length for the
2482 * '<', NL and trailing NUL. */
2483 if (len > LSIZE / 2)
2484 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2485
2486 while ((c = *p++) != NUL)
2487 {
2488 if (c == Ctrl_V || c == '\n')
2489 {
2490 putc(Ctrl_V, fd);
2491 if (c == '\n')
2492 c = 'n';
2493 }
2494 putc(c, fd);
2495 }
2496 putc('\n', fd);
2497}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002498
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002499/*
2500 * Write a string in quotes that barline_parse() can read back.
2501 * Breaks the line in less than LSIZE pieces when needed.
2502 * Returns remaining characters in the line.
2503 */
2504 int
2505barline_writestring(FILE *fd, char_u *s, int remaining_start)
2506{
2507 char_u *p;
2508 int remaining = remaining_start;
2509 int len = 2;
2510
2511 /* Count the number of characters produced, including quotes. */
2512 for (p = s; *p != NUL; ++p)
2513 {
2514 if (*p == NL)
2515 len += 2;
2516 else if (*p == '"' || *p == '\\')
2517 len += 2;
2518 else
2519 ++len;
2520 }
2521 if (len > remaining)
2522 {
2523 fprintf(fd, ">%d\n|<", len);
2524 remaining = LSIZE - 20;
2525 }
2526
2527 putc('"', fd);
2528 for (p = s; *p != NUL; ++p)
2529 {
2530 if (*p == NL)
2531 {
2532 putc('\\', fd);
2533 putc('n', fd);
2534 --remaining;
2535 }
2536 else if (*p == '"' || *p == '\\')
2537 {
2538 putc('\\', fd);
2539 putc(*p, fd);
2540 --remaining;
2541 }
2542 else
2543 putc(*p, fd);
2544 --remaining;
2545
2546 if (remaining < 3)
2547 {
2548 putc('\n', fd);
2549 putc('|', fd);
2550 putc('<', fd);
2551 /* Leave enough space for another continuation. */
2552 remaining = LSIZE - 20;
2553 }
2554 }
2555 putc('"', fd);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002556 return remaining - 2;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002557}
2558
2559/*
2560 * Parse a viminfo line starting with '|'.
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002561 * Add each decoded value to "values".
Bram Moolenaarecefe712016-06-20 12:50:17 +02002562 * Returns TRUE if the next line is to be read after using the parsed values.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002563 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002564 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002565barline_parse(vir_T *virp, char_u *text, garray_T *values)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002566{
2567 char_u *p = text;
2568 char_u *nextp = NULL;
Bram Moolenaarfdd82fe2016-06-06 21:38:44 +02002569 char_u *buf = NULL;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002570 bval_T *value;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002571 int i;
2572 int allocated = FALSE;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002573 int eof;
Bram Moolenaar01227092016-06-11 14:47:40 +02002574#ifdef FEAT_MBYTE
2575 char_u *sconv;
2576 int converted;
2577#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002578
2579 while (*p == ',')
2580 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002581 ++p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002582 if (ga_grow(values, 1) == FAIL)
2583 break;
2584 value = (bval_T *)(values->ga_data) + values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002585
2586 if (*p == '>')
2587 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002588 /* Need to read a continuation line. Put strings in allocated
2589 * memory, because virp->vir_line is overwritten. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002590 if (!allocated)
2591 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002592 for (i = 0; i < values->ga_len; ++i)
2593 {
2594 bval_T *vp = (bval_T *)(values->ga_data) + i;
2595
2596 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002597 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002598 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
2599 vp->bv_allocated = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002600 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002601 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002602 allocated = TRUE;
2603 }
2604
2605 if (vim_isdigit(p[1]))
2606 {
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002607 size_t len;
2608 size_t todo;
2609 size_t n;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002610
2611 /* String value was split into lines that are each shorter
2612 * than LSIZE:
2613 * |{bartype},>{length of "{text}{text2}"}
2614 * |<"{text1}
2615 * |<{text2}",{value}
Bram Moolenaarecefe712016-06-20 12:50:17 +02002616 * Length includes the quotes.
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002617 */
2618 ++p;
2619 len = getdigits(&p);
Bram Moolenaar1d5f1d02016-06-07 22:50:01 +02002620 buf = alloc((int)(len + 1));
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002621 if (buf == NULL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002622 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002623 p = buf;
2624 for (todo = len; todo > 0; todo -= n)
2625 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002626 eof = viminfo_readline(virp);
2627 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002628 || virp->vir_line[1] != '<')
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002629 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002630 /* File was truncated or garbled. Read another line if
2631 * this one starts with '|'. */
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002632 vim_free(buf);
Bram Moolenaarecefe712016-06-20 12:50:17 +02002633 return eof || virp->vir_line[0] == '|';
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02002634 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002635 /* Get length of text, excluding |< and NL chars. */
2636 n = STRLEN(virp->vir_line);
2637 while (n > 0 && (virp->vir_line[n - 1] == NL
2638 || virp->vir_line[n - 1] == CAR))
2639 --n;
2640 n -= 2;
2641 if (n > todo)
2642 {
2643 /* more values follow after the string */
2644 nextp = virp->vir_line + 2 + todo;
2645 n = todo;
2646 }
2647 mch_memmove(p, virp->vir_line + 2, n);
2648 p += n;
2649 }
2650 *p = NUL;
2651 p = buf;
2652 }
2653 else
2654 {
2655 /* Line ending in ">" continues in the next line:
2656 * |{bartype},{lots of values},>
2657 * |<{value},{value}
2658 */
Bram Moolenaarecefe712016-06-20 12:50:17 +02002659 eof = viminfo_readline(virp);
2660 if (eof || virp->vir_line[0] != '|'
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002661 || virp->vir_line[1] != '<')
Bram Moolenaarecefe712016-06-20 12:50:17 +02002662 /* File was truncated or garbled. Read another line if
2663 * this one starts with '|'. */
2664 return eof || virp->vir_line[0] == '|';
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002665 p = virp->vir_line + 2;
2666 }
2667 }
2668
2669 if (isdigit(*p))
2670 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002671 value->bv_type = BVAL_NR;
2672 value->bv_nr = getdigits(&p);
2673 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002674 }
2675 else if (*p == '"')
2676 {
2677 int len = 0;
2678 char_u *s = p;
2679
2680 /* Unescape special characters in-place. */
2681 ++p;
2682 while (*p != '"')
2683 {
2684 if (*p == NL || *p == NUL)
Bram Moolenaarecefe712016-06-20 12:50:17 +02002685 return TRUE; /* syntax error, drop the value */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002686 if (*p == '\\')
2687 {
2688 ++p;
2689 if (*p == 'n')
2690 s[len++] = '\n';
2691 else
2692 s[len++] = *p;
2693 ++p;
2694 }
2695 else
2696 s[len++] = *p++;
2697 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002698 ++p;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002699 s[len] = NUL;
2700
Bram Moolenaar01227092016-06-11 14:47:40 +02002701#ifdef FEAT_MBYTE
2702 converted = FALSE;
2703 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
2704 {
2705 sconv = string_convert(&virp->vir_conv, s, NULL);
2706 if (sconv != NULL)
2707 {
2708 if (s == buf)
2709 vim_free(s);
2710 s = sconv;
2711 buf = s;
2712 converted = TRUE;
2713 }
2714 }
2715#endif
2716 /* Need to copy in allocated memory if the string wasn't allocated
2717 * above and we did allocate before, thus vir_line may change. */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002718 if (s != buf && allocated)
2719 s = vim_strsave(s);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002720 value->bv_string = s;
2721 value->bv_type = BVAL_STRING;
2722 value->bv_len = len;
2723 value->bv_allocated = allocated
Bram Moolenaar01227092016-06-11 14:47:40 +02002724#ifdef FEAT_MBYTE
2725 || converted
2726#endif
2727 ;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002728 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002729 if (nextp != NULL)
2730 {
2731 /* values following a long string */
2732 p = nextp;
2733 nextp = NULL;
2734 }
2735 }
2736 else if (*p == ',')
2737 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002738 value->bv_type = BVAL_EMPTY;
2739 ++values->ga_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002740 }
2741 else
2742 break;
2743 }
Bram Moolenaarecefe712016-06-20 12:50:17 +02002744 return TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002745}
2746
2747 static int
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002748read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002749{
2750 char_u *p = virp->vir_line + 1;
2751 int bartype;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002752 garray_T values;
2753 bval_T *vp;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002754 int i;
Bram Moolenaarecefe712016-06-20 12:50:17 +02002755 int read_next = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002756
2757 /* The format is: |{bartype},{value},...
2758 * For a very long string:
2759 * |{bartype},>{length of "{text}{text2}"}
2760 * |<{text1}
2761 * |<{text2},{value}
2762 * For a long line not using a string
2763 * |{bartype},{lots of values},>
2764 * |<{value},{value}
2765 */
2766 if (*p == '<')
2767 {
2768 /* Continuation line of an unrecognized item. */
2769 if (writing)
2770 ga_add_string(&virp->vir_barlines, virp->vir_line);
2771 }
2772 else
2773 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002774 ga_init2(&values, sizeof(bval_T), 20);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002775 bartype = getdigits(&p);
2776 switch (bartype)
2777 {
2778 case BARTYPE_VERSION:
2779 /* Only use the version when it comes before the encoding.
2780 * If it comes later it was copied by a Vim version that
2781 * doesn't understand the version. */
2782 if (!got_encoding)
2783 {
Bram Moolenaarecefe712016-06-20 12:50:17 +02002784 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002785 vp = (bval_T *)values.ga_data;
2786 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2787 virp->vir_version = vp->bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002788 }
2789 break;
2790
2791 case BARTYPE_HISTORY:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002792 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002793 handle_viminfo_history(&values, writing);
2794 break;
2795
2796 case BARTYPE_REGISTER:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002797 read_next = barline_parse(virp, p, &values);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002798 handle_viminfo_register(&values, force);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002799 break;
2800
Bram Moolenaar2d358992016-06-12 21:20:54 +02002801 case BARTYPE_MARK:
Bram Moolenaarecefe712016-06-20 12:50:17 +02002802 read_next = barline_parse(virp, p, &values);
Bram Moolenaar2d358992016-06-12 21:20:54 +02002803 handle_viminfo_mark(&values, force);
2804 break;
2805
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002806 default:
2807 /* copy unrecognized line (for future use) */
2808 if (writing)
2809 ga_add_string(&virp->vir_barlines, virp->vir_line);
2810 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002811 for (i = 0; i < values.ga_len; ++i)
2812 {
2813 vp = (bval_T *)values.ga_data + i;
2814 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2815 vim_free(vp->bv_string);
2816 }
2817 ga_clear(&values);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002818 }
2819
Bram Moolenaarecefe712016-06-20 12:50:17 +02002820 if (read_next)
2821 return viminfo_readline(virp);
2822 return FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02002823}
2824
2825 static void
2826write_viminfo_version(FILE *fp_out)
2827{
2828 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
2829 BARTYPE_VERSION, VIMINFO_VERSION);
2830}
2831
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002832 static void
2833write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2834{
2835 int i;
2836 garray_T *gap = &virp->vir_barlines;
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002837 int seen_useful = FALSE;
2838 char *line;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002839
2840 if (gap->ga_len > 0)
2841 {
2842 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2843
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002844 /* Skip over continuation lines until seeing a useful line. */
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002845 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaardec85cf2016-07-02 22:33:46 +02002846 {
2847 line = ((char **)(gap->ga_data))[i];
2848 if (seen_useful || line[1] != '<')
2849 {
2850 fputs(line, fp_out);
2851 seen_useful = TRUE;
2852 }
2853 }
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002854 }
2855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856#endif /* FEAT_VIMINFO */
2857
Bram Moolenaar724f2ed2016-06-11 22:21:17 +02002858#if defined(FEAT_CMDHIST) || defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002859/*
2860 * Return the current time in seconds. Calls time(), unless test_settime()
2861 * was used.
2862 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02002863 time_T
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002864vim_time(void)
2865{
2866# ifdef FEAT_EVAL
2867 return time_for_testing == 0 ? time(NULL) : time_for_testing;
2868# else
2869 return time(NULL);
2870# endif
2871}
2872#endif
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874/*
2875 * Implementation of ":fixdel", also used by get_stty().
2876 * <BS> resulting <Del>
2877 * ^? ^H
2878 * not ^? ^?
2879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002881do_fixdel(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 char_u *p;
2884
2885 p = find_termcode((char_u *)"kb");
2886 add_termcode((char_u *)"kD", p != NULL
2887 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2888}
2889
2890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002891print_line_no_prefix(
2892 linenr_T lnum,
2893 int use_number,
2894 int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002896 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 if (curwin->w_p_nu || use_number)
2899 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002900 vim_snprintf((char *)numbuf, sizeof(numbuf),
2901 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2903 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002904 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905}
2906
2907/*
2908 * Print a text line. Also in silent mode ("ex -s").
2909 */
2910 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002911print_line(linenr_T lnum, int use_number, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
2913 int save_silent = silent_mode;
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002916 silent_mode = FALSE;
2917 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2918 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (save_silent)
2920 {
2921 msg_putchar('\n');
2922 cursor_on(); /* msg_start() switches it off */
2923 out_flush();
2924 silent_mode = save_silent;
2925 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00002926 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002929 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002930rename_buffer(char_u *new_fname)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002931{
2932 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02002933 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002934
Bram Moolenaar7e283842013-05-30 11:43:15 +02002935#ifdef FEAT_AUTOCMD
2936 buf = curbuf;
2937 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002938 /* buffer changed, don't change name now */
2939 if (buf != curbuf)
2940 return FAIL;
2941# ifdef FEAT_EVAL
2942 if (aborting()) /* autocmds may abort script processing */
2943 return FAIL;
2944# endif
2945#endif
2946 /*
2947 * The name of the current buffer will be changed.
2948 * A new (unlisted) buffer entry needs to be made to hold the old file
2949 * name, which will become the alternate file name.
2950 * But don't set the alternate file name if the buffer didn't have a
2951 * name.
2952 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02002953 fname = curbuf->b_ffname;
2954 sfname = curbuf->b_sfname;
2955 xfname = curbuf->b_fname;
2956 curbuf->b_ffname = NULL;
2957 curbuf->b_sfname = NULL;
2958 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002959 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02002960 curbuf->b_ffname = fname;
2961 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002962 return FAIL;
2963 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02002964 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002965 if (xfname != NULL && *xfname != NUL)
2966 {
2967 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2968 if (buf != NULL && !cmdmod.keepalt)
2969 curwin->w_alt_fnum = buf->b_fnum;
2970 }
2971 vim_free(fname);
2972 vim_free(sfname);
2973#ifdef FEAT_AUTOCMD
Bram Moolenaar7e283842013-05-30 11:43:15 +02002974 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002975#endif
2976 /* Change directories when the 'acd' option is set. */
2977 DO_AUTOCHDIR
2978 return OK;
2979}
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981/*
2982 * ":file[!] [fname]".
2983 */
2984 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002985ex_file(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002987 /* ":0file" removes the file name. Check for illegal uses ":3file",
2988 * "0file name", etc. */
2989 if (eap->addr_count > 0
2990 && (*eap->arg != NUL
2991 || eap->line2 > 0
2992 || eap->addr_count > 1))
2993 {
2994 EMSG(_(e_invarg));
2995 return;
2996 }
2997
2998 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003000 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003 /* print full file name if :cd used */
Bram Moolenaar426dd022016-03-15 15:09:29 +01003004 if (!shortmess(SHM_FILEINFO))
3005 fileinfo(FALSE, FALSE, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
3008/*
3009 * ":update".
3010 */
3011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003012ex_update(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 if (curbufIsChanged())
3015 (void)do_write(eap);
3016}
3017
3018/*
3019 * ":write" and ":saveas".
3020 */
3021 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003022ex_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 if (eap->usefilter) /* input lines to shell command */
3025 do_bang(1, eap, FALSE, TRUE, FALSE);
3026 else
3027 (void)do_write(eap);
3028}
3029
3030/*
3031 * write current buffer to file 'eap->arg'
3032 * if 'eap->append' is TRUE, append to the file
3033 *
3034 * if *eap->arg == NUL write to current file
3035 *
3036 * return FAIL for failure, OK otherwise
3037 */
3038 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003039do_write(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 int other;
3042 char_u *fname = NULL; /* init to shut up gcc */
3043 char_u *ffname;
3044 int retval = FAIL;
3045 char_u *free_fname = NULL;
3046#ifdef FEAT_BROWSE
3047 char_u *browse_file = NULL;
3048#endif
3049 buf_T *alt_buf = NULL;
3050
3051 if (not_writing()) /* check 'write' option */
3052 return FAIL;
3053
3054 ffname = eap->arg;
3055#ifdef FEAT_BROWSE
3056 if (cmdmod.browse)
3057 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003058 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 NULL, NULL, NULL, curbuf);
3060 if (browse_file == NULL)
3061 goto theend;
3062 ffname = browse_file;
3063 }
3064#endif
3065 if (*ffname == NUL)
3066 {
3067 if (eap->cmdidx == CMD_saveas)
3068 {
3069 EMSG(_(e_argreq));
3070 goto theend;
3071 }
3072 other = FALSE;
3073 }
3074 else
3075 {
3076 fname = ffname;
3077 free_fname = fix_fname(ffname);
3078 /*
3079 * When out-of-memory, keep unexpanded file name, because we MUST be
3080 * able to write the file in this situation.
3081 */
3082 if (free_fname != NULL)
3083 ffname = free_fname;
3084 other = otherfile(ffname);
3085 }
3086
3087 /*
3088 * If we have a new file, put its name in the list of alternate file names.
3089 */
3090 if (other)
3091 {
3092 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
3093 || eap->cmdidx == CMD_saveas)
3094 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
3095 else
3096 alt_buf = buflist_findname(ffname);
3097 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
3098 {
3099 /* Overwriting a file that is loaded in another buffer is not a
3100 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00003101 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 goto theend;
3103 }
3104 }
3105
3106 /*
3107 * Writing to the current file is not allowed in readonly mode
3108 * and a file name is required.
3109 * "nofile" and "nowrite" buffers cannot be written implicitly either.
3110 */
3111 if (!other && (
3112#ifdef FEAT_QUICKFIX
3113 bt_dontwrite_msg(curbuf) ||
3114#endif
3115 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
3116 goto theend;
3117
3118 if (!other)
3119 {
3120 ffname = curbuf->b_ffname;
3121 fname = curbuf->b_fname;
3122 /*
3123 * Not writing the whole file is only allowed with '!'.
3124 */
3125 if ( (eap->line1 != 1
3126 || eap->line2 != curbuf->b_ml.ml_line_count)
3127 && !eap->forceit
3128 && !eap->append
3129 && !p_wa)
3130 {
3131#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3132 if (p_confirm || cmdmod.confirm)
3133 {
3134 if (vim_dialog_yesno(VIM_QUESTION, NULL,
3135 (char_u *)_("Write partial file?"), 2) != VIM_YES)
3136 goto theend;
3137 eap->forceit = TRUE;
3138 }
3139 else
3140#endif
3141 {
3142 EMSG(_("E140: Use ! to write partial buffer"));
3143 goto theend;
3144 }
3145 }
3146 }
3147
3148 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3149 {
3150 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3151 {
3152#ifdef FEAT_AUTOCMD
3153 buf_T *was_curbuf = curbuf;
3154
3155 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003156 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157# ifdef FEAT_EVAL
3158 if (curbuf != was_curbuf || aborting())
3159# else
3160 if (curbuf != was_curbuf)
3161# endif
3162 {
3163 /* buffer changed, don't change name now */
3164 retval = FAIL;
3165 goto theend;
3166 }
3167#endif
3168 /* Exchange the file names for the current and the alternate
3169 * buffer. This makes it look like we are now editing the buffer
3170 * under the new name. Must be done before buf_write(), because
3171 * if there is no file name and 'cpo' contains 'F', it will set
3172 * the file name. */
3173 fname = alt_buf->b_fname;
3174 alt_buf->b_fname = curbuf->b_fname;
3175 curbuf->b_fname = fname;
3176 fname = alt_buf->b_ffname;
3177 alt_buf->b_ffname = curbuf->b_ffname;
3178 curbuf->b_ffname = fname;
3179 fname = alt_buf->b_sfname;
3180 alt_buf->b_sfname = curbuf->b_sfname;
3181 curbuf->b_sfname = fname;
3182 buf_name_changed(curbuf);
3183#ifdef FEAT_AUTOCMD
3184 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00003185 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (!alt_buf->b_p_bl)
3187 {
3188 alt_buf->b_p_bl = TRUE;
3189 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3190 }
3191# ifdef FEAT_EVAL
3192 if (curbuf != was_curbuf || aborting())
3193# else
3194 if (curbuf != was_curbuf)
3195# endif
3196 {
3197 /* buffer changed, don't write the file */
3198 retval = FAIL;
3199 goto theend;
3200 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003201
3202 /* If 'filetype' was empty try detecting it now. */
3203 if (*curbuf->b_p_ft == NUL)
3204 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003205 if (au_has_group((char_u *)"filetypedetect"))
3206 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
Bram Moolenaar1610d052016-06-09 22:53:01 +02003207 TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003208 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003209 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01003210
3211 /* Autocommands may have changed buffer names, esp. when
3212 * 'autochdir' is set. */
3213 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif
3215 }
3216
3217 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3218 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003219
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003220 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003221 if (eap->cmdidx == CMD_saveas)
3222 {
3223 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00003224 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003225 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00003226#ifdef FEAT_WINDOWS
3227 redraw_tabline = TRUE;
3228#endif
3229 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003230 /* Change directories when the 'acd' option is set. */
3231 DO_AUTOCHDIR
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234
3235theend:
3236#ifdef FEAT_BROWSE
3237 vim_free(browse_file);
3238#endif
3239 vim_free(free_fname);
3240 return retval;
3241}
3242
3243/*
3244 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
3245 * BF_NEW or BF_READERR, check for overwriting current file.
3246 * May set eap->forceit if a dialog says it's OK to overwrite.
3247 * Return OK if it's OK, FAIL if it is not.
3248 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02003249 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003250check_overwrite(
3251 exarg_T *eap,
3252 buf_T *buf,
3253 char_u *fname, /* file name to be used (can differ from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 buf->ffname) */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003255 char_u *ffname, /* full path version of fname */
3256 int other) /* writing under other name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 /*
3259 * write to other file or b_flags set or not writing the whole file:
3260 * overwriting only allowed with '!'
3261 */
3262 if ( (other
3263 || (buf->b_flags & BF_NOTEDITED)
3264 || ((buf->b_flags & BF_NEW)
3265 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
3266 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00003268#ifdef FEAT_QUICKFIX
3269 && !bt_nofile(buf)
3270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 && vim_fexists(ffname))
3272 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003273 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003275#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00003276 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003277 if (mch_isdir(ffname))
3278 {
3279 EMSG2(_(e_isadir2), ffname);
3280 return FAIL;
3281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#endif
3283#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003284 if (p_confirm || cmdmod.confirm)
3285 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003286 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003288 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
3289 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
3290 return FAIL;
3291 eap->forceit = TRUE;
3292 }
3293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003295 {
3296 EMSG(_(e_exists));
3297 return FAIL;
3298 }
3299 }
3300
3301 /* For ":w! filename" check that no swap file exists for "filename". */
3302 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003304 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003305 char_u *p;
3306 int r;
3307 char_u *swapname;
3308
3309 /* We only try the first entry in 'directory', without checking if
3310 * it's writable. If the "." directory is not writable the write
3311 * will probably fail anyway.
3312 * Use 'shortname' of the current buffer, since there is no buffer
3313 * for the written file. */
3314 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02003315 {
3316 dir = alloc(5);
3317 if (dir == NULL)
3318 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003319 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003320 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003321 else
3322 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003323 dir = alloc(MAXPATHL);
3324 if (dir == NULL)
3325 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003326 p = p_dir;
3327 copy_option_part(&p, dir, MAXPATHL, ",");
3328 }
3329 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003330 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003331 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003332 if (r)
3333 {
3334#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3335 if (p_confirm || cmdmod.confirm)
3336 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003337 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003338
3339 dialog_msg(buff,
3340 _("Swap file \"%s\" exists, overwrite anyway?"),
3341 swapname);
3342 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
3343 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003344 {
3345 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003346 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003347 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003348 eap->forceit = TRUE;
3349 }
3350 else
3351#endif
3352 {
3353 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
3354 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003355 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00003356 return FAIL;
3357 }
3358 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003359 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 }
3362 return OK;
3363}
3364
3365/*
3366 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3367 */
3368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003369ex_wnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 int i;
3372
3373 if (eap->cmd[1] == 'n')
3374 i = curwin->w_arg_idx + (int)eap->line2;
3375 else
3376 i = curwin->w_arg_idx - (int)eap->line2;
3377 eap->line1 = 1;
3378 eap->line2 = curbuf->b_ml.ml_line_count;
3379 if (do_write(eap) != FAIL)
3380 do_argfile(eap, i);
3381}
3382
3383/*
3384 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3385 */
3386 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003387do_wqall(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 buf_T *buf;
3390 int error = 0;
3391 int save_forceit = eap->forceit;
3392
3393 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3394 exiting = TRUE;
3395
3396 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3397 {
3398 if (bufIsChanged(buf))
3399 {
3400 /*
3401 * Check if there is a reason the buffer cannot be written:
3402 * 1. if the 'write' option is set
3403 * 2. if there is no file name (even after browsing)
3404 * 3. if the 'readonly' is set (even after a dialog)
3405 * 4. if overwriting is allowed (even after a dialog)
3406 */
3407 if (not_writing())
3408 {
3409 ++error;
3410 break;
3411 }
3412#ifdef FEAT_BROWSE
3413 /* ":browse wall": ask for file name if there isn't one */
3414 if (buf->b_ffname == NULL && cmdmod.browse)
3415 browse_save_fname(buf);
3416#endif
3417 if (buf->b_ffname == NULL)
3418 {
3419 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3420 ++error;
3421 }
3422 else if (check_readonly(&eap->forceit, buf)
3423 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3424 FALSE) == FAIL)
3425 {
3426 ++error;
3427 }
3428 else
3429 {
3430 if (buf_write_all(buf, eap->forceit) == FAIL)
3431 ++error;
3432#ifdef FEAT_AUTOCMD
3433 /* an autocommand may have deleted the buffer */
3434 if (!buf_valid(buf))
3435 buf = firstbuf;
3436#endif
3437 }
3438 eap->forceit = save_forceit; /* check_overwrite() may set it */
3439 }
3440 }
3441 if (exiting)
3442 {
3443 if (!error)
3444 getout(0); /* exit Vim */
3445 not_exiting();
3446 }
3447}
3448
3449/*
3450 * Check the 'write' option.
3451 * Return TRUE and give a message when it's not st.
3452 */
3453 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003454not_writing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 if (p_write)
3457 return FALSE;
3458 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3459 return TRUE;
3460}
3461
3462/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003463 * Check if a buffer is read-only (either 'readonly' option is set or file is
3464 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3465 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 */
3467 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003468check_readonly(int *forceit, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003470 stat_T st;
Bram Moolenaar5386a122007-06-28 20:02:32 +00003471
3472 /* Handle a file being readonly when the 'readonly' option is set or when
3473 * the file exists and permissions are read-only.
3474 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3475 * important for device checks but not here. */
3476 if (!*forceit && (buf->b_p_ro
3477 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3478 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
3480#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3481 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3482 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003483 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar5386a122007-06-28 20:02:32 +00003485 if (buf->b_p_ro)
3486 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3487 buf->b_fname);
3488 else
3489 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 +00003490 buf->b_fname);
3491
3492 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3493 {
3494 /* Set forceit, to force the writing of a readonly file */
3495 *forceit = TRUE;
3496 return FALSE;
3497 }
3498 else
3499 return TRUE;
3500 }
3501 else
3502#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003503 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003505 else
3506 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3507 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 return TRUE;
3509 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return FALSE;
3512}
3513
3514/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003515 * Try to abandon current file and edit a new or existing file.
3516 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003518 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003519 * -1 for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 * 'lnum' is the line number for the cursor in the new file (if non-zero).
3521 */
3522 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003523getfile(
3524 int fnum,
3525 char_u *ffname,
3526 char_u *sfname,
3527 int setpm,
3528 linenr_T lnum,
3529 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 int other;
3532 int retval;
3533 char_u *free_me = NULL;
3534
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003535 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003537#ifdef FEAT_AUTOCMD
3538 if (curbuf_locked())
3539 return 1;
3540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
3542 if (fnum == 0)
3543 {
3544 /* make ffname full path, set sfname */
3545 fname_expand(curbuf, &ffname, &sfname);
3546 other = otherfile(ffname);
3547 free_me = ffname; /* has been allocated, free() later */
3548 }
3549 else
3550 other = (fnum != curbuf->b_fnum);
3551
3552 if (other)
3553 ++no_wait_return; /* don't wait for autowrite message */
3554 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
3555 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3556 {
3557#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3558 if (p_confirm && p_write)
3559 dialog_changed(curbuf, FALSE);
3560 if (curbufIsChanged())
3561#endif
3562 {
3563 if (other)
3564 --no_wait_return;
3565 EMSG(_(e_nowrtmsg));
3566 retval = 2; /* file has been changed */
3567 goto theend;
3568 }
3569 }
3570 if (other)
3571 --no_wait_return;
3572 if (setpm)
3573 setpcmark();
3574 if (!other)
3575 {
3576 if (lnum != 0)
3577 curwin->w_cursor.lnum = lnum;
3578 check_cursor_lnum();
3579 beginline(BL_SOL | BL_FIX);
3580 retval = 0; /* it's in the same file */
3581 }
3582 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003583 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
3584 curwin) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 retval = -1; /* opened another file */
3586 else
3587 retval = 1; /* error encountered */
3588
3589theend:
3590 vim_free(free_me);
3591 return retval;
3592}
3593
3594/*
3595 * start editing a new file
3596 *
3597 * fnum: file number; if zero use ffname/sfname
3598 * ffname: the file name
3599 * - full path if sfname used,
3600 * - any file name if sfname is NULL
3601 * - empty string to re-edit with the same file name (but may be
3602 * in a different directory)
3603 * - NULL to start an empty buffer
3604 * sfname: the short file name (or NULL)
3605 * eap: contains the command to be executed after loading the file and
3606 * forced 'ff' and 'fenc'
3607 * newlnum: if > 0: put cursor on this line number (if possible)
3608 * if ECMD_LASTL: use last position in loaded file
3609 * if ECMD_LAST: use last position in all files
3610 * if ECMD_ONE: use first line
3611 * flags:
3612 * ECMD_HIDE: if TRUE don't free the current buffer
3613 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3614 * ECMD_OLDBUF: use existing buffer if it exists
3615 * ECMD_FORCEIT: ! used for Ex command
3616 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003617 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003618 * window, NULL when splitting the window first. When not NULL info
3619 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 *
3621 * return FAIL for failure, OK otherwise
3622 */
3623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003624do_ecmd(
3625 int fnum,
3626 char_u *ffname,
3627 char_u *sfname,
3628 exarg_T *eap, /* can be NULL! */
3629 linenr_T newlnum,
3630 int flags,
3631 win_T *oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 int other_file; /* TRUE if editing another file */
3634 int oldbuf; /* TRUE if using existing buffer */
3635#ifdef FEAT_AUTOCMD
3636 int auto_buf = FALSE; /* TRUE if autocommands brought us
3637 into the buffer unexpectedly */
3638 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003639 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640#endif
3641 buf_T *buf;
3642#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3643 buf_T *old_curbuf = curbuf;
3644#endif
3645 char_u *free_fname = NULL;
3646#ifdef FEAT_BROWSE
3647 char_u *browse_file = NULL;
3648#endif
3649 int retval = FAIL;
3650 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003651 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 linenr_T topline = 0;
3653 int newcol = -1;
3654 int solcol = -1;
3655 pos_T *pos;
3656#ifdef FEAT_SUN_WORKSHOP
3657 char_u *cp;
3658#endif
3659 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003660#ifdef FEAT_SPELL
3661 int did_get_winopts = FALSE;
3662#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003663 int readfile_flags = 0;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003664 int did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 if (eap != NULL)
3667 command = eap->do_ecmd_cmd;
3668
3669 if (fnum != 0)
3670 {
3671 if (fnum == curbuf->b_fnum) /* file is already being edited */
3672 return OK; /* nothing to do */
3673 other_file = TRUE;
3674 }
3675 else
3676 {
3677#ifdef FEAT_BROWSE
3678 if (cmdmod.browse)
3679 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003680# ifdef FEAT_AUTOCMD
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003681 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003682# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003683 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003684# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003685 au_has_group((char_u *)"FileExplorer"))
3686 {
3687 /* No browsing supported but we do have the file explorer:
3688 * Edit the directory. */
3689 if (ffname == NULL || !mch_isdir(ffname))
3690 ffname = (char_u *)".";
3691 }
3692 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003693# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003694 {
3695 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003697 if (browse_file == NULL)
3698 goto theend;
3699 ffname = browse_file;
3700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702#endif
3703 /* if no short name given, use ffname for short name */
3704 if (sfname == NULL)
3705 sfname = ffname;
3706#ifdef USE_FNAME_CASE
3707# ifdef USE_LONG_FNAME
3708 if (USE_LONG_FNAME)
3709# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003710 if (sfname != NULL)
3711 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712#endif
3713
3714#ifdef FEAT_LISTCMDS
3715 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3716 goto theend;
3717#endif
3718
3719 if (ffname == NULL)
3720 other_file = TRUE;
3721 /* there is no file name */
3722 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3723 other_file = FALSE;
3724 else
3725 {
3726 if (*ffname == NUL) /* re-edit with same file name */
3727 {
3728 ffname = curbuf->b_ffname;
3729 sfname = curbuf->b_fname;
3730 }
3731 free_fname = fix_fname(ffname); /* may expand to full path name */
3732 if (free_fname != NULL)
3733 ffname = free_fname;
3734 other_file = otherfile(ffname);
3735#ifdef FEAT_SUN_WORKSHOP
3736 if (usingSunWorkShop && p_acd
3737 && (cp = vim_strrchr(sfname, '/')) != NULL)
3738 sfname = ++cp;
3739#endif
3740 }
3741 }
3742
3743 /*
3744 * if the file was changed we may not be allowed to abandon it
3745 * - if we are going to re-edit the same file
3746 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3747 */
3748 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3749 || (curbuf->b_nwindows == 1
3750 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003751 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3752 | (other_file ? 0 : CCGD_MULTWIN)
3753 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3754 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
3756 if (fnum == 0 && other_file && ffname != NULL)
3757 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3758 goto theend;
3759 }
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 /*
3762 * End Visual mode before switching to another buffer, so the text can be
3763 * copied into the GUI selection buffer.
3764 */
3765 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003767#ifdef FEAT_AUTOCMD
3768 if ((command != NULL || newlnum > (linenr_T)0)
3769 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3770 {
3771 int len;
3772 char_u *p;
3773
3774 /* Set v:swapcommand for the SwapExists autocommands. */
3775 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003776 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003777 else
3778 len = 30;
3779 p = alloc((unsigned)len);
3780 if (p != NULL)
3781 {
3782 if (command != NULL)
3783 vim_snprintf((char *)p, len, ":%s\r", command);
3784 else
3785 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3786 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3787 did_set_swapcommand = TRUE;
3788 vim_free(p);
3789 }
3790 }
3791#endif
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 /*
3794 * If we are starting to edit another file, open a (new) buffer.
3795 * Otherwise we re-use the current buffer.
3796 */
3797 if (other_file)
3798 {
3799#ifdef FEAT_LISTCMDS
3800 if (!(flags & ECMD_ADDBUF))
3801#endif
3802 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003803 if (!cmdmod.keepalt)
3804 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003805 if (oldwin != NULL)
3806 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808
3809 if (fnum)
3810 buf = buflist_findnr(fnum);
3811 else
3812 {
3813#ifdef FEAT_LISTCMDS
3814 if (flags & ECMD_ADDBUF)
3815 {
3816 linenr_T tlnum = 1L;
3817
3818 if (command != NULL)
3819 {
3820 tlnum = atol((char *)command);
3821 if (tlnum <= 0)
3822 tlnum = 1L;
3823 }
3824 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3825 goto theend;
3826 }
3827#endif
3828 buf = buflist_new(ffname, sfname, 0L,
3829 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003830#ifdef FEAT_AUTOCMD
3831 /* autocommands may change curwin and curbuf */
3832 if (oldwin != NULL)
3833 oldwin = curwin;
3834 old_curbuf = curbuf;
3835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 if (buf == NULL)
3838 goto theend;
3839 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3840 {
3841 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 else /* existing memfile */
3844 {
3845 oldbuf = TRUE;
3846 (void)buf_check_timestamp(buf, FALSE);
3847 /* Check if autocommands made buffer invalid or changed the current
3848 * buffer. */
3849 if (!buf_valid(buf)
3850#ifdef FEAT_AUTOCMD
3851 || curbuf != old_curbuf
3852#endif
3853 )
3854 goto theend;
3855#ifdef FEAT_EVAL
3856 if (aborting()) /* autocmds may abort script processing */
3857 goto theend;
3858#endif
3859 }
3860
3861 /* May jump to last used line number for a loaded buffer or when asked
3862 * for explicitly */
3863 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3864 {
3865 pos = buflist_findfpos(buf);
3866 newlnum = pos->lnum;
3867 solcol = pos->col;
3868 }
3869
3870 /*
3871 * Make the (new) buffer the one used by the current window.
3872 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3873 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003874 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 */
3876 if (buf != curbuf)
3877 {
3878#ifdef FEAT_AUTOCMD
3879 /*
3880 * Be careful: The autocommands may delete any buffer and change
3881 * the current buffer.
3882 * - If the buffer we are going to edit is deleted, give up.
3883 * - If the current buffer is deleted, prefer to load the new
3884 * buffer when loading a buffer is required. This avoids
3885 * loading another buffer which then must be closed again.
3886 * - If we ended up in the new buffer already, need to skip a few
3887 * things, set auto_buf.
3888 */
3889 if (buf->b_fname != NULL)
3890 new_name = vim_strsave(buf->b_fname);
3891 au_new_curbuf = buf;
3892 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3893 if (!buf_valid(buf)) /* new buffer has been deleted */
3894 {
3895 delbuf_msg(new_name); /* frees new_name */
3896 goto theend;
3897 }
3898# ifdef FEAT_EVAL
3899 if (aborting()) /* autocmds may abort script processing */
3900 {
3901 vim_free(new_name);
3902 goto theend;
3903 }
3904# endif
3905 if (buf == curbuf) /* already in new buffer */
3906 auto_buf = TRUE;
3907 else
3908 {
3909 if (curbuf == old_curbuf)
3910#endif
3911 buf_copy_options(buf, BCO_ENTER);
3912
3913 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003914 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003915 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003916 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918#ifdef FEAT_AUTOCMD
Bram Moolenaarfc573802011-12-30 15:01:59 +01003919 /* Autocommands may open a new window and leave oldwin open
3920 * which leads to crashes since the above call sets
3921 * oldwin->w_buffer to NULL. */
3922 if (curwin != oldwin && oldwin != aucmd_win
3923 && win_valid(oldwin) && oldwin->w_buffer == NULL)
3924 win_close(oldwin, FALSE);
3925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# ifdef FEAT_EVAL
3927 if (aborting()) /* autocmds may abort script processing */
3928 {
3929 vim_free(new_name);
3930 goto theend;
3931 }
3932# endif
3933 /* Be careful again, like above. */
3934 if (!buf_valid(buf)) /* new buffer has been deleted */
3935 {
3936 delbuf_msg(new_name); /* frees new_name */
3937 goto theend;
3938 }
3939 if (buf == curbuf) /* already in new buffer */
3940 auto_buf = TRUE;
3941 else
3942#endif
3943 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003944#ifdef FEAT_SYN_HL
3945 /*
3946 * <VN> We could instead free the synblock
3947 * and re-attach to buffer, perhaps.
3948 */
3949 if (curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02003950 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 curwin->w_buffer = buf;
3953 curbuf = buf;
3954 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003955
3956 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
3957 if (!oldbuf && eap != NULL)
3958 {
3959 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003960#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003961 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003962#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965
3966 /* May get the window options from the last time this buffer
3967 * was in this window (or another window). If not used
3968 * before, reset the local window options to the global
3969 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00003970 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003971#ifdef FEAT_SPELL
3972 did_get_winopts = TRUE;
3973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975#ifdef FEAT_AUTOCMD
3976 }
3977 vim_free(new_name);
3978 au_new_curbuf = NULL;
3979#endif
3980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982 curwin->w_pcmark.lnum = 1;
3983 curwin->w_pcmark.col = 0;
3984 }
3985 else /* !other_file */
3986 {
3987 if (
3988#ifdef FEAT_LISTCMDS
3989 (flags & ECMD_ADDBUF) ||
3990#endif
3991 check_fname() == FAIL)
3992 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01003993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 oldbuf = (flags & ECMD_OLDBUF);
3995 }
3996
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01003997 /* Don't redraw until the cursor is in the right line, otherwise
3998 * autocommands may cause ml_get errors. */
3999 ++RedrawingDisabled;
4000 did_inc_redrawing_disabled = TRUE;
4001
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004002#ifdef FEAT_AUTOCMD
4003 buf = curbuf;
4004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4006 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01004007 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009 else
4010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 /* Don't make a buffer listed if it's a help buffer. Useful when
4012 * using CTRL-O to go back to a help file. */
4013 if (!curbuf->b_help)
4014 set_buflisted(TRUE);
4015 }
4016
4017#ifdef FEAT_AUTOCMD
4018 /* If autocommands change buffers under our fingers, forget about
4019 * editing the file. */
4020 if (buf != curbuf)
4021 goto theend;
4022# ifdef FEAT_EVAL
4023 if (aborting()) /* autocmds may abort script processing */
4024 goto theend;
4025# endif
4026
4027 /* Since we are starting to edit a file, consider the filetype to be
4028 * unset. Helps for when an autocommand changes files and expects syntax
4029 * highlighting to work in the other file. */
4030 did_filetype = FALSE;
4031#endif
4032
4033/*
4034 * other_file oldbuf
4035 * FALSE FALSE re-edit same file, buffer is re-used
4036 * FALSE TRUE re-edit same file, nothing changes
4037 * TRUE FALSE start editing new file, new buffer
4038 * TRUE TRUE start editing in existing buffer (nothing to do)
4039 */
4040 if (!other_file && !oldbuf) /* re-use the buffer */
4041 {
4042 set_last_cursor(curwin); /* may set b_last_cursor */
4043 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4044 {
4045 newlnum = curwin->w_cursor.lnum;
4046 solcol = curwin->w_cursor.col;
4047 }
4048#ifdef FEAT_AUTOCMD
4049 buf = curbuf;
4050 if (buf->b_fname != NULL)
4051 new_name = vim_strsave(buf->b_fname);
4052 else
4053 new_name = NULL;
4054#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004055 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4056 {
4057 /* Save all the text, so that the reload can be undone.
4058 * Sync first so that this is a separate undo-able action. */
4059 u_sync(FALSE);
4060 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4061 == FAIL)
4062 goto theend;
4063 u_unchanged(curbuf);
4064 buf_freeall(curbuf, BFA_KEEP_UNDO);
4065
4066 /* tell readfile() not to clear or reload undo info */
4067 readfile_flags = READ_KEEP_UNDO;
4068 }
4069 else
4070 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#ifdef FEAT_AUTOCMD
4072 /* If autocommands deleted the buffer we were going to re-edit, give
4073 * up and jump to the end. */
4074 if (!buf_valid(buf))
4075 {
4076 delbuf_msg(new_name); /* frees new_name */
4077 goto theend;
4078 }
4079 vim_free(new_name);
4080
4081 /* If autocommands change buffers under our fingers, forget about
4082 * re-editing the file. Should do the buf_clear_file(), but perhaps
4083 * the autocommands changed the buffer... */
4084 if (buf != curbuf)
4085 goto theend;
4086# ifdef FEAT_EVAL
4087 if (aborting()) /* autocmds may abort script processing */
4088 goto theend;
4089# endif
4090#endif
4091 buf_clear_file(curbuf);
4092 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4093 curbuf->b_op_end.lnum = 0;
4094 }
4095
4096/*
4097 * If we get here we are sure to start editing
4098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 /* Assume success now */
4100 retval = OK;
4101
4102 /*
4103 * Reset cursor position, could be used by autocommands.
4104 */
4105 check_cursor();
4106
4107 /*
4108 * Check if we are editing the w_arg_idx file in the argument list.
4109 */
4110 check_arg_idx(curwin);
4111
4112#ifdef FEAT_AUTOCMD
4113 if (!auto_buf)
4114#endif
4115 {
4116 /*
4117 * Set cursor and init window before reading the file and executing
4118 * autocommands. This allows for the autocommands to position the
4119 * cursor.
4120 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004121 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004124 /* It's possible that all lines in the buffer changed. Need to update
4125 * automatic folding for all windows where it's used. */
4126# ifdef FEAT_WINDOWS
4127 {
4128 win_T *win;
4129 tabpage_T *tp;
4130
4131 FOR_ALL_TAB_WINDOWS(tp, win)
4132 if (win->w_buffer == curbuf)
4133 foldUpdateAll(win);
4134 }
4135# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 foldUpdateAll(curwin);
Bram Moolenaareb1b6792007-08-21 13:29:28 +00004137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
4139
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004140 /* Change directories when the 'acd' option is set. */
4141 DO_AUTOCHDIR
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /*
4144 * Careful: open_buffer() and apply_autocmds() may change the current
4145 * buffer and window.
4146 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01004147 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 topline = curwin->w_topline;
4149 if (!oldbuf) /* need to read the file */
4150 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004151#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 swap_exists_action = SEA_DIALOG;
4153#endif
4154 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4155
4156 /*
4157 * Open the buffer and read the file.
4158 */
4159#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004160 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 retval = FAIL;
4162#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004163 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#endif
4165
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004166#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 if (swap_exists_action == SEA_QUIT)
4168 retval = FAIL;
4169 handle_swap_exists(old_curbuf);
4170#endif
4171 }
4172#ifdef FEAT_AUTOCMD
4173 else
4174 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004175 /* Read the modelines, but only to set window-local options. Any
4176 * buffer-local options have already been set and may have been
4177 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00004178 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
4181 &retval);
4182 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4183 &retval);
4184 }
4185 check_arg_idx(curwin);
4186#endif
4187
Bram Moolenaareab316b2015-03-24 11:46:30 +01004188 /* If autocommands change the cursor position or topline, we should
4189 * keep it. Also when it moves within a line. */
4190 if (!equalpos(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192 newlnum = curwin->w_cursor.lnum;
4193 newcol = curwin->w_cursor.col;
4194 }
4195 if (curwin->w_topline == topline)
4196 topline = 0;
4197
4198 /* Even when cursor didn't move we need to recompute topline. */
4199 changed_line_abv_curs();
4200
4201#ifdef FEAT_TITLE
4202 maketitle();
4203#endif
4204 }
4205
4206#ifdef FEAT_DIFF
4207 /* Tell the diff stuff that this buffer is new and/or needs updating.
4208 * Also needed when re-editing the same buffer, because unloading will
4209 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004210 if (curwin->w_p_diff)
4211 {
4212 diff_buf_add(curbuf);
4213 diff_invalidate(curbuf);
4214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#endif
4216
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004217#ifdef FEAT_SPELL
4218 /* If the window options were changed may need to set the spell language.
4219 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004220 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
4221 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004222#endif
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (command == NULL)
4225 {
4226 if (newcol >= 0) /* position set by autocommands */
4227 {
4228 curwin->w_cursor.lnum = newlnum;
4229 curwin->w_cursor.col = newcol;
4230 check_cursor();
4231 }
4232 else if (newlnum > 0) /* line number from caller or old position */
4233 {
4234 curwin->w_cursor.lnum = newlnum;
4235 check_cursor_lnum();
4236 if (solcol >= 0 && !p_sol)
4237 {
4238 /* 'sol' is off: Use last known column. */
4239 curwin->w_cursor.col = solcol;
4240 check_cursor_col();
4241#ifdef FEAT_VIRTUALEDIT
4242 curwin->w_cursor.coladd = 0;
4243#endif
4244 curwin->w_set_curswant = TRUE;
4245 }
4246 else
4247 beginline(BL_SOL | BL_FIX);
4248 }
4249 else /* no line number, go to last line in Ex mode */
4250 {
4251 if (exmode_active)
4252 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4253 beginline(BL_WHITE | BL_FIX);
4254 }
4255 }
4256
4257#ifdef FEAT_WINDOWS
4258 /* Check if cursors in other windows on the same buffer are still valid */
4259 check_lnums(FALSE);
4260#endif
4261
4262 /*
4263 * Did not read the file, need to show some info about the file.
4264 * Do this after setting the cursor.
4265 */
4266 if (oldbuf
4267#ifdef FEAT_AUTOCMD
4268 && !auto_buf
4269#endif
4270 )
4271 {
4272 int msg_scroll_save = msg_scroll;
4273
4274 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4275 * message. */
4276 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
4277 msg_scroll = FALSE;
4278 if (!msg_scroll) /* wait a bit when overwriting an error msg */
4279 check_for_delay(FALSE);
4280 msg_start();
4281 msg_scroll = msg_scroll_save;
4282 msg_scrolled_ign = TRUE;
4283
Bram Moolenaar426dd022016-03-15 15:09:29 +01004284 if (!shortmess(SHM_FILEINFO))
4285 fileinfo(FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 msg_scrolled_ign = FALSE;
4288 }
4289
4290 if (command != NULL)
4291 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
4292
4293#ifdef FEAT_KEYMAP
4294 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004295 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#endif
4297
4298 --RedrawingDisabled;
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004299 did_inc_redrawing_disabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (!skip_redraw)
4301 {
4302 n = p_so;
4303 if (topline == 0 && command == NULL)
4304 p_so = 999; /* force cursor halfway the window */
4305 update_topline();
4306#ifdef FEAT_SCROLLBIND
4307 curwin->w_scbind_pos = curwin->w_topline;
4308#endif
4309 p_so = n;
4310 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
4311 }
4312
4313 if (p_im)
4314 need_start_insertmode = TRUE;
4315
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004316 /* Change directories when the 'acd' option is set. */
4317 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00004319#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02004320 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02004323 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
4325# endif
4326# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004327 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00004328 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329# endif
4330 }
4331#endif
4332
4333theend:
Bram Moolenaarab9fc7e2016-02-06 15:29:40 +01004334 if (did_inc_redrawing_disabled)
4335 --RedrawingDisabled;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004336#ifdef FEAT_AUTOCMD
4337 if (did_set_swapcommand)
4338 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340#ifdef FEAT_BROWSE
4341 vim_free(browse_file);
4342#endif
4343 vim_free(free_fname);
4344 return retval;
4345}
4346
4347#ifdef FEAT_AUTOCMD
4348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004349delbuf_msg(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4352 name == NULL ? (char_u *)"" : name);
4353 vim_free(name);
4354 au_new_curbuf = NULL;
4355}
4356#endif
4357
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004358static int append_indent = 0; /* autoindent for first line */
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360/*
4361 * ":insert" and ":append", also used by ":change"
4362 */
4363 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004364ex_append(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
4366 char_u *theline;
4367 int did_undo = FALSE;
4368 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004369 int indent = 0;
4370 char_u *p;
4371 int vcol;
4372 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004374 /* the ! flag toggles autoindent */
4375 if (eap->forceit)
4376 curbuf->b_p_ai = !curbuf->b_p_ai;
4377
4378 /* First autoindent comes from the line we start on */
4379 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4380 append_indent = get_indent_lnum(lnum);
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if (eap->cmdidx != CMD_append)
4383 --lnum;
4384
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004385 /* when the buffer is empty need to delete the dummy line */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004386 if (empty && lnum == 1)
4387 lnum = 0;
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 State = INSERT; /* behave like in Insert mode */
4390 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4391 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004392
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004393 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 msg_scroll = TRUE;
4396 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004397 if (curbuf->b_p_ai)
4398 {
4399 if (append_indent >= 0)
4400 {
4401 indent = append_indent;
4402 append_indent = -1;
4403 }
4404 else if (lnum > 0)
4405 indent = get_indent_lnum(lnum);
4406 }
4407 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004409 {
4410 /* No getline() function, use the lines that follow. This ends
4411 * when there is no more. */
4412 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4413 break;
4414 p = vim_strchr(eap->nextcmd, NL);
4415 if (p == NULL)
4416 p = eap->nextcmd + STRLEN(eap->nextcmd);
4417 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4418 if (*p != NUL)
4419 ++p;
4420 eap->nextcmd = p;
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004423 {
4424 int save_State = State;
4425
4426 /* Set State to avoid the cursor shape to be set to INSERT mode
4427 * when getline() returns. */
4428 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 theline = eap->getline(
4430#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004431 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004433 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004434 State = save_State;
4435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004437 if (theline == NULL)
4438 break;
4439
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004440 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4441 if (ex_keep_indent)
4442 append_indent = indent;
4443
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004444 /* Look for the "." after automatic indent. */
4445 vcol = 0;
4446 for (p = theline; indent > vcol; ++p)
4447 {
4448 if (*p == ' ')
4449 ++vcol;
4450 else if (*p == TAB)
4451 vcol += 8 - vcol % 8;
4452 else
4453 break;
4454 }
4455 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004456 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4457 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 vim_free(theline);
4460 break;
4461 }
4462
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004463 /* don't use autoindent if nothing was typed. */
4464 if (p[0] == NUL)
4465 theline[0] = NUL;
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 did_undo = TRUE;
4468 ml_append(lnum, theline, (colnr_T)0, FALSE);
Bram Moolenaar70e136e2016-07-01 14:04:51 +02004469 appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 vim_free(theline);
4472 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004473
4474 if (empty)
4475 {
4476 ml_delete(2L, FALSE);
4477 empty = FALSE;
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 State = NORMAL;
4481
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004482 if (eap->forceit)
4483 curbuf->b_p_ai = !curbuf->b_p_ai;
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004486 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 * "end" is set to lnum when something has been appended, otherwise
4488 * it is the same than "start" -- Acevedo */
4489 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4490 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4491 if (eap->cmdidx != CMD_append)
4492 --curbuf->b_op_start.lnum;
4493 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4494 ? lnum : curbuf->b_op_start.lnum;
4495 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4496 curwin->w_cursor.lnum = lnum;
4497 check_cursor_lnum();
4498 beginline(BL_SOL | BL_FIX);
4499
4500 need_wait_return = FALSE; /* don't use wait_return() now */
4501 ex_no_reprint = TRUE;
4502}
4503
4504/*
4505 * ":change"
4506 */
4507 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004508ex_change(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 linenr_T lnum;
4511
4512 if (eap->line2 >= eap->line1
4513 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4514 return;
4515
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004516 /* the ! flag toggles autoindent */
4517 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4518 append_indent = get_indent_lnum(eap->line1);
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4521 {
4522 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4523 break;
4524 ml_delete(eap->line1, FALSE);
4525 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004526
4527 /* make sure the cursor is not beyond the end of the file now */
4528 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4530
4531 /* ":append" on the line above the deleted lines. */
4532 eap->line2 = eap->line1;
4533 ex_append(eap);
4534}
4535
4536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004537ex_z(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
4539 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004540 int bigness;
4541 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 int minus = 0;
4543 linenr_T start, end, curs, i;
4544 int j;
4545 linenr_T lnum = eap->line2;
4546
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004547 /* Vi compatible: ":z!" uses display height, without a count uses
4548 * 'scroll' */
4549 if (eap->forceit)
4550 bigness = curwin->w_height;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004551#ifdef FEAT_WINDOWS
Bram Moolenaar631abc32014-02-22 22:27:47 +01004552 else if (firstwin != lastwin)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004553 bigness = curwin->w_height - 3;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004554#endif
Bram Moolenaar631abc32014-02-22 22:27:47 +01004555 else
4556 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (bigness < 1)
4558 bigness = 1;
4559
4560 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004561 kind = x;
4562 if (*kind == '-' || *kind == '+' || *kind == '='
4563 || *kind == '^' || *kind == '.')
4564 ++x;
4565 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 ++x;
4567
4568 if (*x != 0)
4569 {
4570 if (!VIM_ISDIGIT(*x))
4571 {
4572 EMSG(_("E144: non-numeric argument to :z"));
4573 return;
4574 }
4575 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004578 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004579 if (*kind == '=')
4580 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004584 /* the number of '-' and '+' multiplies the distance */
4585 if (*kind == '-' || *kind == '+')
4586 for (x = kind + 1; *x == *kind; ++x)
4587 ;
4588
4589 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
4591 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004592 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4593 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004594 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 break;
4596
4597 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004598 start = lnum - (bigness + 1) / 2 + 1;
4599 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 curs = lnum;
4601 minus = 1;
4602 break;
4603
4604 case '^':
4605 start = lnum - bigness * 2;
4606 end = lnum - bigness;
4607 curs = lnum - bigness;
4608 break;
4609
4610 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004611 start = lnum - (bigness + 1) / 2 + 1;
4612 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 curs = end;
4614 break;
4615
4616 default: /* '+' */
4617 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004618 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004619 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004620 else if (eap->addr_count == 0)
4621 ++start;
4622 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 curs = end;
4624 break;
4625 }
4626
4627 if (start < 1)
4628 start = 1;
4629
4630 if (end > curbuf->b_ml.ml_line_count)
4631 end = curbuf->b_ml.ml_line_count;
4632
4633 if (curs > curbuf->b_ml.ml_line_count)
4634 curs = curbuf->b_ml.ml_line_count;
4635
4636 for (i = start; i <= end; i++)
4637 {
4638 if (minus && i == lnum)
4639 {
4640 msg_putchar('\n');
4641
4642 for (j = 1; j < Columns; j++)
4643 msg_putchar('-');
4644 }
4645
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004646 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648 if (minus && i == lnum)
4649 {
4650 msg_putchar('\n');
4651
4652 for (j = 1; j < Columns; j++)
4653 msg_putchar('-');
4654 }
4655 }
4656
4657 curwin->w_cursor.lnum = curs;
4658 ex_no_reprint = TRUE;
4659}
4660
4661/*
4662 * Check if the restricted flag is set.
4663 * If so, give an error message and return TRUE.
4664 * Otherwise, return FALSE.
4665 */
4666 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004667check_restricted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
4669 if (restricted)
4670 {
4671 EMSG(_("E145: Shell commands not allowed in rvim"));
4672 return TRUE;
4673 }
4674 return FALSE;
4675}
4676
4677/*
4678 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4679 * If so, give an error message and return TRUE.
4680 * Otherwise, return FALSE.
4681 */
4682 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004683check_secure(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 if (secure)
4686 {
4687 secure = 2;
4688 EMSG(_(e_curdir));
4689 return TRUE;
4690 }
4691#ifdef HAVE_SANDBOX
4692 /*
4693 * In the sandbox more things are not allowed, including the things
4694 * disallowed in secure mode.
4695 */
4696 if (sandbox != 0)
4697 {
4698 EMSG(_(e_sandbox));
4699 return TRUE;
4700 }
4701#endif
4702 return FALSE;
4703}
4704
4705static char_u *old_sub = NULL; /* previous substitute pattern */
4706static int global_need_beginline; /* call beginline() after ":g" */
4707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708/* do_sub()
4709 *
4710 * Perform a substitution from line eap->line1 to line eap->line2 using the
4711 * command pointed to by eap->arg which should be of the form:
4712 *
4713 * /pattern/substitution/{flags}
4714 *
4715 * The usual escapes are supported as described in the regexp docs.
4716 */
4717 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004718do_sub(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 linenr_T lnum;
4721 long i = 0;
4722 regmmatch_T regmatch;
4723 static int do_all = FALSE; /* do multiple substitutions per line */
4724 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004725 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 static int do_error = TRUE; /* if false, ignore errors */
4727 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004728 static int do_list = FALSE; /* list last line with subs. */
4729 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 static int do_ic = 0; /* ignore case flag */
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004731 int save_do_all; /* remember user specified 'g' flag */
4732 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4734 int delimiter;
4735 int sublen;
4736 int got_quit = FALSE;
4737 int got_match = FALSE;
4738 int temp;
4739 int which_pat;
4740 char_u *cmd;
4741 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004742 linenr_T first_line = 0; /* first changed line */
4743 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * change */
4745 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4746 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004747 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004748 char_u *sub_firstline; /* allocated copy of first sub line */
4749 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004750 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004751 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004752#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004753 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 cmd = eap->arg;
4757 if (!global_busy)
4758 {
4759 sub_nsubs = 0;
4760 sub_nlines = 0;
4761 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004762 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (eap->cmdidx == CMD_tilde)
4765 which_pat = RE_LAST; /* use last used regexp */
4766 else
4767 which_pat = RE_SUBST; /* use last substitute regexp */
4768
4769 /* new pattern and substitution */
4770 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4771 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4772 {
4773 /* don't accept alphanumeric for separator */
4774 if (isalpha(*cmd))
4775 {
4776 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4777 return;
4778 }
4779 /*
4780 * undocumented vi feature:
4781 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4782 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4783 */
4784 if (*cmd == '\\')
4785 {
4786 ++cmd;
4787 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4788 {
4789 EMSG(_(e_backslash));
4790 return;
4791 }
4792 if (*cmd != '&')
4793 which_pat = RE_SEARCH; /* use last '/' pattern */
4794 pat = (char_u *)""; /* empty search pattern */
4795 delimiter = *cmd++; /* remember delimiter character */
4796 }
4797 else /* find the end of the regexp */
4798 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004799#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4800 if (p_altkeymap && curwin->w_p_rl)
4801 lrF_sub(cmd);
4802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 which_pat = RE_LAST; /* use last used regexp */
4804 delimiter = *cmd++; /* remember delimiter character */
4805 pat = cmd; /* remember start of search pat */
4806 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4807 if (cmd[0] == delimiter) /* end delimiter found */
4808 *cmd++ = NUL; /* replace it with a NUL */
4809 }
4810
4811 /*
4812 * Small incompatibility: vi sees '\n' as end of the command, but in
4813 * Vim we want to use '\n' to find/substitute a NUL.
4814 */
4815 sub = cmd; /* remember the start of the substitution */
4816
4817 while (cmd[0])
4818 {
4819 if (cmd[0] == delimiter) /* end delimiter found */
4820 {
4821 *cmd++ = NUL; /* replace it with a NUL */
4822 break;
4823 }
4824 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4825 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004826 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828
4829 if (!eap->skip)
4830 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004831 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4832 if (STRCMP(sub, "%") == 0
4833 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4834 {
4835 if (old_sub == NULL) /* there is no previous command */
4836 {
4837 EMSG(_(e_nopresub));
4838 return;
4839 }
4840 sub = old_sub;
4841 }
4842 else
4843 {
4844 vim_free(old_sub);
4845 old_sub = vim_strsave(sub);
4846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 }
4849 else if (!eap->skip) /* use previous pattern and substitution */
4850 {
4851 if (old_sub == NULL) /* there is no previous command */
4852 {
4853 EMSG(_(e_nopresub));
4854 return;
4855 }
4856 pat = NULL; /* search_regcomp() will use previous pattern */
4857 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004858
4859 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4860 * last column after using "$". */
4861 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004864 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4865 * more efficient.
4866 * TODO: find a generic solution to make line-joining operations more
4867 * efficient, avoid allocating a string that grows in size.
4868 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004869 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004870 && *sub == NUL
4871 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4872 || *cmd == 'p' || *cmd == '#'))))
4873 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004874 linenr_T joined_lines_count;
4875
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004876 curwin->w_cursor.lnum = eap->line1;
4877 if (*cmd == 'l')
4878 eap->flags = EXFLAG_LIST;
4879 else if (*cmd == '#')
4880 eap->flags = EXFLAG_NR;
4881 else if (*cmd == 'p')
4882 eap->flags = EXFLAG_PRINT;
4883
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004884 /* The number of lines joined is the number of lines in the range plus
4885 * one. One less when the last line is included. */
4886 joined_lines_count = eap->line2 - eap->line1 + 1;
4887 if (eap->line2 < curbuf->b_ml.ml_line_count)
4888 ++joined_lines_count;
4889 if (joined_lines_count > 1)
4890 {
4891 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
4892 sub_nsubs = joined_lines_count - 1;
4893 sub_nlines = 1;
4894 (void)do_sub_msg(FALSE);
4895 ex_may_print(eap);
4896 }
4897
4898 if (!cmdmod.keeppatterns)
4899 save_re_pat(RE_SUBST, pat, p_magic);
4900#ifdef FEAT_CMDHIST
4901 /* put pattern in history */
4902 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
4903#endif
4904
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004905 return;
4906 }
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 /*
4909 * Find trailing options. When '&' is used, keep old options.
4910 */
4911 if (*cmd == '&')
4912 ++cmd;
4913 else
4914 {
4915 if (!p_ed)
4916 {
4917 if (p_gd) /* default is global on */
4918 do_all = TRUE;
4919 else
4920 do_all = FALSE;
4921 do_ask = FALSE;
4922 }
4923 do_error = TRUE;
4924 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004925 do_count = FALSE;
Bram Moolenaarfe40d1a2007-07-24 09:16:38 +00004926 do_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 do_ic = 0;
4928 }
4929 while (*cmd)
4930 {
4931 /*
4932 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4933 * 'r' is never inverted.
4934 */
4935 if (*cmd == 'g')
4936 do_all = !do_all;
4937 else if (*cmd == 'c')
4938 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004939 else if (*cmd == 'n')
4940 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 else if (*cmd == 'e')
4942 do_error = !do_error;
4943 else if (*cmd == 'r') /* use last used regexp */
4944 which_pat = RE_LAST;
4945 else if (*cmd == 'p')
4946 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004947 else if (*cmd == '#')
4948 {
4949 do_print = TRUE;
4950 do_number = TRUE;
4951 }
4952 else if (*cmd == 'l')
4953 {
4954 do_print = TRUE;
4955 do_list = TRUE;
4956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 else if (*cmd == 'i') /* ignore case */
4958 do_ic = 'i';
4959 else if (*cmd == 'I') /* don't ignore case */
4960 do_ic = 'I';
4961 else
4962 break;
4963 ++cmd;
4964 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004965 if (do_count)
4966 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004968 save_do_all = do_all;
4969 save_do_ask = do_ask;
4970
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 /*
4972 * check for a trailing count
4973 */
4974 cmd = skipwhite(cmd);
4975 if (VIM_ISDIGIT(*cmd))
4976 {
4977 i = getdigits(&cmd);
4978 if (i <= 0 && !eap->skip && do_error)
4979 {
4980 EMSG(_(e_zerocount));
4981 return;
4982 }
4983 eap->line1 = eap->line2;
4984 eap->line2 += i - 1;
4985 if (eap->line2 > curbuf->b_ml.ml_line_count)
4986 eap->line2 = curbuf->b_ml.ml_line_count;
4987 }
4988
4989 /*
4990 * check for trailing command or garbage
4991 */
4992 cmd = skipwhite(cmd);
4993 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4994 {
4995 eap->nextcmd = check_nextcmd(cmd);
4996 if (eap->nextcmd == NULL)
4997 {
4998 EMSG(_(e_trailing));
4999 return;
5000 }
5001 }
5002
5003 if (eap->skip) /* not executing commands, only parsing */
5004 return;
5005
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005006 if (!do_count && !curbuf->b_p_ma)
5007 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005008 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00005009 EMSG(_(e_modifiable));
5010 return;
5011 }
5012
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5014 {
5015 if (do_error)
5016 EMSG(_(e_invcmd));
5017 return;
5018 }
5019
5020 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
5021 if (do_ic == 'i')
5022 regmatch.rmm_ic = TRUE;
5023 else if (do_ic == 'I')
5024 regmatch.rmm_ic = FALSE;
5025
5026 sub_firstline = NULL;
5027
5028 /*
5029 * ~ in the substitute pattern is replaced with the old pattern.
5030 * We do it here once to avoid it to be replaced over and over again.
5031 * But don't do it when it starts with "\=", then it's an expression.
5032 */
5033 if (!(sub[0] == '\\' && sub[1] == '='))
5034 sub = regtilde(sub, p_magic);
5035
5036 /*
5037 * Check for a match on each line.
5038 */
5039 line2 = eap->line2;
5040 for (lnum = eap->line1; lnum <= line2 && !(got_quit
5041#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
5042 || aborting()
5043#endif
5044 ); ++lnum)
5045 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005046 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5047 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (nmatch)
5049 {
5050 colnr_T copycol;
5051 colnr_T matchcol;
5052 colnr_T prev_matchcol = MAXCOL;
5053 char_u *new_end, *new_start = NULL;
5054 unsigned new_start_len = 0;
5055 char_u *p1;
5056 int did_sub = FALSE;
5057 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005058 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 long nmatch_tl = 0; /* nr of lines matched below lnum */
5060 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005061 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005062 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * The new text is build up step by step, to avoid too much
5066 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00005067 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 * copycol Column in the old text where we started
5069 * looking for a match; from here old text still
5070 * needs to be copied to the new text.
5071 * matchcol Column number of the old text where to look
5072 * for the next match. It's just after the
5073 * previous match or one further.
5074 * prev_matchcol Column just after the previous match (if any).
5075 * Mostly equal to matchcol, except for the first
5076 * match and after skipping an empty match.
5077 * regmatch.*pos Where the pattern matched in the old text.
5078 * new_start The new text, all that has been produced so
5079 * far.
5080 * new_end The new text, where to append new text.
5081 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005082 * lnum The line number where we found the start of
5083 * the match. Can be below the line we searched
5084 * when there is a \n before a \zs in the
5085 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 * sub_firstlnum The line number in the buffer where to look
5087 * for a match. Can be different from "lnum"
5088 * when the pattern or substitute string contains
5089 * line breaks.
5090 *
5091 * Special situations:
5092 * - When the substitute string contains a line break, the part up
5093 * to the line break is inserted in the text, but the copy of
5094 * the original line is kept. "sub_firstlnum" is adjusted for
5095 * the inserted lines.
5096 * - When the matched pattern contains a line break, the old line
5097 * is taken from the line at the end of the pattern. The lines
5098 * in the match are deleted later, "sub_firstlnum" is adjusted
5099 * accordingly.
5100 *
5101 * The new text is built up in new_start[]. It has some extra
5102 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005103 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 *
5105 * Make a copy of the old line, so it won't be taken away when
5106 * updating the screen or handling a multi-line match. The "old_"
5107 * pointers point into this copy.
5108 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005109 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 copycol = 0;
5111 matchcol = 0;
5112
5113 /* At first match, remember current cursor position. */
5114 if (!got_match)
5115 {
5116 setpcmark();
5117 got_match = TRUE;
5118 }
5119
5120 /*
5121 * Loop until nothing more to replace in this line.
5122 * 1. Handle match with empty string.
5123 * 2. If do_ask is set, ask for confirmation.
5124 * 3. substitute the string.
5125 * 4. if do_all is set, find next match
5126 * 5. break if there isn't another match in this line
5127 */
5128 for (;;)
5129 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005130 /* Advance "lnum" to the line where the match starts. The
5131 * match does not start in the first line when there is a line
5132 * break before \zs. */
5133 if (regmatch.startpos[0].lnum > 0)
5134 {
5135 lnum += regmatch.startpos[0].lnum;
5136 sub_firstlnum += regmatch.startpos[0].lnum;
5137 nmatch -= regmatch.startpos[0].lnum;
5138 vim_free(sub_firstline);
5139 sub_firstline = NULL;
5140 }
5141
5142 if (sub_firstline == NULL)
5143 {
5144 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5145 if (sub_firstline == NULL)
5146 {
5147 vim_free(new_start);
5148 goto outofmem;
5149 }
5150 }
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 /* Save the line number of the last change for the final
5153 * cursor position (just like Vi). */
5154 curwin->w_cursor.lnum = lnum;
5155 do_again = FALSE;
5156
5157 /*
5158 * 1. Match empty string does not count, except for first
5159 * match. This reproduces the strange vi behaviour.
5160 * This also catches endless loops.
5161 */
5162 if (matchcol == prev_matchcol
5163 && regmatch.endpos[0].lnum == 0
5164 && matchcol == regmatch.endpos[0].col)
5165 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00005166 if (sub_firstline[matchcol] == NUL)
5167 /* We already were at the end of the line. Don't look
5168 * for a match in this line again. */
5169 skip_match = TRUE;
5170 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02005171 {
5172 /* search for a match at next column */
5173#ifdef FEAT_MBYTE
5174 if (has_mbyte)
5175 matchcol += mb_ptr2len(sub_firstline + matchcol);
5176 else
5177#endif
5178 ++matchcol;
5179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 goto skip;
5181 }
5182
5183 /* Normally we continue searching for a match just after the
5184 * previous match. */
5185 matchcol = regmatch.endpos[0].col;
5186 prev_matchcol = matchcol;
5187
5188 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005189 * 2. If do_count is set only increase the counter.
5190 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005192 if (do_count)
5193 {
5194 /* For a multi-line match, put matchcol at the NUL at
5195 * the end of the line and set nmatch to one, so that
5196 * we continue looking for a match on the next line.
5197 * Avoids that ":s/\nB\@=//gc" get stuck. */
5198 if (nmatch > 1)
5199 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005200 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005201 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00005202 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005203 }
5204 sub_nsubs++;
5205 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005206#ifdef FEAT_EVAL
5207 /* Skip the substitution, unless an expression is used,
5208 * then it is evaluated in the sandbox. */
5209 if (!(sub[0] == '\\' && sub[1] == '='))
5210#endif
5211 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005212 }
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (do_ask)
5215 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00005216 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 /* change State to CONFIRM, so that the mouse works
5219 * properly */
5220 save_State = State;
5221 State = CONFIRM;
5222#ifdef FEAT_MOUSE
5223 setmouse(); /* disable mouse in xterm */
5224#endif
5225 curwin->w_cursor.col = regmatch.startpos[0].col;
5226
5227 /* When 'cpoptions' contains "u" don't sync undo when
5228 * asking for confirmation. */
5229 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5230 ++no_u_sync;
5231
5232 /*
5233 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
5234 */
5235 while (do_ask)
5236 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005237 if (exmode_active)
5238 {
5239 char_u *resp;
5240 colnr_T sc, ec;
5241
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005242 print_line_no_prefix(lnum, do_number, do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005243
5244 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
5245 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
5246 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02005247 if (do_number || curwin->w_p_nu)
5248 {
5249 int numw = number_width(curwin) + 1;
5250 sc += numw;
5251 ec += numw;
5252 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005253 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00005254 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005255 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00005256 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005257 msg_putchar('^');
5258
5259 resp = getexmodeline('?', NULL, 0);
5260 if (resp != NULL)
5261 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005262 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005263 vim_free(resp);
5264 }
5265 }
5266 else
5267 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005268 char_u *orig_line = NULL;
5269 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005271 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005273 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005275 /* Invert the matched string.
5276 * Remove the inversion afterwards. */
5277 temp = RedrawingDisabled;
5278 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005280 if (new_start != NULL)
5281 {
5282 /* There already was a substitution, we would
5283 * like to show this to the user. We cannot
5284 * really update the line, it would change
5285 * what matches. Temporarily replace the line
5286 * and change it back afterwards. */
5287 orig_line = vim_strsave(ml_get(lnum));
5288 if (orig_line != NULL)
5289 {
5290 char_u *new_line = concat_str(new_start,
5291 sub_firstline + copycol);
5292
5293 if (new_line == NULL)
5294 {
5295 vim_free(orig_line);
5296 orig_line = NULL;
5297 }
5298 else
5299 {
5300 /* Position the cursor relative to the
5301 * end of the line, the previous
5302 * substitute may have inserted or
5303 * deleted characters before the
5304 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01005305 len_change = (int)STRLEN(new_line)
5306 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005307 curwin->w_cursor.col += len_change;
5308 ml_replace(lnum, new_line, FALSE);
5309 }
5310 }
5311 }
5312
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005313 search_match_lines = regmatch.endpos[0].lnum
5314 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005315 search_match_endcol = regmatch.endpos[0].col
5316 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005317 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005319 update_topline();
5320 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00005321 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005322 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00005323 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
5325#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005326 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005328 if (msg_row == Rows - 1)
5329 msg_didout = FALSE; /* avoid a scroll-up */
5330 msg_starthere();
5331 i = msg_scroll;
5332 msg_scroll = 0; /* truncate msg when
5333 needed */
5334 msg_no_more = TRUE;
5335 /* write message same highlighting as for
5336 * wait_return */
5337 smsg_attr(hl_attr(HLF_R),
5338 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
5339 msg_no_more = FALSE;
5340 msg_scroll = i;
5341 showruler(TRUE);
5342 windgoto(msg_row, msg_col);
5343 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005346 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005348 ++no_mapping; /* don't map this key */
5349 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005350 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005351 --allow_keys;
5352 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005354 /* clear the question */
5355 msg_didout = FALSE; /* don't scroll up */
5356 msg_col = 0;
5357 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01005358
5359 /* restore the line */
5360 if (orig_line != NULL)
5361 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005362 }
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005365 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005367 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369 )
5370 {
5371 got_quit = TRUE;
5372 break;
5373 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005374 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005376 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005378 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 /* last: replace and then stop */
5381 do_all = FALSE;
5382 line2 = lnum;
5383 break;
5384 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005385 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
5387 do_ask = FALSE;
5388 break;
5389 }
5390#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005391 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005393 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 scrolldown_clamp();
5395#endif
5396 }
5397 State = save_State;
5398#ifdef FEAT_MOUSE
5399 setmouse();
5400#endif
5401 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5402 --no_u_sync;
5403
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005404 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {
5406 /* For a multi-line match, put matchcol at the NUL at
5407 * the end of the line and set nmatch to one, so that
5408 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005409 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5410 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (nmatch > 1)
5412 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005413 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005414 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 }
5416 goto skip;
5417 }
5418 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005419 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421
5422 /* Move the cursor to the start of the match, so that we can
5423 * use "\=col("."). */
5424 curwin->w_cursor.col = regmatch.startpos[0].col;
5425
5426 /*
5427 * 3. substitute the string.
5428 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005429#ifdef FEAT_EVAL
5430 if (do_count)
5431 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005432 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005433 save_ma = curbuf->b_p_ma;
5434 curbuf->b_p_ma = FALSE;
5435 sandbox++;
5436 }
5437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005439 sublen = vim_regsub_multi(&regmatch,
5440 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005442#ifdef FEAT_EVAL
5443 if (do_count)
5444 {
5445 curbuf->b_p_ma = save_ma;
5446 sandbox--;
5447 goto skip;
5448 }
5449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
Bram Moolenaar4463f292005-09-25 22:20:24 +00005451 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005452 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005453 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5454 {
5455 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5456 skip_match = TRUE;
5457 }
5458
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 /* Need room for:
5460 * - result so far in new_start (not for first sub in line)
5461 * - original text up to match
5462 * - length of substituted part
5463 * - original text after match
5464 */
5465 if (nmatch == 1)
5466 p1 = sub_firstline;
5467 else
5468 {
5469 p1 = ml_get(sub_firstlnum + nmatch - 1);
5470 nmatch_tl += nmatch - 1;
5471 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005472 copy_len = regmatch.startpos[0].col - copycol;
5473 needed_len = copy_len + ((unsigned)STRLEN(p1)
5474 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (new_start == NULL)
5476 {
5477 /*
5478 * Get some space for a temporary buffer to do the
5479 * substitution into (and some extra space to avoid
5480 * too many calls to alloc()/free()).
5481 */
5482 new_start_len = needed_len + 50;
5483 if ((new_start = alloc_check(new_start_len)) == NULL)
5484 goto outofmem;
5485 *new_start = NUL;
5486 new_end = new_start;
5487 }
5488 else
5489 {
5490 /*
5491 * Check if the temporary buffer is long enough to do the
5492 * substitution into. If not, make it larger (with a bit
5493 * extra to avoid too many calls to alloc()/free()).
5494 */
5495 len = (unsigned)STRLEN(new_start);
5496 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005497 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
5499 new_start_len = needed_len + 50;
5500 if ((p1 = alloc_check(new_start_len)) == NULL)
5501 {
5502 vim_free(new_start);
5503 goto outofmem;
5504 }
5505 mch_memmove(p1, new_start, (size_t)(len + 1));
5506 vim_free(new_start);
5507 new_start = p1;
5508 }
5509 new_end = new_start + len;
5510 }
5511
5512 /*
5513 * copy the text up to the part that matched
5514 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005515 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5516 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005518 (void)vim_regsub_multi(&regmatch,
5519 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 sub, new_end, TRUE, p_magic, TRUE);
5521 sub_nsubs++;
5522 did_sub = TRUE;
5523
5524 /* Move the cursor to the start of the line, to avoid that it
5525 * is beyond the end of the line after the substitution. */
5526 curwin->w_cursor.col = 0;
5527
5528 /* For a multi-line match, make a copy of the last matched
5529 * line and continue in that one. */
5530 if (nmatch > 1)
5531 {
5532 sub_firstlnum += nmatch - 1;
5533 vim_free(sub_firstline);
5534 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5535 /* When going beyond the last line, stop substituting. */
5536 if (sub_firstlnum <= line2)
5537 do_again = TRUE;
5538 else
5539 do_all = FALSE;
5540 }
5541
5542 /* Remember next character to be copied. */
5543 copycol = regmatch.endpos[0].col;
5544
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005545 if (skip_match)
5546 {
5547 /* Already hit end of the buffer, sub_firstlnum is one
5548 * less than what it ought to be. */
5549 vim_free(sub_firstline);
5550 sub_firstline = vim_strsave((char_u *)"");
5551 copycol = 0;
5552 }
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 /*
5555 * Now the trick is to replace CTRL-M chars with a real line
5556 * break. This would make it impossible to insert a CTRL-M in
5557 * the text. The line break can be avoided by preceding the
5558 * CTRL-M with a backslash. To be able to insert a backslash,
5559 * they must be doubled in the string and are halved here.
5560 * That is Vi compatible.
5561 */
5562 for (p1 = new_end; *p1; ++p1)
5563 {
5564 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005565 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 else if (*p1 == CAR)
5567 {
5568 if (u_inssub(lnum) == OK) /* prepare for undo */
5569 {
5570 *p1 = NUL; /* truncate up to the CR */
5571 ml_append(lnum - 1, new_start,
5572 (colnr_T)(p1 - new_start + 1), FALSE);
5573 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
5574 if (do_ask)
5575 appended_lines(lnum - 1, 1L);
5576 else
5577 {
5578 if (first_line == 0)
5579 first_line = lnum;
5580 last_line = lnum + 1;
5581 }
5582 /* All line numbers increase. */
5583 ++sub_firstlnum;
5584 ++lnum;
5585 ++line2;
5586 /* move the cursor to the new line, like Vi */
5587 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005588 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005589 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 p1 = new_start - 1;
5591 }
5592 }
5593#ifdef FEAT_MBYTE
5594 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005595 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#endif
5597 }
5598
5599 /*
5600 * 4. If do_all is set, find next match.
5601 * Prevent endless loop with patterns that match empty
5602 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5603 * But ":s/\n/#/" is OK.
5604 */
5605skip:
5606 /* We already know that we did the last subst when we are at
5607 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005608 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5609 * "line2" when there is a \zs in the pattern after a line
5610 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005611 lastone = (skip_match
5612 || got_int
5613 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005614 || lnum > line2
Bram Moolenaar8299df92004-07-10 09:47:34 +00005615 || !(do_all || do_again)
5616 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5617 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 nmatch = -1;
5619
5620 /*
5621 * Replace the line in the buffer when needed. This is
5622 * skipped when there are more matches.
5623 * The check for nmatch_tl is needed for when multi-line
5624 * matching must replace the lines before trying to do another
5625 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005626 * When the match starts below where we start searching also
5627 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 */
5629 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 || nmatch_tl > 0
5631 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005632 curbuf, sub_firstlnum,
5633 matchcol, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005634 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 if (new_start != NULL)
5637 {
5638 /*
5639 * Copy the rest of the line, that didn't match.
5640 * "matchcol" has to be adjusted, we use the end of
5641 * the line as reference, because the substitute may
5642 * have changed the number of characters. Same for
5643 * "prev_matchcol".
5644 */
5645 STRCAT(new_start, sub_firstline + copycol);
5646 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5647 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5648 - prev_matchcol;
5649
5650 if (u_savesub(lnum) != OK)
5651 break;
5652 ml_replace(lnum, new_start, TRUE);
5653
5654 if (nmatch_tl > 0)
5655 {
5656 /*
5657 * Matched lines have now been substituted and are
5658 * useless, delete them. The part after the match
5659 * has been appended to new_start, we don't need
5660 * it in the buffer.
5661 */
5662 ++lnum;
5663 if (u_savedel(lnum, nmatch_tl) != OK)
5664 break;
5665 for (i = 0; i < nmatch_tl; ++i)
5666 ml_delete(lnum, (int)FALSE);
5667 mark_adjust(lnum, lnum + nmatch_tl - 1,
5668 (long)MAXLNUM, -nmatch_tl);
5669 if (do_ask)
5670 deleted_lines(lnum, nmatch_tl);
5671 --lnum;
5672 line2 -= nmatch_tl; /* nr of lines decreases */
5673 nmatch_tl = 0;
5674 }
5675
5676 /* When asking, undo is saved each time, must also set
5677 * changed flag each time. */
5678 if (do_ask)
5679 changed_bytes(lnum, 0);
5680 else
5681 {
5682 if (first_line == 0)
5683 first_line = lnum;
5684 last_line = lnum + 1;
5685 }
5686
5687 sub_firstlnum = lnum;
5688 vim_free(sub_firstline); /* free the temp buffer */
5689 sub_firstline = new_start;
5690 new_start = NULL;
5691 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5692 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5693 - prev_matchcol;
5694 copycol = 0;
5695 }
5696 if (nmatch == -1 && !lastone)
5697 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005698 sub_firstlnum, matchcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 /*
5701 * 5. break if there isn't another match in this line
5702 */
5703 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005704 {
5705 /* If the match found didn't start where we were
5706 * searching, do the next search in the line where we
5707 * found the match. */
5708 if (nmatch == -1)
5709 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713
5714 line_breakcheck();
5715 }
5716
5717 if (did_sub)
5718 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005719 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 vim_free(sub_firstline); /* free the copy of the original line */
5721 sub_firstline = NULL;
5722 }
5723
5724 line_breakcheck();
5725 }
5726
5727 if (first_line != 0)
5728 {
5729 /* Need to subtract the number of added lines from "last_line" to get
5730 * the line number before the change (same as adding the number of
5731 * deleted lines). */
5732 i = curbuf->b_ml.ml_line_count - old_line_count;
5733 changed_lines(first_line, 0, last_line - i, i);
5734 }
5735
5736outofmem:
5737 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005738
5739 /* ":s/pat//n" doesn't move the cursor */
5740 if (do_count)
5741 curwin->w_cursor = old_cursor;
5742
Bram Moolenaar46475522010-03-23 17:36:29 +01005743 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
5745 /* Set the '[ and '] marks. */
5746 curbuf->b_op_start.lnum = eap->line1;
5747 curbuf->b_op_end.lnum = line2;
5748 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5749
5750 if (!global_busy)
5751 {
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005752 if (!do_ask) /* when interactive leave cursor on the match */
5753 {
5754 if (endcolumn)
5755 coladvance((colnr_T)MAXCOL);
5756 else
5757 beginline(BL_WHITE | BL_FIX);
5758 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005759 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 MSG("");
5761 }
5762 else
5763 global_need_beginline = TRUE;
5764 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005765 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else if (!global_busy)
5768 {
5769 if (got_int) /* interrupted */
5770 EMSG(_(e_interr));
5771 else if (got_match) /* did find something but nothing substituted */
5772 MSG("");
5773 else if (do_error) /* nothing found */
5774 EMSG2(_(e_patnotf2), get_search_pat());
5775 }
5776
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005777#ifdef FEAT_FOLDING
5778 if (do_ask && hasAnyFolding(curwin))
5779 /* Cursor position may require updating */
5780 changed_window_setting();
5781#endif
5782
Bram Moolenaar473de612013-06-08 18:19:48 +02005783 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005784
5785 /* Restore the flag values, they can be used for ":&&". */
5786 do_all = save_do_all;
5787 do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788}
5789
5790/*
5791 * Give message for number of substitutions.
5792 * Can also be used after a ":global" command.
5793 * Return TRUE if a message was given.
5794 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005795 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005796do_sub_msg(
5797 int count_only) /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 /*
5800 * Only report substitutions when:
5801 * - more than 'report' substitutions
5802 * - command was typed by user, or number of changed lines > 'report'
5803 * - giving messages is not disabled by 'lazyredraw'
5804 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005805 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5806 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 && messaging())
5808 {
5809 if (got_int)
5810 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005811 else
5812 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005814 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005815 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005817 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005818 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 sub_nsubs);
5820 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005821 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005822 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005824 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005825 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005828 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return TRUE;
5830 }
5831 if (got_int)
5832 {
5833 EMSG(_(e_interr));
5834 return TRUE;
5835 }
5836 return FALSE;
5837}
5838
5839/*
5840 * Execute a global command of the form:
5841 *
5842 * g/pattern/X : execute X on all lines where pattern matches
5843 * v/pattern/X : execute X on all lines where pattern does not match
5844 *
5845 * where 'X' is an EX command
5846 *
5847 * The command character (as well as the trailing slash) is optional, and
5848 * is assumed to be 'p' if missing.
5849 *
5850 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005851 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 * for each line that has a mark. This is required because after deleting
5853 * lines we do not know where to search for the next match.
5854 */
5855 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005856ex_global(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005859 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 int type; /* first char of cmd: 'v' or 'g' */
5861 char_u *cmd; /* command argument */
5862
5863 char_u delim; /* delimiter, normally '/' */
5864 char_u *pat;
5865 regmmatch_T regmatch;
5866 int match;
5867 int which_pat;
5868
5869 if (global_busy)
5870 {
5871 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5872 return;
5873 }
5874
5875 if (eap->forceit) /* ":global!" is like ":vglobal" */
5876 type = 'v';
5877 else
5878 type = *eap->cmd;
5879 cmd = eap->arg;
5880 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
5882 /*
5883 * undocumented vi feature:
5884 * "\/" and "\?": use previous search pattern.
5885 * "\&": use previous substitute pattern.
5886 */
5887 if (*cmd == '\\')
5888 {
5889 ++cmd;
5890 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5891 {
5892 EMSG(_(e_backslash));
5893 return;
5894 }
5895 if (*cmd == '&')
5896 which_pat = RE_SUBST; /* use previous substitute pattern */
5897 else
5898 which_pat = RE_SEARCH; /* use previous search pattern */
5899 ++cmd;
5900 pat = (char_u *)"";
5901 }
5902 else if (*cmd == NUL)
5903 {
5904 EMSG(_("E148: Regular expression missing from global"));
5905 return;
5906 }
5907 else
5908 {
5909 delim = *cmd; /* get the delimiter */
5910 if (delim)
5911 ++cmd; /* skip delimiter if there is one */
5912 pat = cmd; /* remember start of pattern */
5913 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5914 if (cmd[0] == delim) /* end delimiter found */
5915 *cmd++ = NUL; /* replace it with a NUL */
5916 }
5917
5918#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5919 if (p_altkeymap && curwin->w_p_rl)
5920 lrFswap(pat,0);
5921#endif
5922
5923 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5924 {
5925 EMSG(_(e_invcmd));
5926 return;
5927 }
5928
5929 /*
5930 * pass 1: set marks for each (not) matching line
5931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5933 {
5934 /* a match on this line? */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005935 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5936 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 if ((type == 'g' && match) || (type == 'v' && !match))
5938 {
5939 ml_setmarked(lnum);
5940 ndone++;
5941 }
5942 line_breakcheck();
5943 }
5944
5945 /*
5946 * pass 2: execute the command for each line that has been marked
5947 */
5948 if (got_int)
5949 MSG(_(e_interr));
5950 else if (ndone == 0)
5951 {
5952 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005953 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 else
Bram Moolenaarc389fd32013-03-07 16:08:35 +01005955 smsg((char_u *)_("Pattern not found: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 }
5957 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005958 {
5959#ifdef FEAT_CLIPBOARD
5960 start_global_changes();
5961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005963#ifdef FEAT_CLIPBOARD
5964 end_global_changes();
5965#endif
5966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
5968 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar473de612013-06-08 18:19:48 +02005969 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970}
5971
5972/*
5973 * Execute "cmd" on lines marked with ml_setmarked().
5974 */
5975 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005976global_exe(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
Bram Moolenaarbb993222011-05-10 16:00:47 +02005978 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5979 buf_T *old_buf = curbuf; /* remember what buffer we started in */
5980 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
5982 /*
5983 * Set current position only once for a global command.
5984 * If global_busy is set, setpcmark() will not do anything.
5985 * If there is an error, global_busy will be incremented.
5986 */
5987 setpcmark();
5988
5989 /* When the command writes a message, don't overwrite the command. */
5990 msg_didout = TRUE;
5991
Bram Moolenaard25bc232010-03-23 17:49:24 +01005992 sub_nsubs = 0;
5993 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 global_need_beginline = FALSE;
5995 global_busy = 1;
5996 old_lcount = curbuf->b_ml.ml_line_count;
5997 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5998 {
5999 curwin->w_cursor.lnum = lnum;
6000 curwin->w_cursor.col = 0;
6001 if (*cmd == NUL || *cmd == '\n')
6002 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
6003 else
6004 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
6005 ui_breakcheck();
6006 }
6007
6008 global_busy = 0;
6009 if (global_need_beginline)
6010 beginline(BL_WHITE | BL_FIX);
6011 else
6012 check_cursor(); /* cursor may be beyond the end of the line */
6013
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006014 /* the cursor may not have moved in the text but a change in a previous
6015 * line may move it on the screen */
6016 changed_line_abv_curs();
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 /* If it looks like no message was written, allow overwriting the
6019 * command with the report for number of changes. */
6020 if (msg_col == 0 && msg_scrolled == 0)
6021 msg_didout = FALSE;
6022
Bram Moolenaar45667512007-05-10 19:24:43 +00006023 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02006024 * number of extra or deleted lines.
6025 * Don't report extra or deleted lines in the edge case where the buffer
6026 * we are in after execution is different from the buffer we started in. */
6027 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
6029}
6030
6031#ifdef FEAT_VIMINFO
6032 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006033read_viminfo_sub_string(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01006035 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 vim_free(old_sub);
6037 if (force || old_sub == NULL)
6038 old_sub = viminfo_readstring(virp, 1, TRUE);
6039 return viminfo_readline(virp);
6040}
6041
6042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006043write_viminfo_sub_string(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
6046 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02006047 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 viminfo_writestring(fp, old_sub);
6049 }
6050}
6051#endif /* FEAT_VIMINFO */
6052
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006053#if defined(EXITFREE) || defined(PROTO)
6054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006055free_old_sub(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006056{
6057 vim_free(old_sub);
6058}
6059#endif
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
6062/*
6063 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006064 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006067prepare_tagpreview(
6068 int undo_sync) /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
6070 win_T *wp;
6071
6072# ifdef FEAT_GUI
6073 need_mouse_correct = TRUE;
6074# endif
6075
6076 /*
6077 * If there is already a preview window open, use that one.
6078 */
6079 if (!curwin->w_p_pvw)
6080 {
6081 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6082 if (wp->w_p_pvw)
6083 break;
6084 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00006085 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 else
6087 {
6088 /*
6089 * There is no preview window open yet. Create one.
6090 */
6091 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
6092 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006093 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 curwin->w_p_pvw = TRUE;
6095 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006096 RESET_BINDING(curwin); /* don't take over 'scrollbind'
6097 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098# ifdef FEAT_DIFF
6099 curwin->w_p_diff = FALSE; /* no 'diff' */
6100# endif
6101# ifdef FEAT_FOLDING
6102 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
6103# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006104 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00006107 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108}
6109
6110#endif
6111
6112
6113/*
6114 * ":help": open a read-only window on a help file
6115 */
6116 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006117ex_help(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 char_u *arg;
6120 char_u *tag;
6121 FILE *helpfd; /* file descriptor of help file */
6122 int n;
6123 int i;
6124#ifdef FEAT_WINDOWS
6125 win_T *wp;
6126#endif
6127 int num_matches;
6128 char_u **matches;
6129 char_u *p;
6130 int empty_fnum = 0;
6131 int alt_fnum = 0;
6132 buf_T *buf;
6133#ifdef FEAT_MULTI_LANG
6134 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006135 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02006137#ifdef FEAT_FOLDING
6138 int old_KeyTyped = KeyTyped;
6139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 if (eap != NULL)
6142 {
6143 /*
6144 * A ":help" command ends at the first LF, or at a '|' that is
6145 * followed by some text. Set nextcmd to the following command.
6146 */
6147 for (arg = eap->arg; *arg; ++arg)
6148 {
6149 if (*arg == '\n' || *arg == '\r'
6150 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
6151 {
6152 *arg++ = NUL;
6153 eap->nextcmd = arg;
6154 break;
6155 }
6156 }
6157 arg = eap->arg;
6158
Bram Moolenaar3dbde622012-04-05 16:05:05 +02006159 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
6161 EMSG(_("E478: Don't panic!"));
6162 return;
6163 }
6164
6165 if (eap->skip) /* not executing commands */
6166 return;
6167 }
6168 else
6169 arg = (char_u *)"";
6170
6171 /* remove trailing blanks */
6172 p = arg + STRLEN(arg) - 1;
6173 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
6174 *p-- = NUL;
6175
6176#ifdef FEAT_MULTI_LANG
6177 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006178 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179#endif
6180
6181 /* When no argument given go to the index. */
6182 if (*arg == NUL)
6183 arg = (char_u *)"help.txt";
6184
6185 /*
6186 * Check if there is a match for the argument.
6187 */
6188 n = find_help_tags(arg, &num_matches, &matches,
6189 eap != NULL && eap->forceit);
6190
6191 i = 0;
6192#ifdef FEAT_MULTI_LANG
6193 if (n != FAIL && lang != NULL)
6194 /* Find first item with the requested language. */
6195 for (i = 0; i < num_matches; ++i)
6196 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006197 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (len > 3 && matches[i][len - 3] == '@'
6199 && STRICMP(matches[i] + len - 2, lang) == 0)
6200 break;
6201 }
6202#endif
6203 if (i >= num_matches || n == FAIL)
6204 {
6205#ifdef FEAT_MULTI_LANG
6206 if (lang != NULL)
6207 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
6208 else
6209#endif
6210 EMSG2(_("E149: Sorry, no help for %s"), arg);
6211 if (n != FAIL)
6212 FreeWild(num_matches, matches);
6213 return;
6214 }
6215
6216 /* The first match (in the requested language) is the best match. */
6217 tag = vim_strsave(matches[i]);
6218 FreeWild(num_matches, matches);
6219
6220#ifdef FEAT_GUI
6221 need_mouse_correct = TRUE;
6222#endif
6223
6224 /*
6225 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006226 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006228 if (!curwin->w_buffer->b_help
6229#ifdef FEAT_WINDOWS
6230 || cmdmod.tab != 0
6231#endif
6232 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006235 if (cmdmod.tab != 0)
6236 wp = NULL;
6237 else
6238 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6239 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
6240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
6242 win_enter(wp, TRUE);
6243 else
6244#endif
6245 {
6246 /*
6247 * There is no help window yet.
6248 * Try to open the file specified by the "helpfile" option.
6249 */
6250 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
6251 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006252 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 goto erret;
6254 }
6255 fclose(helpfd);
6256
6257#ifdef FEAT_WINDOWS
6258 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006259 * specified, the current window is vertically split and
6260 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 n = WSP_HELP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006263 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 n |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006266 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267#else
6268 /* use current window */
6269 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00006271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273#ifdef FEAT_WINDOWS
6274 if (curwin->w_height < p_hh)
6275 win_setheight((int)p_hh);
6276#endif
6277
6278 /*
6279 * Open help file (do_ecmd() will set b_help flag, readfile() will
6280 * set b_p_ro flag).
6281 * Set the alternate file to the previously edited file.
6282 */
6283 alt_fnum = curbuf->b_fnum;
6284 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006285 ECMD_HIDE + ECMD_SET_HELP,
6286#ifdef FEAT_WINDOWS
6287 NULL /* buffer is still open, don't store info */
6288#else
6289 curwin
6290#endif
6291 );
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006292 if (!cmdmod.keepalt)
6293 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 empty_fnum = curbuf->b_fnum;
6295 }
6296 }
6297
6298 if (!p_im)
6299 restart_edit = 0; /* don't want insert mode in help file */
6300
Bram Moolenaar8f535582011-09-30 17:30:31 +02006301#ifdef FEAT_FOLDING
6302 /* Restore KeyTyped, setting 'filetype=help' may reset it.
6303 * It is needed for do_tag top open folds under the cursor. */
6304 KeyTyped = old_KeyTyped;
6305#endif
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (tag != NULL)
6308 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
6309
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006310 /* Delete the empty buffer if we're not using it. Careful: autocommands
6311 * may have jumped to another window, check that the buffer is not in a
6312 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
6314 {
6315 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006316 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 wipe_buffer(buf, TRUE);
6318 }
6319
6320 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006321 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 curwin->w_alt_fnum = alt_fnum;
6323
6324erret:
6325 vim_free(tag);
6326}
6327
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006328/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006329 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006330 */
6331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332ex_helpclose(exarg_T *eap UNUSED)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006333{
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02006334#if defined(FEAT_WINDOWS)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006335 win_T *win;
6336
6337 FOR_ALL_WINDOWS(win)
6338 {
6339 if (win->w_buffer->b_help)
6340 {
6341 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02006342 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006343 }
6344 }
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02006345#endif
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02006346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006348#if defined(FEAT_MULTI_LANG) || defined(PROTO)
6349/*
6350 * In an argument search for a language specifiers in the form "@xx".
6351 * Changes the "@" to NUL if found, and returns a pointer to "xx".
6352 * Returns NULL if not found.
6353 */
6354 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006355check_help_lang(char_u *arg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006356{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006357 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006358
6359 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6360 && ASCII_ISALPHA(arg[len - 1]))
6361 {
6362 arg[len - 3] = NUL; /* remove the '@' */
6363 return arg + len - 2;
6364 }
6365 return NULL;
6366}
6367#endif
6368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369/*
6370 * Return a heuristic indicating how well the given string matches. The
6371 * smaller the number, the better the match. This is the order of priorities,
6372 * from best match to worst match:
6373 * - Match with least alpha-numeric characters is better.
6374 * - Match with least total characters is better.
6375 * - Match towards the start is better.
6376 * - Match starting with "+" is worse (feature instead of command)
6377 * Assumption is made that the matched_string passed has already been found to
6378 * match some string for which help is requested. webb.
6379 */
6380 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006381help_heuristic(
6382 char_u *matched_string,
6383 int offset, /* offset for match */
6384 int wrong_case) /* no matching case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 int num_letters;
6387 char_u *p;
6388
6389 num_letters = 0;
6390 for (p = matched_string; *p; p++)
6391 if (ASCII_ISALNUM(*p))
6392 num_letters++;
6393
6394 /*
6395 * Multiply the number of letters by 100 to give it a much bigger
6396 * weighting than the number of characters.
6397 * If there only is a match while ignoring case, add 5000.
6398 * If the match starts in the middle of a word, add 10000 to put it
6399 * somewhere in the last half.
6400 * If the match is more than 2 chars from the start, multiply by 200 to
6401 * put it after matches at the start.
6402 */
6403 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6404 && ASCII_ISALNUM(matched_string[offset - 1]))
6405 offset += 10000;
6406 else if (offset > 2)
6407 offset *= 200;
6408 if (wrong_case)
6409 offset += 5000;
6410 /* Features are less interesting than the subjects themselves, but "+"
6411 * alone is not a feature. */
6412 if (matched_string[0] == '+' && matched_string[1] != NUL)
6413 offset += 100;
6414 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6415}
6416
6417/*
6418 * Compare functions for qsort() below, that checks the help heuristics number
6419 * that has been put after the tagname by find_tags().
6420 */
6421 static int
6422#ifdef __BORLANDC__
6423_RTLENTRYF
6424#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006425help_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 char *p1;
6428 char *p2;
6429
6430 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6431 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6432 return strcmp(p1, p2);
6433}
6434
6435/*
6436 * Find all help tags matching "arg", sort them and return in matches[], with
6437 * the number of matches in num_matches.
6438 * The matches will be sorted with a "best" match algorithm.
6439 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6440 */
6441 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442find_help_tags(
6443 char_u *arg,
6444 int *num_matches,
6445 char_u ***matches,
6446 int keep_lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 char_u *s, *d;
6449 int i;
6450 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006451 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006452 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006453 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006454 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 "[count]", "[quotex]", "[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006456 "[pattern]", "\\|", "\\%$",
6457 "s/\\~", "s/\\U", "s/\\L",
6458 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006460 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006461 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006462 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006463 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 "\\[count]", "\\[quotex]", "\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006465 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006466 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006467 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 int flags;
6469
6470 d = IObuff; /* assume IObuff is long enough! */
6471
6472 /*
6473 * Recognize a few exceptions to the rule. Some strings that contain '*'
6474 * with "star". Otherwise '*' is recognized as a wildcard.
6475 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006476 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (STRCMP(arg, mtable[i]) == 0)
6478 {
6479 STRCPY(d, rtable[i]);
6480 break;
6481 }
6482
6483 if (i < 0) /* no match in table */
6484 {
6485 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6486 * Also replace "\%^" and "\%(", they match every tag too.
6487 * Also "\zs", "\z1", etc.
6488 * Also "\@<", "\@=", "\@<=", etc.
6489 * And also "\_$" and "\_^". */
6490 if (arg[0] == '\\'
6491 && ((arg[1] != NUL && arg[2] == NUL)
6492 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6493 && arg[2] != NUL)))
6494 {
6495 STRCPY(d, "/\\\\");
6496 STRCPY(d + 3, arg + 1);
6497 /* Check for "/\\_$", should be "/\\_\$" */
6498 if (d[3] == '_' && d[4] == '$')
6499 STRCPY(d + 4, "\\$");
6500 }
6501 else
6502 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006503 /* Replace:
6504 * "[:...:]" with "\[:...:]"
6505 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006506 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006507 */
6508 if ((arg[0] == '[' && (arg[1] == ':'
6509 || (arg[1] == '+' && arg[2] == '+')))
6510 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 *d++ = '\\';
6512
Bram Moolenaar00f9e0d2016-03-15 13:44:12 +01006513 /*
6514 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'.
6515 */
6516 if (*arg == '(' && arg[1] == '\'')
6517 arg++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 for (s = arg; *s; ++s)
6519 {
6520 /*
6521 * Replace "|" with "bar" and '"' with "quote" to match the name of
6522 * the tags for these commands.
6523 * Replace "*" with ".*" and "?" with "." to match command line
6524 * completion.
6525 * Insert a backslash before '~', '$' and '.' to avoid their
6526 * special meaning.
6527 */
6528 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6529 break;
6530 switch (*s)
6531 {
6532 case '|': STRCPY(d, "bar");
6533 d += 3;
6534 continue;
6535 case '"': STRCPY(d, "quote");
6536 d += 5;
6537 continue;
6538 case '*': *d++ = '.';
6539 break;
6540 case '?': *d++ = '.';
6541 continue;
6542 case '$':
6543 case '.':
6544 case '~': *d++ = '\\';
6545 break;
6546 }
6547
6548 /*
6549 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6550 * ":help i_^_CTRL-D" work.
6551 * Insert '-' before and after "CTRL-X" when applicable.
6552 */
6553 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6554 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6555 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006556 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6557 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 STRCPY(d, "CTRL-");
6559 d += 5;
6560 if (*s < ' ')
6561 {
6562#ifdef EBCDIC
6563 *d++ = CtrlChar(*s);
6564#else
6565 *d++ = *s + '@';
6566#endif
6567 if (d[-1] == '\\')
6568 *d++ = '\\'; /* double a backslash */
6569 }
6570 else
6571 *d++ = *++s;
6572 if (s[1] != NUL && s[1] != '_')
6573 *d++ = '_'; /* append a '_' */
6574 continue;
6575 }
6576 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6577 *d++ = '\\';
6578
6579 /*
6580 * Insert a backslash before a backslash after a slash, for search
6581 * pattern tags: "/\|" --> "/\\|".
6582 */
6583 else if (s[0] == '\\' && s[1] != '\\'
6584 && *arg == '/' && s == arg + 1)
6585 *d++ = '\\';
6586
6587 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6588 * "CTRL-\_CTRL-N" */
6589 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6590 {
6591 STRCPY(d, "CTRL-\\\\");
6592 d += 7;
6593 s += 6;
6594 }
6595
6596 *d++ = *s;
6597
6598 /*
Bram Moolenaar81edd172016-04-14 13:51:37 +02006599 * If tag contains "({" or "([", tag terminates at the "(".
6600 * This is for help on functions, e.g.: abs({expr}).
6601 */
6602 if (*s == '(' && (s[1] == '{' || s[1] =='['))
6603 break;
6604
6605 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 * If tag starts with ', toss everything after a second '. Fixes
6607 * CTRL-] on 'option'. (would include the trailing '.').
6608 */
6609 if (*s == '\'' && s > arg && *arg == '\'')
6610 break;
Bram Moolenaar28b942a2016-06-04 22:31:27 +02006611 /* Also '{' and '}'. */
6612 if (*s == '}' && s > arg && *arg == '{')
6613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006616
6617 if (*IObuff == '`')
6618 {
6619 if (d > IObuff + 2 && d[-1] == '`')
6620 {
6621 /* remove the backticks from `command` */
6622 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6623 d[-2] = NUL;
6624 }
6625 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6626 {
6627 /* remove the backticks and comma from `command`, */
6628 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6629 d[-3] = NUL;
6630 }
6631 else if (d > IObuff + 4 && d[-3] == '`'
6632 && d[-2] == '\\' && d[-1] == '.')
6633 {
6634 /* remove the backticks and dot from `command`\. */
6635 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6636 d[-4] = NUL;
6637 }
6638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640 }
6641
6642 *matches = (char_u **)"";
6643 *num_matches = 0;
6644 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6645 if (keep_lang)
6646 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006647 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 /* Sort the matches found on the heuristic number that is after the
6651 * tag name. */
6652 qsort((void *)*matches, (size_t)*num_matches,
6653 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006654 /* Delete more than TAG_MANY to reduce the size of the listing. */
6655 while (*num_matches > TAG_MANY)
6656 vim_free((*matches)[--*num_matches]);
6657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 return OK;
6659}
6660
6661/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006662 * Called when starting to edit a buffer for a help file.
6663 */
6664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006665prepare_help_buffer(void)
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006666{
6667 char_u *p;
6668
6669 curbuf->b_help = TRUE;
6670#ifdef FEAT_QUICKFIX
6671 set_string_option_direct((char_u *)"buftype", -1,
6672 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6673#endif
6674
6675 /*
6676 * Always set these options after jumping to a help tag, because the
6677 * user may have an autocommand that gets in the way.
6678 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6679 * latin1 word characters (for translated help files).
6680 * Only set it when needed, buf_init_chartab() is some work.
6681 */
6682 p =
6683#ifdef EBCDIC
6684 (char_u *)"65-255,^*,^|,^\"";
6685#else
6686 (char_u *)"!-~,^*,^|,^\",192-255";
6687#endif
6688 if (STRCMP(curbuf->b_p_isk, p) != 0)
6689 {
6690 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6691 check_buf_options(curbuf);
6692 (void)buf_init_chartab(curbuf, FALSE);
6693 }
6694
Bram Moolenaar0b105412014-11-30 13:34:23 +01006695#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006696 /* Don't use the global foldmethod.*/
6697 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6698 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006699#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006700
6701 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6702 curwin->w_p_list = FALSE; /* no list mode */
6703
6704 curbuf->b_p_ma = FALSE; /* not modifiable */
6705 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6706 curwin->w_p_nu = 0; /* no line numbers */
6707 curwin->w_p_rnu = 0; /* no relative line numbers */
6708 RESET_BINDING(curwin); /* no scroll or cursor binding */
6709#ifdef FEAT_ARABIC
6710 curwin->w_p_arab = FALSE; /* no arabic mode */
6711#endif
6712#ifdef FEAT_RIGHTLEFT
6713 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6714#endif
6715#ifdef FEAT_FOLDING
6716 curwin->w_p_fen = FALSE; /* No folding in the help window */
6717#endif
6718#ifdef FEAT_DIFF
6719 curwin->w_p_diff = FALSE; /* No 'diff' */
6720#endif
6721#ifdef FEAT_SPELL
6722 curwin->w_p_spell = FALSE; /* No spell checking */
6723#endif
6724
6725 set_buflisted(FALSE);
6726}
6727
6728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 * After reading a help file: May cleanup a help buffer when syntax
6730 * highlighting is not used.
6731 */
6732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733fix_help_buffer(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 linenr_T lnum;
6736 char_u *line;
6737 int in_example = FALSE;
6738 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006739 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 char_u *p;
6741 char_u *rt;
6742 int mustfree;
6743
6744 /* set filetype to "help". */
6745 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6746
6747#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006748 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749#endif
6750 {
6751 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6752 {
6753 line = ml_get_buf(curbuf, lnum, FALSE);
6754 len = (int)STRLEN(line);
6755 if (in_example && len > 0 && !vim_iswhite(line[0]))
6756 {
6757 /* End of example: non-white or '<' in first column. */
6758 if (line[0] == '<')
6759 {
6760 /* blank-out a '<' in the first column */
6761 line = ml_get_buf(curbuf, lnum, TRUE);
6762 line[0] = ' ';
6763 }
6764 in_example = FALSE;
6765 }
6766 if (!in_example && len > 0)
6767 {
6768 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6769 {
6770 /* blank-out a '>' in the last column (start of example) */
6771 line = ml_get_buf(curbuf, lnum, TRUE);
6772 line[len - 1] = ' ';
6773 in_example = TRUE;
6774 }
6775 else if (line[len - 1] == '~')
6776 {
6777 /* blank-out a '~' at the end of line (header marker) */
6778 line = ml_get_buf(curbuf, lnum, TRUE);
6779 line[len - 1] = ' ';
6780 }
6781 }
6782 }
6783 }
6784
6785 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006786 * In the "help.txt" and "help.abx" file, add the locally added help
6787 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006789 fname = gettail(curbuf->b_fname);
6790 if (fnamecmp(fname, "help.txt") == 0
6791#ifdef FEAT_MULTI_LANG
6792 || (fnamencmp(fname, "help.", 5) == 0
6793 && ASCII_ISALPHA(fname[5])
6794 && ASCII_ISALPHA(fname[6])
6795 && TOLOWER_ASC(fname[7]) == 'x'
6796 && fname[8] == NUL)
6797#endif
6798 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
6800 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6801 {
6802 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006803 if (strstr((char *)line, "*local-additions*") == NULL)
6804 continue;
6805
6806 /* Go through all directories in 'runtimepath', skipping
6807 * $VIMRUNTIME. */
6808 p = p_rtp;
6809 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006811 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6812 mustfree = FALSE;
6813 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
6814 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006816 int fcount;
6817 char_u **fnames;
6818 FILE *fd;
6819 char_u *s;
6820 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006822 vimconv_T vc;
6823 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824#endif
6825
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006826 /* Find all "doc/ *.txt" files in this directory. */
6827 add_pathsep(NameBuff);
6828#ifdef FEAT_MULTI_LANG
6829 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006831 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006833 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6834 &fnames, EW_FILE|EW_SILENT) == OK
6835 && fcount > 0)
6836 {
6837#ifdef FEAT_MULTI_LANG
6838 int i1;
6839 int i2;
6840 char_u *f1;
6841 char_u *f2;
6842 char_u *t1;
6843 char_u *e1;
6844 char_u *e2;
6845
6846 /* If foo.abx is found use it instead of foo.txt in
6847 * the same directory. */
6848 for (i1 = 0; i1 < fcount; ++i1)
6849 {
6850 for (i2 = 0; i2 < fcount; ++i2)
6851 {
6852 if (i1 == i2)
6853 continue;
6854 if (fnames[i1] == NULL || fnames[i2] == NULL)
6855 continue;
6856 f1 = fnames[i1];
6857 f2 = fnames[i2];
6858 t1 = gettail(f1);
6859 if (fnamencmp(f1, f2, t1 - f1) != 0)
6860 continue;
6861 e1 = vim_strrchr(t1, '.');
6862 e2 = vim_strrchr(gettail(f2), '.');
6863 if (e1 == NUL || e2 == NUL)
6864 continue;
6865 if (fnamecmp(e1, ".txt") != 0
6866 && fnamecmp(e1, fname + 4) != 0)
6867 {
6868 /* Not .txt and not .abx, remove it. */
6869 vim_free(fnames[i1]);
6870 fnames[i1] = NULL;
6871 continue;
6872 }
6873 if (fnamencmp(f1, f2, e1 - f1) != 0)
6874 continue;
6875 if (fnamecmp(e1, ".txt") == 0
6876 && fnamecmp(e2, fname + 4) == 0)
6877 {
6878 /* use .abx instead of .txt */
6879 vim_free(fnames[i1]);
6880 fnames[i1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006884#endif
6885 for (fi = 0; fi < fcount; ++fi)
6886 {
6887 if (fnames[fi] == NULL)
6888 continue;
6889 fd = mch_fopen((char *)fnames[fi], "r");
6890 if (fd != NULL)
6891 {
6892 vim_fgets(IObuff, IOSIZE, fd);
6893 if (IObuff[0] == '*'
6894 && (s = vim_strchr(IObuff + 1, '*'))
6895 != NULL)
6896 {
6897#ifdef FEAT_MBYTE
6898 int this_utf = MAYBE;
6899#endif
6900 /* Change tag definition to a
6901 * reference and remove <CR>/<NL>. */
6902 IObuff[0] = '|';
6903 *s = '|';
6904 while (*s != NUL)
6905 {
6906 if (*s == '\r' || *s == '\n')
6907 *s = NUL;
6908#ifdef FEAT_MBYTE
6909 /* The text is utf-8 when a byte
6910 * above 127 is found and no
6911 * illegal byte sequence is found.
6912 */
6913 if (*s >= 0x80 && this_utf != FALSE)
6914 {
6915 int l;
6916
6917 this_utf = TRUE;
6918 l = utf_ptr2len(s);
6919 if (l == 1)
6920 this_utf = FALSE;
6921 s += l - 1;
6922 }
6923#endif
6924 ++s;
6925 }
6926#ifdef FEAT_MBYTE
6927 /* The help file is latin1 or utf-8;
6928 * conversion to the current
6929 * 'encoding' may be required. */
6930 vc.vc_type = CONV_NONE;
6931 convert_setup(&vc, (char_u *)(
6932 this_utf == TRUE ? "utf-8"
6933 : "latin1"), p_enc);
6934 if (vc.vc_type == CONV_NONE)
6935 /* No conversion needed. */
6936 cp = IObuff;
6937 else
6938 {
6939 /* Do the conversion. If it fails
6940 * use the unconverted text. */
6941 cp = string_convert(&vc, IObuff,
6942 NULL);
6943 if (cp == NULL)
6944 cp = IObuff;
6945 }
6946 convert_setup(&vc, NULL, NULL);
6947
6948 ml_append(lnum, cp, (colnr_T)0, FALSE);
6949 if (cp != IObuff)
6950 vim_free(cp);
6951#else
6952 ml_append(lnum, IObuff, (colnr_T)0,
6953 FALSE);
6954#endif
6955 ++lnum;
6956 }
6957 fclose(fd);
6958 }
6959 }
6960 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006963 if (mustfree)
6964 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006966 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968 }
6969}
6970
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006971/*
6972 * ":exusage"
6973 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975ex_exusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006976{
6977 do_cmdline_cmd((char_u *)"help ex-cmd-index");
6978}
6979
6980/*
6981 * ":viusage"
6982 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006983 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006984ex_viusage(exarg_T *eap UNUSED)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006985{
6986 do_cmdline_cmd((char_u *)"help normal-index");
6987}
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989/*
Bram Moolenaar6bef5302016-03-12 21:28:26 +01006990 * Generate tags in one help directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006993helptags_one(
6994 char_u *dir, /* doc directory */
6995 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
6996 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
6997 int add_help_tags) /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 FILE *fd_tags;
7000 FILE *fd;
7001 garray_T ga;
7002 int filecount;
7003 char_u **files;
7004 char_u *p1, *p2;
7005 int fi;
7006 char_u *s;
7007 int i;
7008 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007009 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010# ifdef FEAT_MBYTE
7011 int utf8 = MAYBE;
7012 int this_utf8;
7013 int firstline;
7014 int mix = FALSE; /* detected mixed encodings */
7015# endif
7016
7017 /*
7018 * Find all *.txt files.
7019 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01007020 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007022 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 STRCAT(NameBuff, ext);
7024 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7025 EW_FILE|EW_SILENT) == FAIL
7026 || filecount == 0)
7027 {
7028 if (!got_int)
7029 EMSG2("E151: No match: %s", NameBuff);
7030 return;
7031 }
7032
7033 /*
7034 * Open the tags file for writing.
7035 * Do this before scanning through all the files.
7036 */
7037 STRCPY(NameBuff, dir);
7038 add_pathsep(NameBuff);
7039 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007040 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 if (fd_tags == NULL)
7042 {
7043 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
7044 FreeWild(filecount, files);
7045 return;
7046 }
7047
7048 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007049 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
7050 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 */
7052 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00007053 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
7054 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 if (ga_grow(&ga, 1) == FAIL)
7057 got_int = TRUE;
7058 else
7059 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007060 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 if (s == NULL)
7062 got_int = TRUE;
7063 else
7064 {
7065 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
7066 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7067 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
7069 }
7070 }
7071
7072 /*
7073 * Go over all the files and extract the tags.
7074 */
7075 for (fi = 0; fi < filecount && !got_int; ++fi)
7076 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007077 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (fd == NULL)
7079 {
7080 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
7081 continue;
7082 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01007083 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085# ifdef FEAT_MBYTE
7086 firstline = TRUE;
7087# endif
7088 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7089 {
7090# ifdef FEAT_MBYTE
7091 if (firstline)
7092 {
7093 /* Detect utf-8 file by a non-ASCII char in the first line. */
7094 this_utf8 = MAYBE;
7095 for (s = IObuff; *s != NUL; ++s)
7096 if (*s >= 0x80)
7097 {
7098 int l;
7099
7100 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007101 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (l == 1)
7103 {
7104 /* Illegal UTF-8 byte sequence. */
7105 this_utf8 = FALSE;
7106 break;
7107 }
7108 s += l - 1;
7109 }
7110 if (this_utf8 == MAYBE) /* only ASCII characters found */
7111 this_utf8 = FALSE;
7112 if (utf8 == MAYBE) /* first file */
7113 utf8 = this_utf8;
7114 else if (utf8 != this_utf8)
7115 {
7116 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
7117 mix = !got_int;
7118 got_int = TRUE;
7119 }
7120 firstline = FALSE;
7121 }
7122# endif
7123 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
7124 while (p1 != NULL)
7125 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02007126 /* Use vim_strbyte() instead of vim_strchr() so that when
7127 * 'encoding' is dbcs it still works, don't find '*' in the
7128 * second byte. */
7129 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
7131 {
7132 for (s = p1 + 1; s < p2; ++s)
7133 if (*s == ' ' || *s == '\t' || *s == '|')
7134 break;
7135
7136 /*
7137 * Only accept a *tag* when it consists of valid
7138 * characters, there is white space before it and is
7139 * followed by a white character or end-of-line.
7140 */
7141 if (s == p2
7142 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
7143 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
7144 || s[1] == '\0'))
7145 {
7146 *p2 = '\0';
7147 ++p1;
7148 if (ga_grow(&ga, 1) == FAIL)
7149 {
7150 got_int = TRUE;
7151 break;
7152 }
7153 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
7154 if (s == NULL)
7155 {
7156 got_int = TRUE;
7157 break;
7158 }
7159 ((char_u **)ga.ga_data)[ga.ga_len] = s;
7160 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 sprintf((char *)s, "%s\t%s", p1, fname);
7162
7163 /* find next '*' */
7164 p2 = vim_strchr(p2 + 1, '*');
7165 }
7166 }
7167 p1 = p2;
7168 }
7169 line_breakcheck();
7170 }
7171
7172 fclose(fd);
7173 }
7174
7175 FreeWild(filecount, files);
7176
7177 if (!got_int)
7178 {
7179 /*
7180 * Sort the tags.
7181 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02007182 if (ga.ga_data != NULL)
7183 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185 /*
7186 * Check for duplicates.
7187 */
7188 for (i = 1; i < ga.ga_len; ++i)
7189 {
7190 p1 = ((char_u **)ga.ga_data)[i - 1];
7191 p2 = ((char_u **)ga.ga_data)[i];
7192 while (*p1 == *p2)
7193 {
7194 if (*p2 == '\t')
7195 {
7196 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007197 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 _("E154: Duplicate tag \"%s\" in file %s/%s"),
7199 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
7200 EMSG(NameBuff);
7201 *p2 = '\t';
7202 break;
7203 }
7204 ++p1;
7205 ++p2;
7206 }
7207 }
7208
7209# ifdef FEAT_MBYTE
7210 if (utf8 == TRUE)
7211 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
7212# endif
7213
7214 /*
7215 * Write the tags into the file.
7216 */
7217 for (i = 0; i < ga.ga_len; ++i)
7218 {
7219 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00007220 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00007222 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 else
7224 {
7225 fprintf(fd_tags, "%s\t/*", s);
7226 for (p1 = s; *p1 != '\t'; ++p1)
7227 {
7228 /* insert backslash before '\\' and '/' */
7229 if (*p1 == '\\' || *p1 == '/')
7230 putc('\\', fd_tags);
7231 putc(*p1, fd_tags);
7232 }
7233 fprintf(fd_tags, "*\n");
7234 }
7235 }
7236 }
7237#ifdef FEAT_MBYTE
7238 if (mix)
7239 got_int = FALSE; /* continue with other languages */
7240#endif
7241
7242 for (i = 0; i < ga.ga_len; ++i)
7243 vim_free(((char_u **)ga.ga_data)[i]);
7244 ga_clear(&ga);
7245 fclose(fd_tags); /* there is no check for an error... */
7246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007248/*
7249 * Generate tags in one help directory, taking care of translations.
7250 */
7251 static void
7252do_helptags(char_u *dirname, int add_help_tags)
7253{
7254#ifdef FEAT_MULTI_LANG
7255 int len;
7256 int i, j;
7257 garray_T ga;
7258 char_u lang[2];
7259 char_u ext[5];
7260 char_u fname[8];
7261 int filecount;
7262 char_u **files;
7263
7264 /* Get a list of all files in the help directory and in subdirectories. */
7265 STRCPY(NameBuff, dirname);
7266 add_pathsep(NameBuff);
7267 STRCAT(NameBuff, "**");
7268 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
7269 EW_FILE|EW_SILENT) == FAIL
7270 || filecount == 0)
7271 {
7272 EMSG2("E151: No match: %s", NameBuff);
Bram Moolenaar6bef5302016-03-12 21:28:26 +01007273 return;
7274 }
7275
7276 /* Go over all files in the directory to find out what languages are
7277 * present. */
7278 ga_init2(&ga, 1, 10);
7279 for (i = 0; i < filecount; ++i)
7280 {
7281 len = (int)STRLEN(files[i]);
7282 if (len > 4)
7283 {
7284 if (STRICMP(files[i] + len - 4, ".txt") == 0)
7285 {
7286 /* ".txt" -> language "en" */
7287 lang[0] = 'e';
7288 lang[1] = 'n';
7289 }
7290 else if (files[i][len - 4] == '.'
7291 && ASCII_ISALPHA(files[i][len - 3])
7292 && ASCII_ISALPHA(files[i][len - 2])
7293 && TOLOWER_ASC(files[i][len - 1]) == 'x')
7294 {
7295 /* ".abx" -> language "ab" */
7296 lang[0] = TOLOWER_ASC(files[i][len - 3]);
7297 lang[1] = TOLOWER_ASC(files[i][len - 2]);
7298 }
7299 else
7300 continue;
7301
7302 /* Did we find this language already? */
7303 for (j = 0; j < ga.ga_len; j += 2)
7304 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
7305 break;
7306 if (j == ga.ga_len)
7307 {
7308 /* New language, add it. */
7309 if (ga_grow(&ga, 2) == FAIL)
7310 break;
7311 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
7312 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
7313 }
7314 }
7315 }
7316
7317 /*
7318 * Loop over the found languages to generate a tags file for each one.
7319 */
7320 for (j = 0; j < ga.ga_len; j += 2)
7321 {
7322 STRCPY(fname, "tags-xx");
7323 fname[5] = ((char_u *)ga.ga_data)[j];
7324 fname[6] = ((char_u *)ga.ga_data)[j + 1];
7325 if (fname[5] == 'e' && fname[6] == 'n')
7326 {
7327 /* English is an exception: use ".txt" and "tags". */
7328 fname[4] = NUL;
7329 STRCPY(ext, ".txt");
7330 }
7331 else
7332 {
7333 /* Language "ab" uses ".abx" and "tags-ab". */
7334 STRCPY(ext, ".xxx");
7335 ext[1] = fname[5];
7336 ext[2] = fname[6];
7337 }
7338 helptags_one(dirname, ext, fname, add_help_tags);
7339 }
7340
7341 ga_clear(&ga);
7342 FreeWild(filecount, files);
7343
7344#else
7345 /* No language support, just use "*.txt" and "tags". */
7346 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
7347#endif
7348}
7349
7350 static void
7351helptags_cb(char_u *fname, void *cookie)
7352{
7353 do_helptags(fname, *(int *)cookie);
7354}
7355
7356/*
7357 * ":helptags"
7358 */
7359 void
7360ex_helptags(exarg_T *eap)
7361{
7362 expand_T xpc;
7363 char_u *dirname;
7364 int add_help_tags = FALSE;
7365
7366 /* Check for ":helptags ++t {dir}". */
7367 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3]))
7368 {
7369 add_help_tags = TRUE;
7370 eap->arg = skipwhite(eap->arg + 3);
7371 }
7372
7373 if (STRCMP(eap->arg, "ALL") == 0)
7374 {
7375 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
7376 helptags_cb, &add_help_tags);
7377 }
7378 else
7379 {
7380 ExpandInit(&xpc);
7381 xpc.xp_context = EXPAND_DIRECTORIES;
7382 dirname = ExpandOne(&xpc, eap->arg, NULL,
7383 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
7384 if (dirname == NULL || !mch_isdir(dirname))
7385 EMSG2(_("E150: Not a directory: %s"), eap->arg);
7386 else
7387 do_helptags(dirname, add_help_tags);
7388 vim_free(dirname);
7389 }
7390}
7391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392#if defined(FEAT_SIGNS) || defined(PROTO)
7393
7394/*
7395 * Struct to hold the sign properties.
7396 */
7397typedef struct sign sign_T;
7398
7399struct sign
7400{
7401 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007402 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 char_u *sn_name; /* name of sign */
7404 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007405# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 char_u *sn_text; /* text used instead of pixmap */
7409 int sn_line_hl; /* highlight ID for line */
7410 int sn_text_hl; /* highlight ID for text */
7411};
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007414static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01007416static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
7417static void sign_list_defined(sign_T *sp);
7418static void sign_undefine(sign_T *sp, sign_T *sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007420static char *cmds[] = {
7421 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007422# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007423 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007424# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007425 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007426# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007427 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007428# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007429 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007430# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007431 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007432# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007433 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007434# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007435};
7436
7437/*
7438 * Find index of a ":sign" subcmd from its name.
7439 * "*end_cmd" must be writable.
7440 */
7441 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007442sign_cmd_idx(
7443 char_u *begin_cmd, /* begin of sign subcmd */
7444 char_u *end_cmd) /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007445{
7446 int idx;
7447 char save = *end_cmd;
7448
7449 *end_cmd = NUL;
7450 for (idx = 0; ; ++idx)
7451 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7452 break;
7453 *end_cmd = save;
7454 return idx;
7455}
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457/*
7458 * ":sign" command
7459 */
7460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007461ex_sign(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
7463 char_u *arg = eap->arg;
7464 char_u *p;
7465 int idx;
7466 sign_T *sp;
7467 sign_T *sp_prev;
7468 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
7470 /* Parse the subcommand. */
7471 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007472 idx = sign_cmd_idx(arg, p);
7473 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007475 EMSG2(_("E160: Unknown sign command: %s"), arg);
7476 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478 arg = skipwhite(p);
7479
7480 if (idx <= SIGNCMD_LIST)
7481 {
7482 /*
7483 * Define, undefine or list signs.
7484 */
7485 if (idx == SIGNCMD_LIST && *arg == NUL)
7486 {
7487 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007488 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 sign_list_defined(sp);
7490 }
7491 else if (*arg == NUL)
7492 EMSG(_("E156: Missing sign name"));
7493 else
7494 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007495 /* Isolate the sign name. If it's a number skip leading zeroes,
7496 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 p = skiptowhite(arg);
7498 if (*p != NUL)
7499 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007500 while (arg[0] == '0' && arg[1] != NUL)
7501 ++arg;
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 sp_prev = NULL;
7504 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7505 {
7506 if (STRCMP(sp->sn_name, arg) == 0)
7507 break;
7508 sp_prev = sp;
7509 }
7510 if (idx == SIGNCMD_DEFINE)
7511 {
7512 /* ":sign define {name} ...": define a sign */
7513 if (sp == NULL)
7514 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007515 sign_T *lp;
7516 int start = next_sign_typenr;
7517
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 /* Allocate a new sign. */
7519 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7520 if (sp == NULL)
7521 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007523 /* Check that next_sign_typenr is not already being used.
7524 * This only happens after wrapping around. Hopefully
7525 * another one got deleted and we can use its number. */
7526 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007528 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007530 ++next_sign_typenr;
7531 if (next_sign_typenr == MAX_TYPENR)
7532 next_sign_typenr = 1;
7533 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007535 vim_free(sp);
7536 EMSG(_("E612: Too many signs defined"));
7537 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007539 lp = first_sign; /* start all over */
7540 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007542 lp = lp->sn_next;
7543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007545 sp->sn_typenr = next_sign_typenr;
7546 if (++next_sign_typenr == MAX_TYPENR)
7547 next_sign_typenr = 1; /* wrap around */
7548
7549 sp->sn_name = vim_strsave(arg);
7550 if (sp->sn_name == NULL) /* out of memory */
7551 {
7552 vim_free(sp);
7553 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007555
7556 /* add the new sign to the list of signs */
7557 if (sp_prev == NULL)
7558 first_sign = sp;
7559 else
7560 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 }
7562
7563 /* set values for a defined sign. */
7564 for (;;)
7565 {
7566 arg = skipwhite(p);
7567 if (*arg == NUL)
7568 break;
7569 p = skiptowhite_esc(arg);
7570 if (STRNCMP(arg, "icon=", 5) == 0)
7571 {
7572 arg += 5;
7573 vim_free(sp->sn_icon);
7574 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7575 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007576# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 if (gui.in_use)
7578 {
7579 out_flush();
7580 if (sp->sn_image != NULL)
7581 gui_mch_destroy_sign(sp->sn_image);
7582 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7583 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 }
7586 else if (STRNCMP(arg, "text=", 5) == 0)
7587 {
7588 char_u *s;
7589 int cells;
7590 int len;
7591
7592 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007593# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 /* Count cells and check for non-printable chars */
7595 if (has_mbyte)
7596 {
7597 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007598 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
7600 if (!vim_isprintc((*mb_ptr2char)(s)))
7601 break;
7602 cells += (*mb_ptr2cells)(s);
7603 }
7604 }
7605 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
7608 for (s = arg; s < p; ++s)
7609 if (!vim_isprintc(*s))
7610 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007611 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 /* Currently must be one or two display cells */
7614 if (s != p || cells < 1 || cells > 2)
7615 {
7616 *p = NUL;
7617 EMSG2(_("E239: Invalid sign text: %s"), arg);
7618 return;
7619 }
7620
7621 vim_free(sp->sn_text);
7622 /* Allocate one byte more if we need to pad up
7623 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007624 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 sp->sn_text = vim_strnsave(arg, len);
7626
7627 if (sp->sn_text != NULL && cells == 1)
7628 STRCPY(sp->sn_text + len - 1, " ");
7629 }
7630 else if (STRNCMP(arg, "linehl=", 7) == 0)
7631 {
7632 arg += 7;
7633 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7634 }
7635 else if (STRNCMP(arg, "texthl=", 7) == 0)
7636 {
7637 arg += 7;
7638 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7639 }
7640 else
7641 {
7642 EMSG2(_(e_invarg2), arg);
7643 return;
7644 }
7645 }
7646 }
7647 else if (sp == NULL)
7648 EMSG2(_("E155: Unknown sign: %s"), arg);
7649 else if (idx == SIGNCMD_LIST)
7650 /* ":sign list {name}" */
7651 sign_list_defined(sp);
7652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007654 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
7656 }
7657 else
7658 {
7659 int id = -1;
7660 linenr_T lnum = -1;
7661 char_u *sign_name = NULL;
7662 char_u *arg1;
7663
7664 if (*arg == NUL)
7665 {
7666 if (idx == SIGNCMD_PLACE)
7667 {
7668 /* ":sign place": list placed signs in all buffers */
7669 sign_list_placed(NULL);
7670 }
7671 else if (idx == SIGNCMD_UNPLACE)
7672 {
7673 /* ":sign unplace": remove placed sign at cursor */
7674 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7675 if (id > 0)
7676 {
7677 buf_delsign(curwin->w_buffer, id);
7678 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7679 }
7680 else
7681 EMSG(_("E159: Missing sign number"));
7682 }
7683 else
7684 EMSG(_(e_argreq));
7685 return;
7686 }
7687
7688 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7689 {
7690 /* ":sign unplace *": remove all placed signs */
7691 buf_delete_all_signs();
7692 return;
7693 }
7694
7695 /* first arg could be placed sign id */
7696 arg1 = arg;
7697 if (VIM_ISDIGIT(*arg))
7698 {
7699 id = getdigits(&arg);
7700 if (!vim_iswhite(*arg) && *arg != NUL)
7701 {
7702 id = -1;
7703 arg = arg1;
7704 }
7705 else
7706 {
7707 arg = skipwhite(arg);
7708 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7709 {
7710 /* ":sign unplace {id}": remove placed sign by number */
7711 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7712 if ((lnum = buf_delsign(buf, id)) != 0)
7713 update_debug_sign(buf, lnum);
7714 return;
7715 }
7716 }
7717 }
7718
7719 /*
7720 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7721 * Leave "arg" pointing to {fname}.
7722 */
7723 for (;;)
7724 {
7725 if (STRNCMP(arg, "line=", 5) == 0)
7726 {
7727 arg += 5;
7728 lnum = atoi((char *)arg);
7729 arg = skiptowhite(arg);
7730 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007731 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7732 {
7733 if (id != -1)
7734 {
7735 EMSG(_(e_invarg));
7736 return;
7737 }
7738 id = -2;
7739 arg = skiptowhite(arg + 1);
7740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 else if (STRNCMP(arg, "name=", 5) == 0)
7742 {
7743 arg += 5;
7744 sign_name = arg;
7745 arg = skiptowhite(arg);
7746 if (*arg != NUL)
7747 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007748 while (sign_name[0] == '0' && sign_name[1] != NUL)
7749 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 }
7751 else if (STRNCMP(arg, "file=", 5) == 0)
7752 {
7753 arg += 5;
7754 buf = buflist_findname(arg);
7755 break;
7756 }
7757 else if (STRNCMP(arg, "buffer=", 7) == 0)
7758 {
7759 arg += 7;
7760 buf = buflist_findnr((int)getdigits(&arg));
7761 if (*skipwhite(arg) != NUL)
7762 EMSG(_(e_trailing));
7763 break;
7764 }
7765 else
7766 {
7767 EMSG(_(e_invarg));
7768 return;
7769 }
7770 arg = skipwhite(arg);
7771 }
7772
7773 if (buf == NULL)
7774 {
7775 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7776 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007777 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
7779 if (lnum >= 0 || sign_name != NULL)
7780 EMSG(_(e_invarg));
7781 else
7782 /* ":sign place file={fname}": list placed signs in one file */
7783 sign_list_placed(buf);
7784 }
7785 else if (idx == SIGNCMD_JUMP)
7786 {
7787 /* ":sign jump {id} file={fname}" */
7788 if (lnum >= 0 || sign_name != NULL)
7789 EMSG(_(e_invarg));
7790 else if ((lnum = buf_findsign(buf, id)) > 0)
7791 { /* goto a sign ... */
7792 if (buf_jump_open_win(buf) != NULL)
7793 { /* ... in a current window */
7794 curwin->w_cursor.lnum = lnum;
7795 check_cursor_lnum();
7796 beginline(BL_WHITE);
7797 }
7798 else
7799 { /* ... not currently in a window */
7800 char_u *cmd;
7801
7802 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7803 if (cmd == NULL)
7804 return;
7805 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7806 do_cmdline_cmd(cmd);
7807 vim_free(cmd);
7808 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007809# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 }
7813 else
7814 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7815 }
7816 else if (idx == SIGNCMD_UNPLACE)
7817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 if (lnum >= 0 || sign_name != NULL)
7819 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007820 else if (id == -2)
7821 {
7822 /* ":sign unplace * file={fname}" */
7823 redraw_buf_later(buf, NOT_VALID);
7824 buf_delete_signs(buf);
7825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 else
7827 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007828 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 lnum = buf_delsign(buf, id);
7830 update_debug_sign(buf, lnum);
7831 }
7832 }
7833 /* idx == SIGNCMD_PLACE */
7834 else if (sign_name != NULL)
7835 {
7836 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7837 if (STRCMP(sp->sn_name, sign_name) == 0)
7838 break;
7839 if (sp == NULL)
7840 {
7841 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7842 return;
7843 }
7844 if (lnum > 0)
7845 /* ":sign place {id} line={lnum} name={name} file={fname}":
7846 * place a sign */
7847 buf_addsign(buf, id, lnum, sp->sn_typenr);
7848 else
7849 /* ":sign place {id} file={fname}": change sign type */
7850 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007851 if (lnum > 0)
7852 update_debug_sign(buf, lnum);
7853 else
7854 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 }
7856 else
7857 EMSG(_(e_invarg));
7858 }
7859}
7860
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007861# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862/*
7863 * Allocate the icons. Called when the GUI has started. Allows defining
7864 * signs before it starts.
7865 */
7866 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867sign_gui_started(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 sign_T *sp;
7870
7871 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7872 if (sp->sn_icon != NULL)
7873 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7874}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
7877/*
7878 * List one sign.
7879 */
7880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007881sign_list_defined(sign_T *sp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 char_u *p;
7884
Bram Moolenaar555b2802005-05-19 21:08:39 +00007885 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (sp->sn_icon != NULL)
7887 {
7888 MSG_PUTS(" icon=");
7889 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007890# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 if (sp->sn_image == NULL)
7892 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007893# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897 if (sp->sn_text != NULL)
7898 {
7899 MSG_PUTS(" text=");
7900 msg_outtrans(sp->sn_text);
7901 }
7902 if (sp->sn_line_hl > 0)
7903 {
7904 MSG_PUTS(" linehl=");
7905 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
7906 if (p == NULL)
7907 MSG_PUTS("NONE");
7908 else
7909 msg_puts(p);
7910 }
7911 if (sp->sn_text_hl > 0)
7912 {
7913 MSG_PUTS(" texthl=");
7914 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
7915 if (p == NULL)
7916 MSG_PUTS("NONE");
7917 else
7918 msg_puts(p);
7919 }
7920}
7921
7922/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007923 * Undefine a sign and free its memory.
7924 */
7925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007926sign_undefine(sign_T *sp, sign_T *sp_prev)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007927{
7928 vim_free(sp->sn_name);
7929 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007930# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007931 if (sp->sn_image != NULL)
7932 {
7933 out_flush();
7934 gui_mch_destroy_sign(sp->sn_image);
7935 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007936# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007937 vim_free(sp->sn_text);
7938 if (sp_prev == NULL)
7939 first_sign = sp->sn_next;
7940 else
7941 sp_prev->sn_next = sp->sn_next;
7942 vim_free(sp);
7943}
7944
7945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 * Get highlighting attribute for sign "typenr".
7947 * If "line" is TRUE: line highl, if FALSE: text highl.
7948 */
7949 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950sign_get_attr(int typenr, int line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 sign_T *sp;
7953
7954 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7955 if (sp->sn_typenr == typenr)
7956 {
7957 if (line)
7958 {
7959 if (sp->sn_line_hl > 0)
7960 return syn_id2attr(sp->sn_line_hl);
7961 }
7962 else
7963 {
7964 if (sp->sn_text_hl > 0)
7965 return syn_id2attr(sp->sn_text_hl);
7966 }
7967 break;
7968 }
7969 return 0;
7970}
7971
7972/*
7973 * Get text mark for sign "typenr".
7974 * Returns NULL if there isn't one.
7975 */
7976 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007977sign_get_text(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 sign_T *sp;
7980
7981 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7982 if (sp->sn_typenr == typenr)
7983 return sp->sn_text;
7984 return NULL;
7985}
7986
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007987# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007989sign_get_image(
7990 int typenr) /* the attribute which may have a sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 sign_T *sp;
7993
7994 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7995 if (sp->sn_typenr == typenr)
7996 return sp->sn_image;
7997 return NULL;
7998}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
8001/*
8002 * Get the name of a sign by its typenr.
8003 */
8004 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008005sign_typenr2name(int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007 sign_T *sp;
8008
8009 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8010 if (sp->sn_typenr == typenr)
8011 return sp->sn_name;
8012 return (char_u *)_("[Deleted]");
8013}
8014
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008015# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008016/*
8017 * Undefine/free all signs.
8018 */
8019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008020free_signs(void)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008021{
8022 while (first_sign != NULL)
8023 sign_undefine(first_sign, NULL);
8024}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008025# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00008026
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008027# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008028static enum
8029{
8030 EXP_SUBCMD, /* expand :sign sub-commands */
8031 EXP_DEFINE, /* expand :sign define {name} args */
8032 EXP_PLACE, /* expand :sign place {id} args */
8033 EXP_UNPLACE, /* expand :sign unplace" */
8034 EXP_SIGN_NAMES /* expand with name of placed signs */
8035} expand_what;
8036
8037/*
8038 * Function given to ExpandGeneric() to obtain the sign command
8039 * expansion.
8040 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008041 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042get_sign_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008043{
8044 sign_T *sp;
8045 int current_idx;
8046
8047 switch (expand_what)
8048 {
8049 case EXP_SUBCMD:
8050 return (char_u *)cmds[idx];
8051 case EXP_DEFINE:
8052 {
8053 char *define_arg[] =
8054 {
8055 "icon=", "linehl=", "text=", "texthl=", NULL
8056 };
8057 return (char_u *)define_arg[idx];
8058 }
8059 case EXP_PLACE:
8060 {
8061 char *place_arg[] =
8062 {
8063 "line=", "name=", "file=", "buffer=", NULL
8064 };
8065 return (char_u *)place_arg[idx];
8066 }
8067 case EXP_UNPLACE:
8068 {
8069 char *unplace_arg[] = { "file=", "buffer=", NULL };
8070 return (char_u *)unplace_arg[idx];
8071 }
8072 case EXP_SIGN_NAMES:
8073 /* Complete with name of signs already defined */
8074 current_idx = 0;
8075 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
8076 if (current_idx++ == idx)
8077 return sp->sn_name;
8078 return NULL;
8079 default:
8080 return NULL;
8081 }
8082}
8083
8084/*
8085 * Handle command line completion for :sign command.
8086 */
8087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008088set_context_in_sign_cmd(expand_T *xp, char_u *arg)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008089{
8090 char_u *p;
8091 char_u *end_subcmd;
8092 char_u *last;
8093 int cmd_idx;
8094 char_u *begin_subcmd_args;
8095
8096 /* Default: expand subcommands. */
8097 xp->xp_context = EXPAND_SIGN;
8098 expand_what = EXP_SUBCMD;
8099 xp->xp_pattern = arg;
8100
8101 end_subcmd = skiptowhite(arg);
8102 if (*end_subcmd == NUL)
8103 /* expand subcmd name
8104 * :sign {subcmd}<CTRL-D>*/
8105 return;
8106
8107 cmd_idx = sign_cmd_idx(arg, end_subcmd);
8108
8109 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008110 * |
8111 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008112 begin_subcmd_args = skipwhite(end_subcmd);
8113 p = skiptowhite(begin_subcmd_args);
8114 if (*p == NUL)
8115 {
8116 /*
8117 * Expand first argument of subcmd when possible.
8118 * For ":jump {id}" and ":unplace {id}", we could
8119 * possibly expand the ids of all signs already placed.
8120 */
8121 xp->xp_pattern = begin_subcmd_args;
8122 switch (cmd_idx)
8123 {
8124 case SIGNCMD_LIST:
8125 case SIGNCMD_UNDEFINE:
8126 /* :sign list <CTRL-D>
8127 * :sign undefine <CTRL-D> */
8128 expand_what = EXP_SIGN_NAMES;
8129 break;
8130 default:
8131 xp->xp_context = EXPAND_NOTHING;
8132 }
8133 return;
8134 }
8135
8136 /* expand last argument of subcmd */
8137
8138 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008139 * |
8140 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008141
8142 /* Loop until reaching last argument. */
8143 do
8144 {
8145 p = skipwhite(p);
8146 last = p;
8147 p = skiptowhite(p);
8148 } while (*p != NUL);
8149
8150 p = vim_strchr(last, '=');
8151
8152 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008153 * | |
8154 * last p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008155 if (p == NUL)
8156 {
8157 /* Expand last argument name (before equal sign). */
8158 xp->xp_pattern = last;
8159 switch (cmd_idx)
8160 {
8161 case SIGNCMD_DEFINE:
8162 expand_what = EXP_DEFINE;
8163 break;
8164 case SIGNCMD_PLACE:
8165 expand_what = EXP_PLACE;
8166 break;
8167 case SIGNCMD_JUMP:
8168 case SIGNCMD_UNPLACE:
8169 expand_what = EXP_UNPLACE;
8170 break;
8171 default:
8172 xp->xp_context = EXPAND_NOTHING;
8173 }
8174 }
8175 else
8176 {
8177 /* Expand last argument value (after equal sign). */
8178 xp->xp_pattern = p + 1;
8179 switch (cmd_idx)
8180 {
8181 case SIGNCMD_DEFINE:
8182 if (STRNCMP(last, "texthl", p - last) == 0 ||
8183 STRNCMP(last, "linehl", p - last) == 0)
8184 xp->xp_context = EXPAND_HIGHLIGHT;
8185 else if (STRNCMP(last, "icon", p - last) == 0)
8186 xp->xp_context = EXPAND_FILES;
8187 else
8188 xp->xp_context = EXPAND_NOTHING;
8189 break;
8190 case SIGNCMD_PLACE:
8191 if (STRNCMP(last, "name", p - last) == 0)
8192 expand_what = EXP_SIGN_NAMES;
8193 else
8194 xp->xp_context = EXPAND_NOTHING;
8195 break;
8196 default:
8197 xp->xp_context = EXPAND_NOTHING;
8198 }
8199 }
8200}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008201# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00008202#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008203
8204/*
8205 * Make the user happy.
8206 */
8207 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008208ex_smile(exarg_T *eap UNUSED)
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008209{
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008210 static char *code[] = {
8211 "\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$",
8212 "\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"
8213 };
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008214 char *p;
8215 int n;
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008216 int i;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008217
8218 msg_start();
8219 msg_putchar('\n');
Bram Moolenaar700eefe2016-04-13 21:14:37 +02008220 for (i = 0; i < 2; ++i)
8221 for (p = code[i]; *p != NUL; ++p)
8222 if (*p == 'x')
8223 msg_putchar('\n');
8224 else
8225 for (n = *p++; n > 0; --n)
8226 if (*p == 'o' || *p == '$')
8227 msg_putchar_attr(*p, hl_attr(HLF_L));
8228 else
8229 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01008230 msg_clr_eos();
8231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
8233#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
8234/*
8235 * ":drop"
8236 * Opens the first argument in a window. When there are two or more arguments
8237 * the argument list is redefined.
8238 */
8239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008240ex_drop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 win_T *wp;
8244 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008245# ifdef FEAT_WINDOWS
8246 tabpage_T *tp;
8247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248
8249 /*
8250 * Check if the first argument is already being edited in a window. If
8251 * so, jump to that window.
8252 * We would actually need to check all arguments, but that's complicated
8253 * and mostly only one file is dropped.
8254 * This also ignores wildcards, since it is very unlikely the user is
8255 * editing a file name with a wildcard character.
8256 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008257 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00008259 /*
8260 * Expanding wildcards may result in an empty argument list. E.g. when
8261 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
8262 * already did an error message for this.
8263 */
8264 if (ARGCOUNT == 0)
8265 return;
8266
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008267# ifdef FEAT_WINDOWS
8268 if (cmdmod.tab)
8269 {
8270 /* ":tab drop file ...": open a tab for each argument that isn't
8271 * edited in a window yet. It's like ":tab all" but without closing
8272 * windows or tabs. */
8273 ex_all(eap);
8274 }
8275 else
8276# endif
8277 {
8278 /* ":drop file ...": Edit the first argument. Jump to an existing
8279 * window if possible, edit in current window if the current buffer
8280 * can be abandoned, otherwise open a new window. */
8281 buf = buflist_findnr(ARGLIST[0].ae_fnum);
8282
8283 FOR_ALL_TAB_WINDOWS(tp, wp)
8284 {
8285 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008287# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008288 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 return;
8292 }
8293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008295 /*
8296 * Check whether the current buffer is changed. If so, we will need
8297 * to split the current window or data could be lost.
8298 * Skip the check if the 'hidden' option is set, as in this case the
8299 * buffer won't be lost.
8300 */
8301 if (!P_HID(curbuf))
8302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008304 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305# endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008306 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008308 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008310 if (split)
8311 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00008315 /* Fake a ":sfirst" or ":first" command edit the first argument. */
8316 if (split)
8317 {
8318 eap->cmdidx = CMD_sfirst;
8319 eap->cmd[0] = 's';
8320 }
8321 else
8322 eap->cmdidx = CMD_first;
8323 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325}
8326#endif