blob: 1d0a3856c58737f81844d3df3b5e3121459850d5 [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
278static int sort_ic; /* ignore case */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000279static int sort_nr; /* sort on number */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000280static int sort_rx; /* sort on regex instead of skipping it */
281
282static int sort_abort; /* flag to indicate if sorting has been interrupted */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000283
284/* Struct to store info to be sorted. */
285typedef struct
286{
287 linenr_T lnum; /* line number */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000288 long start_col_nr; /* starting column number or number */
289 long end_col_nr; /* ending column number */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000290} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000291
292static int
293#ifdef __BORLANDC__
294_RTLENTRYF
295#endif
296sort_compare __ARGS((const void *s1, const void *s2));
297
298 static int
299#ifdef __BORLANDC__
300_RTLENTRYF
301#endif
302sort_compare(s1, s2)
303 const void *s1;
304 const void *s2;
305{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000306 sorti_T l1 = *(sorti_T *)s1;
307 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000308 int result = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000309
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000310 /* If the user interrupts, there's no way to stop qsort() immediately, but
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000311 * if we return 0 every time, qsort will assume it's done sorting and
312 * exit. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000313 if (sort_abort)
314 return 0;
315 fast_breakcheck();
316 if (got_int)
317 sort_abort = TRUE;
318
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000319 /* When sorting numbers "start_col_nr" is the number, not the column
320 * number. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000321 if (sort_nr)
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200322 result = l1.start_col_nr == l2.start_col_nr ? 0
323 : l1.start_col_nr > l2.start_col_nr ? 1 : -1;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000324 else
325 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000326 /* We need to copy one line into "sortbuf1", because there is no
327 * guarantee that the first pointer becomes invalid when obtaining the
328 * second one. */
329 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.start_col_nr,
330 l1.end_col_nr - l1.start_col_nr + 1);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000331 sortbuf1[l1.end_col_nr - l1.start_col_nr] = 0;
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000332 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.start_col_nr,
333 l2.end_col_nr - l2.start_col_nr + 1);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000334 sortbuf2[l2.end_col_nr - l2.start_col_nr] = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000335
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000336 result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
337 : STRCMP(sortbuf1, sortbuf2);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000338 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000339
340 /* If two lines have the same value, preserve the original line order. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 if (result == 0)
Bram Moolenaarb21e5842006-04-16 18:30:08 +0000342 return (int)(l1.lnum - l2.lnum);
343 return result;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000344}
345
346/*
347 * ":sort".
348 */
349 void
350ex_sort(eap)
351 exarg_T *eap;
352{
353 regmatch_T regmatch;
354 int len;
355 linenr_T lnum;
356 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000357 sorti_T *nrs;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000358 size_t count = (size_t)(eap->line2 - eap->line1 + 1);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000359 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000360 char_u *p;
361 char_u *s;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000362 char_u *s2;
363 char_u c; /* temporary character storage */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000364 int unique = FALSE;
365 long deleted;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000366 colnr_T start_col;
367 colnr_T end_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100368 int sort_what = 0;
369 int format_found = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000370
Bram Moolenaar48c99162008-02-18 18:42:52 +0000371 /* Sorting one line is really quick! */
372 if (count <= 1)
373 return;
374
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000375 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
376 return;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000377 sortbuf1 = NULL;
378 sortbuf2 = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000379 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000380 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000381 if (nrs == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000382 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000383
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100384 sort_abort = sort_ic = sort_rx = sort_nr = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000385
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000386 for (p = eap->arg; *p != NUL; ++p)
387 {
388 if (vim_iswhite(*p))
389 ;
390 else if (*p == 'i')
391 sort_ic = TRUE;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000392 else if (*p == 'r')
393 sort_rx = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000394 else if (*p == 'n')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100395 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000396 sort_nr = 2;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100397 ++format_found;
398 }
399 else if (*p == 'b')
400 {
401 sort_what = STR2NR_BIN + STR2NR_FORCE;
402 ++format_found;
403 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000404 else if (*p == 'o')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100405 {
406 sort_what = STR2NR_OCT + STR2NR_FORCE;
407 ++format_found;
408 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000409 else if (*p == 'x')
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100410 {
411 sort_what = STR2NR_HEX + STR2NR_FORCE;
412 ++format_found;
413 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000414 else if (*p == 'u')
415 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000416 else if (*p == '"') /* comment start */
417 break;
418 else if (check_nextcmd(p) != NULL)
419 {
420 eap->nextcmd = check_nextcmd(p);
421 break;
422 }
423 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000424 {
425 s = skip_regexp(p + 1, *p, TRUE, NULL);
426 if (*s != *p)
427 {
428 EMSG(_(e_invalpat));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000429 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000430 }
431 *s = NUL;
Bram Moolenaar1256e722007-07-10 15:26:20 +0000432 /* Use last search pattern if sort pattern is empty. */
Bram Moolenaar210f3702013-03-07 16:41:30 +0100433 if (s == p + 1)
434 {
435 if (last_search_pat() == NULL)
436 {
437 EMSG(_(e_noprevre));
438 goto sortend;
439 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000440 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
Bram Moolenaar210f3702013-03-07 16:41:30 +0100441 }
Bram Moolenaar1256e722007-07-10 15:26:20 +0000442 else
443 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000444 if (regmatch.regprog == NULL)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000445 goto sortend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000446 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000447 regmatch.rm_ic = p_ic;
448 }
449 else
450 {
451 EMSG2(_(e_invarg2), p);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000452 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000453 }
454 }
455
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100456 /* Can only have one of 'n', 'b', 'o' and 'x'. */
457 if (format_found > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000458 {
459 EMSG(_(e_invarg));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000460 goto sortend;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000461 }
462
463 /* From here on "sort_nr" is used as a flag for any number sorting. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100464 sort_nr += sort_what;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000465
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000466 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000467 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000468 * the lines into allocated memory.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000469 * When sorting on strings "start_col_nr" is the offset in the line, for
470 * numbers sorting it's the number to sort on. This means the pattern
471 * matching and number conversion only has to be done once per line.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000472 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000473 */
474 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
475 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000476 s = ml_get(lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000477 len = (int)STRLEN(s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000478 if (maxlen < len)
479 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000480
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000481 start_col = 0;
482 end_col = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000483 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000484 {
485 if (sort_rx)
486 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000487 start_col = (colnr_T)(regmatch.startp[0] - s);
488 end_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000489 }
490 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000491 start_col = (colnr_T)(regmatch.endp[0] - s);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000492 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000493 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000494 if (regmatch.regprog != NULL)
495 end_col = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000496
497 if (sort_nr)
498 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000499 /* Make sure vim_str2nr doesn't read any digits past the end
500 * of the match, by temporarily terminating the string there */
501 s2 = s + end_col;
502 c = *s2;
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200503 *s2 = NUL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000504 /* Sorting on number: Store the number itself. */
Bram Moolenaar1387a602008-07-24 19:31:11 +0000505 p = s + start_col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100506 if (sort_what & STR2NR_HEX)
Bram Moolenaar1387a602008-07-24 19:31:11 +0000507 s = skiptohex(p);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100508 else if (sort_what & STR2NR_BIN)
509 s = skiptobin(p);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000510 else
Bram Moolenaar1387a602008-07-24 19:31:11 +0000511 s = skiptodigit(p);
512 if (s > p && s[-1] == '-')
513 --s; /* include preceding negative sign */
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200514 if (*s == NUL)
515 /* empty line should sort before any number */
516 nrs[lnum - eap->line1].start_col_nr = -MAXLNUM;
517 else
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100518 vim_str2nr(s, NULL, NULL, sort_what,
519 &nrs[lnum - eap->line1].start_col_nr, NULL, 0);
Bram Moolenaarf75d4982010-10-15 20:20:05 +0200520 *s2 = c;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000521 }
522 else
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000523 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000524 /* Store the column to sort at. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000525 nrs[lnum - eap->line1].start_col_nr = start_col;
526 nrs[lnum - eap->line1].end_col_nr = end_col;
527 }
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000528
529 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000530
531 if (regmatch.regprog != NULL)
532 fast_breakcheck();
533 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000534 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000535 }
536
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000537 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000538 sortbuf1 = alloc((unsigned)maxlen + 1);
539 if (sortbuf1 == NULL)
540 goto sortend;
541 sortbuf2 = alloc((unsigned)maxlen + 1);
542 if (sortbuf2 == NULL)
543 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000544
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000545 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000546 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000547
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000548 if (sort_abort)
549 goto sortend;
550
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000551 /* Insert the lines in the sorted order below the last one. */
552 lnum = eap->line2;
553 for (i = 0; i < count; ++i)
554 {
555 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
556 if (!unique || i == 0
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000557 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000558 {
Bram Moolenaar75e3ad02015-12-17 15:07:32 +0100559 /* Copy the line into a buffer, it may become invalid in
560 * ml_append(). And it's needed for "unique". */
561 STRCPY(sortbuf1, s);
562 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000563 break;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000564 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000565 fast_breakcheck();
566 if (got_int)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000567 goto sortend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000568 }
569
570 /* delete the original lines if appending worked */
571 if (i == count)
572 for (i = 0; i < count; ++i)
573 ml_delete(eap->line1, FALSE);
574 else
575 count = 0;
576
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000577 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000578 deleted = (long)(count - (lnum - eap->line2));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000579 if (deleted > 0)
580 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
581 else if (deleted < 0)
582 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
583 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000584
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000585 curwin->w_cursor.lnum = eap->line1;
586 beginline(BL_WHITE | BL_FIX);
587
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000588sortend:
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000589 vim_free(nrs);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000590 vim_free(sortbuf1);
591 vim_free(sortbuf2);
Bram Moolenaar473de612013-06-08 18:19:48 +0200592 vim_regfree(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000593 if (got_int)
594 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000595}
596
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597/*
598 * ":retab".
599 */
600 void
601ex_retab(eap)
602 exarg_T *eap;
603{
604 linenr_T lnum;
605 int got_tab = FALSE;
606 long num_spaces = 0;
607 long num_tabs;
608 long len;
609 long col;
610 long vcol;
611 long start_col = 0; /* For start of white-space string */
612 long start_vcol = 0; /* For start of white-space string */
613 int temp;
614 long old_len;
615 char_u *ptr;
616 char_u *new_line = (char_u *)1; /* init to non-NULL */
617 int did_undo; /* called u_save for current line */
618 int new_ts;
619 int save_list;
620 linenr_T first_line = 0; /* first changed line */
621 linenr_T last_line = 0; /* last changed line */
622
623 save_list = curwin->w_p_list;
624 curwin->w_p_list = 0; /* don't want list mode here */
625
626 new_ts = getdigits(&(eap->arg));
627 if (new_ts < 0)
628 {
629 EMSG(_(e_positive));
630 return;
631 }
632 if (new_ts == 0)
633 new_ts = curbuf->b_p_ts;
634 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
635 {
636 ptr = ml_get(lnum);
637 col = 0;
638 vcol = 0;
639 did_undo = FALSE;
640 for (;;)
641 {
642 if (vim_iswhite(ptr[col]))
643 {
644 if (!got_tab && num_spaces == 0)
645 {
646 /* First consecutive white-space */
647 start_vcol = vcol;
648 start_col = col;
649 }
650 if (ptr[col] == ' ')
651 num_spaces++;
652 else
653 got_tab = TRUE;
654 }
655 else
656 {
657 if (got_tab || (eap->forceit && num_spaces > 1))
658 {
659 /* Retabulate this string of white-space */
660
661 /* len is virtual length of white string */
662 len = num_spaces = vcol - start_vcol;
663 num_tabs = 0;
664 if (!curbuf->b_p_et)
665 {
666 temp = new_ts - (start_vcol % new_ts);
667 if (num_spaces >= temp)
668 {
669 num_spaces -= temp;
670 num_tabs++;
671 }
672 num_tabs += num_spaces / new_ts;
673 num_spaces -= (num_spaces / new_ts) * new_ts;
674 }
675 if (curbuf->b_p_et || got_tab ||
676 (num_spaces + num_tabs < len))
677 {
678 if (did_undo == FALSE)
679 {
680 did_undo = TRUE;
681 if (u_save((linenr_T)(lnum - 1),
682 (linenr_T)(lnum + 1)) == FAIL)
683 {
684 new_line = NULL; /* flag out-of-memory */
685 break;
686 }
687 }
688
689 /* len is actual number of white characters used */
690 len = num_spaces + num_tabs;
691 old_len = (long)STRLEN(ptr);
692 new_line = lalloc(old_len - col + start_col + len + 1,
693 TRUE);
694 if (new_line == NULL)
695 break;
696 if (start_col > 0)
697 mch_memmove(new_line, ptr, (size_t)start_col);
698 mch_memmove(new_line + start_col + len,
699 ptr + col, (size_t)(old_len - col + 1));
700 ptr = new_line + start_col;
701 for (col = 0; col < len; col++)
702 ptr[col] = (col < num_tabs) ? '\t' : ' ';
703 ml_replace(lnum, new_line, FALSE);
704 if (first_line == 0)
705 first_line = lnum;
706 last_line = lnum;
707 ptr = new_line;
708 col = start_col + len;
709 }
710 }
711 got_tab = FALSE;
712 num_spaces = 0;
713 }
714 if (ptr[col] == NUL)
715 break;
716 vcol += chartabsize(ptr + col, (colnr_T)vcol);
717#ifdef FEAT_MBYTE
718 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000719 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 else
721#endif
722 ++col;
723 }
724 if (new_line == NULL) /* out of memory */
725 break;
726 line_breakcheck();
727 }
728 if (got_int)
729 EMSG(_(e_interr));
730
731 if (curbuf->b_p_ts != new_ts)
732 redraw_curbuf_later(NOT_VALID);
733 if (first_line != 0)
734 changed_lines(first_line, 0, last_line + 1, 0L);
735
736 curwin->w_p_list = save_list; /* restore 'list' */
737
738 curbuf->b_p_ts = new_ts;
739 coladvance(curwin->w_curswant);
740
741 u_clearline();
742}
743#endif
744
745/*
746 * :move command - move lines line1-line2 to line dest
747 *
748 * return FAIL for failure, OK otherwise
749 */
750 int
751do_move(line1, line2, dest)
752 linenr_T line1;
753 linenr_T line2;
754 linenr_T dest;
755{
756 char_u *str;
757 linenr_T l;
758 linenr_T extra; /* Num lines added before line1 */
759 linenr_T num_lines; /* Num lines moved */
760 linenr_T last_line; /* Last line in file after adding new text */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200761#ifdef FEAT_FOLDING
762 int isFolded;
763
764 /* Moving lines seems to corrupt the folds, delete folding info now
765 * and recreate it when finished. Don't do this for manual folding, it
766 * would delete all folds. */
767 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin);
768 if (isFolded)
769 deleteFoldRecurse(&curwin->w_folds);
770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 if (dest >= line1 && dest < line2)
773 {
774 EMSG(_("E134: Move lines into themselves"));
775 return FAIL;
776 }
777
778 num_lines = line2 - line1 + 1;
779
780 /*
781 * First we copy the old text to its new location -- webb
782 * Also copy the flag that ":global" command uses.
783 */
784 if (u_save(dest, dest + 1) == FAIL)
785 return FAIL;
786 for (extra = 0, l = line1; l <= line2; l++)
787 {
788 str = vim_strsave(ml_get(l + extra));
789 if (str != NULL)
790 {
791 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
792 vim_free(str);
793 if (dest < line1)
794 extra++;
795 }
796 }
797
798 /*
799 * Now we must be careful adjusting our marks so that we don't overlap our
800 * mark_adjust() calls.
801 *
802 * We adjust the marks within the old text so that they refer to the
803 * last lines of the file (temporarily), because we know no other marks
804 * will be set there since these line numbers did not exist until we added
805 * our new lines.
806 *
807 * Then we adjust the marks on lines between the old and new text positions
808 * (either forwards or backwards).
809 *
810 * And Finally we adjust the marks we put at the end of the file back to
811 * their final destination at the new text position -- webb
812 */
813 last_line = curbuf->b_ml.ml_line_count;
814 mark_adjust(line1, line2, last_line - line2, 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200815 changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (dest >= line2)
817 {
818 mark_adjust(line2 + 1, dest, -num_lines, 0L);
819 curbuf->b_op_start.lnum = dest - num_lines + 1;
820 curbuf->b_op_end.lnum = dest;
821 }
822 else
823 {
824 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
825 curbuf->b_op_start.lnum = dest + 1;
826 curbuf->b_op_end.lnum = dest + num_lines;
827 }
828 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
829 mark_adjust(last_line - num_lines + 1, last_line,
830 -(last_line - dest - extra), 0L);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200831 changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 /*
834 * Now we delete the original text -- webb
835 */
836 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
837 return FAIL;
838
839 for (l = line1; l <= line2; l++)
840 ml_delete(line1 + extra, TRUE);
841
842 if (!global_busy && num_lines > p_report)
843 {
844 if (num_lines == 1)
845 MSG(_("1 line moved"));
846 else
847 smsg((char_u *)_("%ld lines moved"), num_lines);
848 }
849
850 /*
851 * Leave the cursor on the last of the moved lines.
852 */
853 if (dest >= line1)
854 curwin->w_cursor.lnum = dest;
855 else
856 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
857
858 if (line1 < dest)
Bram Moolenaar0612ec82011-11-30 17:01:58 +0100859 {
860 dest += num_lines + 1;
861 last_line = curbuf->b_ml.ml_line_count;
862 if (dest > last_line + 1)
863 dest = last_line + 1;
864 changed_lines(line1, 0, dest, 0L);
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 else
867 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
868
Bram Moolenaard5f69332015-04-15 12:43:50 +0200869#ifdef FEAT_FOLDING
870 /* recreate folds */
871 if (isFolded)
872 foldUpdateAll(curwin);
873#endif
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 return OK;
876}
877
878/*
879 * ":copy"
880 */
881 void
882ex_copy(line1, line2, n)
883 linenr_T line1;
884 linenr_T line2;
885 linenr_T n;
886{
887 linenr_T count;
888 char_u *p;
889
890 count = line2 - line1 + 1;
891 curbuf->b_op_start.lnum = n + 1;
892 curbuf->b_op_end.lnum = n + count;
893 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
894
895 /*
896 * there are three situations:
897 * 1. destination is above line1
898 * 2. destination is between line1 and line2
899 * 3. destination is below line2
900 *
901 * n = destination (when starting)
902 * curwin->w_cursor.lnum = destination (while copying)
903 * line1 = start of source (while copying)
904 * line2 = end of source (while copying)
905 */
906 if (u_save(n, n + 1) == FAIL)
907 return;
908
909 curwin->w_cursor.lnum = n;
910 while (line1 <= line2)
911 {
912 /* need to use vim_strsave() because the line will be unlocked within
913 * ml_append() */
914 p = vim_strsave(ml_get(line1));
915 if (p != NULL)
916 {
917 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
918 vim_free(p);
919 }
920 /* situation 2: skip already copied lines */
921 if (line1 == n)
922 line1 = curwin->w_cursor.lnum;
923 ++line1;
924 if (curwin->w_cursor.lnum < line1)
925 ++line1;
926 if (curwin->w_cursor.lnum < line2)
927 ++line2;
928 ++curwin->w_cursor.lnum;
929 }
930
931 appended_lines_mark(n, count);
932
933 msgmore((long)count);
934}
935
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000936static char_u *prevcmd = NULL; /* the previous command */
937
938#if defined(EXITFREE) || defined(PROTO)
939 void
940free_prev_shellcmd()
941{
942 vim_free(prevcmd);
943}
944#endif
945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946/*
947 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
948 * Bangs in the argument are replaced with the previously entered command.
949 * Remember the argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 */
951 void
952do_bang(addr_count, eap, forceit, do_in, do_out)
953 int addr_count;
954 exarg_T *eap;
955 int forceit;
956 int do_in, do_out;
957{
958 char_u *arg = eap->arg; /* command */
959 linenr_T line1 = eap->line1; /* start of range */
960 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 char_u *newcmd = NULL; /* the new command */
962 int free_newcmd = FALSE; /* need to free() newcmd */
963 int ins_prevcmd;
964 char_u *t;
965 char_u *p;
966 char_u *trailarg;
967 int len;
968 int scroll_save = msg_scroll;
969
970 /*
971 * Disallow shell commands for "rvim".
972 * Disallow shell commands from .exrc and .vimrc in current directory for
973 * security reasons.
974 */
975 if (check_restricted() || check_secure())
976 return;
977
978 if (addr_count == 0) /* :! */
979 {
980 msg_scroll = FALSE; /* don't scroll here */
981 autowrite_all();
982 msg_scroll = scroll_save;
983 }
984
985 /*
986 * Try to find an embedded bang, like in :!<cmd> ! [args]
987 * (:!! is indicated by the 'forceit' variable)
988 */
989 ins_prevcmd = forceit;
990 trailarg = arg;
991 do
992 {
993 len = (int)STRLEN(trailarg) + 1;
994 if (newcmd != NULL)
995 len += (int)STRLEN(newcmd);
996 if (ins_prevcmd)
997 {
998 if (prevcmd == NULL)
999 {
1000 EMSG(_(e_noprev));
1001 vim_free(newcmd);
1002 return;
1003 }
1004 len += (int)STRLEN(prevcmd);
1005 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001006 if ((t = alloc((unsigned)len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
1008 vim_free(newcmd);
1009 return;
1010 }
1011 *t = NUL;
1012 if (newcmd != NULL)
1013 STRCAT(t, newcmd);
1014 if (ins_prevcmd)
1015 STRCAT(t, prevcmd);
1016 p = t + STRLEN(t);
1017 STRCAT(t, trailarg);
1018 vim_free(newcmd);
1019 newcmd = t;
1020
1021 /*
1022 * Scan the rest of the argument for '!', which is replaced by the
1023 * previous command. "\!" is replaced by "!" (this is vi compatible).
1024 */
1025 trailarg = NULL;
1026 while (*p)
1027 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02001028 if (*p == '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 if (p > newcmd && p[-1] == '\\')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001031 STRMOVE(p - 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 else
1033 {
1034 trailarg = p;
1035 *trailarg++ = NUL;
1036 ins_prevcmd = TRUE;
1037 break;
1038 }
1039 }
1040 ++p;
1041 }
1042 } while (trailarg != NULL);
1043
1044 vim_free(prevcmd);
1045 prevcmd = newcmd;
1046
1047 if (bangredo) /* put cmd in redo buffer for ! command */
1048 {
Bram Moolenaar529d2d62014-03-19 17:41:23 +01001049 /* If % or # appears in the command, it must have been escaped.
1050 * Reescape them, so that redoing them does not substitute them by the
1051 * buffername. */
1052 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
1053
1054 if (cmd != NULL)
1055 {
1056 AppendToRedobuffLit(cmd, -1);
1057 vim_free(cmd);
1058 }
1059 else
1060 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 AppendToRedobuff((char_u *)"\n");
1062 bangredo = FALSE;
1063 }
1064 /*
1065 * Add quotes around the command, for shells that need them.
1066 */
1067 if (*p_shq != NUL)
1068 {
1069 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
1070 if (newcmd == NULL)
1071 return;
1072 STRCPY(newcmd, p_shq);
1073 STRCAT(newcmd, prevcmd);
1074 STRCAT(newcmd, p_shq);
1075 free_newcmd = TRUE;
1076 }
1077 if (addr_count == 0) /* :! */
1078 {
1079 /* echo the command */
1080 msg_start();
1081 msg_putchar(':');
1082 msg_putchar('!');
1083 msg_outtrans(newcmd);
1084 msg_clr_eos();
1085 windgoto(msg_row, msg_col);
1086
1087 do_shell(newcmd, 0);
1088 }
1089 else /* :range! */
Bram Moolenaarade00832006-03-10 21:46:58 +00001090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 /* Careful: This may recursively call do_bang() again! (because of
1092 * autocommands) */
1093 do_filter(line1, line2, eap, newcmd, do_in, do_out);
Bram Moolenaarade00832006-03-10 21:46:58 +00001094#ifdef FEAT_AUTOCMD
1095 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1096#endif
1097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (free_newcmd)
1099 vim_free(newcmd);
1100}
1101
1102/*
1103 * do_filter: filter lines through a command given by the user
1104 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 * We mostly use temp files and the call_shell() routine here. This would
1106 * normally be done using pipes on a UNIX machine, but this is more portable
1107 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 * to deal with redirection somehow, and should handle things like looking
1109 * at the PATH env. variable, and adding reasonable extensions to the
1110 * command name given by the user. All reasonable versions of call_shell()
1111 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112 * Alternatively, if on Unix and redirecting input or output, but not both,
1113 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * We use input redirection if do_in is TRUE.
1115 * We use output redirection if do_out is TRUE.
1116 */
1117 static void
1118do_filter(line1, line2, eap, cmd, do_in, do_out)
1119 linenr_T line1, line2;
1120 exarg_T *eap; /* for forced 'ff' and 'fenc' */
1121 char_u *cmd;
1122 int do_in, do_out;
1123{
1124 char_u *itmp = NULL;
1125 char_u *otmp = NULL;
1126 linenr_T linecount;
1127 linenr_T read_linecount;
1128 pos_T cursor_save;
1129 char_u *cmd_buf;
1130#ifdef FEAT_AUTOCMD
1131 buf_T *old_curbuf = curbuf;
1132#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 if (*cmd == NUL) /* no filter command */
1136 return;
1137
1138#ifdef WIN3264
1139 /*
1140 * Check if external commands are allowed now.
1141 */
1142 if (can_end_termcap_mode(TRUE) == FALSE)
1143 return;
1144#endif
1145
1146 cursor_save = curwin->w_cursor;
1147 linecount = line2 - line1 + 1;
1148 curwin->w_cursor.lnum = line1;
1149 curwin->w_cursor.col = 0;
1150 changed_line_abv_curs();
1151 invalidate_botline();
1152
1153 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 * When using temp files:
1155 * 1. * Form temp file names
1156 * 2. * Write the lines to a temp file
1157 * 3. Run the filter command on the temp file
1158 * 4. * Read the output of the command into the buffer
1159 * 5. * Delete the original lines to be filtered
1160 * 6. * Remove the temp files
1161 *
1162 * When writing the input with a pipe or when catching the output with a
1163 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 */
1165
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (do_out)
1167 shell_flags |= SHELL_DOOUT;
1168
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02001169#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1173 shell_flags |= SHELL_READ;
1174 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 else if (do_in && !do_out && !p_stmp)
1177 {
1178 /* Use a pipe to write stdin of the command, do not use a temp file. */
1179 shell_flags |= SHELL_WRITE;
1180 curbuf->b_op_start.lnum = line1;
1181 curbuf->b_op_end.lnum = line2;
1182 }
1183 else if (do_in && do_out && !p_stmp)
1184 {
1185 /* Use a pipe to write stdin and fetch stdout of the command, do not
1186 * use a temp file. */
1187 shell_flags |= SHELL_READ|SHELL_WRITE;
1188 curbuf->b_op_start.lnum = line1;
1189 curbuf->b_op_end.lnum = line2;
1190 curwin->w_cursor.lnum = line2;
1191 }
1192 else
1193#endif
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001194 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
1195 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 {
1197 EMSG(_(e_notmp));
1198 goto filterend;
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201/*
1202 * The writing and reading of temp files will not be shown.
1203 * Vi also doesn't do this and the messages are not very informative.
1204 */
1205 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 FALSE, FALSE, FALSE, TRUE) == FAIL)
1208 {
1209 msg_putchar('\n'); /* keep message from buf_write() */
1210 --no_wait_return;
1211#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1212 if (!aborting())
1213#endif
1214 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1215 goto filterend;
1216 }
1217#ifdef FEAT_AUTOCMD
1218 if (curbuf != old_curbuf)
1219 goto filterend;
1220#endif
1221
1222 if (!do_out)
1223 msg_putchar('\n');
1224
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001225 /* Create the shell command in allocated memory. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1227 if (cmd_buf == NULL)
1228 goto filterend;
1229
1230 windgoto((int)Rows - 1, 0);
1231 cursor_on();
1232
1233 /*
1234 * When not redirecting the output the command can write anything to the
1235 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1236 * stderr output of external command. Clear the screen later.
1237 * If do_in is FALSE, this could be something like ":r !cat", which may
1238 * also mess up the screen, clear it later.
1239 */
1240 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1241 redraw_later_clear();
1242
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 if (do_out)
1244 {
1245 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001246 {
1247 vim_free(cmd_buf);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248 goto error;
Bram Moolenaara9aafe52008-05-07 11:10:28 +00001249 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001250 redraw_curbuf_later(VALID);
1251 }
1252 read_linecount = curbuf->b_ml.ml_line_count;
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 /*
1255 * When call_shell() fails wait_return() is called to give the user a
1256 * chance to read the error messages. Otherwise errors are ignored, so you
1257 * can see the error messages from the command that appear on stdout; use
1258 * 'u' to fix the text
1259 * Switch to cooked mode when not redirecting stdin, avoids that something
1260 * like ":r !cat" hangs.
1261 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1262 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001263 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 redraw_later_clear();
1266 wait_return(FALSE);
1267 }
1268 vim_free(cmd_buf);
1269
1270 did_check_timestamps = FALSE;
1271 need_check_timestamps = TRUE;
1272
1273 /* When interrupting the shell command, it may still have produced some
1274 * useful output. Reset got_int here, so that readfile() won't cancel
1275 * reading. */
1276 ui_breakcheck();
1277 got_int = FALSE;
1278
1279 if (do_out)
1280 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001281 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001283 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1284 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001286#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1287 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001289 {
1290 msg_putchar('\n');
1291 EMSG2(_(e_notread), otmp);
1292 }
1293 goto error;
1294 }
1295#ifdef FEAT_AUTOCMD
1296 if (curbuf != old_curbuf)
1297 goto filterend;
1298#endif
1299 }
1300
1301 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1302
1303 if (shell_flags & SHELL_READ)
1304 {
1305 curbuf->b_op_start.lnum = line2 + 1;
1306 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1307 appended_lines_mark(line2, read_linecount);
1308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310 if (do_in)
1311 {
1312 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (read_linecount >= linecount)
1315 /* move all marks from old lines to new lines */
1316 mark_adjust(line1, line2, linecount, 0L);
1317 else
1318 {
1319 /* move marks from old lines to new lines, delete marks
1320 * that are in deleted lines */
1321 mark_adjust(line1, line1 + read_linecount - 1,
1322 linecount, 0L);
1323 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1324 }
1325 }
1326
1327 /*
1328 * Put cursor on first filtered line for ":range!cmd".
1329 * Adjust '[ and '] (set by buf_write()).
1330 */
1331 curwin->w_cursor.lnum = line1;
1332 del_lines(linecount, TRUE);
1333 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1334 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1335 write_lnum_adjust(-linecount); /* adjust last line
1336 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337#ifdef FEAT_FOLDING
1338 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
1341 else
1342 {
1343 /*
1344 * Put cursor on last new line for ":r !cmd".
1345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1351 --no_wait_return;
1352
1353 if (linecount > p_report)
1354 {
1355 if (do_in)
1356 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001357 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1358 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001361 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else
1364 msgmore((long)linecount);
1365 }
1366 }
1367 else
1368 {
1369error:
1370 /* put cursor back in same position for ":w !cmd" */
1371 curwin->w_cursor = cursor_save;
1372 --no_wait_return;
1373 wait_return(FALSE);
1374 }
1375
1376filterend:
1377
1378#ifdef FEAT_AUTOCMD
1379 if (curbuf != old_curbuf)
1380 {
1381 --no_wait_return;
1382 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1383 }
1384#endif
1385 if (itmp != NULL)
1386 mch_remove(itmp);
1387 if (otmp != NULL)
1388 mch_remove(otmp);
1389 vim_free(itmp);
1390 vim_free(otmp);
1391}
1392
1393/*
1394 * Call a shell to execute a command.
1395 * When "cmd" is NULL start an interactive shell.
1396 */
1397 void
1398do_shell(cmd, flags)
1399 char_u *cmd;
1400 int flags; /* may be SHELL_DOOUT when output is redirected */
1401{
1402 buf_T *buf;
1403#ifndef FEAT_GUI_MSWIN
1404 int save_nwr;
1405#endif
1406#ifdef MSWIN
1407 int winstart = FALSE;
1408#endif
1409
1410 /*
1411 * Disallow shell commands for "rvim".
1412 * Disallow shell commands from .exrc and .vimrc in current directory for
1413 * security reasons.
1414 */
1415 if (check_restricted() || check_secure())
1416 {
1417 msg_end();
1418 return;
1419 }
1420
1421#ifdef MSWIN
1422 /*
1423 * Check if external commands are allowed now.
1424 */
1425 if (can_end_termcap_mode(TRUE) == FALSE)
1426 return;
1427
1428 /*
1429 * Check if ":!start" is used.
1430 */
1431 if (cmd != NULL)
1432 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1433#endif
1434
1435 /*
1436 * For autocommands we want to get the output on the current screen, to
1437 * avoid having to type return below.
1438 */
1439 msg_putchar('\r'); /* put cursor at start of line */
1440#ifdef FEAT_AUTOCMD
1441 if (!autocmd_busy)
1442#endif
1443 {
1444#ifdef MSWIN
1445 if (!winstart)
1446#endif
1447 stoptermcap();
1448 }
1449#ifdef MSWIN
1450 if (!winstart)
1451#endif
1452 msg_putchar('\n'); /* may shift screen one line up */
1453
1454 /* warning message before calling the shell */
1455 if (p_warn
1456#ifdef FEAT_AUTOCMD
1457 && !autocmd_busy
1458#endif
1459 && msg_silent == 0)
1460 for (buf = firstbuf; buf; buf = buf->b_next)
1461 if (bufIsChanged(buf))
1462 {
1463#ifdef FEAT_GUI_MSWIN
1464 if (!winstart)
1465 starttermcap(); /* don't want a message box here */
1466#endif
1467 MSG_PUTS(_("[No write since last change]\n"));
1468#ifdef FEAT_GUI_MSWIN
1469 if (!winstart)
1470 stoptermcap();
1471#endif
1472 break;
1473 }
1474
1475 /* This windgoto is required for when the '\n' resulted in a "delete line
1476 * 1" command to the terminal. */
1477 if (!swapping_screen())
1478 windgoto(msg_row, msg_col);
1479 cursor_on();
1480 (void)call_shell(cmd, SHELL_COOKED | flags);
1481 did_check_timestamps = FALSE;
1482 need_check_timestamps = TRUE;
1483
1484 /*
1485 * put the message cursor at the end of the screen, avoids wait_return()
1486 * to overwrite the text that the external command showed
1487 */
1488 if (!swapping_screen())
1489 {
1490 msg_row = Rows - 1;
1491 msg_col = 0;
1492 }
1493
1494#ifdef FEAT_AUTOCMD
1495 if (autocmd_busy)
1496 {
1497 if (msg_silent == 0)
1498 redraw_later_clear();
1499 }
1500 else
1501#endif
1502 {
1503 /*
1504 * For ":sh" there is no need to call wait_return(), just redraw.
1505 * Also for the Win32 GUI (the output is in a console window).
1506 * Otherwise there is probably text on the screen that the user wants
1507 * to read before redrawing, so call wait_return().
1508 */
1509#ifndef FEAT_GUI_MSWIN
1510 if (cmd == NULL
1511# ifdef WIN3264
1512 || (winstart && !need_wait_return)
1513# endif
1514 )
1515 {
1516 if (msg_silent == 0)
1517 redraw_later_clear();
1518 need_wait_return = FALSE;
1519 }
1520 else
1521 {
1522 /*
1523 * If we switch screens when starttermcap() is called, we really
1524 * want to wait for "hit return to continue".
1525 */
1526 save_nwr = no_wait_return;
1527 if (swapping_screen())
1528 no_wait_return = FALSE;
1529# ifdef AMIGA
1530 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1531# else
1532 wait_return(msg_silent == 0);
1533# endif
1534 no_wait_return = save_nwr;
1535 }
1536#endif /* FEAT_GUI_W32 */
1537
1538#ifdef MSWIN
1539 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1540#endif
1541 starttermcap(); /* start termcap if not done by wait_return() */
1542
1543 /*
1544 * In an Amiga window redrawing is caused by asking the window size.
1545 * If we got an interrupt this will not work. The chance that the
1546 * window size is wrong is very small, but we need to redraw the
1547 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1548 * but it saves an extra redraw.
1549 */
1550#ifdef AMIGA
1551 if (skip_redraw) /* ':' hit in wait_return() */
1552 {
1553 if (msg_silent == 0)
1554 redraw_later_clear();
1555 }
1556 else if (term_console)
1557 {
1558 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1559 if (got_int && msg_silent == 0)
1560 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1561 else
1562 must_redraw = 0; /* no extra redraw needed */
1563 }
1564#endif
1565 }
1566
1567 /* display any error messages now */
1568 display_errors();
Bram Moolenaarade00832006-03-10 21:46:58 +00001569
1570#ifdef FEAT_AUTOCMD
1571 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573}
1574
1575/*
1576 * Create a shell command from a command string, input redirection file and
1577 * output redirection file.
1578 * Returns an allocated string with the shell command, or NULL for failure.
1579 */
1580 char_u *
1581make_filter_cmd(cmd, itmp, otmp)
1582 char_u *cmd; /* command */
1583 char_u *itmp; /* NULL or name of input file */
1584 char_u *otmp; /* NULL or name of output file */
1585{
1586 char_u *buf;
1587 long_u len;
1588
Bram Moolenaar53076832015-12-31 19:53:21 +01001589#if defined(UNIX)
Bram Moolenaard442ec72014-05-09 20:33:04 +02001590 int is_fish_shell;
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001591 char_u *shell_name = get_isolated_shell_name();
Bram Moolenaard442ec72014-05-09 20:33:04 +02001592
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001593 /* Account for fish's different syntax for subshells */
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02001594 is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
1595 vim_free(shell_name);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001596 if (is_fish_shell)
1597 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */
1598 else
1599#endif
1600 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (itmp != NULL)
1602 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1603 if (otmp != NULL)
1604 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1605 buf = lalloc(len, TRUE);
1606 if (buf == NULL)
1607 return NULL;
1608
Bram Moolenaar53076832015-12-31 19:53:21 +01001609#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001611 * Put braces around the command (for concatenated commands) when
1612 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001614 if (itmp != NULL || otmp != NULL)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001615 {
1616 if (is_fish_shell)
1617 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
1618 else
1619 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
1620 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001621 else
1622 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (itmp != NULL)
1624 {
1625 STRCAT(buf, " < ");
1626 STRCAT(buf, itmp);
1627 }
1628#else
1629 /*
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001630 * For shells that don't understand braces around commands, at least allow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 * the use of commands in a pipe.
1632 */
1633 STRCPY(buf, cmd);
1634 if (itmp != NULL)
1635 {
1636 char_u *p;
1637
1638 /*
1639 * If there is a pipe, we have to put the '<' in front of it.
1640 * Don't do this when 'shellquote' is not empty, otherwise the
1641 * redirection would be inside the quotes.
1642 */
1643 if (*p_shq == NUL)
1644 {
1645 p = vim_strchr(buf, '|');
1646 if (p != NULL)
1647 *p = NUL;
1648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1650 STRCAT(buf, itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (*p_shq == NUL)
1652 {
1653 p = vim_strchr(cmd, '|');
1654 if (p != NULL)
1655 {
1656 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1657 STRCAT(buf, p);
1658 }
1659 }
1660 }
1661#endif
1662 if (otmp != NULL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001663 append_redir(buf, (int)len, p_srr, otmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 return buf;
1666}
1667
1668/*
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001669 * Append output redirection for file "fname" to the end of string buffer
1670 * "buf[buflen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 * Works with the 'shellredir' and 'shellpipe' options.
1672 * The caller should make sure that there is enough room:
1673 * STRLEN(opt) + STRLEN(fname) + 3
1674 */
1675 void
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001676append_redir(buf, buflen, opt, fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 char_u *buf;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001678 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 char_u *opt;
1680 char_u *fname;
1681{
1682 char_u *p;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001683 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001685 end = buf + STRLEN(buf);
Bram Moolenaar542805a2013-08-02 14:15:13 +02001686 /* find "%s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
Bram Moolenaar542805a2013-08-02 14:15:13 +02001688 {
1689 if (p[1] == 's') /* found %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 break;
Bram Moolenaar542805a2013-08-02 14:15:13 +02001691 if (p[1] == '%') /* skip %% */
1692 ++p;
1693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (p != NULL)
1695 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001696 *end = ' '; /* not really needed? Not with sh, ksh or bash */
1697 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
1698 (char *)opt, (char *)fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 else
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001701 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 " %s %s",
1704#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 " %s%s", /* " > %s" causes problems on Amiga */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#endif
1707 (char *)opt, (char *)fname);
1708}
1709
1710#ifdef FEAT_VIMINFO
1711
1712static int no_viminfo __ARGS((void));
1713static int viminfo_errcnt;
1714
1715 static int
1716no_viminfo()
1717{
1718 /* "vim -i NONE" does not read or write a viminfo file */
1719 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1720}
1721
1722/*
1723 * Report an error for reading a viminfo file.
1724 * Count the number of errors. When there are more than 10, return TRUE.
1725 */
1726 int
1727viminfo_error(errnum, message, line)
1728 char *errnum;
1729 char *message;
1730 char_u *line;
1731{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001732 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1733 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001734 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001735 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1736 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 emsg(IObuff);
1738 if (++viminfo_errcnt >= 10)
1739 {
1740 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1741 return TRUE;
1742 }
1743 return FALSE;
1744}
1745
1746/*
1747 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001748 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 */
1750 int
Bram Moolenaard812df62008-11-09 12:46:09 +00001751read_viminfo(file, flags)
1752 char_u *file; /* file name or NULL to use default name */
1753 int flags; /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 FILE *fp;
1756 char_u *fname;
1757
1758 if (no_viminfo())
1759 return FAIL;
1760
Bram Moolenaard812df62008-11-09 12:46:09 +00001761 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (fname == NULL)
1763 return FAIL;
1764 fp = mch_fopen((char *)fname, READBIN);
1765
1766 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001767 {
1768 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001769 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1770 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001771 (flags & VIF_WANT_INFO) ? _(" info") : "",
1772 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1773 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001774 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001775 verbose_leave();
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 vim_free(fname);
1779 if (fp == NULL)
1780 return FAIL;
1781
1782 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001783 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 return OK;
1787}
1788
1789/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001790 * Write the viminfo file. The old one is read in first so that effectively a
1791 * merge of current info and old info is done. This allows multiple vims to
1792 * run simultaneously, without losing any marks etc.
1793 * If "forceit" is TRUE, then the old file is not read in, and only internal
1794 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 */
1796 void
1797write_viminfo(file, forceit)
1798 char_u *file;
1799 int forceit;
1800{
1801 char_u *fname;
1802 FILE *fp_in = NULL; /* input viminfo file, if any */
1803 FILE *fp_out = NULL; /* output viminfo file */
1804 char_u *tempname = NULL; /* name of temp viminfo file */
1805 struct stat st_new; /* mch_stat() of potential new file */
1806 char_u *wp;
1807#if defined(UNIX) || defined(VMS)
1808 mode_t umask_save;
1809#endif
1810#ifdef UNIX
1811 int shortname = FALSE; /* use 8.3 file name */
1812 struct stat st_old; /* mch_stat() of existing viminfo file */
1813#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001814#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001815 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 if (no_viminfo())
1819 return;
1820
1821 fname = viminfo_filename(file); /* may set to default if NULL */
1822 if (fname == NULL)
1823 return;
1824
1825 fp_in = mch_fopen((char *)fname, READBIN);
1826 if (fp_in == NULL)
1827 {
1828 /* if it does exist, but we can't read it, don't try writing */
1829 if (mch_stat((char *)fname, &st_new) == 0)
1830 goto end;
1831#if defined(UNIX) || defined(VMS)
1832 /*
1833 * For Unix we create the .viminfo non-accessible for others,
1834 * because it may contain text from non-accessible documents.
1835 */
1836 umask_save = umask(077);
1837#endif
1838 fp_out = mch_fopen((char *)fname, WRITEBIN);
1839#if defined(UNIX) || defined(VMS)
1840 (void)umask(umask_save);
1841#endif
1842 }
1843 else
1844 {
1845 /*
1846 * There is an existing viminfo file. Create a temporary file to
1847 * write the new viminfo into, in the same directory as the
1848 * existing viminfo file, which will be renamed later.
1849 */
1850#ifdef UNIX
1851 /*
1852 * For Unix we check the owner of the file. It's not very nice to
1853 * overwrite a user's viminfo file after a "su root", with a
1854 * viminfo file that the user can't read.
1855 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001856 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001857 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001859 if (mch_stat((char *)fname, &st_old) == 0
1860 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001861 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 ? (st_old.st_mode & 0200)
1863 : (st_old.st_gid == getgid()
1864 ? (st_old.st_mode & 0020)
1865 : (st_old.st_mode & 0002))))
1866 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001867 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1871 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001872 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 goto end;
1874 }
1875#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001876#ifdef WIN3264
1877 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001878 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 /*
1882 * Make tempname.
1883 * May try twice: Once normal and once with shortname set, just in
1884 * case somebody puts his viminfo file in an 8.3 filesystem.
1885 */
1886 for (;;)
1887 {
1888 tempname = buf_modname(
1889#ifdef UNIX
1890 shortname,
1891#else
1892# ifdef SHORT_FNAME
1893 TRUE,
1894# else
1895# ifdef FEAT_GUI_W32
1896 gui_is_win32s(),
1897# else
1898 FALSE,
1899# endif
1900# endif
1901#endif
1902 fname,
1903#ifdef VMS
1904 (char_u *)"-tmp",
1905#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907#endif
1908 FALSE);
1909 if (tempname == NULL) /* out of memory */
1910 break;
1911
1912 /*
1913 * Check if tempfile already exists. Never overwrite an
1914 * existing file!
1915 */
1916 if (mch_stat((char *)tempname, &st_new) == 0)
1917 {
1918#ifdef UNIX
1919 /*
1920 * Check if tempfile is same as original file. May happen
1921 * when modname() gave the same file back. E.g. silly
1922 * link, or file name-length reached. Try again with
1923 * shortname set.
1924 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001925 if (!shortname && st_new.st_dev == st_old.st_dev
1926 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
1928 vim_free(tempname);
1929 tempname = NULL;
1930 shortname = TRUE;
1931 continue;
1932 }
1933#endif
1934 /*
1935 * Try another name. Change one character, just before
1936 * the extension. This should also work for an 8.3
1937 * file name, when after adding the extension it still is
1938 * the same file as the original.
1939 */
1940 wp = tempname + STRLEN(tempname) - 5;
1941 if (wp < gettail(tempname)) /* empty file name? */
1942 wp = gettail(tempname);
1943 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1944 --*wp)
1945 {
1946 /*
1947 * They all exist? Must be something wrong! Don't
1948 * write the viminfo file then.
1949 */
1950 if (*wp == 'a')
1951 {
1952 vim_free(tempname);
1953 tempname = NULL;
1954 break;
1955 }
1956 }
1957 }
1958 break;
1959 }
1960
1961 if (tempname != NULL)
1962 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00001963#ifdef VMS
1964 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00001965 umask_save = umask(077);
1966 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1967 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001968#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00001969 int fd;
1970
1971 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001972 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001973 * Unix: same as original file, but strip s-bit. Reset umask to
1974 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001975 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00001976# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001977 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00001978 fd = mch_open((char *)tempname,
1979 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001980 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001981 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001982# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001983 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001984 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001985# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00001986 if (fd < 0)
1987 fp_out = NULL;
1988 else
1989 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001990#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 /*
1993 * If we can't create in the same directory, try creating a
1994 * "normal" temp file.
1995 */
1996 if (fp_out == NULL)
1997 {
1998 vim_free(tempname);
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001999 if ((tempname = vim_tempname('o', TRUE)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2001 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00002002
2003#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00002005 * Make sure the owner can read/write it. This only works for
2006 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
2008 if (fp_out != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002009 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#endif
2011 }
2012 }
2013
2014 /*
2015 * Check if the new viminfo file can be written to.
2016 */
2017 if (fp_out == NULL)
2018 {
2019 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002020 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (fp_in != NULL)
2022 fclose(fp_in);
2023 goto end;
2024 }
2025
2026 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002027 {
2028 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002029 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002030 verbose_leave();
2031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002034 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 fclose(fp_out); /* errors are ignored !? */
2037 if (fp_in != NULL)
2038 {
2039 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002040
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002041 /* In case of an error keep the original viminfo file. Otherwise
2042 * rename the newly written file. Give an error if that fails. */
2043 if (viminfo_errcnt == 0 && vim_rename(tempname, fname) == -1)
2044 {
2045 ++viminfo_errcnt;
2046 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2047 }
2048 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002050
2051#ifdef WIN3264
2052 /* If the viminfo file was hidden then also hide the new file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002053 if (hidden)
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002054 mch_hide(fname);
2055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058end:
2059 vim_free(fname);
2060 vim_free(tempname);
2061}
2062
2063/*
2064 * Get the viminfo file name to use.
2065 * If "file" is given and not empty, use it (has already been expanded by
2066 * cmdline functions).
2067 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2068 * expand environment variables.
2069 * Returns an allocated string. NULL when out of memory.
2070 */
2071 static char_u *
2072viminfo_filename(file)
2073 char_u *file;
2074{
2075 if (file == NULL || *file == NUL)
2076 {
2077 if (use_viminfo != NULL)
2078 file = use_viminfo;
2079 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2080 {
2081#ifdef VIMINFO_FILE2
2082 /* don't use $HOME when not defined (turned into "c:/"!). */
2083# ifdef VMS
2084 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2085# else
2086 if (mch_getenv((char_u *)"HOME") == NULL)
2087# endif
2088 {
2089 /* don't use $VIM when not available. */
2090 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2091 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2092 file = (char_u *)VIMINFO_FILE2;
2093 else
2094 file = (char_u *)VIMINFO_FILE;
2095 }
2096 else
2097#endif
2098 file = (char_u *)VIMINFO_FILE;
2099 }
2100 expand_env(file, NameBuff, MAXPATHL);
2101 file = NameBuff;
2102 }
2103 return vim_strsave(file);
2104}
2105
2106/*
2107 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2108 */
2109 static void
Bram Moolenaard812df62008-11-09 12:46:09 +00002110do_viminfo(fp_in, fp_out, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 FILE *fp_in;
2112 FILE *fp_out;
Bram Moolenaard812df62008-11-09 12:46:09 +00002113 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 int count = 0;
2116 int eof = FALSE;
2117 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002118 int merge = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2121 return;
2122 vir.vir_fd = fp_in;
2123#ifdef FEAT_MBYTE
2124 vir.vir_conv.vc_type = CONV_NONE;
2125#endif
2126
2127 if (fp_in != NULL)
2128 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002129 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002130 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002131 eof = read_viminfo_up_to_marks(&vir,
2132 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002133 merge = TRUE;
2134 }
2135 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 /* Skip info, find start of marks */
2137 while (!(eof = viminfo_readline(&vir))
2138 && vir.vir_line[0] != '>')
2139 ;
2140 }
2141 if (fp_out != NULL)
2142 {
2143 /* Write the info: */
2144 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2145 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002146 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002148 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2150#endif
2151 write_viminfo_search_pattern(fp_out);
2152 write_viminfo_sub_string(fp_out);
2153#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002154 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156 write_viminfo_registers(fp_out);
2157#ifdef FEAT_EVAL
2158 write_viminfo_varlist(fp_out);
2159#endif
2160 write_viminfo_filemarks(fp_out);
2161 write_viminfo_bufferlist(fp_out);
2162 count = write_viminfo_marks(fp_out);
2163 }
Bram Moolenaard812df62008-11-09 12:46:09 +00002164 if (fp_in != NULL
2165 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT)))
2166 copy_viminfo_marks(&vir, fp_out, count, eof, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168 vim_free(vir.vir_line);
2169#ifdef FEAT_MBYTE
2170 if (vir.vir_conv.vc_type != CONV_NONE)
2171 convert_setup(&vir.vir_conv, NULL, NULL);
2172#endif
2173}
2174
2175/*
2176 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2177 * first part of the viminfo file which contains everything but the marks that
2178 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2179 */
2180 static int
2181read_viminfo_up_to_marks(virp, forceit, writing)
2182 vir_T *virp;
2183 int forceit;
2184 int writing;
2185{
2186 int eof;
2187 buf_T *buf;
2188
2189#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002190 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
2192 eof = viminfo_readline(virp);
2193 while (!eof && virp->vir_line[0] != '>')
2194 {
2195 switch (virp->vir_line[0])
2196 {
2197 /* Characters reserved for future expansion, ignored now */
2198 case '+': /* "+40 /path/dir file", for running vim without args */
2199 case '|': /* to be defined */
2200 case '^': /* to be defined */
2201 case '<': /* long line - ignored */
2202 /* A comment or empty line. */
2203 case NUL:
2204 case '\r':
2205 case '\n':
2206 case '#':
2207 eof = viminfo_readline(virp);
2208 break;
2209 case '*': /* "*encoding=value" */
2210 eof = viminfo_encoding(virp);
2211 break;
2212 case '!': /* global variable */
2213#ifdef FEAT_EVAL
2214 eof = read_viminfo_varlist(virp, writing);
2215#else
2216 eof = viminfo_readline(virp);
2217#endif
2218 break;
2219 case '%': /* entry for buffer list */
2220 eof = read_viminfo_bufferlist(virp, writing);
2221 break;
2222 case '"':
2223 eof = read_viminfo_register(virp, forceit);
2224 break;
2225 case '/': /* Search string */
2226 case '&': /* Substitute search string */
2227 case '~': /* Last search string, followed by '/' or '&' */
2228 eof = read_viminfo_search_pattern(virp, forceit);
2229 break;
2230 case '$':
2231 eof = read_viminfo_sub_string(virp, forceit);
2232 break;
2233 case ':':
2234 case '?':
2235 case '=':
2236 case '@':
2237#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002238 eof = read_viminfo_history(virp, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#else
2240 eof = viminfo_readline(virp);
2241#endif
2242 break;
2243 case '-':
2244 case '\'':
2245 eof = read_viminfo_filemark(virp, forceit);
2246 break;
2247 default:
2248 if (viminfo_error("E575: ", _("Illegal starting char"),
2249 virp->vir_line))
2250 eof = TRUE;
2251 else
2252 eof = viminfo_readline(virp);
2253 break;
2254 }
2255 }
2256
2257#ifdef FEAT_CMDHIST
2258 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002259 if (!writing)
2260 finish_viminfo_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#endif
2262
2263 /* Change file names to buffer numbers for fmarks. */
2264 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2265 fmarks_check_names(buf);
2266
2267 return eof;
2268}
2269
2270/*
2271 * Compare the 'encoding' value in the viminfo file with the current value of
2272 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2273 * conversion of text with iconv() in viminfo_readstring().
2274 */
2275 static int
2276viminfo_encoding(virp)
2277 vir_T *virp;
2278{
2279#ifdef FEAT_MBYTE
2280 char_u *p;
2281 int i;
2282
2283 if (get_viminfo_parameter('c') != 0)
2284 {
2285 p = vim_strchr(virp->vir_line, '=');
2286 if (p != NULL)
2287 {
2288 /* remove trailing newline */
2289 ++p;
2290 for (i = 0; vim_isprintc(p[i]); ++i)
2291 ;
2292 p[i] = NUL;
2293
2294 convert_setup(&virp->vir_conv, p, p_enc);
2295 }
2296 }
2297#endif
2298 return viminfo_readline(virp);
2299}
2300
2301/*
2302 * Read a line from the viminfo file.
2303 * Returns TRUE for end-of-file;
2304 */
2305 int
2306viminfo_readline(virp)
2307 vir_T *virp;
2308{
2309 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2310}
2311
2312/*
2313 * check string read from viminfo file
2314 * remove '\n' at the end of the line
2315 * - replace CTRL-V CTRL-V with CTRL-V
2316 * - replace CTRL-V 'n' with '\n'
2317 *
2318 * Check for a long line as written by viminfo_writestring().
2319 *
2320 * Return the string in allocated memory (NULL when out of memory).
2321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 char_u *
2323viminfo_readstring(virp, off, convert)
2324 vir_T *virp;
2325 int off; /* offset for virp->vir_line */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002326 int convert UNUSED; /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 char_u *retval;
2329 char_u *s, *d;
2330 long len;
2331
2332 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2333 {
2334 len = atol((char *)virp->vir_line + off + 1);
2335 retval = lalloc(len, TRUE);
2336 if (retval == NULL)
2337 {
2338 /* Line too long? File messed up? Skip next line. */
2339 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2340 return NULL;
2341 }
2342 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2343 s = retval + 1; /* Skip the leading '<' */
2344 }
2345 else
2346 {
2347 retval = vim_strsave(virp->vir_line + off);
2348 if (retval == NULL)
2349 return NULL;
2350 s = retval;
2351 }
2352
2353 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2354 d = retval;
2355 while (*s != NUL && *s != '\n')
2356 {
2357 if (s[0] == Ctrl_V && s[1] != NUL)
2358 {
2359 if (s[1] == 'n')
2360 *d++ = '\n';
2361 else
2362 *d++ = Ctrl_V;
2363 s += 2;
2364 }
2365 else
2366 *d++ = *s++;
2367 }
2368 *d = NUL;
2369
2370#ifdef FEAT_MBYTE
2371 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2372 {
2373 d = string_convert(&virp->vir_conv, retval, NULL);
2374 if (d != NULL)
2375 {
2376 vim_free(retval);
2377 retval = d;
2378 }
2379 }
2380#endif
2381
2382 return retval;
2383}
2384
2385/*
2386 * write string to viminfo file
2387 * - replace CTRL-V with CTRL-V CTRL-V
2388 * - replace '\n' with CTRL-V 'n'
2389 * - add a '\n' at the end
2390 *
2391 * For a long line:
2392 * - write " CTRL-V <length> \n " in first line
2393 * - write " < <string> \n " in second line
2394 */
2395 void
2396viminfo_writestring(fd, p)
2397 FILE *fd;
2398 char_u *p;
2399{
2400 int c;
2401 char_u *s;
2402 int len = 0;
2403
2404 for (s = p; *s != NUL; ++s)
2405 {
2406 if (*s == Ctrl_V || *s == '\n')
2407 ++len;
2408 ++len;
2409 }
2410
2411 /* If the string will be too long, write its length and put it in the next
2412 * line. Take into account that some room is needed for what comes before
2413 * the string (e.g., variable name). Add something to the length for the
2414 * '<', NL and trailing NUL. */
2415 if (len > LSIZE / 2)
2416 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2417
2418 while ((c = *p++) != NUL)
2419 {
2420 if (c == Ctrl_V || c == '\n')
2421 {
2422 putc(Ctrl_V, fd);
2423 if (c == '\n')
2424 c = 'n';
2425 }
2426 putc(c, fd);
2427 }
2428 putc('\n', fd);
2429}
2430#endif /* FEAT_VIMINFO */
2431
2432/*
2433 * Implementation of ":fixdel", also used by get_stty().
2434 * <BS> resulting <Del>
2435 * ^? ^H
2436 * not ^? ^?
2437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 void
2439do_fixdel(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002440 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 char_u *p;
2443
2444 p = find_termcode((char_u *)"kb");
2445 add_termcode((char_u *)"kD", p != NULL
2446 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2447}
2448
2449 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002450print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 linenr_T lnum;
2452 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002453 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002455 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 if (curwin->w_p_nu || use_number)
2458 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002459 vim_snprintf((char *)numbuf, sizeof(numbuf),
2460 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2462 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002463 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464}
2465
2466/*
2467 * Print a text line. Also in silent mode ("ex -s").
2468 */
2469 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002470print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 linenr_T lnum;
2472 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002473 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 int save_silent = silent_mode;
2476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002478 silent_mode = FALSE;
2479 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2480 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (save_silent)
2482 {
2483 msg_putchar('\n');
2484 cursor_on(); /* msg_start() switches it off */
2485 out_flush();
2486 silent_mode = save_silent;
2487 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00002488 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489}
2490
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002491 int
2492rename_buffer(new_fname)
2493 char_u *new_fname;
2494{
2495 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02002496 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002497
Bram Moolenaar7e283842013-05-30 11:43:15 +02002498#ifdef FEAT_AUTOCMD
2499 buf = curbuf;
2500 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002501 /* buffer changed, don't change name now */
2502 if (buf != curbuf)
2503 return FAIL;
2504# ifdef FEAT_EVAL
2505 if (aborting()) /* autocmds may abort script processing */
2506 return FAIL;
2507# endif
2508#endif
2509 /*
2510 * The name of the current buffer will be changed.
2511 * A new (unlisted) buffer entry needs to be made to hold the old file
2512 * name, which will become the alternate file name.
2513 * But don't set the alternate file name if the buffer didn't have a
2514 * name.
2515 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02002516 fname = curbuf->b_ffname;
2517 sfname = curbuf->b_sfname;
2518 xfname = curbuf->b_fname;
2519 curbuf->b_ffname = NULL;
2520 curbuf->b_sfname = NULL;
2521 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002522 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02002523 curbuf->b_ffname = fname;
2524 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002525 return FAIL;
2526 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02002527 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002528 if (xfname != NULL && *xfname != NUL)
2529 {
2530 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2531 if (buf != NULL && !cmdmod.keepalt)
2532 curwin->w_alt_fnum = buf->b_fnum;
2533 }
2534 vim_free(fname);
2535 vim_free(sfname);
2536#ifdef FEAT_AUTOCMD
Bram Moolenaar7e283842013-05-30 11:43:15 +02002537 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002538#endif
2539 /* Change directories when the 'acd' option is set. */
2540 DO_AUTOCHDIR
2541 return OK;
2542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * ":file[!] [fname]".
2546 */
2547 void
2548ex_file(eap)
2549 exarg_T *eap;
2550{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002551 /* ":0file" removes the file name. Check for illegal uses ":3file",
2552 * "0file name", etc. */
2553 if (eap->addr_count > 0
2554 && (*eap->arg != NUL
2555 || eap->line2 > 0
2556 || eap->addr_count > 1))
2557 {
2558 EMSG(_(e_invarg));
2559 return;
2560 }
2561
2562 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002564 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 /* print full file name if :cd used */
2568 fileinfo(FALSE, FALSE, eap->forceit);
2569}
2570
2571/*
2572 * ":update".
2573 */
2574 void
2575ex_update(eap)
2576 exarg_T *eap;
2577{
2578 if (curbufIsChanged())
2579 (void)do_write(eap);
2580}
2581
2582/*
2583 * ":write" and ":saveas".
2584 */
2585 void
2586ex_write(eap)
2587 exarg_T *eap;
2588{
2589 if (eap->usefilter) /* input lines to shell command */
2590 do_bang(1, eap, FALSE, TRUE, FALSE);
2591 else
2592 (void)do_write(eap);
2593}
2594
2595/*
2596 * write current buffer to file 'eap->arg'
2597 * if 'eap->append' is TRUE, append to the file
2598 *
2599 * if *eap->arg == NUL write to current file
2600 *
2601 * return FAIL for failure, OK otherwise
2602 */
2603 int
2604do_write(eap)
2605 exarg_T *eap;
2606{
2607 int other;
2608 char_u *fname = NULL; /* init to shut up gcc */
2609 char_u *ffname;
2610 int retval = FAIL;
2611 char_u *free_fname = NULL;
2612#ifdef FEAT_BROWSE
2613 char_u *browse_file = NULL;
2614#endif
2615 buf_T *alt_buf = NULL;
2616
2617 if (not_writing()) /* check 'write' option */
2618 return FAIL;
2619
2620 ffname = eap->arg;
2621#ifdef FEAT_BROWSE
2622 if (cmdmod.browse)
2623 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002624 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 NULL, NULL, NULL, curbuf);
2626 if (browse_file == NULL)
2627 goto theend;
2628 ffname = browse_file;
2629 }
2630#endif
2631 if (*ffname == NUL)
2632 {
2633 if (eap->cmdidx == CMD_saveas)
2634 {
2635 EMSG(_(e_argreq));
2636 goto theend;
2637 }
2638 other = FALSE;
2639 }
2640 else
2641 {
2642 fname = ffname;
2643 free_fname = fix_fname(ffname);
2644 /*
2645 * When out-of-memory, keep unexpanded file name, because we MUST be
2646 * able to write the file in this situation.
2647 */
2648 if (free_fname != NULL)
2649 ffname = free_fname;
2650 other = otherfile(ffname);
2651 }
2652
2653 /*
2654 * If we have a new file, put its name in the list of alternate file names.
2655 */
2656 if (other)
2657 {
2658 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2659 || eap->cmdidx == CMD_saveas)
2660 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2661 else
2662 alt_buf = buflist_findname(ffname);
2663 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2664 {
2665 /* Overwriting a file that is loaded in another buffer is not a
2666 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002667 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 goto theend;
2669 }
2670 }
2671
2672 /*
2673 * Writing to the current file is not allowed in readonly mode
2674 * and a file name is required.
2675 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2676 */
2677 if (!other && (
2678#ifdef FEAT_QUICKFIX
2679 bt_dontwrite_msg(curbuf) ||
2680#endif
2681 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2682 goto theend;
2683
2684 if (!other)
2685 {
2686 ffname = curbuf->b_ffname;
2687 fname = curbuf->b_fname;
2688 /*
2689 * Not writing the whole file is only allowed with '!'.
2690 */
2691 if ( (eap->line1 != 1
2692 || eap->line2 != curbuf->b_ml.ml_line_count)
2693 && !eap->forceit
2694 && !eap->append
2695 && !p_wa)
2696 {
2697#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2698 if (p_confirm || cmdmod.confirm)
2699 {
2700 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2701 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2702 goto theend;
2703 eap->forceit = TRUE;
2704 }
2705 else
2706#endif
2707 {
2708 EMSG(_("E140: Use ! to write partial buffer"));
2709 goto theend;
2710 }
2711 }
2712 }
2713
2714 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2715 {
2716 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2717 {
2718#ifdef FEAT_AUTOCMD
2719 buf_T *was_curbuf = curbuf;
2720
2721 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002722 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723# ifdef FEAT_EVAL
2724 if (curbuf != was_curbuf || aborting())
2725# else
2726 if (curbuf != was_curbuf)
2727# endif
2728 {
2729 /* buffer changed, don't change name now */
2730 retval = FAIL;
2731 goto theend;
2732 }
2733#endif
2734 /* Exchange the file names for the current and the alternate
2735 * buffer. This makes it look like we are now editing the buffer
2736 * under the new name. Must be done before buf_write(), because
2737 * if there is no file name and 'cpo' contains 'F', it will set
2738 * the file name. */
2739 fname = alt_buf->b_fname;
2740 alt_buf->b_fname = curbuf->b_fname;
2741 curbuf->b_fname = fname;
2742 fname = alt_buf->b_ffname;
2743 alt_buf->b_ffname = curbuf->b_ffname;
2744 curbuf->b_ffname = fname;
2745 fname = alt_buf->b_sfname;
2746 alt_buf->b_sfname = curbuf->b_sfname;
2747 curbuf->b_sfname = fname;
2748 buf_name_changed(curbuf);
2749#ifdef FEAT_AUTOCMD
2750 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002751 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (!alt_buf->b_p_bl)
2753 {
2754 alt_buf->b_p_bl = TRUE;
2755 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2756 }
2757# ifdef FEAT_EVAL
2758 if (curbuf != was_curbuf || aborting())
2759# else
2760 if (curbuf != was_curbuf)
2761# endif
2762 {
2763 /* buffer changed, don't write the file */
2764 retval = FAIL;
2765 goto theend;
2766 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002767
2768 /* If 'filetype' was empty try detecting it now. */
2769 if (*curbuf->b_p_ft == NUL)
2770 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002771 if (au_has_group((char_u *)"filetypedetect"))
2772 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
2773 TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002774 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002775 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01002776
2777 /* Autocommands may have changed buffer names, esp. when
2778 * 'autochdir' is set. */
2779 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#endif
2781 }
2782
2783 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2784 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002785
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002786 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002787 if (eap->cmdidx == CMD_saveas)
2788 {
2789 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00002790 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002791 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00002792#ifdef FEAT_WINDOWS
2793 redraw_tabline = TRUE;
2794#endif
2795 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002796 /* Change directories when the 'acd' option is set. */
2797 DO_AUTOCHDIR
2798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800
2801theend:
2802#ifdef FEAT_BROWSE
2803 vim_free(browse_file);
2804#endif
2805 vim_free(free_fname);
2806 return retval;
2807}
2808
2809/*
2810 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2811 * BF_NEW or BF_READERR, check for overwriting current file.
2812 * May set eap->forceit if a dialog says it's OK to overwrite.
2813 * Return OK if it's OK, FAIL if it is not.
2814 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02002815 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816check_overwrite(eap, buf, fname, ffname, other)
2817 exarg_T *eap;
2818 buf_T *buf;
2819 char_u *fname; /* file name to be used (can differ from
2820 buf->ffname) */
2821 char_u *ffname; /* full path version of fname */
2822 int other; /* writing under other name */
2823{
2824 /*
2825 * write to other file or b_flags set or not writing the whole file:
2826 * overwriting only allowed with '!'
2827 */
2828 if ( (other
2829 || (buf->b_flags & BF_NOTEDITED)
2830 || ((buf->b_flags & BF_NEW)
2831 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2832 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00002834#ifdef FEAT_QUICKFIX
2835 && !bt_nofile(buf)
2836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 && vim_fexists(ffname))
2838 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002839 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002841#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002842 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002843 if (mch_isdir(ffname))
2844 {
2845 EMSG2(_(e_isadir2), ffname);
2846 return FAIL;
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#endif
2849#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002850 if (p_confirm || cmdmod.confirm)
2851 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002852 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002854 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2855 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2856 return FAIL;
2857 eap->forceit = TRUE;
2858 }
2859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002861 {
2862 EMSG(_(e_exists));
2863 return FAIL;
2864 }
2865 }
2866
2867 /* For ":w! filename" check that no swap file exists for "filename". */
2868 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002870 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002871 char_u *p;
2872 int r;
2873 char_u *swapname;
2874
2875 /* We only try the first entry in 'directory', without checking if
2876 * it's writable. If the "." directory is not writable the write
2877 * will probably fail anyway.
2878 * Use 'shortname' of the current buffer, since there is no buffer
2879 * for the written file. */
2880 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002881 {
2882 dir = alloc(5);
2883 if (dir == NULL)
2884 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002885 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002886 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002887 else
2888 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002889 dir = alloc(MAXPATHL);
2890 if (dir == NULL)
2891 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002892 p = p_dir;
2893 copy_option_part(&p, dir, MAXPATHL, ",");
2894 }
2895 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02002896 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002897 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002898 if (r)
2899 {
2900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2901 if (p_confirm || cmdmod.confirm)
2902 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002903 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002904
2905 dialog_msg(buff,
2906 _("Swap file \"%s\" exists, overwrite anyway?"),
2907 swapname);
2908 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2909 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002910 {
2911 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002912 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002913 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002914 eap->forceit = TRUE;
2915 }
2916 else
2917#endif
2918 {
2919 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2920 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002921 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002922 return FAIL;
2923 }
2924 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002925 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
2928 return OK;
2929}
2930
2931/*
2932 * Handle ":wnext", ":wNext" and ":wprevious" commands.
2933 */
2934 void
2935ex_wnext(eap)
2936 exarg_T *eap;
2937{
2938 int i;
2939
2940 if (eap->cmd[1] == 'n')
2941 i = curwin->w_arg_idx + (int)eap->line2;
2942 else
2943 i = curwin->w_arg_idx - (int)eap->line2;
2944 eap->line1 = 1;
2945 eap->line2 = curbuf->b_ml.ml_line_count;
2946 if (do_write(eap) != FAIL)
2947 do_argfile(eap, i);
2948}
2949
2950/*
2951 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
2952 */
2953 void
2954do_wqall(eap)
2955 exarg_T *eap;
2956{
2957 buf_T *buf;
2958 int error = 0;
2959 int save_forceit = eap->forceit;
2960
2961 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
2962 exiting = TRUE;
2963
2964 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2965 {
2966 if (bufIsChanged(buf))
2967 {
2968 /*
2969 * Check if there is a reason the buffer cannot be written:
2970 * 1. if the 'write' option is set
2971 * 2. if there is no file name (even after browsing)
2972 * 3. if the 'readonly' is set (even after a dialog)
2973 * 4. if overwriting is allowed (even after a dialog)
2974 */
2975 if (not_writing())
2976 {
2977 ++error;
2978 break;
2979 }
2980#ifdef FEAT_BROWSE
2981 /* ":browse wall": ask for file name if there isn't one */
2982 if (buf->b_ffname == NULL && cmdmod.browse)
2983 browse_save_fname(buf);
2984#endif
2985 if (buf->b_ffname == NULL)
2986 {
2987 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
2988 ++error;
2989 }
2990 else if (check_readonly(&eap->forceit, buf)
2991 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
2992 FALSE) == FAIL)
2993 {
2994 ++error;
2995 }
2996 else
2997 {
2998 if (buf_write_all(buf, eap->forceit) == FAIL)
2999 ++error;
3000#ifdef FEAT_AUTOCMD
3001 /* an autocommand may have deleted the buffer */
3002 if (!buf_valid(buf))
3003 buf = firstbuf;
3004#endif
3005 }
3006 eap->forceit = save_forceit; /* check_overwrite() may set it */
3007 }
3008 }
3009 if (exiting)
3010 {
3011 if (!error)
3012 getout(0); /* exit Vim */
3013 not_exiting();
3014 }
3015}
3016
3017/*
3018 * Check the 'write' option.
3019 * Return TRUE and give a message when it's not st.
3020 */
3021 int
3022not_writing()
3023{
3024 if (p_write)
3025 return FALSE;
3026 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3027 return TRUE;
3028}
3029
3030/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003031 * Check if a buffer is read-only (either 'readonly' option is set or file is
3032 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3033 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 */
3035 static int
3036check_readonly(forceit, buf)
3037 int *forceit;
3038 buf_T *buf;
3039{
Bram Moolenaar5386a122007-06-28 20:02:32 +00003040 struct stat st;
3041
3042 /* Handle a file being readonly when the 'readonly' option is set or when
3043 * the file exists and permissions are read-only.
3044 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3045 * important for device checks but not here. */
3046 if (!*forceit && (buf->b_p_ro
3047 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3048 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
3050#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3051 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3052 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003053 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaar5386a122007-06-28 20:02:32 +00003055 if (buf->b_p_ro)
3056 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3057 buf->b_fname);
3058 else
3059 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 +00003060 buf->b_fname);
3061
3062 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3063 {
3064 /* Set forceit, to force the writing of a readonly file */
3065 *forceit = TRUE;
3066 return FALSE;
3067 }
3068 else
3069 return TRUE;
3070 }
3071 else
3072#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003073 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003075 else
3076 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3077 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 return TRUE;
3079 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return FALSE;
3082}
3083
3084/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003085 * Try to abandon current file and edit a new or existing file.
3086 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003088 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003089 * -1 for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 * 'lnum' is the line number for the cursor in the new file (if non-zero).
3091 */
3092 int
3093getfile(fnum, ffname, sfname, setpm, lnum, forceit)
3094 int fnum;
3095 char_u *ffname;
3096 char_u *sfname;
3097 int setpm;
3098 linenr_T lnum;
3099 int forceit;
3100{
3101 int other;
3102 int retval;
3103 char_u *free_me = NULL;
3104
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003105 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003107#ifdef FEAT_AUTOCMD
3108 if (curbuf_locked())
3109 return 1;
3110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
3112 if (fnum == 0)
3113 {
3114 /* make ffname full path, set sfname */
3115 fname_expand(curbuf, &ffname, &sfname);
3116 other = otherfile(ffname);
3117 free_me = ffname; /* has been allocated, free() later */
3118 }
3119 else
3120 other = (fnum != curbuf->b_fnum);
3121
3122 if (other)
3123 ++no_wait_return; /* don't wait for autowrite message */
3124 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
3125 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3126 {
3127#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3128 if (p_confirm && p_write)
3129 dialog_changed(curbuf, FALSE);
3130 if (curbufIsChanged())
3131#endif
3132 {
3133 if (other)
3134 --no_wait_return;
3135 EMSG(_(e_nowrtmsg));
3136 retval = 2; /* file has been changed */
3137 goto theend;
3138 }
3139 }
3140 if (other)
3141 --no_wait_return;
3142 if (setpm)
3143 setpcmark();
3144 if (!other)
3145 {
3146 if (lnum != 0)
3147 curwin->w_cursor.lnum = lnum;
3148 check_cursor_lnum();
3149 beginline(BL_SOL | BL_FIX);
3150 retval = 0; /* it's in the same file */
3151 }
3152 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003153 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
3154 curwin) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 retval = -1; /* opened another file */
3156 else
3157 retval = 1; /* error encountered */
3158
3159theend:
3160 vim_free(free_me);
3161 return retval;
3162}
3163
3164/*
3165 * start editing a new file
3166 *
3167 * fnum: file number; if zero use ffname/sfname
3168 * ffname: the file name
3169 * - full path if sfname used,
3170 * - any file name if sfname is NULL
3171 * - empty string to re-edit with the same file name (but may be
3172 * in a different directory)
3173 * - NULL to start an empty buffer
3174 * sfname: the short file name (or NULL)
3175 * eap: contains the command to be executed after loading the file and
3176 * forced 'ff' and 'fenc'
3177 * newlnum: if > 0: put cursor on this line number (if possible)
3178 * if ECMD_LASTL: use last position in loaded file
3179 * if ECMD_LAST: use last position in all files
3180 * if ECMD_ONE: use first line
3181 * flags:
3182 * ECMD_HIDE: if TRUE don't free the current buffer
3183 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3184 * ECMD_OLDBUF: use existing buffer if it exists
3185 * ECMD_FORCEIT: ! used for Ex command
3186 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003187 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003188 * window, NULL when splitting the window first. When not NULL info
3189 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 *
3191 * return FAIL for failure, OK otherwise
3192 */
3193 int
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003194do_ecmd(fnum, ffname, sfname, eap, newlnum, flags, oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 int fnum;
3196 char_u *ffname;
3197 char_u *sfname;
3198 exarg_T *eap; /* can be NULL! */
3199 linenr_T newlnum;
3200 int flags;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003201 win_T *oldwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
3203 int other_file; /* TRUE if editing another file */
3204 int oldbuf; /* TRUE if using existing buffer */
3205#ifdef FEAT_AUTOCMD
3206 int auto_buf = FALSE; /* TRUE if autocommands brought us
3207 into the buffer unexpectedly */
3208 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003209 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#endif
3211 buf_T *buf;
3212#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3213 buf_T *old_curbuf = curbuf;
3214#endif
3215 char_u *free_fname = NULL;
3216#ifdef FEAT_BROWSE
3217 char_u *browse_file = NULL;
3218#endif
3219 int retval = FAIL;
3220 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003221 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 linenr_T topline = 0;
3223 int newcol = -1;
3224 int solcol = -1;
3225 pos_T *pos;
3226#ifdef FEAT_SUN_WORKSHOP
3227 char_u *cp;
3228#endif
3229 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003230#ifdef FEAT_SPELL
3231 int did_get_winopts = FALSE;
3232#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003233 int readfile_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
3235 if (eap != NULL)
3236 command = eap->do_ecmd_cmd;
3237
3238 if (fnum != 0)
3239 {
3240 if (fnum == curbuf->b_fnum) /* file is already being edited */
3241 return OK; /* nothing to do */
3242 other_file = TRUE;
3243 }
3244 else
3245 {
3246#ifdef FEAT_BROWSE
3247 if (cmdmod.browse)
3248 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003249# ifdef FEAT_AUTOCMD
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003250 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003251# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003252 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003253# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003254 au_has_group((char_u *)"FileExplorer"))
3255 {
3256 /* No browsing supported but we do have the file explorer:
3257 * Edit the directory. */
3258 if (ffname == NULL || !mch_isdir(ffname))
3259 ffname = (char_u *)".";
3260 }
3261 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003262# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003263 {
3264 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003266 if (browse_file == NULL)
3267 goto theend;
3268 ffname = browse_file;
3269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271#endif
3272 /* if no short name given, use ffname for short name */
3273 if (sfname == NULL)
3274 sfname = ffname;
3275#ifdef USE_FNAME_CASE
3276# ifdef USE_LONG_FNAME
3277 if (USE_LONG_FNAME)
3278# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003279 if (sfname != NULL)
3280 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#endif
3282
3283#ifdef FEAT_LISTCMDS
3284 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3285 goto theend;
3286#endif
3287
3288 if (ffname == NULL)
3289 other_file = TRUE;
3290 /* there is no file name */
3291 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3292 other_file = FALSE;
3293 else
3294 {
3295 if (*ffname == NUL) /* re-edit with same file name */
3296 {
3297 ffname = curbuf->b_ffname;
3298 sfname = curbuf->b_fname;
3299 }
3300 free_fname = fix_fname(ffname); /* may expand to full path name */
3301 if (free_fname != NULL)
3302 ffname = free_fname;
3303 other_file = otherfile(ffname);
3304#ifdef FEAT_SUN_WORKSHOP
3305 if (usingSunWorkShop && p_acd
3306 && (cp = vim_strrchr(sfname, '/')) != NULL)
3307 sfname = ++cp;
3308#endif
3309 }
3310 }
3311
3312 /*
3313 * if the file was changed we may not be allowed to abandon it
3314 * - if we are going to re-edit the same file
3315 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3316 */
3317 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3318 || (curbuf->b_nwindows == 1
3319 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003320 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3321 | (other_file ? 0 : CCGD_MULTWIN)
3322 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3323 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 if (fnum == 0 && other_file && ffname != NULL)
3326 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3327 goto theend;
3328 }
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 /*
3331 * End Visual mode before switching to another buffer, so the text can be
3332 * copied into the GUI selection buffer.
3333 */
3334 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003336#ifdef FEAT_AUTOCMD
3337 if ((command != NULL || newlnum > (linenr_T)0)
3338 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3339 {
3340 int len;
3341 char_u *p;
3342
3343 /* Set v:swapcommand for the SwapExists autocommands. */
3344 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003345 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003346 else
3347 len = 30;
3348 p = alloc((unsigned)len);
3349 if (p != NULL)
3350 {
3351 if (command != NULL)
3352 vim_snprintf((char *)p, len, ":%s\r", command);
3353 else
3354 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3355 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3356 did_set_swapcommand = TRUE;
3357 vim_free(p);
3358 }
3359 }
3360#endif
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 /*
3363 * If we are starting to edit another file, open a (new) buffer.
3364 * Otherwise we re-use the current buffer.
3365 */
3366 if (other_file)
3367 {
3368#ifdef FEAT_LISTCMDS
3369 if (!(flags & ECMD_ADDBUF))
3370#endif
3371 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003372 if (!cmdmod.keepalt)
3373 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003374 if (oldwin != NULL)
3375 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377
3378 if (fnum)
3379 buf = buflist_findnr(fnum);
3380 else
3381 {
3382#ifdef FEAT_LISTCMDS
3383 if (flags & ECMD_ADDBUF)
3384 {
3385 linenr_T tlnum = 1L;
3386
3387 if (command != NULL)
3388 {
3389 tlnum = atol((char *)command);
3390 if (tlnum <= 0)
3391 tlnum = 1L;
3392 }
3393 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3394 goto theend;
3395 }
3396#endif
3397 buf = buflist_new(ffname, sfname, 0L,
3398 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003399#ifdef FEAT_AUTOCMD
3400 /* autocommands may change curwin and curbuf */
3401 if (oldwin != NULL)
3402 oldwin = curwin;
3403 old_curbuf = curbuf;
3404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 if (buf == NULL)
3407 goto theend;
3408 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3409 {
3410 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 else /* existing memfile */
3413 {
3414 oldbuf = TRUE;
3415 (void)buf_check_timestamp(buf, FALSE);
3416 /* Check if autocommands made buffer invalid or changed the current
3417 * buffer. */
3418 if (!buf_valid(buf)
3419#ifdef FEAT_AUTOCMD
3420 || curbuf != old_curbuf
3421#endif
3422 )
3423 goto theend;
3424#ifdef FEAT_EVAL
3425 if (aborting()) /* autocmds may abort script processing */
3426 goto theend;
3427#endif
3428 }
3429
3430 /* May jump to last used line number for a loaded buffer or when asked
3431 * for explicitly */
3432 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3433 {
3434 pos = buflist_findfpos(buf);
3435 newlnum = pos->lnum;
3436 solcol = pos->col;
3437 }
3438
3439 /*
3440 * Make the (new) buffer the one used by the current window.
3441 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3442 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003443 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 */
3445 if (buf != curbuf)
3446 {
3447#ifdef FEAT_AUTOCMD
3448 /*
3449 * Be careful: The autocommands may delete any buffer and change
3450 * the current buffer.
3451 * - If the buffer we are going to edit is deleted, give up.
3452 * - If the current buffer is deleted, prefer to load the new
3453 * buffer when loading a buffer is required. This avoids
3454 * loading another buffer which then must be closed again.
3455 * - If we ended up in the new buffer already, need to skip a few
3456 * things, set auto_buf.
3457 */
3458 if (buf->b_fname != NULL)
3459 new_name = vim_strsave(buf->b_fname);
3460 au_new_curbuf = buf;
3461 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3462 if (!buf_valid(buf)) /* new buffer has been deleted */
3463 {
3464 delbuf_msg(new_name); /* frees new_name */
3465 goto theend;
3466 }
3467# ifdef FEAT_EVAL
3468 if (aborting()) /* autocmds may abort script processing */
3469 {
3470 vim_free(new_name);
3471 goto theend;
3472 }
3473# endif
3474 if (buf == curbuf) /* already in new buffer */
3475 auto_buf = TRUE;
3476 else
3477 {
3478 if (curbuf == old_curbuf)
3479#endif
3480 buf_copy_options(buf, BCO_ENTER);
3481
3482 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003483 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003484 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003485 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
3487#ifdef FEAT_AUTOCMD
Bram Moolenaarfc573802011-12-30 15:01:59 +01003488 /* Autocommands may open a new window and leave oldwin open
3489 * which leads to crashes since the above call sets
3490 * oldwin->w_buffer to NULL. */
3491 if (curwin != oldwin && oldwin != aucmd_win
3492 && win_valid(oldwin) && oldwin->w_buffer == NULL)
3493 win_close(oldwin, FALSE);
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495# ifdef FEAT_EVAL
3496 if (aborting()) /* autocmds may abort script processing */
3497 {
3498 vim_free(new_name);
3499 goto theend;
3500 }
3501# endif
3502 /* Be careful again, like above. */
3503 if (!buf_valid(buf)) /* new buffer has been deleted */
3504 {
3505 delbuf_msg(new_name); /* frees new_name */
3506 goto theend;
3507 }
3508 if (buf == curbuf) /* already in new buffer */
3509 auto_buf = TRUE;
3510 else
3511#endif
3512 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003513#ifdef FEAT_SYN_HL
3514 /*
3515 * <VN> We could instead free the synblock
3516 * and re-attach to buffer, perhaps.
3517 */
3518 if (curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02003519 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 curwin->w_buffer = buf;
3522 curbuf = buf;
3523 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003524
3525 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
3526 if (!oldbuf && eap != NULL)
3527 {
3528 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003529#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003530 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003531#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534
3535 /* May get the window options from the last time this buffer
3536 * was in this window (or another window). If not used
3537 * before, reset the local window options to the global
3538 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00003539 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003540#ifdef FEAT_SPELL
3541 did_get_winopts = TRUE;
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544#ifdef FEAT_AUTOCMD
3545 }
3546 vim_free(new_name);
3547 au_new_curbuf = NULL;
3548#endif
3549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 curwin->w_pcmark.lnum = 1;
3552 curwin->w_pcmark.col = 0;
3553 }
3554 else /* !other_file */
3555 {
3556 if (
3557#ifdef FEAT_LISTCMDS
3558 (flags & ECMD_ADDBUF) ||
3559#endif
3560 check_fname() == FAIL)
3561 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01003562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 oldbuf = (flags & ECMD_OLDBUF);
3564 }
3565
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003566#ifdef FEAT_AUTOCMD
3567 buf = curbuf;
3568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3570 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003571 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573 else
3574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 /* Don't make a buffer listed if it's a help buffer. Useful when
3576 * using CTRL-O to go back to a help file. */
3577 if (!curbuf->b_help)
3578 set_buflisted(TRUE);
3579 }
3580
3581#ifdef FEAT_AUTOCMD
3582 /* If autocommands change buffers under our fingers, forget about
3583 * editing the file. */
3584 if (buf != curbuf)
3585 goto theend;
3586# ifdef FEAT_EVAL
3587 if (aborting()) /* autocmds may abort script processing */
3588 goto theend;
3589# endif
3590
3591 /* Since we are starting to edit a file, consider the filetype to be
3592 * unset. Helps for when an autocommand changes files and expects syntax
3593 * highlighting to work in the other file. */
3594 did_filetype = FALSE;
3595#endif
3596
3597/*
3598 * other_file oldbuf
3599 * FALSE FALSE re-edit same file, buffer is re-used
3600 * FALSE TRUE re-edit same file, nothing changes
3601 * TRUE FALSE start editing new file, new buffer
3602 * TRUE TRUE start editing in existing buffer (nothing to do)
3603 */
3604 if (!other_file && !oldbuf) /* re-use the buffer */
3605 {
3606 set_last_cursor(curwin); /* may set b_last_cursor */
3607 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3608 {
3609 newlnum = curwin->w_cursor.lnum;
3610 solcol = curwin->w_cursor.col;
3611 }
3612#ifdef FEAT_AUTOCMD
3613 buf = curbuf;
3614 if (buf->b_fname != NULL)
3615 new_name = vim_strsave(buf->b_fname);
3616 else
3617 new_name = NULL;
3618#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003619 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
3620 {
3621 /* Save all the text, so that the reload can be undone.
3622 * Sync first so that this is a separate undo-able action. */
3623 u_sync(FALSE);
3624 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
3625 == FAIL)
3626 goto theend;
3627 u_unchanged(curbuf);
3628 buf_freeall(curbuf, BFA_KEEP_UNDO);
3629
3630 /* tell readfile() not to clear or reload undo info */
3631 readfile_flags = READ_KEEP_UNDO;
3632 }
3633 else
3634 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#ifdef FEAT_AUTOCMD
3636 /* If autocommands deleted the buffer we were going to re-edit, give
3637 * up and jump to the end. */
3638 if (!buf_valid(buf))
3639 {
3640 delbuf_msg(new_name); /* frees new_name */
3641 goto theend;
3642 }
3643 vim_free(new_name);
3644
3645 /* If autocommands change buffers under our fingers, forget about
3646 * re-editing the file. Should do the buf_clear_file(), but perhaps
3647 * the autocommands changed the buffer... */
3648 if (buf != curbuf)
3649 goto theend;
3650# ifdef FEAT_EVAL
3651 if (aborting()) /* autocmds may abort script processing */
3652 goto theend;
3653# endif
3654#endif
3655 buf_clear_file(curbuf);
3656 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3657 curbuf->b_op_end.lnum = 0;
3658 }
3659
3660/*
3661 * If we get here we are sure to start editing
3662 */
3663 /* don't redraw until the cursor is in the right line */
3664 ++RedrawingDisabled;
3665
3666 /* Assume success now */
3667 retval = OK;
3668
3669 /*
3670 * Reset cursor position, could be used by autocommands.
3671 */
3672 check_cursor();
3673
3674 /*
3675 * Check if we are editing the w_arg_idx file in the argument list.
3676 */
3677 check_arg_idx(curwin);
3678
3679#ifdef FEAT_AUTOCMD
3680 if (!auto_buf)
3681#endif
3682 {
3683 /*
3684 * Set cursor and init window before reading the file and executing
3685 * autocommands. This allows for the autocommands to position the
3686 * cursor.
3687 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003688 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003691 /* It's possible that all lines in the buffer changed. Need to update
3692 * automatic folding for all windows where it's used. */
3693# ifdef FEAT_WINDOWS
3694 {
3695 win_T *win;
3696 tabpage_T *tp;
3697
3698 FOR_ALL_TAB_WINDOWS(tp, win)
3699 if (win->w_buffer == curbuf)
3700 foldUpdateAll(win);
3701 }
3702# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 foldUpdateAll(curwin);
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
3706
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003707 /* Change directories when the 'acd' option is set. */
3708 DO_AUTOCHDIR
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 /*
3711 * Careful: open_buffer() and apply_autocmds() may change the current
3712 * buffer and window.
3713 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01003714 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 topline = curwin->w_topline;
3716 if (!oldbuf) /* need to read the file */
3717 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003718#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 swap_exists_action = SEA_DIALOG;
3720#endif
3721 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3722
3723 /*
3724 * Open the buffer and read the file.
3725 */
3726#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003727 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 retval = FAIL;
3729#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003730 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731#endif
3732
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003733#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (swap_exists_action == SEA_QUIT)
3735 retval = FAIL;
3736 handle_swap_exists(old_curbuf);
3737#endif
3738 }
3739#ifdef FEAT_AUTOCMD
3740 else
3741 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003742 /* Read the modelines, but only to set window-local options. Any
3743 * buffer-local options have already been set and may have been
3744 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00003745 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3748 &retval);
3749 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3750 &retval);
3751 }
3752 check_arg_idx(curwin);
3753#endif
3754
Bram Moolenaareab316b2015-03-24 11:46:30 +01003755 /* If autocommands change the cursor position or topline, we should
3756 * keep it. Also when it moves within a line. */
3757 if (!equalpos(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
3759 newlnum = curwin->w_cursor.lnum;
3760 newcol = curwin->w_cursor.col;
3761 }
3762 if (curwin->w_topline == topline)
3763 topline = 0;
3764
3765 /* Even when cursor didn't move we need to recompute topline. */
3766 changed_line_abv_curs();
3767
3768#ifdef FEAT_TITLE
3769 maketitle();
3770#endif
3771 }
3772
3773#ifdef FEAT_DIFF
3774 /* Tell the diff stuff that this buffer is new and/or needs updating.
3775 * Also needed when re-editing the same buffer, because unloading will
3776 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003777 if (curwin->w_p_diff)
3778 {
3779 diff_buf_add(curbuf);
3780 diff_invalidate(curbuf);
3781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
3783
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003784#ifdef FEAT_SPELL
3785 /* If the window options were changed may need to set the spell language.
3786 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003787 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
3788 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003789#endif
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (command == NULL)
3792 {
3793 if (newcol >= 0) /* position set by autocommands */
3794 {
3795 curwin->w_cursor.lnum = newlnum;
3796 curwin->w_cursor.col = newcol;
3797 check_cursor();
3798 }
3799 else if (newlnum > 0) /* line number from caller or old position */
3800 {
3801 curwin->w_cursor.lnum = newlnum;
3802 check_cursor_lnum();
3803 if (solcol >= 0 && !p_sol)
3804 {
3805 /* 'sol' is off: Use last known column. */
3806 curwin->w_cursor.col = solcol;
3807 check_cursor_col();
3808#ifdef FEAT_VIRTUALEDIT
3809 curwin->w_cursor.coladd = 0;
3810#endif
3811 curwin->w_set_curswant = TRUE;
3812 }
3813 else
3814 beginline(BL_SOL | BL_FIX);
3815 }
3816 else /* no line number, go to last line in Ex mode */
3817 {
3818 if (exmode_active)
3819 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3820 beginline(BL_WHITE | BL_FIX);
3821 }
3822 }
3823
3824#ifdef FEAT_WINDOWS
3825 /* Check if cursors in other windows on the same buffer are still valid */
3826 check_lnums(FALSE);
3827#endif
3828
3829 /*
3830 * Did not read the file, need to show some info about the file.
3831 * Do this after setting the cursor.
3832 */
3833 if (oldbuf
3834#ifdef FEAT_AUTOCMD
3835 && !auto_buf
3836#endif
3837 )
3838 {
3839 int msg_scroll_save = msg_scroll;
3840
3841 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3842 * message. */
3843 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3844 msg_scroll = FALSE;
3845 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3846 check_for_delay(FALSE);
3847 msg_start();
3848 msg_scroll = msg_scroll_save;
3849 msg_scrolled_ign = TRUE;
3850
3851 fileinfo(FALSE, TRUE, FALSE);
3852
3853 msg_scrolled_ign = FALSE;
3854 }
3855
3856 if (command != NULL)
3857 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3858
3859#ifdef FEAT_KEYMAP
3860 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003861 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#endif
3863
3864 --RedrawingDisabled;
3865 if (!skip_redraw)
3866 {
3867 n = p_so;
3868 if (topline == 0 && command == NULL)
3869 p_so = 999; /* force cursor halfway the window */
3870 update_topline();
3871#ifdef FEAT_SCROLLBIND
3872 curwin->w_scbind_pos = curwin->w_topline;
3873#endif
3874 p_so = n;
3875 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3876 }
3877
3878 if (p_im)
3879 need_start_insertmode = TRUE;
3880
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003881 /* Change directories when the 'acd' option is set. */
3882 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003884#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003885 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02003888 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3890# endif
3891# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003892 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003893 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894# endif
3895 }
3896#endif
3897
3898theend:
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003899#ifdef FEAT_AUTOCMD
3900 if (did_set_swapcommand)
3901 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
3902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#ifdef FEAT_BROWSE
3904 vim_free(browse_file);
3905#endif
3906 vim_free(free_fname);
3907 return retval;
3908}
3909
3910#ifdef FEAT_AUTOCMD
3911 static void
3912delbuf_msg(name)
3913 char_u *name;
3914{
3915 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3916 name == NULL ? (char_u *)"" : name);
3917 vim_free(name);
3918 au_new_curbuf = NULL;
3919}
3920#endif
3921
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003922static int append_indent = 0; /* autoindent for first line */
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924/*
3925 * ":insert" and ":append", also used by ":change"
3926 */
3927 void
3928ex_append(eap)
3929 exarg_T *eap;
3930{
3931 char_u *theline;
3932 int did_undo = FALSE;
3933 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003934 int indent = 0;
3935 char_u *p;
3936 int vcol;
3937 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003939 /* the ! flag toggles autoindent */
3940 if (eap->forceit)
3941 curbuf->b_p_ai = !curbuf->b_p_ai;
3942
3943 /* First autoindent comes from the line we start on */
3944 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
3945 append_indent = get_indent_lnum(lnum);
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (eap->cmdidx != CMD_append)
3948 --lnum;
3949
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003950 /* when the buffer is empty append to line 0 and delete the dummy line */
3951 if (empty && lnum == 1)
3952 lnum = 0;
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 State = INSERT; /* behave like in Insert mode */
3955 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3956 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003957
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003958 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 msg_scroll = TRUE;
3961 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003962 if (curbuf->b_p_ai)
3963 {
3964 if (append_indent >= 0)
3965 {
3966 indent = append_indent;
3967 append_indent = -1;
3968 }
3969 else if (lnum > 0)
3970 indent = get_indent_lnum(lnum);
3971 }
3972 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003974 {
3975 /* No getline() function, use the lines that follow. This ends
3976 * when there is no more. */
3977 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
3978 break;
3979 p = vim_strchr(eap->nextcmd, NL);
3980 if (p == NULL)
3981 p = eap->nextcmd + STRLEN(eap->nextcmd);
3982 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
3983 if (*p != NUL)
3984 ++p;
3985 eap->nextcmd = p;
3986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02003988 {
3989 int save_State = State;
3990
3991 /* Set State to avoid the cursor shape to be set to INSERT mode
3992 * when getline() returns. */
3993 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 theline = eap->getline(
3995#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003996 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003998 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02003999 State = save_State;
4000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004002 if (theline == NULL)
4003 break;
4004
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004005 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4006 if (ex_keep_indent)
4007 append_indent = indent;
4008
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004009 /* Look for the "." after automatic indent. */
4010 vcol = 0;
4011 for (p = theline; indent > vcol; ++p)
4012 {
4013 if (*p == ' ')
4014 ++vcol;
4015 else if (*p == TAB)
4016 vcol += 8 - vcol % 8;
4017 else
4018 break;
4019 }
4020 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004021 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4022 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
4024 vim_free(theline);
4025 break;
4026 }
4027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004028 /* don't use autoindent if nothing was typed. */
4029 if (p[0] == NUL)
4030 theline[0] = NUL;
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 did_undo = TRUE;
4033 ml_append(lnum, theline, (colnr_T)0, FALSE);
4034 appended_lines_mark(lnum, 1L);
4035
4036 vim_free(theline);
4037 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004038
4039 if (empty)
4040 {
4041 ml_delete(2L, FALSE);
4042 empty = FALSE;
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045 State = NORMAL;
4046
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004047 if (eap->forceit)
4048 curbuf->b_p_ai = !curbuf->b_p_ai;
4049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004051 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * "end" is set to lnum when something has been appended, otherwise
4053 * it is the same than "start" -- Acevedo */
4054 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4055 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4056 if (eap->cmdidx != CMD_append)
4057 --curbuf->b_op_start.lnum;
4058 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4059 ? lnum : curbuf->b_op_start.lnum;
4060 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4061 curwin->w_cursor.lnum = lnum;
4062 check_cursor_lnum();
4063 beginline(BL_SOL | BL_FIX);
4064
4065 need_wait_return = FALSE; /* don't use wait_return() now */
4066 ex_no_reprint = TRUE;
4067}
4068
4069/*
4070 * ":change"
4071 */
4072 void
4073ex_change(eap)
4074 exarg_T *eap;
4075{
4076 linenr_T lnum;
4077
4078 if (eap->line2 >= eap->line1
4079 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4080 return;
4081
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004082 /* the ! flag toggles autoindent */
4083 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4084 append_indent = get_indent_lnum(eap->line1);
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4087 {
4088 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4089 break;
4090 ml_delete(eap->line1, FALSE);
4091 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004092
4093 /* make sure the cursor is not beyond the end of the file now */
4094 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4096
4097 /* ":append" on the line above the deleted lines. */
4098 eap->line2 = eap->line1;
4099 ex_append(eap);
4100}
4101
4102 void
4103ex_z(eap)
4104 exarg_T *eap;
4105{
4106 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004107 int bigness;
4108 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 int minus = 0;
4110 linenr_T start, end, curs, i;
4111 int j;
4112 linenr_T lnum = eap->line2;
4113
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004114 /* Vi compatible: ":z!" uses display height, without a count uses
4115 * 'scroll' */
4116 if (eap->forceit)
4117 bigness = curwin->w_height;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004118#ifdef FEAT_WINDOWS
Bram Moolenaar631abc32014-02-22 22:27:47 +01004119 else if (firstwin != lastwin)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004120 bigness = curwin->w_height - 3;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004121#endif
Bram Moolenaar631abc32014-02-22 22:27:47 +01004122 else
4123 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (bigness < 1)
4125 bigness = 1;
4126
4127 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004128 kind = x;
4129 if (*kind == '-' || *kind == '+' || *kind == '='
4130 || *kind == '^' || *kind == '.')
4131 ++x;
4132 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 ++x;
4134
4135 if (*x != 0)
4136 {
4137 if (!VIM_ISDIGIT(*x))
4138 {
4139 EMSG(_("E144: non-numeric argument to :z"));
4140 return;
4141 }
4142 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004145 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004146 if (*kind == '=')
4147 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004151 /* the number of '-' and '+' multiplies the distance */
4152 if (*kind == '-' || *kind == '+')
4153 for (x = kind + 1; *x == *kind; ++x)
4154 ;
4155
4156 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
4158 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004159 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4160 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004161 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 break;
4163
4164 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004165 start = lnum - (bigness + 1) / 2 + 1;
4166 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 curs = lnum;
4168 minus = 1;
4169 break;
4170
4171 case '^':
4172 start = lnum - bigness * 2;
4173 end = lnum - bigness;
4174 curs = lnum - bigness;
4175 break;
4176
4177 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004178 start = lnum - (bigness + 1) / 2 + 1;
4179 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 curs = end;
4181 break;
4182
4183 default: /* '+' */
4184 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004185 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004186 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004187 else if (eap->addr_count == 0)
4188 ++start;
4189 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 curs = end;
4191 break;
4192 }
4193
4194 if (start < 1)
4195 start = 1;
4196
4197 if (end > curbuf->b_ml.ml_line_count)
4198 end = curbuf->b_ml.ml_line_count;
4199
4200 if (curs > curbuf->b_ml.ml_line_count)
4201 curs = curbuf->b_ml.ml_line_count;
4202
4203 for (i = start; i <= end; i++)
4204 {
4205 if (minus && i == lnum)
4206 {
4207 msg_putchar('\n');
4208
4209 for (j = 1; j < Columns; j++)
4210 msg_putchar('-');
4211 }
4212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004213 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 if (minus && i == lnum)
4216 {
4217 msg_putchar('\n');
4218
4219 for (j = 1; j < Columns; j++)
4220 msg_putchar('-');
4221 }
4222 }
4223
4224 curwin->w_cursor.lnum = curs;
4225 ex_no_reprint = TRUE;
4226}
4227
4228/*
4229 * Check if the restricted flag is set.
4230 * If so, give an error message and return TRUE.
4231 * Otherwise, return FALSE.
4232 */
4233 int
4234check_restricted()
4235{
4236 if (restricted)
4237 {
4238 EMSG(_("E145: Shell commands not allowed in rvim"));
4239 return TRUE;
4240 }
4241 return FALSE;
4242}
4243
4244/*
4245 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4246 * If so, give an error message and return TRUE.
4247 * Otherwise, return FALSE.
4248 */
4249 int
4250check_secure()
4251{
4252 if (secure)
4253 {
4254 secure = 2;
4255 EMSG(_(e_curdir));
4256 return TRUE;
4257 }
4258#ifdef HAVE_SANDBOX
4259 /*
4260 * In the sandbox more things are not allowed, including the things
4261 * disallowed in secure mode.
4262 */
4263 if (sandbox != 0)
4264 {
4265 EMSG(_(e_sandbox));
4266 return TRUE;
4267 }
4268#endif
4269 return FALSE;
4270}
4271
4272static char_u *old_sub = NULL; /* previous substitute pattern */
4273static int global_need_beginline; /* call beginline() after ":g" */
4274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275/* do_sub()
4276 *
4277 * Perform a substitution from line eap->line1 to line eap->line2 using the
4278 * command pointed to by eap->arg which should be of the form:
4279 *
4280 * /pattern/substitution/{flags}
4281 *
4282 * The usual escapes are supported as described in the regexp docs.
4283 */
4284 void
4285do_sub(eap)
4286 exarg_T *eap;
4287{
4288 linenr_T lnum;
4289 long i = 0;
4290 regmmatch_T regmatch;
4291 static int do_all = FALSE; /* do multiple substitutions per line */
4292 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004293 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 static int do_error = TRUE; /* if false, ignore errors */
4295 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004296 static int do_list = FALSE; /* list last line with subs. */
4297 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 static int do_ic = 0; /* ignore case flag */
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004299 int save_do_all; /* remember user specified 'g' flag */
4300 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4302 int delimiter;
4303 int sublen;
4304 int got_quit = FALSE;
4305 int got_match = FALSE;
4306 int temp;
4307 int which_pat;
4308 char_u *cmd;
4309 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004310 linenr_T first_line = 0; /* first changed line */
4311 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * change */
4313 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4314 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004315 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004316 char_u *sub_firstline; /* allocated copy of first sub line */
4317 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004318 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004319 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004320#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004321 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 cmd = eap->arg;
4325 if (!global_busy)
4326 {
4327 sub_nsubs = 0;
4328 sub_nlines = 0;
4329 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004330 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (eap->cmdidx == CMD_tilde)
4333 which_pat = RE_LAST; /* use last used regexp */
4334 else
4335 which_pat = RE_SUBST; /* use last substitute regexp */
4336
4337 /* new pattern and substitution */
4338 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4339 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4340 {
4341 /* don't accept alphanumeric for separator */
4342 if (isalpha(*cmd))
4343 {
4344 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4345 return;
4346 }
4347 /*
4348 * undocumented vi feature:
4349 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4350 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4351 */
4352 if (*cmd == '\\')
4353 {
4354 ++cmd;
4355 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4356 {
4357 EMSG(_(e_backslash));
4358 return;
4359 }
4360 if (*cmd != '&')
4361 which_pat = RE_SEARCH; /* use last '/' pattern */
4362 pat = (char_u *)""; /* empty search pattern */
4363 delimiter = *cmd++; /* remember delimiter character */
4364 }
4365 else /* find the end of the regexp */
4366 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004367#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4368 if (p_altkeymap && curwin->w_p_rl)
4369 lrF_sub(cmd);
4370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 which_pat = RE_LAST; /* use last used regexp */
4372 delimiter = *cmd++; /* remember delimiter character */
4373 pat = cmd; /* remember start of search pat */
4374 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4375 if (cmd[0] == delimiter) /* end delimiter found */
4376 *cmd++ = NUL; /* replace it with a NUL */
4377 }
4378
4379 /*
4380 * Small incompatibility: vi sees '\n' as end of the command, but in
4381 * Vim we want to use '\n' to find/substitute a NUL.
4382 */
4383 sub = cmd; /* remember the start of the substitution */
4384
4385 while (cmd[0])
4386 {
4387 if (cmd[0] == delimiter) /* end delimiter found */
4388 {
4389 *cmd++ = NUL; /* replace it with a NUL */
4390 break;
4391 }
4392 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4393 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004394 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396
4397 if (!eap->skip)
4398 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004399 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4400 if (STRCMP(sub, "%") == 0
4401 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4402 {
4403 if (old_sub == NULL) /* there is no previous command */
4404 {
4405 EMSG(_(e_nopresub));
4406 return;
4407 }
4408 sub = old_sub;
4409 }
4410 else
4411 {
4412 vim_free(old_sub);
4413 old_sub = vim_strsave(sub);
4414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 }
4417 else if (!eap->skip) /* use previous pattern and substitution */
4418 {
4419 if (old_sub == NULL) /* there is no previous command */
4420 {
4421 EMSG(_(e_nopresub));
4422 return;
4423 }
4424 pat = NULL; /* search_regcomp() will use previous pattern */
4425 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004426
4427 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4428 * last column after using "$". */
4429 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004432 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4433 * more efficient.
4434 * TODO: find a generic solution to make line-joining operations more
4435 * efficient, avoid allocating a string that grows in size.
4436 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004437 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004438 && *sub == NUL
4439 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4440 || *cmd == 'p' || *cmd == '#'))))
4441 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004442 linenr_T joined_lines_count;
4443
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004444 curwin->w_cursor.lnum = eap->line1;
4445 if (*cmd == 'l')
4446 eap->flags = EXFLAG_LIST;
4447 else if (*cmd == '#')
4448 eap->flags = EXFLAG_NR;
4449 else if (*cmd == 'p')
4450 eap->flags = EXFLAG_PRINT;
4451
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004452 /* The number of lines joined is the number of lines in the range plus
4453 * one. One less when the last line is included. */
4454 joined_lines_count = eap->line2 - eap->line1 + 1;
4455 if (eap->line2 < curbuf->b_ml.ml_line_count)
4456 ++joined_lines_count;
4457 if (joined_lines_count > 1)
4458 {
4459 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
4460 sub_nsubs = joined_lines_count - 1;
4461 sub_nlines = 1;
4462 (void)do_sub_msg(FALSE);
4463 ex_may_print(eap);
4464 }
4465
4466 if (!cmdmod.keeppatterns)
4467 save_re_pat(RE_SUBST, pat, p_magic);
4468#ifdef FEAT_CMDHIST
4469 /* put pattern in history */
4470 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
4471#endif
4472
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004473 return;
4474 }
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /*
4477 * Find trailing options. When '&' is used, keep old options.
4478 */
4479 if (*cmd == '&')
4480 ++cmd;
4481 else
4482 {
4483 if (!p_ed)
4484 {
4485 if (p_gd) /* default is global on */
4486 do_all = TRUE;
4487 else
4488 do_all = FALSE;
4489 do_ask = FALSE;
4490 }
4491 do_error = TRUE;
4492 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004493 do_count = FALSE;
Bram Moolenaarfe40d1a2007-07-24 09:16:38 +00004494 do_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 do_ic = 0;
4496 }
4497 while (*cmd)
4498 {
4499 /*
4500 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4501 * 'r' is never inverted.
4502 */
4503 if (*cmd == 'g')
4504 do_all = !do_all;
4505 else if (*cmd == 'c')
4506 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004507 else if (*cmd == 'n')
4508 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else if (*cmd == 'e')
4510 do_error = !do_error;
4511 else if (*cmd == 'r') /* use last used regexp */
4512 which_pat = RE_LAST;
4513 else if (*cmd == 'p')
4514 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004515 else if (*cmd == '#')
4516 {
4517 do_print = TRUE;
4518 do_number = TRUE;
4519 }
4520 else if (*cmd == 'l')
4521 {
4522 do_print = TRUE;
4523 do_list = TRUE;
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 else if (*cmd == 'i') /* ignore case */
4526 do_ic = 'i';
4527 else if (*cmd == 'I') /* don't ignore case */
4528 do_ic = 'I';
4529 else
4530 break;
4531 ++cmd;
4532 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004533 if (do_count)
4534 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004536 save_do_all = do_all;
4537 save_do_ask = do_ask;
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 /*
4540 * check for a trailing count
4541 */
4542 cmd = skipwhite(cmd);
4543 if (VIM_ISDIGIT(*cmd))
4544 {
4545 i = getdigits(&cmd);
4546 if (i <= 0 && !eap->skip && do_error)
4547 {
4548 EMSG(_(e_zerocount));
4549 return;
4550 }
4551 eap->line1 = eap->line2;
4552 eap->line2 += i - 1;
4553 if (eap->line2 > curbuf->b_ml.ml_line_count)
4554 eap->line2 = curbuf->b_ml.ml_line_count;
4555 }
4556
4557 /*
4558 * check for trailing command or garbage
4559 */
4560 cmd = skipwhite(cmd);
4561 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4562 {
4563 eap->nextcmd = check_nextcmd(cmd);
4564 if (eap->nextcmd == NULL)
4565 {
4566 EMSG(_(e_trailing));
4567 return;
4568 }
4569 }
4570
4571 if (eap->skip) /* not executing commands, only parsing */
4572 return;
4573
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004574 if (!do_count && !curbuf->b_p_ma)
4575 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004576 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004577 EMSG(_(e_modifiable));
4578 return;
4579 }
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4582 {
4583 if (do_error)
4584 EMSG(_(e_invcmd));
4585 return;
4586 }
4587
4588 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4589 if (do_ic == 'i')
4590 regmatch.rmm_ic = TRUE;
4591 else if (do_ic == 'I')
4592 regmatch.rmm_ic = FALSE;
4593
4594 sub_firstline = NULL;
4595
4596 /*
4597 * ~ in the substitute pattern is replaced with the old pattern.
4598 * We do it here once to avoid it to be replaced over and over again.
4599 * But don't do it when it starts with "\=", then it's an expression.
4600 */
4601 if (!(sub[0] == '\\' && sub[1] == '='))
4602 sub = regtilde(sub, p_magic);
4603
4604 /*
4605 * Check for a match on each line.
4606 */
4607 line2 = eap->line2;
4608 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4609#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4610 || aborting()
4611#endif
4612 ); ++lnum)
4613 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004614 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
4615 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (nmatch)
4617 {
4618 colnr_T copycol;
4619 colnr_T matchcol;
4620 colnr_T prev_matchcol = MAXCOL;
4621 char_u *new_end, *new_start = NULL;
4622 unsigned new_start_len = 0;
4623 char_u *p1;
4624 int did_sub = FALSE;
4625 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004626 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 long nmatch_tl = 0; /* nr of lines matched below lnum */
4628 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004629 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004630 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
4632 /*
4633 * The new text is build up step by step, to avoid too much
4634 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00004635 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 * copycol Column in the old text where we started
4637 * looking for a match; from here old text still
4638 * needs to be copied to the new text.
4639 * matchcol Column number of the old text where to look
4640 * for the next match. It's just after the
4641 * previous match or one further.
4642 * prev_matchcol Column just after the previous match (if any).
4643 * Mostly equal to matchcol, except for the first
4644 * match and after skipping an empty match.
4645 * regmatch.*pos Where the pattern matched in the old text.
4646 * new_start The new text, all that has been produced so
4647 * far.
4648 * new_end The new text, where to append new text.
4649 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004650 * lnum The line number where we found the start of
4651 * the match. Can be below the line we searched
4652 * when there is a \n before a \zs in the
4653 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 * sub_firstlnum The line number in the buffer where to look
4655 * for a match. Can be different from "lnum"
4656 * when the pattern or substitute string contains
4657 * line breaks.
4658 *
4659 * Special situations:
4660 * - When the substitute string contains a line break, the part up
4661 * to the line break is inserted in the text, but the copy of
4662 * the original line is kept. "sub_firstlnum" is adjusted for
4663 * the inserted lines.
4664 * - When the matched pattern contains a line break, the old line
4665 * is taken from the line at the end of the pattern. The lines
4666 * in the match are deleted later, "sub_firstlnum" is adjusted
4667 * accordingly.
4668 *
4669 * The new text is built up in new_start[]. It has some extra
4670 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004671 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 *
4673 * Make a copy of the old line, so it won't be taken away when
4674 * updating the screen or handling a multi-line match. The "old_"
4675 * pointers point into this copy.
4676 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004677 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 copycol = 0;
4679 matchcol = 0;
4680
4681 /* At first match, remember current cursor position. */
4682 if (!got_match)
4683 {
4684 setpcmark();
4685 got_match = TRUE;
4686 }
4687
4688 /*
4689 * Loop until nothing more to replace in this line.
4690 * 1. Handle match with empty string.
4691 * 2. If do_ask is set, ask for confirmation.
4692 * 3. substitute the string.
4693 * 4. if do_all is set, find next match
4694 * 5. break if there isn't another match in this line
4695 */
4696 for (;;)
4697 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004698 /* Advance "lnum" to the line where the match starts. The
4699 * match does not start in the first line when there is a line
4700 * break before \zs. */
4701 if (regmatch.startpos[0].lnum > 0)
4702 {
4703 lnum += regmatch.startpos[0].lnum;
4704 sub_firstlnum += regmatch.startpos[0].lnum;
4705 nmatch -= regmatch.startpos[0].lnum;
4706 vim_free(sub_firstline);
4707 sub_firstline = NULL;
4708 }
4709
4710 if (sub_firstline == NULL)
4711 {
4712 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4713 if (sub_firstline == NULL)
4714 {
4715 vim_free(new_start);
4716 goto outofmem;
4717 }
4718 }
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 /* Save the line number of the last change for the final
4721 * cursor position (just like Vi). */
4722 curwin->w_cursor.lnum = lnum;
4723 do_again = FALSE;
4724
4725 /*
4726 * 1. Match empty string does not count, except for first
4727 * match. This reproduces the strange vi behaviour.
4728 * This also catches endless loops.
4729 */
4730 if (matchcol == prev_matchcol
4731 && regmatch.endpos[0].lnum == 0
4732 && matchcol == regmatch.endpos[0].col)
4733 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004734 if (sub_firstline[matchcol] == NUL)
4735 /* We already were at the end of the line. Don't look
4736 * for a match in this line again. */
4737 skip_match = TRUE;
4738 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02004739 {
4740 /* search for a match at next column */
4741#ifdef FEAT_MBYTE
4742 if (has_mbyte)
4743 matchcol += mb_ptr2len(sub_firstline + matchcol);
4744 else
4745#endif
4746 ++matchcol;
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 goto skip;
4749 }
4750
4751 /* Normally we continue searching for a match just after the
4752 * previous match. */
4753 matchcol = regmatch.endpos[0].col;
4754 prev_matchcol = matchcol;
4755
4756 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004757 * 2. If do_count is set only increase the counter.
4758 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004760 if (do_count)
4761 {
4762 /* For a multi-line match, put matchcol at the NUL at
4763 * the end of the line and set nmatch to one, so that
4764 * we continue looking for a match on the next line.
4765 * Avoids that ":s/\nB\@=//gc" get stuck. */
4766 if (nmatch > 1)
4767 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004768 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004769 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00004770 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004771 }
4772 sub_nsubs++;
4773 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004774#ifdef FEAT_EVAL
4775 /* Skip the substitution, unless an expression is used,
4776 * then it is evaluated in the sandbox. */
4777 if (!(sub[0] == '\\' && sub[1] == '='))
4778#endif
4779 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004780 }
4781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (do_ask)
4783 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00004784 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /* change State to CONFIRM, so that the mouse works
4787 * properly */
4788 save_State = State;
4789 State = CONFIRM;
4790#ifdef FEAT_MOUSE
4791 setmouse(); /* disable mouse in xterm */
4792#endif
4793 curwin->w_cursor.col = regmatch.startpos[0].col;
4794
4795 /* When 'cpoptions' contains "u" don't sync undo when
4796 * asking for confirmation. */
4797 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4798 ++no_u_sync;
4799
4800 /*
4801 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4802 */
4803 while (do_ask)
4804 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004805 if (exmode_active)
4806 {
4807 char_u *resp;
4808 colnr_T sc, ec;
4809
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004810 print_line_no_prefix(lnum, do_number, do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004811
4812 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4813 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4814 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004815 if (do_number || curwin->w_p_nu)
4816 {
4817 int numw = number_width(curwin) + 1;
4818 sc += numw;
4819 ec += numw;
4820 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004821 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004822 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004823 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004824 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004825 msg_putchar('^');
4826
4827 resp = getexmodeline('?', NULL, 0);
4828 if (resp != NULL)
4829 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004830 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004831 vim_free(resp);
4832 }
4833 }
4834 else
4835 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004836 char_u *orig_line = NULL;
4837 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004839 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004841 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004843 /* Invert the matched string.
4844 * Remove the inversion afterwards. */
4845 temp = RedrawingDisabled;
4846 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004848 if (new_start != NULL)
4849 {
4850 /* There already was a substitution, we would
4851 * like to show this to the user. We cannot
4852 * really update the line, it would change
4853 * what matches. Temporarily replace the line
4854 * and change it back afterwards. */
4855 orig_line = vim_strsave(ml_get(lnum));
4856 if (orig_line != NULL)
4857 {
4858 char_u *new_line = concat_str(new_start,
4859 sub_firstline + copycol);
4860
4861 if (new_line == NULL)
4862 {
4863 vim_free(orig_line);
4864 orig_line = NULL;
4865 }
4866 else
4867 {
4868 /* Position the cursor relative to the
4869 * end of the line, the previous
4870 * substitute may have inserted or
4871 * deleted characters before the
4872 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01004873 len_change = (int)STRLEN(new_line)
4874 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004875 curwin->w_cursor.col += len_change;
4876 ml_replace(lnum, new_line, FALSE);
4877 }
4878 }
4879 }
4880
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004881 search_match_lines = regmatch.endpos[0].lnum
4882 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004883 search_match_endcol = regmatch.endpos[0].col
4884 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004885 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004887 update_topline();
4888 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00004889 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004890 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00004891 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004894 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004896 if (msg_row == Rows - 1)
4897 msg_didout = FALSE; /* avoid a scroll-up */
4898 msg_starthere();
4899 i = msg_scroll;
4900 msg_scroll = 0; /* truncate msg when
4901 needed */
4902 msg_no_more = TRUE;
4903 /* write message same highlighting as for
4904 * wait_return */
4905 smsg_attr(hl_attr(HLF_R),
4906 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4907 msg_no_more = FALSE;
4908 msg_scroll = i;
4909 showruler(TRUE);
4910 windgoto(msg_row, msg_col);
4911 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004914 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004916 ++no_mapping; /* don't map this key */
4917 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004918 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004919 --allow_keys;
4920 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004922 /* clear the question */
4923 msg_didout = FALSE; /* don't scroll up */
4924 msg_col = 0;
4925 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004926
4927 /* restore the line */
4928 if (orig_line != NULL)
4929 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004930 }
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004933 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004935 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
4937 )
4938 {
4939 got_quit = TRUE;
4940 break;
4941 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004942 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004944 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004946 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 /* last: replace and then stop */
4949 do_all = FALSE;
4950 line2 = lnum;
4951 break;
4952 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004953 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 do_ask = FALSE;
4956 break;
4957 }
4958#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004959 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004961 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 scrolldown_clamp();
4963#endif
4964 }
4965 State = save_State;
4966#ifdef FEAT_MOUSE
4967 setmouse();
4968#endif
4969 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4970 --no_u_sync;
4971
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004972 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 /* For a multi-line match, put matchcol at the NUL at
4975 * the end of the line and set nmatch to one, so that
4976 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00004977 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
4978 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (nmatch > 1)
4980 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004981 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00004982 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 goto skip;
4985 }
4986 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01004987 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989
4990 /* Move the cursor to the start of the match, so that we can
4991 * use "\=col("."). */
4992 curwin->w_cursor.col = regmatch.startpos[0].col;
4993
4994 /*
4995 * 3. substitute the string.
4996 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004997#ifdef FEAT_EVAL
4998 if (do_count)
4999 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005000 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005001 save_ma = curbuf->b_p_ma;
5002 curbuf->b_p_ma = FALSE;
5003 sandbox++;
5004 }
5005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005007 sublen = vim_regsub_multi(&regmatch,
5008 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005010#ifdef FEAT_EVAL
5011 if (do_count)
5012 {
5013 curbuf->b_p_ma = save_ma;
5014 sandbox--;
5015 goto skip;
5016 }
5017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
Bram Moolenaar4463f292005-09-25 22:20:24 +00005019 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005020 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005021 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5022 {
5023 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5024 skip_match = TRUE;
5025 }
5026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /* Need room for:
5028 * - result so far in new_start (not for first sub in line)
5029 * - original text up to match
5030 * - length of substituted part
5031 * - original text after match
5032 */
5033 if (nmatch == 1)
5034 p1 = sub_firstline;
5035 else
5036 {
5037 p1 = ml_get(sub_firstlnum + nmatch - 1);
5038 nmatch_tl += nmatch - 1;
5039 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005040 copy_len = regmatch.startpos[0].col - copycol;
5041 needed_len = copy_len + ((unsigned)STRLEN(p1)
5042 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (new_start == NULL)
5044 {
5045 /*
5046 * Get some space for a temporary buffer to do the
5047 * substitution into (and some extra space to avoid
5048 * too many calls to alloc()/free()).
5049 */
5050 new_start_len = needed_len + 50;
5051 if ((new_start = alloc_check(new_start_len)) == NULL)
5052 goto outofmem;
5053 *new_start = NUL;
5054 new_end = new_start;
5055 }
5056 else
5057 {
5058 /*
5059 * Check if the temporary buffer is long enough to do the
5060 * substitution into. If not, make it larger (with a bit
5061 * extra to avoid too many calls to alloc()/free()).
5062 */
5063 len = (unsigned)STRLEN(new_start);
5064 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005065 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 new_start_len = needed_len + 50;
5068 if ((p1 = alloc_check(new_start_len)) == NULL)
5069 {
5070 vim_free(new_start);
5071 goto outofmem;
5072 }
5073 mch_memmove(p1, new_start, (size_t)(len + 1));
5074 vim_free(new_start);
5075 new_start = p1;
5076 }
5077 new_end = new_start + len;
5078 }
5079
5080 /*
5081 * copy the text up to the part that matched
5082 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005083 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5084 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005086 (void)vim_regsub_multi(&regmatch,
5087 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 sub, new_end, TRUE, p_magic, TRUE);
5089 sub_nsubs++;
5090 did_sub = TRUE;
5091
5092 /* Move the cursor to the start of the line, to avoid that it
5093 * is beyond the end of the line after the substitution. */
5094 curwin->w_cursor.col = 0;
5095
5096 /* For a multi-line match, make a copy of the last matched
5097 * line and continue in that one. */
5098 if (nmatch > 1)
5099 {
5100 sub_firstlnum += nmatch - 1;
5101 vim_free(sub_firstline);
5102 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5103 /* When going beyond the last line, stop substituting. */
5104 if (sub_firstlnum <= line2)
5105 do_again = TRUE;
5106 else
5107 do_all = FALSE;
5108 }
5109
5110 /* Remember next character to be copied. */
5111 copycol = regmatch.endpos[0].col;
5112
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005113 if (skip_match)
5114 {
5115 /* Already hit end of the buffer, sub_firstlnum is one
5116 * less than what it ought to be. */
5117 vim_free(sub_firstline);
5118 sub_firstline = vim_strsave((char_u *)"");
5119 copycol = 0;
5120 }
5121
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 /*
5123 * Now the trick is to replace CTRL-M chars with a real line
5124 * break. This would make it impossible to insert a CTRL-M in
5125 * the text. The line break can be avoided by preceding the
5126 * CTRL-M with a backslash. To be able to insert a backslash,
5127 * they must be doubled in the string and are halved here.
5128 * That is Vi compatible.
5129 */
5130 for (p1 = new_end; *p1; ++p1)
5131 {
5132 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else if (*p1 == CAR)
5135 {
5136 if (u_inssub(lnum) == OK) /* prepare for undo */
5137 {
5138 *p1 = NUL; /* truncate up to the CR */
5139 ml_append(lnum - 1, new_start,
5140 (colnr_T)(p1 - new_start + 1), FALSE);
5141 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
5142 if (do_ask)
5143 appended_lines(lnum - 1, 1L);
5144 else
5145 {
5146 if (first_line == 0)
5147 first_line = lnum;
5148 last_line = lnum + 1;
5149 }
5150 /* All line numbers increase. */
5151 ++sub_firstlnum;
5152 ++lnum;
5153 ++line2;
5154 /* move the cursor to the new line, like Vi */
5155 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005156 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005157 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 p1 = new_start - 1;
5159 }
5160 }
5161#ifdef FEAT_MBYTE
5162 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005163 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
5165 }
5166
5167 /*
5168 * 4. If do_all is set, find next match.
5169 * Prevent endless loop with patterns that match empty
5170 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5171 * But ":s/\n/#/" is OK.
5172 */
5173skip:
5174 /* We already know that we did the last subst when we are at
5175 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005176 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5177 * "line2" when there is a \zs in the pattern after a line
5178 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005179 lastone = (skip_match
5180 || got_int
5181 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005182 || lnum > line2
Bram Moolenaar8299df92004-07-10 09:47:34 +00005183 || !(do_all || do_again)
5184 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5185 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 nmatch = -1;
5187
5188 /*
5189 * Replace the line in the buffer when needed. This is
5190 * skipped when there are more matches.
5191 * The check for nmatch_tl is needed for when multi-line
5192 * matching must replace the lines before trying to do another
5193 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005194 * When the match starts below where we start searching also
5195 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 */
5197 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 || nmatch_tl > 0
5199 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005200 curbuf, sub_firstlnum,
5201 matchcol, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005202 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 if (new_start != NULL)
5205 {
5206 /*
5207 * Copy the rest of the line, that didn't match.
5208 * "matchcol" has to be adjusted, we use the end of
5209 * the line as reference, because the substitute may
5210 * have changed the number of characters. Same for
5211 * "prev_matchcol".
5212 */
5213 STRCAT(new_start, sub_firstline + copycol);
5214 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5215 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5216 - prev_matchcol;
5217
5218 if (u_savesub(lnum) != OK)
5219 break;
5220 ml_replace(lnum, new_start, TRUE);
5221
5222 if (nmatch_tl > 0)
5223 {
5224 /*
5225 * Matched lines have now been substituted and are
5226 * useless, delete them. The part after the match
5227 * has been appended to new_start, we don't need
5228 * it in the buffer.
5229 */
5230 ++lnum;
5231 if (u_savedel(lnum, nmatch_tl) != OK)
5232 break;
5233 for (i = 0; i < nmatch_tl; ++i)
5234 ml_delete(lnum, (int)FALSE);
5235 mark_adjust(lnum, lnum + nmatch_tl - 1,
5236 (long)MAXLNUM, -nmatch_tl);
5237 if (do_ask)
5238 deleted_lines(lnum, nmatch_tl);
5239 --lnum;
5240 line2 -= nmatch_tl; /* nr of lines decreases */
5241 nmatch_tl = 0;
5242 }
5243
5244 /* When asking, undo is saved each time, must also set
5245 * changed flag each time. */
5246 if (do_ask)
5247 changed_bytes(lnum, 0);
5248 else
5249 {
5250 if (first_line == 0)
5251 first_line = lnum;
5252 last_line = lnum + 1;
5253 }
5254
5255 sub_firstlnum = lnum;
5256 vim_free(sub_firstline); /* free the temp buffer */
5257 sub_firstline = new_start;
5258 new_start = NULL;
5259 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5260 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5261 - prev_matchcol;
5262 copycol = 0;
5263 }
5264 if (nmatch == -1 && !lastone)
5265 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005266 sub_firstlnum, matchcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 /*
5269 * 5. break if there isn't another match in this line
5270 */
5271 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005272 {
5273 /* If the match found didn't start where we were
5274 * searching, do the next search in the line where we
5275 * found the match. */
5276 if (nmatch == -1)
5277 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281
5282 line_breakcheck();
5283 }
5284
5285 if (did_sub)
5286 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005287 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 vim_free(sub_firstline); /* free the copy of the original line */
5289 sub_firstline = NULL;
5290 }
5291
5292 line_breakcheck();
5293 }
5294
5295 if (first_line != 0)
5296 {
5297 /* Need to subtract the number of added lines from "last_line" to get
5298 * the line number before the change (same as adding the number of
5299 * deleted lines). */
5300 i = curbuf->b_ml.ml_line_count - old_line_count;
5301 changed_lines(first_line, 0, last_line - i, i);
5302 }
5303
5304outofmem:
5305 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005306
5307 /* ":s/pat//n" doesn't move the cursor */
5308 if (do_count)
5309 curwin->w_cursor = old_cursor;
5310
Bram Moolenaar46475522010-03-23 17:36:29 +01005311 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
5313 /* Set the '[ and '] marks. */
5314 curbuf->b_op_start.lnum = eap->line1;
5315 curbuf->b_op_end.lnum = line2;
5316 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5317
5318 if (!global_busy)
5319 {
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005320 if (!do_ask) /* when interactive leave cursor on the match */
5321 {
5322 if (endcolumn)
5323 coladvance((colnr_T)MAXCOL);
5324 else
5325 beginline(BL_WHITE | BL_FIX);
5326 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005327 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 MSG("");
5329 }
5330 else
5331 global_need_beginline = TRUE;
5332 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005333 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 else if (!global_busy)
5336 {
5337 if (got_int) /* interrupted */
5338 EMSG(_(e_interr));
5339 else if (got_match) /* did find something but nothing substituted */
5340 MSG("");
5341 else if (do_error) /* nothing found */
5342 EMSG2(_(e_patnotf2), get_search_pat());
5343 }
5344
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005345#ifdef FEAT_FOLDING
5346 if (do_ask && hasAnyFolding(curwin))
5347 /* Cursor position may require updating */
5348 changed_window_setting();
5349#endif
5350
Bram Moolenaar473de612013-06-08 18:19:48 +02005351 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005352
5353 /* Restore the flag values, they can be used for ":&&". */
5354 do_all = save_do_all;
5355 do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356}
5357
5358/*
5359 * Give message for number of substitutions.
5360 * Can also be used after a ":global" command.
5361 * Return TRUE if a message was given.
5362 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005363 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00005364do_sub_msg(count_only)
5365 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 /*
5368 * Only report substitutions when:
5369 * - more than 'report' substitutions
5370 * - command was typed by user, or number of changed lines > 'report'
5371 * - giving messages is not disabled by 'lazyredraw'
5372 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005373 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5374 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 && messaging())
5376 {
5377 if (got_int)
5378 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005379 else
5380 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005382 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005383 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005385 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005386 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 sub_nsubs);
5388 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005389 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005390 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005392 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005393 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005396 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 return TRUE;
5398 }
5399 if (got_int)
5400 {
5401 EMSG(_(e_interr));
5402 return TRUE;
5403 }
5404 return FALSE;
5405}
5406
5407/*
5408 * Execute a global command of the form:
5409 *
5410 * g/pattern/X : execute X on all lines where pattern matches
5411 * v/pattern/X : execute X on all lines where pattern does not match
5412 *
5413 * where 'X' is an EX command
5414 *
5415 * The command character (as well as the trailing slash) is optional, and
5416 * is assumed to be 'p' if missing.
5417 *
5418 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005419 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 * for each line that has a mark. This is required because after deleting
5421 * lines we do not know where to search for the next match.
5422 */
5423 void
5424ex_global(eap)
5425 exarg_T *eap;
5426{
5427 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005428 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 int type; /* first char of cmd: 'v' or 'g' */
5430 char_u *cmd; /* command argument */
5431
5432 char_u delim; /* delimiter, normally '/' */
5433 char_u *pat;
5434 regmmatch_T regmatch;
5435 int match;
5436 int which_pat;
5437
5438 if (global_busy)
5439 {
5440 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5441 return;
5442 }
5443
5444 if (eap->forceit) /* ":global!" is like ":vglobal" */
5445 type = 'v';
5446 else
5447 type = *eap->cmd;
5448 cmd = eap->arg;
5449 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 /*
5452 * undocumented vi feature:
5453 * "\/" and "\?": use previous search pattern.
5454 * "\&": use previous substitute pattern.
5455 */
5456 if (*cmd == '\\')
5457 {
5458 ++cmd;
5459 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5460 {
5461 EMSG(_(e_backslash));
5462 return;
5463 }
5464 if (*cmd == '&')
5465 which_pat = RE_SUBST; /* use previous substitute pattern */
5466 else
5467 which_pat = RE_SEARCH; /* use previous search pattern */
5468 ++cmd;
5469 pat = (char_u *)"";
5470 }
5471 else if (*cmd == NUL)
5472 {
5473 EMSG(_("E148: Regular expression missing from global"));
5474 return;
5475 }
5476 else
5477 {
5478 delim = *cmd; /* get the delimiter */
5479 if (delim)
5480 ++cmd; /* skip delimiter if there is one */
5481 pat = cmd; /* remember start of pattern */
5482 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5483 if (cmd[0] == delim) /* end delimiter found */
5484 *cmd++ = NUL; /* replace it with a NUL */
5485 }
5486
5487#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5488 if (p_altkeymap && curwin->w_p_rl)
5489 lrFswap(pat,0);
5490#endif
5491
5492 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5493 {
5494 EMSG(_(e_invcmd));
5495 return;
5496 }
5497
5498 /*
5499 * pass 1: set marks for each (not) matching line
5500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5502 {
5503 /* a match on this line? */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005504 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5505 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 if ((type == 'g' && match) || (type == 'v' && !match))
5507 {
5508 ml_setmarked(lnum);
5509 ndone++;
5510 }
5511 line_breakcheck();
5512 }
5513
5514 /*
5515 * pass 2: execute the command for each line that has been marked
5516 */
5517 if (got_int)
5518 MSG(_(e_interr));
5519 else if (ndone == 0)
5520 {
5521 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005522 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 else
Bram Moolenaarc389fd32013-03-07 16:08:35 +01005524 smsg((char_u *)_("Pattern not found: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
5526 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005527 {
5528#ifdef FEAT_CLIPBOARD
5529 start_global_changes();
5530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005532#ifdef FEAT_CLIPBOARD
5533 end_global_changes();
5534#endif
5535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar473de612013-06-08 18:19:48 +02005538 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541/*
5542 * Execute "cmd" on lines marked with ml_setmarked().
5543 */
5544 void
5545global_exe(cmd)
5546 char_u *cmd;
5547{
Bram Moolenaarbb993222011-05-10 16:00:47 +02005548 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5549 buf_T *old_buf = curbuf; /* remember what buffer we started in */
5550 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 /*
5553 * Set current position only once for a global command.
5554 * If global_busy is set, setpcmark() will not do anything.
5555 * If there is an error, global_busy will be incremented.
5556 */
5557 setpcmark();
5558
5559 /* When the command writes a message, don't overwrite the command. */
5560 msg_didout = TRUE;
5561
Bram Moolenaard25bc232010-03-23 17:49:24 +01005562 sub_nsubs = 0;
5563 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 global_need_beginline = FALSE;
5565 global_busy = 1;
5566 old_lcount = curbuf->b_ml.ml_line_count;
5567 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5568 {
5569 curwin->w_cursor.lnum = lnum;
5570 curwin->w_cursor.col = 0;
5571 if (*cmd == NUL || *cmd == '\n')
5572 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5573 else
5574 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5575 ui_breakcheck();
5576 }
5577
5578 global_busy = 0;
5579 if (global_need_beginline)
5580 beginline(BL_WHITE | BL_FIX);
5581 else
5582 check_cursor(); /* cursor may be beyond the end of the line */
5583
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005584 /* the cursor may not have moved in the text but a change in a previous
5585 * line may move it on the screen */
5586 changed_line_abv_curs();
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 /* If it looks like no message was written, allow overwriting the
5589 * command with the report for number of changes. */
5590 if (msg_col == 0 && msg_scrolled == 0)
5591 msg_didout = FALSE;
5592
Bram Moolenaar45667512007-05-10 19:24:43 +00005593 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02005594 * number of extra or deleted lines.
5595 * Don't report extra or deleted lines in the edge case where the buffer
5596 * we are in after execution is different from the buffer we started in. */
5597 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5599}
5600
5601#ifdef FEAT_VIMINFO
5602 int
5603read_viminfo_sub_string(virp, force)
5604 vir_T *virp;
5605 int force;
5606{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005607 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 vim_free(old_sub);
5609 if (force || old_sub == NULL)
5610 old_sub = viminfo_readstring(virp, 1, TRUE);
5611 return viminfo_readline(virp);
5612}
5613
5614 void
5615write_viminfo_sub_string(fp)
5616 FILE *fp;
5617{
5618 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5619 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005620 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 viminfo_writestring(fp, old_sub);
5622 }
5623}
5624#endif /* FEAT_VIMINFO */
5625
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005626#if defined(EXITFREE) || defined(PROTO)
5627 void
5628free_old_sub()
5629{
5630 vim_free(old_sub);
5631}
5632#endif
5633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5635/*
5636 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005637 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005639 int
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005640prepare_tagpreview(undo_sync)
5641 int undo_sync; /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 win_T *wp;
5644
5645# ifdef FEAT_GUI
5646 need_mouse_correct = TRUE;
5647# endif
5648
5649 /*
5650 * If there is already a preview window open, use that one.
5651 */
5652 if (!curwin->w_p_pvw)
5653 {
5654 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5655 if (wp->w_p_pvw)
5656 break;
5657 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005658 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else
5660 {
5661 /*
5662 * There is no preview window open yet. Create one.
5663 */
5664 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5665 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005666 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 curwin->w_p_pvw = TRUE;
5668 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005669 RESET_BINDING(curwin); /* don't take over 'scrollbind'
5670 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671# ifdef FEAT_DIFF
5672 curwin->w_p_diff = FALSE; /* no 'diff' */
5673# endif
5674# ifdef FEAT_FOLDING
5675 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5676# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005677 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005680 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681}
5682
5683#endif
5684
5685
5686/*
5687 * ":help": open a read-only window on a help file
5688 */
5689 void
5690ex_help(eap)
5691 exarg_T *eap;
5692{
5693 char_u *arg;
5694 char_u *tag;
5695 FILE *helpfd; /* file descriptor of help file */
5696 int n;
5697 int i;
5698#ifdef FEAT_WINDOWS
5699 win_T *wp;
5700#endif
5701 int num_matches;
5702 char_u **matches;
5703 char_u *p;
5704 int empty_fnum = 0;
5705 int alt_fnum = 0;
5706 buf_T *buf;
5707#ifdef FEAT_MULTI_LANG
5708 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005709 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02005711#ifdef FEAT_FOLDING
5712 int old_KeyTyped = KeyTyped;
5713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 if (eap != NULL)
5716 {
5717 /*
5718 * A ":help" command ends at the first LF, or at a '|' that is
5719 * followed by some text. Set nextcmd to the following command.
5720 */
5721 for (arg = eap->arg; *arg; ++arg)
5722 {
5723 if (*arg == '\n' || *arg == '\r'
5724 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5725 {
5726 *arg++ = NUL;
5727 eap->nextcmd = arg;
5728 break;
5729 }
5730 }
5731 arg = eap->arg;
5732
Bram Moolenaar3dbde622012-04-05 16:05:05 +02005733 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
5735 EMSG(_("E478: Don't panic!"));
5736 return;
5737 }
5738
5739 if (eap->skip) /* not executing commands */
5740 return;
5741 }
5742 else
5743 arg = (char_u *)"";
5744
5745 /* remove trailing blanks */
5746 p = arg + STRLEN(arg) - 1;
5747 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5748 *p-- = NUL;
5749
5750#ifdef FEAT_MULTI_LANG
5751 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005752 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#endif
5754
5755 /* When no argument given go to the index. */
5756 if (*arg == NUL)
5757 arg = (char_u *)"help.txt";
5758
5759 /*
5760 * Check if there is a match for the argument.
5761 */
5762 n = find_help_tags(arg, &num_matches, &matches,
5763 eap != NULL && eap->forceit);
5764
5765 i = 0;
5766#ifdef FEAT_MULTI_LANG
5767 if (n != FAIL && lang != NULL)
5768 /* Find first item with the requested language. */
5769 for (i = 0; i < num_matches; ++i)
5770 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005771 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (len > 3 && matches[i][len - 3] == '@'
5773 && STRICMP(matches[i] + len - 2, lang) == 0)
5774 break;
5775 }
5776#endif
5777 if (i >= num_matches || n == FAIL)
5778 {
5779#ifdef FEAT_MULTI_LANG
5780 if (lang != NULL)
5781 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5782 else
5783#endif
5784 EMSG2(_("E149: Sorry, no help for %s"), arg);
5785 if (n != FAIL)
5786 FreeWild(num_matches, matches);
5787 return;
5788 }
5789
5790 /* The first match (in the requested language) is the best match. */
5791 tag = vim_strsave(matches[i]);
5792 FreeWild(num_matches, matches);
5793
5794#ifdef FEAT_GUI
5795 need_mouse_correct = TRUE;
5796#endif
5797
5798 /*
5799 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005800 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005802 if (!curwin->w_buffer->b_help
5803#ifdef FEAT_WINDOWS
5804 || cmdmod.tab != 0
5805#endif
5806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
5808#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005809 if (cmdmod.tab != 0)
5810 wp = NULL;
5811 else
5812 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5813 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5816 win_enter(wp, TRUE);
5817 else
5818#endif
5819 {
5820 /*
5821 * There is no help window yet.
5822 * Try to open the file specified by the "helpfile" option.
5823 */
5824 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5825 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005826 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 goto erret;
5828 }
5829 fclose(helpfd);
5830
5831#ifdef FEAT_WINDOWS
5832 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005833 * specified, the current window is vertically split and
5834 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 n = WSP_HELP;
5836# ifdef FEAT_VERTSPLIT
5837 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005838 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 n |= WSP_TOP;
5840# endif
5841 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005842 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#else
5844 /* use current window */
5845 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849#ifdef FEAT_WINDOWS
5850 if (curwin->w_height < p_hh)
5851 win_setheight((int)p_hh);
5852#endif
5853
5854 /*
5855 * Open help file (do_ecmd() will set b_help flag, readfile() will
5856 * set b_p_ro flag).
5857 * Set the alternate file to the previously edited file.
5858 */
5859 alt_fnum = curbuf->b_fnum;
5860 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005861 ECMD_HIDE + ECMD_SET_HELP,
5862#ifdef FEAT_WINDOWS
5863 NULL /* buffer is still open, don't store info */
5864#else
5865 curwin
5866#endif
5867 );
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005868 if (!cmdmod.keepalt)
5869 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 empty_fnum = curbuf->b_fnum;
5871 }
5872 }
5873
5874 if (!p_im)
5875 restart_edit = 0; /* don't want insert mode in help file */
5876
Bram Moolenaar8f535582011-09-30 17:30:31 +02005877#ifdef FEAT_FOLDING
5878 /* Restore KeyTyped, setting 'filetype=help' may reset it.
5879 * It is needed for do_tag top open folds under the cursor. */
5880 KeyTyped = old_KeyTyped;
5881#endif
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 if (tag != NULL)
5884 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5885
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005886 /* Delete the empty buffer if we're not using it. Careful: autocommands
5887 * may have jumped to another window, check that the buffer is not in a
5888 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5890 {
5891 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005892 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 wipe_buffer(buf, TRUE);
5894 }
5895
5896 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005897 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 curwin->w_alt_fnum = alt_fnum;
5899
5900erret:
5901 vim_free(tag);
5902}
5903
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005904/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005905 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005906 */
5907 void
5908ex_helpclose(eap)
5909 exarg_T *eap UNUSED;
5910{
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005911#if defined(FEAT_WINDOWS)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005912 win_T *win;
5913
5914 FOR_ALL_WINDOWS(win)
5915 {
5916 if (win->w_buffer->b_help)
5917 {
5918 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005919 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005920 }
5921 }
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005922#endif
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005923}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005925#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5926/*
5927 * In an argument search for a language specifiers in the form "@xx".
5928 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5929 * Returns NULL if not found.
5930 */
5931 char_u *
5932check_help_lang(arg)
5933 char_u *arg;
5934{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005935 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005936
5937 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
5938 && ASCII_ISALPHA(arg[len - 1]))
5939 {
5940 arg[len - 3] = NUL; /* remove the '@' */
5941 return arg + len - 2;
5942 }
5943 return NULL;
5944}
5945#endif
5946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947/*
5948 * Return a heuristic indicating how well the given string matches. The
5949 * smaller the number, the better the match. This is the order of priorities,
5950 * from best match to worst match:
5951 * - Match with least alpha-numeric characters is better.
5952 * - Match with least total characters is better.
5953 * - Match towards the start is better.
5954 * - Match starting with "+" is worse (feature instead of command)
5955 * Assumption is made that the matched_string passed has already been found to
5956 * match some string for which help is requested. webb.
5957 */
5958 int
5959help_heuristic(matched_string, offset, wrong_case)
5960 char_u *matched_string;
5961 int offset; /* offset for match */
5962 int wrong_case; /* no matching case */
5963{
5964 int num_letters;
5965 char_u *p;
5966
5967 num_letters = 0;
5968 for (p = matched_string; *p; p++)
5969 if (ASCII_ISALNUM(*p))
5970 num_letters++;
5971
5972 /*
5973 * Multiply the number of letters by 100 to give it a much bigger
5974 * weighting than the number of characters.
5975 * If there only is a match while ignoring case, add 5000.
5976 * If the match starts in the middle of a word, add 10000 to put it
5977 * somewhere in the last half.
5978 * If the match is more than 2 chars from the start, multiply by 200 to
5979 * put it after matches at the start.
5980 */
5981 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
5982 && ASCII_ISALNUM(matched_string[offset - 1]))
5983 offset += 10000;
5984 else if (offset > 2)
5985 offset *= 200;
5986 if (wrong_case)
5987 offset += 5000;
5988 /* Features are less interesting than the subjects themselves, but "+"
5989 * alone is not a feature. */
5990 if (matched_string[0] == '+' && matched_string[1] != NUL)
5991 offset += 100;
5992 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
5993}
5994
5995/*
5996 * Compare functions for qsort() below, that checks the help heuristics number
5997 * that has been put after the tagname by find_tags().
5998 */
5999 static int
6000#ifdef __BORLANDC__
6001_RTLENTRYF
6002#endif
6003help_compare(s1, s2)
6004 const void *s1;
6005 const void *s2;
6006{
6007 char *p1;
6008 char *p2;
6009
6010 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6011 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6012 return strcmp(p1, p2);
6013}
6014
6015/*
6016 * Find all help tags matching "arg", sort them and return in matches[], with
6017 * the number of matches in num_matches.
6018 * The matches will be sorted with a "best" match algorithm.
6019 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6020 */
6021 int
6022find_help_tags(arg, num_matches, matches, keep_lang)
6023 char_u *arg;
6024 int *num_matches;
6025 char_u ***matches;
6026 int keep_lang;
6027{
6028 char_u *s, *d;
6029 int i;
6030 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006031 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006032 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006033 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006034 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 "[count]", "[quotex]", "[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006036 "[pattern]", "\\|", "\\%$",
6037 "s/\\~", "s/\\U", "s/\\L",
6038 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006040 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006041 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006042 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006043 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 "\\[count]", "\\[quotex]", "\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006045 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006046 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006047 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 int flags;
6049
6050 d = IObuff; /* assume IObuff is long enough! */
6051
6052 /*
6053 * Recognize a few exceptions to the rule. Some strings that contain '*'
6054 * with "star". Otherwise '*' is recognized as a wildcard.
6055 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006056 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (STRCMP(arg, mtable[i]) == 0)
6058 {
6059 STRCPY(d, rtable[i]);
6060 break;
6061 }
6062
6063 if (i < 0) /* no match in table */
6064 {
6065 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6066 * Also replace "\%^" and "\%(", they match every tag too.
6067 * Also "\zs", "\z1", etc.
6068 * Also "\@<", "\@=", "\@<=", etc.
6069 * And also "\_$" and "\_^". */
6070 if (arg[0] == '\\'
6071 && ((arg[1] != NUL && arg[2] == NUL)
6072 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6073 && arg[2] != NUL)))
6074 {
6075 STRCPY(d, "/\\\\");
6076 STRCPY(d + 3, arg + 1);
6077 /* Check for "/\\_$", should be "/\\_\$" */
6078 if (d[3] == '_' && d[4] == '$')
6079 STRCPY(d + 4, "\\$");
6080 }
6081 else
6082 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006083 /* Replace:
6084 * "[:...:]" with "\[:...:]"
6085 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006086 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006087 */
6088 if ((arg[0] == '[' && (arg[1] == ':'
6089 || (arg[1] == '+' && arg[2] == '+')))
6090 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 *d++ = '\\';
6092
6093 for (s = arg; *s; ++s)
6094 {
6095 /*
6096 * Replace "|" with "bar" and '"' with "quote" to match the name of
6097 * the tags for these commands.
6098 * Replace "*" with ".*" and "?" with "." to match command line
6099 * completion.
6100 * Insert a backslash before '~', '$' and '.' to avoid their
6101 * special meaning.
6102 */
6103 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6104 break;
6105 switch (*s)
6106 {
6107 case '|': STRCPY(d, "bar");
6108 d += 3;
6109 continue;
6110 case '"': STRCPY(d, "quote");
6111 d += 5;
6112 continue;
6113 case '*': *d++ = '.';
6114 break;
6115 case '?': *d++ = '.';
6116 continue;
6117 case '$':
6118 case '.':
6119 case '~': *d++ = '\\';
6120 break;
6121 }
6122
6123 /*
6124 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6125 * ":help i_^_CTRL-D" work.
6126 * Insert '-' before and after "CTRL-X" when applicable.
6127 */
6128 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6129 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6130 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006131 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6132 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 STRCPY(d, "CTRL-");
6134 d += 5;
6135 if (*s < ' ')
6136 {
6137#ifdef EBCDIC
6138 *d++ = CtrlChar(*s);
6139#else
6140 *d++ = *s + '@';
6141#endif
6142 if (d[-1] == '\\')
6143 *d++ = '\\'; /* double a backslash */
6144 }
6145 else
6146 *d++ = *++s;
6147 if (s[1] != NUL && s[1] != '_')
6148 *d++ = '_'; /* append a '_' */
6149 continue;
6150 }
6151 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6152 *d++ = '\\';
6153
6154 /*
6155 * Insert a backslash before a backslash after a slash, for search
6156 * pattern tags: "/\|" --> "/\\|".
6157 */
6158 else if (s[0] == '\\' && s[1] != '\\'
6159 && *arg == '/' && s == arg + 1)
6160 *d++ = '\\';
6161
6162 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6163 * "CTRL-\_CTRL-N" */
6164 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6165 {
6166 STRCPY(d, "CTRL-\\\\");
6167 d += 7;
6168 s += 6;
6169 }
6170
6171 *d++ = *s;
6172
6173 /*
6174 * If tag starts with ', toss everything after a second '. Fixes
6175 * CTRL-] on 'option'. (would include the trailing '.').
6176 */
6177 if (*s == '\'' && s > arg && *arg == '\'')
6178 break;
6179 }
6180 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006181
6182 if (*IObuff == '`')
6183 {
6184 if (d > IObuff + 2 && d[-1] == '`')
6185 {
6186 /* remove the backticks from `command` */
6187 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6188 d[-2] = NUL;
6189 }
6190 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6191 {
6192 /* remove the backticks and comma from `command`, */
6193 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6194 d[-3] = NUL;
6195 }
6196 else if (d > IObuff + 4 && d[-3] == '`'
6197 && d[-2] == '\\' && d[-1] == '.')
6198 {
6199 /* remove the backticks and dot from `command`\. */
6200 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6201 d[-4] = NUL;
6202 }
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 }
6205 }
6206
6207 *matches = (char_u **)"";
6208 *num_matches = 0;
6209 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6210 if (keep_lang)
6211 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006212 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 /* Sort the matches found on the heuristic number that is after the
6216 * tag name. */
6217 qsort((void *)*matches, (size_t)*num_matches,
6218 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006219 /* Delete more than TAG_MANY to reduce the size of the listing. */
6220 while (*num_matches > TAG_MANY)
6221 vim_free((*matches)[--*num_matches]);
6222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return OK;
6224}
6225
6226/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006227 * Called when starting to edit a buffer for a help file.
6228 */
6229 static void
6230prepare_help_buffer()
6231{
6232 char_u *p;
6233
6234 curbuf->b_help = TRUE;
6235#ifdef FEAT_QUICKFIX
6236 set_string_option_direct((char_u *)"buftype", -1,
6237 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6238#endif
6239
6240 /*
6241 * Always set these options after jumping to a help tag, because the
6242 * user may have an autocommand that gets in the way.
6243 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6244 * latin1 word characters (for translated help files).
6245 * Only set it when needed, buf_init_chartab() is some work.
6246 */
6247 p =
6248#ifdef EBCDIC
6249 (char_u *)"65-255,^*,^|,^\"";
6250#else
6251 (char_u *)"!-~,^*,^|,^\",192-255";
6252#endif
6253 if (STRCMP(curbuf->b_p_isk, p) != 0)
6254 {
6255 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6256 check_buf_options(curbuf);
6257 (void)buf_init_chartab(curbuf, FALSE);
6258 }
6259
Bram Moolenaar0b105412014-11-30 13:34:23 +01006260#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006261 /* Don't use the global foldmethod.*/
6262 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6263 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006264#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006265
6266 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6267 curwin->w_p_list = FALSE; /* no list mode */
6268
6269 curbuf->b_p_ma = FALSE; /* not modifiable */
6270 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6271 curwin->w_p_nu = 0; /* no line numbers */
6272 curwin->w_p_rnu = 0; /* no relative line numbers */
6273 RESET_BINDING(curwin); /* no scroll or cursor binding */
6274#ifdef FEAT_ARABIC
6275 curwin->w_p_arab = FALSE; /* no arabic mode */
6276#endif
6277#ifdef FEAT_RIGHTLEFT
6278 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6279#endif
6280#ifdef FEAT_FOLDING
6281 curwin->w_p_fen = FALSE; /* No folding in the help window */
6282#endif
6283#ifdef FEAT_DIFF
6284 curwin->w_p_diff = FALSE; /* No 'diff' */
6285#endif
6286#ifdef FEAT_SPELL
6287 curwin->w_p_spell = FALSE; /* No spell checking */
6288#endif
6289
6290 set_buflisted(FALSE);
6291}
6292
6293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 * After reading a help file: May cleanup a help buffer when syntax
6295 * highlighting is not used.
6296 */
6297 void
6298fix_help_buffer()
6299{
6300 linenr_T lnum;
6301 char_u *line;
6302 int in_example = FALSE;
6303 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006304 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 char_u *p;
6306 char_u *rt;
6307 int mustfree;
6308
6309 /* set filetype to "help". */
6310 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6311
6312#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006313 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314#endif
6315 {
6316 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6317 {
6318 line = ml_get_buf(curbuf, lnum, FALSE);
6319 len = (int)STRLEN(line);
6320 if (in_example && len > 0 && !vim_iswhite(line[0]))
6321 {
6322 /* End of example: non-white or '<' in first column. */
6323 if (line[0] == '<')
6324 {
6325 /* blank-out a '<' in the first column */
6326 line = ml_get_buf(curbuf, lnum, TRUE);
6327 line[0] = ' ';
6328 }
6329 in_example = FALSE;
6330 }
6331 if (!in_example && len > 0)
6332 {
6333 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6334 {
6335 /* blank-out a '>' in the last column (start of example) */
6336 line = ml_get_buf(curbuf, lnum, TRUE);
6337 line[len - 1] = ' ';
6338 in_example = TRUE;
6339 }
6340 else if (line[len - 1] == '~')
6341 {
6342 /* blank-out a '~' at the end of line (header marker) */
6343 line = ml_get_buf(curbuf, lnum, TRUE);
6344 line[len - 1] = ' ';
6345 }
6346 }
6347 }
6348 }
6349
6350 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006351 * In the "help.txt" and "help.abx" file, add the locally added help
6352 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006354 fname = gettail(curbuf->b_fname);
6355 if (fnamecmp(fname, "help.txt") == 0
6356#ifdef FEAT_MULTI_LANG
6357 || (fnamencmp(fname, "help.", 5) == 0
6358 && ASCII_ISALPHA(fname[5])
6359 && ASCII_ISALPHA(fname[6])
6360 && TOLOWER_ASC(fname[7]) == 'x'
6361 && fname[8] == NUL)
6362#endif
6363 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
6365 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6366 {
6367 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006368 if (strstr((char *)line, "*local-additions*") == NULL)
6369 continue;
6370
6371 /* Go through all directories in 'runtimepath', skipping
6372 * $VIMRUNTIME. */
6373 p = p_rtp;
6374 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006376 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6377 mustfree = FALSE;
6378 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
6379 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006381 int fcount;
6382 char_u **fnames;
6383 FILE *fd;
6384 char_u *s;
6385 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006387 vimconv_T vc;
6388 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389#endif
6390
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006391 /* Find all "doc/ *.txt" files in this directory. */
6392 add_pathsep(NameBuff);
6393#ifdef FEAT_MULTI_LANG
6394 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006396 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006398 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6399 &fnames, EW_FILE|EW_SILENT) == OK
6400 && fcount > 0)
6401 {
6402#ifdef FEAT_MULTI_LANG
6403 int i1;
6404 int i2;
6405 char_u *f1;
6406 char_u *f2;
6407 char_u *t1;
6408 char_u *e1;
6409 char_u *e2;
6410
6411 /* If foo.abx is found use it instead of foo.txt in
6412 * the same directory. */
6413 for (i1 = 0; i1 < fcount; ++i1)
6414 {
6415 for (i2 = 0; i2 < fcount; ++i2)
6416 {
6417 if (i1 == i2)
6418 continue;
6419 if (fnames[i1] == NULL || fnames[i2] == NULL)
6420 continue;
6421 f1 = fnames[i1];
6422 f2 = fnames[i2];
6423 t1 = gettail(f1);
6424 if (fnamencmp(f1, f2, t1 - f1) != 0)
6425 continue;
6426 e1 = vim_strrchr(t1, '.');
6427 e2 = vim_strrchr(gettail(f2), '.');
6428 if (e1 == NUL || e2 == NUL)
6429 continue;
6430 if (fnamecmp(e1, ".txt") != 0
6431 && fnamecmp(e1, fname + 4) != 0)
6432 {
6433 /* Not .txt and not .abx, remove it. */
6434 vim_free(fnames[i1]);
6435 fnames[i1] = NULL;
6436 continue;
6437 }
6438 if (fnamencmp(f1, f2, e1 - f1) != 0)
6439 continue;
6440 if (fnamecmp(e1, ".txt") == 0
6441 && fnamecmp(e2, fname + 4) == 0)
6442 {
6443 /* use .abx instead of .txt */
6444 vim_free(fnames[i1]);
6445 fnames[i1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 }
6447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006449#endif
6450 for (fi = 0; fi < fcount; ++fi)
6451 {
6452 if (fnames[fi] == NULL)
6453 continue;
6454 fd = mch_fopen((char *)fnames[fi], "r");
6455 if (fd != NULL)
6456 {
6457 vim_fgets(IObuff, IOSIZE, fd);
6458 if (IObuff[0] == '*'
6459 && (s = vim_strchr(IObuff + 1, '*'))
6460 != NULL)
6461 {
6462#ifdef FEAT_MBYTE
6463 int this_utf = MAYBE;
6464#endif
6465 /* Change tag definition to a
6466 * reference and remove <CR>/<NL>. */
6467 IObuff[0] = '|';
6468 *s = '|';
6469 while (*s != NUL)
6470 {
6471 if (*s == '\r' || *s == '\n')
6472 *s = NUL;
6473#ifdef FEAT_MBYTE
6474 /* The text is utf-8 when a byte
6475 * above 127 is found and no
6476 * illegal byte sequence is found.
6477 */
6478 if (*s >= 0x80 && this_utf != FALSE)
6479 {
6480 int l;
6481
6482 this_utf = TRUE;
6483 l = utf_ptr2len(s);
6484 if (l == 1)
6485 this_utf = FALSE;
6486 s += l - 1;
6487 }
6488#endif
6489 ++s;
6490 }
6491#ifdef FEAT_MBYTE
6492 /* The help file is latin1 or utf-8;
6493 * conversion to the current
6494 * 'encoding' may be required. */
6495 vc.vc_type = CONV_NONE;
6496 convert_setup(&vc, (char_u *)(
6497 this_utf == TRUE ? "utf-8"
6498 : "latin1"), p_enc);
6499 if (vc.vc_type == CONV_NONE)
6500 /* No conversion needed. */
6501 cp = IObuff;
6502 else
6503 {
6504 /* Do the conversion. If it fails
6505 * use the unconverted text. */
6506 cp = string_convert(&vc, IObuff,
6507 NULL);
6508 if (cp == NULL)
6509 cp = IObuff;
6510 }
6511 convert_setup(&vc, NULL, NULL);
6512
6513 ml_append(lnum, cp, (colnr_T)0, FALSE);
6514 if (cp != IObuff)
6515 vim_free(cp);
6516#else
6517 ml_append(lnum, IObuff, (colnr_T)0,
6518 FALSE);
6519#endif
6520 ++lnum;
6521 }
6522 fclose(fd);
6523 }
6524 }
6525 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006528 if (mustfree)
6529 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
6533 }
6534}
6535
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006536/*
6537 * ":exusage"
6538 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006539 void
6540ex_exusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006541 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006542{
6543 do_cmdline_cmd((char_u *)"help ex-cmd-index");
6544}
6545
6546/*
6547 * ":viusage"
6548 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006549 void
6550ex_viusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006551 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006552{
6553 do_cmdline_cmd((char_u *)"help normal-index");
6554}
6555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556#if defined(FEAT_EX_EXTRA) || defined(PROTO)
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006557static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang, int add_help_tags));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559/*
6560 * ":helptags"
6561 */
6562 void
6563ex_helptags(eap)
6564 exarg_T *eap;
6565{
6566 garray_T ga;
6567 int i, j;
6568 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006569#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006571#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006572 expand_T xpc;
6573 char_u *dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 char_u ext[5];
6575 char_u fname[8];
6576 int filecount;
6577 char_u **files;
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006578 int add_help_tags = FALSE;
6579
6580 /* Check for ":helptags ++t {dir}". */
6581 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3]))
6582 {
6583 add_help_tags = TRUE;
6584 eap->arg = skipwhite(eap->arg + 3);
6585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
Bram Moolenaar0e253142008-01-13 12:31:34 +00006587 ExpandInit(&xpc);
6588 xpc.xp_context = EXPAND_DIRECTORIES;
6589 dirname = ExpandOne(&xpc, eap->arg, NULL,
6590 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
6591 if (dirname == NULL || !mch_isdir(dirname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
6593 EMSG2(_("E150: Not a directory: %s"), eap->arg);
Bram Moolenaar1c2836e2015-11-10 21:05:48 +01006594 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 return;
6596 }
6597
6598#ifdef FEAT_MULTI_LANG
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006599 /* Get a list of all files in the help directory and in subdirectories. */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006600 STRCPY(NameBuff, dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 add_pathsep(NameBuff);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006602 STRCAT(NameBuff, "**");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6604 EW_FILE|EW_SILENT) == FAIL
6605 || filecount == 0)
6606 {
6607 EMSG2("E151: No match: %s", NameBuff);
Bram Moolenaar0e253142008-01-13 12:31:34 +00006608 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 return;
6610 }
6611
6612 /* Go over all files in the directory to find out what languages are
6613 * present. */
6614 ga_init2(&ga, 1, 10);
6615 for (i = 0; i < filecount; ++i)
6616 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006617 len = (int)STRLEN(files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 if (len > 4)
6619 {
6620 if (STRICMP(files[i] + len - 4, ".txt") == 0)
6621 {
6622 /* ".txt" -> language "en" */
6623 lang[0] = 'e';
6624 lang[1] = 'n';
6625 }
6626 else if (files[i][len - 4] == '.'
6627 && ASCII_ISALPHA(files[i][len - 3])
6628 && ASCII_ISALPHA(files[i][len - 2])
6629 && TOLOWER_ASC(files[i][len - 1]) == 'x')
6630 {
6631 /* ".abx" -> language "ab" */
6632 lang[0] = TOLOWER_ASC(files[i][len - 3]);
6633 lang[1] = TOLOWER_ASC(files[i][len - 2]);
6634 }
6635 else
6636 continue;
6637
6638 /* Did we find this language already? */
6639 for (j = 0; j < ga.ga_len; j += 2)
6640 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
6641 break;
6642 if (j == ga.ga_len)
6643 {
6644 /* New language, add it. */
6645 if (ga_grow(&ga, 2) == FAIL)
6646 break;
6647 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
6648 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
6650 }
6651 }
6652
6653 /*
6654 * Loop over the found languages to generate a tags file for each one.
6655 */
6656 for (j = 0; j < ga.ga_len; j += 2)
6657 {
6658 STRCPY(fname, "tags-xx");
6659 fname[5] = ((char_u *)ga.ga_data)[j];
6660 fname[6] = ((char_u *)ga.ga_data)[j + 1];
6661 if (fname[5] == 'e' && fname[6] == 'n')
6662 {
6663 /* English is an exception: use ".txt" and "tags". */
6664 fname[4] = NUL;
6665 STRCPY(ext, ".txt");
6666 }
6667 else
6668 {
6669 /* Language "ab" uses ".abx" and "tags-ab". */
6670 STRCPY(ext, ".xxx");
6671 ext[1] = fname[5];
6672 ext[2] = fname[6];
6673 }
Bram Moolenaar0e253142008-01-13 12:31:34 +00006674 helptags_one(dirname, ext, fname, add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 }
6676
6677 ga_clear(&ga);
6678 FreeWild(filecount, files);
6679
6680#else
6681 /* No language support, just use "*.txt" and "tags". */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006682 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006684 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685}
6686
6687 static void
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006688helptags_one(dir, ext, tagfname, add_help_tags)
6689 char_u *dir; /* doc directory */
6690 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006691 char_u *tagfname; /* "tags" for English, "tags-fr" for French. */
6692 int add_help_tags; /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 FILE *fd_tags;
6695 FILE *fd;
6696 garray_T ga;
6697 int filecount;
6698 char_u **files;
6699 char_u *p1, *p2;
6700 int fi;
6701 char_u *s;
6702 int i;
6703 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006704 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705# ifdef FEAT_MBYTE
6706 int utf8 = MAYBE;
6707 int this_utf8;
6708 int firstline;
6709 int mix = FALSE; /* detected mixed encodings */
6710# endif
6711
6712 /*
6713 * Find all *.txt files.
6714 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01006715 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006717 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 STRCAT(NameBuff, ext);
6719 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6720 EW_FILE|EW_SILENT) == FAIL
6721 || filecount == 0)
6722 {
6723 if (!got_int)
6724 EMSG2("E151: No match: %s", NameBuff);
6725 return;
6726 }
6727
6728 /*
6729 * Open the tags file for writing.
6730 * Do this before scanning through all the files.
6731 */
6732 STRCPY(NameBuff, dir);
6733 add_pathsep(NameBuff);
6734 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006735 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (fd_tags == NULL)
6737 {
6738 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6739 FreeWild(filecount, files);
6740 return;
6741 }
6742
6743 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006744 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
6745 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 */
6747 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006748 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
6749 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 if (ga_grow(&ga, 1) == FAIL)
6752 got_int = TRUE;
6753 else
6754 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006755 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (s == NULL)
6757 got_int = TRUE;
6758 else
6759 {
6760 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6761 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6762 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
6764 }
6765 }
6766
6767 /*
6768 * Go over all the files and extract the tags.
6769 */
6770 for (fi = 0; fi < filecount && !got_int; ++fi)
6771 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006772 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (fd == NULL)
6774 {
6775 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6776 continue;
6777 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006778 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
6780# ifdef FEAT_MBYTE
6781 firstline = TRUE;
6782# endif
6783 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6784 {
6785# ifdef FEAT_MBYTE
6786 if (firstline)
6787 {
6788 /* Detect utf-8 file by a non-ASCII char in the first line. */
6789 this_utf8 = MAYBE;
6790 for (s = IObuff; *s != NUL; ++s)
6791 if (*s >= 0x80)
6792 {
6793 int l;
6794
6795 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006796 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (l == 1)
6798 {
6799 /* Illegal UTF-8 byte sequence. */
6800 this_utf8 = FALSE;
6801 break;
6802 }
6803 s += l - 1;
6804 }
6805 if (this_utf8 == MAYBE) /* only ASCII characters found */
6806 this_utf8 = FALSE;
6807 if (utf8 == MAYBE) /* first file */
6808 utf8 = this_utf8;
6809 else if (utf8 != this_utf8)
6810 {
6811 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6812 mix = !got_int;
6813 got_int = TRUE;
6814 }
6815 firstline = FALSE;
6816 }
6817# endif
6818 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6819 while (p1 != NULL)
6820 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02006821 /* Use vim_strbyte() instead of vim_strchr() so that when
6822 * 'encoding' is dbcs it still works, don't find '*' in the
6823 * second byte. */
6824 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6826 {
6827 for (s = p1 + 1; s < p2; ++s)
6828 if (*s == ' ' || *s == '\t' || *s == '|')
6829 break;
6830
6831 /*
6832 * Only accept a *tag* when it consists of valid
6833 * characters, there is white space before it and is
6834 * followed by a white character or end-of-line.
6835 */
6836 if (s == p2
6837 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6838 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6839 || s[1] == '\0'))
6840 {
6841 *p2 = '\0';
6842 ++p1;
6843 if (ga_grow(&ga, 1) == FAIL)
6844 {
6845 got_int = TRUE;
6846 break;
6847 }
6848 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6849 if (s == NULL)
6850 {
6851 got_int = TRUE;
6852 break;
6853 }
6854 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6855 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 sprintf((char *)s, "%s\t%s", p1, fname);
6857
6858 /* find next '*' */
6859 p2 = vim_strchr(p2 + 1, '*');
6860 }
6861 }
6862 p1 = p2;
6863 }
6864 line_breakcheck();
6865 }
6866
6867 fclose(fd);
6868 }
6869
6870 FreeWild(filecount, files);
6871
6872 if (!got_int)
6873 {
6874 /*
6875 * Sort the tags.
6876 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02006877 if (ga.ga_data != NULL)
6878 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880 /*
6881 * Check for duplicates.
6882 */
6883 for (i = 1; i < ga.ga_len; ++i)
6884 {
6885 p1 = ((char_u **)ga.ga_data)[i - 1];
6886 p2 = ((char_u **)ga.ga_data)[i];
6887 while (*p1 == *p2)
6888 {
6889 if (*p2 == '\t')
6890 {
6891 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006892 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6894 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6895 EMSG(NameBuff);
6896 *p2 = '\t';
6897 break;
6898 }
6899 ++p1;
6900 ++p2;
6901 }
6902 }
6903
6904# ifdef FEAT_MBYTE
6905 if (utf8 == TRUE)
6906 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6907# endif
6908
6909 /*
6910 * Write the tags into the file.
6911 */
6912 for (i = 0; i < ga.ga_len; ++i)
6913 {
6914 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00006915 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00006917 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 else
6919 {
6920 fprintf(fd_tags, "%s\t/*", s);
6921 for (p1 = s; *p1 != '\t'; ++p1)
6922 {
6923 /* insert backslash before '\\' and '/' */
6924 if (*p1 == '\\' || *p1 == '/')
6925 putc('\\', fd_tags);
6926 putc(*p1, fd_tags);
6927 }
6928 fprintf(fd_tags, "*\n");
6929 }
6930 }
6931 }
6932#ifdef FEAT_MBYTE
6933 if (mix)
6934 got_int = FALSE; /* continue with other languages */
6935#endif
6936
6937 for (i = 0; i < ga.ga_len; ++i)
6938 vim_free(((char_u **)ga.ga_data)[i]);
6939 ga_clear(&ga);
6940 fclose(fd_tags); /* there is no check for an error... */
6941}
6942#endif
6943
6944#if defined(FEAT_SIGNS) || defined(PROTO)
6945
6946/*
6947 * Struct to hold the sign properties.
6948 */
6949typedef struct sign sign_T;
6950
6951struct sign
6952{
6953 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02006954 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 char_u *sn_name; /* name of sign */
6956 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006957# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 char_u *sn_text; /* text used instead of pixmap */
6961 int sn_line_hl; /* highlight ID for line */
6962 int sn_text_hl; /* highlight ID for text */
6963};
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02006966static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaar985cb442009-05-14 19:51:46 +00006968static int sign_cmd_idx __ARGS((char_u *begin_cmd, char_u *end_cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969static void sign_list_defined __ARGS((sign_T *sp));
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00006970static void sign_undefine __ARGS((sign_T *sp, sign_T *sp_prev));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006972static char *cmds[] = {
6973 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006974# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006975 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006976# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006977 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006978# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006979 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006980# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006981 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006982# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006983 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006984# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006985 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006986# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006987};
6988
6989/*
6990 * Find index of a ":sign" subcmd from its name.
6991 * "*end_cmd" must be writable.
6992 */
6993 static int
6994sign_cmd_idx(begin_cmd, end_cmd)
Bram Moolenaar985cb442009-05-14 19:51:46 +00006995 char_u *begin_cmd; /* begin of sign subcmd */
6996 char_u *end_cmd; /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006997{
6998 int idx;
6999 char save = *end_cmd;
7000
7001 *end_cmd = NUL;
7002 for (idx = 0; ; ++idx)
7003 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7004 break;
7005 *end_cmd = save;
7006 return idx;
7007}
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009/*
7010 * ":sign" command
7011 */
7012 void
7013ex_sign(eap)
7014 exarg_T *eap;
7015{
7016 char_u *arg = eap->arg;
7017 char_u *p;
7018 int idx;
7019 sign_T *sp;
7020 sign_T *sp_prev;
7021 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
7023 /* Parse the subcommand. */
7024 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007025 idx = sign_cmd_idx(arg, p);
7026 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007028 EMSG2(_("E160: Unknown sign command: %s"), arg);
7029 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
7031 arg = skipwhite(p);
7032
7033 if (idx <= SIGNCMD_LIST)
7034 {
7035 /*
7036 * Define, undefine or list signs.
7037 */
7038 if (idx == SIGNCMD_LIST && *arg == NUL)
7039 {
7040 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007041 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 sign_list_defined(sp);
7043 }
7044 else if (*arg == NUL)
7045 EMSG(_("E156: Missing sign name"));
7046 else
7047 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007048 /* Isolate the sign name. If it's a number skip leading zeroes,
7049 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 p = skiptowhite(arg);
7051 if (*p != NUL)
7052 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007053 while (arg[0] == '0' && arg[1] != NUL)
7054 ++arg;
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 sp_prev = NULL;
7057 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7058 {
7059 if (STRCMP(sp->sn_name, arg) == 0)
7060 break;
7061 sp_prev = sp;
7062 }
7063 if (idx == SIGNCMD_DEFINE)
7064 {
7065 /* ":sign define {name} ...": define a sign */
7066 if (sp == NULL)
7067 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007068 sign_T *lp;
7069 int start = next_sign_typenr;
7070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 /* Allocate a new sign. */
7072 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7073 if (sp == NULL)
7074 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007076 /* Check that next_sign_typenr is not already being used.
7077 * This only happens after wrapping around. Hopefully
7078 * another one got deleted and we can use its number. */
7079 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007081 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007083 ++next_sign_typenr;
7084 if (next_sign_typenr == MAX_TYPENR)
7085 next_sign_typenr = 1;
7086 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007088 vim_free(sp);
7089 EMSG(_("E612: Too many signs defined"));
7090 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007092 lp = first_sign; /* start all over */
7093 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007095 lp = lp->sn_next;
7096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007098 sp->sn_typenr = next_sign_typenr;
7099 if (++next_sign_typenr == MAX_TYPENR)
7100 next_sign_typenr = 1; /* wrap around */
7101
7102 sp->sn_name = vim_strsave(arg);
7103 if (sp->sn_name == NULL) /* out of memory */
7104 {
7105 vim_free(sp);
7106 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007108
7109 /* add the new sign to the list of signs */
7110 if (sp_prev == NULL)
7111 first_sign = sp;
7112 else
7113 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
7115
7116 /* set values for a defined sign. */
7117 for (;;)
7118 {
7119 arg = skipwhite(p);
7120 if (*arg == NUL)
7121 break;
7122 p = skiptowhite_esc(arg);
7123 if (STRNCMP(arg, "icon=", 5) == 0)
7124 {
7125 arg += 5;
7126 vim_free(sp->sn_icon);
7127 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7128 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007129# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (gui.in_use)
7131 {
7132 out_flush();
7133 if (sp->sn_image != NULL)
7134 gui_mch_destroy_sign(sp->sn_image);
7135 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7136 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139 else if (STRNCMP(arg, "text=", 5) == 0)
7140 {
7141 char_u *s;
7142 int cells;
7143 int len;
7144
7145 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007146# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 /* Count cells and check for non-printable chars */
7148 if (has_mbyte)
7149 {
7150 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007151 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
7153 if (!vim_isprintc((*mb_ptr2char)(s)))
7154 break;
7155 cells += (*mb_ptr2cells)(s);
7156 }
7157 }
7158 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
7161 for (s = arg; s < p; ++s)
7162 if (!vim_isprintc(*s))
7163 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007164 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166 /* Currently must be one or two display cells */
7167 if (s != p || cells < 1 || cells > 2)
7168 {
7169 *p = NUL;
7170 EMSG2(_("E239: Invalid sign text: %s"), arg);
7171 return;
7172 }
7173
7174 vim_free(sp->sn_text);
7175 /* Allocate one byte more if we need to pad up
7176 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007177 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 sp->sn_text = vim_strnsave(arg, len);
7179
7180 if (sp->sn_text != NULL && cells == 1)
7181 STRCPY(sp->sn_text + len - 1, " ");
7182 }
7183 else if (STRNCMP(arg, "linehl=", 7) == 0)
7184 {
7185 arg += 7;
7186 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7187 }
7188 else if (STRNCMP(arg, "texthl=", 7) == 0)
7189 {
7190 arg += 7;
7191 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7192 }
7193 else
7194 {
7195 EMSG2(_(e_invarg2), arg);
7196 return;
7197 }
7198 }
7199 }
7200 else if (sp == NULL)
7201 EMSG2(_("E155: Unknown sign: %s"), arg);
7202 else if (idx == SIGNCMD_LIST)
7203 /* ":sign list {name}" */
7204 sign_list_defined(sp);
7205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007207 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
7209 }
7210 else
7211 {
7212 int id = -1;
7213 linenr_T lnum = -1;
7214 char_u *sign_name = NULL;
7215 char_u *arg1;
7216
7217 if (*arg == NUL)
7218 {
7219 if (idx == SIGNCMD_PLACE)
7220 {
7221 /* ":sign place": list placed signs in all buffers */
7222 sign_list_placed(NULL);
7223 }
7224 else if (idx == SIGNCMD_UNPLACE)
7225 {
7226 /* ":sign unplace": remove placed sign at cursor */
7227 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7228 if (id > 0)
7229 {
7230 buf_delsign(curwin->w_buffer, id);
7231 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7232 }
7233 else
7234 EMSG(_("E159: Missing sign number"));
7235 }
7236 else
7237 EMSG(_(e_argreq));
7238 return;
7239 }
7240
7241 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7242 {
7243 /* ":sign unplace *": remove all placed signs */
7244 buf_delete_all_signs();
7245 return;
7246 }
7247
7248 /* first arg could be placed sign id */
7249 arg1 = arg;
7250 if (VIM_ISDIGIT(*arg))
7251 {
7252 id = getdigits(&arg);
7253 if (!vim_iswhite(*arg) && *arg != NUL)
7254 {
7255 id = -1;
7256 arg = arg1;
7257 }
7258 else
7259 {
7260 arg = skipwhite(arg);
7261 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7262 {
7263 /* ":sign unplace {id}": remove placed sign by number */
7264 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7265 if ((lnum = buf_delsign(buf, id)) != 0)
7266 update_debug_sign(buf, lnum);
7267 return;
7268 }
7269 }
7270 }
7271
7272 /*
7273 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7274 * Leave "arg" pointing to {fname}.
7275 */
7276 for (;;)
7277 {
7278 if (STRNCMP(arg, "line=", 5) == 0)
7279 {
7280 arg += 5;
7281 lnum = atoi((char *)arg);
7282 arg = skiptowhite(arg);
7283 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007284 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7285 {
7286 if (id != -1)
7287 {
7288 EMSG(_(e_invarg));
7289 return;
7290 }
7291 id = -2;
7292 arg = skiptowhite(arg + 1);
7293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 else if (STRNCMP(arg, "name=", 5) == 0)
7295 {
7296 arg += 5;
7297 sign_name = arg;
7298 arg = skiptowhite(arg);
7299 if (*arg != NUL)
7300 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007301 while (sign_name[0] == '0' && sign_name[1] != NUL)
7302 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 }
7304 else if (STRNCMP(arg, "file=", 5) == 0)
7305 {
7306 arg += 5;
7307 buf = buflist_findname(arg);
7308 break;
7309 }
7310 else if (STRNCMP(arg, "buffer=", 7) == 0)
7311 {
7312 arg += 7;
7313 buf = buflist_findnr((int)getdigits(&arg));
7314 if (*skipwhite(arg) != NUL)
7315 EMSG(_(e_trailing));
7316 break;
7317 }
7318 else
7319 {
7320 EMSG(_(e_invarg));
7321 return;
7322 }
7323 arg = skipwhite(arg);
7324 }
7325
7326 if (buf == NULL)
7327 {
7328 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7329 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007330 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 {
7332 if (lnum >= 0 || sign_name != NULL)
7333 EMSG(_(e_invarg));
7334 else
7335 /* ":sign place file={fname}": list placed signs in one file */
7336 sign_list_placed(buf);
7337 }
7338 else if (idx == SIGNCMD_JUMP)
7339 {
7340 /* ":sign jump {id} file={fname}" */
7341 if (lnum >= 0 || sign_name != NULL)
7342 EMSG(_(e_invarg));
7343 else if ((lnum = buf_findsign(buf, id)) > 0)
7344 { /* goto a sign ... */
7345 if (buf_jump_open_win(buf) != NULL)
7346 { /* ... in a current window */
7347 curwin->w_cursor.lnum = lnum;
7348 check_cursor_lnum();
7349 beginline(BL_WHITE);
7350 }
7351 else
7352 { /* ... not currently in a window */
7353 char_u *cmd;
7354
7355 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7356 if (cmd == NULL)
7357 return;
7358 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7359 do_cmdline_cmd(cmd);
7360 vim_free(cmd);
7361 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007362# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 }
7366 else
7367 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7368 }
7369 else if (idx == SIGNCMD_UNPLACE)
7370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (lnum >= 0 || sign_name != NULL)
7372 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007373 else if (id == -2)
7374 {
7375 /* ":sign unplace * file={fname}" */
7376 redraw_buf_later(buf, NOT_VALID);
7377 buf_delete_signs(buf);
7378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 else
7380 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007381 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 lnum = buf_delsign(buf, id);
7383 update_debug_sign(buf, lnum);
7384 }
7385 }
7386 /* idx == SIGNCMD_PLACE */
7387 else if (sign_name != NULL)
7388 {
7389 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7390 if (STRCMP(sp->sn_name, sign_name) == 0)
7391 break;
7392 if (sp == NULL)
7393 {
7394 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7395 return;
7396 }
7397 if (lnum > 0)
7398 /* ":sign place {id} line={lnum} name={name} file={fname}":
7399 * place a sign */
7400 buf_addsign(buf, id, lnum, sp->sn_typenr);
7401 else
7402 /* ":sign place {id} file={fname}": change sign type */
7403 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007404 if (lnum > 0)
7405 update_debug_sign(buf, lnum);
7406 else
7407 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
7409 else
7410 EMSG(_(e_invarg));
7411 }
7412}
7413
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007414# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415/*
7416 * Allocate the icons. Called when the GUI has started. Allows defining
7417 * signs before it starts.
7418 */
7419 void
7420sign_gui_started()
7421{
7422 sign_T *sp;
7423
7424 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7425 if (sp->sn_icon != NULL)
7426 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7427}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
7430/*
7431 * List one sign.
7432 */
7433 static void
7434sign_list_defined(sp)
7435 sign_T *sp;
7436{
7437 char_u *p;
7438
Bram Moolenaar555b2802005-05-19 21:08:39 +00007439 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 if (sp->sn_icon != NULL)
7441 {
7442 MSG_PUTS(" icon=");
7443 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007444# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (sp->sn_image == NULL)
7446 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 if (sp->sn_text != NULL)
7452 {
7453 MSG_PUTS(" text=");
7454 msg_outtrans(sp->sn_text);
7455 }
7456 if (sp->sn_line_hl > 0)
7457 {
7458 MSG_PUTS(" linehl=");
7459 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
7460 if (p == NULL)
7461 MSG_PUTS("NONE");
7462 else
7463 msg_puts(p);
7464 }
7465 if (sp->sn_text_hl > 0)
7466 {
7467 MSG_PUTS(" texthl=");
7468 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
7469 if (p == NULL)
7470 MSG_PUTS("NONE");
7471 else
7472 msg_puts(p);
7473 }
7474}
7475
7476/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007477 * Undefine a sign and free its memory.
7478 */
7479 static void
7480sign_undefine(sp, sp_prev)
7481 sign_T *sp;
7482 sign_T *sp_prev;
7483{
7484 vim_free(sp->sn_name);
7485 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007486# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007487 if (sp->sn_image != NULL)
7488 {
7489 out_flush();
7490 gui_mch_destroy_sign(sp->sn_image);
7491 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007492# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007493 vim_free(sp->sn_text);
7494 if (sp_prev == NULL)
7495 first_sign = sp->sn_next;
7496 else
7497 sp_prev->sn_next = sp->sn_next;
7498 vim_free(sp);
7499}
7500
7501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 * Get highlighting attribute for sign "typenr".
7503 * If "line" is TRUE: line highl, if FALSE: text highl.
7504 */
7505 int
7506sign_get_attr(typenr, line)
7507 int typenr;
7508 int line;
7509{
7510 sign_T *sp;
7511
7512 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7513 if (sp->sn_typenr == typenr)
7514 {
7515 if (line)
7516 {
7517 if (sp->sn_line_hl > 0)
7518 return syn_id2attr(sp->sn_line_hl);
7519 }
7520 else
7521 {
7522 if (sp->sn_text_hl > 0)
7523 return syn_id2attr(sp->sn_text_hl);
7524 }
7525 break;
7526 }
7527 return 0;
7528}
7529
7530/*
7531 * Get text mark for sign "typenr".
7532 * Returns NULL if there isn't one.
7533 */
7534 char_u *
7535sign_get_text(typenr)
7536 int typenr;
7537{
7538 sign_T *sp;
7539
7540 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7541 if (sp->sn_typenr == typenr)
7542 return sp->sn_text;
7543 return NULL;
7544}
7545
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007546# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 void *
7548sign_get_image(typenr)
7549 int typenr; /* the attribute which may have a sign */
7550{
7551 sign_T *sp;
7552
7553 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7554 if (sp->sn_typenr == typenr)
7555 return sp->sn_image;
7556 return NULL;
7557}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
7560/*
7561 * Get the name of a sign by its typenr.
7562 */
7563 char_u *
7564sign_typenr2name(typenr)
7565 int typenr;
7566{
7567 sign_T *sp;
7568
7569 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7570 if (sp->sn_typenr == typenr)
7571 return sp->sn_name;
7572 return (char_u *)_("[Deleted]");
7573}
7574
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007575# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007576/*
7577 * Undefine/free all signs.
7578 */
7579 void
7580free_signs()
7581{
7582 while (first_sign != NULL)
7583 sign_undefine(first_sign, NULL);
7584}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007585# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007586
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007587# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007588static enum
7589{
7590 EXP_SUBCMD, /* expand :sign sub-commands */
7591 EXP_DEFINE, /* expand :sign define {name} args */
7592 EXP_PLACE, /* expand :sign place {id} args */
7593 EXP_UNPLACE, /* expand :sign unplace" */
7594 EXP_SIGN_NAMES /* expand with name of placed signs */
7595} expand_what;
7596
7597/*
7598 * Function given to ExpandGeneric() to obtain the sign command
7599 * expansion.
7600 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007601 char_u *
7602get_sign_name(xp, idx)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00007603 expand_T *xp UNUSED;
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007604 int idx;
7605{
7606 sign_T *sp;
7607 int current_idx;
7608
7609 switch (expand_what)
7610 {
7611 case EXP_SUBCMD:
7612 return (char_u *)cmds[idx];
7613 case EXP_DEFINE:
7614 {
7615 char *define_arg[] =
7616 {
7617 "icon=", "linehl=", "text=", "texthl=", NULL
7618 };
7619 return (char_u *)define_arg[idx];
7620 }
7621 case EXP_PLACE:
7622 {
7623 char *place_arg[] =
7624 {
7625 "line=", "name=", "file=", "buffer=", NULL
7626 };
7627 return (char_u *)place_arg[idx];
7628 }
7629 case EXP_UNPLACE:
7630 {
7631 char *unplace_arg[] = { "file=", "buffer=", NULL };
7632 return (char_u *)unplace_arg[idx];
7633 }
7634 case EXP_SIGN_NAMES:
7635 /* Complete with name of signs already defined */
7636 current_idx = 0;
7637 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7638 if (current_idx++ == idx)
7639 return sp->sn_name;
7640 return NULL;
7641 default:
7642 return NULL;
7643 }
7644}
7645
7646/*
7647 * Handle command line completion for :sign command.
7648 */
7649 void
7650set_context_in_sign_cmd(xp, arg)
7651 expand_T *xp;
7652 char_u *arg;
7653{
7654 char_u *p;
7655 char_u *end_subcmd;
7656 char_u *last;
7657 int cmd_idx;
7658 char_u *begin_subcmd_args;
7659
7660 /* Default: expand subcommands. */
7661 xp->xp_context = EXPAND_SIGN;
7662 expand_what = EXP_SUBCMD;
7663 xp->xp_pattern = arg;
7664
7665 end_subcmd = skiptowhite(arg);
7666 if (*end_subcmd == NUL)
7667 /* expand subcmd name
7668 * :sign {subcmd}<CTRL-D>*/
7669 return;
7670
7671 cmd_idx = sign_cmd_idx(arg, end_subcmd);
7672
7673 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007674 * |
7675 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007676 begin_subcmd_args = skipwhite(end_subcmd);
7677 p = skiptowhite(begin_subcmd_args);
7678 if (*p == NUL)
7679 {
7680 /*
7681 * Expand first argument of subcmd when possible.
7682 * For ":jump {id}" and ":unplace {id}", we could
7683 * possibly expand the ids of all signs already placed.
7684 */
7685 xp->xp_pattern = begin_subcmd_args;
7686 switch (cmd_idx)
7687 {
7688 case SIGNCMD_LIST:
7689 case SIGNCMD_UNDEFINE:
7690 /* :sign list <CTRL-D>
7691 * :sign undefine <CTRL-D> */
7692 expand_what = EXP_SIGN_NAMES;
7693 break;
7694 default:
7695 xp->xp_context = EXPAND_NOTHING;
7696 }
7697 return;
7698 }
7699
7700 /* expand last argument of subcmd */
7701
7702 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007703 * |
7704 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007705
7706 /* Loop until reaching last argument. */
7707 do
7708 {
7709 p = skipwhite(p);
7710 last = p;
7711 p = skiptowhite(p);
7712 } while (*p != NUL);
7713
7714 p = vim_strchr(last, '=');
7715
7716 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007717 * | |
7718 * last p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007719 if (p == NUL)
7720 {
7721 /* Expand last argument name (before equal sign). */
7722 xp->xp_pattern = last;
7723 switch (cmd_idx)
7724 {
7725 case SIGNCMD_DEFINE:
7726 expand_what = EXP_DEFINE;
7727 break;
7728 case SIGNCMD_PLACE:
7729 expand_what = EXP_PLACE;
7730 break;
7731 case SIGNCMD_JUMP:
7732 case SIGNCMD_UNPLACE:
7733 expand_what = EXP_UNPLACE;
7734 break;
7735 default:
7736 xp->xp_context = EXPAND_NOTHING;
7737 }
7738 }
7739 else
7740 {
7741 /* Expand last argument value (after equal sign). */
7742 xp->xp_pattern = p + 1;
7743 switch (cmd_idx)
7744 {
7745 case SIGNCMD_DEFINE:
7746 if (STRNCMP(last, "texthl", p - last) == 0 ||
7747 STRNCMP(last, "linehl", p - last) == 0)
7748 xp->xp_context = EXPAND_HIGHLIGHT;
7749 else if (STRNCMP(last, "icon", p - last) == 0)
7750 xp->xp_context = EXPAND_FILES;
7751 else
7752 xp->xp_context = EXPAND_NOTHING;
7753 break;
7754 case SIGNCMD_PLACE:
7755 if (STRNCMP(last, "name", p - last) == 0)
7756 expand_what = EXP_SIGN_NAMES;
7757 else
7758 xp->xp_context = EXPAND_NOTHING;
7759 break;
7760 default:
7761 xp->xp_context = EXPAND_NOTHING;
7762 }
7763 }
7764}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007765# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007766#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007767
7768/*
7769 * Make the user happy.
7770 */
7771 void
7772ex_smile(eap)
7773 exarg_T *eap UNUSED;
7774{
7775 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";
7776 char *p;
7777 int n;
7778
7779 msg_start();
7780 msg_putchar('\n');
7781 for (p = code; *p != NUL; ++p)
7782 if (*p == 'x')
7783 msg_putchar('\n');
7784 else
7785 for (n = *p++; n > 0; --n)
Bram Moolenaar2d820802015-12-31 20:46:39 +01007786 if (*p == 'o' || *p == '$')
7787 msg_putchar_attr(*p, hl_attr(HLF_L));
7788 else
7789 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007790 msg_clr_eos();
7791}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
7794/*
7795 * ":drop"
7796 * Opens the first argument in a window. When there are two or more arguments
7797 * the argument list is redefined.
7798 */
7799 void
7800ex_drop(eap)
7801 exarg_T *eap;
7802{
7803 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 win_T *wp;
7805 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007806# ifdef FEAT_WINDOWS
7807 tabpage_T *tp;
7808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810 /*
7811 * Check if the first argument is already being edited in a window. If
7812 * so, jump to that window.
7813 * We would actually need to check all arguments, but that's complicated
7814 * and mostly only one file is dropped.
7815 * This also ignores wildcards, since it is very unlikely the user is
7816 * editing a file name with a wildcard character.
7817 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007818 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00007820 /*
7821 * Expanding wildcards may result in an empty argument list. E.g. when
7822 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
7823 * already did an error message for this.
7824 */
7825 if (ARGCOUNT == 0)
7826 return;
7827
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007828# ifdef FEAT_WINDOWS
7829 if (cmdmod.tab)
7830 {
7831 /* ":tab drop file ...": open a tab for each argument that isn't
7832 * edited in a window yet. It's like ":tab all" but without closing
7833 * windows or tabs. */
7834 ex_all(eap);
7835 }
7836 else
7837# endif
7838 {
7839 /* ":drop file ...": Edit the first argument. Jump to an existing
7840 * window if possible, edit in current window if the current buffer
7841 * can be abandoned, otherwise open a new window. */
7842 buf = buflist_findnr(ARGLIST[0].ae_fnum);
7843
7844 FOR_ALL_TAB_WINDOWS(tp, wp)
7845 {
7846 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007848# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007849 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 return;
7853 }
7854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007856 /*
7857 * Check whether the current buffer is changed. If so, we will need
7858 * to split the current window or data could be lost.
7859 * Skip the check if the 'hidden' option is set, as in this case the
7860 * buffer won't be lost.
7861 */
7862 if (!P_HID(curbuf))
7863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007865 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866# endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007867 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007869 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007871 if (split)
7872 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007876 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7877 if (split)
7878 {
7879 eap->cmdidx = CMD_sfirst;
7880 eap->cmd[0] = 's';
7881 }
7882 else
7883 eap->cmdidx = CMD_first;
7884 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886}
7887#endif