blob: bb6daa8ca08aa6e83b5628d3db22edfc86545315 [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
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001710#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712static int no_viminfo __ARGS((void));
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001713static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714static int viminfo_errcnt;
1715
1716 static int
1717no_viminfo()
1718{
1719 /* "vim -i NONE" does not read or write a viminfo file */
1720 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1721}
1722
1723/*
1724 * Report an error for reading a viminfo file.
1725 * Count the number of errors. When there are more than 10, return TRUE.
1726 */
1727 int
1728viminfo_error(errnum, message, line)
1729 char *errnum;
1730 char *message;
1731 char_u *line;
1732{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001733 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1734 errnum, message);
Bram Moolenaar6c964832007-12-09 18:38:35 +00001735 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar758711c2005-02-02 23:11:38 +00001736 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1737 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 emsg(IObuff);
1739 if (++viminfo_errcnt >= 10)
1740 {
1741 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1742 return TRUE;
1743 }
1744 return FALSE;
1745}
1746
1747/*
1748 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
Bram Moolenaard812df62008-11-09 12:46:09 +00001749 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 */
1751 int
Bram Moolenaard812df62008-11-09 12:46:09 +00001752read_viminfo(file, flags)
1753 char_u *file; /* file name or NULL to use default name */
1754 int flags; /* VIF_WANT_INFO et al. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
1756 FILE *fp;
1757 char_u *fname;
1758
1759 if (no_viminfo())
1760 return FAIL;
1761
Bram Moolenaard812df62008-11-09 12:46:09 +00001762 fname = viminfo_filename(file); /* get file name in allocated buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (fname == NULL)
1764 return FAIL;
1765 fp = mch_fopen((char *)fname, READBIN);
1766
1767 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001768 {
1769 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001770 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1771 fname,
Bram Moolenaard812df62008-11-09 12:46:09 +00001772 (flags & VIF_WANT_INFO) ? _(" info") : "",
1773 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
1774 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
Bram Moolenaar555b2802005-05-19 21:08:39 +00001775 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001776 verbose_leave();
1777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 vim_free(fname);
1780 if (fp == NULL)
1781 return FAIL;
1782
1783 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00001784 do_viminfo(fp, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786 fclose(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 return OK;
1788}
1789
1790/*
Bram Moolenaarff1806f2013-06-15 16:31:47 +02001791 * Write the viminfo file. The old one is read in first so that effectively a
1792 * merge of current info and old info is done. This allows multiple vims to
1793 * run simultaneously, without losing any marks etc.
1794 * If "forceit" is TRUE, then the old file is not read in, and only internal
1795 * info is written to the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 void
1798write_viminfo(file, forceit)
1799 char_u *file;
1800 int forceit;
1801{
1802 char_u *fname;
1803 FILE *fp_in = NULL; /* input viminfo file, if any */
1804 FILE *fp_out = NULL; /* output viminfo file */
1805 char_u *tempname = NULL; /* name of temp viminfo file */
1806 struct stat st_new; /* mch_stat() of potential new file */
1807 char_u *wp;
1808#if defined(UNIX) || defined(VMS)
1809 mode_t umask_save;
1810#endif
1811#ifdef UNIX
1812 int shortname = FALSE; /* use 8.3 file name */
1813 struct stat st_old; /* mch_stat() of existing viminfo file */
1814#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001815#ifdef WIN3264
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001816 int hidden = FALSE;
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 if (no_viminfo())
1820 return;
1821
1822 fname = viminfo_filename(file); /* may set to default if NULL */
1823 if (fname == NULL)
1824 return;
1825
1826 fp_in = mch_fopen((char *)fname, READBIN);
1827 if (fp_in == NULL)
1828 {
1829 /* if it does exist, but we can't read it, don't try writing */
1830 if (mch_stat((char *)fname, &st_new) == 0)
1831 goto end;
1832#if defined(UNIX) || defined(VMS)
1833 /*
1834 * For Unix we create the .viminfo non-accessible for others,
1835 * because it may contain text from non-accessible documents.
1836 */
1837 umask_save = umask(077);
1838#endif
1839 fp_out = mch_fopen((char *)fname, WRITEBIN);
1840#if defined(UNIX) || defined(VMS)
1841 (void)umask(umask_save);
1842#endif
1843 }
1844 else
1845 {
1846 /*
1847 * There is an existing viminfo file. Create a temporary file to
1848 * write the new viminfo into, in the same directory as the
1849 * existing viminfo file, which will be renamed later.
1850 */
1851#ifdef UNIX
1852 /*
1853 * For Unix we check the owner of the file. It's not very nice to
1854 * overwrite a user's viminfo file after a "su root", with a
1855 * viminfo file that the user can't read.
1856 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001857 st_old.st_dev = (dev_t)0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00001858 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 st_old.st_mode = 0600;
Bram Moolenaar311d9822007-02-27 15:48:28 +00001860 if (mch_stat((char *)fname, &st_old) == 0
1861 && getuid() != ROOT_UID
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001862 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 ? (st_old.st_mode & 0200)
1864 : (st_old.st_gid == getgid()
1865 ? (st_old.st_mode & 0020)
1866 : (st_old.st_mode & 0002))))
1867 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001868 int tt = msg_didany;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870 /* avoid a wait_return for this message, it's annoying */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1872 msg_didany = tt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001873 fclose(fp_in);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 goto end;
1875 }
1876#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001877#ifdef WIN3264
1878 /* Get the file attributes of the existing viminfo file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01001879 hidden = mch_ishidden(fname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
1882 /*
1883 * Make tempname.
1884 * May try twice: Once normal and once with shortname set, just in
1885 * case somebody puts his viminfo file in an 8.3 filesystem.
1886 */
1887 for (;;)
1888 {
1889 tempname = buf_modname(
1890#ifdef UNIX
1891 shortname,
1892#else
1893# ifdef SHORT_FNAME
1894 TRUE,
1895# else
1896# ifdef FEAT_GUI_W32
1897 gui_is_win32s(),
1898# else
1899 FALSE,
1900# endif
1901# endif
1902#endif
1903 fname,
1904#ifdef VMS
1905 (char_u *)"-tmp",
1906#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 (char_u *)".tmp",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908#endif
1909 FALSE);
1910 if (tempname == NULL) /* out of memory */
1911 break;
1912
1913 /*
1914 * Check if tempfile already exists. Never overwrite an
1915 * existing file!
1916 */
1917 if (mch_stat((char *)tempname, &st_new) == 0)
1918 {
1919#ifdef UNIX
1920 /*
1921 * Check if tempfile is same as original file. May happen
1922 * when modname() gave the same file back. E.g. silly
1923 * link, or file name-length reached. Try again with
1924 * shortname set.
1925 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001926 if (!shortname && st_new.st_dev == st_old.st_dev
1927 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 vim_free(tempname);
1930 tempname = NULL;
1931 shortname = TRUE;
1932 continue;
1933 }
1934#endif
1935 /*
1936 * Try another name. Change one character, just before
1937 * the extension. This should also work for an 8.3
1938 * file name, when after adding the extension it still is
1939 * the same file as the original.
1940 */
1941 wp = tempname + STRLEN(tempname) - 5;
1942 if (wp < gettail(tempname)) /* empty file name? */
1943 wp = gettail(tempname);
1944 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1945 --*wp)
1946 {
1947 /*
1948 * They all exist? Must be something wrong! Don't
1949 * write the viminfo file then.
1950 */
1951 if (*wp == 'a')
1952 {
1953 vim_free(tempname);
1954 tempname = NULL;
1955 break;
1956 }
1957 }
1958 }
1959 break;
1960 }
1961
1962 if (tempname != NULL)
1963 {
Bram Moolenaar114216c2006-03-14 23:08:30 +00001964#ifdef VMS
1965 /* fdopen() fails for some reason */
Bram Moolenaarcf034472006-03-15 23:07:59 +00001966 umask_save = umask(077);
1967 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1968 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001969#else
Bram Moolenaara5792f52005-11-23 21:25:05 +00001970 int fd;
1971
1972 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001973 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001974 * Unix: same as original file, but strip s-bit. Reset umask to
1975 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001976 * Others: r&w for user only. */
Bram Moolenaar114216c2006-03-14 23:08:30 +00001977# ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001978 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00001979 fd = mch_open((char *)tempname,
1980 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001981 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001982 (void)umask(umask_save);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001983# else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001984 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001985 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001986# endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00001987 if (fd < 0)
1988 fp_out = NULL;
1989 else
1990 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar114216c2006-03-14 23:08:30 +00001991#endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
1993 /*
1994 * If we can't create in the same directory, try creating a
1995 * "normal" temp file.
1996 */
1997 if (fp_out == NULL)
1998 {
1999 vim_free(tempname);
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002000 if ((tempname = vim_tempname('o', TRUE)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 fp_out = mch_fopen((char *)tempname, WRITEBIN);
2002 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00002003
2004#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00002006 * Make sure the owner can read/write it. This only works for
2007 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 */
2009 if (fp_out != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002010 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011#endif
2012 }
2013 }
2014
2015 /*
2016 * Check if the new viminfo file can be written to.
2017 */
2018 if (fp_out == NULL)
2019 {
2020 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002021 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (fp_in != NULL)
2023 fclose(fp_in);
2024 goto end;
2025 }
2026
2027 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002028 {
2029 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002030 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002031 verbose_leave();
2032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034 viminfo_errcnt = 0;
Bram Moolenaard812df62008-11-09 12:46:09 +00002035 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 fclose(fp_out); /* errors are ignored !? */
2038 if (fp_in != NULL)
2039 {
2040 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002041
Bram Moolenaar3ed8b132014-07-09 21:18:04 +02002042 /* In case of an error keep the original viminfo file. Otherwise
2043 * rename the newly written file. Give an error if that fails. */
2044 if (viminfo_errcnt == 0 && vim_rename(tempname, fname) == -1)
2045 {
2046 ++viminfo_errcnt;
2047 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
2048 }
2049 if (viminfo_errcnt > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002051
2052#ifdef WIN3264
2053 /* If the viminfo file was hidden then also hide the new file. */
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002054 if (hidden)
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002055 mch_hide(fname);
2056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00002058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059end:
2060 vim_free(fname);
2061 vim_free(tempname);
2062}
2063
2064/*
2065 * Get the viminfo file name to use.
2066 * If "file" is given and not empty, use it (has already been expanded by
2067 * cmdline functions).
2068 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
2069 * expand environment variables.
2070 * Returns an allocated string. NULL when out of memory.
2071 */
2072 static char_u *
2073viminfo_filename(file)
2074 char_u *file;
2075{
2076 if (file == NULL || *file == NUL)
2077 {
2078 if (use_viminfo != NULL)
2079 file = use_viminfo;
2080 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
2081 {
2082#ifdef VIMINFO_FILE2
2083 /* don't use $HOME when not defined (turned into "c:/"!). */
2084# ifdef VMS
2085 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
2086# else
2087 if (mch_getenv((char_u *)"HOME") == NULL)
2088# endif
2089 {
2090 /* don't use $VIM when not available. */
2091 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
2092 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
2093 file = (char_u *)VIMINFO_FILE2;
2094 else
2095 file = (char_u *)VIMINFO_FILE;
2096 }
2097 else
2098#endif
2099 file = (char_u *)VIMINFO_FILE;
2100 }
2101 expand_env(file, NameBuff, MAXPATHL);
2102 file = NameBuff;
2103 }
2104 return vim_strsave(file);
2105}
2106
2107/*
2108 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2109 */
2110 static void
Bram Moolenaard812df62008-11-09 12:46:09 +00002111do_viminfo(fp_in, fp_out, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 FILE *fp_in;
2113 FILE *fp_out;
Bram Moolenaard812df62008-11-09 12:46:09 +00002114 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 int count = 0;
2117 int eof = FALSE;
2118 vir_T vir;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002119 int merge = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2122 return;
2123 vir.vir_fd = fp_in;
2124#ifdef FEAT_MBYTE
2125 vir.vir_conv.vc_type = CONV_NONE;
2126#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002127 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 if (fp_in != NULL)
2130 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002131 if (flags & VIF_WANT_INFO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002132 {
Bram Moolenaard812df62008-11-09 12:46:09 +00002133 eof = read_viminfo_up_to_marks(&vir,
2134 flags & VIF_FORCEIT, fp_out != NULL);
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002135 merge = TRUE;
2136 }
2137 else if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /* Skip info, find start of marks */
2139 while (!(eof = viminfo_readline(&vir))
2140 && vir.vir_line[0] != '>')
2141 ;
2142 }
2143 if (fp_out != NULL)
2144 {
2145 /* Write the info: */
2146 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2147 VIM_VERSION_MEDIUM);
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002148 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149#ifdef FEAT_MBYTE
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02002150 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2152#endif
2153 write_viminfo_search_pattern(fp_out);
2154 write_viminfo_sub_string(fp_out);
2155#ifdef FEAT_CMDHIST
Bram Moolenaarff1806f2013-06-15 16:31:47 +02002156 write_viminfo_history(fp_out, merge);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#endif
2158 write_viminfo_registers(fp_out);
2159#ifdef FEAT_EVAL
2160 write_viminfo_varlist(fp_out);
2161#endif
2162 write_viminfo_filemarks(fp_out);
2163 write_viminfo_bufferlist(fp_out);
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002164 write_viminfo_barlines(&vir, fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 count = write_viminfo_marks(fp_out);
2166 }
Bram Moolenaard812df62008-11-09 12:46:09 +00002167 if (fp_in != NULL
2168 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT)))
2169 copy_viminfo_marks(&vir, fp_out, count, eof, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 vim_free(vir.vir_line);
2172#ifdef FEAT_MBYTE
2173 if (vir.vir_conv.vc_type != CONV_NONE)
2174 convert_setup(&vir.vir_conv, NULL, NULL);
2175#endif
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002176 ga_clear_strings(&vir.vir_barlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177}
2178
2179/*
2180 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2181 * first part of the viminfo file which contains everything but the marks that
2182 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2183 */
2184 static int
2185read_viminfo_up_to_marks(virp, forceit, writing)
2186 vir_T *virp;
2187 int forceit;
2188 int writing;
2189{
2190 int eof;
2191 buf_T *buf;
2192
2193#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002194 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196 eof = viminfo_readline(virp);
2197 while (!eof && virp->vir_line[0] != '>')
2198 {
2199 switch (virp->vir_line[0])
2200 {
2201 /* Characters reserved for future expansion, ignored now */
2202 case '+': /* "+40 /path/dir file", for running vim without args */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 case '^': /* to be defined */
2204 case '<': /* long line - ignored */
2205 /* A comment or empty line. */
2206 case NUL:
2207 case '\r':
2208 case '\n':
2209 case '#':
2210 eof = viminfo_readline(virp);
2211 break;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002212 case '|': /* copy line (for future use) */
2213 if (writing)
2214 ga_add_string(&virp->vir_barlines, virp->vir_line);
2215 eof = viminfo_readline(virp);
2216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 case '*': /* "*encoding=value" */
2218 eof = viminfo_encoding(virp);
2219 break;
2220 case '!': /* global variable */
2221#ifdef FEAT_EVAL
2222 eof = read_viminfo_varlist(virp, writing);
2223#else
2224 eof = viminfo_readline(virp);
2225#endif
2226 break;
2227 case '%': /* entry for buffer list */
2228 eof = read_viminfo_bufferlist(virp, writing);
2229 break;
2230 case '"':
2231 eof = read_viminfo_register(virp, forceit);
2232 break;
2233 case '/': /* Search string */
2234 case '&': /* Substitute search string */
2235 case '~': /* Last search string, followed by '/' or '&' */
2236 eof = read_viminfo_search_pattern(virp, forceit);
2237 break;
2238 case '$':
2239 eof = read_viminfo_sub_string(virp, forceit);
2240 break;
2241 case ':':
2242 case '?':
2243 case '=':
2244 case '@':
2245#ifdef FEAT_CMDHIST
Bram Moolenaar07219f92013-04-14 23:19:36 +02002246 eof = read_viminfo_history(virp, writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247#else
2248 eof = viminfo_readline(virp);
2249#endif
2250 break;
2251 case '-':
2252 case '\'':
2253 eof = read_viminfo_filemark(virp, forceit);
2254 break;
2255 default:
2256 if (viminfo_error("E575: ", _("Illegal starting char"),
2257 virp->vir_line))
2258 eof = TRUE;
2259 else
2260 eof = viminfo_readline(virp);
2261 break;
2262 }
2263 }
2264
2265#ifdef FEAT_CMDHIST
2266 /* Finish reading history items. */
Bram Moolenaar07219f92013-04-14 23:19:36 +02002267 if (!writing)
2268 finish_viminfo_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#endif
2270
2271 /* Change file names to buffer numbers for fmarks. */
2272 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2273 fmarks_check_names(buf);
2274
2275 return eof;
2276}
2277
2278/*
2279 * Compare the 'encoding' value in the viminfo file with the current value of
2280 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2281 * conversion of text with iconv() in viminfo_readstring().
2282 */
2283 static int
2284viminfo_encoding(virp)
2285 vir_T *virp;
2286{
2287#ifdef FEAT_MBYTE
2288 char_u *p;
2289 int i;
2290
2291 if (get_viminfo_parameter('c') != 0)
2292 {
2293 p = vim_strchr(virp->vir_line, '=');
2294 if (p != NULL)
2295 {
2296 /* remove trailing newline */
2297 ++p;
2298 for (i = 0; vim_isprintc(p[i]); ++i)
2299 ;
2300 p[i] = NUL;
2301
2302 convert_setup(&virp->vir_conv, p, p_enc);
2303 }
2304 }
2305#endif
2306 return viminfo_readline(virp);
2307}
2308
2309/*
2310 * Read a line from the viminfo file.
2311 * Returns TRUE for end-of-file;
2312 */
2313 int
2314viminfo_readline(virp)
2315 vir_T *virp;
2316{
2317 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2318}
2319
2320/*
2321 * check string read from viminfo file
2322 * remove '\n' at the end of the line
2323 * - replace CTRL-V CTRL-V with CTRL-V
2324 * - replace CTRL-V 'n' with '\n'
2325 *
2326 * Check for a long line as written by viminfo_writestring().
2327 *
2328 * Return the string in allocated memory (NULL when out of memory).
2329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 char_u *
2331viminfo_readstring(virp, off, convert)
2332 vir_T *virp;
2333 int off; /* offset for virp->vir_line */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002334 int convert UNUSED; /* convert the string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 char_u *retval;
2337 char_u *s, *d;
2338 long len;
2339
2340 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2341 {
2342 len = atol((char *)virp->vir_line + off + 1);
2343 retval = lalloc(len, TRUE);
2344 if (retval == NULL)
2345 {
2346 /* Line too long? File messed up? Skip next line. */
2347 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2348 return NULL;
2349 }
2350 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2351 s = retval + 1; /* Skip the leading '<' */
2352 }
2353 else
2354 {
2355 retval = vim_strsave(virp->vir_line + off);
2356 if (retval == NULL)
2357 return NULL;
2358 s = retval;
2359 }
2360
2361 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2362 d = retval;
2363 while (*s != NUL && *s != '\n')
2364 {
2365 if (s[0] == Ctrl_V && s[1] != NUL)
2366 {
2367 if (s[1] == 'n')
2368 *d++ = '\n';
2369 else
2370 *d++ = Ctrl_V;
2371 s += 2;
2372 }
2373 else
2374 *d++ = *s++;
2375 }
2376 *d = NUL;
2377
2378#ifdef FEAT_MBYTE
2379 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2380 {
2381 d = string_convert(&virp->vir_conv, retval, NULL);
2382 if (d != NULL)
2383 {
2384 vim_free(retval);
2385 retval = d;
2386 }
2387 }
2388#endif
2389
2390 return retval;
2391}
2392
2393/*
2394 * write string to viminfo file
2395 * - replace CTRL-V with CTRL-V CTRL-V
2396 * - replace '\n' with CTRL-V 'n'
2397 * - add a '\n' at the end
2398 *
2399 * For a long line:
2400 * - write " CTRL-V <length> \n " in first line
2401 * - write " < <string> \n " in second line
2402 */
2403 void
2404viminfo_writestring(fd, p)
2405 FILE *fd;
2406 char_u *p;
2407{
2408 int c;
2409 char_u *s;
2410 int len = 0;
2411
2412 for (s = p; *s != NUL; ++s)
2413 {
2414 if (*s == Ctrl_V || *s == '\n')
2415 ++len;
2416 ++len;
2417 }
2418
2419 /* If the string will be too long, write its length and put it in the next
2420 * line. Take into account that some room is needed for what comes before
2421 * the string (e.g., variable name). Add something to the length for the
2422 * '<', NL and trailing NUL. */
2423 if (len > LSIZE / 2)
2424 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2425
2426 while ((c = *p++) != NUL)
2427 {
2428 if (c == Ctrl_V || c == '\n')
2429 {
2430 putc(Ctrl_V, fd);
2431 if (c == '\n')
2432 c = 'n';
2433 }
2434 putc(c, fd);
2435 }
2436 putc('\n', fd);
2437}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002438
2439 static void
2440write_viminfo_barlines(vir_T *virp, FILE *fp_out)
2441{
2442 int i;
2443 garray_T *gap = &virp->vir_barlines;
2444
2445 if (gap->ga_len > 0)
2446 {
2447 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
2448
2449 for (i = 0; i < gap->ga_len; ++i)
2450 fputs(((char **)(gap->ga_data))[i], fp_out);
2451 }
2452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#endif /* FEAT_VIMINFO */
2454
2455/*
2456 * Implementation of ":fixdel", also used by get_stty().
2457 * <BS> resulting <Del>
2458 * ^? ^H
2459 * not ^? ^?
2460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 void
2462do_fixdel(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002463 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 char_u *p;
2466
2467 p = find_termcode((char_u *)"kb");
2468 add_termcode((char_u *)"kD", p != NULL
2469 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2470}
2471
2472 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002473print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 linenr_T lnum;
2475 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002476 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002478 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 if (curwin->w_p_nu || use_number)
2481 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002482 vim_snprintf((char *)numbuf, sizeof(numbuf),
2483 "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2485 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002486 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487}
2488
2489/*
2490 * Print a text line. Also in silent mode ("ex -s").
2491 */
2492 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002493print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 linenr_T lnum;
2495 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002496 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 int save_silent = silent_mode;
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002501 silent_mode = FALSE;
2502 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2503 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (save_silent)
2505 {
2506 msg_putchar('\n');
2507 cursor_on(); /* msg_start() switches it off */
2508 out_flush();
2509 silent_mode = save_silent;
2510 }
Bram Moolenaar65b9a6a2009-02-04 12:14:51 +00002511 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512}
2513
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002514 int
2515rename_buffer(new_fname)
2516 char_u *new_fname;
2517{
2518 char_u *fname, *sfname, *xfname;
Bram Moolenaar7e283842013-05-30 11:43:15 +02002519 buf_T *buf;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002520
Bram Moolenaar7e283842013-05-30 11:43:15 +02002521#ifdef FEAT_AUTOCMD
2522 buf = curbuf;
2523 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002524 /* buffer changed, don't change name now */
2525 if (buf != curbuf)
2526 return FAIL;
2527# ifdef FEAT_EVAL
2528 if (aborting()) /* autocmds may abort script processing */
2529 return FAIL;
2530# endif
2531#endif
2532 /*
2533 * The name of the current buffer will be changed.
2534 * A new (unlisted) buffer entry needs to be made to hold the old file
2535 * name, which will become the alternate file name.
2536 * But don't set the alternate file name if the buffer didn't have a
2537 * name.
2538 */
Bram Moolenaar7e283842013-05-30 11:43:15 +02002539 fname = curbuf->b_ffname;
2540 sfname = curbuf->b_sfname;
2541 xfname = curbuf->b_fname;
2542 curbuf->b_ffname = NULL;
2543 curbuf->b_sfname = NULL;
2544 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002545 {
Bram Moolenaar7e283842013-05-30 11:43:15 +02002546 curbuf->b_ffname = fname;
2547 curbuf->b_sfname = sfname;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002548 return FAIL;
2549 }
Bram Moolenaar7e283842013-05-30 11:43:15 +02002550 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002551 if (xfname != NULL && *xfname != NUL)
2552 {
2553 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2554 if (buf != NULL && !cmdmod.keepalt)
2555 curwin->w_alt_fnum = buf->b_fnum;
2556 }
2557 vim_free(fname);
2558 vim_free(sfname);
2559#ifdef FEAT_AUTOCMD
Bram Moolenaar7e283842013-05-30 11:43:15 +02002560 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002561#endif
2562 /* Change directories when the 'acd' option is set. */
2563 DO_AUTOCHDIR
2564 return OK;
2565}
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567/*
2568 * ":file[!] [fname]".
2569 */
2570 void
2571ex_file(eap)
2572 exarg_T *eap;
2573{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002574 /* ":0file" removes the file name. Check for illegal uses ":3file",
2575 * "0file name", etc. */
2576 if (eap->addr_count > 0
2577 && (*eap->arg != NUL
2578 || eap->line2 > 0
2579 || eap->addr_count > 1))
2580 {
2581 EMSG(_(e_invarg));
2582 return;
2583 }
2584
2585 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002587 if (rename_buffer(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
2590 /* print full file name if :cd used */
2591 fileinfo(FALSE, FALSE, eap->forceit);
2592}
2593
2594/*
2595 * ":update".
2596 */
2597 void
2598ex_update(eap)
2599 exarg_T *eap;
2600{
2601 if (curbufIsChanged())
2602 (void)do_write(eap);
2603}
2604
2605/*
2606 * ":write" and ":saveas".
2607 */
2608 void
2609ex_write(eap)
2610 exarg_T *eap;
2611{
2612 if (eap->usefilter) /* input lines to shell command */
2613 do_bang(1, eap, FALSE, TRUE, FALSE);
2614 else
2615 (void)do_write(eap);
2616}
2617
2618/*
2619 * write current buffer to file 'eap->arg'
2620 * if 'eap->append' is TRUE, append to the file
2621 *
2622 * if *eap->arg == NUL write to current file
2623 *
2624 * return FAIL for failure, OK otherwise
2625 */
2626 int
2627do_write(eap)
2628 exarg_T *eap;
2629{
2630 int other;
2631 char_u *fname = NULL; /* init to shut up gcc */
2632 char_u *ffname;
2633 int retval = FAIL;
2634 char_u *free_fname = NULL;
2635#ifdef FEAT_BROWSE
2636 char_u *browse_file = NULL;
2637#endif
2638 buf_T *alt_buf = NULL;
2639
2640 if (not_writing()) /* check 'write' option */
2641 return FAIL;
2642
2643 ffname = eap->arg;
2644#ifdef FEAT_BROWSE
2645 if (cmdmod.browse)
2646 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002647 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 NULL, NULL, NULL, curbuf);
2649 if (browse_file == NULL)
2650 goto theend;
2651 ffname = browse_file;
2652 }
2653#endif
2654 if (*ffname == NUL)
2655 {
2656 if (eap->cmdidx == CMD_saveas)
2657 {
2658 EMSG(_(e_argreq));
2659 goto theend;
2660 }
2661 other = FALSE;
2662 }
2663 else
2664 {
2665 fname = ffname;
2666 free_fname = fix_fname(ffname);
2667 /*
2668 * When out-of-memory, keep unexpanded file name, because we MUST be
2669 * able to write the file in this situation.
2670 */
2671 if (free_fname != NULL)
2672 ffname = free_fname;
2673 other = otherfile(ffname);
2674 }
2675
2676 /*
2677 * If we have a new file, put its name in the list of alternate file names.
2678 */
2679 if (other)
2680 {
2681 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2682 || eap->cmdidx == CMD_saveas)
2683 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2684 else
2685 alt_buf = buflist_findname(ffname);
2686 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2687 {
2688 /* Overwriting a file that is loaded in another buffer is not a
2689 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002690 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 goto theend;
2692 }
2693 }
2694
2695 /*
2696 * Writing to the current file is not allowed in readonly mode
2697 * and a file name is required.
2698 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2699 */
2700 if (!other && (
2701#ifdef FEAT_QUICKFIX
2702 bt_dontwrite_msg(curbuf) ||
2703#endif
2704 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2705 goto theend;
2706
2707 if (!other)
2708 {
2709 ffname = curbuf->b_ffname;
2710 fname = curbuf->b_fname;
2711 /*
2712 * Not writing the whole file is only allowed with '!'.
2713 */
2714 if ( (eap->line1 != 1
2715 || eap->line2 != curbuf->b_ml.ml_line_count)
2716 && !eap->forceit
2717 && !eap->append
2718 && !p_wa)
2719 {
2720#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2721 if (p_confirm || cmdmod.confirm)
2722 {
2723 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2724 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2725 goto theend;
2726 eap->forceit = TRUE;
2727 }
2728 else
2729#endif
2730 {
2731 EMSG(_("E140: Use ! to write partial buffer"));
2732 goto theend;
2733 }
2734 }
2735 }
2736
2737 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2738 {
2739 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2740 {
2741#ifdef FEAT_AUTOCMD
2742 buf_T *was_curbuf = curbuf;
2743
2744 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002745 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746# ifdef FEAT_EVAL
2747 if (curbuf != was_curbuf || aborting())
2748# else
2749 if (curbuf != was_curbuf)
2750# endif
2751 {
2752 /* buffer changed, don't change name now */
2753 retval = FAIL;
2754 goto theend;
2755 }
2756#endif
2757 /* Exchange the file names for the current and the alternate
2758 * buffer. This makes it look like we are now editing the buffer
2759 * under the new name. Must be done before buf_write(), because
2760 * if there is no file name and 'cpo' contains 'F', it will set
2761 * the file name. */
2762 fname = alt_buf->b_fname;
2763 alt_buf->b_fname = curbuf->b_fname;
2764 curbuf->b_fname = fname;
2765 fname = alt_buf->b_ffname;
2766 alt_buf->b_ffname = curbuf->b_ffname;
2767 curbuf->b_ffname = fname;
2768 fname = alt_buf->b_sfname;
2769 alt_buf->b_sfname = curbuf->b_sfname;
2770 curbuf->b_sfname = fname;
2771 buf_name_changed(curbuf);
2772#ifdef FEAT_AUTOCMD
2773 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002774 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (!alt_buf->b_p_bl)
2776 {
2777 alt_buf->b_p_bl = TRUE;
2778 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2779 }
2780# ifdef FEAT_EVAL
2781 if (curbuf != was_curbuf || aborting())
2782# else
2783 if (curbuf != was_curbuf)
2784# endif
2785 {
2786 /* buffer changed, don't write the file */
2787 retval = FAIL;
2788 goto theend;
2789 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002790
2791 /* If 'filetype' was empty try detecting it now. */
2792 if (*curbuf->b_p_ft == NUL)
2793 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002794 if (au_has_group((char_u *)"filetypedetect"))
2795 (void)do_doautocmd((char_u *)"filetypedetect BufRead",
2796 TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002797 do_modelines(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002798 }
Bram Moolenaarf666f0e2010-11-24 17:59:32 +01002799
2800 /* Autocommands may have changed buffer names, esp. when
2801 * 'autochdir' is set. */
2802 fname = curbuf->b_sfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
2804 }
2805
2806 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2807 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002808
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002809 /* After ":saveas fname" reset 'readonly'. */
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002810 if (eap->cmdidx == CMD_saveas)
2811 {
2812 if (retval == OK)
Bram Moolenaar4352f132009-02-11 15:03:45 +00002813 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002814 curbuf->b_p_ro = FALSE;
Bram Moolenaar4352f132009-02-11 15:03:45 +00002815#ifdef FEAT_WINDOWS
2816 redraw_tabline = TRUE;
2817#endif
2818 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002819 /* Change directories when the 'acd' option is set. */
2820 DO_AUTOCHDIR
2821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823
2824theend:
2825#ifdef FEAT_BROWSE
2826 vim_free(browse_file);
2827#endif
2828 vim_free(free_fname);
2829 return retval;
2830}
2831
2832/*
2833 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2834 * BF_NEW or BF_READERR, check for overwriting current file.
2835 * May set eap->forceit if a dialog says it's OK to overwrite.
2836 * Return OK if it's OK, FAIL if it is not.
2837 */
Bram Moolenaar8218f602012-04-25 17:32:18 +02002838 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839check_overwrite(eap, buf, fname, ffname, other)
2840 exarg_T *eap;
2841 buf_T *buf;
2842 char_u *fname; /* file name to be used (can differ from
2843 buf->ffname) */
2844 char_u *ffname; /* full path version of fname */
2845 int other; /* writing under other name */
2846{
2847 /*
2848 * write to other file or b_flags set or not writing the whole file:
2849 * overwriting only allowed with '!'
2850 */
2851 if ( (other
2852 || (buf->b_flags & BF_NOTEDITED)
2853 || ((buf->b_flags & BF_NEW)
2854 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2855 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 && !p_wa
Bram Moolenaar11717bb2007-12-08 21:21:18 +00002857#ifdef FEAT_QUICKFIX
2858 && !bt_nofile(buf)
2859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 && vim_fexists(ffname))
2861 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002862 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002864#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002865 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002866 if (mch_isdir(ffname))
2867 {
2868 EMSG2(_(e_isadir2), ffname);
2869 return FAIL;
2870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871#endif
2872#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002873 if (p_confirm || cmdmod.confirm)
2874 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002875 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002877 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2878 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2879 return FAIL;
2880 eap->forceit = TRUE;
2881 }
2882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002884 {
2885 EMSG(_(e_exists));
2886 return FAIL;
2887 }
2888 }
2889
2890 /* For ":w! filename" check that no swap file exists for "filename". */
2891 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002893 char_u *dir;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002894 char_u *p;
2895 int r;
2896 char_u *swapname;
2897
2898 /* We only try the first entry in 'directory', without checking if
2899 * it's writable. If the "." directory is not writable the write
2900 * will probably fail anyway.
2901 * Use 'shortname' of the current buffer, since there is no buffer
2902 * for the written file. */
2903 if (*p_dir == NUL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002904 {
2905 dir = alloc(5);
2906 if (dir == NULL)
2907 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002908 STRCPY(dir, ".");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002909 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002910 else
2911 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002912 dir = alloc(MAXPATHL);
2913 if (dir == NULL)
2914 return FAIL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002915 p = p_dir;
2916 copy_option_part(&p, dir, MAXPATHL, ",");
2917 }
2918 swapname = makeswapname(fname, ffname, curbuf, dir);
Bram Moolenaard9462e32011-04-11 21:35:11 +02002919 vim_free(dir);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002920 r = vim_fexists(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002921 if (r)
2922 {
2923#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2924 if (p_confirm || cmdmod.confirm)
2925 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002926 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002927
2928 dialog_msg(buff,
2929 _("Swap file \"%s\" exists, overwrite anyway?"),
2930 swapname);
2931 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2932 != VIM_YES)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002933 {
2934 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002935 return FAIL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002936 }
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002937 eap->forceit = TRUE;
2938 }
2939 else
2940#endif
2941 {
2942 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2943 swapname);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002944 vim_free(swapname);
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002945 return FAIL;
2946 }
2947 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002948 vim_free(swapname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 }
2951 return OK;
2952}
2953
2954/*
2955 * Handle ":wnext", ":wNext" and ":wprevious" commands.
2956 */
2957 void
2958ex_wnext(eap)
2959 exarg_T *eap;
2960{
2961 int i;
2962
2963 if (eap->cmd[1] == 'n')
2964 i = curwin->w_arg_idx + (int)eap->line2;
2965 else
2966 i = curwin->w_arg_idx - (int)eap->line2;
2967 eap->line1 = 1;
2968 eap->line2 = curbuf->b_ml.ml_line_count;
2969 if (do_write(eap) != FAIL)
2970 do_argfile(eap, i);
2971}
2972
2973/*
2974 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
2975 */
2976 void
2977do_wqall(eap)
2978 exarg_T *eap;
2979{
2980 buf_T *buf;
2981 int error = 0;
2982 int save_forceit = eap->forceit;
2983
2984 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
2985 exiting = TRUE;
2986
2987 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2988 {
2989 if (bufIsChanged(buf))
2990 {
2991 /*
2992 * Check if there is a reason the buffer cannot be written:
2993 * 1. if the 'write' option is set
2994 * 2. if there is no file name (even after browsing)
2995 * 3. if the 'readonly' is set (even after a dialog)
2996 * 4. if overwriting is allowed (even after a dialog)
2997 */
2998 if (not_writing())
2999 {
3000 ++error;
3001 break;
3002 }
3003#ifdef FEAT_BROWSE
3004 /* ":browse wall": ask for file name if there isn't one */
3005 if (buf->b_ffname == NULL && cmdmod.browse)
3006 browse_save_fname(buf);
3007#endif
3008 if (buf->b_ffname == NULL)
3009 {
3010 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
3011 ++error;
3012 }
3013 else if (check_readonly(&eap->forceit, buf)
3014 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
3015 FALSE) == FAIL)
3016 {
3017 ++error;
3018 }
3019 else
3020 {
3021 if (buf_write_all(buf, eap->forceit) == FAIL)
3022 ++error;
3023#ifdef FEAT_AUTOCMD
3024 /* an autocommand may have deleted the buffer */
3025 if (!buf_valid(buf))
3026 buf = firstbuf;
3027#endif
3028 }
3029 eap->forceit = save_forceit; /* check_overwrite() may set it */
3030 }
3031 }
3032 if (exiting)
3033 {
3034 if (!error)
3035 getout(0); /* exit Vim */
3036 not_exiting();
3037 }
3038}
3039
3040/*
3041 * Check the 'write' option.
3042 * Return TRUE and give a message when it's not st.
3043 */
3044 int
3045not_writing()
3046{
3047 if (p_write)
3048 return FALSE;
3049 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
3050 return TRUE;
3051}
3052
3053/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003054 * Check if a buffer is read-only (either 'readonly' option is set or file is
3055 * read-only). Ask for overruling in a dialog. Return TRUE and give an error
3056 * message when the buffer is readonly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 */
3058 static int
3059check_readonly(forceit, buf)
3060 int *forceit;
3061 buf_T *buf;
3062{
Bram Moolenaar5386a122007-06-28 20:02:32 +00003063 struct stat st;
3064
3065 /* Handle a file being readonly when the 'readonly' option is set or when
3066 * the file exists and permissions are read-only.
3067 * We will send 0777 to check_file_readonly(), as the "perm" variable is
3068 * important for device checks but not here. */
3069 if (!*forceit && (buf->b_p_ro
3070 || (mch_stat((char *)buf->b_ffname, &st) >= 0
3071 && check_file_readonly(buf->b_ffname, 0777))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
3073#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3074 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
3075 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003076 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaar5386a122007-06-28 20:02:32 +00003078 if (buf->b_p_ro)
3079 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
3080 buf->b_fname);
3081 else
3082 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 +00003083 buf->b_fname);
3084
3085 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
3086 {
3087 /* Set forceit, to force the writing of a readonly file */
3088 *forceit = TRUE;
3089 return FALSE;
3090 }
3091 else
3092 return TRUE;
3093 }
3094 else
3095#endif
Bram Moolenaar5386a122007-06-28 20:02:32 +00003096 if (buf->b_p_ro)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 EMSG(_(e_readonly));
Bram Moolenaar5386a122007-06-28 20:02:32 +00003098 else
3099 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
3100 buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return TRUE;
3102 }
Bram Moolenaar5386a122007-06-28 20:02:32 +00003103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return FALSE;
3105}
3106
3107/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003108 * Try to abandon current file and edit a new or existing file.
3109 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003111 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003112 * -1 for successfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * 'lnum' is the line number for the cursor in the new file (if non-zero).
3114 */
3115 int
3116getfile(fnum, ffname, sfname, setpm, lnum, forceit)
3117 int fnum;
3118 char_u *ffname;
3119 char_u *sfname;
3120 int setpm;
3121 linenr_T lnum;
3122 int forceit;
3123{
3124 int other;
3125 int retval;
3126 char_u *free_me = NULL;
3127
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003128 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 return 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003130#ifdef FEAT_AUTOCMD
3131 if (curbuf_locked())
3132 return 1;
3133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 if (fnum == 0)
3136 {
3137 /* make ffname full path, set sfname */
3138 fname_expand(curbuf, &ffname, &sfname);
3139 other = otherfile(ffname);
3140 free_me = ffname; /* has been allocated, free() later */
3141 }
3142 else
3143 other = (fnum != curbuf->b_fnum);
3144
3145 if (other)
3146 ++no_wait_return; /* don't wait for autowrite message */
3147 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
3148 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
3149 {
3150#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3151 if (p_confirm && p_write)
3152 dialog_changed(curbuf, FALSE);
3153 if (curbufIsChanged())
3154#endif
3155 {
3156 if (other)
3157 --no_wait_return;
3158 EMSG(_(e_nowrtmsg));
3159 retval = 2; /* file has been changed */
3160 goto theend;
3161 }
3162 }
3163 if (other)
3164 --no_wait_return;
3165 if (setpm)
3166 setpcmark();
3167 if (!other)
3168 {
3169 if (lnum != 0)
3170 curwin->w_cursor.lnum = lnum;
3171 check_cursor_lnum();
3172 beginline(BL_SOL | BL_FIX);
3173 retval = 0; /* it's in the same file */
3174 }
3175 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003176 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
3177 curwin) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 retval = -1; /* opened another file */
3179 else
3180 retval = 1; /* error encountered */
3181
3182theend:
3183 vim_free(free_me);
3184 return retval;
3185}
3186
3187/*
3188 * start editing a new file
3189 *
3190 * fnum: file number; if zero use ffname/sfname
3191 * ffname: the file name
3192 * - full path if sfname used,
3193 * - any file name if sfname is NULL
3194 * - empty string to re-edit with the same file name (but may be
3195 * in a different directory)
3196 * - NULL to start an empty buffer
3197 * sfname: the short file name (or NULL)
3198 * eap: contains the command to be executed after loading the file and
3199 * forced 'ff' and 'fenc'
3200 * newlnum: if > 0: put cursor on this line number (if possible)
3201 * if ECMD_LASTL: use last position in loaded file
3202 * if ECMD_LAST: use last position in all files
3203 * if ECMD_ONE: use first line
3204 * flags:
3205 * ECMD_HIDE: if TRUE don't free the current buffer
3206 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
3207 * ECMD_OLDBUF: use existing buffer if it exists
3208 * ECMD_FORCEIT: ! used for Ex command
3209 * ECMD_ADDBUF: don't edit, just add to buffer list
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003210 * oldwin: Should be "curwin" when editing a new buffer in the current
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003211 * window, NULL when splitting the window first. When not NULL info
3212 * of the previous buffer for "oldwin" is stored.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 *
3214 * return FAIL for failure, OK otherwise
3215 */
3216 int
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003217do_ecmd(fnum, ffname, sfname, eap, newlnum, flags, oldwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 int fnum;
3219 char_u *ffname;
3220 char_u *sfname;
3221 exarg_T *eap; /* can be NULL! */
3222 linenr_T newlnum;
3223 int flags;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003224 win_T *oldwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 int other_file; /* TRUE if editing another file */
3227 int oldbuf; /* TRUE if using existing buffer */
3228#ifdef FEAT_AUTOCMD
3229 int auto_buf = FALSE; /* TRUE if autocommands brought us
3230 into the buffer unexpectedly */
3231 char_u *new_name = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003232 int did_set_swapcommand = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#endif
3234 buf_T *buf;
3235#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3236 buf_T *old_curbuf = curbuf;
3237#endif
3238 char_u *free_fname = NULL;
3239#ifdef FEAT_BROWSE
3240 char_u *browse_file = NULL;
3241#endif
3242 int retval = FAIL;
3243 long n;
Bram Moolenaareab316b2015-03-24 11:46:30 +01003244 pos_T orig_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 linenr_T topline = 0;
3246 int newcol = -1;
3247 int solcol = -1;
3248 pos_T *pos;
3249#ifdef FEAT_SUN_WORKSHOP
3250 char_u *cp;
3251#endif
3252 char_u *command = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003253#ifdef FEAT_SPELL
3254 int did_get_winopts = FALSE;
3255#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003256 int readfile_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 if (eap != NULL)
3259 command = eap->do_ecmd_cmd;
3260
3261 if (fnum != 0)
3262 {
3263 if (fnum == curbuf->b_fnum) /* file is already being edited */
3264 return OK; /* nothing to do */
3265 other_file = TRUE;
3266 }
3267 else
3268 {
3269#ifdef FEAT_BROWSE
3270 if (cmdmod.browse)
3271 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003272# ifdef FEAT_AUTOCMD
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003273 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003274# ifdef FEAT_GUI
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003275 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003276# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003277 au_has_group((char_u *)"FileExplorer"))
3278 {
3279 /* No browsing supported but we do have the file explorer:
3280 * Edit the directory. */
3281 if (ffname == NULL || !mch_isdir(ffname))
3282 ffname = (char_u *)".";
3283 }
3284 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003285# endif
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003286 {
3287 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003289 if (browse_file == NULL)
3290 goto theend;
3291 ffname = browse_file;
3292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294#endif
3295 /* if no short name given, use ffname for short name */
3296 if (sfname == NULL)
3297 sfname = ffname;
3298#ifdef USE_FNAME_CASE
3299# ifdef USE_LONG_FNAME
3300 if (USE_LONG_FNAME)
3301# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003302 if (sfname != NULL)
3303 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
3305
3306#ifdef FEAT_LISTCMDS
3307 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3308 goto theend;
3309#endif
3310
3311 if (ffname == NULL)
3312 other_file = TRUE;
3313 /* there is no file name */
3314 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3315 other_file = FALSE;
3316 else
3317 {
3318 if (*ffname == NUL) /* re-edit with same file name */
3319 {
3320 ffname = curbuf->b_ffname;
3321 sfname = curbuf->b_fname;
3322 }
3323 free_fname = fix_fname(ffname); /* may expand to full path name */
3324 if (free_fname != NULL)
3325 ffname = free_fname;
3326 other_file = otherfile(ffname);
3327#ifdef FEAT_SUN_WORKSHOP
3328 if (usingSunWorkShop && p_acd
3329 && (cp = vim_strrchr(sfname, '/')) != NULL)
3330 sfname = ++cp;
3331#endif
3332 }
3333 }
3334
3335 /*
3336 * if the file was changed we may not be allowed to abandon it
3337 * - if we are going to re-edit the same file
3338 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3339 */
3340 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3341 || (curbuf->b_nwindows == 1
3342 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
Bram Moolenaar45d3b142013-11-09 03:31:51 +01003343 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
3344 | (other_file ? 0 : CCGD_MULTWIN)
3345 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
3346 | (eap == NULL ? 0 : CCGD_EXCMD)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 if (fnum == 0 && other_file && ffname != NULL)
3349 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3350 goto theend;
3351 }
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 /*
3354 * End Visual mode before switching to another buffer, so the text can be
3355 * copied into the GUI selection buffer.
3356 */
3357 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003359#ifdef FEAT_AUTOCMD
3360 if ((command != NULL || newlnum > (linenr_T)0)
3361 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3362 {
3363 int len;
3364 char_u *p;
3365
3366 /* Set v:swapcommand for the SwapExists autocommands. */
3367 if (command != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003368 len = (int)STRLEN(command) + 3;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003369 else
3370 len = 30;
3371 p = alloc((unsigned)len);
3372 if (p != NULL)
3373 {
3374 if (command != NULL)
3375 vim_snprintf((char *)p, len, ":%s\r", command);
3376 else
3377 vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
3378 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
3379 did_set_swapcommand = TRUE;
3380 vim_free(p);
3381 }
3382 }
3383#endif
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 /*
3386 * If we are starting to edit another file, open a (new) buffer.
3387 * Otherwise we re-use the current buffer.
3388 */
3389 if (other_file)
3390 {
3391#ifdef FEAT_LISTCMDS
3392 if (!(flags & ECMD_ADDBUF))
3393#endif
3394 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003395 if (!cmdmod.keepalt)
3396 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003397 if (oldwin != NULL)
3398 buflist_altfpos(oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400
3401 if (fnum)
3402 buf = buflist_findnr(fnum);
3403 else
3404 {
3405#ifdef FEAT_LISTCMDS
3406 if (flags & ECMD_ADDBUF)
3407 {
3408 linenr_T tlnum = 1L;
3409
3410 if (command != NULL)
3411 {
3412 tlnum = atol((char *)command);
3413 if (tlnum <= 0)
3414 tlnum = 1L;
3415 }
3416 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3417 goto theend;
3418 }
3419#endif
3420 buf = buflist_new(ffname, sfname, 0L,
3421 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02003422#ifdef FEAT_AUTOCMD
3423 /* autocommands may change curwin and curbuf */
3424 if (oldwin != NULL)
3425 oldwin = curwin;
3426 old_curbuf = curbuf;
3427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429 if (buf == NULL)
3430 goto theend;
3431 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3432 {
3433 oldbuf = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435 else /* existing memfile */
3436 {
3437 oldbuf = TRUE;
3438 (void)buf_check_timestamp(buf, FALSE);
3439 /* Check if autocommands made buffer invalid or changed the current
3440 * buffer. */
3441 if (!buf_valid(buf)
3442#ifdef FEAT_AUTOCMD
3443 || curbuf != old_curbuf
3444#endif
3445 )
3446 goto theend;
3447#ifdef FEAT_EVAL
3448 if (aborting()) /* autocmds may abort script processing */
3449 goto theend;
3450#endif
3451 }
3452
3453 /* May jump to last used line number for a loaded buffer or when asked
3454 * for explicitly */
3455 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3456 {
3457 pos = buflist_findfpos(buf);
3458 newlnum = pos->lnum;
3459 solcol = pos->col;
3460 }
3461
3462 /*
3463 * Make the (new) buffer the one used by the current window.
3464 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3465 * If the current buffer was empty and has no file name, curbuf
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01003466 * is returned by buflist_new(), nothing to do here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 */
3468 if (buf != curbuf)
3469 {
3470#ifdef FEAT_AUTOCMD
3471 /*
3472 * Be careful: The autocommands may delete any buffer and change
3473 * the current buffer.
3474 * - If the buffer we are going to edit is deleted, give up.
3475 * - If the current buffer is deleted, prefer to load the new
3476 * buffer when loading a buffer is required. This avoids
3477 * loading another buffer which then must be closed again.
3478 * - If we ended up in the new buffer already, need to skip a few
3479 * things, set auto_buf.
3480 */
3481 if (buf->b_fname != NULL)
3482 new_name = vim_strsave(buf->b_fname);
3483 au_new_curbuf = buf;
3484 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3485 if (!buf_valid(buf)) /* new buffer has been deleted */
3486 {
3487 delbuf_msg(new_name); /* frees new_name */
3488 goto theend;
3489 }
3490# ifdef FEAT_EVAL
3491 if (aborting()) /* autocmds may abort script processing */
3492 {
3493 vim_free(new_name);
3494 goto theend;
3495 }
3496# endif
3497 if (buf == curbuf) /* already in new buffer */
3498 auto_buf = TRUE;
3499 else
3500 {
3501 if (curbuf == old_curbuf)
3502#endif
3503 buf_copy_options(buf, BCO_ENTER);
3504
3505 /* close the link to the current buffer */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003506 u_sync(FALSE);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003507 close_buffer(oldwin, curbuf,
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003508 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510#ifdef FEAT_AUTOCMD
Bram Moolenaarfc573802011-12-30 15:01:59 +01003511 /* Autocommands may open a new window and leave oldwin open
3512 * which leads to crashes since the above call sets
3513 * oldwin->w_buffer to NULL. */
3514 if (curwin != oldwin && oldwin != aucmd_win
3515 && win_valid(oldwin) && oldwin->w_buffer == NULL)
3516 win_close(oldwin, FALSE);
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518# ifdef FEAT_EVAL
3519 if (aborting()) /* autocmds may abort script processing */
3520 {
3521 vim_free(new_name);
3522 goto theend;
3523 }
3524# endif
3525 /* Be careful again, like above. */
3526 if (!buf_valid(buf)) /* new buffer has been deleted */
3527 {
3528 delbuf_msg(new_name); /* frees new_name */
3529 goto theend;
3530 }
3531 if (buf == curbuf) /* already in new buffer */
3532 auto_buf = TRUE;
3533 else
3534#endif
3535 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003536#ifdef FEAT_SYN_HL
3537 /*
3538 * <VN> We could instead free the synblock
3539 * and re-attach to buffer, perhaps.
3540 */
3541 if (curwin->w_s == &(curwin->w_buffer->b_s))
Bram Moolenaar720ce532012-04-25 12:57:28 +02003542 curwin->w_s = &(buf->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 curwin->w_buffer = buf;
3545 curbuf = buf;
3546 ++curbuf->b_nwindows;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003547
3548 /* Set 'fileformat', 'binary' and 'fenc' when forced. */
3549 if (!oldbuf && eap != NULL)
3550 {
3551 set_file_options(TRUE, eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003552#ifdef FEAT_MBYTE
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003553 set_forced_fenc(eap);
Bram Moolenaara2320f42013-07-28 15:16:19 +02003554#endif
Bram Moolenaarad875fb2013-07-24 15:02:03 +02003555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
3558 /* May get the window options from the last time this buffer
3559 * was in this window (or another window). If not used
3560 * before, reset the local window options to the global
3561 * values. Also restores old folding stuff. */
Bram Moolenaarb1269f12007-06-19 09:51:25 +00003562 get_winopts(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003563#ifdef FEAT_SPELL
3564 did_get_winopts = TRUE;
3565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567#ifdef FEAT_AUTOCMD
3568 }
3569 vim_free(new_name);
3570 au_new_curbuf = NULL;
3571#endif
3572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
3574 curwin->w_pcmark.lnum = 1;
3575 curwin->w_pcmark.col = 0;
3576 }
3577 else /* !other_file */
3578 {
3579 if (
3580#ifdef FEAT_LISTCMDS
3581 (flags & ECMD_ADDBUF) ||
3582#endif
3583 check_fname() == FAIL)
3584 goto theend;
Bram Moolenaardf5caa02015-01-27 11:26:15 +01003585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 oldbuf = (flags & ECMD_OLDBUF);
3587 }
3588
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003589#ifdef FEAT_AUTOCMD
3590 buf = curbuf;
3591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3593 {
Bram Moolenaar54fb4382014-11-12 19:28:16 +01003594 prepare_help_buffer();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596 else
3597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 /* Don't make a buffer listed if it's a help buffer. Useful when
3599 * using CTRL-O to go back to a help file. */
3600 if (!curbuf->b_help)
3601 set_buflisted(TRUE);
3602 }
3603
3604#ifdef FEAT_AUTOCMD
3605 /* If autocommands change buffers under our fingers, forget about
3606 * editing the file. */
3607 if (buf != curbuf)
3608 goto theend;
3609# ifdef FEAT_EVAL
3610 if (aborting()) /* autocmds may abort script processing */
3611 goto theend;
3612# endif
3613
3614 /* Since we are starting to edit a file, consider the filetype to be
3615 * unset. Helps for when an autocommand changes files and expects syntax
3616 * highlighting to work in the other file. */
3617 did_filetype = FALSE;
3618#endif
3619
3620/*
3621 * other_file oldbuf
3622 * FALSE FALSE re-edit same file, buffer is re-used
3623 * FALSE TRUE re-edit same file, nothing changes
3624 * TRUE FALSE start editing new file, new buffer
3625 * TRUE TRUE start editing in existing buffer (nothing to do)
3626 */
3627 if (!other_file && !oldbuf) /* re-use the buffer */
3628 {
3629 set_last_cursor(curwin); /* may set b_last_cursor */
3630 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3631 {
3632 newlnum = curwin->w_cursor.lnum;
3633 solcol = curwin->w_cursor.col;
3634 }
3635#ifdef FEAT_AUTOCMD
3636 buf = curbuf;
3637 if (buf->b_fname != NULL)
3638 new_name = vim_strsave(buf->b_fname);
3639 else
3640 new_name = NULL;
3641#endif
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003642 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
3643 {
3644 /* Save all the text, so that the reload can be undone.
3645 * Sync first so that this is a separate undo-able action. */
3646 u_sync(FALSE);
3647 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
3648 == FAIL)
3649 goto theend;
3650 u_unchanged(curbuf);
3651 buf_freeall(curbuf, BFA_KEEP_UNDO);
3652
3653 /* tell readfile() not to clear or reload undo info */
3654 readfile_flags = READ_KEEP_UNDO;
3655 }
3656 else
3657 buf_freeall(curbuf, 0); /* free all things for buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#ifdef FEAT_AUTOCMD
3659 /* If autocommands deleted the buffer we were going to re-edit, give
3660 * up and jump to the end. */
3661 if (!buf_valid(buf))
3662 {
3663 delbuf_msg(new_name); /* frees new_name */
3664 goto theend;
3665 }
3666 vim_free(new_name);
3667
3668 /* If autocommands change buffers under our fingers, forget about
3669 * re-editing the file. Should do the buf_clear_file(), but perhaps
3670 * the autocommands changed the buffer... */
3671 if (buf != curbuf)
3672 goto theend;
3673# ifdef FEAT_EVAL
3674 if (aborting()) /* autocmds may abort script processing */
3675 goto theend;
3676# endif
3677#endif
3678 buf_clear_file(curbuf);
3679 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3680 curbuf->b_op_end.lnum = 0;
3681 }
3682
3683/*
3684 * If we get here we are sure to start editing
3685 */
3686 /* don't redraw until the cursor is in the right line */
3687 ++RedrawingDisabled;
3688
3689 /* Assume success now */
3690 retval = OK;
3691
3692 /*
3693 * Reset cursor position, could be used by autocommands.
3694 */
3695 check_cursor();
3696
3697 /*
3698 * Check if we are editing the w_arg_idx file in the argument list.
3699 */
3700 check_arg_idx(curwin);
3701
3702#ifdef FEAT_AUTOCMD
3703 if (!auto_buf)
3704#endif
3705 {
3706 /*
3707 * Set cursor and init window before reading the file and executing
3708 * autocommands. This allows for the autocommands to position the
3709 * cursor.
3710 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003711 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
3713#ifdef FEAT_FOLDING
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003714 /* It's possible that all lines in the buffer changed. Need to update
3715 * automatic folding for all windows where it's used. */
3716# ifdef FEAT_WINDOWS
3717 {
3718 win_T *win;
3719 tabpage_T *tp;
3720
3721 FOR_ALL_TAB_WINDOWS(tp, win)
3722 if (win->w_buffer == curbuf)
3723 foldUpdateAll(win);
3724 }
3725# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 foldUpdateAll(curwin);
Bram Moolenaareb1b6792007-08-21 13:29:28 +00003727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#endif
3729
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003730 /* Change directories when the 'acd' option is set. */
3731 DO_AUTOCHDIR
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 /*
3734 * Careful: open_buffer() and apply_autocmds() may change the current
3735 * buffer and window.
3736 */
Bram Moolenaareab316b2015-03-24 11:46:30 +01003737 orig_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 topline = curwin->w_topline;
3739 if (!oldbuf) /* need to read the file */
3740 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003741#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 swap_exists_action = SEA_DIALOG;
3743#endif
3744 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3745
3746 /*
3747 * Open the buffer and read the file.
3748 */
3749#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003750 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 retval = FAIL;
3752#else
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003753 (void)open_buffer(FALSE, eap, readfile_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#endif
3755
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003756#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (swap_exists_action == SEA_QUIT)
3758 retval = FAIL;
3759 handle_swap_exists(old_curbuf);
3760#endif
3761 }
3762#ifdef FEAT_AUTOCMD
3763 else
3764 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003765 /* Read the modelines, but only to set window-local options. Any
3766 * buffer-local options have already been set and may have been
3767 * changed by the user. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00003768 do_modelines(OPT_WINONLY);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3771 &retval);
3772 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3773 &retval);
3774 }
3775 check_arg_idx(curwin);
3776#endif
3777
Bram Moolenaareab316b2015-03-24 11:46:30 +01003778 /* If autocommands change the cursor position or topline, we should
3779 * keep it. Also when it moves within a line. */
3780 if (!equalpos(curwin->w_cursor, orig_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
3782 newlnum = curwin->w_cursor.lnum;
3783 newcol = curwin->w_cursor.col;
3784 }
3785 if (curwin->w_topline == topline)
3786 topline = 0;
3787
3788 /* Even when cursor didn't move we need to recompute topline. */
3789 changed_line_abv_curs();
3790
3791#ifdef FEAT_TITLE
3792 maketitle();
3793#endif
3794 }
3795
3796#ifdef FEAT_DIFF
3797 /* Tell the diff stuff that this buffer is new and/or needs updating.
3798 * Also needed when re-editing the same buffer, because unloading will
3799 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003800 if (curwin->w_p_diff)
3801 {
3802 diff_buf_add(curbuf);
3803 diff_invalidate(curbuf);
3804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#endif
3806
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003807#ifdef FEAT_SPELL
3808 /* If the window options were changed may need to set the spell language.
3809 * Can only do this after the buffer has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003810 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
3811 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00003812#endif
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (command == NULL)
3815 {
3816 if (newcol >= 0) /* position set by autocommands */
3817 {
3818 curwin->w_cursor.lnum = newlnum;
3819 curwin->w_cursor.col = newcol;
3820 check_cursor();
3821 }
3822 else if (newlnum > 0) /* line number from caller or old position */
3823 {
3824 curwin->w_cursor.lnum = newlnum;
3825 check_cursor_lnum();
3826 if (solcol >= 0 && !p_sol)
3827 {
3828 /* 'sol' is off: Use last known column. */
3829 curwin->w_cursor.col = solcol;
3830 check_cursor_col();
3831#ifdef FEAT_VIRTUALEDIT
3832 curwin->w_cursor.coladd = 0;
3833#endif
3834 curwin->w_set_curswant = TRUE;
3835 }
3836 else
3837 beginline(BL_SOL | BL_FIX);
3838 }
3839 else /* no line number, go to last line in Ex mode */
3840 {
3841 if (exmode_active)
3842 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3843 beginline(BL_WHITE | BL_FIX);
3844 }
3845 }
3846
3847#ifdef FEAT_WINDOWS
3848 /* Check if cursors in other windows on the same buffer are still valid */
3849 check_lnums(FALSE);
3850#endif
3851
3852 /*
3853 * Did not read the file, need to show some info about the file.
3854 * Do this after setting the cursor.
3855 */
3856 if (oldbuf
3857#ifdef FEAT_AUTOCMD
3858 && !auto_buf
3859#endif
3860 )
3861 {
3862 int msg_scroll_save = msg_scroll;
3863
3864 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3865 * message. */
3866 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3867 msg_scroll = FALSE;
3868 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3869 check_for_delay(FALSE);
3870 msg_start();
3871 msg_scroll = msg_scroll_save;
3872 msg_scrolled_ign = TRUE;
3873
3874 fileinfo(FALSE, TRUE, FALSE);
3875
3876 msg_scrolled_ign = FALSE;
3877 }
3878
3879 if (command != NULL)
3880 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3881
3882#ifdef FEAT_KEYMAP
3883 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003884 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885#endif
3886
3887 --RedrawingDisabled;
3888 if (!skip_redraw)
3889 {
3890 n = p_so;
3891 if (topline == 0 && command == NULL)
3892 p_so = 999; /* force cursor halfway the window */
3893 update_topline();
3894#ifdef FEAT_SCROLLBIND
3895 curwin->w_scbind_pos = curwin->w_topline;
3896#endif
3897 p_so = n;
3898 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3899 }
3900
3901 if (p_im)
3902 need_start_insertmode = TRUE;
3903
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003904 /* Change directories when the 'acd' option is set. */
3905 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar7b89edc2006-04-06 20:21:51 +00003907#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003908 if (curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
3910# ifdef FEAT_SUN_WORKSHOP
Bram Moolenaar67c53842010-05-22 18:28:27 +02003911 if (gui.in_use && usingSunWorkShop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3913# endif
3914# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003915 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003916 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917# endif
3918 }
3919#endif
3920
3921theend:
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003922#ifdef FEAT_AUTOCMD
3923 if (did_set_swapcommand)
3924 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
3925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#ifdef FEAT_BROWSE
3927 vim_free(browse_file);
3928#endif
3929 vim_free(free_fname);
3930 return retval;
3931}
3932
3933#ifdef FEAT_AUTOCMD
3934 static void
3935delbuf_msg(name)
3936 char_u *name;
3937{
3938 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3939 name == NULL ? (char_u *)"" : name);
3940 vim_free(name);
3941 au_new_curbuf = NULL;
3942}
3943#endif
3944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003945static int append_indent = 0; /* autoindent for first line */
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947/*
3948 * ":insert" and ":append", also used by ":change"
3949 */
3950 void
3951ex_append(eap)
3952 exarg_T *eap;
3953{
3954 char_u *theline;
3955 int did_undo = FALSE;
3956 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003957 int indent = 0;
3958 char_u *p;
3959 int vcol;
3960 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003962 /* the ! flag toggles autoindent */
3963 if (eap->forceit)
3964 curbuf->b_p_ai = !curbuf->b_p_ai;
3965
3966 /* First autoindent comes from the line we start on */
3967 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
3968 append_indent = get_indent_lnum(lnum);
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (eap->cmdidx != CMD_append)
3971 --lnum;
3972
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003973 /* when the buffer is empty append to line 0 and delete the dummy line */
3974 if (empty && lnum == 1)
3975 lnum = 0;
3976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 State = INSERT; /* behave like in Insert mode */
3978 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3979 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003980
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003981 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 msg_scroll = TRUE;
3984 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003985 if (curbuf->b_p_ai)
3986 {
3987 if (append_indent >= 0)
3988 {
3989 indent = append_indent;
3990 append_indent = -1;
3991 }
3992 else if (lnum > 0)
3993 indent = get_indent_lnum(lnum);
3994 }
3995 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003997 {
3998 /* No getline() function, use the lines that follow. This ends
3999 * when there is no more. */
4000 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
4001 break;
4002 p = vim_strchr(eap->nextcmd, NL);
4003 if (p == NULL)
4004 p = eap->nextcmd + STRLEN(eap->nextcmd);
4005 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
4006 if (*p != NUL)
4007 ++p;
4008 eap->nextcmd = p;
4009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 else
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004011 {
4012 int save_State = State;
4013
4014 /* Set State to avoid the cursor shape to be set to INSERT mode
4015 * when getline() returns. */
4016 State = CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 theline = eap->getline(
4018#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004019 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004021 NUL, eap->cookie, indent);
Bram Moolenaar26f08b02014-08-29 09:02:27 +02004022 State = save_State;
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004025 if (theline == NULL)
4026 break;
4027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004028 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
4029 if (ex_keep_indent)
4030 append_indent = indent;
4031
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004032 /* Look for the "." after automatic indent. */
4033 vcol = 0;
4034 for (p = theline; indent > vcol; ++p)
4035 {
4036 if (*p == ' ')
4037 ++vcol;
4038 else if (*p == TAB)
4039 vcol += 8 - vcol % 8;
4040 else
4041 break;
4042 }
4043 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00004044 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
4045 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 vim_free(theline);
4048 break;
4049 }
4050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004051 /* don't use autoindent if nothing was typed. */
4052 if (p[0] == NUL)
4053 theline[0] = NUL;
4054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 did_undo = TRUE;
4056 ml_append(lnum, theline, (colnr_T)0, FALSE);
4057 appended_lines_mark(lnum, 1L);
4058
4059 vim_free(theline);
4060 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004061
4062 if (empty)
4063 {
4064 ml_delete(2L, FALSE);
4065 empty = FALSE;
4066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 State = NORMAL;
4069
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004070 if (eap->forceit)
4071 curbuf->b_p_ai = !curbuf->b_p_ai;
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 /* "start" is set to eap->line2+1 unless that position is invalid (when
Bram Moolenaar45667512007-05-10 19:24:43 +00004074 * eap->line2 pointed to the end of the buffer and nothing was appended)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * "end" is set to lnum when something has been appended, otherwise
4076 * it is the same than "start" -- Acevedo */
4077 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
4078 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
4079 if (eap->cmdidx != CMD_append)
4080 --curbuf->b_op_start.lnum;
4081 curbuf->b_op_end.lnum = (eap->line2 < lnum)
4082 ? lnum : curbuf->b_op_start.lnum;
4083 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4084 curwin->w_cursor.lnum = lnum;
4085 check_cursor_lnum();
4086 beginline(BL_SOL | BL_FIX);
4087
4088 need_wait_return = FALSE; /* don't use wait_return() now */
4089 ex_no_reprint = TRUE;
4090}
4091
4092/*
4093 * ":change"
4094 */
4095 void
4096ex_change(eap)
4097 exarg_T *eap;
4098{
4099 linenr_T lnum;
4100
4101 if (eap->line2 >= eap->line1
4102 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
4103 return;
4104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004105 /* the ! flag toggles autoindent */
4106 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
4107 append_indent = get_indent_lnum(eap->line1);
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
4110 {
4111 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
4112 break;
4113 ml_delete(eap->line1, FALSE);
4114 }
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00004115
4116 /* make sure the cursor is not beyond the end of the file now */
4117 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
4119
4120 /* ":append" on the line above the deleted lines. */
4121 eap->line2 = eap->line1;
4122 ex_append(eap);
4123}
4124
4125 void
4126ex_z(eap)
4127 exarg_T *eap;
4128{
4129 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004130 int bigness;
4131 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 int minus = 0;
4133 linenr_T start, end, curs, i;
4134 int j;
4135 linenr_T lnum = eap->line2;
4136
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004137 /* Vi compatible: ":z!" uses display height, without a count uses
4138 * 'scroll' */
4139 if (eap->forceit)
4140 bigness = curwin->w_height;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004141#ifdef FEAT_WINDOWS
Bram Moolenaar631abc32014-02-22 22:27:47 +01004142 else if (firstwin != lastwin)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004143 bigness = curwin->w_height - 3;
Bram Moolenaar78a15312009-05-15 19:33:18 +00004144#endif
Bram Moolenaar631abc32014-02-22 22:27:47 +01004145 else
4146 bigness = curwin->w_p_scr * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (bigness < 1)
4148 bigness = 1;
4149
4150 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004151 kind = x;
4152 if (*kind == '-' || *kind == '+' || *kind == '='
4153 || *kind == '^' || *kind == '.')
4154 ++x;
4155 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 ++x;
4157
4158 if (*x != 0)
4159 {
4160 if (!VIM_ISDIGIT(*x))
4161 {
4162 EMSG(_("E144: non-numeric argument to :z"));
4163 return;
4164 }
4165 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004168 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004169 if (*kind == '=')
4170 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004174 /* the number of '-' and '+' multiplies the distance */
4175 if (*kind == '-' || *kind == '+')
4176 for (x = kind + 1; *x == *kind; ++x)
4177 ;
4178
4179 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 case '-':
Bram Moolenaard9758e32011-06-12 22:03:23 +02004182 start = lnum - bigness * (linenr_T)(x - kind) + 1;
4183 end = start + bigness - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004184 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 break;
4186
4187 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004188 start = lnum - (bigness + 1) / 2 + 1;
4189 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 curs = lnum;
4191 minus = 1;
4192 break;
4193
4194 case '^':
4195 start = lnum - bigness * 2;
4196 end = lnum - bigness;
4197 curs = lnum - bigness;
4198 break;
4199
4200 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004201 start = lnum - (bigness + 1) / 2 + 1;
4202 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 curs = end;
4204 break;
4205
4206 default: /* '+' */
4207 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004208 if (*kind == '+')
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004209 start += bigness * (linenr_T)(x - kind - 1) + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004210 else if (eap->addr_count == 0)
4211 ++start;
4212 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 curs = end;
4214 break;
4215 }
4216
4217 if (start < 1)
4218 start = 1;
4219
4220 if (end > curbuf->b_ml.ml_line_count)
4221 end = curbuf->b_ml.ml_line_count;
4222
4223 if (curs > curbuf->b_ml.ml_line_count)
4224 curs = curbuf->b_ml.ml_line_count;
4225
4226 for (i = start; i <= end; i++)
4227 {
4228 if (minus && i == lnum)
4229 {
4230 msg_putchar('\n');
4231
4232 for (j = 1; j < Columns; j++)
4233 msg_putchar('-');
4234 }
4235
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004236 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 if (minus && i == lnum)
4239 {
4240 msg_putchar('\n');
4241
4242 for (j = 1; j < Columns; j++)
4243 msg_putchar('-');
4244 }
4245 }
4246
4247 curwin->w_cursor.lnum = curs;
4248 ex_no_reprint = TRUE;
4249}
4250
4251/*
4252 * Check if the restricted flag is set.
4253 * If so, give an error message and return TRUE.
4254 * Otherwise, return FALSE.
4255 */
4256 int
4257check_restricted()
4258{
4259 if (restricted)
4260 {
4261 EMSG(_("E145: Shell commands not allowed in rvim"));
4262 return TRUE;
4263 }
4264 return FALSE;
4265}
4266
4267/*
4268 * Check if the secure flag is set (.exrc or .vimrc in current directory).
4269 * If so, give an error message and return TRUE.
4270 * Otherwise, return FALSE.
4271 */
4272 int
4273check_secure()
4274{
4275 if (secure)
4276 {
4277 secure = 2;
4278 EMSG(_(e_curdir));
4279 return TRUE;
4280 }
4281#ifdef HAVE_SANDBOX
4282 /*
4283 * In the sandbox more things are not allowed, including the things
4284 * disallowed in secure mode.
4285 */
4286 if (sandbox != 0)
4287 {
4288 EMSG(_(e_sandbox));
4289 return TRUE;
4290 }
4291#endif
4292 return FALSE;
4293}
4294
4295static char_u *old_sub = NULL; /* previous substitute pattern */
4296static int global_need_beginline; /* call beginline() after ":g" */
4297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298/* do_sub()
4299 *
4300 * Perform a substitution from line eap->line1 to line eap->line2 using the
4301 * command pointed to by eap->arg which should be of the form:
4302 *
4303 * /pattern/substitution/{flags}
4304 *
4305 * The usual escapes are supported as described in the regexp docs.
4306 */
4307 void
4308do_sub(eap)
4309 exarg_T *eap;
4310{
4311 linenr_T lnum;
4312 long i = 0;
4313 regmmatch_T regmatch;
4314 static int do_all = FALSE; /* do multiple substitutions per line */
4315 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004316 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 static int do_error = TRUE; /* if false, ignore errors */
4318 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004319 static int do_list = FALSE; /* list last line with subs. */
4320 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 static int do_ic = 0; /* ignore case flag */
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004322 int save_do_all; /* remember user specified 'g' flag */
4323 int save_do_ask; /* remember user specified 'c' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4325 int delimiter;
4326 int sublen;
4327 int got_quit = FALSE;
4328 int got_match = FALSE;
4329 int temp;
4330 int which_pat;
4331 char_u *cmd;
4332 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004333 linenr_T first_line = 0; /* first changed line */
4334 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 * change */
4336 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4337 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004338 long nmatch; /* number of lines in match */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004339 char_u *sub_firstline; /* allocated copy of first sub line */
4340 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004341 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar46475522010-03-23 17:36:29 +01004342 int start_nsubs;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004343#ifdef FEAT_EVAL
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004344 int save_ma = 0;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 cmd = eap->arg;
4348 if (!global_busy)
4349 {
4350 sub_nsubs = 0;
4351 sub_nlines = 0;
4352 }
Bram Moolenaar46475522010-03-23 17:36:29 +01004353 start_nsubs = sub_nsubs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (eap->cmdidx == CMD_tilde)
4356 which_pat = RE_LAST; /* use last used regexp */
4357 else
4358 which_pat = RE_SUBST; /* use last substitute regexp */
4359
4360 /* new pattern and substitution */
4361 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4362 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4363 {
4364 /* don't accept alphanumeric for separator */
4365 if (isalpha(*cmd))
4366 {
4367 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4368 return;
4369 }
4370 /*
4371 * undocumented vi feature:
4372 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4373 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4374 */
4375 if (*cmd == '\\')
4376 {
4377 ++cmd;
4378 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4379 {
4380 EMSG(_(e_backslash));
4381 return;
4382 }
4383 if (*cmd != '&')
4384 which_pat = RE_SEARCH; /* use last '/' pattern */
4385 pat = (char_u *)""; /* empty search pattern */
4386 delimiter = *cmd++; /* remember delimiter character */
4387 }
4388 else /* find the end of the regexp */
4389 {
Bram Moolenaar3a169c32008-01-02 12:59:21 +00004390#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4391 if (p_altkeymap && curwin->w_p_rl)
4392 lrF_sub(cmd);
4393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 which_pat = RE_LAST; /* use last used regexp */
4395 delimiter = *cmd++; /* remember delimiter character */
4396 pat = cmd; /* remember start of search pat */
4397 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4398 if (cmd[0] == delimiter) /* end delimiter found */
4399 *cmd++ = NUL; /* replace it with a NUL */
4400 }
4401
4402 /*
4403 * Small incompatibility: vi sees '\n' as end of the command, but in
4404 * Vim we want to use '\n' to find/substitute a NUL.
4405 */
4406 sub = cmd; /* remember the start of the substitution */
4407
4408 while (cmd[0])
4409 {
4410 if (cmd[0] == delimiter) /* end delimiter found */
4411 {
4412 *cmd++ = NUL; /* replace it with a NUL */
4413 break;
4414 }
4415 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4416 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004417 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419
4420 if (!eap->skip)
4421 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004422 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4423 if (STRCMP(sub, "%") == 0
4424 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4425 {
4426 if (old_sub == NULL) /* there is no previous command */
4427 {
4428 EMSG(_(e_nopresub));
4429 return;
4430 }
4431 sub = old_sub;
4432 }
4433 else
4434 {
4435 vim_free(old_sub);
4436 old_sub = vim_strsave(sub);
4437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 }
4440 else if (!eap->skip) /* use previous pattern and substitution */
4441 {
4442 if (old_sub == NULL) /* there is no previous command */
4443 {
4444 EMSG(_(e_nopresub));
4445 return;
4446 }
4447 pat = NULL; /* search_regcomp() will use previous pattern */
4448 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004449
4450 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4451 * last column after using "$". */
4452 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004455 /* Recognize ":%s/\n//" and turn it into a join command, which is much
4456 * more efficient.
4457 * TODO: find a generic solution to make line-joining operations more
4458 * efficient, avoid allocating a string that grows in size.
4459 */
Bram Moolenaar21e854e2014-04-04 19:00:48 +02004460 if (pat != NULL && STRCMP(pat, "\\n") == 0
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004461 && *sub == NUL
4462 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
4463 || *cmd == 'p' || *cmd == '#'))))
4464 {
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004465 linenr_T joined_lines_count;
4466
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004467 curwin->w_cursor.lnum = eap->line1;
4468 if (*cmd == 'l')
4469 eap->flags = EXFLAG_LIST;
4470 else if (*cmd == '#')
4471 eap->flags = EXFLAG_NR;
4472 else if (*cmd == 'p')
4473 eap->flags = EXFLAG_PRINT;
4474
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +01004475 /* The number of lines joined is the number of lines in the range plus
4476 * one. One less when the last line is included. */
4477 joined_lines_count = eap->line2 - eap->line1 + 1;
4478 if (eap->line2 < curbuf->b_ml.ml_line_count)
4479 ++joined_lines_count;
4480 if (joined_lines_count > 1)
4481 {
4482 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
4483 sub_nsubs = joined_lines_count - 1;
4484 sub_nlines = 1;
4485 (void)do_sub_msg(FALSE);
4486 ex_may_print(eap);
4487 }
4488
4489 if (!cmdmod.keeppatterns)
4490 save_re_pat(RE_SUBST, pat, p_magic);
4491#ifdef FEAT_CMDHIST
4492 /* put pattern in history */
4493 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
4494#endif
4495
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02004496 return;
4497 }
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /*
4500 * Find trailing options. When '&' is used, keep old options.
4501 */
4502 if (*cmd == '&')
4503 ++cmd;
4504 else
4505 {
4506 if (!p_ed)
4507 {
4508 if (p_gd) /* default is global on */
4509 do_all = TRUE;
4510 else
4511 do_all = FALSE;
4512 do_ask = FALSE;
4513 }
4514 do_error = TRUE;
4515 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004516 do_count = FALSE;
Bram Moolenaarfe40d1a2007-07-24 09:16:38 +00004517 do_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 do_ic = 0;
4519 }
4520 while (*cmd)
4521 {
4522 /*
4523 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4524 * 'r' is never inverted.
4525 */
4526 if (*cmd == 'g')
4527 do_all = !do_all;
4528 else if (*cmd == 'c')
4529 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004530 else if (*cmd == 'n')
4531 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else if (*cmd == 'e')
4533 do_error = !do_error;
4534 else if (*cmd == 'r') /* use last used regexp */
4535 which_pat = RE_LAST;
4536 else if (*cmd == 'p')
4537 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004538 else if (*cmd == '#')
4539 {
4540 do_print = TRUE;
4541 do_number = TRUE;
4542 }
4543 else if (*cmd == 'l')
4544 {
4545 do_print = TRUE;
4546 do_list = TRUE;
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 else if (*cmd == 'i') /* ignore case */
4549 do_ic = 'i';
4550 else if (*cmd == 'I') /* don't ignore case */
4551 do_ic = 'I';
4552 else
4553 break;
4554 ++cmd;
4555 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004556 if (do_count)
4557 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02004559 save_do_all = do_all;
4560 save_do_ask = do_ask;
4561
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 /*
4563 * check for a trailing count
4564 */
4565 cmd = skipwhite(cmd);
4566 if (VIM_ISDIGIT(*cmd))
4567 {
4568 i = getdigits(&cmd);
4569 if (i <= 0 && !eap->skip && do_error)
4570 {
4571 EMSG(_(e_zerocount));
4572 return;
4573 }
4574 eap->line1 = eap->line2;
4575 eap->line2 += i - 1;
4576 if (eap->line2 > curbuf->b_ml.ml_line_count)
4577 eap->line2 = curbuf->b_ml.ml_line_count;
4578 }
4579
4580 /*
4581 * check for trailing command or garbage
4582 */
4583 cmd = skipwhite(cmd);
4584 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4585 {
4586 eap->nextcmd = check_nextcmd(cmd);
4587 if (eap->nextcmd == NULL)
4588 {
4589 EMSG(_(e_trailing));
4590 return;
4591 }
4592 }
4593
4594 if (eap->skip) /* not executing commands, only parsing */
4595 return;
4596
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004597 if (!do_count && !curbuf->b_p_ma)
4598 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004599 /* Substitution is not allowed in non-'modifiable' buffer */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00004600 EMSG(_(e_modifiable));
4601 return;
4602 }
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4605 {
4606 if (do_error)
4607 EMSG(_(e_invcmd));
4608 return;
4609 }
4610
4611 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4612 if (do_ic == 'i')
4613 regmatch.rmm_ic = TRUE;
4614 else if (do_ic == 'I')
4615 regmatch.rmm_ic = FALSE;
4616
4617 sub_firstline = NULL;
4618
4619 /*
4620 * ~ in the substitute pattern is replaced with the old pattern.
4621 * We do it here once to avoid it to be replaced over and over again.
4622 * But don't do it when it starts with "\=", then it's an expression.
4623 */
4624 if (!(sub[0] == '\\' && sub[1] == '='))
4625 sub = regtilde(sub, p_magic);
4626
4627 /*
4628 * Check for a match on each line.
4629 */
4630 line2 = eap->line2;
4631 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4632#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4633 || aborting()
4634#endif
4635 ); ++lnum)
4636 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004637 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
4638 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (nmatch)
4640 {
4641 colnr_T copycol;
4642 colnr_T matchcol;
4643 colnr_T prev_matchcol = MAXCOL;
4644 char_u *new_end, *new_start = NULL;
4645 unsigned new_start_len = 0;
4646 char_u *p1;
4647 int did_sub = FALSE;
4648 int lastone;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004649 int len, copy_len, needed_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 long nmatch_tl = 0; /* nr of lines matched below lnum */
4651 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004652 int skip_match = FALSE;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004653 linenr_T sub_firstlnum; /* nr of first sub line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 /*
4656 * The new text is build up step by step, to avoid too much
4657 * copying. There are these pieces:
Bram Moolenaara9aafe52008-05-07 11:10:28 +00004658 * sub_firstline The old text, unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 * copycol Column in the old text where we started
4660 * looking for a match; from here old text still
4661 * needs to be copied to the new text.
4662 * matchcol Column number of the old text where to look
4663 * for the next match. It's just after the
4664 * previous match or one further.
4665 * prev_matchcol Column just after the previous match (if any).
4666 * Mostly equal to matchcol, except for the first
4667 * match and after skipping an empty match.
4668 * regmatch.*pos Where the pattern matched in the old text.
4669 * new_start The new text, all that has been produced so
4670 * far.
4671 * new_end The new text, where to append new text.
4672 *
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004673 * lnum The line number where we found the start of
4674 * the match. Can be below the line we searched
4675 * when there is a \n before a \zs in the
4676 * pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 * sub_firstlnum The line number in the buffer where to look
4678 * for a match. Can be different from "lnum"
4679 * when the pattern or substitute string contains
4680 * line breaks.
4681 *
4682 * Special situations:
4683 * - When the substitute string contains a line break, the part up
4684 * to the line break is inserted in the text, but the copy of
4685 * the original line is kept. "sub_firstlnum" is adjusted for
4686 * the inserted lines.
4687 * - When the matched pattern contains a line break, the old line
4688 * is taken from the line at the end of the pattern. The lines
4689 * in the match are deleted later, "sub_firstlnum" is adjusted
4690 * accordingly.
4691 *
4692 * The new text is built up in new_start[]. It has some extra
4693 * room to avoid using alloc()/free() too often. new_start_len is
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004694 * the length of the allocated memory at new_start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 *
4696 * Make a copy of the old line, so it won't be taken away when
4697 * updating the screen or handling a multi-line match. The "old_"
4698 * pointers point into this copy.
4699 */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004700 sub_firstlnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 copycol = 0;
4702 matchcol = 0;
4703
4704 /* At first match, remember current cursor position. */
4705 if (!got_match)
4706 {
4707 setpcmark();
4708 got_match = TRUE;
4709 }
4710
4711 /*
4712 * Loop until nothing more to replace in this line.
4713 * 1. Handle match with empty string.
4714 * 2. If do_ask is set, ask for confirmation.
4715 * 3. substitute the string.
4716 * 4. if do_all is set, find next match
4717 * 5. break if there isn't another match in this line
4718 */
4719 for (;;)
4720 {
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004721 /* Advance "lnum" to the line where the match starts. The
4722 * match does not start in the first line when there is a line
4723 * break before \zs. */
4724 if (regmatch.startpos[0].lnum > 0)
4725 {
4726 lnum += regmatch.startpos[0].lnum;
4727 sub_firstlnum += regmatch.startpos[0].lnum;
4728 nmatch -= regmatch.startpos[0].lnum;
4729 vim_free(sub_firstline);
4730 sub_firstline = NULL;
4731 }
4732
4733 if (sub_firstline == NULL)
4734 {
4735 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4736 if (sub_firstline == NULL)
4737 {
4738 vim_free(new_start);
4739 goto outofmem;
4740 }
4741 }
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 /* Save the line number of the last change for the final
4744 * cursor position (just like Vi). */
4745 curwin->w_cursor.lnum = lnum;
4746 do_again = FALSE;
4747
4748 /*
4749 * 1. Match empty string does not count, except for first
4750 * match. This reproduces the strange vi behaviour.
4751 * This also catches endless loops.
4752 */
4753 if (matchcol == prev_matchcol
4754 && regmatch.endpos[0].lnum == 0
4755 && matchcol == regmatch.endpos[0].col)
4756 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004757 if (sub_firstline[matchcol] == NUL)
4758 /* We already were at the end of the line. Don't look
4759 * for a match in this line again. */
4760 skip_match = TRUE;
4761 else
Bram Moolenaar0df11022011-05-19 14:30:16 +02004762 {
4763 /* search for a match at next column */
4764#ifdef FEAT_MBYTE
4765 if (has_mbyte)
4766 matchcol += mb_ptr2len(sub_firstline + matchcol);
4767 else
4768#endif
4769 ++matchcol;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 goto skip;
4772 }
4773
4774 /* Normally we continue searching for a match just after the
4775 * previous match. */
4776 matchcol = regmatch.endpos[0].col;
4777 prev_matchcol = matchcol;
4778
4779 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004780 * 2. If do_count is set only increase the counter.
4781 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004783 if (do_count)
4784 {
4785 /* For a multi-line match, put matchcol at the NUL at
4786 * the end of the line and set nmatch to one, so that
4787 * we continue looking for a match on the next line.
4788 * Avoids that ":s/\nB\@=//gc" get stuck. */
4789 if (nmatch > 1)
4790 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004791 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004792 nmatch = 1;
Bram Moolenaar12ddc3e2008-01-04 13:53:09 +00004793 skip_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004794 }
4795 sub_nsubs++;
4796 did_sub = TRUE;
Bram Moolenaar07e31c52012-08-08 16:51:15 +02004797#ifdef FEAT_EVAL
4798 /* Skip the substitution, unless an expression is used,
4799 * then it is evaluated in the sandbox. */
4800 if (!(sub[0] == '\\' && sub[1] == '='))
4801#endif
4802 goto skip;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004803 }
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (do_ask)
4806 {
Bram Moolenaar985cb442009-05-14 19:51:46 +00004807 int typed = 0;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /* change State to CONFIRM, so that the mouse works
4810 * properly */
4811 save_State = State;
4812 State = CONFIRM;
4813#ifdef FEAT_MOUSE
4814 setmouse(); /* disable mouse in xterm */
4815#endif
4816 curwin->w_cursor.col = regmatch.startpos[0].col;
4817
4818 /* When 'cpoptions' contains "u" don't sync undo when
4819 * asking for confirmation. */
4820 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4821 ++no_u_sync;
4822
4823 /*
4824 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4825 */
4826 while (do_ask)
4827 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004828 if (exmode_active)
4829 {
4830 char_u *resp;
4831 colnr_T sc, ec;
4832
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004833 print_line_no_prefix(lnum, do_number, do_list);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004834
4835 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4836 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4837 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
Bram Moolenaar3eead7c2013-10-02 18:43:06 +02004838 if (do_number || curwin->w_p_nu)
4839 {
4840 int numw = number_width(curwin) + 1;
4841 sc += numw;
4842 ec += numw;
4843 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004844 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004845 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004846 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004847 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004848 msg_putchar('^');
4849
4850 resp = getexmodeline('?', NULL, 0);
4851 if (resp != NULL)
4852 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004853 typed = *resp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004854 vim_free(resp);
4855 }
4856 }
4857 else
4858 {
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004859 char_u *orig_line = NULL;
4860 int len_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004862 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004864 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004866 /* Invert the matched string.
4867 * Remove the inversion afterwards. */
4868 temp = RedrawingDisabled;
4869 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004871 if (new_start != NULL)
4872 {
4873 /* There already was a substitution, we would
4874 * like to show this to the user. We cannot
4875 * really update the line, it would change
4876 * what matches. Temporarily replace the line
4877 * and change it back afterwards. */
4878 orig_line = vim_strsave(ml_get(lnum));
4879 if (orig_line != NULL)
4880 {
4881 char_u *new_line = concat_str(new_start,
4882 sub_firstline + copycol);
4883
4884 if (new_line == NULL)
4885 {
4886 vim_free(orig_line);
4887 orig_line = NULL;
4888 }
4889 else
4890 {
4891 /* Position the cursor relative to the
4892 * end of the line, the previous
4893 * substitute may have inserted or
4894 * deleted characters before the
4895 * cursor. */
Bram Moolenaar04e5b5a2013-01-30 21:56:21 +01004896 len_change = (int)STRLEN(new_line)
4897 - (int)STRLEN(orig_line);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004898 curwin->w_cursor.col += len_change;
4899 ml_replace(lnum, new_line, FALSE);
4900 }
4901 }
4902 }
4903
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00004904 search_match_lines = regmatch.endpos[0].lnum
4905 - regmatch.startpos[0].lnum;
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004906 search_match_endcol = regmatch.endpos[0].col
4907 + len_change;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004908 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004910 update_topline();
4911 validate_cursor();
Bram Moolenaara1956f62006-03-12 22:18:00 +00004912 update_screen(SOME_VALID);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004913 highlight_match = FALSE;
Bram Moolenaara1956f62006-03-12 22:18:00 +00004914 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
4916#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004917 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004919 if (msg_row == Rows - 1)
4920 msg_didout = FALSE; /* avoid a scroll-up */
4921 msg_starthere();
4922 i = msg_scroll;
4923 msg_scroll = 0; /* truncate msg when
4924 needed */
4925 msg_no_more = TRUE;
4926 /* write message same highlighting as for
4927 * wait_return */
4928 smsg_attr(hl_attr(HLF_R),
4929 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4930 msg_no_more = FALSE;
4931 msg_scroll = i;
4932 showruler(TRUE);
4933 windgoto(msg_row, msg_col);
4934 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
4936#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004937 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004939 ++no_mapping; /* don't map this key */
4940 ++allow_keys; /* allow special keys */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004941 typed = plain_vgetc();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004942 --allow_keys;
4943 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004945 /* clear the question */
4946 msg_didout = FALSE; /* don't scroll up */
4947 msg_col = 0;
4948 gotocmdline(TRUE);
Bram Moolenaar4bc8cf02013-01-30 16:30:26 +01004949
4950 /* restore the line */
4951 if (orig_line != NULL)
4952 ml_replace(lnum, orig_line, FALSE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004953 }
4954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 need_wait_return = FALSE; /* no hit-return prompt */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004956 if (typed == 'q' || typed == ESC || typed == Ctrl_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#ifdef UNIX
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004958 || typed == intr_char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#endif
4960 )
4961 {
4962 got_quit = TRUE;
4963 break;
4964 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004965 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004967 if (typed == 'y')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 break;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004969 if (typed == 'l')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 /* last: replace and then stop */
4972 do_all = FALSE;
4973 line2 = lnum;
4974 break;
4975 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004976 if (typed == 'a')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 do_ask = FALSE;
4979 break;
4980 }
4981#ifdef FEAT_INS_EXPAND
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004982 if (typed == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 scrollup_clamp();
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004984 else if (typed == Ctrl_Y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 scrolldown_clamp();
4986#endif
4987 }
4988 State = save_State;
4989#ifdef FEAT_MOUSE
4990 setmouse();
4991#endif
4992 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4993 --no_u_sync;
4994
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004995 if (typed == 'n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 /* For a multi-line match, put matchcol at the NUL at
4998 * the end of the line and set nmatch to one, so that
4999 * we continue looking for a match on the next line.
Bram Moolenaarc432b502007-03-15 20:38:26 +00005000 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc"
5001 * get stuck when pressing 'n'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (nmatch > 1)
5003 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005004 matchcol = (colnr_T)STRLEN(sub_firstline);
Bram Moolenaarc432b502007-03-15 20:38:26 +00005005 skip_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007 goto skip;
5008 }
5009 if (got_quit)
Bram Moolenaar11cb6e62013-02-06 18:24:02 +01005010 goto skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012
5013 /* Move the cursor to the start of the match, so that we can
5014 * use "\=col("."). */
5015 curwin->w_cursor.col = regmatch.startpos[0].col;
5016
5017 /*
5018 * 3. substitute the string.
5019 */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005020#ifdef FEAT_EVAL
5021 if (do_count)
5022 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005023 /* prevent accidentally changing the buffer by a function */
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005024 save_ma = curbuf->b_p_ma;
5025 curbuf->b_p_ma = FALSE;
5026 sandbox++;
5027 }
5028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 /* get length of substitution part */
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005030 sublen = vim_regsub_multi(&regmatch,
5031 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 sub, sub_firstline, FALSE, p_magic, TRUE);
Bram Moolenaar07e31c52012-08-08 16:51:15 +02005033#ifdef FEAT_EVAL
5034 if (do_count)
5035 {
5036 curbuf->b_p_ma = save_ma;
5037 sandbox--;
5038 goto skip;
5039 }
5040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaar4463f292005-09-25 22:20:24 +00005042 /* When the match included the "$" of the last line it may
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005043 * go beyond the last line of the buffer. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00005044 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
5045 {
5046 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
5047 skip_match = TRUE;
5048 }
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /* Need room for:
5051 * - result so far in new_start (not for first sub in line)
5052 * - original text up to match
5053 * - length of substituted part
5054 * - original text after match
5055 */
5056 if (nmatch == 1)
5057 p1 = sub_firstline;
5058 else
5059 {
5060 p1 = ml_get(sub_firstlnum + nmatch - 1);
5061 nmatch_tl += nmatch - 1;
5062 }
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005063 copy_len = regmatch.startpos[0].col - copycol;
5064 needed_len = copy_len + ((unsigned)STRLEN(p1)
5065 - regmatch.endpos[0].col) + sublen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (new_start == NULL)
5067 {
5068 /*
5069 * Get some space for a temporary buffer to do the
5070 * substitution into (and some extra space to avoid
5071 * too many calls to alloc()/free()).
5072 */
5073 new_start_len = needed_len + 50;
5074 if ((new_start = alloc_check(new_start_len)) == NULL)
5075 goto outofmem;
5076 *new_start = NUL;
5077 new_end = new_start;
5078 }
5079 else
5080 {
5081 /*
5082 * Check if the temporary buffer is long enough to do the
5083 * substitution into. If not, make it larger (with a bit
5084 * extra to avoid too many calls to alloc()/free()).
5085 */
5086 len = (unsigned)STRLEN(new_start);
5087 needed_len += len;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005088 if (needed_len > (int)new_start_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 new_start_len = needed_len + 50;
5091 if ((p1 = alloc_check(new_start_len)) == NULL)
5092 {
5093 vim_free(new_start);
5094 goto outofmem;
5095 }
5096 mch_memmove(p1, new_start, (size_t)(len + 1));
5097 vim_free(new_start);
5098 new_start = p1;
5099 }
5100 new_end = new_start + len;
5101 }
5102
5103 /*
5104 * copy the text up to the part that matched
5105 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005106 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
5107 new_end += copy_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005109 (void)vim_regsub_multi(&regmatch,
5110 sub_firstlnum - regmatch.startpos[0].lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 sub, new_end, TRUE, p_magic, TRUE);
5112 sub_nsubs++;
5113 did_sub = TRUE;
5114
5115 /* Move the cursor to the start of the line, to avoid that it
5116 * is beyond the end of the line after the substitution. */
5117 curwin->w_cursor.col = 0;
5118
5119 /* For a multi-line match, make a copy of the last matched
5120 * line and continue in that one. */
5121 if (nmatch > 1)
5122 {
5123 sub_firstlnum += nmatch - 1;
5124 vim_free(sub_firstline);
5125 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
5126 /* When going beyond the last line, stop substituting. */
5127 if (sub_firstlnum <= line2)
5128 do_again = TRUE;
5129 else
5130 do_all = FALSE;
5131 }
5132
5133 /* Remember next character to be copied. */
5134 copycol = regmatch.endpos[0].col;
5135
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005136 if (skip_match)
5137 {
5138 /* Already hit end of the buffer, sub_firstlnum is one
5139 * less than what it ought to be. */
5140 vim_free(sub_firstline);
5141 sub_firstline = vim_strsave((char_u *)"");
5142 copycol = 0;
5143 }
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /*
5146 * Now the trick is to replace CTRL-M chars with a real line
5147 * break. This would make it impossible to insert a CTRL-M in
5148 * the text. The line break can be avoided by preceding the
5149 * CTRL-M with a backslash. To be able to insert a backslash,
5150 * they must be doubled in the string and are halved here.
5151 * That is Vi compatible.
5152 */
5153 for (p1 = new_end; *p1; ++p1)
5154 {
5155 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156 STRMOVE(p1, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 else if (*p1 == CAR)
5158 {
5159 if (u_inssub(lnum) == OK) /* prepare for undo */
5160 {
5161 *p1 = NUL; /* truncate up to the CR */
5162 ml_append(lnum - 1, new_start,
5163 (colnr_T)(p1 - new_start + 1), FALSE);
5164 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
5165 if (do_ask)
5166 appended_lines(lnum - 1, 1L);
5167 else
5168 {
5169 if (first_line == 0)
5170 first_line = lnum;
5171 last_line = lnum + 1;
5172 }
5173 /* All line numbers increase. */
5174 ++sub_firstlnum;
5175 ++lnum;
5176 ++line2;
5177 /* move the cursor to the new line, like Vi */
5178 ++curwin->w_cursor.lnum;
Bram Moolenaarf9ffd182007-11-20 17:04:29 +00005179 /* copy the rest */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180 STRMOVE(new_start, p1 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 p1 = new_start - 1;
5182 }
5183 }
5184#ifdef FEAT_MBYTE
5185 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005186 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#endif
5188 }
5189
5190 /*
5191 * 4. If do_all is set, find next match.
5192 * Prevent endless loop with patterns that match empty
5193 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
5194 * But ":s/\n/#/" is OK.
5195 */
5196skip:
5197 /* We already know that we did the last subst when we are at
5198 * the end of the line, except that a pattern like
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005199 * "bar\|\nfoo" may match at the NUL. "lnum" can be below
5200 * "line2" when there is a \zs in the pattern after a line
5201 * break. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00005202 lastone = (skip_match
5203 || got_int
5204 || got_quit
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005205 || lnum > line2
Bram Moolenaar8299df92004-07-10 09:47:34 +00005206 || !(do_all || do_again)
5207 || (sub_firstline[matchcol] == NUL && nmatch <= 1
5208 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 nmatch = -1;
5210
5211 /*
5212 * Replace the line in the buffer when needed. This is
5213 * skipped when there are more matches.
5214 * The check for nmatch_tl is needed for when multi-line
5215 * matching must replace the lines before trying to do another
5216 * match, otherwise "\@<=" won't work.
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005217 * When the match starts below where we start searching also
5218 * need to replace the line first (using \zs after \n).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 */
5220 if (lastone
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 || nmatch_tl > 0
5222 || (nmatch = vim_regexec_multi(&regmatch, curwin,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005223 curbuf, sub_firstlnum,
5224 matchcol, NULL)) == 0
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005225 || regmatch.startpos[0].lnum > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (new_start != NULL)
5228 {
5229 /*
5230 * Copy the rest of the line, that didn't match.
5231 * "matchcol" has to be adjusted, we use the end of
5232 * the line as reference, because the substitute may
5233 * have changed the number of characters. Same for
5234 * "prev_matchcol".
5235 */
5236 STRCAT(new_start, sub_firstline + copycol);
5237 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5238 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5239 - prev_matchcol;
5240
5241 if (u_savesub(lnum) != OK)
5242 break;
5243 ml_replace(lnum, new_start, TRUE);
5244
5245 if (nmatch_tl > 0)
5246 {
5247 /*
5248 * Matched lines have now been substituted and are
5249 * useless, delete them. The part after the match
5250 * has been appended to new_start, we don't need
5251 * it in the buffer.
5252 */
5253 ++lnum;
5254 if (u_savedel(lnum, nmatch_tl) != OK)
5255 break;
5256 for (i = 0; i < nmatch_tl; ++i)
5257 ml_delete(lnum, (int)FALSE);
5258 mark_adjust(lnum, lnum + nmatch_tl - 1,
5259 (long)MAXLNUM, -nmatch_tl);
5260 if (do_ask)
5261 deleted_lines(lnum, nmatch_tl);
5262 --lnum;
5263 line2 -= nmatch_tl; /* nr of lines decreases */
5264 nmatch_tl = 0;
5265 }
5266
5267 /* When asking, undo is saved each time, must also set
5268 * changed flag each time. */
5269 if (do_ask)
5270 changed_bytes(lnum, 0);
5271 else
5272 {
5273 if (first_line == 0)
5274 first_line = lnum;
5275 last_line = lnum + 1;
5276 }
5277
5278 sub_firstlnum = lnum;
5279 vim_free(sub_firstline); /* free the temp buffer */
5280 sub_firstline = new_start;
5281 new_start = NULL;
5282 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
5283 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
5284 - prev_matchcol;
5285 copycol = 0;
5286 }
5287 if (nmatch == -1 && !lastone)
5288 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005289 sub_firstlnum, matchcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 /*
5292 * 5. break if there isn't another match in this line
5293 */
5294 if (nmatch <= 0)
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005295 {
5296 /* If the match found didn't start where we were
5297 * searching, do the next search in the line where we
5298 * found the match. */
5299 if (nmatch == -1)
5300 lnum -= regmatch.startpos[0].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 break;
Bram Moolenaarbd7cc032008-01-09 21:40:50 +00005302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304
5305 line_breakcheck();
5306 }
5307
5308 if (did_sub)
5309 ++sub_nlines;
Bram Moolenaarda2bd6f2008-09-14 19:41:30 +00005310 vim_free(new_start); /* for when substitute was cancelled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 vim_free(sub_firstline); /* free the copy of the original line */
5312 sub_firstline = NULL;
5313 }
5314
5315 line_breakcheck();
5316 }
5317
5318 if (first_line != 0)
5319 {
5320 /* Need to subtract the number of added lines from "last_line" to get
5321 * the line number before the change (same as adding the number of
5322 * deleted lines). */
5323 i = curbuf->b_ml.ml_line_count - old_line_count;
5324 changed_lines(first_line, 0, last_line - i, i);
5325 }
5326
5327outofmem:
5328 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005329
5330 /* ":s/pat//n" doesn't move the cursor */
5331 if (do_count)
5332 curwin->w_cursor = old_cursor;
5333
Bram Moolenaar46475522010-03-23 17:36:29 +01005334 if (sub_nsubs > start_nsubs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
5336 /* Set the '[ and '] marks. */
5337 curbuf->b_op_start.lnum = eap->line1;
5338 curbuf->b_op_end.lnum = line2;
5339 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
5340
5341 if (!global_busy)
5342 {
Bram Moolenaar0faaeb82012-03-07 14:57:52 +01005343 if (!do_ask) /* when interactive leave cursor on the match */
5344 {
5345 if (endcolumn)
5346 coladvance((colnr_T)MAXCOL);
5347 else
5348 beginline(BL_WHITE | BL_FIX);
5349 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005350 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 MSG("");
5352 }
5353 else
5354 global_need_beginline = TRUE;
5355 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005356 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358 else if (!global_busy)
5359 {
5360 if (got_int) /* interrupted */
5361 EMSG(_(e_interr));
5362 else if (got_match) /* did find something but nothing substituted */
5363 MSG("");
5364 else if (do_error) /* nothing found */
5365 EMSG2(_(e_patnotf2), get_search_pat());
5366 }
5367
Bram Moolenaar8c4fbd12013-01-17 18:34:05 +01005368#ifdef FEAT_FOLDING
5369 if (do_ask && hasAnyFolding(curwin))
5370 /* Cursor position may require updating */
5371 changed_window_setting();
5372#endif
5373
Bram Moolenaar473de612013-06-08 18:19:48 +02005374 vim_regfree(regmatch.regprog);
Bram Moolenaarcad2fc92015-05-04 10:46:03 +02005375
5376 /* Restore the flag values, they can be used for ":&&". */
5377 do_all = save_do_all;
5378 do_ask = save_do_ask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379}
5380
5381/*
5382 * Give message for number of substitutions.
5383 * Can also be used after a ":global" command.
5384 * Return TRUE if a message was given.
5385 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005386 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00005387do_sub_msg(count_only)
5388 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
5390 /*
5391 * Only report substitutions when:
5392 * - more than 'report' substitutions
5393 * - command was typed by user, or number of changed lines > 'report'
5394 * - giving messages is not disabled by 'lazyredraw'
5395 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005396 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
5397 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 && messaging())
5399 {
5400 if (got_int)
5401 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005402 else
5403 *msg_buf = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 if (sub_nsubs == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005405 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005406 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005408 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar05159a02005-02-26 23:04:13 +00005409 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 sub_nsubs);
5411 if (sub_nlines == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02005412 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005413 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 else
Bram Moolenaara800b422010-06-27 01:15:55 +02005415 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005416 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005419 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 return TRUE;
5421 }
5422 if (got_int)
5423 {
5424 EMSG(_(e_interr));
5425 return TRUE;
5426 }
5427 return FALSE;
5428}
5429
5430/*
5431 * Execute a global command of the form:
5432 *
5433 * g/pattern/X : execute X on all lines where pattern matches
5434 * v/pattern/X : execute X on all lines where pattern does not match
5435 *
5436 * where 'X' is an EX command
5437 *
5438 * The command character (as well as the trailing slash) is optional, and
5439 * is assumed to be 'p' if missing.
5440 *
5441 * This is implemented in two passes: first we scan the file for the pattern and
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02005442 * set a mark for each line that (not) matches. Secondly we execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * for each line that has a mark. This is required because after deleting
5444 * lines we do not know where to search for the next match.
5445 */
5446 void
5447ex_global(eap)
5448 exarg_T *eap;
5449{
5450 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005451 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 int type; /* first char of cmd: 'v' or 'g' */
5453 char_u *cmd; /* command argument */
5454
5455 char_u delim; /* delimiter, normally '/' */
5456 char_u *pat;
5457 regmmatch_T regmatch;
5458 int match;
5459 int which_pat;
5460
5461 if (global_busy)
5462 {
5463 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
5464 return;
5465 }
5466
5467 if (eap->forceit) /* ":global!" is like ":vglobal" */
5468 type = 'v';
5469 else
5470 type = *eap->cmd;
5471 cmd = eap->arg;
5472 which_pat = RE_LAST; /* default: use last used regexp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474 /*
5475 * undocumented vi feature:
5476 * "\/" and "\?": use previous search pattern.
5477 * "\&": use previous substitute pattern.
5478 */
5479 if (*cmd == '\\')
5480 {
5481 ++cmd;
5482 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
5483 {
5484 EMSG(_(e_backslash));
5485 return;
5486 }
5487 if (*cmd == '&')
5488 which_pat = RE_SUBST; /* use previous substitute pattern */
5489 else
5490 which_pat = RE_SEARCH; /* use previous search pattern */
5491 ++cmd;
5492 pat = (char_u *)"";
5493 }
5494 else if (*cmd == NUL)
5495 {
5496 EMSG(_("E148: Regular expression missing from global"));
5497 return;
5498 }
5499 else
5500 {
5501 delim = *cmd; /* get the delimiter */
5502 if (delim)
5503 ++cmd; /* skip delimiter if there is one */
5504 pat = cmd; /* remember start of pattern */
5505 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
5506 if (cmd[0] == delim) /* end delimiter found */
5507 *cmd++ = NUL; /* replace it with a NUL */
5508 }
5509
5510#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5511 if (p_altkeymap && curwin->w_p_rl)
5512 lrFswap(pat,0);
5513#endif
5514
5515 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5516 {
5517 EMSG(_(e_invcmd));
5518 return;
5519 }
5520
5521 /*
5522 * pass 1: set marks for each (not) matching line
5523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5525 {
5526 /* a match on this line? */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005527 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
5528 (colnr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if ((type == 'g' && match) || (type == 'v' && !match))
5530 {
5531 ml_setmarked(lnum);
5532 ndone++;
5533 }
5534 line_breakcheck();
5535 }
5536
5537 /*
5538 * pass 2: execute the command for each line that has been marked
5539 */
5540 if (got_int)
5541 MSG(_(e_interr));
5542 else if (ndone == 0)
5543 {
5544 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005545 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else
Bram Moolenaarc389fd32013-03-07 16:08:35 +01005547 smsg((char_u *)_("Pattern not found: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549 else
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005550 {
5551#ifdef FEAT_CLIPBOARD
5552 start_global_changes();
5553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 global_exe(cmd);
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02005555#ifdef FEAT_CLIPBOARD
5556 end_global_changes();
5557#endif
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar473de612013-06-08 18:19:48 +02005561 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564/*
5565 * Execute "cmd" on lines marked with ml_setmarked().
5566 */
5567 void
5568global_exe(cmd)
5569 char_u *cmd;
5570{
Bram Moolenaarbb993222011-05-10 16:00:47 +02005571 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5572 buf_T *old_buf = curbuf; /* remember what buffer we started in */
5573 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 /*
5576 * Set current position only once for a global command.
5577 * If global_busy is set, setpcmark() will not do anything.
5578 * If there is an error, global_busy will be incremented.
5579 */
5580 setpcmark();
5581
5582 /* When the command writes a message, don't overwrite the command. */
5583 msg_didout = TRUE;
5584
Bram Moolenaard25bc232010-03-23 17:49:24 +01005585 sub_nsubs = 0;
5586 sub_nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 global_need_beginline = FALSE;
5588 global_busy = 1;
5589 old_lcount = curbuf->b_ml.ml_line_count;
5590 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5591 {
5592 curwin->w_cursor.lnum = lnum;
5593 curwin->w_cursor.col = 0;
5594 if (*cmd == NUL || *cmd == '\n')
5595 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5596 else
5597 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5598 ui_breakcheck();
5599 }
5600
5601 global_busy = 0;
5602 if (global_need_beginline)
5603 beginline(BL_WHITE | BL_FIX);
5604 else
5605 check_cursor(); /* cursor may be beyond the end of the line */
5606
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005607 /* the cursor may not have moved in the text but a change in a previous
5608 * line may move it on the screen */
5609 changed_line_abv_curs();
5610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 /* If it looks like no message was written, allow overwriting the
5612 * command with the report for number of changes. */
5613 if (msg_col == 0 && msg_scrolled == 0)
5614 msg_didout = FALSE;
5615
Bram Moolenaar45667512007-05-10 19:24:43 +00005616 /* If substitutes done, report number of substitutes, otherwise report
Bram Moolenaarbb993222011-05-10 16:00:47 +02005617 * number of extra or deleted lines.
5618 * Don't report extra or deleted lines in the edge case where the buffer
5619 * we are in after execution is different from the buffer we started in. */
5620 if (!do_sub_msg(FALSE) && curbuf == old_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5622}
5623
5624#ifdef FEAT_VIMINFO
5625 int
5626read_viminfo_sub_string(virp, force)
5627 vir_T *virp;
5628 int force;
5629{
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005630 if (force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 vim_free(old_sub);
5632 if (force || old_sub == NULL)
5633 old_sub = viminfo_readstring(virp, 1, TRUE);
5634 return viminfo_readline(virp);
5635}
5636
5637 void
5638write_viminfo_sub_string(fp)
5639 FILE *fp;
5640{
5641 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5642 {
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005643 fputs(_("\n# Last Substitute String:\n$"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 viminfo_writestring(fp, old_sub);
5645 }
5646}
5647#endif /* FEAT_VIMINFO */
5648
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005649#if defined(EXITFREE) || defined(PROTO)
5650 void
5651free_old_sub()
5652{
5653 vim_free(old_sub);
5654}
5655#endif
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5658/*
5659 * Set up for a tagpreview.
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005660 * Return TRUE when it was created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005662 int
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005663prepare_tagpreview(undo_sync)
5664 int undo_sync; /* sync undo when leaving the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 win_T *wp;
5667
5668# ifdef FEAT_GUI
5669 need_mouse_correct = TRUE;
5670# endif
5671
5672 /*
5673 * If there is already a preview window open, use that one.
5674 */
5675 if (!curwin->w_p_pvw)
5676 {
5677 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5678 if (wp->w_p_pvw)
5679 break;
5680 if (wp != NULL)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005681 win_enter(wp, undo_sync);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 else
5683 {
5684 /*
5685 * There is no preview window open yet. Create one.
5686 */
5687 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5688 == FAIL)
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005689 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 curwin->w_p_pvw = TRUE;
5691 curwin->w_p_wfh = TRUE;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005692 RESET_BINDING(curwin); /* don't take over 'scrollbind'
5693 and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694# ifdef FEAT_DIFF
5695 curwin->w_p_diff = FALSE; /* no 'diff' */
5696# endif
5697# ifdef FEAT_FOLDING
5698 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5699# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005700 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 }
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00005703 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704}
5705
5706#endif
5707
5708
5709/*
5710 * ":help": open a read-only window on a help file
5711 */
5712 void
5713ex_help(eap)
5714 exarg_T *eap;
5715{
5716 char_u *arg;
5717 char_u *tag;
5718 FILE *helpfd; /* file descriptor of help file */
5719 int n;
5720 int i;
5721#ifdef FEAT_WINDOWS
5722 win_T *wp;
5723#endif
5724 int num_matches;
5725 char_u **matches;
5726 char_u *p;
5727 int empty_fnum = 0;
5728 int alt_fnum = 0;
5729 buf_T *buf;
5730#ifdef FEAT_MULTI_LANG
5731 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005732 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733#endif
Bram Moolenaar8f535582011-09-30 17:30:31 +02005734#ifdef FEAT_FOLDING
5735 int old_KeyTyped = KeyTyped;
5736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 if (eap != NULL)
5739 {
5740 /*
5741 * A ":help" command ends at the first LF, or at a '|' that is
5742 * followed by some text. Set nextcmd to the following command.
5743 */
5744 for (arg = eap->arg; *arg; ++arg)
5745 {
5746 if (*arg == '\n' || *arg == '\r'
5747 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5748 {
5749 *arg++ = NUL;
5750 eap->nextcmd = arg;
5751 break;
5752 }
5753 }
5754 arg = eap->arg;
5755
Bram Moolenaar3dbde622012-04-05 16:05:05 +02005756 if (eap->forceit && *arg == NUL && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 EMSG(_("E478: Don't panic!"));
5759 return;
5760 }
5761
5762 if (eap->skip) /* not executing commands */
5763 return;
5764 }
5765 else
5766 arg = (char_u *)"";
5767
5768 /* remove trailing blanks */
5769 p = arg + STRLEN(arg) - 1;
5770 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5771 *p-- = NUL;
5772
5773#ifdef FEAT_MULTI_LANG
5774 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005775 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#endif
5777
5778 /* When no argument given go to the index. */
5779 if (*arg == NUL)
5780 arg = (char_u *)"help.txt";
5781
5782 /*
5783 * Check if there is a match for the argument.
5784 */
5785 n = find_help_tags(arg, &num_matches, &matches,
5786 eap != NULL && eap->forceit);
5787
5788 i = 0;
5789#ifdef FEAT_MULTI_LANG
5790 if (n != FAIL && lang != NULL)
5791 /* Find first item with the requested language. */
5792 for (i = 0; i < num_matches; ++i)
5793 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005794 len = (int)STRLEN(matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 if (len > 3 && matches[i][len - 3] == '@'
5796 && STRICMP(matches[i] + len - 2, lang) == 0)
5797 break;
5798 }
5799#endif
5800 if (i >= num_matches || n == FAIL)
5801 {
5802#ifdef FEAT_MULTI_LANG
5803 if (lang != NULL)
5804 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5805 else
5806#endif
5807 EMSG2(_("E149: Sorry, no help for %s"), arg);
5808 if (n != FAIL)
5809 FreeWild(num_matches, matches);
5810 return;
5811 }
5812
5813 /* The first match (in the requested language) is the best match. */
5814 tag = vim_strsave(matches[i]);
5815 FreeWild(num_matches, matches);
5816
5817#ifdef FEAT_GUI
5818 need_mouse_correct = TRUE;
5819#endif
5820
5821 /*
5822 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005823 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005825 if (!curwin->w_buffer->b_help
5826#ifdef FEAT_WINDOWS
5827 || cmdmod.tab != 0
5828#endif
5829 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
5831#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005832 if (cmdmod.tab != 0)
5833 wp = NULL;
5834 else
5835 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5836 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5839 win_enter(wp, TRUE);
5840 else
5841#endif
5842 {
5843 /*
5844 * There is no help window yet.
5845 * Try to open the file specified by the "helpfile" option.
5846 */
5847 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5848 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005849 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 goto erret;
5851 }
5852 fclose(helpfd);
5853
5854#ifdef FEAT_WINDOWS
5855 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005856 * specified, the current window is vertically split and
5857 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 n = WSP_HELP;
5859# ifdef FEAT_VERTSPLIT
5860 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005861 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 n |= WSP_TOP;
5863# endif
5864 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005865 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866#else
5867 /* use current window */
5868 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
5872#ifdef FEAT_WINDOWS
5873 if (curwin->w_height < p_hh)
5874 win_setheight((int)p_hh);
5875#endif
5876
5877 /*
5878 * Open help file (do_ecmd() will set b_help flag, readfile() will
5879 * set b_p_ro flag).
5880 * Set the alternate file to the previously edited file.
5881 */
5882 alt_fnum = curbuf->b_fnum;
5883 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005884 ECMD_HIDE + ECMD_SET_HELP,
5885#ifdef FEAT_WINDOWS
5886 NULL /* buffer is still open, don't store info */
5887#else
5888 curwin
5889#endif
5890 );
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005891 if (!cmdmod.keepalt)
5892 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 empty_fnum = curbuf->b_fnum;
5894 }
5895 }
5896
5897 if (!p_im)
5898 restart_edit = 0; /* don't want insert mode in help file */
5899
Bram Moolenaar8f535582011-09-30 17:30:31 +02005900#ifdef FEAT_FOLDING
5901 /* Restore KeyTyped, setting 'filetype=help' may reset it.
5902 * It is needed for do_tag top open folds under the cursor. */
5903 KeyTyped = old_KeyTyped;
5904#endif
5905
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (tag != NULL)
5907 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5908
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005909 /* Delete the empty buffer if we're not using it. Careful: autocommands
5910 * may have jumped to another window, check that the buffer is not in a
5911 * window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5913 {
5914 buf = buflist_findnr(empty_fnum);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005915 if (buf != NULL && buf->b_nwindows == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 wipe_buffer(buf, TRUE);
5917 }
5918
5919 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005920 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 curwin->w_alt_fnum = alt_fnum;
5922
5923erret:
5924 vim_free(tag);
5925}
5926
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005927/*
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005928 * ":helpclose": Close one help window
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005929 */
5930 void
5931ex_helpclose(eap)
5932 exarg_T *eap UNUSED;
5933{
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005934#if defined(FEAT_WINDOWS)
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005935 win_T *win;
5936
5937 FOR_ALL_WINDOWS(win)
5938 {
5939 if (win->w_buffer->b_help)
5940 {
5941 win_close(win, FALSE);
Bram Moolenaareb21e4c2014-09-19 22:05:53 +02005942 return;
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005943 }
5944 }
Bram Moolenaar3fa57e02014-09-19 22:23:26 +02005945#endif
Bram Moolenaar5bfa2ed2014-09-19 19:39:34 +02005946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005948#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5949/*
5950 * In an argument search for a language specifiers in the form "@xx".
5951 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5952 * Returns NULL if not found.
5953 */
5954 char_u *
5955check_help_lang(arg)
5956 char_u *arg;
5957{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005958 int len = (int)STRLEN(arg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005959
5960 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
5961 && ASCII_ISALPHA(arg[len - 1]))
5962 {
5963 arg[len - 3] = NUL; /* remove the '@' */
5964 return arg + len - 2;
5965 }
5966 return NULL;
5967}
5968#endif
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970/*
5971 * Return a heuristic indicating how well the given string matches. The
5972 * smaller the number, the better the match. This is the order of priorities,
5973 * from best match to worst match:
5974 * - Match with least alpha-numeric characters is better.
5975 * - Match with least total characters is better.
5976 * - Match towards the start is better.
5977 * - Match starting with "+" is worse (feature instead of command)
5978 * Assumption is made that the matched_string passed has already been found to
5979 * match some string for which help is requested. webb.
5980 */
5981 int
5982help_heuristic(matched_string, offset, wrong_case)
5983 char_u *matched_string;
5984 int offset; /* offset for match */
5985 int wrong_case; /* no matching case */
5986{
5987 int num_letters;
5988 char_u *p;
5989
5990 num_letters = 0;
5991 for (p = matched_string; *p; p++)
5992 if (ASCII_ISALNUM(*p))
5993 num_letters++;
5994
5995 /*
5996 * Multiply the number of letters by 100 to give it a much bigger
5997 * weighting than the number of characters.
5998 * If there only is a match while ignoring case, add 5000.
5999 * If the match starts in the middle of a word, add 10000 to put it
6000 * somewhere in the last half.
6001 * If the match is more than 2 chars from the start, multiply by 200 to
6002 * put it after matches at the start.
6003 */
6004 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
6005 && ASCII_ISALNUM(matched_string[offset - 1]))
6006 offset += 10000;
6007 else if (offset > 2)
6008 offset *= 200;
6009 if (wrong_case)
6010 offset += 5000;
6011 /* Features are less interesting than the subjects themselves, but "+"
6012 * alone is not a feature. */
6013 if (matched_string[0] == '+' && matched_string[1] != NUL)
6014 offset += 100;
6015 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
6016}
6017
6018/*
6019 * Compare functions for qsort() below, that checks the help heuristics number
6020 * that has been put after the tagname by find_tags().
6021 */
6022 static int
6023#ifdef __BORLANDC__
6024_RTLENTRYF
6025#endif
6026help_compare(s1, s2)
6027 const void *s1;
6028 const void *s2;
6029{
6030 char *p1;
6031 char *p2;
6032
6033 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
6034 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
6035 return strcmp(p1, p2);
6036}
6037
6038/*
6039 * Find all help tags matching "arg", sort them and return in matches[], with
6040 * the number of matches in num_matches.
6041 * The matches will be sorted with a "best" match algorithm.
6042 * When "keep_lang" is TRUE try keeping the language of the current buffer.
6043 */
6044 int
6045find_help_tags(arg, num_matches, matches, keep_lang)
6046 char_u *arg;
6047 int *num_matches;
6048 char_u ***matches;
6049 int keep_lang;
6050{
6051 char_u *s, *d;
6052 int i;
6053 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006054 "/*", "/\\*", "\"*", "**",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006055 "cpo-*", "/\\(\\)", "/\\%(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006056 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006057 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 "[count]", "[quotex]", "[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006059 "[pattern]", "\\|", "\\%$",
6060 "s/\\~", "s/\\U", "s/\\L",
6061 "s/\\1", "s/\\2", "s/\\3", "s/\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00006063 "/star", "/\\\\star", "quotestar", "starstar",
Bram Moolenaarc528b1d2013-08-03 13:41:15 +02006064 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006065 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006066 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 "\\[count]", "\\[quotex]", "\\[range]",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006068 "\\[pattern]", "\\\\bar", "/\\\\%\\$",
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006069 "s/\\\\\\~", "s/\\\\U", "s/\\\\L",
Bram Moolenaarc467d9b2014-02-11 12:15:43 +01006070 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int flags;
6072
6073 d = IObuff; /* assume IObuff is long enough! */
6074
6075 /*
6076 * Recognize a few exceptions to the rule. Some strings that contain '*'
6077 * with "star". Otherwise '*' is recognized as a wildcard.
6078 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006079 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (STRCMP(arg, mtable[i]) == 0)
6081 {
6082 STRCPY(d, rtable[i]);
6083 break;
6084 }
6085
6086 if (i < 0) /* no match in table */
6087 {
6088 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
6089 * Also replace "\%^" and "\%(", they match every tag too.
6090 * Also "\zs", "\z1", etc.
6091 * Also "\@<", "\@=", "\@<=", etc.
6092 * And also "\_$" and "\_^". */
6093 if (arg[0] == '\\'
6094 && ((arg[1] != NUL && arg[2] == NUL)
6095 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
6096 && arg[2] != NUL)))
6097 {
6098 STRCPY(d, "/\\\\");
6099 STRCPY(d + 3, arg + 1);
6100 /* Check for "/\\_$", should be "/\\_\$" */
6101 if (d[3] == '_' && d[4] == '$')
6102 STRCPY(d + 4, "\\$");
6103 }
6104 else
6105 {
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006106 /* Replace:
6107 * "[:...:]" with "\[:...:]"
6108 * "[++...]" with "\[++...]"
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006109 * "\{" with "\\{" -- matching "} \}"
Bram Moolenaar7c0a86b2012-09-05 15:15:07 +02006110 */
6111 if ((arg[0] == '[' && (arg[1] == ':'
6112 || (arg[1] == '+' && arg[2] == '+')))
6113 || (arg[0] == '\\' && arg[1] == '{'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 *d++ = '\\';
6115
6116 for (s = arg; *s; ++s)
6117 {
6118 /*
6119 * Replace "|" with "bar" and '"' with "quote" to match the name of
6120 * the tags for these commands.
6121 * Replace "*" with ".*" and "?" with "." to match command line
6122 * completion.
6123 * Insert a backslash before '~', '$' and '.' to avoid their
6124 * special meaning.
6125 */
6126 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
6127 break;
6128 switch (*s)
6129 {
6130 case '|': STRCPY(d, "bar");
6131 d += 3;
6132 continue;
6133 case '"': STRCPY(d, "quote");
6134 d += 5;
6135 continue;
6136 case '*': *d++ = '.';
6137 break;
6138 case '?': *d++ = '.';
6139 continue;
6140 case '$':
6141 case '.':
6142 case '~': *d++ = '\\';
6143 break;
6144 }
6145
6146 /*
6147 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
6148 * ":help i_^_CTRL-D" work.
6149 * Insert '-' before and after "CTRL-X" when applicable.
6150 */
6151 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
6152 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
6153 {
Bram Moolenaard82db602013-08-07 15:24:41 +02006154 if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
6155 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 STRCPY(d, "CTRL-");
6157 d += 5;
6158 if (*s < ' ')
6159 {
6160#ifdef EBCDIC
6161 *d++ = CtrlChar(*s);
6162#else
6163 *d++ = *s + '@';
6164#endif
6165 if (d[-1] == '\\')
6166 *d++ = '\\'; /* double a backslash */
6167 }
6168 else
6169 *d++ = *++s;
6170 if (s[1] != NUL && s[1] != '_')
6171 *d++ = '_'; /* append a '_' */
6172 continue;
6173 }
6174 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
6175 *d++ = '\\';
6176
6177 /*
6178 * Insert a backslash before a backslash after a slash, for search
6179 * pattern tags: "/\|" --> "/\\|".
6180 */
6181 else if (s[0] == '\\' && s[1] != '\\'
6182 && *arg == '/' && s == arg + 1)
6183 *d++ = '\\';
6184
6185 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
6186 * "CTRL-\_CTRL-N" */
6187 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
6188 {
6189 STRCPY(d, "CTRL-\\\\");
6190 d += 7;
6191 s += 6;
6192 }
6193
6194 *d++ = *s;
6195
6196 /*
6197 * If tag starts with ', toss everything after a second '. Fixes
6198 * CTRL-] on 'option'. (would include the trailing '.').
6199 */
6200 if (*s == '\'' && s > arg && *arg == '\'')
6201 break;
6202 }
6203 *d = NUL;
Bram Moolenaar720ce532012-04-25 12:57:28 +02006204
6205 if (*IObuff == '`')
6206 {
6207 if (d > IObuff + 2 && d[-1] == '`')
6208 {
6209 /* remove the backticks from `command` */
6210 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6211 d[-2] = NUL;
6212 }
6213 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
6214 {
6215 /* remove the backticks and comma from `command`, */
6216 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6217 d[-3] = NUL;
6218 }
6219 else if (d > IObuff + 4 && d[-3] == '`'
6220 && d[-2] == '\\' && d[-1] == '.')
6221 {
6222 /* remove the backticks and dot from `command`\. */
6223 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
6224 d[-4] = NUL;
6225 }
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228 }
6229
6230 *matches = (char_u **)"";
6231 *num_matches = 0;
6232 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
6233 if (keep_lang)
6234 flags |= TAG_KEEP_LANG;
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006235 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 && *num_matches > 0)
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 /* Sort the matches found on the heuristic number that is after the
6239 * tag name. */
6240 qsort((void *)*matches, (size_t)*num_matches,
6241 sizeof(char_u *), help_compare);
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00006242 /* Delete more than TAG_MANY to reduce the size of the listing. */
6243 while (*num_matches > TAG_MANY)
6244 vim_free((*matches)[--*num_matches]);
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 return OK;
6247}
6248
6249/*
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006250 * Called when starting to edit a buffer for a help file.
6251 */
6252 static void
6253prepare_help_buffer()
6254{
6255 char_u *p;
6256
6257 curbuf->b_help = TRUE;
6258#ifdef FEAT_QUICKFIX
6259 set_string_option_direct((char_u *)"buftype", -1,
6260 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
6261#endif
6262
6263 /*
6264 * Always set these options after jumping to a help tag, because the
6265 * user may have an autocommand that gets in the way.
6266 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
6267 * latin1 word characters (for translated help files).
6268 * Only set it when needed, buf_init_chartab() is some work.
6269 */
6270 p =
6271#ifdef EBCDIC
6272 (char_u *)"65-255,^*,^|,^\"";
6273#else
6274 (char_u *)"!-~,^*,^|,^\",192-255";
6275#endif
6276 if (STRCMP(curbuf->b_p_isk, p) != 0)
6277 {
6278 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
6279 check_buf_options(curbuf);
6280 (void)buf_init_chartab(curbuf, FALSE);
6281 }
6282
Bram Moolenaar0b105412014-11-30 13:34:23 +01006283#ifdef FEAT_FOLDING
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006284 /* Don't use the global foldmethod.*/
6285 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
6286 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar0b105412014-11-30 13:34:23 +01006287#endif
Bram Moolenaar54fb4382014-11-12 19:28:16 +01006288
6289 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
6290 curwin->w_p_list = FALSE; /* no list mode */
6291
6292 curbuf->b_p_ma = FALSE; /* not modifiable */
6293 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
6294 curwin->w_p_nu = 0; /* no line numbers */
6295 curwin->w_p_rnu = 0; /* no relative line numbers */
6296 RESET_BINDING(curwin); /* no scroll or cursor binding */
6297#ifdef FEAT_ARABIC
6298 curwin->w_p_arab = FALSE; /* no arabic mode */
6299#endif
6300#ifdef FEAT_RIGHTLEFT
6301 curwin->w_p_rl = FALSE; /* help window is left-to-right */
6302#endif
6303#ifdef FEAT_FOLDING
6304 curwin->w_p_fen = FALSE; /* No folding in the help window */
6305#endif
6306#ifdef FEAT_DIFF
6307 curwin->w_p_diff = FALSE; /* No 'diff' */
6308#endif
6309#ifdef FEAT_SPELL
6310 curwin->w_p_spell = FALSE; /* No spell checking */
6311#endif
6312
6313 set_buflisted(FALSE);
6314}
6315
6316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 * After reading a help file: May cleanup a help buffer when syntax
6318 * highlighting is not used.
6319 */
6320 void
6321fix_help_buffer()
6322{
6323 linenr_T lnum;
6324 char_u *line;
6325 int in_example = FALSE;
6326 int len;
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006327 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 char_u *p;
6329 char_u *rt;
6330 int mustfree;
6331
6332 /* set filetype to "help". */
6333 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6334
6335#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006336 if (!syntax_present(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337#endif
6338 {
6339 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6340 {
6341 line = ml_get_buf(curbuf, lnum, FALSE);
6342 len = (int)STRLEN(line);
6343 if (in_example && len > 0 && !vim_iswhite(line[0]))
6344 {
6345 /* End of example: non-white or '<' in first column. */
6346 if (line[0] == '<')
6347 {
6348 /* blank-out a '<' in the first column */
6349 line = ml_get_buf(curbuf, lnum, TRUE);
6350 line[0] = ' ';
6351 }
6352 in_example = FALSE;
6353 }
6354 if (!in_example && len > 0)
6355 {
6356 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
6357 {
6358 /* blank-out a '>' in the last column (start of example) */
6359 line = ml_get_buf(curbuf, lnum, TRUE);
6360 line[len - 1] = ' ';
6361 in_example = TRUE;
6362 }
6363 else if (line[len - 1] == '~')
6364 {
6365 /* blank-out a '~' at the end of line (header marker) */
6366 line = ml_get_buf(curbuf, lnum, TRUE);
6367 line[len - 1] = ' ';
6368 }
6369 }
6370 }
6371 }
6372
6373 /*
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006374 * In the "help.txt" and "help.abx" file, add the locally added help
6375 * files. This uses the very first line in the help file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 */
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006377 fname = gettail(curbuf->b_fname);
6378 if (fnamecmp(fname, "help.txt") == 0
6379#ifdef FEAT_MULTI_LANG
6380 || (fnamencmp(fname, "help.", 5) == 0
6381 && ASCII_ISALPHA(fname[5])
6382 && ASCII_ISALPHA(fname[6])
6383 && TOLOWER_ASC(fname[7]) == 'x'
6384 && fname[8] == NUL)
6385#endif
6386 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 {
6388 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
6389 {
6390 line = ml_get_buf(curbuf, lnum, FALSE);
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006391 if (strstr((char *)line, "*local-additions*") == NULL)
6392 continue;
6393
6394 /* Go through all directories in 'runtimepath', skipping
6395 * $VIMRUNTIME. */
6396 p = p_rtp;
6397 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006399 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6400 mustfree = FALSE;
6401 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
6402 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 {
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006404 int fcount;
6405 char_u **fnames;
6406 FILE *fd;
6407 char_u *s;
6408 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409#ifdef FEAT_MBYTE
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006410 vimconv_T vc;
6411 char_u *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412#endif
6413
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006414 /* Find all "doc/ *.txt" files in this directory. */
6415 add_pathsep(NameBuff);
6416#ifdef FEAT_MULTI_LANG
6417 STRCAT(NameBuff, "doc/*.??[tx]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#else
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006419 STRCAT(NameBuff, "doc/*.txt");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420#endif
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006421 if (gen_expand_wildcards(1, &NameBuff, &fcount,
6422 &fnames, EW_FILE|EW_SILENT) == OK
6423 && fcount > 0)
6424 {
6425#ifdef FEAT_MULTI_LANG
6426 int i1;
6427 int i2;
6428 char_u *f1;
6429 char_u *f2;
6430 char_u *t1;
6431 char_u *e1;
6432 char_u *e2;
6433
6434 /* If foo.abx is found use it instead of foo.txt in
6435 * the same directory. */
6436 for (i1 = 0; i1 < fcount; ++i1)
6437 {
6438 for (i2 = 0; i2 < fcount; ++i2)
6439 {
6440 if (i1 == i2)
6441 continue;
6442 if (fnames[i1] == NULL || fnames[i2] == NULL)
6443 continue;
6444 f1 = fnames[i1];
6445 f2 = fnames[i2];
6446 t1 = gettail(f1);
6447 if (fnamencmp(f1, f2, t1 - f1) != 0)
6448 continue;
6449 e1 = vim_strrchr(t1, '.');
6450 e2 = vim_strrchr(gettail(f2), '.');
6451 if (e1 == NUL || e2 == NUL)
6452 continue;
6453 if (fnamecmp(e1, ".txt") != 0
6454 && fnamecmp(e1, fname + 4) != 0)
6455 {
6456 /* Not .txt and not .abx, remove it. */
6457 vim_free(fnames[i1]);
6458 fnames[i1] = NULL;
6459 continue;
6460 }
6461 if (fnamencmp(f1, f2, e1 - f1) != 0)
6462 continue;
6463 if (fnamecmp(e1, ".txt") == 0
6464 && fnamecmp(e2, fname + 4) == 0)
6465 {
6466 /* use .abx instead of .txt */
6467 vim_free(fnames[i1]);
6468 fnames[i1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006472#endif
6473 for (fi = 0; fi < fcount; ++fi)
6474 {
6475 if (fnames[fi] == NULL)
6476 continue;
6477 fd = mch_fopen((char *)fnames[fi], "r");
6478 if (fd != NULL)
6479 {
6480 vim_fgets(IObuff, IOSIZE, fd);
6481 if (IObuff[0] == '*'
6482 && (s = vim_strchr(IObuff + 1, '*'))
6483 != NULL)
6484 {
6485#ifdef FEAT_MBYTE
6486 int this_utf = MAYBE;
6487#endif
6488 /* Change tag definition to a
6489 * reference and remove <CR>/<NL>. */
6490 IObuff[0] = '|';
6491 *s = '|';
6492 while (*s != NUL)
6493 {
6494 if (*s == '\r' || *s == '\n')
6495 *s = NUL;
6496#ifdef FEAT_MBYTE
6497 /* The text is utf-8 when a byte
6498 * above 127 is found and no
6499 * illegal byte sequence is found.
6500 */
6501 if (*s >= 0x80 && this_utf != FALSE)
6502 {
6503 int l;
6504
6505 this_utf = TRUE;
6506 l = utf_ptr2len(s);
6507 if (l == 1)
6508 this_utf = FALSE;
6509 s += l - 1;
6510 }
6511#endif
6512 ++s;
6513 }
6514#ifdef FEAT_MBYTE
6515 /* The help file is latin1 or utf-8;
6516 * conversion to the current
6517 * 'encoding' may be required. */
6518 vc.vc_type = CONV_NONE;
6519 convert_setup(&vc, (char_u *)(
6520 this_utf == TRUE ? "utf-8"
6521 : "latin1"), p_enc);
6522 if (vc.vc_type == CONV_NONE)
6523 /* No conversion needed. */
6524 cp = IObuff;
6525 else
6526 {
6527 /* Do the conversion. If it fails
6528 * use the unconverted text. */
6529 cp = string_convert(&vc, IObuff,
6530 NULL);
6531 if (cp == NULL)
6532 cp = IObuff;
6533 }
6534 convert_setup(&vc, NULL, NULL);
6535
6536 ml_append(lnum, cp, (colnr_T)0, FALSE);
6537 if (cp != IObuff)
6538 vim_free(cp);
6539#else
6540 ml_append(lnum, IObuff, (colnr_T)0,
6541 FALSE);
6542#endif
6543 ++lnum;
6544 }
6545 fclose(fd);
6546 }
6547 }
6548 FreeWild(fcount, fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006551 if (mustfree)
6552 vim_free(rt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
Bram Moolenaar667b4d22011-10-20 18:17:42 +02006554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556 }
6557}
6558
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006559/*
6560 * ":exusage"
6561 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006562 void
6563ex_exusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006564 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006565{
6566 do_cmdline_cmd((char_u *)"help ex-cmd-index");
6567}
6568
6569/*
6570 * ":viusage"
6571 */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006572 void
6573ex_viusage(eap)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00006574 exarg_T *eap UNUSED;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006575{
6576 do_cmdline_cmd((char_u *)"help normal-index");
6577}
6578
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579#if defined(FEAT_EX_EXTRA) || defined(PROTO)
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006580static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang, int add_help_tags));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582/*
6583 * ":helptags"
6584 */
6585 void
6586ex_helptags(eap)
6587 exarg_T *eap;
6588{
6589 garray_T ga;
6590 int i, j;
6591 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006592#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006594#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006595 expand_T xpc;
6596 char_u *dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 char_u ext[5];
6598 char_u fname[8];
6599 int filecount;
6600 char_u **files;
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006601 int add_help_tags = FALSE;
6602
6603 /* Check for ":helptags ++t {dir}". */
6604 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3]))
6605 {
6606 add_help_tags = TRUE;
6607 eap->arg = skipwhite(eap->arg + 3);
6608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
Bram Moolenaar0e253142008-01-13 12:31:34 +00006610 ExpandInit(&xpc);
6611 xpc.xp_context = EXPAND_DIRECTORIES;
6612 dirname = ExpandOne(&xpc, eap->arg, NULL,
6613 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
6614 if (dirname == NULL || !mch_isdir(dirname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 {
6616 EMSG2(_("E150: Not a directory: %s"), eap->arg);
Bram Moolenaar1c2836e2015-11-10 21:05:48 +01006617 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 return;
6619 }
6620
6621#ifdef FEAT_MULTI_LANG
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006622 /* Get a list of all files in the help directory and in subdirectories. */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006623 STRCPY(NameBuff, dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 add_pathsep(NameBuff);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006625 STRCAT(NameBuff, "**");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6627 EW_FILE|EW_SILENT) == FAIL
6628 || filecount == 0)
6629 {
6630 EMSG2("E151: No match: %s", NameBuff);
Bram Moolenaar0e253142008-01-13 12:31:34 +00006631 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 return;
6633 }
6634
6635 /* Go over all files in the directory to find out what languages are
6636 * present. */
6637 ga_init2(&ga, 1, 10);
6638 for (i = 0; i < filecount; ++i)
6639 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006640 len = (int)STRLEN(files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (len > 4)
6642 {
6643 if (STRICMP(files[i] + len - 4, ".txt") == 0)
6644 {
6645 /* ".txt" -> language "en" */
6646 lang[0] = 'e';
6647 lang[1] = 'n';
6648 }
6649 else if (files[i][len - 4] == '.'
6650 && ASCII_ISALPHA(files[i][len - 3])
6651 && ASCII_ISALPHA(files[i][len - 2])
6652 && TOLOWER_ASC(files[i][len - 1]) == 'x')
6653 {
6654 /* ".abx" -> language "ab" */
6655 lang[0] = TOLOWER_ASC(files[i][len - 3]);
6656 lang[1] = TOLOWER_ASC(files[i][len - 2]);
6657 }
6658 else
6659 continue;
6660
6661 /* Did we find this language already? */
6662 for (j = 0; j < ga.ga_len; j += 2)
6663 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
6664 break;
6665 if (j == ga.ga_len)
6666 {
6667 /* New language, add it. */
6668 if (ga_grow(&ga, 2) == FAIL)
6669 break;
6670 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
6671 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 }
6674 }
6675
6676 /*
6677 * Loop over the found languages to generate a tags file for each one.
6678 */
6679 for (j = 0; j < ga.ga_len; j += 2)
6680 {
6681 STRCPY(fname, "tags-xx");
6682 fname[5] = ((char_u *)ga.ga_data)[j];
6683 fname[6] = ((char_u *)ga.ga_data)[j + 1];
6684 if (fname[5] == 'e' && fname[6] == 'n')
6685 {
6686 /* English is an exception: use ".txt" and "tags". */
6687 fname[4] = NUL;
6688 STRCPY(ext, ".txt");
6689 }
6690 else
6691 {
6692 /* Language "ab" uses ".abx" and "tags-ab". */
6693 STRCPY(ext, ".xxx");
6694 ext[1] = fname[5];
6695 ext[2] = fname[6];
6696 }
Bram Moolenaar0e253142008-01-13 12:31:34 +00006697 helptags_one(dirname, ext, fname, add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699
6700 ga_clear(&ga);
6701 FreeWild(filecount, files);
6702
6703#else
6704 /* No language support, just use "*.txt" and "tags". */
Bram Moolenaar0e253142008-01-13 12:31:34 +00006705 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706#endif
Bram Moolenaar0e253142008-01-13 12:31:34 +00006707 vim_free(dirname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708}
6709
6710 static void
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006711helptags_one(dir, ext, tagfname, add_help_tags)
6712 char_u *dir; /* doc directory */
6713 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006714 char_u *tagfname; /* "tags" for English, "tags-fr" for French. */
6715 int add_help_tags; /* add "help-tags" tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 FILE *fd_tags;
6718 FILE *fd;
6719 garray_T ga;
6720 int filecount;
6721 char_u **files;
6722 char_u *p1, *p2;
6723 int fi;
6724 char_u *s;
6725 int i;
6726 char_u *fname;
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006727 int dirlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728# ifdef FEAT_MBYTE
6729 int utf8 = MAYBE;
6730 int this_utf8;
6731 int firstline;
6732 int mix = FALSE; /* detected mixed encodings */
6733# endif
6734
6735 /*
6736 * Find all *.txt files.
6737 */
Bram Moolenaar862cfa32012-11-29 20:10:00 +01006738 dirlen = (int)STRLEN(dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 STRCPY(NameBuff, dir);
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006740 STRCAT(NameBuff, "/**/*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 STRCAT(NameBuff, ext);
6742 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
6743 EW_FILE|EW_SILENT) == FAIL
6744 || filecount == 0)
6745 {
6746 if (!got_int)
6747 EMSG2("E151: No match: %s", NameBuff);
6748 return;
6749 }
6750
6751 /*
6752 * Open the tags file for writing.
6753 * Do this before scanning through all the files.
6754 */
6755 STRCPY(NameBuff, dir);
6756 add_pathsep(NameBuff);
6757 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006758 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 if (fd_tags == NULL)
6760 {
6761 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6762 FreeWild(filecount, files);
6763 return;
6764 }
6765
6766 /*
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006767 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc"
6768 * add the "help-tags" tag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 */
6770 ga_init2(&ga, (int)sizeof(char_u *), 100);
Bram Moolenaar426e5c92008-01-11 20:02:02 +00006771 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
6772 dir, FALSE) == FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
6774 if (ga_grow(&ga, 1) == FAIL)
6775 got_int = TRUE;
6776 else
6777 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006778 s = alloc(18 + (unsigned)STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (s == NULL)
6780 got_int = TRUE;
6781 else
6782 {
6783 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6784 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6785 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 }
6787 }
6788 }
6789
6790 /*
6791 * Go over all the files and extract the tags.
6792 */
6793 for (fi = 0; fi < filecount && !got_int; ++fi)
6794 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006795 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if (fd == NULL)
6797 {
6798 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6799 continue;
6800 }
Bram Moolenaar442b5c42012-11-28 16:06:22 +01006801 fname = files[fi] + dirlen + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803# ifdef FEAT_MBYTE
6804 firstline = TRUE;
6805# endif
6806 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6807 {
6808# ifdef FEAT_MBYTE
6809 if (firstline)
6810 {
6811 /* Detect utf-8 file by a non-ASCII char in the first line. */
6812 this_utf8 = MAYBE;
6813 for (s = IObuff; *s != NUL; ++s)
6814 if (*s >= 0x80)
6815 {
6816 int l;
6817
6818 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006819 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (l == 1)
6821 {
6822 /* Illegal UTF-8 byte sequence. */
6823 this_utf8 = FALSE;
6824 break;
6825 }
6826 s += l - 1;
6827 }
6828 if (this_utf8 == MAYBE) /* only ASCII characters found */
6829 this_utf8 = FALSE;
6830 if (utf8 == MAYBE) /* first file */
6831 utf8 = this_utf8;
6832 else if (utf8 != this_utf8)
6833 {
6834 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6835 mix = !got_int;
6836 got_int = TRUE;
6837 }
6838 firstline = FALSE;
6839 }
6840# endif
6841 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6842 while (p1 != NULL)
6843 {
Bram Moolenaara0149c72012-05-18 16:24:11 +02006844 /* Use vim_strbyte() instead of vim_strchr() so that when
6845 * 'encoding' is dbcs it still works, don't find '*' in the
6846 * second byte. */
6847 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6849 {
6850 for (s = p1 + 1; s < p2; ++s)
6851 if (*s == ' ' || *s == '\t' || *s == '|')
6852 break;
6853
6854 /*
6855 * Only accept a *tag* when it consists of valid
6856 * characters, there is white space before it and is
6857 * followed by a white character or end-of-line.
6858 */
6859 if (s == p2
6860 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6861 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6862 || s[1] == '\0'))
6863 {
6864 *p2 = '\0';
6865 ++p1;
6866 if (ga_grow(&ga, 1) == FAIL)
6867 {
6868 got_int = TRUE;
6869 break;
6870 }
6871 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6872 if (s == NULL)
6873 {
6874 got_int = TRUE;
6875 break;
6876 }
6877 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6878 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 sprintf((char *)s, "%s\t%s", p1, fname);
6880
6881 /* find next '*' */
6882 p2 = vim_strchr(p2 + 1, '*');
6883 }
6884 }
6885 p1 = p2;
6886 }
6887 line_breakcheck();
6888 }
6889
6890 fclose(fd);
6891 }
6892
6893 FreeWild(filecount, files);
6894
6895 if (!got_int)
6896 {
6897 /*
6898 * Sort the tags.
6899 */
Bram Moolenaarcde88542015-08-11 19:14:00 +02006900 if (ga.ga_data != NULL)
6901 sort_strings((char_u **)ga.ga_data, ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903 /*
6904 * Check for duplicates.
6905 */
6906 for (i = 1; i < ga.ga_len; ++i)
6907 {
6908 p1 = ((char_u **)ga.ga_data)[i - 1];
6909 p2 = ((char_u **)ga.ga_data)[i];
6910 while (*p1 == *p2)
6911 {
6912 if (*p2 == '\t')
6913 {
6914 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006915 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6917 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6918 EMSG(NameBuff);
6919 *p2 = '\t';
6920 break;
6921 }
6922 ++p1;
6923 ++p2;
6924 }
6925 }
6926
6927# ifdef FEAT_MBYTE
6928 if (utf8 == TRUE)
6929 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6930# endif
6931
6932 /*
6933 * Write the tags into the file.
6934 */
6935 for (i = 0; i < ga.ga_len; ++i)
6936 {
6937 s = ((char_u **)ga.ga_data)[i];
Bram Moolenaarf6210482007-07-25 20:56:39 +00006938 if (STRNCMP(s, "help-tags\t", 10) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 /* help-tags entry was added in formatted form */
Bram Moolenaarf6210482007-07-25 20:56:39 +00006940 fputs((char *)s, fd_tags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 else
6942 {
6943 fprintf(fd_tags, "%s\t/*", s);
6944 for (p1 = s; *p1 != '\t'; ++p1)
6945 {
6946 /* insert backslash before '\\' and '/' */
6947 if (*p1 == '\\' || *p1 == '/')
6948 putc('\\', fd_tags);
6949 putc(*p1, fd_tags);
6950 }
6951 fprintf(fd_tags, "*\n");
6952 }
6953 }
6954 }
6955#ifdef FEAT_MBYTE
6956 if (mix)
6957 got_int = FALSE; /* continue with other languages */
6958#endif
6959
6960 for (i = 0; i < ga.ga_len; ++i)
6961 vim_free(((char_u **)ga.ga_data)[i]);
6962 ga_clear(&ga);
6963 fclose(fd_tags); /* there is no check for an error... */
6964}
6965#endif
6966
6967#if defined(FEAT_SIGNS) || defined(PROTO)
6968
6969/*
6970 * Struct to hold the sign properties.
6971 */
6972typedef struct sign sign_T;
6973
6974struct sign
6975{
6976 sign_T *sn_next; /* next sign in list */
Bram Moolenaarf75d4982010-10-15 20:20:05 +02006977 int sn_typenr; /* type number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 char_u *sn_name; /* name of sign */
6979 char_u *sn_icon; /* name of pixmap */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006980# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 void *sn_image; /* icon image */
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 char_u *sn_text; /* text used instead of pixmap */
6984 int sn_line_hl; /* highlight ID for line */
6985 int sn_text_hl; /* highlight ID for text */
6986};
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988static sign_T *first_sign = NULL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02006989static int next_sign_typenr = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
Bram Moolenaar985cb442009-05-14 19:51:46 +00006991static int sign_cmd_idx __ARGS((char_u *begin_cmd, char_u *end_cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992static void sign_list_defined __ARGS((sign_T *sp));
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00006993static void sign_undefine __ARGS((sign_T *sp, sign_T *sp_prev));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006995static char *cmds[] = {
6996 "define",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006997# define SIGNCMD_DEFINE 0
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006998 "undefine",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01006999# define SIGNCMD_UNDEFINE 1
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007000 "list",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007001# define SIGNCMD_LIST 2
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007002 "place",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007003# define SIGNCMD_PLACE 3
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007004 "unplace",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007005# define SIGNCMD_UNPLACE 4
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007006 "jump",
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007007# define SIGNCMD_JUMP 5
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007008 NULL
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007009# define SIGNCMD_LAST 6
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007010};
7011
7012/*
7013 * Find index of a ":sign" subcmd from its name.
7014 * "*end_cmd" must be writable.
7015 */
7016 static int
7017sign_cmd_idx(begin_cmd, end_cmd)
Bram Moolenaar985cb442009-05-14 19:51:46 +00007018 char_u *begin_cmd; /* begin of sign subcmd */
7019 char_u *end_cmd; /* just after sign subcmd */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007020{
7021 int idx;
7022 char save = *end_cmd;
7023
7024 *end_cmd = NUL;
7025 for (idx = 0; ; ++idx)
7026 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
7027 break;
7028 *end_cmd = save;
7029 return idx;
7030}
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032/*
7033 * ":sign" command
7034 */
7035 void
7036ex_sign(eap)
7037 exarg_T *eap;
7038{
7039 char_u *arg = eap->arg;
7040 char_u *p;
7041 int idx;
7042 sign_T *sp;
7043 sign_T *sp_prev;
7044 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
7046 /* Parse the subcommand. */
7047 p = skiptowhite(arg);
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007048 idx = sign_cmd_idx(arg, p);
7049 if (idx == SIGNCMD_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007051 EMSG2(_("E160: Unknown sign command: %s"), arg);
7052 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
7054 arg = skipwhite(p);
7055
7056 if (idx <= SIGNCMD_LIST)
7057 {
7058 /*
7059 * Define, undefine or list signs.
7060 */
7061 if (idx == SIGNCMD_LIST && *arg == NUL)
7062 {
7063 /* ":sign list": list all defined signs */
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01007064 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 sign_list_defined(sp);
7066 }
7067 else if (*arg == NUL)
7068 EMSG(_("E156: Missing sign name"));
7069 else
7070 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007071 /* Isolate the sign name. If it's a number skip leading zeroes,
7072 * so that "099" and "99" are the same sign. But keep "0". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 p = skiptowhite(arg);
7074 if (*p != NUL)
7075 *p++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007076 while (arg[0] == '0' && arg[1] != NUL)
7077 ++arg;
7078
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 sp_prev = NULL;
7080 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7081 {
7082 if (STRCMP(sp->sn_name, arg) == 0)
7083 break;
7084 sp_prev = sp;
7085 }
7086 if (idx == SIGNCMD_DEFINE)
7087 {
7088 /* ":sign define {name} ...": define a sign */
7089 if (sp == NULL)
7090 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007091 sign_T *lp;
7092 int start = next_sign_typenr;
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 /* Allocate a new sign. */
7095 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
7096 if (sp == NULL)
7097 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007099 /* Check that next_sign_typenr is not already being used.
7100 * This only happens after wrapping around. Hopefully
7101 * another one got deleted and we can use its number. */
7102 for (lp = first_sign; lp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007104 if (lp->sn_typenr == next_sign_typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007106 ++next_sign_typenr;
7107 if (next_sign_typenr == MAX_TYPENR)
7108 next_sign_typenr = 1;
7109 if (next_sign_typenr == start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007111 vim_free(sp);
7112 EMSG(_("E612: Too many signs defined"));
7113 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007115 lp = first_sign; /* start all over */
7116 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007118 lp = lp->sn_next;
7119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007121 sp->sn_typenr = next_sign_typenr;
7122 if (++next_sign_typenr == MAX_TYPENR)
7123 next_sign_typenr = 1; /* wrap around */
7124
7125 sp->sn_name = vim_strsave(arg);
7126 if (sp->sn_name == NULL) /* out of memory */
7127 {
7128 vim_free(sp);
7129 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
Bram Moolenaara4f332b2010-10-13 16:44:23 +02007131
7132 /* add the new sign to the list of signs */
7133 if (sp_prev == NULL)
7134 first_sign = sp;
7135 else
7136 sp_prev->sn_next = sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138
7139 /* set values for a defined sign. */
7140 for (;;)
7141 {
7142 arg = skipwhite(p);
7143 if (*arg == NUL)
7144 break;
7145 p = skiptowhite_esc(arg);
7146 if (STRNCMP(arg, "icon=", 5) == 0)
7147 {
7148 arg += 5;
7149 vim_free(sp->sn_icon);
7150 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
7151 backslash_halve(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007152# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 if (gui.in_use)
7154 {
7155 out_flush();
7156 if (sp->sn_image != NULL)
7157 gui_mch_destroy_sign(sp->sn_image);
7158 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7159 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
7162 else if (STRNCMP(arg, "text=", 5) == 0)
7163 {
7164 char_u *s;
7165 int cells;
7166 int len;
7167
7168 arg += 5;
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007169# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 /* Count cells and check for non-printable chars */
7171 if (has_mbyte)
7172 {
7173 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007174 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176 if (!vim_isprintc((*mb_ptr2char)(s)))
7177 break;
7178 cells += (*mb_ptr2cells)(s);
7179 }
7180 }
7181 else
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
7184 for (s = arg; s < p; ++s)
7185 if (!vim_isprintc(*s))
7186 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007187 cells = (int)(s - arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189 /* Currently must be one or two display cells */
7190 if (s != p || cells < 1 || cells > 2)
7191 {
7192 *p = NUL;
7193 EMSG2(_("E239: Invalid sign text: %s"), arg);
7194 return;
7195 }
7196
7197 vim_free(sp->sn_text);
7198 /* Allocate one byte more if we need to pad up
7199 * with a space. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007200 len = (int)(p - arg + ((cells == 1) ? 1 : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 sp->sn_text = vim_strnsave(arg, len);
7202
7203 if (sp->sn_text != NULL && cells == 1)
7204 STRCPY(sp->sn_text + len - 1, " ");
7205 }
7206 else if (STRNCMP(arg, "linehl=", 7) == 0)
7207 {
7208 arg += 7;
7209 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
7210 }
7211 else if (STRNCMP(arg, "texthl=", 7) == 0)
7212 {
7213 arg += 7;
7214 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
7215 }
7216 else
7217 {
7218 EMSG2(_(e_invarg2), arg);
7219 return;
7220 }
7221 }
7222 }
7223 else if (sp == NULL)
7224 EMSG2(_("E155: Unknown sign: %s"), arg);
7225 else if (idx == SIGNCMD_LIST)
7226 /* ":sign list {name}" */
7227 sign_list_defined(sp);
7228 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 /* ":sign undefine {name}" */
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007230 sign_undefine(sp, sp_prev);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232 }
7233 else
7234 {
7235 int id = -1;
7236 linenr_T lnum = -1;
7237 char_u *sign_name = NULL;
7238 char_u *arg1;
7239
7240 if (*arg == NUL)
7241 {
7242 if (idx == SIGNCMD_PLACE)
7243 {
7244 /* ":sign place": list placed signs in all buffers */
7245 sign_list_placed(NULL);
7246 }
7247 else if (idx == SIGNCMD_UNPLACE)
7248 {
7249 /* ":sign unplace": remove placed sign at cursor */
7250 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
7251 if (id > 0)
7252 {
7253 buf_delsign(curwin->w_buffer, id);
7254 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
7255 }
7256 else
7257 EMSG(_("E159: Missing sign number"));
7258 }
7259 else
7260 EMSG(_(e_argreq));
7261 return;
7262 }
7263
7264 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
7265 {
7266 /* ":sign unplace *": remove all placed signs */
7267 buf_delete_all_signs();
7268 return;
7269 }
7270
7271 /* first arg could be placed sign id */
7272 arg1 = arg;
7273 if (VIM_ISDIGIT(*arg))
7274 {
7275 id = getdigits(&arg);
7276 if (!vim_iswhite(*arg) && *arg != NUL)
7277 {
7278 id = -1;
7279 arg = arg1;
7280 }
7281 else
7282 {
7283 arg = skipwhite(arg);
7284 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
7285 {
7286 /* ":sign unplace {id}": remove placed sign by number */
7287 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7288 if ((lnum = buf_delsign(buf, id)) != 0)
7289 update_debug_sign(buf, lnum);
7290 return;
7291 }
7292 }
7293 }
7294
7295 /*
7296 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
7297 * Leave "arg" pointing to {fname}.
7298 */
7299 for (;;)
7300 {
7301 if (STRNCMP(arg, "line=", 5) == 0)
7302 {
7303 arg += 5;
7304 lnum = atoi((char *)arg);
7305 arg = skiptowhite(arg);
7306 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007307 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
7308 {
7309 if (id != -1)
7310 {
7311 EMSG(_(e_invarg));
7312 return;
7313 }
7314 id = -2;
7315 arg = skiptowhite(arg + 1);
7316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 else if (STRNCMP(arg, "name=", 5) == 0)
7318 {
7319 arg += 5;
7320 sign_name = arg;
7321 arg = skiptowhite(arg);
7322 if (*arg != NUL)
7323 *arg++ = NUL;
Bram Moolenaarb60574b2010-10-14 21:29:37 +02007324 while (sign_name[0] == '0' && sign_name[1] != NUL)
7325 ++sign_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
7327 else if (STRNCMP(arg, "file=", 5) == 0)
7328 {
7329 arg += 5;
7330 buf = buflist_findname(arg);
7331 break;
7332 }
7333 else if (STRNCMP(arg, "buffer=", 7) == 0)
7334 {
7335 arg += 7;
7336 buf = buflist_findnr((int)getdigits(&arg));
7337 if (*skipwhite(arg) != NUL)
7338 EMSG(_(e_trailing));
7339 break;
7340 }
7341 else
7342 {
7343 EMSG(_(e_invarg));
7344 return;
7345 }
7346 arg = skipwhite(arg);
7347 }
7348
7349 if (buf == NULL)
7350 {
7351 EMSG2(_("E158: Invalid buffer name: %s"), arg);
7352 }
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007353 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 if (lnum >= 0 || sign_name != NULL)
7356 EMSG(_(e_invarg));
7357 else
7358 /* ":sign place file={fname}": list placed signs in one file */
7359 sign_list_placed(buf);
7360 }
7361 else if (idx == SIGNCMD_JUMP)
7362 {
7363 /* ":sign jump {id} file={fname}" */
7364 if (lnum >= 0 || sign_name != NULL)
7365 EMSG(_(e_invarg));
7366 else if ((lnum = buf_findsign(buf, id)) > 0)
7367 { /* goto a sign ... */
7368 if (buf_jump_open_win(buf) != NULL)
7369 { /* ... in a current window */
7370 curwin->w_cursor.lnum = lnum;
7371 check_cursor_lnum();
7372 beginline(BL_WHITE);
7373 }
7374 else
7375 { /* ... not currently in a window */
7376 char_u *cmd;
7377
7378 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
7379 if (cmd == NULL)
7380 return;
7381 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
7382 do_cmdline_cmd(cmd);
7383 vim_free(cmd);
7384 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007385# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 foldOpenCursor();
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389 else
7390 EMSGN(_("E157: Invalid sign ID: %ld"), id);
7391 }
7392 else if (idx == SIGNCMD_UNPLACE)
7393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (lnum >= 0 || sign_name != NULL)
7395 EMSG(_(e_invarg));
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007396 else if (id == -2)
7397 {
7398 /* ":sign unplace * file={fname}" */
7399 redraw_buf_later(buf, NOT_VALID);
7400 buf_delete_signs(buf);
7401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 else
7403 {
Bram Moolenaarf65e5662012-07-10 15:18:22 +02007404 /* ":sign unplace {id} file={fname}" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 lnum = buf_delsign(buf, id);
7406 update_debug_sign(buf, lnum);
7407 }
7408 }
7409 /* idx == SIGNCMD_PLACE */
7410 else if (sign_name != NULL)
7411 {
7412 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7413 if (STRCMP(sp->sn_name, sign_name) == 0)
7414 break;
7415 if (sp == NULL)
7416 {
7417 EMSG2(_("E155: Unknown sign: %s"), sign_name);
7418 return;
7419 }
7420 if (lnum > 0)
7421 /* ":sign place {id} line={lnum} name={name} file={fname}":
7422 * place a sign */
7423 buf_addsign(buf, id, lnum, sp->sn_typenr);
7424 else
7425 /* ":sign place {id} file={fname}": change sign type */
7426 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
Bram Moolenaarf4d7f162014-05-07 14:38:44 +02007427 if (lnum > 0)
7428 update_debug_sign(buf, lnum);
7429 else
7430 EMSG2(_("E885: Not possible to change sign %s"), sign_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 else
7433 EMSG(_(e_invarg));
7434 }
7435}
7436
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007437# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438/*
7439 * Allocate the icons. Called when the GUI has started. Allows defining
7440 * signs before it starts.
7441 */
7442 void
7443sign_gui_started()
7444{
7445 sign_T *sp;
7446
7447 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7448 if (sp->sn_icon != NULL)
7449 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
7450}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453/*
7454 * List one sign.
7455 */
7456 static void
7457sign_list_defined(sp)
7458 sign_T *sp;
7459{
7460 char_u *p;
7461
Bram Moolenaar555b2802005-05-19 21:08:39 +00007462 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (sp->sn_icon != NULL)
7464 {
7465 MSG_PUTS(" icon=");
7466 msg_outtrans(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007467# ifdef FEAT_SIGN_ICONS
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (sp->sn_image == NULL)
7469 MSG_PUTS(_(" (NOT FOUND)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007470# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 MSG_PUTS(_(" (not supported)"));
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474 if (sp->sn_text != NULL)
7475 {
7476 MSG_PUTS(" text=");
7477 msg_outtrans(sp->sn_text);
7478 }
7479 if (sp->sn_line_hl > 0)
7480 {
7481 MSG_PUTS(" linehl=");
7482 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
7483 if (p == NULL)
7484 MSG_PUTS("NONE");
7485 else
7486 msg_puts(p);
7487 }
7488 if (sp->sn_text_hl > 0)
7489 {
7490 MSG_PUTS(" texthl=");
7491 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
7492 if (p == NULL)
7493 MSG_PUTS("NONE");
7494 else
7495 msg_puts(p);
7496 }
7497}
7498
7499/*
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007500 * Undefine a sign and free its memory.
7501 */
7502 static void
7503sign_undefine(sp, sp_prev)
7504 sign_T *sp;
7505 sign_T *sp_prev;
7506{
7507 vim_free(sp->sn_name);
7508 vim_free(sp->sn_icon);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007509# ifdef FEAT_SIGN_ICONS
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007510 if (sp->sn_image != NULL)
7511 {
7512 out_flush();
7513 gui_mch_destroy_sign(sp->sn_image);
7514 }
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007515# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007516 vim_free(sp->sn_text);
7517 if (sp_prev == NULL)
7518 first_sign = sp->sn_next;
7519 else
7520 sp_prev->sn_next = sp->sn_next;
7521 vim_free(sp);
7522}
7523
7524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 * Get highlighting attribute for sign "typenr".
7526 * If "line" is TRUE: line highl, if FALSE: text highl.
7527 */
7528 int
7529sign_get_attr(typenr, line)
7530 int typenr;
7531 int line;
7532{
7533 sign_T *sp;
7534
7535 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7536 if (sp->sn_typenr == typenr)
7537 {
7538 if (line)
7539 {
7540 if (sp->sn_line_hl > 0)
7541 return syn_id2attr(sp->sn_line_hl);
7542 }
7543 else
7544 {
7545 if (sp->sn_text_hl > 0)
7546 return syn_id2attr(sp->sn_text_hl);
7547 }
7548 break;
7549 }
7550 return 0;
7551}
7552
7553/*
7554 * Get text mark for sign "typenr".
7555 * Returns NULL if there isn't one.
7556 */
7557 char_u *
7558sign_get_text(typenr)
7559 int typenr;
7560{
7561 sign_T *sp;
7562
7563 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7564 if (sp->sn_typenr == typenr)
7565 return sp->sn_text;
7566 return NULL;
7567}
7568
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007569# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 void *
7571sign_get_image(typenr)
7572 int typenr; /* the attribute which may have a sign */
7573{
7574 sign_T *sp;
7575
7576 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7577 if (sp->sn_typenr == typenr)
7578 return sp->sn_image;
7579 return NULL;
7580}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
7583/*
7584 * Get the name of a sign by its typenr.
7585 */
7586 char_u *
7587sign_typenr2name(typenr)
7588 int typenr;
7589{
7590 sign_T *sp;
7591
7592 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7593 if (sp->sn_typenr == typenr)
7594 return sp->sn_name;
7595 return (char_u *)_("[Deleted]");
7596}
7597
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007598# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007599/*
7600 * Undefine/free all signs.
7601 */
7602 void
7603free_signs()
7604{
7605 while (first_sign != NULL)
7606 sign_undefine(first_sign, NULL);
7607}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007608# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00007609
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007610# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007611static enum
7612{
7613 EXP_SUBCMD, /* expand :sign sub-commands */
7614 EXP_DEFINE, /* expand :sign define {name} args */
7615 EXP_PLACE, /* expand :sign place {id} args */
7616 EXP_UNPLACE, /* expand :sign unplace" */
7617 EXP_SIGN_NAMES /* expand with name of placed signs */
7618} expand_what;
7619
7620/*
7621 * Function given to ExpandGeneric() to obtain the sign command
7622 * expansion.
7623 */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007624 char_u *
7625get_sign_name(xp, idx)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00007626 expand_T *xp UNUSED;
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007627 int idx;
7628{
7629 sign_T *sp;
7630 int current_idx;
7631
7632 switch (expand_what)
7633 {
7634 case EXP_SUBCMD:
7635 return (char_u *)cmds[idx];
7636 case EXP_DEFINE:
7637 {
7638 char *define_arg[] =
7639 {
7640 "icon=", "linehl=", "text=", "texthl=", NULL
7641 };
7642 return (char_u *)define_arg[idx];
7643 }
7644 case EXP_PLACE:
7645 {
7646 char *place_arg[] =
7647 {
7648 "line=", "name=", "file=", "buffer=", NULL
7649 };
7650 return (char_u *)place_arg[idx];
7651 }
7652 case EXP_UNPLACE:
7653 {
7654 char *unplace_arg[] = { "file=", "buffer=", NULL };
7655 return (char_u *)unplace_arg[idx];
7656 }
7657 case EXP_SIGN_NAMES:
7658 /* Complete with name of signs already defined */
7659 current_idx = 0;
7660 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
7661 if (current_idx++ == idx)
7662 return sp->sn_name;
7663 return NULL;
7664 default:
7665 return NULL;
7666 }
7667}
7668
7669/*
7670 * Handle command line completion for :sign command.
7671 */
7672 void
7673set_context_in_sign_cmd(xp, arg)
7674 expand_T *xp;
7675 char_u *arg;
7676{
7677 char_u *p;
7678 char_u *end_subcmd;
7679 char_u *last;
7680 int cmd_idx;
7681 char_u *begin_subcmd_args;
7682
7683 /* Default: expand subcommands. */
7684 xp->xp_context = EXPAND_SIGN;
7685 expand_what = EXP_SUBCMD;
7686 xp->xp_pattern = arg;
7687
7688 end_subcmd = skiptowhite(arg);
7689 if (*end_subcmd == NUL)
7690 /* expand subcmd name
7691 * :sign {subcmd}<CTRL-D>*/
7692 return;
7693
7694 cmd_idx = sign_cmd_idx(arg, end_subcmd);
7695
7696 /* :sign {subcmd} {subcmd_args}
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007697 * |
7698 * begin_subcmd_args */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007699 begin_subcmd_args = skipwhite(end_subcmd);
7700 p = skiptowhite(begin_subcmd_args);
7701 if (*p == NUL)
7702 {
7703 /*
7704 * Expand first argument of subcmd when possible.
7705 * For ":jump {id}" and ":unplace {id}", we could
7706 * possibly expand the ids of all signs already placed.
7707 */
7708 xp->xp_pattern = begin_subcmd_args;
7709 switch (cmd_idx)
7710 {
7711 case SIGNCMD_LIST:
7712 case SIGNCMD_UNDEFINE:
7713 /* :sign list <CTRL-D>
7714 * :sign undefine <CTRL-D> */
7715 expand_what = EXP_SIGN_NAMES;
7716 break;
7717 default:
7718 xp->xp_context = EXPAND_NOTHING;
7719 }
7720 return;
7721 }
7722
7723 /* expand last argument of subcmd */
7724
7725 /* :sign define {name} {args}...
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007726 * |
7727 * p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007728
7729 /* Loop until reaching last argument. */
7730 do
7731 {
7732 p = skipwhite(p);
7733 last = p;
7734 p = skiptowhite(p);
7735 } while (*p != NUL);
7736
7737 p = vim_strchr(last, '=');
7738
7739 /* :sign define {name} {args}... {last}=
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007740 * | |
7741 * last p */
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007742 if (p == NUL)
7743 {
7744 /* Expand last argument name (before equal sign). */
7745 xp->xp_pattern = last;
7746 switch (cmd_idx)
7747 {
7748 case SIGNCMD_DEFINE:
7749 expand_what = EXP_DEFINE;
7750 break;
7751 case SIGNCMD_PLACE:
7752 expand_what = EXP_PLACE;
7753 break;
7754 case SIGNCMD_JUMP:
7755 case SIGNCMD_UNPLACE:
7756 expand_what = EXP_UNPLACE;
7757 break;
7758 default:
7759 xp->xp_context = EXPAND_NOTHING;
7760 }
7761 }
7762 else
7763 {
7764 /* Expand last argument value (after equal sign). */
7765 xp->xp_pattern = p + 1;
7766 switch (cmd_idx)
7767 {
7768 case SIGNCMD_DEFINE:
7769 if (STRNCMP(last, "texthl", p - last) == 0 ||
7770 STRNCMP(last, "linehl", p - last) == 0)
7771 xp->xp_context = EXPAND_HIGHLIGHT;
7772 else if (STRNCMP(last, "icon", p - last) == 0)
7773 xp->xp_context = EXPAND_FILES;
7774 else
7775 xp->xp_context = EXPAND_NOTHING;
7776 break;
7777 case SIGNCMD_PLACE:
7778 if (STRNCMP(last, "name", p - last) == 0)
7779 expand_what = EXP_SIGN_NAMES;
7780 else
7781 xp->xp_context = EXPAND_NOTHING;
7782 break;
7783 default:
7784 xp->xp_context = EXPAND_NOTHING;
7785 }
7786 }
7787}
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007788# endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00007789#endif
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007790
7791/*
7792 * Make the user happy.
7793 */
7794 void
7795ex_smile(eap)
7796 exarg_T *eap UNUSED;
7797{
7798 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";
7799 char *p;
7800 int n;
7801
7802 msg_start();
7803 msg_putchar('\n');
7804 for (p = code; *p != NUL; ++p)
7805 if (*p == 'x')
7806 msg_putchar('\n');
7807 else
7808 for (n = *p++; n > 0; --n)
Bram Moolenaar2d820802015-12-31 20:46:39 +01007809 if (*p == 'o' || *p == '$')
7810 msg_putchar_attr(*p, hl_attr(HLF_L));
7811 else
7812 msg_putchar(*p);
Bram Moolenaar86e179d2015-12-31 16:10:23 +01007813 msg_clr_eos();
7814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
7816#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
7817/*
7818 * ":drop"
7819 * Opens the first argument in a window. When there are two or more arguments
7820 * the argument list is redefined.
7821 */
7822 void
7823ex_drop(eap)
7824 exarg_T *eap;
7825{
7826 int split = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 win_T *wp;
7828 buf_T *buf;
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007829# ifdef FEAT_WINDOWS
7830 tabpage_T *tp;
7831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
7833 /*
7834 * Check if the first argument is already being edited in a window. If
7835 * so, jump to that window.
7836 * We would actually need to check all arguments, but that's complicated
7837 * and mostly only one file is dropped.
7838 * This also ignores wildcards, since it is very unlikely the user is
7839 * editing a file name with a wildcard character.
7840 */
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007841 set_arglist(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
Bram Moolenaarb01a8b72007-02-13 02:47:22 +00007843 /*
7844 * Expanding wildcards may result in an empty argument list. E.g. when
7845 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we
7846 * already did an error message for this.
7847 */
7848 if (ARGCOUNT == 0)
7849 return;
7850
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007851# ifdef FEAT_WINDOWS
7852 if (cmdmod.tab)
7853 {
7854 /* ":tab drop file ...": open a tab for each argument that isn't
7855 * edited in a window yet. It's like ":tab all" but without closing
7856 * windows or tabs. */
7857 ex_all(eap);
7858 }
7859 else
7860# endif
7861 {
7862 /* ":drop file ...": Edit the first argument. Jump to an existing
7863 * window if possible, edit in current window if the current buffer
7864 * can be abandoned, otherwise open a new window. */
7865 buf = buflist_findnr(ARGLIST[0].ae_fnum);
7866
7867 FOR_ALL_TAB_WINDOWS(tp, wp)
7868 {
7869 if (wp->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007871# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007872 goto_tabpage_win(tp, wp);
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 curwin->w_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 return;
7876 }
7877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007879 /*
7880 * Check whether the current buffer is changed. If so, we will need
7881 * to split the current window or data could be lost.
7882 * Skip the check if the 'hidden' option is set, as in this case the
7883 * buffer won't be lost.
7884 */
7885 if (!P_HID(curbuf))
7886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007888 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889# endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007890 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# ifdef FEAT_WINDOWS
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007892 --emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893# else
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007894 if (split)
7895 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896# endif
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
Bram Moolenaara5b6ad12006-03-11 21:36:59 +00007899 /* Fake a ":sfirst" or ":first" command edit the first argument. */
7900 if (split)
7901 {
7902 eap->cmdidx = CMD_sfirst;
7903 eap->cmd[0] = 's';
7904 }
7905 else
7906 eap->cmdidx = CMD_first;
7907 ex_rewind(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909}
7910#endif