blob: 11c3cf48ba47c2a91c2f97b723394e84f4376863 [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
17#ifdef FEAT_EX_EXTRA
18static int linelen __ARGS((int *has_tab));
19#endif
20static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out));
21#ifdef FEAT_VIMINFO
22static char_u *viminfo_filename __ARGS((char_u *));
Bram Moolenaard812df62008-11-09 12:46:09 +000023static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int flags));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024static int viminfo_encoding __ARGS((vir_T *virp));
25static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing));
26#endif
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028static int check_readonly __ARGS((int *forceit, buf_T *buf));
29#ifdef FEAT_AUTOCMD
30static void delbuf_msg __ARGS((char_u *name));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static int
33#ifdef __BORLANDC__
34 _RTLENTRYF
35#endif
36 help_compare __ARGS((const void *s1, const void *s2));
Bram Moolenaar54fb4382014-11-12 19:28:16 +010037static void prepare_help_buffer __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
40 * ":ascii" and "ga".
41 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 void
43do_ascii(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +000044 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000045{
46 int c;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000047 int cval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 char buf1[20];
49 char buf2[20];
50 char_u buf3[7];
51#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000052 int cc[MAX_MCO];
53 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 int len;
55
56 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000057 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 else
59#endif
60 c = gchar_cursor();
61 if (c == NUL)
62 {
63 MSG("NUL");
64 return;
65 }
66
67#ifdef FEAT_MBYTE
68 IObuff[0] = NUL;
69 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
70#endif
71 {
72 if (c == NL) /* NUL is stored as NL */
73 c = NUL;
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000074 if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
75 cval = NL; /* NL is stored as CR */
76 else
77 cval = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 if (vim_isprintc_strict(c) && (c < ' '
79#ifndef EBCDIC
80 || c > '~'
81#endif
82 ))
83 {
84 transchar_nonprint(buf3, c);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000085 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 }
87 else
88 buf1[0] = NUL;
89#ifndef EBCDIC
90 if (c >= 0x80)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000091 vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
92 (char *)transchar(c & 0x7f));
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 else
94#endif
95 buf2[0] = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +000096 vim_snprintf((char *)IObuff, IOSIZE,
97 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
Bram Moolenaar1418a0d2009-01-13 15:58:01 +000098 transchar(c), buf1, buf2, cval, cval, cval);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#ifdef FEAT_MBYTE
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000100 if (enc_utf8)
101 c = cc[ci++];
102 else
103 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105 }
106
107#ifdef FEAT_MBYTE
108 /* Repeat for combining characters. */
109 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
110 {
111 len = (int)STRLEN(IObuff);
112 /* This assumes every multi-byte char is printable... */
113 if (len > 0)
114 IObuff[len++] = ' ';
115 IObuff[len++] = '<';
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000116 if (enc_utf8 && utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000117# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 )
121 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000122 len += (*mb_char2bytes)(c, IObuff + len);
123 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
125 : _("> %d, Hex %08x, Octal %o"), c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000126 if (ci == MAX_MCO)
127 break;
Bram Moolenaar1f788e72006-09-05 16:30:40 +0000128 if (enc_utf8)
129 c = cc[ci++];
130 else
131 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 }
133#endif
134
135 msg(IObuff);
136}
137
138#if defined(FEAT_EX_EXTRA) || defined(PROTO)
139/*
140 * ":left", ":center" and ":right": align text.
141 */
142 void
143ex_align(eap)
144 exarg_T *eap;
145{
146 pos_T save_curpos;
147 int len;
148 int indent = 0;
149 int new_indent;
150 int has_tab;
151 int width;
152
153#ifdef FEAT_RIGHTLEFT
154 if (curwin->w_p_rl)
155 {
156 /* switch left and right aligning */
157 if (eap->cmdidx == CMD_right)
158 eap->cmdidx = CMD_left;
159 else if (eap->cmdidx == CMD_left)
160 eap->cmdidx = CMD_right;
161 }
162#endif
163
164 width = atoi((char *)eap->arg);
165 save_curpos = curwin->w_cursor;
166 if (eap->cmdidx == CMD_left) /* width is used for new indent */
167 {
168 if (width >= 0)
169 indent = width;
170 }
171 else
172 {
173 /*
174 * if 'textwidth' set, use it
175 * else if 'wrapmargin' set, use it
176 * if invalid value, use 80
177 */
178 if (width <= 0)
179 width = curbuf->b_p_tw;
180 if (width == 0 && curbuf->b_p_wm > 0)
181 width = W_WIDTH(curwin) - curbuf->b_p_wm;
182 if (width <= 0)
183 width = 80;
184 }
185
186 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
187 return;
188
189 for (curwin->w_cursor.lnum = eap->line1;
190 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
191 {
192 if (eap->cmdidx == CMD_left) /* left align */
193 new_indent = indent;
194 else
195 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000196 has_tab = FALSE; /* avoid uninit warnings */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 len = linelen(eap->cmdidx == CMD_right ? &has_tab
198 : NULL) - get_indent();
199
200 if (len <= 0) /* skip blank lines */
201 continue;
202
203 if (eap->cmdidx == CMD_center)
204 new_indent = (width - len) / 2;
205 else
206 {
207 new_indent = width - len; /* right align */
208
209 /*
210 * Make sure that embedded TABs don't make the text go too far
211 * to the right.
212 */
213 if (has_tab)
214 while (new_indent > 0)
215 {
216 (void)set_indent(new_indent, 0);
217 if (linelen(NULL) <= width)
218 {
219 /*
220 * Now try to move the line as much as possible to
221 * the right. Stop when it moves too far.
222 */
223 do
224 (void)set_indent(++new_indent, 0);
225 while (linelen(NULL) <= width);
226 --new_indent;
227 break;
228 }
229 --new_indent;
230 }
231 }
232 }
233 if (new_indent < 0)
234 new_indent = 0;
235 (void)set_indent(new_indent, 0); /* set indent */
236 }
237 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
238 curwin->w_cursor = save_curpos;
239 beginline(BL_WHITE | BL_FIX);
240}
241
242/*
243 * Get the length of the current line, excluding trailing white space.
244 */
245 static int
246linelen(has_tab)
247 int *has_tab;
248{
249 char_u *line;
250 char_u *first;
251 char_u *last;
252 int save;
253 int len;
254
255 /* find the first non-blank character */
256 line = ml_get_curline();
257 first = skipwhite(line);
258
259 /* find the character after the last non-blank character */
260 for (last = first + STRLEN(first);
261 last > first && vim_iswhite(last[-1]); --last)
262 ;
263 save = *last;
264 *last = NUL;
265 len = linetabsize(line); /* get line length */
266 if (has_tab != NULL) /* check for embedded TAB */
267 *has_tab = (vim_strrchr(first, TAB) != NULL);
268 *last = save;
269
270 return len;
271}
272
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000273/* Buffer for two lines used during sorting. They are allocated to
274 * contain the longest line being sorted. */
275static char_u *sortbuf1;
276static char_u *sortbuf2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000277
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100278static int sort_ic; /* ignore case */
279static int sort_nr; /* sort on number */
280static int sort_rx; /* sort on regex instead of skipping it */
281#ifdef FEAT_FLOAT
282static int sort_flt; /* sort on floating number */
283#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000284
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100285static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000286
287/* Struct to store info to be sorted. */
288typedef struct
289{
290 linenr_T lnum; /* line number */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100291 union {
292 struct
293 {
294 long start_col_nr; /* starting column number */
295 long end_col_nr; /* ending column number */
296 } line;
297 long value; /* value if sorting by integer */
298#ifdef FEAT_FLOAT
299 float_T value_flt; /* value if sorting by float */
300#endif
301 } st_u;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000302} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000303
304static int
305#ifdef __BORLANDC__
306_RTLENTRYF
307#endif
308sort_compare __ARGS((const void *s1, const void *s2));
309
310 static int
311#ifdef __BORLANDC__
312_RTLENTRYF
313#endif
314sort_compare(s1, s2)
315 const void *s1;
316 const void *s2;
317{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000318 sorti_T l1 = *(sorti_T *)s1;
319 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000320 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000321
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000322 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000323 * if we return 0 every time, qsort will assume it's done sorting and
324 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000325 if (sort_abort)
326 return 0;
327 fast_breakcheck();
328 if (got_int)
329 sort_abort = TRUE;
330
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000331 /* When sorting numbers "start_col_nr" is the number, not the column
332 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000333 if (sort_nr)
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100334 result = l1.st_u.value == l2.st_u.value ? 0
335 : l1.st_u.value > l2.st_u.value ? 1 : -1;
336#ifdef FEAT_FLOAT
337 else if (sort_flt)
338 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
339 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
340#endif
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 else
342 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000343 /* We need to copy one line into "sortbuf1", because there is no
344 * guarantee that the first pointer becomes invalid when obtaining the
345 * second one. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100346 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
347 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
348 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
349 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
350 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
351 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000352
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000353 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
354 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000355 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000356
357 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000358 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000359 return (int)(l1.lnum - l2.lnum);
360 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000361}
362
363/*
364 * ":sort".
365 */
366 void
367ex_sort(eap)
368 exarg_T *eap;
369{
370 regmatch_T regmatch;
371 int len;
372 linenr_T lnum;
373 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000374 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000375 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000376 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000377 char_u *p;
378 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000379 char_u *s2;
380 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000381 int unique = FALSE;
382 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000383 colnr_T start_col;
384 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100385 int sort_what = 0;
386 int format_found = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000387
Bram Moolenaar48c99162008-02-18 18:42:52 +0000388 /* Sorting one line is really quick! */
389 if (count <= 1)
390 return;
391
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000392 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
393 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000394 sortbuf1 = NULL;
395 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000396 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000397 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000398 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000399 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000400
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100401 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100402#ifdef FEAT_FLOAT
403 sort_flt = 0;
404#endif
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000405
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000406 for (p = eap->arg; *p != NUL; ++p)
407 {
408 if (vim_iswhite(*p))
409 ;
410 else if (*p == 'i')
411 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000412 else if (*p == 'r')
413 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000414 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100415 {
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100416 sort_nr = 1;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100417 ++format_found;
418 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100419#ifdef FEAT_FLOAT
420 else if (*p == 'f')
421 {
422 sort_flt = 1;
423 ++format_found;
424 }
425#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100426 else if (*p == 'b')
427 {
428 sort_what = STR2NR_BIN + STR2NR_FORCE;
429 ++format_found;
430 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000431 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100432 {
433 sort_what = STR2NR_OCT + STR2NR_FORCE;
434 ++format_found;
435 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000436 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100437 {
438 sort_what = STR2NR_HEX + STR2NR_FORCE;
439 ++format_found;
440 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000441 else if (*p == 'u')
442 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000443 else if (*p == '"') /* comment start */
444 break;
445 else if (check_nextcmd(p) != NULL)
446 {
447 eap->nextcmd = check_nextcmd(p);
448 break;
449 }
450 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000451 {
452 s = skip_regexp(p + 1, *p, TRUE, NULL);
453 if (*s != *p)
454 {
455 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000456 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000457 }
458 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000459 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100460 if (s == p + 1)
461 {
462 if (last_search_pat() == NULL)
463 {
464 EMSG(_(e_noprevre));
465 goto sortend;
466 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000467 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100468 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000469 else
470 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000471 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000472 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000473 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000474 regmatch.rm_ic = p_ic;
475 }
476 else
477 {
478 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000479 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000480 }
481 }
482
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100483 /* Can only have one of 'n', 'b', 'o' and 'x'. */
484 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000485 {
486 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000487 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000488 }
489
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100490 /* From here on "sort_nr" is used as a flag for any integer number
491 * sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100492 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000493
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000494 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000495 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000496 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000497 * When sorting on strings "start_col_nr" is the offset in the line, for
498 * numbers sorting it's the number to sort on. This means the pattern
499 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000500 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000501 */
502 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
503 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000504 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000505 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000506 if (maxlen < len)
507 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000508
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000509 start_col = 0;
510 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000511 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000512 {
513 if (sort_rx)
514 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000515 start_col = (colnr_T)(regmatch.startp[0] - s);
516 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000517 }
518 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000519 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000520 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000521 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000522 if (regmatch.regprog != NULL)
523 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000524
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100525 if (sort_nr || sort_flt)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000526 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000527 /* Make sure vim_str2nr doesn't read any digits past the end
528 * of the match, by temporarily terminating the string there */
529 s2 = s + end_col;
530 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200531 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000532 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000533 p = s + start_col;
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100534 if (sort_nr)
535 {
536 if (sort_what & STR2NR_HEX)
537 s = skiptohex(p);
538 else if (sort_what & STR2NR_BIN)
539 s = skiptobin(p);
540 else
541 s = skiptodigit(p);
542 if (s > p && s[-1] == '-')
543 --s; /* include preceding negative sign */
544 if (*s == NUL)
545 /* empty line should sort before any number */
546 nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
547 else
548 vim_str2nr(s, NULL, NULL, sort_what,
549 &nrs[lnum - eap->line1].st_u.value, NULL, 0);
550 }
551#ifdef FEAT_FLOAT
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000552 else
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100553 {
554 s = skipwhite(p);
555 if (*s == '+')
556 s = skipwhite(s + 1);
557
558 if (*s == NUL)
559 /* empty line should sort before any number */
560 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
561 else
562 nrs[lnum - eap->line1].st_u.value_flt =
563 strtod((char *)s, NULL);
564 }
565#endif
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200566 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000567 }
568 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000569 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000570 /* Store the column to sort at. */
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100571 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
572 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000573 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000574
575 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000576
577 if (regmatch.regprog != NULL)
578 fast_breakcheck();
579 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000580 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000581 }
582
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000583 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000584 sortbuf1 = alloc((unsigned)maxlen + 1);
585 if (sortbuf1 == NULL)
586 goto sortend;
587 sortbuf2 = alloc((unsigned)maxlen + 1);
588 if (sortbuf2 == NULL)
589 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000590
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000591 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000592 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000593
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000594 if (sort_abort)
595 goto sortend;
596
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000597 /* Insert the lines in the sorted order below the last one. */
598 lnum = eap->line2;
599 for (i = 0; i < count; ++i)
600 {
601 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
602 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000603 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000604 {
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100605 /* Copy the line into a buffer, it may become invalid in
606 * ml_append(). And it's needed for "unique". */
607 STRCPY(sortbuf1, s);
608 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000609 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000610 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000611 fast_breakcheck();
612 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000613 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000614 }
615
616 /* delete the original lines if appending worked */
617 if (i == count)
618 for (i = 0; i < count; ++i)
619 ml_delete(eap->line1, FALSE);
620 else
621 count = 0;
622
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000623 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000624 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000625 if (deleted > 0)
626 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
627 else if (deleted < 0)
628 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
629 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000630
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000631 curwin->w_cursor.lnum = eap->line1;
632 beginline(BL_WHITE | BL_FIX);
633
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000634sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000635 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000636 vim_free(sortbuf1);
637 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200638 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000639 if (got_int)
640 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000641}
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643/*
644 * ":retab".
645 */
646 void
647ex_retab(eap)
648 exarg_T *eap;
649{
650 linenr_T lnum;
651 int got_tab = FALSE;
652 long num_spaces = 0;
653 long num_tabs;
654 long len;
655 long col;
656 long vcol;
657 long start_col = 0; /* For start of white-space string */
658 long start_vcol = 0; /* For start of white-space string */
659 int temp;
660 long old_len;
661 char_u *ptr;
662 char_u *new_line = (char_u *)1; /* init to non-NULL */
663 int did_undo; /* called u_save for current line */
664 int new_ts;
665 int save_list;
666 linenr_T first_line = 0; /* first changed line */
667 linenr_T last_line = 0; /* last changed line */
668
669 save_list = curwin->w_p_list;
670 curwin->w_p_list = 0; /* don't want list mode here */
671
672 new_ts = getdigits(&(eap->arg));
673 if (new_ts < 0)
674 {
675 EMSG(_(e_positive));
676 return;
677 }
678 if (new_ts == 0)
679 new_ts = curbuf->b_p_ts;
680 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
681 {
682 ptr = ml_get(lnum);
683 col = 0;
684 vcol = 0;
685 did_undo = FALSE;
686 for (;;)
687 {
688 if (vim_iswhite(ptr[col]))
689 {
690 if (!got_tab && num_spaces == 0)
691 {
692 /* First consecutive white-space */
693 start_vcol = vcol;
694 start_col = col;
695 }
696 if (ptr[col] == ' ')
697 num_spaces++;
698 else
699 got_tab = TRUE;
700 }
701 else
702 {
703 if (got_tab || (eap->forceit && num_spaces > 1))
704 {
705 /* Retabulate this string of white-space */
706
707 /* len is virtual length of white string */
708 len = num_spaces = vcol - start_vcol;
709 num_tabs = 0;
710 if (!curbuf->b_p_et)
711 {
712 temp = new_ts - (start_vcol % new_ts);
713 if (num_spaces >= temp)
714 {
715 num_spaces -= temp;
716 num_tabs++;
717 }
718 num_tabs += num_spaces / new_ts;
719 num_spaces -= (num_spaces / new_ts) * new_ts;
720 }
721 if (curbuf->b_p_et || got_tab ||
722 (num_spaces + num_tabs < len))
723 {
724 if (did_undo == FALSE)
725 {
726 did_undo = TRUE;
727 if (u_save((linenr_T)(lnum - 1),
728 (linenr_T)(lnum + 1)) == FAIL)
729 {
730 new_line = NULL; /* flag out-of-memory */
731 break;
732 }
733 }
734
735 /* len is actual number of white characters used */
736 len = num_spaces + num_tabs;
737 old_len = (long)STRLEN(ptr);
738 new_line = lalloc(old_len - col + start_col + len + 1,
739 TRUE);
740 if (new_line == NULL)
741 break;
742 if (start_col > 0)
743 mch_memmove(new_line, ptr, (size_t)start_col);
744 mch_memmove(new_line + start_col + len,
745 ptr + col, (size_t)(old_len - col + 1));
746 ptr = new_line + start_col;
747 for (col = 0; col < len; col++)
748 ptr[col] = (col < num_tabs) ? '\t' : ' ';
749 ml_replace(lnum, new_line, FALSE);
750 if (first_line == 0)
751 first_line = lnum;
752 last_line = lnum;
753 ptr = new_line;
754 col = start_col + len;
755 }
756 }
757 got_tab = FALSE;
758 num_spaces = 0;
759 }
760 if (ptr[col] == NUL)
761 break;
762 vcol += chartabsize(ptr + col, (colnr_T)vcol);
763#ifdef FEAT_MBYTE
764 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000765 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 else
767#endif
768 ++col;
769 }
770 if (new_line == NULL) /* out of memory */
771 break;
772 line_breakcheck();
773 }
774 if (got_int)
775 EMSG(_(e_interr));
776
777 if (curbuf->b_p_ts != new_ts)
778 redraw_curbuf_later(NOT_VALID);
779 if (first_line != 0)
780 changed_lines(first_line, 0, last_line + 1, 0L);
781
782 curwin->w_p_list = save_list; /* restore 'list' */
783
784 curbuf->b_p_ts = new_ts;
785 coladvance(curwin->w_curswant);
786
787 u_clearline();
788}
789#endif
790
791/*
792 * :move command - move lines line1-line2 to line dest
793 *
794 * return FAIL for failure, OK otherwise
795 */
796 int
797do_move(line1, line2, dest)
798 linenr_T line1;
799 linenr_T line2;
800 linenr_T dest;
801{
802 char_u *str;
803 linenr_T l;
804 linenr_T extra; /* Num lines added before line1 */
805 linenr_T num_lines; /* Num lines moved */
806 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200807#ifdef FEAT_FOLDING
808 int isFolded;
809
810 /* Moving lines seems to corrupt the folds, delete folding info now
811 * and recreate it when finished. Don't do this for manual folding, it
812 * would delete all folds. */
813 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin);
814 if (isFolded)
815 deleteFoldRecurse(&curwin->w_folds);
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818 if (dest >= line1 && dest < line2)
819 {
820 EMSG(_("E134: Move lines into themselves"));
821 return FAIL;
822 }
823
824 num_lines = line2 - line1 + 1;
825
826 /*
827 * First we copy the old text to its new location -- webb
828 * Also copy the flag that ":global" command uses.
829 */
830 if (u_save(dest, dest + 1) == FAIL)
831 return FAIL;
832 for (extra = 0, l = line1; l <= line2; l++)
833 {
834 str = vim_strsave(ml_get(l + extra));
835 if (str != NULL)
836 {
837 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
838 vim_free(str);
839 if (dest < line1)
840 extra++;
841 }
842 }
843
844 /*
845 * Now we must be careful adjusting our marks so that we don't overlap our
846 * mark_adjust() calls.
847 *
848 * We adjust the marks within the old text so that they refer to the
849 * last lines of the file (temporarily), because we know no other marks
850 * will be set there since these line numbers did not exist until we added
851 * our new lines.
852 *
853 * Then we adjust the marks on lines between the old and new text positions
854 * (either forwards or backwards).
855 *
856 * And Finally we adjust the marks we put at the end of the file back to
857 * their final destination at the new text position -- webb
858 */
859 last_line = curbuf->b_ml.ml_line_count;
860 mark_adjust(line1, line2, last_line - line2, 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200861 changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (dest >= line2)
863 {
864 mark_adjust(line2 + 1, dest, -num_lines, 0L);
865 curbuf->b_op_start.lnum = dest - num_lines + 1;
866 curbuf->b_op_end.lnum = dest;
867 }
868 else
869 {
870 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
871 curbuf->b_op_start.lnum = dest + 1;
872 curbuf->b_op_end.lnum = dest + num_lines;
873 }
874 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
875 mark_adjust(last_line - num_lines + 1, last_line,
876 -(last_line - dest - extra), 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200877 changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 /*
880 * Now we delete the original text -- webb
881 */
882 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
883 return FAIL;
884
885 for (l = line1; l <= line2; l++)
886 ml_delete(line1 + extra, TRUE);
887
888 if (!global_busy && num_lines > p_report)
889 {
890 if (num_lines == 1)
891 MSG(_("1 line moved"));
892 else
893 smsg((char_u *)_("%ld lines moved"), num_lines);
894 }
895
896 /*
897 * Leave the cursor on the last of the moved lines.
898 */
899 if (dest >= line1)
900 curwin->w_cursor.lnum = dest;
901 else
902 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
903
904 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +0100905 {
906 dest += num_lines + 1;
907 last_line = curbuf->b_ml.ml_line_count;
908 if (dest > last_line + 1)
909 dest = last_line + 1;
910 changed_lines(line1, 0, dest, 0L);
911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 else
913 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
914
Bram Moolenaard5f69332015-04-15 12:43:50 +0200915#ifdef FEAT_FOLDING
916 /* recreate folds */
917 if (isFolded)
918 foldUpdateAll(curwin);
919#endif
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return OK;
922}
923
924/*
925 * ":copy"
926 */
927 void
928ex_copy(line1, line2, n)
929 linenr_T line1;
930 linenr_T line2;
931 linenr_T n;
932{
933 linenr_T count;
934 char_u *p;
935
936 count = line2 - line1 + 1;
937 curbuf->b_op_start.lnum = n + 1;
938 curbuf->b_op_end.lnum = n + count;
939 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
940
941 /*
942 * there are three situations:
943 * 1. destination is above line1
944 * 2. destination is between line1 and line2
945 * 3. destination is below line2
946 *
947 * n = destination (when starting)
948 * curwin->w_cursor.lnum = destination (while copying)
949 * line1 = start of source (while copying)
950 * line2 = end of source (while copying)
951 */
952 if (u_save(n, n + 1) == FAIL)
953 return;
954
955 curwin->w_cursor.lnum = n;
956 while (line1 <= line2)
957 {
958 /* need to use vim_strsave() because the line will be unlocked within
959 * ml_append() */
960 p = vim_strsave(ml_get(line1));
961 if (p != NULL)
962 {
963 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
964 vim_free(p);
965 }
966 /* situation 2: skip already copied lines */
967 if (line1 == n)
968 line1 = curwin->w_cursor.lnum;
969 ++line1;
970 if (curwin->w_cursor.lnum < line1)
971 ++line1;
972 if (curwin->w_cursor.lnum < line2)
973 ++line2;
974 ++curwin->w_cursor.lnum;
975 }
976
977 appended_lines_mark(n, count);
978
979 msgmore((long)count);
980}
981
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000982static char_u *prevcmd = NULL; /* the previous command */
983
984#if defined(EXITFREE) || defined(PROTO)
985 void
986free_prev_shellcmd()
987{
988 vim_free(prevcmd);
989}
990#endif
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
993 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
994 * Bangs in the argument are replaced with the previously entered command.
995 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 */
997 void
998do_bang(addr_count, eap, forceit, do_in, do_out)
999 int addr_count;
1000 exarg_T *eap;
1001 int forceit;
1002 int do_in, do_out;
1003{
1004 char_u *arg = eap->arg; /* command */
1005 linenr_T line1 = eap->line1; /* start of range */
1006 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 char_u *newcmd = NULL; /* the new command */
1008 int free_newcmd = FALSE; /* need to free() newcmd */
1009 int ins_prevcmd;
1010 char_u *t;
1011 char_u *p;
1012 char_u *trailarg;
1013 int len;
1014 int scroll_save = msg_scroll;
1015
1016 /*
1017 * Disallow shell commands for "rvim".
1018 * Disallow shell commands from .exrc and .vimrc in current directory for
1019 * security reasons.
1020 */
1021 if (check_restricted() || check_secure())
1022 return;
1023
1024 if (addr_count == 0) /* :! */
1025 {
1026 msg_scroll = FALSE; /* don't scroll here */
1027 autowrite_all();
1028 msg_scroll = scroll_save;
1029 }
1030
1031 /*
1032 * Try to find an embedded bang, like in :!<cmd> ! [args]
1033 * (:!! is indicated by the 'forceit' variable)
1034 */
1035 ins_prevcmd = forceit;
1036 trailarg = arg;
1037 do
1038 {
1039 len = (int)STRLEN(trailarg) + 1;
1040 if (newcmd != NULL)
1041 len += (int)STRLEN(newcmd);
1042 if (ins_prevcmd)
1043 {
1044 if (prevcmd == NULL)
1045 {
1046 EMSG(_(e_noprev));
1047 vim_free(newcmd);
1048 return;
1049 }
1050 len += (int)STRLEN(prevcmd);
1051 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001052 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
1054 vim_free(newcmd);
1055 return;
1056 }
1057 *t = NUL;
1058 if (newcmd != NULL)
1059 STRCAT(t, newcmd);
1060 if (ins_prevcmd)
1061 STRCAT(t, prevcmd);
1062 p = t + STRLEN(t);
1063 STRCAT(t, trailarg);
1064 vim_free(newcmd);
1065 newcmd = t;
1066
1067 /*
1068 * Scan the rest of the argument for '!', which is replaced by the
1069 * previous command. "\!" is replaced by "!" (this is vi compatible).
1070 */
1071 trailarg = NULL;
1072 while (*p)
1073 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001074 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001077 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 else
1079 {
1080 trailarg = p;
1081 *trailarg++ = NUL;
1082 ins_prevcmd = TRUE;
1083 break;
1084 }
1085 }
1086 ++p;
1087 }
1088 } while (trailarg != NULL);
1089
1090 vim_free(prevcmd);
1091 prevcmd = newcmd;
1092
1093 if (bangredo) /* put cmd in redo buffer for ! command */
1094 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001095 /* If % or # appears in the command, it must have been escaped.
1096 * Reescape them, so that redoing them does not substitute them by the
1097 * buffername. */
1098 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1099
1100 if (cmd != NULL)
1101 {
1102 AppendToRedobuffLit(cmd, -1);
1103 vim_free(cmd);
1104 }
1105 else
1106 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 AppendToRedobuff((char_u *)"\n");
1108 bangredo = FALSE;
1109 }
1110 /*
1111 * Add quotes around the command, for shells that need them.
1112 */
1113 if (*p_shq != NUL)
1114 {
1115 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1116 if (newcmd == NULL)
1117 return;
1118 STRCPY(newcmd, p_shq);
1119 STRCAT(newcmd, prevcmd);
1120 STRCAT(newcmd, p_shq);
1121 free_newcmd = TRUE;
1122 }
1123 if (addr_count == 0) /* :! */
1124 {
1125 /* echo the command */
1126 msg_start();
1127 msg_putchar(':');
1128 msg_putchar('!');
1129 msg_outtrans(newcmd);
1130 msg_clr_eos();
1131 windgoto(msg_row, msg_col);
1132
1133 do_shell(newcmd, 0);
1134 }
1135 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 /* Careful: This may recursively call do_bang() again! (because of
1138 * autocommands) */
1139 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001140#ifdef FEAT_AUTOCMD
1141 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1142#endif
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (free_newcmd)
1145 vim_free(newcmd);
1146}
1147
1148/*
1149 * do_filter: filter lines through a command given by the user
1150 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 * We mostly use temp files and the call_shell() routine here. This would
1152 * normally be done using pipes on a UNIX machine, but this is more portable
1153 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * to deal with redirection somehow, and should handle things like looking
1155 * at the PATH env. variable, and adding reasonable extensions to the
1156 * command name given by the user. All reasonable versions of call_shell()
1157 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 * Alternatively, if on Unix and redirecting input or output, but not both,
1159 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 * We use input redirection if do_in is TRUE.
1161 * We use output redirection if do_out is TRUE.
1162 */
1163 static void
1164do_filter(line1, line2, eap, cmd, do_in, do_out)
1165 linenr_T line1, line2;
1166 exarg_T *eap; /* for forced 'ff' and 'fenc' */
1167 char_u *cmd;
1168 int do_in, do_out;
1169{
1170 char_u *itmp = NULL;
1171 char_u *otmp = NULL;
1172 linenr_T linecount;
1173 linenr_T read_linecount;
1174 pos_T cursor_save;
1175 char_u *cmd_buf;
1176#ifdef FEAT_AUTOCMD
1177 buf_T *old_curbuf = curbuf;
1178#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
1181 if (*cmd == NUL) /* no filter command */
1182 return;
1183
1184#ifdef WIN3264
1185 /*
1186 * Check if external commands are allowed now.
1187 */
1188 if (can_end_termcap_mode(TRUE) == FALSE)
1189 return;
1190#endif
1191
1192 cursor_save = curwin->w_cursor;
1193 linecount = line2 - line1 + 1;
1194 curwin->w_cursor.lnum = line1;
1195 curwin->w_cursor.col = 0;
1196 changed_line_abv_curs();
1197 invalidate_botline();
1198
1199 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 * When using temp files:
1201 * 1. * Form temp file names
1202 * 2. * Write the lines to a temp file
1203 * 3. Run the filter command on the temp file
1204 * 4. * Read the output of the command into the buffer
1205 * 5. * Delete the original lines to be filtered
1206 * 6. * Remove the temp files
1207 *
1208 * When writing the input with a pipe or when catching the output with a
1209 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
1211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 if (do_out)
1213 shell_flags |= SHELL_DOOUT;
1214
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001215#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1219 shell_flags |= SHELL_READ;
1220 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222 else if (do_in && !do_out && !p_stmp)
1223 {
1224 /* Use a pipe to write stdin of the command, do not use a temp file. */
1225 shell_flags |= SHELL_WRITE;
1226 curbuf->b_op_start.lnum = line1;
1227 curbuf->b_op_end.lnum = line2;
1228 }
1229 else if (do_in && do_out && !p_stmp)
1230 {
1231 /* Use a pipe to write stdin and fetch stdout of the command, do not
1232 * use a temp file. */
1233 shell_flags |= SHELL_READ|SHELL_WRITE;
1234 curbuf->b_op_start.lnum = line1;
1235 curbuf->b_op_end.lnum = line2;
1236 curwin->w_cursor.lnum = line2;
1237 }
1238 else
1239#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001240 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1241 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 {
1243 EMSG(_(e_notmp));
1244 goto filterend;
1245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247/*
1248 * The writing and reading of temp files will not be shown.
1249 * Vi also doesn't do this and the messages are not very informative.
1250 */
1251 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 FALSE, FALSE, FALSE, TRUE) == FAIL)
1254 {
1255 msg_putchar('\n'); /* keep message from buf_write() */
1256 --no_wait_return;
1257#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1258 if (!aborting())
1259#endif
1260 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1261 goto filterend;
1262 }
1263#ifdef FEAT_AUTOCMD
1264 if (curbuf != old_curbuf)
1265 goto filterend;
1266#endif
1267
1268 if (!do_out)
1269 msg_putchar('\n');
1270
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001271 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1273 if (cmd_buf == NULL)
1274 goto filterend;
1275
1276 windgoto((int)Rows - 1, 0);
1277 cursor_on();
1278
1279 /*
1280 * When not redirecting the output the command can write anything to the
1281 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1282 * stderr output of external command. Clear the screen later.
1283 * If do_in is FALSE, this could be something like ":r !cat", which may
1284 * also mess up the screen, clear it later.
1285 */
1286 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1287 redraw_later_clear();
1288
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001289 if (do_out)
1290 {
1291 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001292 {
1293 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001294 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001295 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001296 redraw_curbuf_later(VALID);
1297 }
1298 read_linecount = curbuf->b_ml.ml_line_count;
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /*
1301 * When call_shell() fails wait_return() is called to give the user a
1302 * chance to read the error messages. Otherwise errors are ignored, so you
1303 * can see the error messages from the command that appear on stdout; use
1304 * 'u' to fix the text
1305 * Switch to cooked mode when not redirecting stdin, avoids that something
1306 * like ":r !cat" hangs.
1307 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1308 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001309 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {
1311 redraw_later_clear();
1312 wait_return(FALSE);
1313 }
1314 vim_free(cmd_buf);
1315
1316 did_check_timestamps = FALSE;
1317 need_check_timestamps = TRUE;
1318
1319 /* When interrupting the shell command, it may still have produced some
1320 * useful output. Reset got_int here, so that readfile() won't cancel
1321 * reading. */
1322 ui_breakcheck();
1323 got_int = FALSE;
1324
1325 if (do_out)
1326 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001327 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001329 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1330 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001332#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1333 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001335 {
1336 msg_putchar('\n');
1337 EMSG2(_(e_notread), otmp);
1338 }
1339 goto error;
1340 }
1341#ifdef FEAT_AUTOCMD
1342 if (curbuf != old_curbuf)
1343 goto filterend;
1344#endif
1345 }
1346
1347 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1348
1349 if (shell_flags & SHELL_READ)
1350 {
1351 curbuf->b_op_start.lnum = line2 + 1;
1352 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1353 appended_lines_mark(line2, read_linecount);
1354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
1356 if (do_in)
1357 {
1358 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (read_linecount >= linecount)
1361 /* move all marks from old lines to new lines */
1362 mark_adjust(line1, line2, linecount, 0L);
1363 else
1364 {
1365 /* move marks from old lines to new lines, delete marks
1366 * that are in deleted lines */
1367 mark_adjust(line1, line1 + read_linecount - 1,
1368 linecount, 0L);
1369 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1370 }
1371 }
1372
1373 /*
1374 * Put cursor on first filtered line for ":range!cmd".
1375 * Adjust '[ and '] (set by buf_write()).
1376 */
1377 curwin->w_cursor.lnum = line1;
1378 del_lines(linecount, TRUE);
1379 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1380 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1381 write_lnum_adjust(-linecount); /* adjust last line
1382 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001383#ifdef FEAT_FOLDING
1384 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else
1388 {
1389 /*
1390 * Put cursor on last new line for ":r !cmd".
1391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001393 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1397 --no_wait_return;
1398
1399 if (linecount > p_report)
1400 {
1401 if (do_in)
1402 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001403 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1404 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001407 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 else
1410 msgmore((long)linecount);
1411 }
1412 }
1413 else
1414 {
1415error:
1416 /* put cursor back in same position for ":w !cmd" */
1417 curwin->w_cursor = cursor_save;
1418 --no_wait_return;
1419 wait_return(FALSE);
1420 }
1421
1422filterend:
1423
1424#ifdef FEAT_AUTOCMD
1425 if (curbuf != old_curbuf)
1426 {
1427 --no_wait_return;
1428 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1429 }
1430#endif
1431 if (itmp != NULL)
1432 mch_remove(itmp);
1433 if (otmp != NULL)
1434 mch_remove(otmp);
1435 vim_free(itmp);
1436 vim_free(otmp);
1437}
1438
1439/*
1440 * Call a shell to execute a command.
1441 * When "cmd" is NULL start an interactive shell.
1442 */
1443 void
1444do_shell(cmd, flags)
1445 char_u *cmd;
1446 int flags; /* may be SHELL_DOOUT when output is redirected */
1447{
1448 buf_T *buf;
1449#ifndef FEAT_GUI_MSWIN
1450 int save_nwr;
1451#endif
1452#ifdef MSWIN
1453 int winstart = FALSE;
1454#endif
1455
1456 /*
1457 * Disallow shell commands for "rvim".
1458 * Disallow shell commands from .exrc and .vimrc in current directory for
1459 * security reasons.
1460 */
1461 if (check_restricted() || check_secure())
1462 {
1463 msg_end();
1464 return;
1465 }
1466
1467#ifdef MSWIN
1468 /*
1469 * Check if external commands are allowed now.
1470 */
1471 if (can_end_termcap_mode(TRUE) == FALSE)
1472 return;
1473
1474 /*
1475 * Check if ":!start" is used.
1476 */
1477 if (cmd != NULL)
1478 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1479#endif
1480
1481 /*
1482 * For autocommands we want to get the output on the current screen, to
1483 * avoid having to type return below.
1484 */
1485 msg_putchar('\r'); /* put cursor at start of line */
1486#ifdef FEAT_AUTOCMD
1487 if (!autocmd_busy)
1488#endif
1489 {
1490#ifdef MSWIN
1491 if (!winstart)
1492#endif
1493 stoptermcap();
1494 }
1495#ifdef MSWIN
1496 if (!winstart)
1497#endif
1498 msg_putchar('\n'); /* may shift screen one line up */
1499
1500 /* warning message before calling the shell */
1501 if (p_warn
1502#ifdef FEAT_AUTOCMD
1503 && !autocmd_busy
1504#endif
1505 && msg_silent == 0)
1506 for (buf = firstbuf; buf; buf = buf->b_next)
1507 if (bufIsChanged(buf))
1508 {
1509#ifdef FEAT_GUI_MSWIN
1510 if (!winstart)
1511 starttermcap(); /* don't want a message box here */
1512#endif
1513 MSG_PUTS(_("[No write since last change]\n"));
1514#ifdef FEAT_GUI_MSWIN
1515 if (!winstart)
1516 stoptermcap();
1517#endif
1518 break;
1519 }
1520
1521 /* This windgoto is required for when the '\n' resulted in a "delete line
1522 * 1" command to the terminal. */
1523 if (!swapping_screen())
1524 windgoto(msg_row, msg_col);
1525 cursor_on();
1526 (void)call_shell(cmd, SHELL_COOKED | flags);
1527 did_check_timestamps = FALSE;
1528 need_check_timestamps = TRUE;
1529
1530 /*
1531 * put the message cursor at the end of the screen, avoids wait_return()
1532 * to overwrite the text that the external command showed
1533 */
1534 if (!swapping_screen())
1535 {
1536 msg_row = Rows - 1;
1537 msg_col = 0;
1538 }
1539
1540#ifdef FEAT_AUTOCMD
1541 if (autocmd_busy)
1542 {
1543 if (msg_silent == 0)
1544 redraw_later_clear();
1545 }
1546 else
1547#endif
1548 {
1549 /*
1550 * For ":sh" there is no need to call wait_return(), just redraw.
1551 * Also for the Win32 GUI (the output is in a console window).
1552 * Otherwise there is probably text on the screen that the user wants
1553 * to read before redrawing, so call wait_return().
1554 */
1555#ifndef FEAT_GUI_MSWIN
1556 if (cmd == NULL
1557# ifdef WIN3264
1558 || (winstart && !need_wait_return)
1559# endif
1560 )
1561 {
1562 if (msg_silent == 0)
1563 redraw_later_clear();
1564 need_wait_return = FALSE;
1565 }
1566 else
1567 {
1568 /*
1569 * If we switch screens when starttermcap() is called, we really
1570 * want to wait for "hit return to continue".
1571 */
1572 save_nwr = no_wait_return;
1573 if (swapping_screen())
1574 no_wait_return = FALSE;
1575# ifdef AMIGA
1576 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1577# else
1578 wait_return(msg_silent == 0);
1579# endif
1580 no_wait_return = save_nwr;
1581 }
1582#endif /* FEAT_GUI_W32 */
1583
1584#ifdef MSWIN
1585 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1586#endif
1587 starttermcap(); /* start termcap if not done by wait_return() */
1588
1589 /*
1590 * In an Amiga window redrawing is caused by asking the window size.
1591 * If we got an interrupt this will not work. The chance that the
1592 * window size is wrong is very small, but we need to redraw the
1593 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1594 * but it saves an extra redraw.
1595 */
1596#ifdef AMIGA
1597 if (skip_redraw) /* ':' hit in wait_return() */
1598 {
1599 if (msg_silent == 0)
1600 redraw_later_clear();
1601 }
1602 else if (term_console)
1603 {
1604 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1605 if (got_int && msg_silent == 0)
1606 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1607 else
1608 must_redraw = 0; /* no extra redraw needed */
1609 }
1610#endif
1611 }
1612
1613 /* display any error messages now */
1614 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001615
1616#ifdef FEAT_AUTOCMD
1617 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619}
1620
1621/*
1622 * Create a shell command from a command string, input redirection file and
1623 * output redirection file.
1624 * Returns an allocated string with the shell command, or NULL for failure.
1625 */
1626 char_u *
1627make_filter_cmd(cmd, itmp, otmp)
1628 char_u *cmd; /* command */
1629 char_u *itmp; /* NULL or name of input file */
1630 char_u *otmp; /* NULL or name of output file */
1631{
1632 char_u *buf;
1633 long_u len;
1634
Bram Moolenaar53076832015-12-31 19:53:21 +01001635#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001636 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001637 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001638
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001639 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001640 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1641 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001642 if (is_fish_shell)
1643 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1644 else
1645#endif
1646 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (itmp != NULL)
1648 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1649 if (otmp != NULL)
1650 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1651 buf = lalloc(len, TRUE);
1652 if (buf == NULL)
1653 return NULL;
1654
Bram Moolenaar53076832015-12-31 19:53:21 +01001655#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001657 * Put braces around the command (for concatenated commands) when
1658 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001660 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001661 {
1662 if (is_fish_shell)
1663 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1664 else
1665 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1666 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001667 else
1668 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (itmp != NULL)
1670 {
1671 STRCAT(buf, " < ");
1672 STRCAT(buf, itmp);
1673 }
1674#else
1675 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001676 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 * the use of commands in a pipe.
1678 */
1679 STRCPY(buf, cmd);
1680 if (itmp != NULL)
1681 {
1682 char_u *p;
1683
1684 /*
1685 * If there is a pipe, we have to put the '<' in front of it.
1686 * Don't do this when 'shellquote' is not empty, otherwise the
1687 * redirection would be inside the quotes.
1688 */
1689 if (*p_shq == NUL)
1690 {
1691 p = vim_strchr(buf, '|');
1692 if (p != NULL)
1693 *p = NUL;
1694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1696 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (*p_shq == NUL)
1698 {
1699 p = vim_strchr(cmd, '|');
1700 if (p != NULL)
1701 {
1702 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1703 STRCAT(buf, p);
1704 }
1705 }
1706 }
1707#endif
1708 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001709 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 return buf;
1712}
1713
1714/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001715 * Append output redirection for file "fname" to the end of string buffer
1716 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 * Works with the 'shellredir' and 'shellpipe' options.
1718 * The caller should make sure that there is enough room:
1719 * STRLEN(opt) + STRLEN(fname) + 3
1720 */
1721 void
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001722append_redir(buf, buflen, opt, fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 char_u *buf;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001724 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 char_u *opt;
1726 char_u *fname;
1727{
1728 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001729 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001731 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001732 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001734 {
1735 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001737 if (p[1] == '%') /* skip %% */
1738 ++p;
1739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (p != NULL)
1741 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001742 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1743 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1744 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
1746 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001747 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 " %s %s",
1750#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#endif
1753 (char *)opt, (char *)fname);
1754}
1755
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001756#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758static int no_viminfo __ARGS((void));
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001759static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760static int viminfo_errcnt;
1761
1762 static int
1763no_viminfo()
1764{
1765 /* "vim -i NONE" does not read or write a viminfo file */
1766 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1767}
1768
1769/*
1770 * Report an error for reading a viminfo file.
1771 * Count the number of errors. When there are more than 10, return TRUE.
1772 */
1773 int
1774viminfo_error(errnum, message, line)
1775 char *errnum;
1776 char *message;
1777 char_u *line;
1778{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001779 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1780 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001781 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001782 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1783 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 emsg(IObuff);
1785 if (++viminfo_errcnt >= 10)
1786 {
1787 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1788 return TRUE;
1789 }
1790 return FALSE;
1791}
1792
1793/*
1794 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001795 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 int
Bram Moolenaard812df62008-11-09 12:46:09 +00001798read_viminfo(file, flags)
1799 char_u *file; /* file name or NULL to use default name */
1800 int flags; /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 FILE *fp;
1803 char_u *fname;
1804
1805 if (no_viminfo())
1806 return FAIL;
1807
Bram Moolenaard812df62008-11-09 12:46:09 +00001808 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (fname == NULL)
1810 return FAIL;
1811 fp = mch_fopen((char *)fname, READBIN);
1812
1813 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001814 {
1815 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001816 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1817 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001818 (flags & VIF_WANT_INFO) ? _(" info") : "",
1819 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1820 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001821 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001822 verbose_leave();
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 vim_free(fname);
1826 if (fp == NULL)
1827 return FAIL;
1828
1829 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001830 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 return OK;
1834}
1835
1836/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001837 * Write the viminfo file. The old one is read in first so that effectively a
1838 * merge of current info and old info is done. This allows multiple vims to
1839 * run simultaneously, without losing any marks etc.
1840 * If "forceit" is TRUE, then the old file is not read in, and only internal
1841 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 void
1844write_viminfo(file, forceit)
1845 char_u *file;
1846 int forceit;
1847{
1848 char_u *fname;
1849 FILE *fp_in = NULL; /* input viminfo file, if any */
1850 FILE *fp_out = NULL; /* output viminfo file */
1851 char_u *tempname = NULL; /* name of temp viminfo file */
1852 struct stat st_new; /* mch_stat() of potential new file */
1853 char_u *wp;
1854#if defined(UNIX) || defined(VMS)
1855 mode_t umask_save;
1856#endif
1857#ifdef UNIX
1858 int shortname = FALSE; /* use 8.3 file name */
1859 struct stat st_old; /* mch_stat() of existing viminfo file */
1860#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001861#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001862 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865 if (no_viminfo())
1866 return;
1867
1868 fname = viminfo_filename(file); /* may set to default if NULL */
1869 if (fname == NULL)
1870 return;
1871
1872 fp_in = mch_fopen((char *)fname, READBIN);
1873 if (fp_in == NULL)
1874 {
1875 /* if it does exist, but we can't read it, don't try writing */
1876 if (mch_stat((char *)fname, &st_new) == 0)
1877 goto end;
1878#if defined(UNIX) || defined(VMS)
1879 /*
1880 * For Unix we create the .viminfo non-accessible for others,
1881 * because it may contain text from non-accessible documents.
1882 */
1883 umask_save = umask(077);
1884#endif
1885 fp_out = mch_fopen((char *)fname, WRITEBIN);
1886#if defined(UNIX) || defined(VMS)
1887 (void)umask(umask_save);
1888#endif
1889 }
1890 else
1891 {
1892 /*
1893 * There is an existing viminfo file. Create a temporary file to
1894 * write the new viminfo into, in the same directory as the
1895 * existing viminfo file, which will be renamed later.
1896 */
1897#ifdef UNIX
1898 /*
1899 * For Unix we check the owner of the file. It's not very nice to
1900 * overwrite a user's viminfo file after a "su root", with a
1901 * viminfo file that the user can't read.
1902 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001903 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001904 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001906 if (mch_stat((char *)fname, &st_old) == 0
1907 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001908 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 ? (st_old.st_mode & 0200)
1910 : (st_old.st_gid == getgid()
1911 ? (st_old.st_mode & 0020)
1912 : (st_old.st_mode & 0002))))
1913 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001914 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1918 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001919 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 goto end;
1921 }
1922#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001923#ifdef WIN3264
1924 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001925 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 /*
1929 * Make tempname.
1930 * May try twice: Once normal and once with shortname set, just in
1931 * case somebody puts his viminfo file in an 8.3 filesystem.
1932 */
1933 for (;;)
1934 {
1935 tempname = buf_modname(
1936#ifdef UNIX
1937 shortname,
1938#else
1939# ifdef SHORT_FNAME
1940 TRUE,
1941# else
1942# ifdef FEAT_GUI_W32
1943 gui_is_win32s(),
1944# else
1945 FALSE,
1946# endif
1947# endif
1948#endif
1949 fname,
1950#ifdef VMS
1951 (char_u *)"-tmp",
1952#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
1955 FALSE);
1956 if (tempname == NULL) /* out of memory */
1957 break;
1958
1959 /*
1960 * Check if tempfile already exists. Never overwrite an
1961 * existing file!
1962 */
1963 if (mch_stat((char *)tempname, &st_new) == 0)
1964 {
1965#ifdef UNIX
1966 /*
1967 * Check if tempfile is same as original file. May happen
1968 * when modname() gave the same file back. E.g. silly
1969 * link, or file name-length reached. Try again with
1970 * shortname set.
1971 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001972 if (!shortname && st_new.st_dev == st_old.st_dev
1973 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 vim_free(tempname);
1976 tempname = NULL;
1977 shortname = TRUE;
1978 continue;
1979 }
1980#endif
1981 /*
1982 * Try another name. Change one character, just before
1983 * the extension. This should also work for an 8.3
1984 * file name, when after adding the extension it still is
1985 * the same file as the original.
1986 */
1987 wp = tempname + STRLEN(tempname) - 5;
1988 if (wp < gettail(tempname)) /* empty file name? */
1989 wp = gettail(tempname);
1990 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1991 --*wp)
1992 {
1993 /*
1994 * They all exist? Must be something wrong! Don't
1995 * write the viminfo file then.
1996 */
1997 if (*wp == 'a')
1998 {
1999 vim_free(tempname);
2000 tempname = NULL;
2001 break;
2002 }
2003 }
2004 }
2005 break;
2006 }
2007
2008 if (tempname != NULL)
2009 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00002010#ifdef VMS
2011 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00002012 umask_save = umask(077);
2013 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2014 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002015#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00002016 int fd;
2017
2018 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002019 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002020 * Unix: same as original file, but strip s-bit. Reset umask to
2021 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002022 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00002023# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002024 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002025 fd = mch_open((char *)tempname,
2026 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002027 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002028 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002029# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00002030 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002031 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002032# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 if (fd < 0)
2034 fp_out = NULL;
2035 else
2036 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00002037#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
2039 /*
2040 * If we can't create in the same directory, try creating a
2041 * "normal" temp file.
2042 */
2043 if (fp_out == NULL)
2044 {
2045 vim_free(tempname);
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002046 if ((tempname = vim_tempname('o', TRUE)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2048 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00002049
2050#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00002052 * Make sure the owner can read/write it. This only works for
2053 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 */
2055 if (fp_out != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002056 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#endif
2058 }
2059 }
2060
2061 /*
2062 * Check if the new viminfo file can be written to.
2063 */
2064 if (fp_out == NULL)
2065 {
2066 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002067 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (fp_in != NULL)
2069 fclose(fp_in);
2070 goto end;
2071 }
2072
2073 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002074 {
2075 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002076 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002077 verbose_leave();
2078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002081 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 fclose(fp_out); /* errors are ignored !? */
2084 if (fp_in != NULL)
2085 {
2086 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002087
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002088 /* In case of an error keep the original viminfo file. Otherwise
2089 * rename the newly written file. Give an error if that fails. */
2090 if (viminfo_errcnt == 0 && vim_rename(tempname, fname) == -1)
2091 {
2092 ++viminfo_errcnt;
2093 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2094 }
2095 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002097
2098#ifdef WIN3264
2099 /* If the viminfo file was hidden then also hide the new file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002100 if (hidden)
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002101 mch_hide(fname);
2102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105end:
2106 vim_free(fname);
2107 vim_free(tempname);
2108}
2109
2110/*
2111 * Get the viminfo file name to use.
2112 * If "file" is given and not empty, use it (has already been expanded by
2113 * cmdline functions).
2114 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2115 * expand environment variables.
2116 * Returns an allocated string. NULL when out of memory.
2117 */
2118 static char_u *
2119viminfo_filename(file)
2120 char_u *file;
2121{
2122 if (file == NULL || *file == NUL)
2123 {
2124 if (use_viminfo != NULL)
2125 file = use_viminfo;
2126 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2127 {
2128#ifdef VIMINFO_FILE2
2129 /* don't use $HOME when not defined (turned into "c:/"!). */
2130# ifdef VMS
2131 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2132# else
2133 if (mch_getenv((char_u *)"HOME") == NULL)
2134# endif
2135 {
2136 /* don't use $VIM when not available. */
2137 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2138 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2139 file = (char_u *)VIMINFO_FILE2;
2140 else
2141 file = (char_u *)VIMINFO_FILE;
2142 }
2143 else
2144#endif
2145 file = (char_u *)VIMINFO_FILE;
2146 }
2147 expand_env(file, NameBuff, MAXPATHL);
2148 file = NameBuff;
2149 }
2150 return vim_strsave(file);
2151}
2152
2153/*
2154 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2155 */
2156 static void
Bram Moolenaard812df62008-11-09 12:46:09 +00002157do_viminfo(fp_in, fp_out, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 FILE *fp_in;
2159 FILE *fp_out;
Bram Moolenaard812df62008-11-09 12:46:09 +00002160 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 int count = 0;
2163 int eof = FALSE;
2164 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002165 int merge = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
2167 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2168 return;
2169 vir.vir_fd = fp_in;
2170#ifdef FEAT_MBYTE
2171 vir.vir_conv.vc_type = CONV_NONE;
2172#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002173 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175 if (fp_in != NULL)
2176 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002177 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002178 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002179 eof = read_viminfo_up_to_marks(&vir,
2180 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002181 merge = TRUE;
2182 }
2183 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 /* Skip info, find start of marks */
2185 while (!(eof = viminfo_readline(&vir))
2186 && vir.vir_line[0] != '>')
2187 ;
2188 }
2189 if (fp_out != NULL)
2190 {
2191 /* Write the info: */
2192 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2193 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002194 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002196 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2198#endif
2199 write_viminfo_search_pattern(fp_out);
2200 write_viminfo_sub_string(fp_out);
2201#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002202 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203#endif
2204 write_viminfo_registers(fp_out);
2205#ifdef FEAT_EVAL
2206 write_viminfo_varlist(fp_out);
2207#endif
2208 write_viminfo_filemarks(fp_out);
2209 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002210 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 count = write_viminfo_marks(fp_out);
2212 }
Bram Moolenaard812df62008-11-09 12:46:09 +00002213 if (fp_in != NULL
2214 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT)))
2215 copy_viminfo_marks(&vir, fp_out, count, eof, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 vim_free(vir.vir_line);
2218#ifdef FEAT_MBYTE
2219 if (vir.vir_conv.vc_type != CONV_NONE)
2220 convert_setup(&vir.vir_conv, NULL, NULL);
2221#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002222 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
2225/*
2226 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2227 * first part of the viminfo file which contains everything but the marks that
2228 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2229 */
2230 static int
2231read_viminfo_up_to_marks(virp, forceit, writing)
2232 vir_T *virp;
2233 int forceit;
2234 int writing;
2235{
2236 int eof;
2237 buf_T *buf;
2238
2239#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002240 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#endif
2242 eof = viminfo_readline(virp);
2243 while (!eof && virp->vir_line[0] != '>')
2244 {
2245 switch (virp->vir_line[0])
2246 {
2247 /* Characters reserved for future expansion, ignored now */
2248 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 case '^': /* to be defined */
2250 case '<': /* long line - ignored */
2251 /* A comment or empty line. */
2252 case NUL:
2253 case '\r':
2254 case '\n':
2255 case '#':
2256 eof = viminfo_readline(virp);
2257 break;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002258 case '|': /* copy line (for future use) */
2259 if (writing)
2260 ga_add_string(&virp->vir_barlines, virp->vir_line);
2261 eof = viminfo_readline(virp);
2262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 case '*': /* "*encoding=value" */
2264 eof = viminfo_encoding(virp);
2265 break;
2266 case '!': /* global variable */
2267#ifdef FEAT_EVAL
2268 eof = read_viminfo_varlist(virp, writing);
2269#else
2270 eof = viminfo_readline(virp);
2271#endif
2272 break;
2273 case '%': /* entry for buffer list */
2274 eof = read_viminfo_bufferlist(virp, writing);
2275 break;
2276 case '"':
2277 eof = read_viminfo_register(virp, forceit);
2278 break;
2279 case '/': /* Search string */
2280 case '&': /* Substitute search string */
2281 case '~': /* Last search string, followed by '/' or '&' */
2282 eof = read_viminfo_search_pattern(virp, forceit);
2283 break;
2284 case '$':
2285 eof = read_viminfo_sub_string(virp, forceit);
2286 break;
2287 case ':':
2288 case '?':
2289 case '=':
2290 case '@':
2291#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002292 eof = read_viminfo_history(virp, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#else
2294 eof = viminfo_readline(virp);
2295#endif
2296 break;
2297 case '-':
2298 case '\'':
2299 eof = read_viminfo_filemark(virp, forceit);
2300 break;
2301 default:
2302 if (viminfo_error("E575: ", _("Illegal starting char"),
2303 virp->vir_line))
2304 eof = TRUE;
2305 else
2306 eof = viminfo_readline(virp);
2307 break;
2308 }
2309 }
2310
2311#ifdef FEAT_CMDHIST
2312 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002313 if (!writing)
2314 finish_viminfo_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
2316
2317 /* Change file names to buffer numbers for fmarks. */
2318 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2319 fmarks_check_names(buf);
2320
2321 return eof;
2322}
2323
2324/*
2325 * Compare the 'encoding' value in the viminfo file with the current value of
2326 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2327 * conversion of text with iconv() in viminfo_readstring().
2328 */
2329 static int
2330viminfo_encoding(virp)
2331 vir_T *virp;
2332{
2333#ifdef FEAT_MBYTE
2334 char_u *p;
2335 int i;
2336
2337 if (get_viminfo_parameter('c') != 0)
2338 {
2339 p = vim_strchr(virp->vir_line, '=');
2340 if (p != NULL)
2341 {
2342 /* remove trailing newline */
2343 ++p;
2344 for (i = 0; vim_isprintc(p[i]); ++i)
2345 ;
2346 p[i] = NUL;
2347
2348 convert_setup(&virp->vir_conv, p, p_enc);
2349 }
2350 }
2351#endif
2352 return viminfo_readline(virp);
2353}
2354
2355/*
2356 * Read a line from the viminfo file.
2357 * Returns TRUE for end-of-file;
2358 */
2359 int
2360viminfo_readline(virp)
2361 vir_T *virp;
2362{
2363 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2364}
2365
2366/*
2367 * check string read from viminfo file
2368 * remove '\n' at the end of the line
2369 * - replace CTRL-V CTRL-V with CTRL-V
2370 * - replace CTRL-V 'n' with '\n'
2371 *
2372 * Check for a long line as written by viminfo_writestring().
2373 *
2374 * Return the string in allocated memory (NULL when out of memory).
2375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 char_u *
2377viminfo_readstring(virp, off, convert)
2378 vir_T *virp;
2379 int off; /* offset for virp->vir_line */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002380 int convert UNUSED; /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *retval;
2383 char_u *s, *d;
2384 long len;
2385
2386 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2387 {
2388 len = atol((char *)virp->vir_line + off + 1);
2389 retval = lalloc(len, TRUE);
2390 if (retval == NULL)
2391 {
2392 /* Line too long? File messed up? Skip next line. */
2393 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2394 return NULL;
2395 }
2396 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2397 s = retval + 1; /* Skip the leading '<' */
2398 }
2399 else
2400 {
2401 retval = vim_strsave(virp->vir_line + off);
2402 if (retval == NULL)
2403 return NULL;
2404 s = retval;
2405 }
2406
2407 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2408 d = retval;
2409 while (*s != NUL && *s != '\n')
2410 {
2411 if (s[0] == Ctrl_V && s[1] != NUL)
2412 {
2413 if (s[1] == 'n')
2414 *d++ = '\n';
2415 else
2416 *d++ = Ctrl_V;
2417 s += 2;
2418 }
2419 else
2420 *d++ = *s++;
2421 }
2422 *d = NUL;
2423
2424#ifdef FEAT_MBYTE
2425 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2426 {
2427 d = string_convert(&virp->vir_conv, retval, NULL);
2428 if (d != NULL)
2429 {
2430 vim_free(retval);
2431 retval = d;
2432 }
2433 }
2434#endif
2435
2436 return retval;
2437}
2438
2439/*
2440 * write string to viminfo file
2441 * - replace CTRL-V with CTRL-V CTRL-V
2442 * - replace '\n' with CTRL-V 'n'
2443 * - add a '\n' at the end
2444 *
2445 * For a long line:
2446 * - write " CTRL-V <length> \n " in first line
2447 * - write " < <string> \n " in second line
2448 */
2449 void
2450viminfo_writestring(fd, p)
2451 FILE *fd;
2452 char_u *p;
2453{
2454 int c;
2455 char_u *s;
2456 int len = 0;
2457
2458 for (s = p; *s != NUL; ++s)
2459 {
2460 if (*s == Ctrl_V || *s == '\n')
2461 ++len;
2462 ++len;
2463 }
2464
2465 /* If the string will be too long, write its length and put it in the next
2466 * line. Take into account that some room is needed for what comes before
2467 * the string (e.g., variable name). Add something to the length for the
2468 * '<', NL and trailing NUL. */
2469 if (len > LSIZE / 2)
2470 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2471
2472 while ((c = *p++) != NUL)
2473 {
2474 if (c == Ctrl_V || c == '\n')
2475 {
2476 putc(Ctrl_V, fd);
2477 if (c == '\n')
2478 c = 'n';
2479 }
2480 putc(c, fd);
2481 }
2482 putc('\n', fd);
2483}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002484
2485 static void
2486write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2487{
2488 int i;
2489 garray_T *gap = &virp->vir_barlines;
2490
2491 if (gap->ga_len > 0)
2492 {
2493 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2494
2495 for (i = 0; i < gap->ga_len; ++i)
2496 fputs(((char **)(gap->ga_data))[i], fp_out);
2497 }
2498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#endif /* FEAT_VIMINFO */
2500
2501/*
2502 * Implementation of ":fixdel", also used by get_stty().
2503 * <BS> resulting <Del>
2504 * ^? ^H
2505 * not ^? ^?
2506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 void
2508do_fixdel(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002509 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 char_u *p;
2512
2513 p = find_termcode((char_u *)"kb");
2514 add_termcode((char_u *)"kD", p != NULL
2515 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2516}
2517
2518 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002519print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 linenr_T lnum;
2521 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002522 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002524 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526 if (curwin->w_p_nu || use_number)
2527 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002528 vim_snprintf((char *)numbuf, sizeof(numbuf),
2529 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2531 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002532 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533}
2534
2535/*
2536 * Print a text line. Also in silent mode ("ex -s").
2537 */
2538 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002539print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 linenr_T lnum;
2541 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002542 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 int save_silent = silent_mode;
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002547 silent_mode = FALSE;
2548 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2549 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (save_silent)
2551 {
2552 msg_putchar('\n');
2553 cursor_on(); /* msg_start() switches it off */
2554 out_flush();
2555 silent_mode = save_silent;
2556 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00002557 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558}
2559
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002560 int
2561rename_buffer(new_fname)
2562 char_u *new_fname;
2563{
2564 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02002565 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002566
Bram Moolenaar7e283842013-05-30 11:43:15 +02002567#ifdef FEAT_AUTOCMD
2568 buf = curbuf;
2569 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002570 /* buffer changed, don't change name now */
2571 if (buf != curbuf)
2572 return FAIL;
2573# ifdef FEAT_EVAL
2574 if (aborting()) /* autocmds may abort script processing */
2575 return FAIL;
2576# endif
2577#endif
2578 /*
2579 * The name of the current buffer will be changed.
2580 * A new (unlisted) buffer entry needs to be made to hold the old file
2581 * name, which will become the alternate file name.
2582 * But don't set the alternate file name if the buffer didn't have a
2583 * name.
2584 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02002585 fname = curbuf->b_ffname;
2586 sfname = curbuf->b_sfname;
2587 xfname = curbuf->b_fname;
2588 curbuf->b_ffname = NULL;
2589 curbuf->b_sfname = NULL;
2590 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002591 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02002592 curbuf->b_ffname = fname;
2593 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002594 return FAIL;
2595 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02002596 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002597 if (xfname != NULL && *xfname != NUL)
2598 {
2599 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2600 if (buf != NULL && !cmdmod.keepalt)
2601 curwin->w_alt_fnum = buf->b_fnum;
2602 }
2603 vim_free(fname);
2604 vim_free(sfname);
2605#ifdef FEAT_AUTOCMD
Bram Moolenaar7e283842013-05-30 11:43:15 +02002606 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002607#endif
2608 /* Change directories when the 'acd' option is set. */
2609 DO_AUTOCHDIR
2610 return OK;
2611}
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613/*
2614 * ":file[!] [fname]".
2615 */
2616 void
2617ex_file(eap)
2618 exarg_T *eap;
2619{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002620 /* ":0file" removes the file name. Check for illegal uses ":3file",
2621 * "0file name", etc. */
2622 if (eap->addr_count > 0
2623 && (*eap->arg != NUL
2624 || eap->line2 > 0
2625 || eap->addr_count > 1))
2626 {
2627 EMSG(_(e_invarg));
2628 return;
2629 }
2630
2631 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002633 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636 /* print full file name if :cd used */
2637 fileinfo(FALSE, FALSE, eap->forceit);
2638}
2639
2640/*
2641 * ":update".
2642 */
2643 void
2644ex_update(eap)
2645 exarg_T *eap;
2646{
2647 if (curbufIsChanged())
2648 (void)do_write(eap);
2649}
2650
2651/*
2652 * ":write" and ":saveas".
2653 */
2654 void
2655ex_write(eap)
2656 exarg_T *eap;
2657{
2658 if (eap->usefilter) /* input lines to shell command */
2659 do_bang(1, eap, FALSE, TRUE, FALSE);
2660 else
2661 (void)do_write(eap);
2662}
2663
2664/*
2665 * write current buffer to file 'eap->arg'
2666 * if 'eap->append' is TRUE, append to the file
2667 *
2668 * if *eap->arg == NUL write to current file
2669 *
2670 * return FAIL for failure, OK otherwise
2671 */
2672 int
2673do_write(eap)
2674 exarg_T *eap;
2675{
2676 int other;
2677 char_u *fname = NULL; /* init to shut up gcc */
2678 char_u *ffname;
2679 int retval = FAIL;
2680 char_u *free_fname = NULL;
2681#ifdef FEAT_BROWSE
2682 char_u *browse_file = NULL;
2683#endif
2684 buf_T *alt_buf = NULL;
2685
2686 if (not_writing()) /* check 'write' option */
2687 return FAIL;
2688
2689 ffname = eap->arg;
2690#ifdef FEAT_BROWSE
2691 if (cmdmod.browse)
2692 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002693 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 NULL, NULL, NULL, curbuf);
2695 if (browse_file == NULL)
2696 goto theend;
2697 ffname = browse_file;
2698 }
2699#endif
2700 if (*ffname == NUL)
2701 {
2702 if (eap->cmdidx == CMD_saveas)
2703 {
2704 EMSG(_(e_argreq));
2705 goto theend;
2706 }
2707 other = FALSE;
2708 }
2709 else
2710 {
2711 fname = ffname;
2712 free_fname = fix_fname(ffname);
2713 /*
2714 * When out-of-memory, keep unexpanded file name, because we MUST be
2715 * able to write the file in this situation.
2716 */
2717 if (free_fname != NULL)
2718 ffname = free_fname;
2719 other = otherfile(ffname);
2720 }
2721
2722 /*
2723 * If we have a new file, put its name in the list of alternate file names.
2724 */
2725 if (other)
2726 {
2727 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2728 || eap->cmdidx == CMD_saveas)
2729 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2730 else
2731 alt_buf = buflist_findname(ffname);
2732 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2733 {
2734 /* Overwriting a file that is loaded in another buffer is not a
2735 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002736 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 goto theend;
2738 }
2739 }
2740
2741 /*
2742 * Writing to the current file is not allowed in readonly mode
2743 * and a file name is required.
2744 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2745 */
2746 if (!other && (
2747#ifdef FEAT_QUICKFIX
2748 bt_dontwrite_msg(curbuf) ||
2749#endif
2750 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2751 goto theend;
2752
2753 if (!other)
2754 {
2755 ffname = curbuf->b_ffname;
2756 fname = curbuf->b_fname;
2757 /*
2758 * Not writing the whole file is only allowed with '!'.
2759 */
2760 if ( (eap->line1 != 1
2761 || eap->line2 != curbuf->b_ml.ml_line_count)
2762 && !eap->forceit
2763 && !eap->append
2764 && !p_wa)
2765 {
2766#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2767 if (p_confirm || cmdmod.confirm)
2768 {
2769 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2770 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2771 goto theend;
2772 eap->forceit = TRUE;
2773 }
2774 else
2775#endif
2776 {
2777 EMSG(_("E140: Use ! to write partial buffer"));
2778 goto theend;
2779 }
2780 }
2781 }
2782
2783 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2784 {
2785 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2786 {
2787#ifdef FEAT_AUTOCMD
2788 buf_T *was_curbuf = curbuf;
2789
2790 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002791 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792# ifdef FEAT_EVAL
2793 if (curbuf != was_curbuf || aborting())
2794# else
2795 if (curbuf != was_curbuf)
2796# endif
2797 {
2798 /* buffer changed, don't change name now */
2799 retval = FAIL;
2800 goto theend;
2801 }
2802#endif
2803 /* Exchange the file names for the current and the alternate
2804 * buffer. This makes it look like we are now editing the buffer
2805 * under the new name. Must be done before buf_write(), because
2806 * if there is no file name and 'cpo' contains 'F', it will set
2807 * the file name. */
2808 fname = alt_buf->b_fname;
2809 alt_buf->b_fname = curbuf->b_fname;
2810 curbuf->b_fname = fname;
2811 fname = alt_buf->b_ffname;
2812 alt_buf->b_ffname = curbuf->b_ffname;
2813 curbuf->b_ffname = fname;
2814 fname = alt_buf->b_sfname;
2815 alt_buf->b_sfname = curbuf->b_sfname;
2816 curbuf->b_sfname = fname;
2817 buf_name_changed(curbuf);
2818#ifdef FEAT_AUTOCMD
2819 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002820 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (!alt_buf->b_p_bl)
2822 {
2823 alt_buf->b_p_bl = TRUE;
2824 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2825 }
2826# ifdef FEAT_EVAL
2827 if (curbuf != was_curbuf || aborting())
2828# else
2829 if (curbuf != was_curbuf)
2830# endif
2831 {
2832 /* buffer changed, don't write the file */
2833 retval = FAIL;
2834 goto theend;
2835 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002836
2837 /* If 'filetype' was empty try detecting it now. */
2838 if (*curbuf->b_p_ft == NUL)
2839 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002840 if (au_has_group((char_u *)"filetypedetect"))
2841 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
2842 TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002843 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002844 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01002845
2846 /* Autocommands may have changed buffer names, esp. when
2847 * 'autochdir' is set. */
2848 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#endif
2850 }
2851
2852 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2853 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002854
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002855 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002856 if (eap->cmdidx == CMD_saveas)
2857 {
2858 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00002859 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002860 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00002861#ifdef FEAT_WINDOWS
2862 redraw_tabline = TRUE;
2863#endif
2864 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002865 /* Change directories when the 'acd' option is set. */
2866 DO_AUTOCHDIR
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869
2870theend:
2871#ifdef FEAT_BROWSE
2872 vim_free(browse_file);
2873#endif
2874 vim_free(free_fname);
2875 return retval;
2876}
2877
2878/*
2879 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2880 * BF_NEW or BF_READERR, check for overwriting current file.
2881 * May set eap->forceit if a dialog says it's OK to overwrite.
2882 * Return OK if it's OK, FAIL if it is not.
2883 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02002884 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885check_overwrite(eap, buf, fname, ffname, other)
2886 exarg_T *eap;
2887 buf_T *buf;
2888 char_u *fname; /* file name to be used (can differ from
2889 buf->ffname) */
2890 char_u *ffname; /* full path version of fname */
2891 int other; /* writing under other name */
2892{
2893 /*
2894 * write to other file or b_flags set or not writing the whole file:
2895 * overwriting only allowed with '!'
2896 */
2897 if ( (other
2898 || (buf->b_flags & BF_NOTEDITED)
2899 || ((buf->b_flags & BF_NEW)
2900 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2901 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00002903#ifdef FEAT_QUICKFIX
2904 && !bt_nofile(buf)
2905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 && vim_fexists(ffname))
2907 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002908 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002910#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002911 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002912 if (mch_isdir(ffname))
2913 {
2914 EMSG2(_(e_isadir2), ffname);
2915 return FAIL;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#endif
2918#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002919 if (p_confirm || cmdmod.confirm)
2920 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002921 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002923 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2924 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2925 return FAIL;
2926 eap->forceit = TRUE;
2927 }
2928 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002930 {
2931 EMSG(_(e_exists));
2932 return FAIL;
2933 }
2934 }
2935
2936 /* For ":w! filename" check that no swap file exists for "filename". */
2937 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002939 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002940 char_u *p;
2941 int r;
2942 char_u *swapname;
2943
2944 /* We only try the first entry in 'directory', without checking if
2945 * it's writable. If the "." directory is not writable the write
2946 * will probably fail anyway.
2947 * Use 'shortname' of the current buffer, since there is no buffer
2948 * for the written file. */
2949 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002950 {
2951 dir = alloc(5);
2952 if (dir == NULL)
2953 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002954 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002955 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002956 else
2957 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002958 dir = alloc(MAXPATHL);
2959 if (dir == NULL)
2960 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002961 p = p_dir;
2962 copy_option_part(&p, dir, MAXPATHL, ",");
2963 }
2964 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02002965 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002966 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002967 if (r)
2968 {
2969#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2970 if (p_confirm || cmdmod.confirm)
2971 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002972 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002973
2974 dialog_msg(buff,
2975 _("Swap file \"%s\" exists, overwrite anyway?"),
2976 swapname);
2977 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2978 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002979 {
2980 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002981 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002982 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002983 eap->forceit = TRUE;
2984 }
2985 else
2986#endif
2987 {
2988 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2989 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002990 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002991 return FAIL;
2992 }
2993 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002994 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 }
2997 return OK;
2998}
2999
3000/*
3001 * Handle ":wnext", ":wNext" and ":wprevious" commands.
3002 */
3003 void
3004ex_wnext(eap)
3005 exarg_T *eap;
3006{
3007 int i;
3008
3009 if (eap->cmd[1] == 'n')
3010 i = curwin->w_arg_idx + (int)eap->line2;
3011 else
3012 i = curwin->w_arg_idx - (int)eap->line2;
3013 eap->line1 = 1;
3014 eap->line2 = curbuf->b_ml.ml_line_count;
3015 if (do_write(eap) != FAIL)
3016 do_argfile(eap, i);
3017}
3018
3019/*
3020 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
3021 */
3022 void
3023do_wqall(eap)
3024 exarg_T *eap;
3025{
3026 buf_T *buf;
3027 int error = 0;
3028 int save_forceit = eap->forceit;
3029
3030 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
3031 exiting = TRUE;
3032
3033 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3034 {
3035 if (bufIsChanged(buf))
3036 {
3037 /*
3038 * Check if there is a reason the buffer cannot be written:
3039 * 1. if the 'write' option is set
3040 * 2. if there is no file name (even after browsing)
3041 * 3. if the 'readonly' is set (even after a dialog)
3042 * 4. if overwriting is allowed (even after a dialog)
3043 */
3044 if (not_writing())
3045 {
3046 ++error;
3047 break;
3048 }
3049#ifdef FEAT_BROWSE
3050 /* ":browse wall": ask for file name if there isn't one */
3051 if (buf->b_ffname == NULL && cmdmod.browse)
3052 browse_save_fname(buf);
3053#endif
3054 if (buf->b_ffname == NULL)
3055 {
3056 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3057 ++error;
3058 }
3059 else if (check_readonly(&eap->forceit, buf)
3060 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3061 FALSE) == FAIL)
3062 {
3063 ++error;
3064 }
3065 else
3066 {
3067 if (buf_write_all(buf, eap->forceit) == FAIL)
3068 ++error;
3069#ifdef FEAT_AUTOCMD
3070 /* an autocommand may have deleted the buffer */
3071 if (!buf_valid(buf))
3072 buf = firstbuf;
3073#endif
3074 }
3075 eap->forceit = save_forceit; /* check_overwrite() may set it */
3076 }
3077 }
3078 if (exiting)
3079 {
3080 if (!error)
3081 getout(0); /* exit Vim */
3082 not_exiting();
3083 }
3084}
3085
3086/*
3087 * Check the 'write' option.
3088 * Return TRUE and give a message when it's not st.
3089 */
3090 int
3091not_writing()
3092{
3093 if (p_write)
3094 return FALSE;
3095 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3096 return TRUE;
3097}
3098
3099/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003100 * Check if a buffer is read-only (either 'readonly' option is set or file is
3101 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3102 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
3104 static int
3105check_readonly(forceit, buf)
3106 int *forceit;
3107 buf_T *buf;
3108{
Bram Moolenaar5386a122007-06-28 20:02:32 +00003109 struct stat st;
3110
3111 /* Handle a file being readonly when the 'readonly' option is set or when
3112 * the file exists and permissions are read-only.
3113 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3114 * important for device checks but not here. */
3115 if (!*forceit && (buf->b_p_ro
3116 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3117 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3120 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3121 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003122 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
Bram Moolenaar5386a122007-06-28 20:02:32 +00003124 if (buf->b_p_ro)
3125 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3126 buf->b_fname);
3127 else
3128 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 +00003129 buf->b_fname);
3130
3131 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3132 {
3133 /* Set forceit, to force the writing of a readonly file */
3134 *forceit = TRUE;
3135 return FALSE;
3136 }
3137 else
3138 return TRUE;
3139 }
3140 else
3141#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003142 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003144 else
3145 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3146 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 return TRUE;
3148 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return FALSE;
3151}
3152
3153/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003154 * Try to abandon current file and edit a new or existing file.
3155 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003157 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003158 * -1 for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * 'lnum' is the line number for the cursor in the new file (if non-zero).
3160 */
3161 int
3162getfile(fnum, ffname, sfname, setpm, lnum, forceit)
3163 int fnum;
3164 char_u *ffname;
3165 char_u *sfname;
3166 int setpm;
3167 linenr_T lnum;
3168 int forceit;
3169{
3170 int other;
3171 int retval;
3172 char_u *free_me = NULL;
3173
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003174 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003176#ifdef FEAT_AUTOCMD
3177 if (curbuf_locked())
3178 return 1;
3179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 if (fnum == 0)
3182 {
3183 /* make ffname full path, set sfname */
3184 fname_expand(curbuf, &ffname, &sfname);
3185 other = otherfile(ffname);
3186 free_me = ffname; /* has been allocated, free() later */
3187 }
3188 else
3189 other = (fnum != curbuf->b_fnum);
3190
3191 if (other)
3192 ++no_wait_return; /* don't wait for autowrite message */
3193 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
3194 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3195 {
3196#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3197 if (p_confirm && p_write)
3198 dialog_changed(curbuf, FALSE);
3199 if (curbufIsChanged())
3200#endif
3201 {
3202 if (other)
3203 --no_wait_return;
3204 EMSG(_(e_nowrtmsg));
3205 retval = 2; /* file has been changed */
3206 goto theend;
3207 }
3208 }
3209 if (other)
3210 --no_wait_return;
3211 if (setpm)
3212 setpcmark();
3213 if (!other)
3214 {
3215 if (lnum != 0)
3216 curwin->w_cursor.lnum = lnum;
3217 check_cursor_lnum();
3218 beginline(BL_SOL | BL_FIX);
3219 retval = 0; /* it's in the same file */
3220 }
3221 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003222 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
3223 curwin) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 retval = -1; /* opened another file */
3225 else
3226 retval = 1; /* error encountered */
3227
3228theend:
3229 vim_free(free_me);
3230 return retval;
3231}
3232
3233/*
3234 * start editing a new file
3235 *
3236 * fnum: file number; if zero use ffname/sfname
3237 * ffname: the file name
3238 * - full path if sfname used,
3239 * - any file name if sfname is NULL
3240 * - empty string to re-edit with the same file name (but may be
3241 * in a different directory)
3242 * - NULL to start an empty buffer
3243 * sfname: the short file name (or NULL)
3244 * eap: contains the command to be executed after loading the file and
3245 * forced 'ff' and 'fenc'
3246 * newlnum: if > 0: put cursor on this line number (if possible)
3247 * if ECMD_LASTL: use last position in loaded file
3248 * if ECMD_LAST: use last position in all files
3249 * if ECMD_ONE: use first line
3250 * flags:
3251 * ECMD_HIDE: if TRUE don't free the current buffer
3252 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3253 * ECMD_OLDBUF: use existing buffer if it exists
3254 * ECMD_FORCEIT: ! used for Ex command
3255 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003256 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003257 * window, NULL when splitting the window first. When not NULL info
3258 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 *
3260 * return FAIL for failure, OK otherwise
3261 */
3262 int
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003263do_ecmd(fnum, ffname, sfname, eap, newlnum, flags, oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 int fnum;
3265 char_u *ffname;
3266 char_u *sfname;
3267 exarg_T *eap; /* can be NULL! */
3268 linenr_T newlnum;
3269 int flags;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003270 win_T *oldwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 int other_file; /* TRUE if editing another file */
3273 int oldbuf; /* TRUE if using existing buffer */
3274#ifdef FEAT_AUTOCMD
3275 int auto_buf = FALSE; /* TRUE if autocommands brought us
3276 into the buffer unexpectedly */
3277 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003278 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280 buf_T *buf;
3281#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3282 buf_T *old_curbuf = curbuf;
3283#endif
3284 char_u *free_fname = NULL;
3285#ifdef FEAT_BROWSE
3286 char_u *browse_file = NULL;
3287#endif
3288 int retval = FAIL;
3289 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003290 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 linenr_T topline = 0;
3292 int newcol = -1;
3293 int solcol = -1;
3294 pos_T *pos;
3295#ifdef FEAT_SUN_WORKSHOP
3296 char_u *cp;
3297#endif
3298 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003299#ifdef FEAT_SPELL
3300 int did_get_winopts = FALSE;
3301#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003302 int readfile_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 if (eap != NULL)
3305 command = eap->do_ecmd_cmd;
3306
3307 if (fnum != 0)
3308 {
3309 if (fnum == curbuf->b_fnum) /* file is already being edited */
3310 return OK; /* nothing to do */
3311 other_file = TRUE;
3312 }
3313 else
3314 {
3315#ifdef FEAT_BROWSE
3316 if (cmdmod.browse)
3317 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003318# ifdef FEAT_AUTOCMD
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003319 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003320# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003321 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003322# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003323 au_has_group((char_u *)"FileExplorer"))
3324 {
3325 /* No browsing supported but we do have the file explorer:
3326 * Edit the directory. */
3327 if (ffname == NULL || !mch_isdir(ffname))
3328 ffname = (char_u *)".";
3329 }
3330 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003331# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003332 {
3333 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003335 if (browse_file == NULL)
3336 goto theend;
3337 ffname = browse_file;
3338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340#endif
3341 /* if no short name given, use ffname for short name */
3342 if (sfname == NULL)
3343 sfname = ffname;
3344#ifdef USE_FNAME_CASE
3345# ifdef USE_LONG_FNAME
3346 if (USE_LONG_FNAME)
3347# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003348 if (sfname != NULL)
3349 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#endif
3351
3352#ifdef FEAT_LISTCMDS
3353 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3354 goto theend;
3355#endif
3356
3357 if (ffname == NULL)
3358 other_file = TRUE;
3359 /* there is no file name */
3360 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3361 other_file = FALSE;
3362 else
3363 {
3364 if (*ffname == NUL) /* re-edit with same file name */
3365 {
3366 ffname = curbuf->b_ffname;
3367 sfname = curbuf->b_fname;
3368 }
3369 free_fname = fix_fname(ffname); /* may expand to full path name */
3370 if (free_fname != NULL)
3371 ffname = free_fname;
3372 other_file = otherfile(ffname);
3373#ifdef FEAT_SUN_WORKSHOP
3374 if (usingSunWorkShop && p_acd
3375 && (cp = vim_strrchr(sfname, '/')) != NULL)
3376 sfname = ++cp;
3377#endif
3378 }
3379 }
3380
3381 /*
3382 * if the file was changed we may not be allowed to abandon it
3383 * - if we are going to re-edit the same file
3384 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3385 */
3386 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3387 || (curbuf->b_nwindows == 1
3388 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003389 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3390 | (other_file ? 0 : CCGD_MULTWIN)
3391 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3392 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
3394 if (fnum == 0 && other_file && ffname != NULL)
3395 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3396 goto theend;
3397 }
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 /*
3400 * End Visual mode before switching to another buffer, so the text can be
3401 * copied into the GUI selection buffer.
3402 */
3403 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003405#ifdef FEAT_AUTOCMD
3406 if ((command != NULL || newlnum > (linenr_T)0)
3407 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3408 {
3409 int len;
3410 char_u *p;
3411
3412 /* Set v:swapcommand for the SwapExists autocommands. */
3413 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003414 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003415 else
3416 len = 30;
3417 p = alloc((unsigned)len);
3418 if (p != NULL)
3419 {
3420 if (command != NULL)
3421 vim_snprintf((char *)p, len, ":%s\r", command);
3422 else
3423 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3424 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3425 did_set_swapcommand = TRUE;
3426 vim_free(p);
3427 }
3428 }
3429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /*
3432 * If we are starting to edit another file, open a (new) buffer.
3433 * Otherwise we re-use the current buffer.
3434 */
3435 if (other_file)
3436 {
3437#ifdef FEAT_LISTCMDS
3438 if (!(flags & ECMD_ADDBUF))
3439#endif
3440 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003441 if (!cmdmod.keepalt)
3442 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003443 if (oldwin != NULL)
3444 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447 if (fnum)
3448 buf = buflist_findnr(fnum);
3449 else
3450 {
3451#ifdef FEAT_LISTCMDS
3452 if (flags & ECMD_ADDBUF)
3453 {
3454 linenr_T tlnum = 1L;
3455
3456 if (command != NULL)
3457 {
3458 tlnum = atol((char *)command);
3459 if (tlnum <= 0)
3460 tlnum = 1L;
3461 }
3462 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3463 goto theend;
3464 }
3465#endif
3466 buf = buflist_new(ffname, sfname, 0L,
3467 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003468#ifdef FEAT_AUTOCMD
3469 /* autocommands may change curwin and curbuf */
3470 if (oldwin != NULL)
3471 oldwin = curwin;
3472 old_curbuf = curbuf;
3473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 if (buf == NULL)
3476 goto theend;
3477 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3478 {
3479 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 else /* existing memfile */
3482 {
3483 oldbuf = TRUE;
3484 (void)buf_check_timestamp(buf, FALSE);
3485 /* Check if autocommands made buffer invalid or changed the current
3486 * buffer. */
3487 if (!buf_valid(buf)
3488#ifdef FEAT_AUTOCMD
3489 || curbuf != old_curbuf
3490#endif
3491 )
3492 goto theend;
3493#ifdef FEAT_EVAL
3494 if (aborting()) /* autocmds may abort script processing */
3495 goto theend;
3496#endif
3497 }
3498
3499 /* May jump to last used line number for a loaded buffer or when asked
3500 * for explicitly */
3501 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3502 {
3503 pos = buflist_findfpos(buf);
3504 newlnum = pos->lnum;
3505 solcol = pos->col;
3506 }
3507
3508 /*
3509 * Make the (new) buffer the one used by the current window.
3510 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3511 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003512 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 */
3514 if (buf != curbuf)
3515 {
3516#ifdef FEAT_AUTOCMD
3517 /*
3518 * Be careful: The autocommands may delete any buffer and change
3519 * the current buffer.
3520 * - If the buffer we are going to edit is deleted, give up.
3521 * - If the current buffer is deleted, prefer to load the new
3522 * buffer when loading a buffer is required. This avoids
3523 * loading another buffer which then must be closed again.
3524 * - If we ended up in the new buffer already, need to skip a few
3525 * things, set auto_buf.
3526 */
3527 if (buf->b_fname != NULL)
3528 new_name = vim_strsave(buf->b_fname);
3529 au_new_curbuf = buf;
3530 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3531 if (!buf_valid(buf)) /* new buffer has been deleted */
3532 {
3533 delbuf_msg(new_name); /* frees new_name */
3534 goto theend;
3535 }
3536# ifdef FEAT_EVAL
3537 if (aborting()) /* autocmds may abort script processing */
3538 {
3539 vim_free(new_name);
3540 goto theend;
3541 }
3542# endif
3543 if (buf == curbuf) /* already in new buffer */
3544 auto_buf = TRUE;
3545 else
3546 {
3547 if (curbuf == old_curbuf)
3548#endif
3549 buf_copy_options(buf, BCO_ENTER);
3550
3551 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003552 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003553 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003554 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556#ifdef FEAT_AUTOCMD
Bram Moolenaarfc573802011-12-30 15:01:59 +01003557 /* Autocommands may open a new window and leave oldwin open
3558 * which leads to crashes since the above call sets
3559 * oldwin->w_buffer to NULL. */
3560 if (curwin != oldwin && oldwin != aucmd_win
3561 && win_valid(oldwin) && oldwin->w_buffer == NULL)
3562 win_close(oldwin, FALSE);
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564# ifdef FEAT_EVAL
3565 if (aborting()) /* autocmds may abort script processing */
3566 {
3567 vim_free(new_name);
3568 goto theend;
3569 }
3570# endif
3571 /* Be careful again, like above. */
3572 if (!buf_valid(buf)) /* new buffer has been deleted */
3573 {
3574 delbuf_msg(new_name); /* frees new_name */
3575 goto theend;
3576 }
3577 if (buf == curbuf) /* already in new buffer */
3578 auto_buf = TRUE;
3579 else
3580#endif
3581 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582#ifdef FEAT_SYN_HL
3583 /*
3584 * <VN> We could instead free the synblock
3585 * and re-attach to buffer, perhaps.
3586 */
3587 if (curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02003588 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 curwin->w_buffer = buf;
3591 curbuf = buf;
3592 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003593
3594 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
3595 if (!oldbuf && eap != NULL)
3596 {
3597 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003598#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003599 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003600#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
3604 /* May get the window options from the last time this buffer
3605 * was in this window (or another window). If not used
3606 * before, reset the local window options to the global
3607 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00003608 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003609#ifdef FEAT_SPELL
3610 did_get_winopts = TRUE;
3611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613#ifdef FEAT_AUTOCMD
3614 }
3615 vim_free(new_name);
3616 au_new_curbuf = NULL;
3617#endif
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 curwin->w_pcmark.lnum = 1;
3621 curwin->w_pcmark.col = 0;
3622 }
3623 else /* !other_file */
3624 {
3625 if (
3626#ifdef FEAT_LISTCMDS
3627 (flags & ECMD_ADDBUF) ||
3628#endif
3629 check_fname() == FAIL)
3630 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01003631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 oldbuf = (flags & ECMD_OLDBUF);
3633 }
3634
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003635#ifdef FEAT_AUTOCMD
3636 buf = curbuf;
3637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3639 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003640 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642 else
3643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /* Don't make a buffer listed if it's a help buffer. Useful when
3645 * using CTRL-O to go back to a help file. */
3646 if (!curbuf->b_help)
3647 set_buflisted(TRUE);
3648 }
3649
3650#ifdef FEAT_AUTOCMD
3651 /* If autocommands change buffers under our fingers, forget about
3652 * editing the file. */
3653 if (buf != curbuf)
3654 goto theend;
3655# ifdef FEAT_EVAL
3656 if (aborting()) /* autocmds may abort script processing */
3657 goto theend;
3658# endif
3659
3660 /* Since we are starting to edit a file, consider the filetype to be
3661 * unset. Helps for when an autocommand changes files and expects syntax
3662 * highlighting to work in the other file. */
3663 did_filetype = FALSE;
3664#endif
3665
3666/*
3667 * other_file oldbuf
3668 * FALSE FALSE re-edit same file, buffer is re-used
3669 * FALSE TRUE re-edit same file, nothing changes
3670 * TRUE FALSE start editing new file, new buffer
3671 * TRUE TRUE start editing in existing buffer (nothing to do)
3672 */
3673 if (!other_file && !oldbuf) /* re-use the buffer */
3674 {
3675 set_last_cursor(curwin); /* may set b_last_cursor */
3676 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3677 {
3678 newlnum = curwin->w_cursor.lnum;
3679 solcol = curwin->w_cursor.col;
3680 }
3681#ifdef FEAT_AUTOCMD
3682 buf = curbuf;
3683 if (buf->b_fname != NULL)
3684 new_name = vim_strsave(buf->b_fname);
3685 else
3686 new_name = NULL;
3687#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003688 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
3689 {
3690 /* Save all the text, so that the reload can be undone.
3691 * Sync first so that this is a separate undo-able action. */
3692 u_sync(FALSE);
3693 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
3694 == FAIL)
3695 goto theend;
3696 u_unchanged(curbuf);
3697 buf_freeall(curbuf, BFA_KEEP_UNDO);
3698
3699 /* tell readfile() not to clear or reload undo info */
3700 readfile_flags = READ_KEEP_UNDO;
3701 }
3702 else
3703 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#ifdef FEAT_AUTOCMD
3705 /* If autocommands deleted the buffer we were going to re-edit, give
3706 * up and jump to the end. */
3707 if (!buf_valid(buf))
3708 {
3709 delbuf_msg(new_name); /* frees new_name */
3710 goto theend;
3711 }
3712 vim_free(new_name);
3713
3714 /* If autocommands change buffers under our fingers, forget about
3715 * re-editing the file. Should do the buf_clear_file(), but perhaps
3716 * the autocommands changed the buffer... */
3717 if (buf != curbuf)
3718 goto theend;
3719# ifdef FEAT_EVAL
3720 if (aborting()) /* autocmds may abort script processing */
3721 goto theend;
3722# endif
3723#endif
3724 buf_clear_file(curbuf);
3725 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3726 curbuf->b_op_end.lnum = 0;
3727 }
3728
3729/*
3730 * If we get here we are sure to start editing
3731 */
3732 /* don't redraw until the cursor is in the right line */
3733 ++RedrawingDisabled;
3734
3735 /* Assume success now */
3736 retval = OK;
3737
3738 /*
3739 * Reset cursor position, could be used by autocommands.
3740 */
3741 check_cursor();
3742
3743 /*
3744 * Check if we are editing the w_arg_idx file in the argument list.
3745 */
3746 check_arg_idx(curwin);
3747
3748#ifdef FEAT_AUTOCMD
3749 if (!auto_buf)
3750#endif
3751 {
3752 /*
3753 * Set cursor and init window before reading the file and executing
3754 * autocommands. This allows for the autocommands to position the
3755 * cursor.
3756 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003757 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003760 /* It's possible that all lines in the buffer changed. Need to update
3761 * automatic folding for all windows where it's used. */
3762# ifdef FEAT_WINDOWS
3763 {
3764 win_T *win;
3765 tabpage_T *tp;
3766
3767 FOR_ALL_TAB_WINDOWS(tp, win)
3768 if (win->w_buffer == curbuf)
3769 foldUpdateAll(win);
3770 }
3771# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 foldUpdateAll(curwin);
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#endif
3775
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003776 /* Change directories when the 'acd' option is set. */
3777 DO_AUTOCHDIR
3778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 /*
3780 * Careful: open_buffer() and apply_autocmds() may change the current
3781 * buffer and window.
3782 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01003783 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 topline = curwin->w_topline;
3785 if (!oldbuf) /* need to read the file */
3786 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003787#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 swap_exists_action = SEA_DIALOG;
3789#endif
3790 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3791
3792 /*
3793 * Open the buffer and read the file.
3794 */
3795#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003796 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 retval = FAIL;
3798#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003799 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#endif
3801
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003802#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (swap_exists_action == SEA_QUIT)
3804 retval = FAIL;
3805 handle_swap_exists(old_curbuf);
3806#endif
3807 }
3808#ifdef FEAT_AUTOCMD
3809 else
3810 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003811 /* Read the modelines, but only to set window-local options. Any
3812 * buffer-local options have already been set and may have been
3813 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00003814 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3817 &retval);
3818 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3819 &retval);
3820 }
3821 check_arg_idx(curwin);
3822#endif
3823
Bram Moolenaareab316b2015-03-24 11:46:30 +01003824 /* If autocommands change the cursor position or topline, we should
3825 * keep it. Also when it moves within a line. */
3826 if (!equalpos(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 newlnum = curwin->w_cursor.lnum;
3829 newcol = curwin->w_cursor.col;
3830 }
3831 if (curwin->w_topline == topline)
3832 topline = 0;
3833
3834 /* Even when cursor didn't move we need to recompute topline. */
3835 changed_line_abv_curs();
3836
3837#ifdef FEAT_TITLE
3838 maketitle();
3839#endif
3840 }
3841
3842#ifdef FEAT_DIFF
3843 /* Tell the diff stuff that this buffer is new and/or needs updating.
3844 * Also needed when re-editing the same buffer, because unloading will
3845 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003846 if (curwin->w_p_diff)
3847 {
3848 diff_buf_add(curbuf);
3849 diff_invalidate(curbuf);
3850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851#endif
3852
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003853#ifdef FEAT_SPELL
3854 /* If the window options were changed may need to set the spell language.
3855 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003856 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
3857 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003858#endif
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (command == NULL)
3861 {
3862 if (newcol >= 0) /* position set by autocommands */
3863 {
3864 curwin->w_cursor.lnum = newlnum;
3865 curwin->w_cursor.col = newcol;
3866 check_cursor();
3867 }
3868 else if (newlnum > 0) /* line number from caller or old position */
3869 {
3870 curwin->w_cursor.lnum = newlnum;
3871 check_cursor_lnum();
3872 if (solcol >= 0 && !p_sol)
3873 {
3874 /* 'sol' is off: Use last known column. */
3875 curwin->w_cursor.col = solcol;
3876 check_cursor_col();
3877#ifdef FEAT_VIRTUALEDIT
3878 curwin->w_cursor.coladd = 0;
3879#endif
3880 curwin->w_set_curswant = TRUE;
3881 }
3882 else
3883 beginline(BL_SOL | BL_FIX);
3884 }
3885 else /* no line number, go to last line in Ex mode */
3886 {
3887 if (exmode_active)
3888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3889 beginline(BL_WHITE | BL_FIX);
3890 }
3891 }
3892
3893#ifdef FEAT_WINDOWS
3894 /* Check if cursors in other windows on the same buffer are still valid */
3895 check_lnums(FALSE);
3896#endif
3897
3898 /*
3899 * Did not read the file, need to show some info about the file.
3900 * Do this after setting the cursor.
3901 */
3902 if (oldbuf
3903#ifdef FEAT_AUTOCMD
3904 && !auto_buf
3905#endif
3906 )
3907 {
3908 int msg_scroll_save = msg_scroll;
3909
3910 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3911 * message. */
3912 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3913 msg_scroll = FALSE;
3914 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3915 check_for_delay(FALSE);
3916 msg_start();
3917 msg_scroll = msg_scroll_save;
3918 msg_scrolled_ign = TRUE;
3919
3920 fileinfo(FALSE, TRUE, FALSE);
3921
3922 msg_scrolled_ign = FALSE;
3923 }
3924
3925 if (command != NULL)
3926 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3927
3928#ifdef FEAT_KEYMAP
3929 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003930 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#endif
3932
3933 --RedrawingDisabled;
3934 if (!skip_redraw)
3935 {
3936 n = p_so;
3937 if (topline == 0 && command == NULL)
3938 p_so = 999; /* force cursor halfway the window */
3939 update_topline();
3940#ifdef FEAT_SCROLLBIND
3941 curwin->w_scbind_pos = curwin->w_topline;
3942#endif
3943 p_so = n;
3944 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3945 }
3946
3947 if (p_im)
3948 need_start_insertmode = TRUE;
3949
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003950 /* Change directories when the 'acd' option is set. */
3951 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003953#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003954 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
3956# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02003957 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3959# endif
3960# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003961 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003962 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963# endif
3964 }
3965#endif
3966
3967theend:
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003968#ifdef FEAT_AUTOCMD
3969 if (did_set_swapcommand)
3970 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
3971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972#ifdef FEAT_BROWSE
3973 vim_free(browse_file);
3974#endif
3975 vim_free(free_fname);
3976 return retval;
3977}
3978
3979#ifdef FEAT_AUTOCMD
3980 static void
3981delbuf_msg(name)
3982 char_u *name;
3983{
3984 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3985 name == NULL ? (char_u *)"" : name);
3986 vim_free(name);
3987 au_new_curbuf = NULL;
3988}
3989#endif
3990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003991static int append_indent = 0; /* autoindent for first line */
3992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993/*
3994 * ":insert" and ":append", also used by ":change"
3995 */
3996 void
3997ex_append(eap)
3998 exarg_T *eap;
3999{
4000 char_u *theline;
4001 int did_undo = FALSE;
4002 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004003 int indent = 0;
4004 char_u *p;
4005 int vcol;
4006 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004008 /* the ! flag toggles autoindent */
4009 if (eap->forceit)
4010 curbuf->b_p_ai = !curbuf->b_p_ai;
4011
4012 /* First autoindent comes from the line we start on */
4013 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
4014 append_indent = get_indent_lnum(lnum);
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (eap->cmdidx != CMD_append)
4017 --lnum;
4018
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004019 /* when the buffer is empty append to line 0 and delete the dummy line */
4020 if (empty && lnum == 1)
4021 lnum = 0;
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 State = INSERT; /* behave like in Insert mode */
4024 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
4025 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004026
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004027 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
4029 msg_scroll = TRUE;
4030 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004031 if (curbuf->b_p_ai)
4032 {
4033 if (append_indent >= 0)
4034 {
4035 indent = append_indent;
4036 append_indent = -1;
4037 }
4038 else if (lnum > 0)
4039 indent = get_indent_lnum(lnum);
4040 }
4041 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004043 {
4044 /* No getline() function, use the lines that follow. This ends
4045 * when there is no more. */
4046 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4047 break;
4048 p = vim_strchr(eap->nextcmd, NL);
4049 if (p == NULL)
4050 p = eap->nextcmd + STRLEN(eap->nextcmd);
4051 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4052 if (*p != NUL)
4053 ++p;
4054 eap->nextcmd = p;
4055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004057 {
4058 int save_State = State;
4059
4060 /* Set State to avoid the cursor shape to be set to INSERT mode
4061 * when getline() returns. */
4062 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 theline = eap->getline(
4064#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004065 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004067 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004068 State = save_State;
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004071 if (theline == NULL)
4072 break;
4073
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004074 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4075 if (ex_keep_indent)
4076 append_indent = indent;
4077
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004078 /* Look for the "." after automatic indent. */
4079 vcol = 0;
4080 for (p = theline; indent > vcol; ++p)
4081 {
4082 if (*p == ' ')
4083 ++vcol;
4084 else if (*p == TAB)
4085 vcol += 8 - vcol % 8;
4086 else
4087 break;
4088 }
4089 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004090 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4091 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 vim_free(theline);
4094 break;
4095 }
4096
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004097 /* don't use autoindent if nothing was typed. */
4098 if (p[0] == NUL)
4099 theline[0] = NUL;
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 did_undo = TRUE;
4102 ml_append(lnum, theline, (colnr_T)0, FALSE);
4103 appended_lines_mark(lnum, 1L);
4104
4105 vim_free(theline);
4106 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004107
4108 if (empty)
4109 {
4110 ml_delete(2L, FALSE);
4111 empty = FALSE;
4112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 State = NORMAL;
4115
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004116 if (eap->forceit)
4117 curbuf->b_p_ai = !curbuf->b_p_ai;
4118
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004120 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * "end" is set to lnum when something has been appended, otherwise
4122 * it is the same than "start" -- Acevedo */
4123 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4124 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4125 if (eap->cmdidx != CMD_append)
4126 --curbuf->b_op_start.lnum;
4127 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4128 ? lnum : curbuf->b_op_start.lnum;
4129 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4130 curwin->w_cursor.lnum = lnum;
4131 check_cursor_lnum();
4132 beginline(BL_SOL | BL_FIX);
4133
4134 need_wait_return = FALSE; /* don't use wait_return() now */
4135 ex_no_reprint = TRUE;
4136}
4137
4138/*
4139 * ":change"
4140 */
4141 void
4142ex_change(eap)
4143 exarg_T *eap;
4144{
4145 linenr_T lnum;
4146
4147 if (eap->line2 >= eap->line1
4148 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4149 return;
4150
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004151 /* the ! flag toggles autoindent */
4152 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4153 append_indent = get_indent_lnum(eap->line1);
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4156 {
4157 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4158 break;
4159 ml_delete(eap->line1, FALSE);
4160 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004161
4162 /* make sure the cursor is not beyond the end of the file now */
4163 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4165
4166 /* ":append" on the line above the deleted lines. */
4167 eap->line2 = eap->line1;
4168 ex_append(eap);
4169}
4170
4171 void
4172ex_z(eap)
4173 exarg_T *eap;
4174{
4175 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004176 int bigness;
4177 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 int minus = 0;
4179 linenr_T start, end, curs, i;
4180 int j;
4181 linenr_T lnum = eap->line2;
4182
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004183 /* Vi compatible: ":z!" uses display height, without a count uses
4184 * 'scroll' */
4185 if (eap->forceit)
4186 bigness = curwin->w_height;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004187#ifdef FEAT_WINDOWS
Bram Moolenaar631abc32014-02-22 22:27:47 +01004188 else if (firstwin != lastwin)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004189 bigness = curwin->w_height - 3;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004190#endif
Bram Moolenaar631abc32014-02-22 22:27:47 +01004191 else
4192 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (bigness < 1)
4194 bigness = 1;
4195
4196 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004197 kind = x;
4198 if (*kind == '-' || *kind == '+' || *kind == '='
4199 || *kind == '^' || *kind == '.')
4200 ++x;
4201 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 ++x;
4203
4204 if (*x != 0)
4205 {
4206 if (!VIM_ISDIGIT(*x))
4207 {
4208 EMSG(_("E144: non-numeric argument to :z"));
4209 return;
4210 }
4211 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004214 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004215 if (*kind == '=')
4216 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004220 /* the number of '-' and '+' multiplies the distance */
4221 if (*kind == '-' || *kind == '+')
4222 for (x = kind + 1; *x == *kind; ++x)
4223 ;
4224
4225 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004228 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4229 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004230 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 break;
4232
4233 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004234 start = lnum - (bigness + 1) / 2 + 1;
4235 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 curs = lnum;
4237 minus = 1;
4238 break;
4239
4240 case '^':
4241 start = lnum - bigness * 2;
4242 end = lnum - bigness;
4243 curs = lnum - bigness;
4244 break;
4245
4246 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004247 start = lnum - (bigness + 1) / 2 + 1;
4248 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 curs = end;
4250 break;
4251
4252 default: /* '+' */
4253 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004254 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004255 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004256 else if (eap->addr_count == 0)
4257 ++start;
4258 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 curs = end;
4260 break;
4261 }
4262
4263 if (start < 1)
4264 start = 1;
4265
4266 if (end > curbuf->b_ml.ml_line_count)
4267 end = curbuf->b_ml.ml_line_count;
4268
4269 if (curs > curbuf->b_ml.ml_line_count)
4270 curs = curbuf->b_ml.ml_line_count;
4271
4272 for (i = start; i <= end; i++)
4273 {
4274 if (minus && i == lnum)
4275 {
4276 msg_putchar('\n');
4277
4278 for (j = 1; j < Columns; j++)
4279 msg_putchar('-');
4280 }
4281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004282 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
4284 if (minus && i == lnum)
4285 {
4286 msg_putchar('\n');
4287
4288 for (j = 1; j < Columns; j++)
4289 msg_putchar('-');
4290 }
4291 }
4292
4293 curwin->w_cursor.lnum = curs;
4294 ex_no_reprint = TRUE;
4295}
4296
4297/*
4298 * Check if the restricted flag is set.
4299 * If so, give an error message and return TRUE.
4300 * Otherwise, return FALSE.
4301 */
4302 int
4303check_restricted()
4304{
4305 if (restricted)
4306 {
4307 EMSG(_("E145: Shell commands not allowed in rvim"));
4308 return TRUE;
4309 }
4310 return FALSE;
4311}
4312
4313/*
4314 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4315 * If so, give an error message and return TRUE.
4316 * Otherwise, return FALSE.
4317 */
4318 int
4319check_secure()
4320{
4321 if (secure)
4322 {
4323 secure = 2;
4324 EMSG(_(e_curdir));
4325 return TRUE;
4326 }
4327#ifdef HAVE_SANDBOX
4328 /*
4329 * In the sandbox more things are not allowed, including the things
4330 * disallowed in secure mode.
4331 */
4332 if (sandbox != 0)
4333 {
4334 EMSG(_(e_sandbox));
4335 return TRUE;
4336 }
4337#endif
4338 return FALSE;
4339}
4340
4341static char_u *old_sub = NULL; /* previous substitute pattern */
4342static int global_need_beginline; /* call beginline() after ":g" */
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344/* do_sub()
4345 *
4346 * Perform a substitution from line eap->line1 to line eap->line2 using the
4347 * command pointed to by eap->arg which should be of the form:
4348 *
4349 * /pattern/substitution/{flags}
4350 *
4351 * The usual escapes are supported as described in the regexp docs.
4352 */
4353 void
4354do_sub(eap)
4355 exarg_T *eap;
4356{
4357 linenr_T lnum;
4358 long i = 0;
4359 regmmatch_T regmatch;
4360 static int do_all = FALSE; /* do multiple substitutions per line */
4361 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004362 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 static int do_error = TRUE; /* if false, ignore errors */
4364 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004365 static int do_list = FALSE; /* list last line with subs. */
4366 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 static int do_ic = 0; /* ignore case flag */
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004368 int save_do_all; /* remember user specified 'g' flag */
4369 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4371 int delimiter;
4372 int sublen;
4373 int got_quit = FALSE;
4374 int got_match = FALSE;
4375 int temp;
4376 int which_pat;
4377 char_u *cmd;
4378 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004379 linenr_T first_line = 0; /* first changed line */
4380 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 * change */
4382 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4383 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004384 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004385 char_u *sub_firstline; /* allocated copy of first sub line */
4386 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004387 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004388 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004389#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004390 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 cmd = eap->arg;
4394 if (!global_busy)
4395 {
4396 sub_nsubs = 0;
4397 sub_nlines = 0;
4398 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004399 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 if (eap->cmdidx == CMD_tilde)
4402 which_pat = RE_LAST; /* use last used regexp */
4403 else
4404 which_pat = RE_SUBST; /* use last substitute regexp */
4405
4406 /* new pattern and substitution */
4407 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4408 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4409 {
4410 /* don't accept alphanumeric for separator */
4411 if (isalpha(*cmd))
4412 {
4413 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4414 return;
4415 }
4416 /*
4417 * undocumented vi feature:
4418 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4419 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4420 */
4421 if (*cmd == '\\')
4422 {
4423 ++cmd;
4424 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4425 {
4426 EMSG(_(e_backslash));
4427 return;
4428 }
4429 if (*cmd != '&')
4430 which_pat = RE_SEARCH; /* use last '/' pattern */
4431 pat = (char_u *)""; /* empty search pattern */
4432 delimiter = *cmd++; /* remember delimiter character */
4433 }
4434 else /* find the end of the regexp */
4435 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004436#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4437 if (p_altkeymap && curwin->w_p_rl)
4438 lrF_sub(cmd);
4439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 which_pat = RE_LAST; /* use last used regexp */
4441 delimiter = *cmd++; /* remember delimiter character */
4442 pat = cmd; /* remember start of search pat */
4443 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4444 if (cmd[0] == delimiter) /* end delimiter found */
4445 *cmd++ = NUL; /* replace it with a NUL */
4446 }
4447
4448 /*
4449 * Small incompatibility: vi sees '\n' as end of the command, but in
4450 * Vim we want to use '\n' to find/substitute a NUL.
4451 */
4452 sub = cmd; /* remember the start of the substitution */
4453
4454 while (cmd[0])
4455 {
4456 if (cmd[0] == delimiter) /* end delimiter found */
4457 {
4458 *cmd++ = NUL; /* replace it with a NUL */
4459 break;
4460 }
4461 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4462 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004463 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465
4466 if (!eap->skip)
4467 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004468 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4469 if (STRCMP(sub, "%") == 0
4470 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4471 {
4472 if (old_sub == NULL) /* there is no previous command */
4473 {
4474 EMSG(_(e_nopresub));
4475 return;
4476 }
4477 sub = old_sub;
4478 }
4479 else
4480 {
4481 vim_free(old_sub);
4482 old_sub = vim_strsave(sub);
4483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 }
4486 else if (!eap->skip) /* use previous pattern and substitution */
4487 {
4488 if (old_sub == NULL) /* there is no previous command */
4489 {
4490 EMSG(_(e_nopresub));
4491 return;
4492 }
4493 pat = NULL; /* search_regcomp() will use previous pattern */
4494 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004495
4496 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4497 * last column after using "$". */
4498 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004501 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4502 * more efficient.
4503 * TODO: find a generic solution to make line-joining operations more
4504 * efficient, avoid allocating a string that grows in size.
4505 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004506 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004507 && *sub == NUL
4508 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4509 || *cmd == 'p' || *cmd == '#'))))
4510 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004511 linenr_T joined_lines_count;
4512
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004513 curwin->w_cursor.lnum = eap->line1;
4514 if (*cmd == 'l')
4515 eap->flags = EXFLAG_LIST;
4516 else if (*cmd == '#')
4517 eap->flags = EXFLAG_NR;
4518 else if (*cmd == 'p')
4519 eap->flags = EXFLAG_PRINT;
4520
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004521 /* The number of lines joined is the number of lines in the range plus
4522 * one. One less when the last line is included. */
4523 joined_lines_count = eap->line2 - eap->line1 + 1;
4524 if (eap->line2 < curbuf->b_ml.ml_line_count)
4525 ++joined_lines_count;
4526 if (joined_lines_count > 1)
4527 {
4528 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
4529 sub_nsubs = joined_lines_count - 1;
4530 sub_nlines = 1;
4531 (void)do_sub_msg(FALSE);
4532 ex_may_print(eap);
4533 }
4534
4535 if (!cmdmod.keeppatterns)
4536 save_re_pat(RE_SUBST, pat, p_magic);
4537#ifdef FEAT_CMDHIST
4538 /* put pattern in history */
4539 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
4540#endif
4541
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004542 return;
4543 }
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 /*
4546 * Find trailing options. When '&' is used, keep old options.
4547 */
4548 if (*cmd == '&')
4549 ++cmd;
4550 else
4551 {
4552 if (!p_ed)
4553 {
4554 if (p_gd) /* default is global on */
4555 do_all = TRUE;
4556 else
4557 do_all = FALSE;
4558 do_ask = FALSE;
4559 }
4560 do_error = TRUE;
4561 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004562 do_count = FALSE;
Bram Moolenaarfe40d1a2007-07-24 09:16:38 +00004563 do_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 do_ic = 0;
4565 }
4566 while (*cmd)
4567 {
4568 /*
4569 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4570 * 'r' is never inverted.
4571 */
4572 if (*cmd == 'g')
4573 do_all = !do_all;
4574 else if (*cmd == 'c')
4575 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004576 else if (*cmd == 'n')
4577 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else if (*cmd == 'e')
4579 do_error = !do_error;
4580 else if (*cmd == 'r') /* use last used regexp */
4581 which_pat = RE_LAST;
4582 else if (*cmd == 'p')
4583 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004584 else if (*cmd == '#')
4585 {
4586 do_print = TRUE;
4587 do_number = TRUE;
4588 }
4589 else if (*cmd == 'l')
4590 {
4591 do_print = TRUE;
4592 do_list = TRUE;
4593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 else if (*cmd == 'i') /* ignore case */
4595 do_ic = 'i';
4596 else if (*cmd == 'I') /* don't ignore case */
4597 do_ic = 'I';
4598 else
4599 break;
4600 ++cmd;
4601 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004602 if (do_count)
4603 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004605 save_do_all = do_all;
4606 save_do_ask = do_ask;
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /*
4609 * check for a trailing count
4610 */
4611 cmd = skipwhite(cmd);
4612 if (VIM_ISDIGIT(*cmd))
4613 {
4614 i = getdigits(&cmd);
4615 if (i <= 0 && !eap->skip && do_error)
4616 {
4617 EMSG(_(e_zerocount));
4618 return;
4619 }
4620 eap->line1 = eap->line2;
4621 eap->line2 += i - 1;
4622 if (eap->line2 > curbuf->b_ml.ml_line_count)
4623 eap->line2 = curbuf->b_ml.ml_line_count;
4624 }
4625
4626 /*
4627 * check for trailing command or garbage
4628 */
4629 cmd = skipwhite(cmd);
4630 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4631 {
4632 eap->nextcmd = check_nextcmd(cmd);
4633 if (eap->nextcmd == NULL)
4634 {
4635 EMSG(_(e_trailing));
4636 return;
4637 }
4638 }
4639
4640 if (eap->skip) /* not executing commands, only parsing */
4641 return;
4642
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004643 if (!do_count && !curbuf->b_p_ma)
4644 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004645 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004646 EMSG(_(e_modifiable));
4647 return;
4648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4651 {
4652 if (do_error)
4653 EMSG(_(e_invcmd));
4654 return;
4655 }
4656
4657 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4658 if (do_ic == 'i')
4659 regmatch.rmm_ic = TRUE;
4660 else if (do_ic == 'I')
4661 regmatch.rmm_ic = FALSE;
4662
4663 sub_firstline = NULL;
4664
4665 /*
4666 * ~ in the substitute pattern is replaced with the old pattern.
4667 * We do it here once to avoid it to be replaced over and over again.
4668 * But don't do it when it starts with "\=", then it's an expression.
4669 */
4670 if (!(sub[0] == '\\' && sub[1] == '='))
4671 sub = regtilde(sub, p_magic);
4672
4673 /*
4674 * Check for a match on each line.
4675 */
4676 line2 = eap->line2;
4677 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4678#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4679 || aborting()
4680#endif
4681 ); ++lnum)
4682 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004683 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
4684 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (nmatch)
4686 {
4687 colnr_T copycol;
4688 colnr_T matchcol;
4689 colnr_T prev_matchcol = MAXCOL;
4690 char_u *new_end, *new_start = NULL;
4691 unsigned new_start_len = 0;
4692 char_u *p1;
4693 int did_sub = FALSE;
4694 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004695 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 long nmatch_tl = 0; /* nr of lines matched below lnum */
4697 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004698 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004699 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 /*
4702 * The new text is build up step by step, to avoid too much
4703 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00004704 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 * copycol Column in the old text where we started
4706 * looking for a match; from here old text still
4707 * needs to be copied to the new text.
4708 * matchcol Column number of the old text where to look
4709 * for the next match. It's just after the
4710 * previous match or one further.
4711 * prev_matchcol Column just after the previous match (if any).
4712 * Mostly equal to matchcol, except for the first
4713 * match and after skipping an empty match.
4714 * regmatch.*pos Where the pattern matched in the old text.
4715 * new_start The new text, all that has been produced so
4716 * far.
4717 * new_end The new text, where to append new text.
4718 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004719 * lnum The line number where we found the start of
4720 * the match. Can be below the line we searched
4721 * when there is a \n before a \zs in the
4722 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 * sub_firstlnum The line number in the buffer where to look
4724 * for a match. Can be different from "lnum"
4725 * when the pattern or substitute string contains
4726 * line breaks.
4727 *
4728 * Special situations:
4729 * - When the substitute string contains a line break, the part up
4730 * to the line break is inserted in the text, but the copy of
4731 * the original line is kept. "sub_firstlnum" is adjusted for
4732 * the inserted lines.
4733 * - When the matched pattern contains a line break, the old line
4734 * is taken from the line at the end of the pattern. The lines
4735 * in the match are deleted later, "sub_firstlnum" is adjusted
4736 * accordingly.
4737 *
4738 * The new text is built up in new_start[]. It has some extra
4739 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004740 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 *
4742 * Make a copy of the old line, so it won't be taken away when
4743 * updating the screen or handling a multi-line match. The "old_"
4744 * pointers point into this copy.
4745 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004746 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 copycol = 0;
4748 matchcol = 0;
4749
4750 /* At first match, remember current cursor position. */
4751 if (!got_match)
4752 {
4753 setpcmark();
4754 got_match = TRUE;
4755 }
4756
4757 /*
4758 * Loop until nothing more to replace in this line.
4759 * 1. Handle match with empty string.
4760 * 2. If do_ask is set, ask for confirmation.
4761 * 3. substitute the string.
4762 * 4. if do_all is set, find next match
4763 * 5. break if there isn't another match in this line
4764 */
4765 for (;;)
4766 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004767 /* Advance "lnum" to the line where the match starts. The
4768 * match does not start in the first line when there is a line
4769 * break before \zs. */
4770 if (regmatch.startpos[0].lnum > 0)
4771 {
4772 lnum += regmatch.startpos[0].lnum;
4773 sub_firstlnum += regmatch.startpos[0].lnum;
4774 nmatch -= regmatch.startpos[0].lnum;
4775 vim_free(sub_firstline);
4776 sub_firstline = NULL;
4777 }
4778
4779 if (sub_firstline == NULL)
4780 {
4781 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4782 if (sub_firstline == NULL)
4783 {
4784 vim_free(new_start);
4785 goto outofmem;
4786 }
4787 }
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 /* Save the line number of the last change for the final
4790 * cursor position (just like Vi). */
4791 curwin->w_cursor.lnum = lnum;
4792 do_again = FALSE;
4793
4794 /*
4795 * 1. Match empty string does not count, except for first
4796 * match. This reproduces the strange vi behaviour.
4797 * This also catches endless loops.
4798 */
4799 if (matchcol == prev_matchcol
4800 && regmatch.endpos[0].lnum == 0
4801 && matchcol == regmatch.endpos[0].col)
4802 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004803 if (sub_firstline[matchcol] == NUL)
4804 /* We already were at the end of the line. Don't look
4805 * for a match in this line again. */
4806 skip_match = TRUE;
4807 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02004808 {
4809 /* search for a match at next column */
4810#ifdef FEAT_MBYTE
4811 if (has_mbyte)
4812 matchcol += mb_ptr2len(sub_firstline + matchcol);
4813 else
4814#endif
4815 ++matchcol;
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 goto skip;
4818 }
4819
4820 /* Normally we continue searching for a match just after the
4821 * previous match. */
4822 matchcol = regmatch.endpos[0].col;
4823 prev_matchcol = matchcol;
4824
4825 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004826 * 2. If do_count is set only increase the counter.
4827 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004829 if (do_count)
4830 {
4831 /* For a multi-line match, put matchcol at the NUL at
4832 * the end of the line and set nmatch to one, so that
4833 * we continue looking for a match on the next line.
4834 * Avoids that ":s/\nB\@=//gc" get stuck. */
4835 if (nmatch > 1)
4836 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004837 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004838 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00004839 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004840 }
4841 sub_nsubs++;
4842 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004843#ifdef FEAT_EVAL
4844 /* Skip the substitution, unless an expression is used,
4845 * then it is evaluated in the sandbox. */
4846 if (!(sub[0] == '\\' && sub[1] == '='))
4847#endif
4848 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004849 }
4850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 if (do_ask)
4852 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00004853 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /* change State to CONFIRM, so that the mouse works
4856 * properly */
4857 save_State = State;
4858 State = CONFIRM;
4859#ifdef FEAT_MOUSE
4860 setmouse(); /* disable mouse in xterm */
4861#endif
4862 curwin->w_cursor.col = regmatch.startpos[0].col;
4863
4864 /* When 'cpoptions' contains "u" don't sync undo when
4865 * asking for confirmation. */
4866 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4867 ++no_u_sync;
4868
4869 /*
4870 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4871 */
4872 while (do_ask)
4873 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004874 if (exmode_active)
4875 {
4876 char_u *resp;
4877 colnr_T sc, ec;
4878
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004879 print_line_no_prefix(lnum, do_number, do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004880
4881 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4882 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4883 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004884 if (do_number || curwin->w_p_nu)
4885 {
4886 int numw = number_width(curwin) + 1;
4887 sc += numw;
4888 ec += numw;
4889 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004890 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004891 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004892 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004893 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004894 msg_putchar('^');
4895
4896 resp = getexmodeline('?', NULL, 0);
4897 if (resp != NULL)
4898 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004899 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004900 vim_free(resp);
4901 }
4902 }
4903 else
4904 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004905 char_u *orig_line = NULL;
4906 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004908 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004910 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004912 /* Invert the matched string.
4913 * Remove the inversion afterwards. */
4914 temp = RedrawingDisabled;
4915 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004917 if (new_start != NULL)
4918 {
4919 /* There already was a substitution, we would
4920 * like to show this to the user. We cannot
4921 * really update the line, it would change
4922 * what matches. Temporarily replace the line
4923 * and change it back afterwards. */
4924 orig_line = vim_strsave(ml_get(lnum));
4925 if (orig_line != NULL)
4926 {
4927 char_u *new_line = concat_str(new_start,
4928 sub_firstline + copycol);
4929
4930 if (new_line == NULL)
4931 {
4932 vim_free(orig_line);
4933 orig_line = NULL;
4934 }
4935 else
4936 {
4937 /* Position the cursor relative to the
4938 * end of the line, the previous
4939 * substitute may have inserted or
4940 * deleted characters before the
4941 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01004942 len_change = (int)STRLEN(new_line)
4943 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004944 curwin->w_cursor.col += len_change;
4945 ml_replace(lnum, new_line, FALSE);
4946 }
4947 }
4948 }
4949
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004950 search_match_lines = regmatch.endpos[0].lnum
4951 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004952 search_match_endcol = regmatch.endpos[0].col
4953 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004954 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004956 update_topline();
4957 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00004958 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004959 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00004960 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004963 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004965 if (msg_row == Rows - 1)
4966 msg_didout = FALSE; /* avoid a scroll-up */
4967 msg_starthere();
4968 i = msg_scroll;
4969 msg_scroll = 0; /* truncate msg when
4970 needed */
4971 msg_no_more = TRUE;
4972 /* write message same highlighting as for
4973 * wait_return */
4974 smsg_attr(hl_attr(HLF_R),
4975 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4976 msg_no_more = FALSE;
4977 msg_scroll = i;
4978 showruler(TRUE);
4979 windgoto(msg_row, msg_col);
4980 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
4982#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004983 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004985 ++no_mapping; /* don't map this key */
4986 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004987 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004988 --allow_keys;
4989 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004991 /* clear the question */
4992 msg_didout = FALSE; /* don't scroll up */
4993 msg_col = 0;
4994 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004995
4996 /* restore the line */
4997 if (orig_line != NULL)
4998 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004999 }
5000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005002 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005004 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#endif
5006 )
5007 {
5008 got_quit = TRUE;
5009 break;
5010 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005011 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005013 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005015 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 /* last: replace and then stop */
5018 do_all = FALSE;
5019 line2 = lnum;
5020 break;
5021 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005022 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 do_ask = FALSE;
5025 break;
5026 }
5027#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005028 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005030 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 scrolldown_clamp();
5032#endif
5033 }
5034 State = save_State;
5035#ifdef FEAT_MOUSE
5036 setmouse();
5037#endif
5038 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
5039 --no_u_sync;
5040
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005041 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 /* For a multi-line match, put matchcol at the NUL at
5044 * the end of the line and set nmatch to one, so that
5045 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005046 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5047 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (nmatch > 1)
5049 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005050 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005051 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 goto skip;
5054 }
5055 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005056 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058
5059 /* Move the cursor to the start of the match, so that we can
5060 * use "\=col("."). */
5061 curwin->w_cursor.col = regmatch.startpos[0].col;
5062
5063 /*
5064 * 3. substitute the string.
5065 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005066#ifdef FEAT_EVAL
5067 if (do_count)
5068 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005069 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005070 save_ma = curbuf->b_p_ma;
5071 curbuf->b_p_ma = FALSE;
5072 sandbox++;
5073 }
5074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005076 sublen = vim_regsub_multi(&regmatch,
5077 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005079#ifdef FEAT_EVAL
5080 if (do_count)
5081 {
5082 curbuf->b_p_ma = save_ma;
5083 sandbox--;
5084 goto skip;
5085 }
5086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
Bram Moolenaar4463f292005-09-25 22:20:24 +00005088 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005089 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005090 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5091 {
5092 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5093 skip_match = TRUE;
5094 }
5095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /* Need room for:
5097 * - result so far in new_start (not for first sub in line)
5098 * - original text up to match
5099 * - length of substituted part
5100 * - original text after match
5101 */
5102 if (nmatch == 1)
5103 p1 = sub_firstline;
5104 else
5105 {
5106 p1 = ml_get(sub_firstlnum + nmatch - 1);
5107 nmatch_tl += nmatch - 1;
5108 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005109 copy_len = regmatch.startpos[0].col - copycol;
5110 needed_len = copy_len + ((unsigned)STRLEN(p1)
5111 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (new_start == NULL)
5113 {
5114 /*
5115 * Get some space for a temporary buffer to do the
5116 * substitution into (and some extra space to avoid
5117 * too many calls to alloc()/free()).
5118 */
5119 new_start_len = needed_len + 50;
5120 if ((new_start = alloc_check(new_start_len)) == NULL)
5121 goto outofmem;
5122 *new_start = NUL;
5123 new_end = new_start;
5124 }
5125 else
5126 {
5127 /*
5128 * Check if the temporary buffer is long enough to do the
5129 * substitution into. If not, make it larger (with a bit
5130 * extra to avoid too many calls to alloc()/free()).
5131 */
5132 len = (unsigned)STRLEN(new_start);
5133 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005134 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 new_start_len = needed_len + 50;
5137 if ((p1 = alloc_check(new_start_len)) == NULL)
5138 {
5139 vim_free(new_start);
5140 goto outofmem;
5141 }
5142 mch_memmove(p1, new_start, (size_t)(len + 1));
5143 vim_free(new_start);
5144 new_start = p1;
5145 }
5146 new_end = new_start + len;
5147 }
5148
5149 /*
5150 * copy the text up to the part that matched
5151 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005152 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5153 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005155 (void)vim_regsub_multi(&regmatch,
5156 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 sub, new_end, TRUE, p_magic, TRUE);
5158 sub_nsubs++;
5159 did_sub = TRUE;
5160
5161 /* Move the cursor to the start of the line, to avoid that it
5162 * is beyond the end of the line after the substitution. */
5163 curwin->w_cursor.col = 0;
5164
5165 /* For a multi-line match, make a copy of the last matched
5166 * line and continue in that one. */
5167 if (nmatch > 1)
5168 {
5169 sub_firstlnum += nmatch - 1;
5170 vim_free(sub_firstline);
5171 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5172 /* When going beyond the last line, stop substituting. */
5173 if (sub_firstlnum <= line2)
5174 do_again = TRUE;
5175 else
5176 do_all = FALSE;
5177 }
5178
5179 /* Remember next character to be copied. */
5180 copycol = regmatch.endpos[0].col;
5181
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005182 if (skip_match)
5183 {
5184 /* Already hit end of the buffer, sub_firstlnum is one
5185 * less than what it ought to be. */
5186 vim_free(sub_firstline);
5187 sub_firstline = vim_strsave((char_u *)"");
5188 copycol = 0;
5189 }
5190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 /*
5192 * Now the trick is to replace CTRL-M chars with a real line
5193 * break. This would make it impossible to insert a CTRL-M in
5194 * the text. The line break can be avoided by preceding the
5195 * CTRL-M with a backslash. To be able to insert a backslash,
5196 * they must be doubled in the string and are halved here.
5197 * That is Vi compatible.
5198 */
5199 for (p1 = new_end; *p1; ++p1)
5200 {
5201 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else if (*p1 == CAR)
5204 {
5205 if (u_inssub(lnum) == OK) /* prepare for undo */
5206 {
5207 *p1 = NUL; /* truncate up to the CR */
5208 ml_append(lnum - 1, new_start,
5209 (colnr_T)(p1 - new_start + 1), FALSE);
5210 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
5211 if (do_ask)
5212 appended_lines(lnum - 1, 1L);
5213 else
5214 {
5215 if (first_line == 0)
5216 first_line = lnum;
5217 last_line = lnum + 1;
5218 }
5219 /* All line numbers increase. */
5220 ++sub_firstlnum;
5221 ++lnum;
5222 ++line2;
5223 /* move the cursor to the new line, like Vi */
5224 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005225 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 p1 = new_start - 1;
5228 }
5229 }
5230#ifdef FEAT_MBYTE
5231 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005232 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#endif
5234 }
5235
5236 /*
5237 * 4. If do_all is set, find next match.
5238 * Prevent endless loop with patterns that match empty
5239 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5240 * But ":s/\n/#/" is OK.
5241 */
5242skip:
5243 /* We already know that we did the last subst when we are at
5244 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005245 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5246 * "line2" when there is a \zs in the pattern after a line
5247 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005248 lastone = (skip_match
5249 || got_int
5250 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005251 || lnum > line2
Bram Moolenaar8299df92004-07-10 09:47:34 +00005252 || !(do_all || do_again)
5253 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5254 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 nmatch = -1;
5256
5257 /*
5258 * Replace the line in the buffer when needed. This is
5259 * skipped when there are more matches.
5260 * The check for nmatch_tl is needed for when multi-line
5261 * matching must replace the lines before trying to do another
5262 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005263 * When the match starts below where we start searching also
5264 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 */
5266 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 || nmatch_tl > 0
5268 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005269 curbuf, sub_firstlnum,
5270 matchcol, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005271 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 if (new_start != NULL)
5274 {
5275 /*
5276 * Copy the rest of the line, that didn't match.
5277 * "matchcol" has to be adjusted, we use the end of
5278 * the line as reference, because the substitute may
5279 * have changed the number of characters. Same for
5280 * "prev_matchcol".
5281 */
5282 STRCAT(new_start, sub_firstline + copycol);
5283 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5284 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5285 - prev_matchcol;
5286
5287 if (u_savesub(lnum) != OK)
5288 break;
5289 ml_replace(lnum, new_start, TRUE);
5290
5291 if (nmatch_tl > 0)
5292 {
5293 /*
5294 * Matched lines have now been substituted and are
5295 * useless, delete them. The part after the match
5296 * has been appended to new_start, we don't need
5297 * it in the buffer.
5298 */
5299 ++lnum;
5300 if (u_savedel(lnum, nmatch_tl) != OK)
5301 break;
5302 for (i = 0; i < nmatch_tl; ++i)
5303 ml_delete(lnum, (int)FALSE);
5304 mark_adjust(lnum, lnum + nmatch_tl - 1,
5305 (long)MAXLNUM, -nmatch_tl);
5306 if (do_ask)
5307 deleted_lines(lnum, nmatch_tl);
5308 --lnum;
5309 line2 -= nmatch_tl; /* nr of lines decreases */
5310 nmatch_tl = 0;
5311 }
5312
5313 /* When asking, undo is saved each time, must also set
5314 * changed flag each time. */
5315 if (do_ask)
5316 changed_bytes(lnum, 0);
5317 else
5318 {
5319 if (first_line == 0)
5320 first_line = lnum;
5321 last_line = lnum + 1;
5322 }
5323
5324 sub_firstlnum = lnum;
5325 vim_free(sub_firstline); /* free the temp buffer */
5326 sub_firstline = new_start;
5327 new_start = NULL;
5328 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5329 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5330 - prev_matchcol;
5331 copycol = 0;
5332 }
5333 if (nmatch == -1 && !lastone)
5334 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005335 sub_firstlnum, matchcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 /*
5338 * 5. break if there isn't another match in this line
5339 */
5340 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005341 {
5342 /* If the match found didn't start where we were
5343 * searching, do the next search in the line where we
5344 * found the match. */
5345 if (nmatch == -1)
5346 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350
5351 line_breakcheck();
5352 }
5353
5354 if (did_sub)
5355 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005356 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 vim_free(sub_firstline); /* free the copy of the original line */
5358 sub_firstline = NULL;
5359 }
5360
5361 line_breakcheck();
5362 }
5363
5364 if (first_line != 0)
5365 {
5366 /* Need to subtract the number of added lines from "last_line" to get
5367 * the line number before the change (same as adding the number of
5368 * deleted lines). */
5369 i = curbuf->b_ml.ml_line_count - old_line_count;
5370 changed_lines(first_line, 0, last_line - i, i);
5371 }
5372
5373outofmem:
5374 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005375
5376 /* ":s/pat//n" doesn't move the cursor */
5377 if (do_count)
5378 curwin->w_cursor = old_cursor;
5379
Bram Moolenaar46475522010-03-23 17:36:29 +01005380 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 /* Set the '[ and '] marks. */
5383 curbuf->b_op_start.lnum = eap->line1;
5384 curbuf->b_op_end.lnum = line2;
5385 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5386
5387 if (!global_busy)
5388 {
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005389 if (!do_ask) /* when interactive leave cursor on the match */
5390 {
5391 if (endcolumn)
5392 coladvance((colnr_T)MAXCOL);
5393 else
5394 beginline(BL_WHITE | BL_FIX);
5395 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005396 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 MSG("");
5398 }
5399 else
5400 global_need_beginline = TRUE;
5401 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005402 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404 else if (!global_busy)
5405 {
5406 if (got_int) /* interrupted */
5407 EMSG(_(e_interr));
5408 else if (got_match) /* did find something but nothing substituted */
5409 MSG("");
5410 else if (do_error) /* nothing found */
5411 EMSG2(_(e_patnotf2), get_search_pat());
5412 }
5413
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005414#ifdef FEAT_FOLDING
5415 if (do_ask && hasAnyFolding(curwin))
5416 /* Cursor position may require updating */
5417 changed_window_setting();
5418#endif
5419
Bram Moolenaar473de612013-06-08 18:19:48 +02005420 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005421
5422 /* Restore the flag values, they can be used for ":&&". */
5423 do_all = save_do_all;
5424 do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425}
5426
5427/*
5428 * Give message for number of substitutions.
5429 * Can also be used after a ":global" command.
5430 * Return TRUE if a message was given.
5431 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005432 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00005433do_sub_msg(count_only)
5434 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 /*
5437 * Only report substitutions when:
5438 * - more than 'report' substitutions
5439 * - command was typed by user, or number of changed lines > 'report'
5440 * - giving messages is not disabled by 'lazyredraw'
5441 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005442 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5443 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 && messaging())
5445 {
5446 if (got_int)
5447 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005448 else
5449 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005451 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005452 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005454 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005455 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 sub_nsubs);
5457 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005458 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005459 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005461 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005462 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005465 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 return TRUE;
5467 }
5468 if (got_int)
5469 {
5470 EMSG(_(e_interr));
5471 return TRUE;
5472 }
5473 return FALSE;
5474}
5475
5476/*
5477 * Execute a global command of the form:
5478 *
5479 * g/pattern/X : execute X on all lines where pattern matches
5480 * v/pattern/X : execute X on all lines where pattern does not match
5481 *
5482 * where 'X' is an EX command
5483 *
5484 * The command character (as well as the trailing slash) is optional, and
5485 * is assumed to be 'p' if missing.
5486 *
5487 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005488 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 * for each line that has a mark. This is required because after deleting
5490 * lines we do not know where to search for the next match.
5491 */
5492 void
5493ex_global(eap)
5494 exarg_T *eap;
5495{
5496 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005497 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 int type; /* first char of cmd: 'v' or 'g' */
5499 char_u *cmd; /* command argument */
5500
5501 char_u delim; /* delimiter, normally '/' */
5502 char_u *pat;
5503 regmmatch_T regmatch;
5504 int match;
5505 int which_pat;
5506
5507 if (global_busy)
5508 {
5509 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5510 return;
5511 }
5512
5513 if (eap->forceit) /* ":global!" is like ":vglobal" */
5514 type = 'v';
5515 else
5516 type = *eap->cmd;
5517 cmd = eap->arg;
5518 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 /*
5521 * undocumented vi feature:
5522 * "\/" and "\?": use previous search pattern.
5523 * "\&": use previous substitute pattern.
5524 */
5525 if (*cmd == '\\')
5526 {
5527 ++cmd;
5528 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5529 {
5530 EMSG(_(e_backslash));
5531 return;
5532 }
5533 if (*cmd == '&')
5534 which_pat = RE_SUBST; /* use previous substitute pattern */
5535 else
5536 which_pat = RE_SEARCH; /* use previous search pattern */
5537 ++cmd;
5538 pat = (char_u *)"";
5539 }
5540 else if (*cmd == NUL)
5541 {
5542 EMSG(_("E148: Regular expression missing from global"));
5543 return;
5544 }
5545 else
5546 {
5547 delim = *cmd; /* get the delimiter */
5548 if (delim)
5549 ++cmd; /* skip delimiter if there is one */
5550 pat = cmd; /* remember start of pattern */
5551 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5552 if (cmd[0] == delim) /* end delimiter found */
5553 *cmd++ = NUL; /* replace it with a NUL */
5554 }
5555
5556#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5557 if (p_altkeymap && curwin->w_p_rl)
5558 lrFswap(pat,0);
5559#endif
5560
5561 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5562 {
5563 EMSG(_(e_invcmd));
5564 return;
5565 }
5566
5567 /*
5568 * pass 1: set marks for each (not) matching line
5569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5571 {
5572 /* a match on this line? */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005573 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5574 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 if ((type == 'g' && match) || (type == 'v' && !match))
5576 {
5577 ml_setmarked(lnum);
5578 ndone++;
5579 }
5580 line_breakcheck();
5581 }
5582
5583 /*
5584 * pass 2: execute the command for each line that has been marked
5585 */
5586 if (got_int)
5587 MSG(_(e_interr));
5588 else if (ndone == 0)
5589 {
5590 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005591 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 else
Bram Moolenaarc389fd32013-03-07 16:08:35 +01005593 smsg((char_u *)_("Pattern not found: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005596 {
5597#ifdef FEAT_CLIPBOARD
5598 start_global_changes();
5599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005601#ifdef FEAT_CLIPBOARD
5602 end_global_changes();
5603#endif
5604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar473de612013-06-08 18:19:48 +02005607 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610/*
5611 * Execute "cmd" on lines marked with ml_setmarked().
5612 */
5613 void
5614global_exe(cmd)
5615 char_u *cmd;
5616{
Bram Moolenaarbb993222011-05-10 16:00:47 +02005617 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5618 buf_T *old_buf = curbuf; /* remember what buffer we started in */
5619 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 /*
5622 * Set current position only once for a global command.
5623 * If global_busy is set, setpcmark() will not do anything.
5624 * If there is an error, global_busy will be incremented.
5625 */
5626 setpcmark();
5627
5628 /* When the command writes a message, don't overwrite the command. */
5629 msg_didout = TRUE;
5630
Bram Moolenaard25bc232010-03-23 17:49:24 +01005631 sub_nsubs = 0;
5632 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 global_need_beginline = FALSE;
5634 global_busy = 1;
5635 old_lcount = curbuf->b_ml.ml_line_count;
5636 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5637 {
5638 curwin->w_cursor.lnum = lnum;
5639 curwin->w_cursor.col = 0;
5640 if (*cmd == NUL || *cmd == '\n')
5641 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5642 else
5643 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5644 ui_breakcheck();
5645 }
5646
5647 global_busy = 0;
5648 if (global_need_beginline)
5649 beginline(BL_WHITE | BL_FIX);
5650 else
5651 check_cursor(); /* cursor may be beyond the end of the line */
5652
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005653 /* the cursor may not have moved in the text but a change in a previous
5654 * line may move it on the screen */
5655 changed_line_abv_curs();
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 /* If it looks like no message was written, allow overwriting the
5658 * command with the report for number of changes. */
5659 if (msg_col == 0 && msg_scrolled == 0)
5660 msg_didout = FALSE;
5661
Bram Moolenaar45667512007-05-10 19:24:43 +00005662 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02005663 * number of extra or deleted lines.
5664 * Don't report extra or deleted lines in the edge case where the buffer
5665 * we are in after execution is different from the buffer we started in. */
5666 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5668}
5669
5670#ifdef FEAT_VIMINFO
5671 int
5672read_viminfo_sub_string(virp, force)
5673 vir_T *virp;
5674 int force;
5675{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005676 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 vim_free(old_sub);
5678 if (force || old_sub == NULL)
5679 old_sub = viminfo_readstring(virp, 1, TRUE);
5680 return viminfo_readline(virp);
5681}
5682
5683 void
5684write_viminfo_sub_string(fp)
5685 FILE *fp;
5686{
5687 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5688 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005689 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 viminfo_writestring(fp, old_sub);
5691 }
5692}
5693#endif /* FEAT_VIMINFO */
5694
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005695#if defined(EXITFREE) || defined(PROTO)
5696 void
5697free_old_sub()
5698{
5699 vim_free(old_sub);
5700}
5701#endif
5702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5704/*
5705 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005706 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005708 int
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005709prepare_tagpreview(undo_sync)
5710 int undo_sync; /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 win_T *wp;
5713
5714# ifdef FEAT_GUI
5715 need_mouse_correct = TRUE;
5716# endif
5717
5718 /*
5719 * If there is already a preview window open, use that one.
5720 */
5721 if (!curwin->w_p_pvw)
5722 {
5723 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5724 if (wp->w_p_pvw)
5725 break;
5726 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005727 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 else
5729 {
5730 /*
5731 * There is no preview window open yet. Create one.
5732 */
5733 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5734 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005735 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 curwin->w_p_pvw = TRUE;
5737 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005738 RESET_BINDING(curwin); /* don't take over 'scrollbind'
5739 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740# ifdef FEAT_DIFF
5741 curwin->w_p_diff = FALSE; /* no 'diff' */
5742# endif
5743# ifdef FEAT_FOLDING
5744 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5745# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005746 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005749 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750}
5751
5752#endif
5753
5754
5755/*
5756 * ":help": open a read-only window on a help file
5757 */
5758 void
5759ex_help(eap)
5760 exarg_T *eap;
5761{
5762 char_u *arg;
5763 char_u *tag;
5764 FILE *helpfd; /* file descriptor of help file */
5765 int n;
5766 int i;
5767#ifdef FEAT_WINDOWS
5768 win_T *wp;
5769#endif
5770 int num_matches;
5771 char_u **matches;
5772 char_u *p;
5773 int empty_fnum = 0;
5774 int alt_fnum = 0;
5775 buf_T *buf;
5776#ifdef FEAT_MULTI_LANG
5777 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005778 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02005780#ifdef FEAT_FOLDING
5781 int old_KeyTyped = KeyTyped;
5782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 if (eap != NULL)
5785 {
5786 /*
5787 * A ":help" command ends at the first LF, or at a '|' that is
5788 * followed by some text. Set nextcmd to the following command.
5789 */
5790 for (arg = eap->arg; *arg; ++arg)
5791 {
5792 if (*arg == '\n' || *arg == '\r'
5793 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5794 {
5795 *arg++ = NUL;
5796 eap->nextcmd = arg;
5797 break;
5798 }
5799 }
5800 arg = eap->arg;
5801
Bram Moolenaar3dbde622012-04-05 16:05:05 +02005802 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
5804 EMSG(_("E478: Don't panic!"));
5805 return;
5806 }
5807
5808 if (eap->skip) /* not executing commands */
5809 return;
5810 }
5811 else
5812 arg = (char_u *)"";
5813
5814 /* remove trailing blanks */
5815 p = arg + STRLEN(arg) - 1;
5816 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5817 *p-- = NUL;
5818
5819#ifdef FEAT_MULTI_LANG
5820 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005821 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822#endif
5823
5824 /* When no argument given go to the index. */
5825 if (*arg == NUL)
5826 arg = (char_u *)"help.txt";
5827
5828 /*
5829 * Check if there is a match for the argument.
5830 */
5831 n = find_help_tags(arg, &num_matches, &matches,
5832 eap != NULL && eap->forceit);
5833
5834 i = 0;
5835#ifdef FEAT_MULTI_LANG
5836 if (n != FAIL && lang != NULL)
5837 /* Find first item with the requested language. */
5838 for (i = 0; i < num_matches; ++i)
5839 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005840 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (len > 3 && matches[i][len - 3] == '@'
5842 && STRICMP(matches[i] + len - 2, lang) == 0)
5843 break;
5844 }
5845#endif
5846 if (i >= num_matches || n == FAIL)
5847 {
5848#ifdef FEAT_MULTI_LANG
5849 if (lang != NULL)
5850 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5851 else
5852#endif
5853 EMSG2(_("E149: Sorry, no help for %s"), arg);
5854 if (n != FAIL)
5855 FreeWild(num_matches, matches);
5856 return;
5857 }
5858
5859 /* The first match (in the requested language) is the best match. */
5860 tag = vim_strsave(matches[i]);
5861 FreeWild(num_matches, matches);
5862
5863#ifdef FEAT_GUI
5864 need_mouse_correct = TRUE;
5865#endif
5866
5867 /*
5868 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005869 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005871 if (!curwin->w_buffer->b_help
5872#ifdef FEAT_WINDOWS
5873 || cmdmod.tab != 0
5874#endif
5875 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
5877#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005878 if (cmdmod.tab != 0)
5879 wp = NULL;
5880 else
5881 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5882 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5885 win_enter(wp, TRUE);
5886 else
5887#endif
5888 {
5889 /*
5890 * There is no help window yet.
5891 * Try to open the file specified by the "helpfile" option.
5892 */
5893 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5894 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005895 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 goto erret;
5897 }
5898 fclose(helpfd);
5899
5900#ifdef FEAT_WINDOWS
5901 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005902 * specified, the current window is vertically split and
5903 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 n = WSP_HELP;
5905# ifdef FEAT_VERTSPLIT
5906 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005907 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 n |= WSP_TOP;
5909# endif
5910 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005911 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912#else
5913 /* use current window */
5914 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918#ifdef FEAT_WINDOWS
5919 if (curwin->w_height < p_hh)
5920 win_setheight((int)p_hh);
5921#endif
5922
5923 /*
5924 * Open help file (do_ecmd() will set b_help flag, readfile() will
5925 * set b_p_ro flag).
5926 * Set the alternate file to the previously edited file.
5927 */
5928 alt_fnum = curbuf->b_fnum;
5929 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005930 ECMD_HIDE + ECMD_SET_HELP,
5931#ifdef FEAT_WINDOWS
5932 NULL /* buffer is still open, don't store info */
5933#else
5934 curwin
5935#endif
5936 );
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005937 if (!cmdmod.keepalt)
5938 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 empty_fnum = curbuf->b_fnum;
5940 }
5941 }
5942
5943 if (!p_im)
5944 restart_edit = 0; /* don't want insert mode in help file */
5945
Bram Moolenaar8f535582011-09-30 17:30:31 +02005946#ifdef FEAT_FOLDING
5947 /* Restore KeyTyped, setting 'filetype=help' may reset it.
5948 * It is needed for do_tag top open folds under the cursor. */
5949 KeyTyped = old_KeyTyped;
5950#endif
5951
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (tag != NULL)
5953 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5954
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005955 /* Delete the empty buffer if we're not using it. Careful: autocommands
5956 * may have jumped to another window, check that the buffer is not in a
5957 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5959 {
5960 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005961 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 wipe_buffer(buf, TRUE);
5963 }
5964
5965 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005966 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 curwin->w_alt_fnum = alt_fnum;
5968
5969erret:
5970 vim_free(tag);
5971}
5972
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005973/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005974 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005975 */
5976 void
5977ex_helpclose(eap)
5978 exarg_T *eap UNUSED;
5979{
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005980#if defined(FEAT_WINDOWS)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005981 win_T *win;
5982
5983 FOR_ALL_WINDOWS(win)
5984 {
5985 if (win->w_buffer->b_help)
5986 {
5987 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005988 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005989 }
5990 }
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005991#endif
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005992}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005994#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5995/*
5996 * In an argument search for a language specifiers in the form "@xx".
5997 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5998 * Returns NULL if not found.
5999 */
6000 char_u *
6001check_help_lang(arg)
6002 char_u *arg;
6003{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006004 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006005
6006 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
6007 && ASCII_ISALPHA(arg[len - 1]))
6008 {
6009 arg[len - 3] = NUL; /* remove the '@' */
6010 return arg + len - 2;
6011 }
6012 return NULL;
6013}
6014#endif
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016/*
6017 * Return a heuristic indicating how well the given string matches. The
6018 * smaller the number, the better the match. This is the order of priorities,
6019 * from best match to worst match:
6020 * - Match with least alpha-numeric characters is better.
6021 * - Match with least total characters is better.
6022 * - Match towards the start is better.
6023 * - Match starting with "+" is worse (feature instead of command)
6024 * Assumption is made that the matched_string passed has already been found to
6025 * match some string for which help is requested. webb.
6026 */
6027 int
6028help_heuristic(matched_string, offset, wrong_case)
6029 char_u *matched_string;
6030 int offset; /* offset for match */
6031 int wrong_case; /* no matching case */
6032{
6033 int num_letters;
6034 char_u *p;
6035
6036 num_letters = 0;
6037 for (p = matched_string; *p; p++)
6038 if (ASCII_ISALNUM(*p))
6039 num_letters++;
6040
6041 /*
6042 * Multiply the number of letters by 100 to give it a much bigger
6043 * weighting than the number of characters.
6044 * If there only is a match while ignoring case, add 5000.
6045 * If the match starts in the middle of a word, add 10000 to put it
6046 * somewhere in the last half.
6047 * If the match is more than 2 chars from the start, multiply by 200 to
6048 * put it after matches at the start.
6049 */
6050 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6051 && ASCII_ISALNUM(matched_string[offset - 1]))
6052 offset += 10000;
6053 else if (offset > 2)
6054 offset *= 200;
6055 if (wrong_case)
6056 offset += 5000;
6057 /* Features are less interesting than the subjects themselves, but "+"
6058 * alone is not a feature. */
6059 if (matched_string[0] == '+' && matched_string[1] != NUL)
6060 offset += 100;
6061 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6062}
6063
6064/*
6065 * Compare functions for qsort() below, that checks the help heuristics number
6066 * that has been put after the tagname by find_tags().
6067 */
6068 static int
6069#ifdef __BORLANDC__
6070_RTLENTRYF
6071#endif
6072help_compare(s1, s2)
6073 const void *s1;
6074 const void *s2;
6075{
6076 char *p1;
6077 char *p2;
6078
6079 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6080 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6081 return strcmp(p1, p2);
6082}
6083
6084/*
6085 * Find all help tags matching "arg", sort them and return in matches[], with
6086 * the number of matches in num_matches.
6087 * The matches will be sorted with a "best" match algorithm.
6088 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6089 */
6090 int
6091find_help_tags(arg, num_matches, matches, keep_lang)
6092 char_u *arg;
6093 int *num_matches;
6094 char_u ***matches;
6095 int keep_lang;
6096{
6097 char_u *s, *d;
6098 int i;
6099 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006100 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006101 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006102 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006103 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 "[count]", "[quotex]", "[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006105 "[pattern]", "\\|", "\\%$",
6106 "s/\\~", "s/\\U", "s/\\L",
6107 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006109 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006110 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006111 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006112 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 "\\[count]", "\\[quotex]", "\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006114 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006115 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006116 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 int flags;
6118
6119 d = IObuff; /* assume IObuff is long enough! */
6120
6121 /*
6122 * Recognize a few exceptions to the rule. Some strings that contain '*'
6123 * with "star". Otherwise '*' is recognized as a wildcard.
6124 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006125 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (STRCMP(arg, mtable[i]) == 0)
6127 {
6128 STRCPY(d, rtable[i]);
6129 break;
6130 }
6131
6132 if (i < 0) /* no match in table */
6133 {
6134 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6135 * Also replace "\%^" and "\%(", they match every tag too.
6136 * Also "\zs", "\z1", etc.
6137 * Also "\@<", "\@=", "\@<=", etc.
6138 * And also "\_$" and "\_^". */
6139 if (arg[0] == '\\'
6140 && ((arg[1] != NUL && arg[2] == NUL)
6141 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6142 && arg[2] != NUL)))
6143 {
6144 STRCPY(d, "/\\\\");
6145 STRCPY(d + 3, arg + 1);
6146 /* Check for "/\\_$", should be "/\\_\$" */
6147 if (d[3] == '_' && d[4] == '$')
6148 STRCPY(d + 4, "\\$");
6149 }
6150 else
6151 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006152 /* Replace:
6153 * "[:...:]" with "\[:...:]"
6154 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006155 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006156 */
6157 if ((arg[0] == '[' && (arg[1] == ':'
6158 || (arg[1] == '+' && arg[2] == '+')))
6159 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 *d++ = '\\';
6161
6162 for (s = arg; *s; ++s)
6163 {
6164 /*
6165 * Replace "|" with "bar" and '"' with "quote" to match the name of
6166 * the tags for these commands.
6167 * Replace "*" with ".*" and "?" with "." to match command line
6168 * completion.
6169 * Insert a backslash before '~', '$' and '.' to avoid their
6170 * special meaning.
6171 */
6172 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6173 break;
6174 switch (*s)
6175 {
6176 case '|': STRCPY(d, "bar");
6177 d += 3;
6178 continue;
6179 case '"': STRCPY(d, "quote");
6180 d += 5;
6181 continue;
6182 case '*': *d++ = '.';
6183 break;
6184 case '?': *d++ = '.';
6185 continue;
6186 case '$':
6187 case '.':
6188 case '~': *d++ = '\\';
6189 break;
6190 }
6191
6192 /*
6193 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6194 * ":help i_^_CTRL-D" work.
6195 * Insert '-' before and after "CTRL-X" when applicable.
6196 */
6197 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6198 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6199 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006200 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6201 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 STRCPY(d, "CTRL-");
6203 d += 5;
6204 if (*s < ' ')
6205 {
6206#ifdef EBCDIC
6207 *d++ = CtrlChar(*s);
6208#else
6209 *d++ = *s + '@';
6210#endif
6211 if (d[-1] == '\\')
6212 *d++ = '\\'; /* double a backslash */
6213 }
6214 else
6215 *d++ = *++s;
6216 if (s[1] != NUL && s[1] != '_')
6217 *d++ = '_'; /* append a '_' */
6218 continue;
6219 }
6220 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6221 *d++ = '\\';
6222
6223 /*
6224 * Insert a backslash before a backslash after a slash, for search
6225 * pattern tags: "/\|" --> "/\\|".
6226 */
6227 else if (s[0] == '\\' && s[1] != '\\'
6228 && *arg == '/' && s == arg + 1)
6229 *d++ = '\\';
6230
6231 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6232 * "CTRL-\_CTRL-N" */
6233 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6234 {
6235 STRCPY(d, "CTRL-\\\\");
6236 d += 7;
6237 s += 6;
6238 }
6239
6240 *d++ = *s;
6241
6242 /*
6243 * If tag starts with ', toss everything after a second '. Fixes
6244 * CTRL-] on 'option'. (would include the trailing '.').
6245 */
6246 if (*s == '\'' && s > arg && *arg == '\'')
6247 break;
6248 }
6249 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006250
6251 if (*IObuff == '`')
6252 {
6253 if (d > IObuff + 2 && d[-1] == '`')
6254 {
6255 /* remove the backticks from `command` */
6256 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6257 d[-2] = NUL;
6258 }
6259 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6260 {
6261 /* remove the backticks and comma from `command`, */
6262 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6263 d[-3] = NUL;
6264 }
6265 else if (d > IObuff + 4 && d[-3] == '`'
6266 && d[-2] == '\\' && d[-1] == '.')
6267 {
6268 /* remove the backticks and dot from `command`\. */
6269 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6270 d[-4] = NUL;
6271 }
6272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274 }
6275
6276 *matches = (char_u **)"";
6277 *num_matches = 0;
6278 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6279 if (keep_lang)
6280 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006281 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 /* Sort the matches found on the heuristic number that is after the
6285 * tag name. */
6286 qsort((void *)*matches, (size_t)*num_matches,
6287 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006288 /* Delete more than TAG_MANY to reduce the size of the listing. */
6289 while (*num_matches > TAG_MANY)
6290 vim_free((*matches)[--*num_matches]);
6291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 return OK;
6293}
6294
6295/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006296 * Called when starting to edit a buffer for a help file.
6297 */
6298 static void
6299prepare_help_buffer()
6300{
6301 char_u *p;
6302
6303 curbuf->b_help = TRUE;
6304#ifdef FEAT_QUICKFIX
6305 set_string_option_direct((char_u *)"buftype", -1,
6306 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6307#endif
6308
6309 /*
6310 * Always set these options after jumping to a help tag, because the
6311 * user may have an autocommand that gets in the way.
6312 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6313 * latin1 word characters (for translated help files).
6314 * Only set it when needed, buf_init_chartab() is some work.
6315 */
6316 p =
6317#ifdef EBCDIC
6318 (char_u *)"65-255,^*,^|,^\"";
6319#else
6320 (char_u *)"!-~,^*,^|,^\",192-255";
6321#endif
6322 if (STRCMP(curbuf->b_p_isk, p) != 0)
6323 {
6324 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6325 check_buf_options(curbuf);
6326 (void)buf_init_chartab(curbuf, FALSE);
6327 }
6328
Bram Moolenaar0b105412014-11-30 13:34:23 +01006329#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006330 /* Don't use the global foldmethod.*/
6331 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6332 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006333#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006334
6335 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6336 curwin->w_p_list = FALSE; /* no list mode */
6337
6338 curbuf->b_p_ma = FALSE; /* not modifiable */
6339 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6340 curwin->w_p_nu = 0; /* no line numbers */
6341 curwin->w_p_rnu = 0; /* no relative line numbers */
6342 RESET_BINDING(curwin); /* no scroll or cursor binding */
6343#ifdef FEAT_ARABIC
6344 curwin->w_p_arab = FALSE; /* no arabic mode */
6345#endif
6346#ifdef FEAT_RIGHTLEFT
6347 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6348#endif
6349#ifdef FEAT_FOLDING
6350 curwin->w_p_fen = FALSE; /* No folding in the help window */
6351#endif
6352#ifdef FEAT_DIFF
6353 curwin->w_p_diff = FALSE; /* No 'diff' */
6354#endif
6355#ifdef FEAT_SPELL
6356 curwin->w_p_spell = FALSE; /* No spell checking */
6357#endif
6358
6359 set_buflisted(FALSE);
6360}
6361
6362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 * After reading a help file: May cleanup a help buffer when syntax
6364 * highlighting is not used.
6365 */
6366 void
6367fix_help_buffer()
6368{
6369 linenr_T lnum;
6370 char_u *line;
6371 int in_example = FALSE;
6372 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006373 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 char_u *p;
6375 char_u *rt;
6376 int mustfree;
6377
6378 /* set filetype to "help". */
6379 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6380
6381#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006382 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383#endif
6384 {
6385 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6386 {
6387 line = ml_get_buf(curbuf, lnum, FALSE);
6388 len = (int)STRLEN(line);
6389 if (in_example && len > 0 && !vim_iswhite(line[0]))
6390 {
6391 /* End of example: non-white or '<' in first column. */
6392 if (line[0] == '<')
6393 {
6394 /* blank-out a '<' in the first column */
6395 line = ml_get_buf(curbuf, lnum, TRUE);
6396 line[0] = ' ';
6397 }
6398 in_example = FALSE;
6399 }
6400 if (!in_example && len > 0)
6401 {
6402 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6403 {
6404 /* blank-out a '>' in the last column (start of example) */
6405 line = ml_get_buf(curbuf, lnum, TRUE);
6406 line[len - 1] = ' ';
6407 in_example = TRUE;
6408 }
6409 else if (line[len - 1] == '~')
6410 {
6411 /* blank-out a '~' at the end of line (header marker) */
6412 line = ml_get_buf(curbuf, lnum, TRUE);
6413 line[len - 1] = ' ';
6414 }
6415 }
6416 }
6417 }
6418
6419 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006420 * In the "help.txt" and "help.abx" file, add the locally added help
6421 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006423 fname = gettail(curbuf->b_fname);
6424 if (fnamecmp(fname, "help.txt") == 0
6425#ifdef FEAT_MULTI_LANG
6426 || (fnamencmp(fname, "help.", 5) == 0
6427 && ASCII_ISALPHA(fname[5])
6428 && ASCII_ISALPHA(fname[6])
6429 && TOLOWER_ASC(fname[7]) == 'x'
6430 && fname[8] == NUL)
6431#endif
6432 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
6434 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6435 {
6436 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006437 if (strstr((char *)line, "*local-additions*") == NULL)
6438 continue;
6439
6440 /* Go through all directories in 'runtimepath', skipping
6441 * $VIMRUNTIME. */
6442 p = p_rtp;
6443 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006445 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6446 mustfree = FALSE;
6447 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
6448 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006450 int fcount;
6451 char_u **fnames;
6452 FILE *fd;
6453 char_u *s;
6454 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006456 vimconv_T vc;
6457 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458#endif
6459
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006460 /* Find all "doc/ *.txt" files in this directory. */
6461 add_pathsep(NameBuff);
6462#ifdef FEAT_MULTI_LANG
6463 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006465 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006467 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6468 &fnames, EW_FILE|EW_SILENT) == OK
6469 && fcount > 0)
6470 {
6471#ifdef FEAT_MULTI_LANG
6472 int i1;
6473 int i2;
6474 char_u *f1;
6475 char_u *f2;
6476 char_u *t1;
6477 char_u *e1;
6478 char_u *e2;
6479
6480 /* If foo.abx is found use it instead of foo.txt in
6481 * the same directory. */
6482 for (i1 = 0; i1 < fcount; ++i1)
6483 {
6484 for (i2 = 0; i2 < fcount; ++i2)
6485 {
6486 if (i1 == i2)
6487 continue;
6488 if (fnames[i1] == NULL || fnames[i2] == NULL)
6489 continue;
6490 f1 = fnames[i1];
6491 f2 = fnames[i2];
6492 t1 = gettail(f1);
6493 if (fnamencmp(f1, f2, t1 - f1) != 0)
6494 continue;
6495 e1 = vim_strrchr(t1, '.');
6496 e2 = vim_strrchr(gettail(f2), '.');
6497 if (e1 == NUL || e2 == NUL)
6498 continue;
6499 if (fnamecmp(e1, ".txt") != 0
6500 && fnamecmp(e1, fname + 4) != 0)
6501 {
6502 /* Not .txt and not .abx, remove it. */
6503 vim_free(fnames[i1]);
6504 fnames[i1] = NULL;
6505 continue;
6506 }
6507 if (fnamencmp(f1, f2, e1 - f1) != 0)
6508 continue;
6509 if (fnamecmp(e1, ".txt") == 0
6510 && fnamecmp(e2, fname + 4) == 0)
6511 {
6512 /* use .abx instead of .txt */
6513 vim_free(fnames[i1]);
6514 fnames[i1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006518#endif
6519 for (fi = 0; fi < fcount; ++fi)
6520 {
6521 if (fnames[fi] == NULL)
6522 continue;
6523 fd = mch_fopen((char *)fnames[fi], "r");
6524 if (fd != NULL)
6525 {
6526 vim_fgets(IObuff, IOSIZE, fd);
6527 if (IObuff[0] == '*'
6528 && (s = vim_strchr(IObuff + 1, '*'))
6529 != NULL)
6530 {
6531#ifdef FEAT_MBYTE
6532 int this_utf = MAYBE;
6533#endif
6534 /* Change tag definition to a
6535 * reference and remove <CR>/<NL>. */
6536 IObuff[0] = '|';
6537 *s = '|';
6538 while (*s != NUL)
6539 {
6540 if (*s == '\r' || *s == '\n')
6541 *s = NUL;
6542#ifdef FEAT_MBYTE
6543 /* The text is utf-8 when a byte
6544 * above 127 is found and no
6545 * illegal byte sequence is found.
6546 */
6547 if (*s >= 0x80 && this_utf != FALSE)
6548 {
6549 int l;
6550
6551 this_utf = TRUE;
6552 l = utf_ptr2len(s);
6553 if (l == 1)
6554 this_utf = FALSE;
6555 s += l - 1;
6556 }
6557#endif
6558 ++s;
6559 }
6560#ifdef FEAT_MBYTE
6561 /* The help file is latin1 or utf-8;
6562 * conversion to the current
6563 * 'encoding' may be required. */
6564 vc.vc_type = CONV_NONE;
6565 convert_setup(&vc, (char_u *)(
6566 this_utf == TRUE ? "utf-8"
6567 : "latin1"), p_enc);
6568 if (vc.vc_type == CONV_NONE)
6569 /* No conversion needed. */
6570 cp = IObuff;
6571 else
6572 {
6573 /* Do the conversion. If it fails
6574 * use the unconverted text. */
6575 cp = string_convert(&vc, IObuff,
6576 NULL);
6577 if (cp == NULL)
6578 cp = IObuff;
6579 }
6580 convert_setup(&vc, NULL, NULL);
6581
6582 ml_append(lnum, cp, (colnr_T)0, FALSE);
6583 if (cp != IObuff)
6584 vim_free(cp);
6585#else
6586 ml_append(lnum, IObuff, (colnr_T)0,
6587 FALSE);
6588#endif
6589 ++lnum;
6590 }
6591 fclose(fd);
6592 }
6593 }
6594 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006597 if (mustfree)
6598 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006600 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
6602 }
6603}
6604
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006605/*
6606 * ":exusage"
6607 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006608 void
6609ex_exusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006610 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006611{
6612 do_cmdline_cmd((char_u *)"help ex-cmd-index");
6613}
6614
6615/*
6616 * ":viusage"
6617 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006618 void
6619ex_viusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006620 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006621{
6622 do_cmdline_cmd((char_u *)"help normal-index");
6623}
6624
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625#if defined(FEAT_EX_EXTRA) || defined(PROTO)
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006626static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang, int add_help_tags));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
6628/*
6629 * ":helptags"
6630 */
6631 void
6632ex_helptags(eap)
6633 exarg_T *eap;
6634{
6635 garray_T ga;
6636 int i, j;
6637 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006638#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006640#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006641 expand_T xpc;
6642 char_u *dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 char_u ext[5];
6644 char_u fname[8];
6645 int filecount;
6646 char_u **files;
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006647 int add_help_tags = FALSE;
6648
6649 /* Check for ":helptags ++t {dir}". */
6650 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3]))
6651 {
6652 add_help_tags = TRUE;
6653 eap->arg = skipwhite(eap->arg + 3);
6654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
Bram Moolenaar0e253142008-01-13 12:31:34 +00006656 ExpandInit(&xpc);
6657 xpc.xp_context = EXPAND_DIRECTORIES;
6658 dirname = ExpandOne(&xpc, eap->arg, NULL,
6659 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
6660 if (dirname == NULL || !mch_isdir(dirname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
6662 EMSG2(_("E150: Not a directory: %s"), eap->arg);
Bram Moolenaar1c2836e2015-11-10 21:05:48 +01006663 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 return;
6665 }
6666
6667#ifdef FEAT_MULTI_LANG
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006668 /* Get a list of all files in the help directory and in subdirectories. */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006669 STRCPY(NameBuff, dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 add_pathsep(NameBuff);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006671 STRCAT(NameBuff, "**");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6673 EW_FILE|EW_SILENT) == FAIL
6674 || filecount == 0)
6675 {
6676 EMSG2("E151: No match: %s", NameBuff);
Bram Moolenaar0e253142008-01-13 12:31:34 +00006677 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 return;
6679 }
6680
6681 /* Go over all files in the directory to find out what languages are
6682 * present. */
6683 ga_init2(&ga, 1, 10);
6684 for (i = 0; i < filecount; ++i)
6685 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006686 len = (int)STRLEN(files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (len > 4)
6688 {
6689 if (STRICMP(files[i] + len - 4, ".txt") == 0)
6690 {
6691 /* ".txt" -> language "en" */
6692 lang[0] = 'e';
6693 lang[1] = 'n';
6694 }
6695 else if (files[i][len - 4] == '.'
6696 && ASCII_ISALPHA(files[i][len - 3])
6697 && ASCII_ISALPHA(files[i][len - 2])
6698 && TOLOWER_ASC(files[i][len - 1]) == 'x')
6699 {
6700 /* ".abx" -> language "ab" */
6701 lang[0] = TOLOWER_ASC(files[i][len - 3]);
6702 lang[1] = TOLOWER_ASC(files[i][len - 2]);
6703 }
6704 else
6705 continue;
6706
6707 /* Did we find this language already? */
6708 for (j = 0; j < ga.ga_len; j += 2)
6709 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
6710 break;
6711 if (j == ga.ga_len)
6712 {
6713 /* New language, add it. */
6714 if (ga_grow(&ga, 2) == FAIL)
6715 break;
6716 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
6717 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 }
6719 }
6720 }
6721
6722 /*
6723 * Loop over the found languages to generate a tags file for each one.
6724 */
6725 for (j = 0; j < ga.ga_len; j += 2)
6726 {
6727 STRCPY(fname, "tags-xx");
6728 fname[5] = ((char_u *)ga.ga_data)[j];
6729 fname[6] = ((char_u *)ga.ga_data)[j + 1];
6730 if (fname[5] == 'e' && fname[6] == 'n')
6731 {
6732 /* English is an exception: use ".txt" and "tags". */
6733 fname[4] = NUL;
6734 STRCPY(ext, ".txt");
6735 }
6736 else
6737 {
6738 /* Language "ab" uses ".abx" and "tags-ab". */
6739 STRCPY(ext, ".xxx");
6740 ext[1] = fname[5];
6741 ext[2] = fname[6];
6742 }
Bram Moolenaar0e253142008-01-13 12:31:34 +00006743 helptags_one(dirname, ext, fname, add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
6745
6746 ga_clear(&ga);
6747 FreeWild(filecount, files);
6748
6749#else
6750 /* No language support, just use "*.txt" and "tags". */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006751 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006753 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755
6756 static void
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006757helptags_one(dir, ext, tagfname, add_help_tags)
6758 char_u *dir; /* doc directory */
6759 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006760 char_u *tagfname; /* "tags" for English, "tags-fr" for French. */
6761 int add_help_tags; /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 FILE *fd_tags;
6764 FILE *fd;
6765 garray_T ga;
6766 int filecount;
6767 char_u **files;
6768 char_u *p1, *p2;
6769 int fi;
6770 char_u *s;
6771 int i;
6772 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006773 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774# ifdef FEAT_MBYTE
6775 int utf8 = MAYBE;
6776 int this_utf8;
6777 int firstline;
6778 int mix = FALSE; /* detected mixed encodings */
6779# endif
6780
6781 /*
6782 * Find all *.txt files.
6783 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01006784 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006786 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 STRCAT(NameBuff, ext);
6788 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6789 EW_FILE|EW_SILENT) == FAIL
6790 || filecount == 0)
6791 {
6792 if (!got_int)
6793 EMSG2("E151: No match: %s", NameBuff);
6794 return;
6795 }
6796
6797 /*
6798 * Open the tags file for writing.
6799 * Do this before scanning through all the files.
6800 */
6801 STRCPY(NameBuff, dir);
6802 add_pathsep(NameBuff);
6803 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006804 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (fd_tags == NULL)
6806 {
6807 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6808 FreeWild(filecount, files);
6809 return;
6810 }
6811
6812 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006813 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
6814 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 */
6816 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006817 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
6818 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
6820 if (ga_grow(&ga, 1) == FAIL)
6821 got_int = TRUE;
6822 else
6823 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006824 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (s == NULL)
6826 got_int = TRUE;
6827 else
6828 {
6829 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6830 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6831 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833 }
6834 }
6835
6836 /*
6837 * Go over all the files and extract the tags.
6838 */
6839 for (fi = 0; fi < filecount && !got_int; ++fi)
6840 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006841 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if (fd == NULL)
6843 {
6844 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6845 continue;
6846 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006847 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
6849# ifdef FEAT_MBYTE
6850 firstline = TRUE;
6851# endif
6852 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6853 {
6854# ifdef FEAT_MBYTE
6855 if (firstline)
6856 {
6857 /* Detect utf-8 file by a non-ASCII char in the first line. */
6858 this_utf8 = MAYBE;
6859 for (s = IObuff; *s != NUL; ++s)
6860 if (*s >= 0x80)
6861 {
6862 int l;
6863
6864 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006865 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (l == 1)
6867 {
6868 /* Illegal UTF-8 byte sequence. */
6869 this_utf8 = FALSE;
6870 break;
6871 }
6872 s += l - 1;
6873 }
6874 if (this_utf8 == MAYBE) /* only ASCII characters found */
6875 this_utf8 = FALSE;
6876 if (utf8 == MAYBE) /* first file */
6877 utf8 = this_utf8;
6878 else if (utf8 != this_utf8)
6879 {
6880 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6881 mix = !got_int;
6882 got_int = TRUE;
6883 }
6884 firstline = FALSE;
6885 }
6886# endif
6887 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6888 while (p1 != NULL)
6889 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02006890 /* Use vim_strbyte() instead of vim_strchr() so that when
6891 * 'encoding' is dbcs it still works, don't find '*' in the
6892 * second byte. */
6893 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6895 {
6896 for (s = p1 + 1; s < p2; ++s)
6897 if (*s == ' ' || *s == '\t' || *s == '|')
6898 break;
6899
6900 /*
6901 * Only accept a *tag* when it consists of valid
6902 * characters, there is white space before it and is
6903 * followed by a white character or end-of-line.
6904 */
6905 if (s == p2
6906 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6907 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6908 || s[1] == '\0'))
6909 {
6910 *p2 = '\0';
6911 ++p1;
6912 if (ga_grow(&ga, 1) == FAIL)
6913 {
6914 got_int = TRUE;
6915 break;
6916 }
6917 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6918 if (s == NULL)
6919 {
6920 got_int = TRUE;
6921 break;
6922 }
6923 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6924 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 sprintf((char *)s, "%s\t%s", p1, fname);
6926
6927 /* find next '*' */
6928 p2 = vim_strchr(p2 + 1, '*');
6929 }
6930 }
6931 p1 = p2;
6932 }
6933 line_breakcheck();
6934 }
6935
6936 fclose(fd);
6937 }
6938
6939 FreeWild(filecount, files);
6940
6941 if (!got_int)
6942 {
6943 /*
6944 * Sort the tags.
6945 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02006946 if (ga.ga_data != NULL)
6947 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949 /*
6950 * Check for duplicates.
6951 */
6952 for (i = 1; i < ga.ga_len; ++i)
6953 {
6954 p1 = ((char_u **)ga.ga_data)[i - 1];
6955 p2 = ((char_u **)ga.ga_data)[i];
6956 while (*p1 == *p2)
6957 {
6958 if (*p2 == '\t')
6959 {
6960 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006961 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6963 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6964 EMSG(NameBuff);
6965 *p2 = '\t';
6966 break;
6967 }
6968 ++p1;
6969 ++p2;
6970 }
6971 }
6972
6973# ifdef FEAT_MBYTE
6974 if (utf8 == TRUE)
6975 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6976# endif
6977
6978 /*
6979 * Write the tags into the file.
6980 */
6981 for (i = 0; i < ga.ga_len; ++i)
6982 {
6983 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00006984 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00006986 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 else
6988 {
6989 fprintf(fd_tags, "%s\t/*", s);
6990 for (p1 = s; *p1 != '\t'; ++p1)
6991 {
6992 /* insert backslash before '\\' and '/' */
6993 if (*p1 == '\\' || *p1 == '/')
6994 putc('\\', fd_tags);
6995 putc(*p1, fd_tags);
6996 }
6997 fprintf(fd_tags, "*\n");
6998 }
6999 }
7000 }
7001#ifdef FEAT_MBYTE
7002 if (mix)
7003 got_int = FALSE; /* continue with other languages */
7004#endif
7005
7006 for (i = 0; i < ga.ga_len; ++i)
7007 vim_free(((char_u **)ga.ga_data)[i]);
7008 ga_clear(&ga);
7009 fclose(fd_tags); /* there is no check for an error... */
7010}
7011#endif
7012
7013#if defined(FEAT_SIGNS) || defined(PROTO)
7014
7015/*
7016 * Struct to hold the sign properties.
7017 */
7018typedef struct sign sign_T;
7019
7020struct sign
7021{
7022 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02007023 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 char_u *sn_name; /* name of sign */
7025 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007026# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 char_u *sn_text; /* text used instead of pixmap */
7030 int sn_line_hl; /* highlight ID for line */
7031 int sn_text_hl; /* highlight ID for text */
7032};
7033
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007035static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
Bram Moolenaar985cb442009-05-14 19:51:46 +00007037static int sign_cmd_idx __ARGS((char_u *begin_cmd, char_u *end_cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038static void sign_list_defined __ARGS((sign_T *sp));
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007039static void sign_undefine __ARGS((sign_T *sp, sign_T *sp_prev));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007041static char *cmds[] = {
7042 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007043# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007044 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007045# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007046 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007047# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007048 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007049# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007050 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007051# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007052 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007053# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007054 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007055# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007056};
7057
7058/*
7059 * Find index of a ":sign" subcmd from its name.
7060 * "*end_cmd" must be writable.
7061 */
7062 static int
7063sign_cmd_idx(begin_cmd, end_cmd)
Bram Moolenaar985cb442009-05-14 19:51:46 +00007064 char_u *begin_cmd; /* begin of sign subcmd */
7065 char_u *end_cmd; /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007066{
7067 int idx;
7068 char save = *end_cmd;
7069
7070 *end_cmd = NUL;
7071 for (idx = 0; ; ++idx)
7072 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7073 break;
7074 *end_cmd = save;
7075 return idx;
7076}
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078/*
7079 * ":sign" command
7080 */
7081 void
7082ex_sign(eap)
7083 exarg_T *eap;
7084{
7085 char_u *arg = eap->arg;
7086 char_u *p;
7087 int idx;
7088 sign_T *sp;
7089 sign_T *sp_prev;
7090 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
7092 /* Parse the subcommand. */
7093 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007094 idx = sign_cmd_idx(arg, p);
7095 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007097 EMSG2(_("E160: Unknown sign command: %s"), arg);
7098 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100 arg = skipwhite(p);
7101
7102 if (idx <= SIGNCMD_LIST)
7103 {
7104 /*
7105 * Define, undefine or list signs.
7106 */
7107 if (idx == SIGNCMD_LIST && *arg == NUL)
7108 {
7109 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007110 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 sign_list_defined(sp);
7112 }
7113 else if (*arg == NUL)
7114 EMSG(_("E156: Missing sign name"));
7115 else
7116 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007117 /* Isolate the sign name. If it's a number skip leading zeroes,
7118 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 p = skiptowhite(arg);
7120 if (*p != NUL)
7121 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007122 while (arg[0] == '0' && arg[1] != NUL)
7123 ++arg;
7124
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 sp_prev = NULL;
7126 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7127 {
7128 if (STRCMP(sp->sn_name, arg) == 0)
7129 break;
7130 sp_prev = sp;
7131 }
7132 if (idx == SIGNCMD_DEFINE)
7133 {
7134 /* ":sign define {name} ...": define a sign */
7135 if (sp == NULL)
7136 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007137 sign_T *lp;
7138 int start = next_sign_typenr;
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 /* Allocate a new sign. */
7141 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7142 if (sp == NULL)
7143 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007145 /* Check that next_sign_typenr is not already being used.
7146 * This only happens after wrapping around. Hopefully
7147 * another one got deleted and we can use its number. */
7148 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007150 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007152 ++next_sign_typenr;
7153 if (next_sign_typenr == MAX_TYPENR)
7154 next_sign_typenr = 1;
7155 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007157 vim_free(sp);
7158 EMSG(_("E612: Too many signs defined"));
7159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007161 lp = first_sign; /* start all over */
7162 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007164 lp = lp->sn_next;
7165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007167 sp->sn_typenr = next_sign_typenr;
7168 if (++next_sign_typenr == MAX_TYPENR)
7169 next_sign_typenr = 1; /* wrap around */
7170
7171 sp->sn_name = vim_strsave(arg);
7172 if (sp->sn_name == NULL) /* out of memory */
7173 {
7174 vim_free(sp);
7175 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007177
7178 /* add the new sign to the list of signs */
7179 if (sp_prev == NULL)
7180 first_sign = sp;
7181 else
7182 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184
7185 /* set values for a defined sign. */
7186 for (;;)
7187 {
7188 arg = skipwhite(p);
7189 if (*arg == NUL)
7190 break;
7191 p = skiptowhite_esc(arg);
7192 if (STRNCMP(arg, "icon=", 5) == 0)
7193 {
7194 arg += 5;
7195 vim_free(sp->sn_icon);
7196 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7197 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007198# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (gui.in_use)
7200 {
7201 out_flush();
7202 if (sp->sn_image != NULL)
7203 gui_mch_destroy_sign(sp->sn_image);
7204 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7205 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 else if (STRNCMP(arg, "text=", 5) == 0)
7209 {
7210 char_u *s;
7211 int cells;
7212 int len;
7213
7214 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007215# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 /* Count cells and check for non-printable chars */
7217 if (has_mbyte)
7218 {
7219 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007220 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
7222 if (!vim_isprintc((*mb_ptr2char)(s)))
7223 break;
7224 cells += (*mb_ptr2cells)(s);
7225 }
7226 }
7227 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {
7230 for (s = arg; s < p; ++s)
7231 if (!vim_isprintc(*s))
7232 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007233 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235 /* Currently must be one or two display cells */
7236 if (s != p || cells < 1 || cells > 2)
7237 {
7238 *p = NUL;
7239 EMSG2(_("E239: Invalid sign text: %s"), arg);
7240 return;
7241 }
7242
7243 vim_free(sp->sn_text);
7244 /* Allocate one byte more if we need to pad up
7245 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007246 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 sp->sn_text = vim_strnsave(arg, len);
7248
7249 if (sp->sn_text != NULL && cells == 1)
7250 STRCPY(sp->sn_text + len - 1, " ");
7251 }
7252 else if (STRNCMP(arg, "linehl=", 7) == 0)
7253 {
7254 arg += 7;
7255 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7256 }
7257 else if (STRNCMP(arg, "texthl=", 7) == 0)
7258 {
7259 arg += 7;
7260 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7261 }
7262 else
7263 {
7264 EMSG2(_(e_invarg2), arg);
7265 return;
7266 }
7267 }
7268 }
7269 else if (sp == NULL)
7270 EMSG2(_("E155: Unknown sign: %s"), arg);
7271 else if (idx == SIGNCMD_LIST)
7272 /* ":sign list {name}" */
7273 sign_list_defined(sp);
7274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007276 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
7278 }
7279 else
7280 {
7281 int id = -1;
7282 linenr_T lnum = -1;
7283 char_u *sign_name = NULL;
7284 char_u *arg1;
7285
7286 if (*arg == NUL)
7287 {
7288 if (idx == SIGNCMD_PLACE)
7289 {
7290 /* ":sign place": list placed signs in all buffers */
7291 sign_list_placed(NULL);
7292 }
7293 else if (idx == SIGNCMD_UNPLACE)
7294 {
7295 /* ":sign unplace": remove placed sign at cursor */
7296 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7297 if (id > 0)
7298 {
7299 buf_delsign(curwin->w_buffer, id);
7300 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7301 }
7302 else
7303 EMSG(_("E159: Missing sign number"));
7304 }
7305 else
7306 EMSG(_(e_argreq));
7307 return;
7308 }
7309
7310 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7311 {
7312 /* ":sign unplace *": remove all placed signs */
7313 buf_delete_all_signs();
7314 return;
7315 }
7316
7317 /* first arg could be placed sign id */
7318 arg1 = arg;
7319 if (VIM_ISDIGIT(*arg))
7320 {
7321 id = getdigits(&arg);
7322 if (!vim_iswhite(*arg) && *arg != NUL)
7323 {
7324 id = -1;
7325 arg = arg1;
7326 }
7327 else
7328 {
7329 arg = skipwhite(arg);
7330 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7331 {
7332 /* ":sign unplace {id}": remove placed sign by number */
7333 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7334 if ((lnum = buf_delsign(buf, id)) != 0)
7335 update_debug_sign(buf, lnum);
7336 return;
7337 }
7338 }
7339 }
7340
7341 /*
7342 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7343 * Leave "arg" pointing to {fname}.
7344 */
7345 for (;;)
7346 {
7347 if (STRNCMP(arg, "line=", 5) == 0)
7348 {
7349 arg += 5;
7350 lnum = atoi((char *)arg);
7351 arg = skiptowhite(arg);
7352 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007353 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7354 {
7355 if (id != -1)
7356 {
7357 EMSG(_(e_invarg));
7358 return;
7359 }
7360 id = -2;
7361 arg = skiptowhite(arg + 1);
7362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 else if (STRNCMP(arg, "name=", 5) == 0)
7364 {
7365 arg += 5;
7366 sign_name = arg;
7367 arg = skiptowhite(arg);
7368 if (*arg != NUL)
7369 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007370 while (sign_name[0] == '0' && sign_name[1] != NUL)
7371 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373 else if (STRNCMP(arg, "file=", 5) == 0)
7374 {
7375 arg += 5;
7376 buf = buflist_findname(arg);
7377 break;
7378 }
7379 else if (STRNCMP(arg, "buffer=", 7) == 0)
7380 {
7381 arg += 7;
7382 buf = buflist_findnr((int)getdigits(&arg));
7383 if (*skipwhite(arg) != NUL)
7384 EMSG(_(e_trailing));
7385 break;
7386 }
7387 else
7388 {
7389 EMSG(_(e_invarg));
7390 return;
7391 }
7392 arg = skipwhite(arg);
7393 }
7394
7395 if (buf == NULL)
7396 {
7397 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7398 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007399 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 {
7401 if (lnum >= 0 || sign_name != NULL)
7402 EMSG(_(e_invarg));
7403 else
7404 /* ":sign place file={fname}": list placed signs in one file */
7405 sign_list_placed(buf);
7406 }
7407 else if (idx == SIGNCMD_JUMP)
7408 {
7409 /* ":sign jump {id} file={fname}" */
7410 if (lnum >= 0 || sign_name != NULL)
7411 EMSG(_(e_invarg));
7412 else if ((lnum = buf_findsign(buf, id)) > 0)
7413 { /* goto a sign ... */
7414 if (buf_jump_open_win(buf) != NULL)
7415 { /* ... in a current window */
7416 curwin->w_cursor.lnum = lnum;
7417 check_cursor_lnum();
7418 beginline(BL_WHITE);
7419 }
7420 else
7421 { /* ... not currently in a window */
7422 char_u *cmd;
7423
7424 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7425 if (cmd == NULL)
7426 return;
7427 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7428 do_cmdline_cmd(cmd);
7429 vim_free(cmd);
7430 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007431# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 }
7435 else
7436 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7437 }
7438 else if (idx == SIGNCMD_UNPLACE)
7439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 if (lnum >= 0 || sign_name != NULL)
7441 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007442 else if (id == -2)
7443 {
7444 /* ":sign unplace * file={fname}" */
7445 redraw_buf_later(buf, NOT_VALID);
7446 buf_delete_signs(buf);
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 else
7449 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007450 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 lnum = buf_delsign(buf, id);
7452 update_debug_sign(buf, lnum);
7453 }
7454 }
7455 /* idx == SIGNCMD_PLACE */
7456 else if (sign_name != NULL)
7457 {
7458 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7459 if (STRCMP(sp->sn_name, sign_name) == 0)
7460 break;
7461 if (sp == NULL)
7462 {
7463 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7464 return;
7465 }
7466 if (lnum > 0)
7467 /* ":sign place {id} line={lnum} name={name} file={fname}":
7468 * place a sign */
7469 buf_addsign(buf, id, lnum, sp->sn_typenr);
7470 else
7471 /* ":sign place {id} file={fname}": change sign type */
7472 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007473 if (lnum > 0)
7474 update_debug_sign(buf, lnum);
7475 else
7476 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478 else
7479 EMSG(_(e_invarg));
7480 }
7481}
7482
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007483# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484/*
7485 * Allocate the icons. Called when the GUI has started. Allows defining
7486 * signs before it starts.
7487 */
7488 void
7489sign_gui_started()
7490{
7491 sign_T *sp;
7492
7493 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7494 if (sp->sn_icon != NULL)
7495 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7496}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
7499/*
7500 * List one sign.
7501 */
7502 static void
7503sign_list_defined(sp)
7504 sign_T *sp;
7505{
7506 char_u *p;
7507
Bram Moolenaar555b2802005-05-19 21:08:39 +00007508 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 if (sp->sn_icon != NULL)
7510 {
7511 MSG_PUTS(" icon=");
7512 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007513# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 if (sp->sn_image == NULL)
7515 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007516# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 }
7520 if (sp->sn_text != NULL)
7521 {
7522 MSG_PUTS(" text=");
7523 msg_outtrans(sp->sn_text);
7524 }
7525 if (sp->sn_line_hl > 0)
7526 {
7527 MSG_PUTS(" linehl=");
7528 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
7529 if (p == NULL)
7530 MSG_PUTS("NONE");
7531 else
7532 msg_puts(p);
7533 }
7534 if (sp->sn_text_hl > 0)
7535 {
7536 MSG_PUTS(" texthl=");
7537 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
7538 if (p == NULL)
7539 MSG_PUTS("NONE");
7540 else
7541 msg_puts(p);
7542 }
7543}
7544
7545/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007546 * Undefine a sign and free its memory.
7547 */
7548 static void
7549sign_undefine(sp, sp_prev)
7550 sign_T *sp;
7551 sign_T *sp_prev;
7552{
7553 vim_free(sp->sn_name);
7554 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007555# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007556 if (sp->sn_image != NULL)
7557 {
7558 out_flush();
7559 gui_mch_destroy_sign(sp->sn_image);
7560 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007561# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007562 vim_free(sp->sn_text);
7563 if (sp_prev == NULL)
7564 first_sign = sp->sn_next;
7565 else
7566 sp_prev->sn_next = sp->sn_next;
7567 vim_free(sp);
7568}
7569
7570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 * Get highlighting attribute for sign "typenr".
7572 * If "line" is TRUE: line highl, if FALSE: text highl.
7573 */
7574 int
7575sign_get_attr(typenr, line)
7576 int typenr;
7577 int line;
7578{
7579 sign_T *sp;
7580
7581 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7582 if (sp->sn_typenr == typenr)
7583 {
7584 if (line)
7585 {
7586 if (sp->sn_line_hl > 0)
7587 return syn_id2attr(sp->sn_line_hl);
7588 }
7589 else
7590 {
7591 if (sp->sn_text_hl > 0)
7592 return syn_id2attr(sp->sn_text_hl);
7593 }
7594 break;
7595 }
7596 return 0;
7597}
7598
7599/*
7600 * Get text mark for sign "typenr".
7601 * Returns NULL if there isn't one.
7602 */
7603 char_u *
7604sign_get_text(typenr)
7605 int typenr;
7606{
7607 sign_T *sp;
7608
7609 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7610 if (sp->sn_typenr == typenr)
7611 return sp->sn_text;
7612 return NULL;
7613}
7614
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007615# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 void *
7617sign_get_image(typenr)
7618 int typenr; /* the attribute which may have a sign */
7619{
7620 sign_T *sp;
7621
7622 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7623 if (sp->sn_typenr == typenr)
7624 return sp->sn_image;
7625 return NULL;
7626}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
7629/*
7630 * Get the name of a sign by its typenr.
7631 */
7632 char_u *
7633sign_typenr2name(typenr)
7634 int typenr;
7635{
7636 sign_T *sp;
7637
7638 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7639 if (sp->sn_typenr == typenr)
7640 return sp->sn_name;
7641 return (char_u *)_("[Deleted]");
7642}
7643
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007644# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007645/*
7646 * Undefine/free all signs.
7647 */
7648 void
7649free_signs()
7650{
7651 while (first_sign != NULL)
7652 sign_undefine(first_sign, NULL);
7653}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007654# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007655
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007656# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007657static enum
7658{
7659 EXP_SUBCMD, /* expand :sign sub-commands */
7660 EXP_DEFINE, /* expand :sign define {name} args */
7661 EXP_PLACE, /* expand :sign place {id} args */
7662 EXP_UNPLACE, /* expand :sign unplace" */
7663 EXP_SIGN_NAMES /* expand with name of placed signs */
7664} expand_what;
7665
7666/*
7667 * Function given to ExpandGeneric() to obtain the sign command
7668 * expansion.
7669 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007670 char_u *
7671get_sign_name(xp, idx)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00007672 expand_T *xp UNUSED;
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007673 int idx;
7674{
7675 sign_T *sp;
7676 int current_idx;
7677
7678 switch (expand_what)
7679 {
7680 case EXP_SUBCMD:
7681 return (char_u *)cmds[idx];
7682 case EXP_DEFINE:
7683 {
7684 char *define_arg[] =
7685 {
7686 "icon=", "linehl=", "text=", "texthl=", NULL
7687 };
7688 return (char_u *)define_arg[idx];
7689 }
7690 case EXP_PLACE:
7691 {
7692 char *place_arg[] =
7693 {
7694 "line=", "name=", "file=", "buffer=", NULL
7695 };
7696 return (char_u *)place_arg[idx];
7697 }
7698 case EXP_UNPLACE:
7699 {
7700 char *unplace_arg[] = { "file=", "buffer=", NULL };
7701 return (char_u *)unplace_arg[idx];
7702 }
7703 case EXP_SIGN_NAMES:
7704 /* Complete with name of signs already defined */
7705 current_idx = 0;
7706 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7707 if (current_idx++ == idx)
7708 return sp->sn_name;
7709 return NULL;
7710 default:
7711 return NULL;
7712 }
7713}
7714
7715/*
7716 * Handle command line completion for :sign command.
7717 */
7718 void
7719set_context_in_sign_cmd(xp, arg)
7720 expand_T *xp;
7721 char_u *arg;
7722{
7723 char_u *p;
7724 char_u *end_subcmd;
7725 char_u *last;
7726 int cmd_idx;
7727 char_u *begin_subcmd_args;
7728
7729 /* Default: expand subcommands. */
7730 xp->xp_context = EXPAND_SIGN;
7731 expand_what = EXP_SUBCMD;
7732 xp->xp_pattern = arg;
7733
7734 end_subcmd = skiptowhite(arg);
7735 if (*end_subcmd == NUL)
7736 /* expand subcmd name
7737 * :sign {subcmd}<CTRL-D>*/
7738 return;
7739
7740 cmd_idx = sign_cmd_idx(arg, end_subcmd);
7741
7742 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007743 * |
7744 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007745 begin_subcmd_args = skipwhite(end_subcmd);
7746 p = skiptowhite(begin_subcmd_args);
7747 if (*p == NUL)
7748 {
7749 /*
7750 * Expand first argument of subcmd when possible.
7751 * For ":jump {id}" and ":unplace {id}", we could
7752 * possibly expand the ids of all signs already placed.
7753 */
7754 xp->xp_pattern = begin_subcmd_args;
7755 switch (cmd_idx)
7756 {
7757 case SIGNCMD_LIST:
7758 case SIGNCMD_UNDEFINE:
7759 /* :sign list <CTRL-D>
7760 * :sign undefine <CTRL-D> */
7761 expand_what = EXP_SIGN_NAMES;
7762 break;
7763 default:
7764 xp->xp_context = EXPAND_NOTHING;
7765 }
7766 return;
7767 }
7768
7769 /* expand last argument of subcmd */
7770
7771 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007772 * |
7773 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007774
7775 /* Loop until reaching last argument. */
7776 do
7777 {
7778 p = skipwhite(p);
7779 last = p;
7780 p = skiptowhite(p);
7781 } while (*p != NUL);
7782
7783 p = vim_strchr(last, '=');
7784
7785 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007786 * | |
7787 * last p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007788 if (p == NUL)
7789 {
7790 /* Expand last argument name (before equal sign). */
7791 xp->xp_pattern = last;
7792 switch (cmd_idx)
7793 {
7794 case SIGNCMD_DEFINE:
7795 expand_what = EXP_DEFINE;
7796 break;
7797 case SIGNCMD_PLACE:
7798 expand_what = EXP_PLACE;
7799 break;
7800 case SIGNCMD_JUMP:
7801 case SIGNCMD_UNPLACE:
7802 expand_what = EXP_UNPLACE;
7803 break;
7804 default:
7805 xp->xp_context = EXPAND_NOTHING;
7806 }
7807 }
7808 else
7809 {
7810 /* Expand last argument value (after equal sign). */
7811 xp->xp_pattern = p + 1;
7812 switch (cmd_idx)
7813 {
7814 case SIGNCMD_DEFINE:
7815 if (STRNCMP(last, "texthl", p - last) == 0 ||
7816 STRNCMP(last, "linehl", p - last) == 0)
7817 xp->xp_context = EXPAND_HIGHLIGHT;
7818 else if (STRNCMP(last, "icon", p - last) == 0)
7819 xp->xp_context = EXPAND_FILES;
7820 else
7821 xp->xp_context = EXPAND_NOTHING;
7822 break;
7823 case SIGNCMD_PLACE:
7824 if (STRNCMP(last, "name", p - last) == 0)
7825 expand_what = EXP_SIGN_NAMES;
7826 else
7827 xp->xp_context = EXPAND_NOTHING;
7828 break;
7829 default:
7830 xp->xp_context = EXPAND_NOTHING;
7831 }
7832 }
7833}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007834# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007835#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007836
7837/*
7838 * Make the user happy.
7839 */
7840 void
7841ex_smile(eap)
7842 exarg_T *eap UNUSED;
7843{
7844 static char *code = "\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$\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";
7845 char *p;
7846 int n;
7847
7848 msg_start();
7849 msg_putchar('\n');
7850 for (p = code; *p != NUL; ++p)
7851 if (*p == 'x')
7852 msg_putchar('\n');
7853 else
7854 for (n = *p++; n > 0; --n)
Bram Moolenaar2d820802015-12-31 20:46:39 +01007855 if (*p == 'o' || *p == '$')
7856 msg_putchar_attr(*p, hl_attr(HLF_L));
7857 else
7858 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007859 msg_clr_eos();
7860}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
7862#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
7863/*
7864 * ":drop"
7865 * Opens the first argument in a window. When there are two or more arguments
7866 * the argument list is redefined.
7867 */
7868 void
7869ex_drop(eap)
7870 exarg_T *eap;
7871{
7872 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 win_T *wp;
7874 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007875# ifdef FEAT_WINDOWS
7876 tabpage_T *tp;
7877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
7879 /*
7880 * Check if the first argument is already being edited in a window. If
7881 * so, jump to that window.
7882 * We would actually need to check all arguments, but that's complicated
7883 * and mostly only one file is dropped.
7884 * This also ignores wildcards, since it is very unlikely the user is
7885 * editing a file name with a wildcard character.
7886 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007887 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00007889 /*
7890 * Expanding wildcards may result in an empty argument list. E.g. when
7891 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
7892 * already did an error message for this.
7893 */
7894 if (ARGCOUNT == 0)
7895 return;
7896
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007897# ifdef FEAT_WINDOWS
7898 if (cmdmod.tab)
7899 {
7900 /* ":tab drop file ...": open a tab for each argument that isn't
7901 * edited in a window yet. It's like ":tab all" but without closing
7902 * windows or tabs. */
7903 ex_all(eap);
7904 }
7905 else
7906# endif
7907 {
7908 /* ":drop file ...": Edit the first argument. Jump to an existing
7909 * window if possible, edit in current window if the current buffer
7910 * can be abandoned, otherwise open a new window. */
7911 buf = buflist_findnr(ARGLIST[0].ae_fnum);
7912
7913 FOR_ALL_TAB_WINDOWS(tp, wp)
7914 {
7915 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007917# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007918 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 return;
7922 }
7923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007925 /*
7926 * Check whether the current buffer is changed. If so, we will need
7927 * to split the current window or data could be lost.
7928 * Skip the check if the 'hidden' option is set, as in this case the
7929 * buffer won't be lost.
7930 */
7931 if (!P_HID(curbuf))
7932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007934 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935# endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007936 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007938 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007940 if (split)
7941 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007945 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7946 if (split)
7947 {
7948 eap->cmdidx = CMD_sfirst;
7949 eap->cmd[0] = 's';
7950 }
7951 else
7952 eap->cmdidx = CMD_first;
7953 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956#endif