blob: 22bbd45dda950271389cbd66f3287357303a319f [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
14#include "vim.h"
Bram Moolenaara5792f52005-11-23 21:25:05 +000015#ifdef HAVE_FCNTL_H
16# include <fcntl.h>
17#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include "version.h"
19
20#ifdef FEAT_EX_EXTRA
21static int linelen __ARGS((int *has_tab));
22#endif
23static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out));
24#ifdef FEAT_VIMINFO
25static char_u *viminfo_filename __ARGS((char_u *));
26static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int want_info, int want_marks, int force_read));
27static int viminfo_encoding __ARGS((vir_T *virp));
28static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing));
29#endif
30
31static int check_overwrite __ARGS((exarg_T *eap, buf_T *buf, char_u *fname, char_u *ffname, int other));
32static int check_readonly __ARGS((int *forceit, buf_T *buf));
33#ifdef FEAT_AUTOCMD
34static void delbuf_msg __ARGS((char_u *name));
35#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static int
37#ifdef __BORLANDC__
38 _RTLENTRYF
39#endif
40 help_compare __ARGS((const void *s1, const void *s2));
41
42/*
43 * ":ascii" and "ga".
44 */
45/*ARGSUSED*/
46 void
47do_ascii(eap)
48 exarg_T *eap;
49{
50 int c;
51 char buf1[20];
52 char buf2[20];
53 char_u buf3[7];
54#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000055 int cc[MAX_MCO];
56 int ci = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 int len;
58
59 if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000060 c = utfc_ptr2char(ml_get_cursor(), cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 else
62#endif
63 c = gchar_cursor();
64 if (c == NUL)
65 {
66 MSG("NUL");
67 return;
68 }
69
70#ifdef FEAT_MBYTE
71 IObuff[0] = NUL;
72 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
73#endif
74 {
75 if (c == NL) /* NUL is stored as NL */
76 c = NUL;
77 if (vim_isprintc_strict(c) && (c < ' '
78#ifndef EBCDIC
79 || c > '~'
80#endif
81 ))
82 {
83 transchar_nonprint(buf3, c);
84 sprintf(buf1, " <%s>", (char *)buf3);
85 }
86 else
87 buf1[0] = NUL;
88#ifndef EBCDIC
89 if (c >= 0x80)
90 sprintf(buf2, " <M-%s>", transchar(c & 0x7f));
91 else
92#endif
93 buf2[0] = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +000094 vim_snprintf((char *)IObuff, IOSIZE,
95 _("<%s>%s%s %d, Hex %02x, Octal %03o"),
96 transchar(c), buf1, buf2, c, c, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000098 c = cc[ci++];
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
100 }
101
102#ifdef FEAT_MBYTE
103 /* Repeat for combining characters. */
104 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
105 {
106 len = (int)STRLEN(IObuff);
107 /* This assumes every multi-byte char is printable... */
108 if (len > 0)
109 IObuff[len++] = ' ';
110 IObuff[len++] = '<';
111 if (utf_iscomposing(c)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000112# ifdef USE_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 && !gui.in_use
Bram Moolenaar4770d092006-01-12 23:22:24 +0000114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 )
116 IObuff[len++] = ' '; /* draw composing char on top of a space */
Bram Moolenaar555b2802005-05-19 21:08:39 +0000117 len += (*mb_char2bytes)(c, IObuff + len);
118 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
120 : _("> %d, Hex %08x, Octal %o"), c, c, c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000121 if (ci == MAX_MCO)
122 break;
123 c = cc[ci++];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 }
125#endif
126
127 msg(IObuff);
128}
129
130#if defined(FEAT_EX_EXTRA) || defined(PROTO)
131/*
132 * ":left", ":center" and ":right": align text.
133 */
134 void
135ex_align(eap)
136 exarg_T *eap;
137{
138 pos_T save_curpos;
139 int len;
140 int indent = 0;
141 int new_indent;
142 int has_tab;
143 int width;
144
145#ifdef FEAT_RIGHTLEFT
146 if (curwin->w_p_rl)
147 {
148 /* switch left and right aligning */
149 if (eap->cmdidx == CMD_right)
150 eap->cmdidx = CMD_left;
151 else if (eap->cmdidx == CMD_left)
152 eap->cmdidx = CMD_right;
153 }
154#endif
155
156 width = atoi((char *)eap->arg);
157 save_curpos = curwin->w_cursor;
158 if (eap->cmdidx == CMD_left) /* width is used for new indent */
159 {
160 if (width >= 0)
161 indent = width;
162 }
163 else
164 {
165 /*
166 * if 'textwidth' set, use it
167 * else if 'wrapmargin' set, use it
168 * if invalid value, use 80
169 */
170 if (width <= 0)
171 width = curbuf->b_p_tw;
172 if (width == 0 && curbuf->b_p_wm > 0)
173 width = W_WIDTH(curwin) - curbuf->b_p_wm;
174 if (width <= 0)
175 width = 80;
176 }
177
178 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
179 return;
180
181 for (curwin->w_cursor.lnum = eap->line1;
182 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
183 {
184 if (eap->cmdidx == CMD_left) /* left align */
185 new_indent = indent;
186 else
187 {
188 len = linelen(eap->cmdidx == CMD_right ? &has_tab
189 : NULL) - get_indent();
190
191 if (len <= 0) /* skip blank lines */
192 continue;
193
194 if (eap->cmdidx == CMD_center)
195 new_indent = (width - len) / 2;
196 else
197 {
198 new_indent = width - len; /* right align */
199
200 /*
201 * Make sure that embedded TABs don't make the text go too far
202 * to the right.
203 */
204 if (has_tab)
205 while (new_indent > 0)
206 {
207 (void)set_indent(new_indent, 0);
208 if (linelen(NULL) <= width)
209 {
210 /*
211 * Now try to move the line as much as possible to
212 * the right. Stop when it moves too far.
213 */
214 do
215 (void)set_indent(++new_indent, 0);
216 while (linelen(NULL) <= width);
217 --new_indent;
218 break;
219 }
220 --new_indent;
221 }
222 }
223 }
224 if (new_indent < 0)
225 new_indent = 0;
226 (void)set_indent(new_indent, 0); /* set indent */
227 }
228 changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
229 curwin->w_cursor = save_curpos;
230 beginline(BL_WHITE | BL_FIX);
231}
232
233/*
234 * Get the length of the current line, excluding trailing white space.
235 */
236 static int
237linelen(has_tab)
238 int *has_tab;
239{
240 char_u *line;
241 char_u *first;
242 char_u *last;
243 int save;
244 int len;
245
246 /* find the first non-blank character */
247 line = ml_get_curline();
248 first = skipwhite(line);
249
250 /* find the character after the last non-blank character */
251 for (last = first + STRLEN(first);
252 last > first && vim_iswhite(last[-1]); --last)
253 ;
254 save = *last;
255 *last = NUL;
256 len = linetabsize(line); /* get line length */
257 if (has_tab != NULL) /* check for embedded TAB */
258 *has_tab = (vim_strrchr(first, TAB) != NULL);
259 *last = save;
260
261 return len;
262}
263
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000264/* Buffer for one line used during sorting. It's allocated to contain the
265 * longest line being sorted. */
266static char_u *sortbuf;
267
268static int sort_ic; /* ignore case */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000269static int sort_nr; /* sort on number */
270
271/* Struct to store info to be sorted. */
272typedef struct
273{
274 linenr_T lnum; /* line number */
275 long col_nr; /* column number or number */
276} sorti_T;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000277
278static int
279#ifdef __BORLANDC__
280_RTLENTRYF
281#endif
282sort_compare __ARGS((const void *s1, const void *s2));
283
284 static int
285#ifdef __BORLANDC__
286_RTLENTRYF
287#endif
288sort_compare(s1, s2)
289 const void *s1;
290 const void *s2;
291{
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000292 sorti_T l1 = *(sorti_T *)s1;
293 sorti_T l2 = *(sorti_T *)s2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000294 char_u *s;
295
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000296 /* When sorting numbers "col_nr" is the number, not the column number. */
297 if (sort_nr)
298 return l1.col_nr - l2.col_nr;
299
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000300 /* We need to copy one line into "sortbuf", because there is no guarantee
301 * that the first pointer becomes invalid when obtaining the second one. */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000302 STRCPY(sortbuf, ml_get(l1.lnum) + l1.col_nr);
303 s = ml_get(l2.lnum) + l2.col_nr;
304
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000305 return sort_ic ? STRICMP(sortbuf, s) : STRCMP(sortbuf, s);
306}
307
308/*
309 * ":sort".
310 */
311 void
312ex_sort(eap)
313 exarg_T *eap;
314{
315 regmatch_T regmatch;
316 int len;
317 linenr_T lnum;
318 long maxlen = 0;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000319 sorti_T *nrs;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000320 size_t count = eap->line2 - eap->line1 + 1;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000321 size_t i;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000322 char_u *p;
323 char_u *s;
324 int unique = FALSE;
325 long deleted;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000326 colnr_T col;
327 int sort_oct; /* sort on octal number */
328 int sort_hex; /* sort on hex number */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000329
330 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
331 return;
332 sortbuf = NULL;
333 regmatch.regprog = NULL;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000334 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000335 if (nrs == NULL)
336 goto theend;
337
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000338 sort_ic = sort_nr = sort_oct = sort_hex = 0;
339
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000340 for (p = eap->arg; *p != NUL; ++p)
341 {
342 if (vim_iswhite(*p))
343 ;
344 else if (*p == 'i')
345 sort_ic = TRUE;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000346 else if (*p == 'n')
347 sort_nr = 2;
348 else if (*p == 'o')
349 sort_oct = 2;
350 else if (*p == 'x')
351 sort_hex = 2;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000352 else if (*p == 'u')
353 unique = TRUE;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000354 else if (*p == '"') /* comment start */
355 break;
356 else if (check_nextcmd(p) != NULL)
357 {
358 eap->nextcmd = check_nextcmd(p);
359 break;
360 }
361 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000362 {
363 s = skip_regexp(p + 1, *p, TRUE, NULL);
364 if (*s != *p)
365 {
366 EMSG(_(e_invalpat));
367 goto theend;
368 }
369 *s = NUL;
370 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
371 if (regmatch.regprog == NULL)
372 goto theend;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +0000373 p = s; /* continue after the regexp */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000374 regmatch.rm_ic = p_ic;
375 }
376 else
377 {
378 EMSG2(_(e_invarg2), p);
379 goto theend;
380 }
381 }
382
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000383 /* Can only have one of 'n', 'o' and 'x'. */
384 if (sort_nr + sort_oct + sort_hex > 2)
385 {
386 EMSG(_(e_invarg));
387 goto theend;
388 }
389
390 /* From here on "sort_nr" is used as a flag for any number sorting. */
391 sort_nr += sort_oct + sort_hex;
392
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000393 /*
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000394 * Make an array with all line numbers. This avoids having to copy all
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000395 * the lines into allocated memory.
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000396 * When sorting on strings "col_nr" is de offset in the line, for numbers
397 * sorting it's the number to sort on. This means the pattern matching
398 * and number conversion only has to be done once per line.
399 * Also get the longest line length for allocating "sortbuf".
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000400 */
401 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
402 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000403 s = ml_get(lnum);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000404 len = STRLEN(s);
405 if (maxlen < len)
406 maxlen = len;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000407
408 if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
409 col = regmatch.endp[0] - s;
410 else
411 col = 0;
412
413 if (sort_nr)
414 {
415 /* Sorting on number: Store the number itself. */
416 if (sort_hex)
417 s = skiptohex(s + col);
418 else
419 s = skiptodigit(s + col);
420 vim_str2nr(s, NULL, NULL, sort_oct, sort_hex,
421 &nrs[lnum - eap->line1].col_nr, NULL);
422 }
423 else
424 /* Store the column to sort at. */
425 nrs[lnum - eap->line1].col_nr = col;
426
427 nrs[lnum - eap->line1].lnum = lnum;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000428
429 if (regmatch.regprog != NULL)
430 fast_breakcheck();
431 if (got_int)
432 goto theend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000433 }
434
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000435 /* Allocate a buffer that can hold the longest line. */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000436 sortbuf = alloc((unsigned)maxlen + 1);
437 if (sortbuf == NULL)
438 goto theend;
439
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000440 /* Sort the array of line numbers. Note: can't be interrupted! */
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000441 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000442
443 /* Insert the lines in the sorted order below the last one. */
444 lnum = eap->line2;
445 for (i = 0; i < count; ++i)
446 {
447 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
448 if (!unique || i == 0
449 || (sort_ic ? STRICMP(s, sortbuf) : STRCMP(s, sortbuf)) != 0)
450 {
451 if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL)
452 break;
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000453 if (unique)
454 STRCPY(sortbuf, s);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000455 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000456 fast_breakcheck();
457 if (got_int)
458 goto theend;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000459 }
460
461 /* delete the original lines if appending worked */
462 if (i == count)
463 for (i = 0; i < count; ++i)
464 ml_delete(eap->line1, FALSE);
465 else
466 count = 0;
467
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000468 /* Adjust marks for deleted (or added) lines and prepare for displaying. */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000469 deleted = count - (lnum - eap->line2);
470 if (deleted > 0)
471 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
472 else if (deleted < 0)
473 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
474 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000475
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000476 curwin->w_cursor.lnum = eap->line1;
477 beginline(BL_WHITE | BL_FIX);
478
479theend:
480 vim_free(nrs);
481 vim_free(sortbuf);
482 vim_free(regmatch.regprog);
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000483 if (got_int)
484 EMSG(_(e_interr));
Bram Moolenaar67fe1a12005-05-22 22:12:58 +0000485}
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * ":retab".
489 */
490 void
491ex_retab(eap)
492 exarg_T *eap;
493{
494 linenr_T lnum;
495 int got_tab = FALSE;
496 long num_spaces = 0;
497 long num_tabs;
498 long len;
499 long col;
500 long vcol;
501 long start_col = 0; /* For start of white-space string */
502 long start_vcol = 0; /* For start of white-space string */
503 int temp;
504 long old_len;
505 char_u *ptr;
506 char_u *new_line = (char_u *)1; /* init to non-NULL */
507 int did_undo; /* called u_save for current line */
508 int new_ts;
509 int save_list;
510 linenr_T first_line = 0; /* first changed line */
511 linenr_T last_line = 0; /* last changed line */
512
513 save_list = curwin->w_p_list;
514 curwin->w_p_list = 0; /* don't want list mode here */
515
516 new_ts = getdigits(&(eap->arg));
517 if (new_ts < 0)
518 {
519 EMSG(_(e_positive));
520 return;
521 }
522 if (new_ts == 0)
523 new_ts = curbuf->b_p_ts;
524 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
525 {
526 ptr = ml_get(lnum);
527 col = 0;
528 vcol = 0;
529 did_undo = FALSE;
530 for (;;)
531 {
532 if (vim_iswhite(ptr[col]))
533 {
534 if (!got_tab && num_spaces == 0)
535 {
536 /* First consecutive white-space */
537 start_vcol = vcol;
538 start_col = col;
539 }
540 if (ptr[col] == ' ')
541 num_spaces++;
542 else
543 got_tab = TRUE;
544 }
545 else
546 {
547 if (got_tab || (eap->forceit && num_spaces > 1))
548 {
549 /* Retabulate this string of white-space */
550
551 /* len is virtual length of white string */
552 len = num_spaces = vcol - start_vcol;
553 num_tabs = 0;
554 if (!curbuf->b_p_et)
555 {
556 temp = new_ts - (start_vcol % new_ts);
557 if (num_spaces >= temp)
558 {
559 num_spaces -= temp;
560 num_tabs++;
561 }
562 num_tabs += num_spaces / new_ts;
563 num_spaces -= (num_spaces / new_ts) * new_ts;
564 }
565 if (curbuf->b_p_et || got_tab ||
566 (num_spaces + num_tabs < len))
567 {
568 if (did_undo == FALSE)
569 {
570 did_undo = TRUE;
571 if (u_save((linenr_T)(lnum - 1),
572 (linenr_T)(lnum + 1)) == FAIL)
573 {
574 new_line = NULL; /* flag out-of-memory */
575 break;
576 }
577 }
578
579 /* len is actual number of white characters used */
580 len = num_spaces + num_tabs;
581 old_len = (long)STRLEN(ptr);
582 new_line = lalloc(old_len - col + start_col + len + 1,
583 TRUE);
584 if (new_line == NULL)
585 break;
586 if (start_col > 0)
587 mch_memmove(new_line, ptr, (size_t)start_col);
588 mch_memmove(new_line + start_col + len,
589 ptr + col, (size_t)(old_len - col + 1));
590 ptr = new_line + start_col;
591 for (col = 0; col < len; col++)
592 ptr[col] = (col < num_tabs) ? '\t' : ' ';
593 ml_replace(lnum, new_line, FALSE);
594 if (first_line == 0)
595 first_line = lnum;
596 last_line = lnum;
597 ptr = new_line;
598 col = start_col + len;
599 }
600 }
601 got_tab = FALSE;
602 num_spaces = 0;
603 }
604 if (ptr[col] == NUL)
605 break;
606 vcol += chartabsize(ptr + col, (colnr_T)vcol);
607#ifdef FEAT_MBYTE
608 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000609 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 else
611#endif
612 ++col;
613 }
614 if (new_line == NULL) /* out of memory */
615 break;
616 line_breakcheck();
617 }
618 if (got_int)
619 EMSG(_(e_interr));
620
621 if (curbuf->b_p_ts != new_ts)
622 redraw_curbuf_later(NOT_VALID);
623 if (first_line != 0)
624 changed_lines(first_line, 0, last_line + 1, 0L);
625
626 curwin->w_p_list = save_list; /* restore 'list' */
627
628 curbuf->b_p_ts = new_ts;
629 coladvance(curwin->w_curswant);
630
631 u_clearline();
632}
633#endif
634
635/*
636 * :move command - move lines line1-line2 to line dest
637 *
638 * return FAIL for failure, OK otherwise
639 */
640 int
641do_move(line1, line2, dest)
642 linenr_T line1;
643 linenr_T line2;
644 linenr_T dest;
645{
646 char_u *str;
647 linenr_T l;
648 linenr_T extra; /* Num lines added before line1 */
649 linenr_T num_lines; /* Num lines moved */
650 linenr_T last_line; /* Last line in file after adding new text */
651
652 if (dest >= line1 && dest < line2)
653 {
654 EMSG(_("E134: Move lines into themselves"));
655 return FAIL;
656 }
657
658 num_lines = line2 - line1 + 1;
659
660 /*
661 * First we copy the old text to its new location -- webb
662 * Also copy the flag that ":global" command uses.
663 */
664 if (u_save(dest, dest + 1) == FAIL)
665 return FAIL;
666 for (extra = 0, l = line1; l <= line2; l++)
667 {
668 str = vim_strsave(ml_get(l + extra));
669 if (str != NULL)
670 {
671 ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
672 vim_free(str);
673 if (dest < line1)
674 extra++;
675 }
676 }
677
678 /*
679 * Now we must be careful adjusting our marks so that we don't overlap our
680 * mark_adjust() calls.
681 *
682 * We adjust the marks within the old text so that they refer to the
683 * last lines of the file (temporarily), because we know no other marks
684 * will be set there since these line numbers did not exist until we added
685 * our new lines.
686 *
687 * Then we adjust the marks on lines between the old and new text positions
688 * (either forwards or backwards).
689 *
690 * And Finally we adjust the marks we put at the end of the file back to
691 * their final destination at the new text position -- webb
692 */
693 last_line = curbuf->b_ml.ml_line_count;
694 mark_adjust(line1, line2, last_line - line2, 0L);
695 if (dest >= line2)
696 {
697 mark_adjust(line2 + 1, dest, -num_lines, 0L);
698 curbuf->b_op_start.lnum = dest - num_lines + 1;
699 curbuf->b_op_end.lnum = dest;
700 }
701 else
702 {
703 mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
704 curbuf->b_op_start.lnum = dest + 1;
705 curbuf->b_op_end.lnum = dest + num_lines;
706 }
707 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
708 mark_adjust(last_line - num_lines + 1, last_line,
709 -(last_line - dest - extra), 0L);
710
711 /*
712 * Now we delete the original text -- webb
713 */
714 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
715 return FAIL;
716
717 for (l = line1; l <= line2; l++)
718 ml_delete(line1 + extra, TRUE);
719
720 if (!global_busy && num_lines > p_report)
721 {
722 if (num_lines == 1)
723 MSG(_("1 line moved"));
724 else
725 smsg((char_u *)_("%ld lines moved"), num_lines);
726 }
727
728 /*
729 * Leave the cursor on the last of the moved lines.
730 */
731 if (dest >= line1)
732 curwin->w_cursor.lnum = dest;
733 else
734 curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
735
736 if (line1 < dest)
737 changed_lines(line1, 0, dest + num_lines + 1, 0L);
738 else
739 changed_lines(dest + 1, 0, line1 + num_lines, 0L);
740
741 return OK;
742}
743
744/*
745 * ":copy"
746 */
747 void
748ex_copy(line1, line2, n)
749 linenr_T line1;
750 linenr_T line2;
751 linenr_T n;
752{
753 linenr_T count;
754 char_u *p;
755
756 count = line2 - line1 + 1;
757 curbuf->b_op_start.lnum = n + 1;
758 curbuf->b_op_end.lnum = n + count;
759 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
760
761 /*
762 * there are three situations:
763 * 1. destination is above line1
764 * 2. destination is between line1 and line2
765 * 3. destination is below line2
766 *
767 * n = destination (when starting)
768 * curwin->w_cursor.lnum = destination (while copying)
769 * line1 = start of source (while copying)
770 * line2 = end of source (while copying)
771 */
772 if (u_save(n, n + 1) == FAIL)
773 return;
774
775 curwin->w_cursor.lnum = n;
776 while (line1 <= line2)
777 {
778 /* need to use vim_strsave() because the line will be unlocked within
779 * ml_append() */
780 p = vim_strsave(ml_get(line1));
781 if (p != NULL)
782 {
783 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
784 vim_free(p);
785 }
786 /* situation 2: skip already copied lines */
787 if (line1 == n)
788 line1 = curwin->w_cursor.lnum;
789 ++line1;
790 if (curwin->w_cursor.lnum < line1)
791 ++line1;
792 if (curwin->w_cursor.lnum < line2)
793 ++line2;
794 ++curwin->w_cursor.lnum;
795 }
796
797 appended_lines_mark(n, count);
798
799 msgmore((long)count);
800}
801
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000802static char_u *prevcmd = NULL; /* the previous command */
803
804#if defined(EXITFREE) || defined(PROTO)
805 void
806free_prev_shellcmd()
807{
808 vim_free(prevcmd);
809}
810#endif
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812/*
813 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
814 * Bangs in the argument are replaced with the previously entered command.
815 * Remember the argument.
816 *
817 * RISCOS: Bangs only replaced when followed by a space, since many
818 * pathnames contain one.
819 */
820 void
821do_bang(addr_count, eap, forceit, do_in, do_out)
822 int addr_count;
823 exarg_T *eap;
824 int forceit;
825 int do_in, do_out;
826{
827 char_u *arg = eap->arg; /* command */
828 linenr_T line1 = eap->line1; /* start of range */
829 linenr_T line2 = eap->line2; /* end of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 char_u *newcmd = NULL; /* the new command */
831 int free_newcmd = FALSE; /* need to free() newcmd */
832 int ins_prevcmd;
833 char_u *t;
834 char_u *p;
835 char_u *trailarg;
836 int len;
837 int scroll_save = msg_scroll;
838
839 /*
840 * Disallow shell commands for "rvim".
841 * Disallow shell commands from .exrc and .vimrc in current directory for
842 * security reasons.
843 */
844 if (check_restricted() || check_secure())
845 return;
846
847 if (addr_count == 0) /* :! */
848 {
849 msg_scroll = FALSE; /* don't scroll here */
850 autowrite_all();
851 msg_scroll = scroll_save;
852 }
853
854 /*
855 * Try to find an embedded bang, like in :!<cmd> ! [args]
856 * (:!! is indicated by the 'forceit' variable)
857 */
858 ins_prevcmd = forceit;
859 trailarg = arg;
860 do
861 {
862 len = (int)STRLEN(trailarg) + 1;
863 if (newcmd != NULL)
864 len += (int)STRLEN(newcmd);
865 if (ins_prevcmd)
866 {
867 if (prevcmd == NULL)
868 {
869 EMSG(_(e_noprev));
870 vim_free(newcmd);
871 return;
872 }
873 len += (int)STRLEN(prevcmd);
874 }
875 if ((t = alloc(len)) == NULL)
876 {
877 vim_free(newcmd);
878 return;
879 }
880 *t = NUL;
881 if (newcmd != NULL)
882 STRCAT(t, newcmd);
883 if (ins_prevcmd)
884 STRCAT(t, prevcmd);
885 p = t + STRLEN(t);
886 STRCAT(t, trailarg);
887 vim_free(newcmd);
888 newcmd = t;
889
890 /*
891 * Scan the rest of the argument for '!', which is replaced by the
892 * previous command. "\!" is replaced by "!" (this is vi compatible).
893 */
894 trailarg = NULL;
895 while (*p)
896 {
897 if (*p == '!'
898#ifdef RISCOS
899 && (p[1] == ' ' || p[1] == NUL)
900#endif
901 )
902 {
903 if (p > newcmd && p[-1] == '\\')
904 mch_memmove(p - 1, p, (size_t)(STRLEN(p) + 1));
905 else
906 {
907 trailarg = p;
908 *trailarg++ = NUL;
909 ins_prevcmd = TRUE;
910 break;
911 }
912 }
913 ++p;
914 }
915 } while (trailarg != NULL);
916
917 vim_free(prevcmd);
918 prevcmd = newcmd;
919
920 if (bangredo) /* put cmd in redo buffer for ! command */
921 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000922 AppendToRedobuffLit(prevcmd, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 AppendToRedobuff((char_u *)"\n");
924 bangredo = FALSE;
925 }
926 /*
927 * Add quotes around the command, for shells that need them.
928 */
929 if (*p_shq != NUL)
930 {
931 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
932 if (newcmd == NULL)
933 return;
934 STRCPY(newcmd, p_shq);
935 STRCAT(newcmd, prevcmd);
936 STRCAT(newcmd, p_shq);
937 free_newcmd = TRUE;
938 }
939 if (addr_count == 0) /* :! */
940 {
941 /* echo the command */
942 msg_start();
943 msg_putchar(':');
944 msg_putchar('!');
945 msg_outtrans(newcmd);
946 msg_clr_eos();
947 windgoto(msg_row, msg_col);
948
949 do_shell(newcmd, 0);
950 }
951 else /* :range! */
952 /* Careful: This may recursively call do_bang() again! (because of
953 * autocommands) */
954 do_filter(line1, line2, eap, newcmd, do_in, do_out);
955 if (free_newcmd)
956 vim_free(newcmd);
957}
958
959/*
960 * do_filter: filter lines through a command given by the user
961 *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 * We mostly use temp files and the call_shell() routine here. This would
963 * normally be done using pipes on a UNIX machine, but this is more portable
964 * to non-unix machines. The call_shell() routine needs to be able
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 * to deal with redirection somehow, and should handle things like looking
966 * at the PATH env. variable, and adding reasonable extensions to the
967 * command name given by the user. All reasonable versions of call_shell()
968 * do this.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000969 * Alternatively, if on Unix and redirecting input or output, but not both,
970 * and the 'shelltemp' option isn't set, use pipes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 * We use input redirection if do_in is TRUE.
972 * We use output redirection if do_out is TRUE.
973 */
974 static void
975do_filter(line1, line2, eap, cmd, do_in, do_out)
976 linenr_T line1, line2;
977 exarg_T *eap; /* for forced 'ff' and 'fenc' */
978 char_u *cmd;
979 int do_in, do_out;
980{
981 char_u *itmp = NULL;
982 char_u *otmp = NULL;
983 linenr_T linecount;
984 linenr_T read_linecount;
985 pos_T cursor_save;
986 char_u *cmd_buf;
987#ifdef FEAT_AUTOCMD
988 buf_T *old_curbuf = curbuf;
989#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000990 int shell_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 if (*cmd == NUL) /* no filter command */
993 return;
994
995#ifdef WIN3264
996 /*
997 * Check if external commands are allowed now.
998 */
999 if (can_end_termcap_mode(TRUE) == FALSE)
1000 return;
1001#endif
1002
1003 cursor_save = curwin->w_cursor;
1004 linecount = line2 - line1 + 1;
1005 curwin->w_cursor.lnum = line1;
1006 curwin->w_cursor.col = 0;
1007 changed_line_abv_curs();
1008 invalidate_botline();
1009
1010 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 * When using temp files:
1012 * 1. * Form temp file names
1013 * 2. * Write the lines to a temp file
1014 * 3. Run the filter command on the temp file
1015 * 4. * Read the output of the command into the buffer
1016 * 5. * Delete the original lines to be filtered
1017 * 6. * Remove the temp files
1018 *
1019 * When writing the input with a pipe or when catching the output with a
1020 * pipe only need to do 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 */
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023 if (do_out)
1024 shell_flags |= SHELL_DOOUT;
1025
1026#if !defined(USE_SYSTEM) && defined(UNIX)
1027 if (!do_in && do_out && !p_stmp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 /* Use a pipe to fetch stdout of the command, do not use a temp file. */
1030 shell_flags |= SHELL_READ;
1031 curwin->w_cursor.lnum = line2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 else if (do_in && !do_out && !p_stmp)
1034 {
1035 /* Use a pipe to write stdin of the command, do not use a temp file. */
1036 shell_flags |= SHELL_WRITE;
1037 curbuf->b_op_start.lnum = line1;
1038 curbuf->b_op_end.lnum = line2;
1039 }
1040 else if (do_in && do_out && !p_stmp)
1041 {
1042 /* Use a pipe to write stdin and fetch stdout of the command, do not
1043 * use a temp file. */
1044 shell_flags |= SHELL_READ|SHELL_WRITE;
1045 curbuf->b_op_start.lnum = line1;
1046 curbuf->b_op_end.lnum = line2;
1047 curwin->w_cursor.lnum = line2;
1048 }
1049 else
1050#endif
1051 if ((do_in && (itmp = vim_tempname('i')) == NULL)
1052 || (do_out && (otmp = vim_tempname('o')) == NULL))
1053 {
1054 EMSG(_(e_notmp));
1055 goto filterend;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058/*
1059 * The writing and reading of temp files will not be shown.
1060 * Vi also doesn't do this and the messages are not very informative.
1061 */
1062 ++no_wait_return; /* don't call wait_return() while busy */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 FALSE, FALSE, FALSE, TRUE) == FAIL)
1065 {
1066 msg_putchar('\n'); /* keep message from buf_write() */
1067 --no_wait_return;
1068#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1069 if (!aborting())
1070#endif
1071 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1072 goto filterend;
1073 }
1074#ifdef FEAT_AUTOCMD
1075 if (curbuf != old_curbuf)
1076 goto filterend;
1077#endif
1078
1079 if (!do_out)
1080 msg_putchar('\n');
1081
1082 cmd_buf = make_filter_cmd(cmd, itmp, otmp);
1083 if (cmd_buf == NULL)
1084 goto filterend;
1085
1086 windgoto((int)Rows - 1, 0);
1087 cursor_on();
1088
1089 /*
1090 * When not redirecting the output the command can write anything to the
1091 * screen. If 'shellredir' is equal to ">", screen may be messed up by
1092 * stderr output of external command. Clear the screen later.
1093 * If do_in is FALSE, this could be something like ":r !cat", which may
1094 * also mess up the screen, clear it later.
1095 */
1096 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
1097 redraw_later_clear();
1098
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099 if (do_out)
1100 {
1101 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
1102 goto error;
1103 redraw_curbuf_later(VALID);
1104 }
1105 read_linecount = curbuf->b_ml.ml_line_count;
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 /*
1108 * When call_shell() fails wait_return() is called to give the user a
1109 * chance to read the error messages. Otherwise errors are ignored, so you
1110 * can see the error messages from the command that appear on stdout; use
1111 * 'u' to fix the text
1112 * Switch to cooked mode when not redirecting stdin, avoids that something
1113 * like ":r !cat" hangs.
1114 * Pass on the SHELL_DOOUT flag when the output is being redirected.
1115 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
1118 redraw_later_clear();
1119 wait_return(FALSE);
1120 }
1121 vim_free(cmd_buf);
1122
1123 did_check_timestamps = FALSE;
1124 need_check_timestamps = TRUE;
1125
1126 /* When interrupting the shell command, it may still have produced some
1127 * useful output. Reset got_int here, so that readfile() won't cancel
1128 * reading. */
1129 ui_breakcheck();
1130 got_int = FALSE;
1131
1132 if (do_out)
1133 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 if (otmp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1137 eap, READ_FILTER) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1140 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 {
1143 msg_putchar('\n');
1144 EMSG2(_(e_notread), otmp);
1145 }
1146 goto error;
1147 }
1148#ifdef FEAT_AUTOCMD
1149 if (curbuf != old_curbuf)
1150 goto filterend;
1151#endif
1152 }
1153
1154 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1155
1156 if (shell_flags & SHELL_READ)
1157 {
1158 curbuf->b_op_start.lnum = line2 + 1;
1159 curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
1160 appended_lines_mark(line2, read_linecount);
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
1163 if (do_in)
1164 {
1165 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
1166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (read_linecount >= linecount)
1168 /* move all marks from old lines to new lines */
1169 mark_adjust(line1, line2, linecount, 0L);
1170 else
1171 {
1172 /* move marks from old lines to new lines, delete marks
1173 * that are in deleted lines */
1174 mark_adjust(line1, line1 + read_linecount - 1,
1175 linecount, 0L);
1176 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
1177 }
1178 }
1179
1180 /*
1181 * Put cursor on first filtered line for ":range!cmd".
1182 * Adjust '[ and '] (set by buf_write()).
1183 */
1184 curwin->w_cursor.lnum = line1;
1185 del_lines(linecount, TRUE);
1186 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
1187 curbuf->b_op_end.lnum -= linecount; /* adjust '] */
1188 write_lnum_adjust(-linecount); /* adjust last line
1189 for next write */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001190#ifdef FEAT_FOLDING
1191 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
1192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 else
1195 {
1196 /*
1197 * Put cursor on last new line for ":r !cmd".
1198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
1204 --no_wait_return;
1205
1206 if (linecount > p_report)
1207 {
1208 if (do_in)
1209 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001210 vim_snprintf((char *)msg_buf, sizeof(msg_buf),
1211 _("%ld lines filtered"), (long)linecount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (msg(msg_buf) && !msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00001214 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 else
1217 msgmore((long)linecount);
1218 }
1219 }
1220 else
1221 {
1222error:
1223 /* put cursor back in same position for ":w !cmd" */
1224 curwin->w_cursor = cursor_save;
1225 --no_wait_return;
1226 wait_return(FALSE);
1227 }
1228
1229filterend:
1230
1231#ifdef FEAT_AUTOCMD
1232 if (curbuf != old_curbuf)
1233 {
1234 --no_wait_return;
1235 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1236 }
1237#endif
1238 if (itmp != NULL)
1239 mch_remove(itmp);
1240 if (otmp != NULL)
1241 mch_remove(otmp);
1242 vim_free(itmp);
1243 vim_free(otmp);
1244}
1245
1246/*
1247 * Call a shell to execute a command.
1248 * When "cmd" is NULL start an interactive shell.
1249 */
1250 void
1251do_shell(cmd, flags)
1252 char_u *cmd;
1253 int flags; /* may be SHELL_DOOUT when output is redirected */
1254{
1255 buf_T *buf;
1256#ifndef FEAT_GUI_MSWIN
1257 int save_nwr;
1258#endif
1259#ifdef MSWIN
1260 int winstart = FALSE;
1261#endif
1262
1263 /*
1264 * Disallow shell commands for "rvim".
1265 * Disallow shell commands from .exrc and .vimrc in current directory for
1266 * security reasons.
1267 */
1268 if (check_restricted() || check_secure())
1269 {
1270 msg_end();
1271 return;
1272 }
1273
1274#ifdef MSWIN
1275 /*
1276 * Check if external commands are allowed now.
1277 */
1278 if (can_end_termcap_mode(TRUE) == FALSE)
1279 return;
1280
1281 /*
1282 * Check if ":!start" is used.
1283 */
1284 if (cmd != NULL)
1285 winstart = (STRNICMP(cmd, "start ", 6) == 0);
1286#endif
1287
1288 /*
1289 * For autocommands we want to get the output on the current screen, to
1290 * avoid having to type return below.
1291 */
1292 msg_putchar('\r'); /* put cursor at start of line */
1293#ifdef FEAT_AUTOCMD
1294 if (!autocmd_busy)
1295#endif
1296 {
1297#ifdef MSWIN
1298 if (!winstart)
1299#endif
1300 stoptermcap();
1301 }
1302#ifdef MSWIN
1303 if (!winstart)
1304#endif
1305 msg_putchar('\n'); /* may shift screen one line up */
1306
1307 /* warning message before calling the shell */
1308 if (p_warn
1309#ifdef FEAT_AUTOCMD
1310 && !autocmd_busy
1311#endif
1312 && msg_silent == 0)
1313 for (buf = firstbuf; buf; buf = buf->b_next)
1314 if (bufIsChanged(buf))
1315 {
1316#ifdef FEAT_GUI_MSWIN
1317 if (!winstart)
1318 starttermcap(); /* don't want a message box here */
1319#endif
1320 MSG_PUTS(_("[No write since last change]\n"));
1321#ifdef FEAT_GUI_MSWIN
1322 if (!winstart)
1323 stoptermcap();
1324#endif
1325 break;
1326 }
1327
1328 /* This windgoto is required for when the '\n' resulted in a "delete line
1329 * 1" command to the terminal. */
1330 if (!swapping_screen())
1331 windgoto(msg_row, msg_col);
1332 cursor_on();
1333 (void)call_shell(cmd, SHELL_COOKED | flags);
1334 did_check_timestamps = FALSE;
1335 need_check_timestamps = TRUE;
1336
1337 /*
1338 * put the message cursor at the end of the screen, avoids wait_return()
1339 * to overwrite the text that the external command showed
1340 */
1341 if (!swapping_screen())
1342 {
1343 msg_row = Rows - 1;
1344 msg_col = 0;
1345 }
1346
1347#ifdef FEAT_AUTOCMD
1348 if (autocmd_busy)
1349 {
1350 if (msg_silent == 0)
1351 redraw_later_clear();
1352 }
1353 else
1354#endif
1355 {
1356 /*
1357 * For ":sh" there is no need to call wait_return(), just redraw.
1358 * Also for the Win32 GUI (the output is in a console window).
1359 * Otherwise there is probably text on the screen that the user wants
1360 * to read before redrawing, so call wait_return().
1361 */
1362#ifndef FEAT_GUI_MSWIN
1363 if (cmd == NULL
1364# ifdef WIN3264
1365 || (winstart && !need_wait_return)
1366# endif
1367 )
1368 {
1369 if (msg_silent == 0)
1370 redraw_later_clear();
1371 need_wait_return = FALSE;
1372 }
1373 else
1374 {
1375 /*
1376 * If we switch screens when starttermcap() is called, we really
1377 * want to wait for "hit return to continue".
1378 */
1379 save_nwr = no_wait_return;
1380 if (swapping_screen())
1381 no_wait_return = FALSE;
1382# ifdef AMIGA
1383 wait_return(term_console ? -1 : msg_silent == 0); /* see below */
1384# else
1385 wait_return(msg_silent == 0);
1386# endif
1387 no_wait_return = save_nwr;
1388 }
1389#endif /* FEAT_GUI_W32 */
1390
1391#ifdef MSWIN
1392 if (!winstart) /* if winstart==TRUE, never stopped termcap! */
1393#endif
1394 starttermcap(); /* start termcap if not done by wait_return() */
1395
1396 /*
1397 * In an Amiga window redrawing is caused by asking the window size.
1398 * If we got an interrupt this will not work. The chance that the
1399 * window size is wrong is very small, but we need to redraw the
1400 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
1401 * but it saves an extra redraw.
1402 */
1403#ifdef AMIGA
1404 if (skip_redraw) /* ':' hit in wait_return() */
1405 {
1406 if (msg_silent == 0)
1407 redraw_later_clear();
1408 }
1409 else if (term_console)
1410 {
1411 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
1412 if (got_int && msg_silent == 0)
1413 redraw_later_clear(); /* if got_int is TRUE, redraw needed */
1414 else
1415 must_redraw = 0; /* no extra redraw needed */
1416 }
1417#endif
1418 }
1419
1420 /* display any error messages now */
1421 display_errors();
1422}
1423
1424/*
1425 * Create a shell command from a command string, input redirection file and
1426 * output redirection file.
1427 * Returns an allocated string with the shell command, or NULL for failure.
1428 */
1429 char_u *
1430make_filter_cmd(cmd, itmp, otmp)
1431 char_u *cmd; /* command */
1432 char_u *itmp; /* NULL or name of input file */
1433 char_u *otmp; /* NULL or name of output file */
1434{
1435 char_u *buf;
1436 long_u len;
1437
1438 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
1439 if (itmp != NULL)
1440 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
1441 if (otmp != NULL)
1442 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
1443 buf = lalloc(len, TRUE);
1444 if (buf == NULL)
1445 return NULL;
1446
1447#if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2)
1448 /*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001449 * Put braces around the command (for concatenated commands) when
1450 * redirecting input and/or output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001452 if (itmp != NULL || otmp != NULL)
1453 sprintf((char *)buf, "(%s)", (char *)cmd);
1454 else
1455 STRCPY(buf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (itmp != NULL)
1457 {
1458 STRCAT(buf, " < ");
1459 STRCAT(buf, itmp);
1460 }
1461#else
1462 /*
1463 * for shells that don't understand braces around commands, at least allow
1464 * the use of commands in a pipe.
1465 */
1466 STRCPY(buf, cmd);
1467 if (itmp != NULL)
1468 {
1469 char_u *p;
1470
1471 /*
1472 * If there is a pipe, we have to put the '<' in front of it.
1473 * Don't do this when 'shellquote' is not empty, otherwise the
1474 * redirection would be inside the quotes.
1475 */
1476 if (*p_shq == NUL)
1477 {
1478 p = vim_strchr(buf, '|');
1479 if (p != NULL)
1480 *p = NUL;
1481 }
1482# ifdef RISCOS
1483 STRCAT(buf, " { < "); /* Use RISC OS notation for input. */
1484 STRCAT(buf, itmp);
1485 STRCAT(buf, " } ");
1486# else
1487 STRCAT(buf, " <"); /* " < " causes problems on Amiga */
1488 STRCAT(buf, itmp);
1489# endif
1490 if (*p_shq == NUL)
1491 {
1492 p = vim_strchr(cmd, '|');
1493 if (p != NULL)
1494 {
1495 STRCAT(buf, " "); /* insert a space before the '|' for DOS */
1496 STRCAT(buf, p);
1497 }
1498 }
1499 }
1500#endif
1501 if (otmp != NULL)
1502 append_redir(buf, p_srr, otmp);
1503
1504 return buf;
1505}
1506
1507/*
1508 * Append output redirection for file "fname" to the end of string buffer "buf"
1509 * Works with the 'shellredir' and 'shellpipe' options.
1510 * The caller should make sure that there is enough room:
1511 * STRLEN(opt) + STRLEN(fname) + 3
1512 */
1513 void
1514append_redir(buf, opt, fname)
1515 char_u *buf;
1516 char_u *opt;
1517 char_u *fname;
1518{
1519 char_u *p;
1520
1521 buf += STRLEN(buf);
1522 /* find "%s", skipping "%%" */
1523 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
1524 if (p[1] == 's')
1525 break;
1526 if (p != NULL)
1527 {
1528 *buf = ' '; /* not really needed? Not with sh, ksh or bash */
1529 sprintf((char *)buf + 1, (char *)opt, (char *)fname);
1530 }
1531 else
1532 sprintf((char *)buf,
1533#ifdef FEAT_QUICKFIX
1534# ifndef RISCOS
1535 opt != p_sp ? " %s%s" :
1536# endif
1537 " %s %s",
1538#else
1539# ifndef RISCOS
1540 " %s%s", /* " > %s" causes problems on Amiga */
1541# else
1542 " %s %s", /* But is needed for 'shellpipe' and RISC OS */
1543# endif
1544#endif
1545 (char *)opt, (char *)fname);
1546}
1547
1548#ifdef FEAT_VIMINFO
1549
1550static int no_viminfo __ARGS((void));
1551static int viminfo_errcnt;
1552
1553 static int
1554no_viminfo()
1555{
1556 /* "vim -i NONE" does not read or write a viminfo file */
1557 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
1558}
1559
1560/*
1561 * Report an error for reading a viminfo file.
1562 * Count the number of errors. When there are more than 10, return TRUE.
1563 */
1564 int
1565viminfo_error(errnum, message, line)
1566 char *errnum;
1567 char *message;
1568 char_u *line;
1569{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001570 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1571 errnum, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff));
Bram Moolenaar758711c2005-02-02 23:11:38 +00001573 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1574 IObuff[STRLEN(IObuff) - 1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 emsg(IObuff);
1576 if (++viminfo_errcnt >= 10)
1577 {
1578 EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
1579 return TRUE;
1580 }
1581 return FALSE;
1582}
1583
1584/*
1585 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
1586 * set are not over-written unless force is TRUE. -- webb
1587 */
1588 int
1589read_viminfo(file, want_info, want_marks, forceit)
1590 char_u *file;
1591 int want_info;
1592 int want_marks;
1593 int forceit;
1594{
1595 FILE *fp;
1596 char_u *fname;
1597
1598 if (no_viminfo())
1599 return FAIL;
1600
1601 fname = viminfo_filename(file); /* may set to default if NULL */
1602 if (fname == NULL)
1603 return FAIL;
1604 fp = mch_fopen((char *)fname, READBIN);
1605
1606 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001607 {
1608 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001609 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
1610 fname,
1611 want_info ? _(" info") : "",
1612 want_marks ? _(" marks") : "",
1613 fp == NULL ? _(" FAILED") : "");
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001614 verbose_leave();
1615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 vim_free(fname);
1618 if (fp == NULL)
1619 return FAIL;
1620
1621 viminfo_errcnt = 0;
1622 do_viminfo(fp, NULL, want_info, want_marks, forceit);
1623
1624 fclose(fp);
1625
1626 return OK;
1627}
1628
1629/*
1630 * write_viminfo() -- Write the viminfo file. The old one is read in first so
1631 * that effectively a merge of current info and old info is done. This allows
1632 * multiple vims to run simultaneously, without losing any marks etc. If
1633 * forceit is TRUE, then the old file is not read in, and only internal info is
1634 * written to the file. -- webb
1635 */
1636 void
1637write_viminfo(file, forceit)
1638 char_u *file;
1639 int forceit;
1640{
1641 char_u *fname;
1642 FILE *fp_in = NULL; /* input viminfo file, if any */
1643 FILE *fp_out = NULL; /* output viminfo file */
1644 char_u *tempname = NULL; /* name of temp viminfo file */
1645 struct stat st_new; /* mch_stat() of potential new file */
1646 char_u *wp;
1647#if defined(UNIX) || defined(VMS)
1648 mode_t umask_save;
1649#endif
1650#ifdef UNIX
1651 int shortname = FALSE; /* use 8.3 file name */
1652 struct stat st_old; /* mch_stat() of existing viminfo file */
1653#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001654#ifdef WIN3264
1655 long perm = -1;
1656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 if (no_viminfo())
1659 return;
1660
1661 fname = viminfo_filename(file); /* may set to default if NULL */
1662 if (fname == NULL)
1663 return;
1664
1665 fp_in = mch_fopen((char *)fname, READBIN);
1666 if (fp_in == NULL)
1667 {
1668 /* if it does exist, but we can't read it, don't try writing */
1669 if (mch_stat((char *)fname, &st_new) == 0)
1670 goto end;
1671#if defined(UNIX) || defined(VMS)
1672 /*
1673 * For Unix we create the .viminfo non-accessible for others,
1674 * because it may contain text from non-accessible documents.
1675 */
1676 umask_save = umask(077);
1677#endif
1678 fp_out = mch_fopen((char *)fname, WRITEBIN);
1679#if defined(UNIX) || defined(VMS)
1680 (void)umask(umask_save);
1681#endif
1682 }
1683 else
1684 {
1685 /*
1686 * There is an existing viminfo file. Create a temporary file to
1687 * write the new viminfo into, in the same directory as the
1688 * existing viminfo file, which will be renamed later.
1689 */
1690#ifdef UNIX
1691 /*
1692 * For Unix we check the owner of the file. It's not very nice to
1693 * overwrite a user's viminfo file after a "su root", with a
1694 * viminfo file that the user can't read.
1695 */
1696 st_old.st_dev = st_old.st_ino = 0;
1697 st_old.st_mode = 0600;
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001698 if (mch_stat((char *)fname, &st_old) == 0 && getuid()
1699 && !(st_old.st_uid == getuid()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ? (st_old.st_mode & 0200)
1701 : (st_old.st_gid == getgid()
1702 ? (st_old.st_mode & 0020)
1703 : (st_old.st_mode & 0002))))
1704 {
1705 int tt;
1706
1707 /* avoid a wait_return for this message, it's annoying */
1708 tt = msg_didany;
1709 EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
1710 msg_didany = tt;
1711 goto end;
1712 }
1713#endif
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001714#ifdef WIN3264
1715 /* Get the file attributes of the existing viminfo file. */
1716 perm = mch_getperm(fname);
1717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719 /*
1720 * Make tempname.
1721 * May try twice: Once normal and once with shortname set, just in
1722 * case somebody puts his viminfo file in an 8.3 filesystem.
1723 */
1724 for (;;)
1725 {
1726 tempname = buf_modname(
1727#ifdef UNIX
1728 shortname,
1729#else
1730# ifdef SHORT_FNAME
1731 TRUE,
1732# else
1733# ifdef FEAT_GUI_W32
1734 gui_is_win32s(),
1735# else
1736 FALSE,
1737# endif
1738# endif
1739#endif
1740 fname,
1741#ifdef VMS
1742 (char_u *)"-tmp",
1743#else
1744# ifdef RISCOS
1745 (char_u *)"/tmp",
1746# else
1747 (char_u *)".tmp",
1748# endif
1749#endif
1750 FALSE);
1751 if (tempname == NULL) /* out of memory */
1752 break;
1753
1754 /*
1755 * Check if tempfile already exists. Never overwrite an
1756 * existing file!
1757 */
1758 if (mch_stat((char *)tempname, &st_new) == 0)
1759 {
1760#ifdef UNIX
1761 /*
1762 * Check if tempfile is same as original file. May happen
1763 * when modname() gave the same file back. E.g. silly
1764 * link, or file name-length reached. Try again with
1765 * shortname set.
1766 */
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001767 if (!shortname && st_new.st_dev == st_old.st_dev
1768 && st_new.st_ino == st_old.st_ino)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770 vim_free(tempname);
1771 tempname = NULL;
1772 shortname = TRUE;
1773 continue;
1774 }
1775#endif
1776 /*
1777 * Try another name. Change one character, just before
1778 * the extension. This should also work for an 8.3
1779 * file name, when after adding the extension it still is
1780 * the same file as the original.
1781 */
1782 wp = tempname + STRLEN(tempname) - 5;
1783 if (wp < gettail(tempname)) /* empty file name? */
1784 wp = gettail(tempname);
1785 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
1786 --*wp)
1787 {
1788 /*
1789 * They all exist? Must be something wrong! Don't
1790 * write the viminfo file then.
1791 */
1792 if (*wp == 'a')
1793 {
1794 vim_free(tempname);
1795 tempname = NULL;
1796 break;
1797 }
1798 }
1799 }
1800 break;
1801 }
1802
1803 if (tempname != NULL)
1804 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00001805 int fd;
1806
1807 /* Use mch_open() to be able to use O_NOFOLLOW and set file
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001808 * protection:
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001809 * Unix: same as original file, but strip s-bit. Reset umask to
1810 * avoid it getting in the way.
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001811 * Others: r&w for user only. */
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001812#ifdef UNIX
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001813 umask_save = umask(0);
Bram Moolenaara5792f52005-11-23 21:25:05 +00001814 fd = mch_open((char *)tempname,
1815 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001816 (int)((st_old.st_mode & 0777) | 0600));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001817 (void)umask(umask_save);
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001818#else
Bram Moolenaarbba577a2005-11-28 23:05:55 +00001819 fd = mch_open((char *)tempname,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001820 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
Bram Moolenaar12625ca2005-11-25 19:58:47 +00001821#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00001822 if (fd < 0)
1823 fp_out = NULL;
1824 else
1825 fp_out = fdopen(fd, WRITEBIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
1827 /*
1828 * If we can't create in the same directory, try creating a
1829 * "normal" temp file.
1830 */
1831 if (fp_out == NULL)
1832 {
1833 vim_free(tempname);
1834 if ((tempname = vim_tempname('o')) != NULL)
1835 fp_out = mch_fopen((char *)tempname, WRITEBIN);
1836 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00001837
1838#if defined(UNIX) && defined(HAVE_FCHOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /*
Bram Moolenaara5792f52005-11-23 21:25:05 +00001840 * Make sure the owner can read/write it. This only works for
1841 * root.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 if (fp_out != NULL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001844 (void)fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845#endif
1846 }
1847 }
1848
1849 /*
1850 * Check if the new viminfo file can be written to.
1851 */
1852 if (fp_out == NULL)
1853 {
1854 EMSG2(_("E138: Can't write viminfo file %s!"),
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001855 (fp_in == NULL || tempname == NULL) ? fname : tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (fp_in != NULL)
1857 fclose(fp_in);
1858 goto end;
1859 }
1860
1861 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001862 {
1863 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00001864 smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001865 verbose_leave();
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 viminfo_errcnt = 0;
1869 do_viminfo(fp_in, fp_out, !forceit, !forceit, FALSE);
1870
1871 fclose(fp_out); /* errors are ignored !? */
1872 if (fp_in != NULL)
1873 {
1874 fclose(fp_in);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 /*
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001877 * In case of an error keep the original viminfo file.
1878 * Otherwise rename the newly written file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 */
1880 if (viminfo_errcnt || vim_rename(tempname, fname) == -1)
1881 mch_remove(tempname);
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001882
1883#ifdef WIN3264
1884 /* If the viminfo file was hidden then also hide the new file. */
1885 if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN))
1886 mch_hide(fname);
1887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
Bram Moolenaarbca84a12005-12-14 22:08:35 +00001889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890end:
1891 vim_free(fname);
1892 vim_free(tempname);
1893}
1894
1895/*
1896 * Get the viminfo file name to use.
1897 * If "file" is given and not empty, use it (has already been expanded by
1898 * cmdline functions).
1899 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
1900 * expand environment variables.
1901 * Returns an allocated string. NULL when out of memory.
1902 */
1903 static char_u *
1904viminfo_filename(file)
1905 char_u *file;
1906{
1907 if (file == NULL || *file == NUL)
1908 {
1909 if (use_viminfo != NULL)
1910 file = use_viminfo;
1911 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
1912 {
1913#ifdef VIMINFO_FILE2
1914 /* don't use $HOME when not defined (turned into "c:/"!). */
1915# ifdef VMS
1916 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
1917# else
1918 if (mch_getenv((char_u *)"HOME") == NULL)
1919# endif
1920 {
1921 /* don't use $VIM when not available. */
1922 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
1923 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
1924 file = (char_u *)VIMINFO_FILE2;
1925 else
1926 file = (char_u *)VIMINFO_FILE;
1927 }
1928 else
1929#endif
1930 file = (char_u *)VIMINFO_FILE;
1931 }
1932 expand_env(file, NameBuff, MAXPATHL);
1933 file = NameBuff;
1934 }
1935 return vim_strsave(file);
1936}
1937
1938/*
1939 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
1940 */
1941 static void
1942do_viminfo(fp_in, fp_out, want_info, want_marks, force_read)
1943 FILE *fp_in;
1944 FILE *fp_out;
1945 int want_info;
1946 int want_marks;
1947 int force_read;
1948{
1949 int count = 0;
1950 int eof = FALSE;
1951 vir_T vir;
1952
1953 if ((vir.vir_line = alloc(LSIZE)) == NULL)
1954 return;
1955 vir.vir_fd = fp_in;
1956#ifdef FEAT_MBYTE
1957 vir.vir_conv.vc_type = CONV_NONE;
1958#endif
1959
1960 if (fp_in != NULL)
1961 {
1962 if (want_info)
1963 eof = read_viminfo_up_to_marks(&vir, force_read, fp_out != NULL);
1964 else
1965 /* Skip info, find start of marks */
1966 while (!(eof = viminfo_readline(&vir))
1967 && vir.vir_line[0] != '>')
1968 ;
1969 }
1970 if (fp_out != NULL)
1971 {
1972 /* Write the info: */
1973 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
1974 VIM_VERSION_MEDIUM);
1975 fprintf(fp_out, _("# You may edit it if you're careful!\n\n"));
1976#ifdef FEAT_MBYTE
1977 fprintf(fp_out, _("# Value of 'encoding' when this file was written\n"));
1978 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
1979#endif
1980 write_viminfo_search_pattern(fp_out);
1981 write_viminfo_sub_string(fp_out);
1982#ifdef FEAT_CMDHIST
1983 write_viminfo_history(fp_out);
1984#endif
1985 write_viminfo_registers(fp_out);
1986#ifdef FEAT_EVAL
1987 write_viminfo_varlist(fp_out);
1988#endif
1989 write_viminfo_filemarks(fp_out);
1990 write_viminfo_bufferlist(fp_out);
1991 count = write_viminfo_marks(fp_out);
1992 }
1993 if (fp_in != NULL && want_marks)
1994 copy_viminfo_marks(&vir, fp_out, count, eof);
1995
1996 vim_free(vir.vir_line);
1997#ifdef FEAT_MBYTE
1998 if (vir.vir_conv.vc_type != CONV_NONE)
1999 convert_setup(&vir.vir_conv, NULL, NULL);
2000#endif
2001}
2002
2003/*
2004 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2005 * first part of the viminfo file which contains everything but the marks that
2006 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2007 */
2008 static int
2009read_viminfo_up_to_marks(virp, forceit, writing)
2010 vir_T *virp;
2011 int forceit;
2012 int writing;
2013{
2014 int eof;
2015 buf_T *buf;
2016
2017#ifdef FEAT_CMDHIST
2018 prepare_viminfo_history(forceit ? 9999 : 0);
2019#endif
2020 eof = viminfo_readline(virp);
2021 while (!eof && virp->vir_line[0] != '>')
2022 {
2023 switch (virp->vir_line[0])
2024 {
2025 /* Characters reserved for future expansion, ignored now */
2026 case '+': /* "+40 /path/dir file", for running vim without args */
2027 case '|': /* to be defined */
2028 case '^': /* to be defined */
2029 case '<': /* long line - ignored */
2030 /* A comment or empty line. */
2031 case NUL:
2032 case '\r':
2033 case '\n':
2034 case '#':
2035 eof = viminfo_readline(virp);
2036 break;
2037 case '*': /* "*encoding=value" */
2038 eof = viminfo_encoding(virp);
2039 break;
2040 case '!': /* global variable */
2041#ifdef FEAT_EVAL
2042 eof = read_viminfo_varlist(virp, writing);
2043#else
2044 eof = viminfo_readline(virp);
2045#endif
2046 break;
2047 case '%': /* entry for buffer list */
2048 eof = read_viminfo_bufferlist(virp, writing);
2049 break;
2050 case '"':
2051 eof = read_viminfo_register(virp, forceit);
2052 break;
2053 case '/': /* Search string */
2054 case '&': /* Substitute search string */
2055 case '~': /* Last search string, followed by '/' or '&' */
2056 eof = read_viminfo_search_pattern(virp, forceit);
2057 break;
2058 case '$':
2059 eof = read_viminfo_sub_string(virp, forceit);
2060 break;
2061 case ':':
2062 case '?':
2063 case '=':
2064 case '@':
2065#ifdef FEAT_CMDHIST
2066 eof = read_viminfo_history(virp);
2067#else
2068 eof = viminfo_readline(virp);
2069#endif
2070 break;
2071 case '-':
2072 case '\'':
2073 eof = read_viminfo_filemark(virp, forceit);
2074 break;
2075 default:
2076 if (viminfo_error("E575: ", _("Illegal starting char"),
2077 virp->vir_line))
2078 eof = TRUE;
2079 else
2080 eof = viminfo_readline(virp);
2081 break;
2082 }
2083 }
2084
2085#ifdef FEAT_CMDHIST
2086 /* Finish reading history items. */
2087 finish_viminfo_history();
2088#endif
2089
2090 /* Change file names to buffer numbers for fmarks. */
2091 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2092 fmarks_check_names(buf);
2093
2094 return eof;
2095}
2096
2097/*
2098 * Compare the 'encoding' value in the viminfo file with the current value of
2099 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
2100 * conversion of text with iconv() in viminfo_readstring().
2101 */
2102 static int
2103viminfo_encoding(virp)
2104 vir_T *virp;
2105{
2106#ifdef FEAT_MBYTE
2107 char_u *p;
2108 int i;
2109
2110 if (get_viminfo_parameter('c') != 0)
2111 {
2112 p = vim_strchr(virp->vir_line, '=');
2113 if (p != NULL)
2114 {
2115 /* remove trailing newline */
2116 ++p;
2117 for (i = 0; vim_isprintc(p[i]); ++i)
2118 ;
2119 p[i] = NUL;
2120
2121 convert_setup(&virp->vir_conv, p, p_enc);
2122 }
2123 }
2124#endif
2125 return viminfo_readline(virp);
2126}
2127
2128/*
2129 * Read a line from the viminfo file.
2130 * Returns TRUE for end-of-file;
2131 */
2132 int
2133viminfo_readline(virp)
2134 vir_T *virp;
2135{
2136 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2137}
2138
2139/*
2140 * check string read from viminfo file
2141 * remove '\n' at the end of the line
2142 * - replace CTRL-V CTRL-V with CTRL-V
2143 * - replace CTRL-V 'n' with '\n'
2144 *
2145 * Check for a long line as written by viminfo_writestring().
2146 *
2147 * Return the string in allocated memory (NULL when out of memory).
2148 */
2149/*ARGSUSED*/
2150 char_u *
2151viminfo_readstring(virp, off, convert)
2152 vir_T *virp;
2153 int off; /* offset for virp->vir_line */
2154 int convert; /* convert the string */
2155{
2156 char_u *retval;
2157 char_u *s, *d;
2158 long len;
2159
2160 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
2161 {
2162 len = atol((char *)virp->vir_line + off + 1);
2163 retval = lalloc(len, TRUE);
2164 if (retval == NULL)
2165 {
2166 /* Line too long? File messed up? Skip next line. */
2167 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
2168 return NULL;
2169 }
2170 (void)vim_fgets(retval, (int)len, virp->vir_fd);
2171 s = retval + 1; /* Skip the leading '<' */
2172 }
2173 else
2174 {
2175 retval = vim_strsave(virp->vir_line + off);
2176 if (retval == NULL)
2177 return NULL;
2178 s = retval;
2179 }
2180
2181 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
2182 d = retval;
2183 while (*s != NUL && *s != '\n')
2184 {
2185 if (s[0] == Ctrl_V && s[1] != NUL)
2186 {
2187 if (s[1] == 'n')
2188 *d++ = '\n';
2189 else
2190 *d++ = Ctrl_V;
2191 s += 2;
2192 }
2193 else
2194 *d++ = *s++;
2195 }
2196 *d = NUL;
2197
2198#ifdef FEAT_MBYTE
2199 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
2200 {
2201 d = string_convert(&virp->vir_conv, retval, NULL);
2202 if (d != NULL)
2203 {
2204 vim_free(retval);
2205 retval = d;
2206 }
2207 }
2208#endif
2209
2210 return retval;
2211}
2212
2213/*
2214 * write string to viminfo file
2215 * - replace CTRL-V with CTRL-V CTRL-V
2216 * - replace '\n' with CTRL-V 'n'
2217 * - add a '\n' at the end
2218 *
2219 * For a long line:
2220 * - write " CTRL-V <length> \n " in first line
2221 * - write " < <string> \n " in second line
2222 */
2223 void
2224viminfo_writestring(fd, p)
2225 FILE *fd;
2226 char_u *p;
2227{
2228 int c;
2229 char_u *s;
2230 int len = 0;
2231
2232 for (s = p; *s != NUL; ++s)
2233 {
2234 if (*s == Ctrl_V || *s == '\n')
2235 ++len;
2236 ++len;
2237 }
2238
2239 /* If the string will be too long, write its length and put it in the next
2240 * line. Take into account that some room is needed for what comes before
2241 * the string (e.g., variable name). Add something to the length for the
2242 * '<', NL and trailing NUL. */
2243 if (len > LSIZE / 2)
2244 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
2245
2246 while ((c = *p++) != NUL)
2247 {
2248 if (c == Ctrl_V || c == '\n')
2249 {
2250 putc(Ctrl_V, fd);
2251 if (c == '\n')
2252 c = 'n';
2253 }
2254 putc(c, fd);
2255 }
2256 putc('\n', fd);
2257}
2258#endif /* FEAT_VIMINFO */
2259
2260/*
2261 * Implementation of ":fixdel", also used by get_stty().
2262 * <BS> resulting <Del>
2263 * ^? ^H
2264 * not ^? ^?
2265 */
2266/*ARGSUSED*/
2267 void
2268do_fixdel(eap)
2269 exarg_T *eap;
2270{
2271 char_u *p;
2272
2273 p = find_termcode((char_u *)"kb");
2274 add_termcode((char_u *)"kD", p != NULL
2275 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
2276}
2277
2278 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002279print_line_no_prefix(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 linenr_T lnum;
2281 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002282 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002284 char_u numbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 if (curwin->w_p_nu || use_number)
2287 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002288 sprintf((char *)numbuf, "%*ld ", number_width(curwin), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
2290 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002291 msg_prt_line(ml_get(lnum), list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292}
2293
2294/*
2295 * Print a text line. Also in silent mode ("ex -s").
2296 */
2297 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002298print_line(lnum, use_number, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 linenr_T lnum;
2300 int use_number;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002301 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 int save_silent = silent_mode;
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 msg_start();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002306 silent_mode = FALSE;
2307 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2308 print_line_no_prefix(lnum, use_number, list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (save_silent)
2310 {
2311 msg_putchar('\n');
2312 cursor_on(); /* msg_start() switches it off */
2313 out_flush();
2314 silent_mode = save_silent;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002315 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317}
2318
2319/*
2320 * ":file[!] [fname]".
2321 */
2322 void
2323ex_file(eap)
2324 exarg_T *eap;
2325{
2326 char_u *fname, *sfname, *xfname;
2327 buf_T *buf;
2328
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002329 /* ":0file" removes the file name. Check for illegal uses ":3file",
2330 * "0file name", etc. */
2331 if (eap->addr_count > 0
2332 && (*eap->arg != NUL
2333 || eap->line2 > 0
2334 || eap->addr_count > 1))
2335 {
2336 EMSG(_(e_invarg));
2337 return;
2338 }
2339
2340 if (*eap->arg != NUL || eap->addr_count == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342#ifdef FEAT_AUTOCMD
2343 buf = curbuf;
2344 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
2345 /* buffer changed, don't change name now */
2346 if (buf != curbuf)
2347 return;
2348# ifdef FEAT_EVAL
2349 if (aborting()) /* autocmds may abort script processing */
2350 return;
2351# endif
2352#endif
2353 /*
2354 * The name of the current buffer will be changed.
2355 * A new (unlisted) buffer entry needs to be made to hold the old file
2356 * name, which will become the alternate file name.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002357 * But don't set the alternate file name if the buffer didn't have a
2358 * name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
2360 fname = curbuf->b_ffname;
2361 sfname = curbuf->b_sfname;
2362 xfname = curbuf->b_fname;
2363 curbuf->b_ffname = NULL;
2364 curbuf->b_sfname = NULL;
2365 if (setfname(curbuf, eap->arg, NULL, TRUE) == FAIL)
2366 {
2367 curbuf->b_ffname = fname;
2368 curbuf->b_sfname = sfname;
2369 return;
2370 }
2371 curbuf->b_flags |= BF_NOTEDITED;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002372 if (xfname != NULL && *xfname != NUL)
2373 {
2374 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
2375 if (buf != NULL && !cmdmod.keepalt)
2376 curwin->w_alt_fnum = buf->b_fnum;
2377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 vim_free(fname);
2379 vim_free(sfname);
2380#ifdef FEAT_AUTOCMD
2381 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
2382#endif
2383 }
2384 /* print full file name if :cd used */
2385 fileinfo(FALSE, FALSE, eap->forceit);
2386}
2387
2388/*
2389 * ":update".
2390 */
2391 void
2392ex_update(eap)
2393 exarg_T *eap;
2394{
2395 if (curbufIsChanged())
2396 (void)do_write(eap);
2397}
2398
2399/*
2400 * ":write" and ":saveas".
2401 */
2402 void
2403ex_write(eap)
2404 exarg_T *eap;
2405{
2406 if (eap->usefilter) /* input lines to shell command */
2407 do_bang(1, eap, FALSE, TRUE, FALSE);
2408 else
2409 (void)do_write(eap);
2410}
2411
2412/*
2413 * write current buffer to file 'eap->arg'
2414 * if 'eap->append' is TRUE, append to the file
2415 *
2416 * if *eap->arg == NUL write to current file
2417 *
2418 * return FAIL for failure, OK otherwise
2419 */
2420 int
2421do_write(eap)
2422 exarg_T *eap;
2423{
2424 int other;
2425 char_u *fname = NULL; /* init to shut up gcc */
2426 char_u *ffname;
2427 int retval = FAIL;
2428 char_u *free_fname = NULL;
2429#ifdef FEAT_BROWSE
2430 char_u *browse_file = NULL;
2431#endif
2432 buf_T *alt_buf = NULL;
2433
2434 if (not_writing()) /* check 'write' option */
2435 return FAIL;
2436
2437 ffname = eap->arg;
2438#ifdef FEAT_BROWSE
2439 if (cmdmod.browse)
2440 {
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002441 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 NULL, NULL, NULL, curbuf);
2443 if (browse_file == NULL)
2444 goto theend;
2445 ffname = browse_file;
2446 }
2447#endif
2448 if (*ffname == NUL)
2449 {
2450 if (eap->cmdidx == CMD_saveas)
2451 {
2452 EMSG(_(e_argreq));
2453 goto theend;
2454 }
2455 other = FALSE;
2456 }
2457 else
2458 {
2459 fname = ffname;
2460 free_fname = fix_fname(ffname);
2461 /*
2462 * When out-of-memory, keep unexpanded file name, because we MUST be
2463 * able to write the file in this situation.
2464 */
2465 if (free_fname != NULL)
2466 ffname = free_fname;
2467 other = otherfile(ffname);
2468 }
2469
2470 /*
2471 * If we have a new file, put its name in the list of alternate file names.
2472 */
2473 if (other)
2474 {
2475 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
2476 || eap->cmdidx == CMD_saveas)
2477 alt_buf = setaltfname(ffname, fname, (linenr_T)1);
2478 else
2479 alt_buf = buflist_findname(ffname);
2480 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
2481 {
2482 /* Overwriting a file that is loaded in another buffer is not a
2483 * good idea. */
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002484 EMSG(_(e_bufloaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 goto theend;
2486 }
2487 }
2488
2489 /*
2490 * Writing to the current file is not allowed in readonly mode
2491 * and a file name is required.
2492 * "nofile" and "nowrite" buffers cannot be written implicitly either.
2493 */
2494 if (!other && (
2495#ifdef FEAT_QUICKFIX
2496 bt_dontwrite_msg(curbuf) ||
2497#endif
2498 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
2499 goto theend;
2500
2501 if (!other)
2502 {
2503 ffname = curbuf->b_ffname;
2504 fname = curbuf->b_fname;
2505 /*
2506 * Not writing the whole file is only allowed with '!'.
2507 */
2508 if ( (eap->line1 != 1
2509 || eap->line2 != curbuf->b_ml.ml_line_count)
2510 && !eap->forceit
2511 && !eap->append
2512 && !p_wa)
2513 {
2514#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2515 if (p_confirm || cmdmod.confirm)
2516 {
2517 if (vim_dialog_yesno(VIM_QUESTION, NULL,
2518 (char_u *)_("Write partial file?"), 2) != VIM_YES)
2519 goto theend;
2520 eap->forceit = TRUE;
2521 }
2522 else
2523#endif
2524 {
2525 EMSG(_("E140: Use ! to write partial buffer"));
2526 goto theend;
2527 }
2528 }
2529 }
2530
2531 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
2532 {
2533 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
2534 {
2535#ifdef FEAT_AUTOCMD
2536 buf_T *was_curbuf = curbuf;
2537
2538 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002539 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540# ifdef FEAT_EVAL
2541 if (curbuf != was_curbuf || aborting())
2542# else
2543 if (curbuf != was_curbuf)
2544# endif
2545 {
2546 /* buffer changed, don't change name now */
2547 retval = FAIL;
2548 goto theend;
2549 }
2550#endif
2551 /* Exchange the file names for the current and the alternate
2552 * buffer. This makes it look like we are now editing the buffer
2553 * under the new name. Must be done before buf_write(), because
2554 * if there is no file name and 'cpo' contains 'F', it will set
2555 * the file name. */
2556 fname = alt_buf->b_fname;
2557 alt_buf->b_fname = curbuf->b_fname;
2558 curbuf->b_fname = fname;
2559 fname = alt_buf->b_ffname;
2560 alt_buf->b_ffname = curbuf->b_ffname;
2561 curbuf->b_ffname = fname;
2562 fname = alt_buf->b_sfname;
2563 alt_buf->b_sfname = curbuf->b_sfname;
2564 curbuf->b_sfname = fname;
2565 buf_name_changed(curbuf);
2566#ifdef FEAT_AUTOCMD
2567 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar269ec652004-07-29 08:43:53 +00002568 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (!alt_buf->b_p_bl)
2570 {
2571 alt_buf->b_p_bl = TRUE;
2572 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
2573 }
2574# ifdef FEAT_EVAL
2575 if (curbuf != was_curbuf || aborting())
2576# else
2577 if (curbuf != was_curbuf)
2578# endif
2579 {
2580 /* buffer changed, don't write the file */
2581 retval = FAIL;
2582 goto theend;
2583 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002584
2585 /* If 'filetype' was empty try detecting it now. */
2586 if (*curbuf->b_p_ft == NUL)
2587 {
2588 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
2589 do_modelines(FALSE);
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591#endif
2592 }
2593
2594 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
2595 eap, eap->append, eap->forceit, TRUE, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598
2599theend:
2600#ifdef FEAT_BROWSE
2601 vim_free(browse_file);
2602#endif
2603 vim_free(free_fname);
2604 return retval;
2605}
2606
2607/*
2608 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED,
2609 * BF_NEW or BF_READERR, check for overwriting current file.
2610 * May set eap->forceit if a dialog says it's OK to overwrite.
2611 * Return OK if it's OK, FAIL if it is not.
2612 */
2613/*ARGSUSED*/
2614 static int
2615check_overwrite(eap, buf, fname, ffname, other)
2616 exarg_T *eap;
2617 buf_T *buf;
2618 char_u *fname; /* file name to be used (can differ from
2619 buf->ffname) */
2620 char_u *ffname; /* full path version of fname */
2621 int other; /* writing under other name */
2622{
2623 /*
2624 * write to other file or b_flags set or not writing the whole file:
2625 * overwriting only allowed with '!'
2626 */
2627 if ( (other
2628 || (buf->b_flags & BF_NOTEDITED)
2629 || ((buf->b_flags & BF_NEW)
2630 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
2631 || (buf->b_flags & BF_READERR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 && !p_wa
2633 && vim_fexists(ffname))
2634 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002635 if (!eap->forceit && !eap->append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002637#ifdef UNIX
Bram Moolenaar0ac93792006-01-21 22:16:51 +00002638 /* with UNIX it is possible to open a directory */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002639 if (mch_isdir(ffname))
2640 {
2641 EMSG2(_(e_isadir2), ffname);
2642 return FAIL;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644#endif
2645#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002646 if (p_confirm || cmdmod.confirm)
2647 {
2648 char_u buff[IOSIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002650 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
2651 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
2652 return FAIL;
2653 eap->forceit = TRUE;
2654 }
2655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#endif
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002657 {
2658 EMSG(_(e_exists));
2659 return FAIL;
2660 }
2661 }
2662
2663 /* For ":w! filename" check that no swap file exists for "filename". */
2664 if (other && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
Bram Moolenaar04a09c12005-08-01 22:02:32 +00002666 char_u dir[MAXPATHL];
2667 char_u *p;
2668 int r;
2669 char_u *swapname;
2670
2671 /* We only try the first entry in 'directory', without checking if
2672 * it's writable. If the "." directory is not writable the write
2673 * will probably fail anyway.
2674 * Use 'shortname' of the current buffer, since there is no buffer
2675 * for the written file. */
2676 if (*p_dir == NUL)
2677 STRCPY(dir, ".");
2678 else
2679 {
2680 p = p_dir;
2681 copy_option_part(&p, dir, MAXPATHL, ",");
2682 }
2683 swapname = makeswapname(fname, ffname, curbuf, dir);
2684 r = vim_fexists(swapname);
2685 vim_free(swapname);
2686 if (r)
2687 {
2688#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2689 if (p_confirm || cmdmod.confirm)
2690 {
2691 char_u buff[IOSIZE];
2692
2693 dialog_msg(buff,
2694 _("Swap file \"%s\" exists, overwrite anyway?"),
2695 swapname);
2696 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
2697 != VIM_YES)
2698 return FAIL;
2699 eap->forceit = TRUE;
2700 }
2701 else
2702#endif
2703 {
2704 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
2705 swapname);
2706 return FAIL;
2707 }
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 }
2711 return OK;
2712}
2713
2714/*
2715 * Handle ":wnext", ":wNext" and ":wprevious" commands.
2716 */
2717 void
2718ex_wnext(eap)
2719 exarg_T *eap;
2720{
2721 int i;
2722
2723 if (eap->cmd[1] == 'n')
2724 i = curwin->w_arg_idx + (int)eap->line2;
2725 else
2726 i = curwin->w_arg_idx - (int)eap->line2;
2727 eap->line1 = 1;
2728 eap->line2 = curbuf->b_ml.ml_line_count;
2729 if (do_write(eap) != FAIL)
2730 do_argfile(eap, i);
2731}
2732
2733/*
2734 * ":wall", ":wqall" and ":xall": Write all changed files (and exit).
2735 */
2736 void
2737do_wqall(eap)
2738 exarg_T *eap;
2739{
2740 buf_T *buf;
2741 int error = 0;
2742 int save_forceit = eap->forceit;
2743
2744 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
2745 exiting = TRUE;
2746
2747 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2748 {
2749 if (bufIsChanged(buf))
2750 {
2751 /*
2752 * Check if there is a reason the buffer cannot be written:
2753 * 1. if the 'write' option is set
2754 * 2. if there is no file name (even after browsing)
2755 * 3. if the 'readonly' is set (even after a dialog)
2756 * 4. if overwriting is allowed (even after a dialog)
2757 */
2758 if (not_writing())
2759 {
2760 ++error;
2761 break;
2762 }
2763#ifdef FEAT_BROWSE
2764 /* ":browse wall": ask for file name if there isn't one */
2765 if (buf->b_ffname == NULL && cmdmod.browse)
2766 browse_save_fname(buf);
2767#endif
2768 if (buf->b_ffname == NULL)
2769 {
2770 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
2771 ++error;
2772 }
2773 else if (check_readonly(&eap->forceit, buf)
2774 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
2775 FALSE) == FAIL)
2776 {
2777 ++error;
2778 }
2779 else
2780 {
2781 if (buf_write_all(buf, eap->forceit) == FAIL)
2782 ++error;
2783#ifdef FEAT_AUTOCMD
2784 /* an autocommand may have deleted the buffer */
2785 if (!buf_valid(buf))
2786 buf = firstbuf;
2787#endif
2788 }
2789 eap->forceit = save_forceit; /* check_overwrite() may set it */
2790 }
2791 }
2792 if (exiting)
2793 {
2794 if (!error)
2795 getout(0); /* exit Vim */
2796 not_exiting();
2797 }
2798}
2799
2800/*
2801 * Check the 'write' option.
2802 * Return TRUE and give a message when it's not st.
2803 */
2804 int
2805not_writing()
2806{
2807 if (p_write)
2808 return FALSE;
2809 EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
2810 return TRUE;
2811}
2812
2813/*
2814 * Check if a buffer is read-only. Ask for overruling in a dialog.
2815 * Return TRUE and give an error message when the buffer is readonly.
2816 */
2817 static int
2818check_readonly(forceit, buf)
2819 int *forceit;
2820 buf_T *buf;
2821{
2822 if (!*forceit && buf->b_p_ro)
2823 {
2824#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2825 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
2826 {
2827 char_u buff[IOSIZE];
2828
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00002829 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 buf->b_fname);
2831
2832 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
2833 {
2834 /* Set forceit, to force the writing of a readonly file */
2835 *forceit = TRUE;
2836 return FALSE;
2837 }
2838 else
2839 return TRUE;
2840 }
2841 else
2842#endif
2843 EMSG(_(e_readonly));
2844 return TRUE;
2845 }
2846 return FALSE;
2847}
2848
2849/*
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002850 * Try to abandon current file and edit a new or existing file.
2851 * 'fnum' is the number of the file, if zero use ffname/sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 *
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002853 * Return 1 for "normal" error, 2 for "not written" error, 0 for success
2854 * -1 for succesfully opening another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 * 'lnum' is the line number for the cursor in the new file (if non-zero).
2856 */
2857 int
2858getfile(fnum, ffname, sfname, setpm, lnum, forceit)
2859 int fnum;
2860 char_u *ffname;
2861 char_u *sfname;
2862 int setpm;
2863 linenr_T lnum;
2864 int forceit;
2865{
2866 int other;
2867 int retval;
2868 char_u *free_me = NULL;
2869
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002870 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 if (fnum == 0)
2874 {
2875 /* make ffname full path, set sfname */
2876 fname_expand(curbuf, &ffname, &sfname);
2877 other = otherfile(ffname);
2878 free_me = ffname; /* has been allocated, free() later */
2879 }
2880 else
2881 other = (fnum != curbuf->b_fnum);
2882
2883 if (other)
2884 ++no_wait_return; /* don't wait for autowrite message */
2885 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf)
2886 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
2887 {
2888#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2889 if (p_confirm && p_write)
2890 dialog_changed(curbuf, FALSE);
2891 if (curbufIsChanged())
2892#endif
2893 {
2894 if (other)
2895 --no_wait_return;
2896 EMSG(_(e_nowrtmsg));
2897 retval = 2; /* file has been changed */
2898 goto theend;
2899 }
2900 }
2901 if (other)
2902 --no_wait_return;
2903 if (setpm)
2904 setpcmark();
2905 if (!other)
2906 {
2907 if (lnum != 0)
2908 curwin->w_cursor.lnum = lnum;
2909 check_cursor_lnum();
2910 beginline(BL_SOL | BL_FIX);
2911 retval = 0; /* it's in the same file */
2912 }
2913 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
2914 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0)) == OK)
2915 retval = -1; /* opened another file */
2916 else
2917 retval = 1; /* error encountered */
2918
2919theend:
2920 vim_free(free_me);
2921 return retval;
2922}
2923
2924/*
2925 * start editing a new file
2926 *
2927 * fnum: file number; if zero use ffname/sfname
2928 * ffname: the file name
2929 * - full path if sfname used,
2930 * - any file name if sfname is NULL
2931 * - empty string to re-edit with the same file name (but may be
2932 * in a different directory)
2933 * - NULL to start an empty buffer
2934 * sfname: the short file name (or NULL)
2935 * eap: contains the command to be executed after loading the file and
2936 * forced 'ff' and 'fenc'
2937 * newlnum: if > 0: put cursor on this line number (if possible)
2938 * if ECMD_LASTL: use last position in loaded file
2939 * if ECMD_LAST: use last position in all files
2940 * if ECMD_ONE: use first line
2941 * flags:
2942 * ECMD_HIDE: if TRUE don't free the current buffer
2943 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
2944 * ECMD_OLDBUF: use existing buffer if it exists
2945 * ECMD_FORCEIT: ! used for Ex command
2946 * ECMD_ADDBUF: don't edit, just add to buffer list
2947 *
2948 * return FAIL for failure, OK otherwise
2949 */
2950 int
2951do_ecmd(fnum, ffname, sfname, eap, newlnum, flags)
2952 int fnum;
2953 char_u *ffname;
2954 char_u *sfname;
2955 exarg_T *eap; /* can be NULL! */
2956 linenr_T newlnum;
2957 int flags;
2958{
2959 int other_file; /* TRUE if editing another file */
2960 int oldbuf; /* TRUE if using existing buffer */
2961#ifdef FEAT_AUTOCMD
2962 int auto_buf = FALSE; /* TRUE if autocommands brought us
2963 into the buffer unexpectedly */
2964 char_u *new_name = NULL;
2965#endif
2966 buf_T *buf;
2967#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2968 buf_T *old_curbuf = curbuf;
2969#endif
2970 char_u *free_fname = NULL;
2971#ifdef FEAT_BROWSE
2972 char_u *browse_file = NULL;
2973#endif
2974 int retval = FAIL;
2975 long n;
2976 linenr_T lnum;
2977 linenr_T topline = 0;
2978 int newcol = -1;
2979 int solcol = -1;
2980 pos_T *pos;
2981#ifdef FEAT_SUN_WORKSHOP
2982 char_u *cp;
2983#endif
2984 char_u *command = NULL;
2985
2986 if (eap != NULL)
2987 command = eap->do_ecmd_cmd;
2988
2989 if (fnum != 0)
2990 {
2991 if (fnum == curbuf->b_fnum) /* file is already being edited */
2992 return OK; /* nothing to do */
2993 other_file = TRUE;
2994 }
2995 else
2996 {
2997#ifdef FEAT_BROWSE
2998 if (cmdmod.browse)
2999 {
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003000 if (
3001# ifdef FEAT_GUI
3002 !gui.in_use &&
3003# endif
3004 au_has_group((char_u *)"FileExplorer"))
3005 {
3006 /* No browsing supported but we do have the file explorer:
3007 * Edit the directory. */
3008 if (ffname == NULL || !mch_isdir(ffname))
3009 ffname = (char_u *)".";
3010 }
3011 else
3012 {
3013 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 NULL, NULL, NULL, curbuf);
Bram Moolenaar0be6e642005-08-04 21:32:22 +00003015 if (browse_file == NULL)
3016 goto theend;
3017 ffname = browse_file;
3018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
3020#endif
3021 /* if no short name given, use ffname for short name */
3022 if (sfname == NULL)
3023 sfname = ffname;
3024#ifdef USE_FNAME_CASE
3025# ifdef USE_LONG_FNAME
3026 if (USE_LONG_FNAME)
3027# endif
Bram Moolenaar342337a2005-07-21 21:11:17 +00003028 if (sfname != NULL)
3029 fname_case(sfname, 0); /* set correct case for sfname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#endif
3031
3032#ifdef FEAT_LISTCMDS
3033 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
3034 goto theend;
3035#endif
3036
3037 if (ffname == NULL)
3038 other_file = TRUE;
3039 /* there is no file name */
3040 else if (*ffname == NUL && curbuf->b_ffname == NULL)
3041 other_file = FALSE;
3042 else
3043 {
3044 if (*ffname == NUL) /* re-edit with same file name */
3045 {
3046 ffname = curbuf->b_ffname;
3047 sfname = curbuf->b_fname;
3048 }
3049 free_fname = fix_fname(ffname); /* may expand to full path name */
3050 if (free_fname != NULL)
3051 ffname = free_fname;
3052 other_file = otherfile(ffname);
3053#ifdef FEAT_SUN_WORKSHOP
3054 if (usingSunWorkShop && p_acd
3055 && (cp = vim_strrchr(sfname, '/')) != NULL)
3056 sfname = ++cp;
3057#endif
3058 }
3059 }
3060
3061 /*
3062 * if the file was changed we may not be allowed to abandon it
3063 * - if we are going to re-edit the same file
3064 * - or if we are the only window on this file and if ECMD_HIDE is FALSE
3065 */
3066 if ( ((!other_file && !(flags & ECMD_OLDBUF))
3067 || (curbuf->b_nwindows == 1
3068 && !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
3069 && check_changed(curbuf, p_awa, !other_file,
3070 (flags & ECMD_FORCEIT), FALSE))
3071 {
3072 if (fnum == 0 && other_file && ffname != NULL)
3073 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
3074 goto theend;
3075 }
3076
3077#ifdef FEAT_VISUAL
3078 /*
3079 * End Visual mode before switching to another buffer, so the text can be
3080 * copied into the GUI selection buffer.
3081 */
3082 reset_VIsual();
3083#endif
3084
3085 /*
3086 * If we are starting to edit another file, open a (new) buffer.
3087 * Otherwise we re-use the current buffer.
3088 */
3089 if (other_file)
3090 {
3091#ifdef FEAT_LISTCMDS
3092 if (!(flags & ECMD_ADDBUF))
3093#endif
3094 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003095 if (!cmdmod.keepalt)
3096 curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 buflist_altfpos();
3098 }
3099
3100 if (fnum)
3101 buf = buflist_findnr(fnum);
3102 else
3103 {
3104#ifdef FEAT_LISTCMDS
3105 if (flags & ECMD_ADDBUF)
3106 {
3107 linenr_T tlnum = 1L;
3108
3109 if (command != NULL)
3110 {
3111 tlnum = atol((char *)command);
3112 if (tlnum <= 0)
3113 tlnum = 1L;
3114 }
3115 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
3116 goto theend;
3117 }
3118#endif
3119 buf = buflist_new(ffname, sfname, 0L,
3120 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
3121 }
3122 if (buf == NULL)
3123 goto theend;
3124 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3125 {
3126 oldbuf = FALSE;
3127 buf->b_nwindows = 0;
3128 }
3129 else /* existing memfile */
3130 {
3131 oldbuf = TRUE;
3132 (void)buf_check_timestamp(buf, FALSE);
3133 /* Check if autocommands made buffer invalid or changed the current
3134 * buffer. */
3135 if (!buf_valid(buf)
3136#ifdef FEAT_AUTOCMD
3137 || curbuf != old_curbuf
3138#endif
3139 )
3140 goto theend;
3141#ifdef FEAT_EVAL
3142 if (aborting()) /* autocmds may abort script processing */
3143 goto theend;
3144#endif
3145 }
3146
3147 /* May jump to last used line number for a loaded buffer or when asked
3148 * for explicitly */
3149 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
3150 {
3151 pos = buflist_findfpos(buf);
3152 newlnum = pos->lnum;
3153 solcol = pos->col;
3154 }
3155
3156 /*
3157 * Make the (new) buffer the one used by the current window.
3158 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
3159 * If the current buffer was empty and has no file name, curbuf
3160 * is returned by buflist_new().
3161 */
3162 if (buf != curbuf)
3163 {
3164#ifdef FEAT_AUTOCMD
3165 /*
3166 * Be careful: The autocommands may delete any buffer and change
3167 * the current buffer.
3168 * - If the buffer we are going to edit is deleted, give up.
3169 * - If the current buffer is deleted, prefer to load the new
3170 * buffer when loading a buffer is required. This avoids
3171 * loading another buffer which then must be closed again.
3172 * - If we ended up in the new buffer already, need to skip a few
3173 * things, set auto_buf.
3174 */
3175 if (buf->b_fname != NULL)
3176 new_name = vim_strsave(buf->b_fname);
3177 au_new_curbuf = buf;
3178 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3179 if (!buf_valid(buf)) /* new buffer has been deleted */
3180 {
3181 delbuf_msg(new_name); /* frees new_name */
3182 goto theend;
3183 }
3184# ifdef FEAT_EVAL
3185 if (aborting()) /* autocmds may abort script processing */
3186 {
3187 vim_free(new_name);
3188 goto theend;
3189 }
3190# endif
3191 if (buf == curbuf) /* already in new buffer */
3192 auto_buf = TRUE;
3193 else
3194 {
3195 if (curbuf == old_curbuf)
3196#endif
3197 buf_copy_options(buf, BCO_ENTER);
3198
3199 /* close the link to the current buffer */
3200 close_buffer(curwin, curbuf,
3201 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD);
3202
3203#ifdef FEAT_AUTOCMD
3204# ifdef FEAT_EVAL
3205 if (aborting()) /* autocmds may abort script processing */
3206 {
3207 vim_free(new_name);
3208 goto theend;
3209 }
3210# endif
3211 /* Be careful again, like above. */
3212 if (!buf_valid(buf)) /* new buffer has been deleted */
3213 {
3214 delbuf_msg(new_name); /* frees new_name */
3215 goto theend;
3216 }
3217 if (buf == curbuf) /* already in new buffer */
3218 auto_buf = TRUE;
3219 else
3220#endif
3221 {
3222 curwin->w_buffer = buf;
3223 curbuf = buf;
3224 ++curbuf->b_nwindows;
3225 /* set 'fileformat' */
3226 if (*p_ffs && !oldbuf)
3227 set_fileformat(default_fileformat(), OPT_LOCAL);
3228 }
3229
3230 /* May get the window options from the last time this buffer
3231 * was in this window (or another window). If not used
3232 * before, reset the local window options to the global
3233 * values. Also restores old folding stuff. */
3234 get_winopts(buf);
3235
3236#ifdef FEAT_AUTOCMD
3237 }
3238 vim_free(new_name);
3239 au_new_curbuf = NULL;
3240#endif
3241 }
3242 else
3243 ++curbuf->b_nwindows;
3244
3245 curwin->w_pcmark.lnum = 1;
3246 curwin->w_pcmark.col = 0;
3247 }
3248 else /* !other_file */
3249 {
3250 if (
3251#ifdef FEAT_LISTCMDS
3252 (flags & ECMD_ADDBUF) ||
3253#endif
3254 check_fname() == FAIL)
3255 goto theend;
3256 oldbuf = (flags & ECMD_OLDBUF);
3257 }
3258
3259 if ((flags & ECMD_SET_HELP) || keep_help_flag)
3260 {
3261 char_u *p;
3262
3263 curbuf->b_help = TRUE;
3264#ifdef FEAT_QUICKFIX
3265 set_string_option_direct((char_u *)"buftype", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003266 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
3268
3269 /*
3270 * Always set these options after jumping to a help tag, because the
3271 * user may have an autocommand that gets in the way.
3272 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and
3273 * latin1 word characters (for translated help files).
3274 * Only set it when needed, buf_init_chartab() is some work.
3275 */
3276 p =
3277#ifdef EBCDIC
3278 (char_u *)"65-255,^*,^|,^\"";
3279#else
3280 (char_u *)"!-~,^*,^|,^\",192-255";
3281#endif
3282 if (STRCMP(curbuf->b_p_isk, p) != 0)
3283 {
3284 set_string_option_direct((char_u *)"isk", -1, p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003285 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 check_buf_options(curbuf);
3287 (void)buf_init_chartab(curbuf, FALSE);
3288 }
3289
3290 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */
3291 curwin->w_p_list = FALSE; /* no list mode */
3292
3293 curbuf->b_p_ma = FALSE; /* not modifiable */
3294 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */
3295 curwin->w_p_nu = 0; /* no line numbers */
3296#ifdef FEAT_SCROLLBIND
3297 curwin->w_p_scb = FALSE; /* no scroll binding */
3298#endif
3299#ifdef FEAT_ARABIC
3300 curwin->w_p_arab = FALSE; /* no arabic mode */
3301#endif
3302#ifdef FEAT_RIGHTLEFT
3303 curwin->w_p_rl = FALSE; /* help window is left-to-right */
3304#endif
3305#ifdef FEAT_FOLDING
3306 curwin->w_p_fen = FALSE; /* No folding in the help window */
3307#endif
3308#ifdef FEAT_DIFF
3309 curwin->w_p_diff = FALSE; /* No 'diff' */
3310#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003311#ifdef FEAT_SYN_HL
3312 curwin->w_p_spell = FALSE; /* No spell checking */
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
3315#ifdef FEAT_AUTOCMD
3316 buf = curbuf;
3317#endif
3318 set_buflisted(FALSE);
3319 }
3320 else
3321 {
3322#ifdef FEAT_AUTOCMD
3323 buf = curbuf;
3324#endif
3325 /* Don't make a buffer listed if it's a help buffer. Useful when
3326 * using CTRL-O to go back to a help file. */
3327 if (!curbuf->b_help)
3328 set_buflisted(TRUE);
3329 }
3330
3331#ifdef FEAT_AUTOCMD
3332 /* If autocommands change buffers under our fingers, forget about
3333 * editing the file. */
3334 if (buf != curbuf)
3335 goto theend;
3336# ifdef FEAT_EVAL
3337 if (aborting()) /* autocmds may abort script processing */
3338 goto theend;
3339# endif
3340
3341 /* Since we are starting to edit a file, consider the filetype to be
3342 * unset. Helps for when an autocommand changes files and expects syntax
3343 * highlighting to work in the other file. */
3344 did_filetype = FALSE;
3345#endif
3346
3347/*
3348 * other_file oldbuf
3349 * FALSE FALSE re-edit same file, buffer is re-used
3350 * FALSE TRUE re-edit same file, nothing changes
3351 * TRUE FALSE start editing new file, new buffer
3352 * TRUE TRUE start editing in existing buffer (nothing to do)
3353 */
3354 if (!other_file && !oldbuf) /* re-use the buffer */
3355 {
3356 set_last_cursor(curwin); /* may set b_last_cursor */
3357 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
3358 {
3359 newlnum = curwin->w_cursor.lnum;
3360 solcol = curwin->w_cursor.col;
3361 }
3362#ifdef FEAT_AUTOCMD
3363 buf = curbuf;
3364 if (buf->b_fname != NULL)
3365 new_name = vim_strsave(buf->b_fname);
3366 else
3367 new_name = NULL;
3368#endif
3369 buf_freeall(curbuf, FALSE, FALSE); /* free all things for buffer */
3370#ifdef FEAT_AUTOCMD
3371 /* If autocommands deleted the buffer we were going to re-edit, give
3372 * up and jump to the end. */
3373 if (!buf_valid(buf))
3374 {
3375 delbuf_msg(new_name); /* frees new_name */
3376 goto theend;
3377 }
3378 vim_free(new_name);
3379
3380 /* If autocommands change buffers under our fingers, forget about
3381 * re-editing the file. Should do the buf_clear_file(), but perhaps
3382 * the autocommands changed the buffer... */
3383 if (buf != curbuf)
3384 goto theend;
3385# ifdef FEAT_EVAL
3386 if (aborting()) /* autocmds may abort script processing */
3387 goto theend;
3388# endif
3389#endif
3390 buf_clear_file(curbuf);
3391 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
3392 curbuf->b_op_end.lnum = 0;
3393 }
3394
3395/*
3396 * If we get here we are sure to start editing
3397 */
3398 /* don't redraw until the cursor is in the right line */
3399 ++RedrawingDisabled;
3400
3401 /* Assume success now */
3402 retval = OK;
3403
3404 /*
3405 * Reset cursor position, could be used by autocommands.
3406 */
3407 check_cursor();
3408
3409 /*
3410 * Check if we are editing the w_arg_idx file in the argument list.
3411 */
3412 check_arg_idx(curwin);
3413
3414#ifdef FEAT_AUTOCMD
3415 if (!auto_buf)
3416#endif
3417 {
3418 /*
3419 * Set cursor and init window before reading the file and executing
3420 * autocommands. This allows for the autocommands to position the
3421 * cursor.
3422 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003423 curwin_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425#ifdef FEAT_FOLDING
3426 /* It's like all lines in the buffer changed. Need to update
3427 * automatic folding. */
3428 foldUpdateAll(curwin);
3429#endif
3430
3431#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
3432 if (p_acd && curbuf->b_ffname != NULL
3433 && vim_chdirfile(curbuf->b_ffname) == OK)
3434 shorten_fnames(TRUE);
3435#endif
3436 /*
3437 * Careful: open_buffer() and apply_autocmds() may change the current
3438 * buffer and window.
3439 */
3440 lnum = curwin->w_cursor.lnum;
3441 topline = curwin->w_topline;
3442 if (!oldbuf) /* need to read the file */
3443 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003444#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 swap_exists_action = SEA_DIALOG;
3446#endif
3447 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
3448
3449 /*
3450 * Open the buffer and read the file.
3451 */
3452#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3453 if (should_abort(open_buffer(FALSE, eap)))
3454 retval = FAIL;
3455#else
3456 (void)open_buffer(FALSE, eap);
3457#endif
3458
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003459#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (swap_exists_action == SEA_QUIT)
3461 retval = FAIL;
3462 handle_swap_exists(old_curbuf);
3463#endif
3464 }
3465#ifdef FEAT_AUTOCMD
3466 else
3467 {
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003468 /* Read the modelines, but only to set window-local options. Any
3469 * buffer-local options have already been set and may have been
3470 * changed by the user. */
3471 do_modelines(TRUE);
3472
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
3474 &retval);
3475 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
3476 &retval);
3477 }
3478 check_arg_idx(curwin);
3479#endif
3480
3481 /*
3482 * If autocommands change the cursor position or topline, we should
3483 * keep it.
3484 */
3485 if (curwin->w_cursor.lnum != lnum)
3486 {
3487 newlnum = curwin->w_cursor.lnum;
3488 newcol = curwin->w_cursor.col;
3489 }
3490 if (curwin->w_topline == topline)
3491 topline = 0;
3492
3493 /* Even when cursor didn't move we need to recompute topline. */
3494 changed_line_abv_curs();
3495
3496#ifdef FEAT_TITLE
3497 maketitle();
3498#endif
3499 }
3500
3501#ifdef FEAT_DIFF
3502 /* Tell the diff stuff that this buffer is new and/or needs updating.
3503 * Also needed when re-editing the same buffer, because unloading will
3504 * have removed it as a diff buffer. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003505 if (curwin->w_p_diff)
3506 {
3507 diff_buf_add(curbuf);
3508 diff_invalidate(curbuf);
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#endif
3511
3512 if (command == NULL)
3513 {
3514 if (newcol >= 0) /* position set by autocommands */
3515 {
3516 curwin->w_cursor.lnum = newlnum;
3517 curwin->w_cursor.col = newcol;
3518 check_cursor();
3519 }
3520 else if (newlnum > 0) /* line number from caller or old position */
3521 {
3522 curwin->w_cursor.lnum = newlnum;
3523 check_cursor_lnum();
3524 if (solcol >= 0 && !p_sol)
3525 {
3526 /* 'sol' is off: Use last known column. */
3527 curwin->w_cursor.col = solcol;
3528 check_cursor_col();
3529#ifdef FEAT_VIRTUALEDIT
3530 curwin->w_cursor.coladd = 0;
3531#endif
3532 curwin->w_set_curswant = TRUE;
3533 }
3534 else
3535 beginline(BL_SOL | BL_FIX);
3536 }
3537 else /* no line number, go to last line in Ex mode */
3538 {
3539 if (exmode_active)
3540 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3541 beginline(BL_WHITE | BL_FIX);
3542 }
3543 }
3544
3545#ifdef FEAT_WINDOWS
3546 /* Check if cursors in other windows on the same buffer are still valid */
3547 check_lnums(FALSE);
3548#endif
3549
3550 /*
3551 * Did not read the file, need to show some info about the file.
3552 * Do this after setting the cursor.
3553 */
3554 if (oldbuf
3555#ifdef FEAT_AUTOCMD
3556 && !auto_buf
3557#endif
3558 )
3559 {
3560 int msg_scroll_save = msg_scroll;
3561
3562 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
3563 * message. */
3564 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
3565 msg_scroll = FALSE;
3566 if (!msg_scroll) /* wait a bit when overwriting an error msg */
3567 check_for_delay(FALSE);
3568 msg_start();
3569 msg_scroll = msg_scroll_save;
3570 msg_scrolled_ign = TRUE;
3571
3572 fileinfo(FALSE, TRUE, FALSE);
3573
3574 msg_scrolled_ign = FALSE;
3575 }
3576
3577 if (command != NULL)
3578 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
3579
3580#ifdef FEAT_KEYMAP
3581 if (curbuf->b_kmap_state & KEYMAP_INIT)
3582 keymap_init();
3583#endif
3584
3585 --RedrawingDisabled;
3586 if (!skip_redraw)
3587 {
3588 n = p_so;
3589 if (topline == 0 && command == NULL)
3590 p_so = 999; /* force cursor halfway the window */
3591 update_topline();
3592#ifdef FEAT_SCROLLBIND
3593 curwin->w_scbind_pos = curwin->w_topline;
3594#endif
3595 p_so = n;
3596 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */
3597 }
3598
3599 if (p_im)
3600 need_start_insertmode = TRUE;
3601
3602#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
3603 /* Change directories when the acd option is set on. */
3604 if (p_acd && curbuf->b_ffname != NULL
3605 && vim_chdirfile(curbuf->b_ffname) == OK)
3606 shorten_fnames(TRUE);
3607
3608 if (gui.in_use && curbuf->b_ffname != NULL)
3609 {
3610# ifdef FEAT_SUN_WORKSHOP
3611 if (usingSunWorkShop)
3612 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
3613# endif
3614# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003615 if (usingNetbeans & ((flags & ECMD_SET_HELP) != ECMD_SET_HELP))
3616 netbeans_file_opened(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617# endif
3618 }
3619#endif
3620
3621theend:
3622#ifdef FEAT_BROWSE
3623 vim_free(browse_file);
3624#endif
3625 vim_free(free_fname);
3626 return retval;
3627}
3628
3629#ifdef FEAT_AUTOCMD
3630 static void
3631delbuf_msg(name)
3632 char_u *name;
3633{
3634 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
3635 name == NULL ? (char_u *)"" : name);
3636 vim_free(name);
3637 au_new_curbuf = NULL;
3638}
3639#endif
3640
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003641static int append_indent = 0; /* autoindent for first line */
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643/*
3644 * ":insert" and ":append", also used by ":change"
3645 */
3646 void
3647ex_append(eap)
3648 exarg_T *eap;
3649{
3650 char_u *theline;
3651 int did_undo = FALSE;
3652 linenr_T lnum = eap->line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003653 int indent = 0;
3654 char_u *p;
3655 int vcol;
3656 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003658 /* the ! flag toggles autoindent */
3659 if (eap->forceit)
3660 curbuf->b_p_ai = !curbuf->b_p_ai;
3661
3662 /* First autoindent comes from the line we start on */
3663 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
3664 append_indent = get_indent_lnum(lnum);
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (eap->cmdidx != CMD_append)
3667 --lnum;
3668
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003669 /* when the buffer is empty append to line 0 and delete the dummy line */
3670 if (empty && lnum == 1)
3671 lnum = 0;
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 State = INSERT; /* behave like in Insert mode */
3674 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3675 State |= LANGMAP;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003677 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
3679 msg_scroll = TRUE;
3680 need_wait_return = FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003681 if (curbuf->b_p_ai)
3682 {
3683 if (append_indent >= 0)
3684 {
3685 indent = append_indent;
3686 append_indent = -1;
3687 }
3688 else if (lnum > 0)
3689 indent = get_indent_lnum(lnum);
3690 }
3691 ex_keep_indent = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (eap->getline == NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003693 {
3694 /* No getline() function, use the lines that follow. This ends
3695 * when there is no more. */
3696 if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
3697 break;
3698 p = vim_strchr(eap->nextcmd, NL);
3699 if (p == NULL)
3700 p = eap->nextcmd + STRLEN(eap->nextcmd);
3701 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
3702 if (*p != NUL)
3703 ++p;
3704 eap->nextcmd = p;
3705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 else
3707 theline = eap->getline(
3708#ifdef FEAT_EVAL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003709 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003711 NUL, eap->cookie, indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 lines_left = Rows - 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003713 if (theline == NULL)
3714 break;
3715
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003716 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */
3717 if (ex_keep_indent)
3718 append_indent = indent;
3719
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003720 /* Look for the "." after automatic indent. */
3721 vcol = 0;
3722 for (p = theline; indent > vcol; ++p)
3723 {
3724 if (*p == ' ')
3725 ++vcol;
3726 else if (*p == TAB)
3727 vcol += 8 - vcol % 8;
3728 else
3729 break;
3730 }
3731 if ((p[0] == '.' && p[1] == NUL)
Bram Moolenaareaa48e72005-06-08 22:07:37 +00003732 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
3733 == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
3735 vim_free(theline);
3736 break;
3737 }
3738
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003739 /* don't use autoindent if nothing was typed. */
3740 if (p[0] == NUL)
3741 theline[0] = NUL;
3742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 did_undo = TRUE;
3744 ml_append(lnum, theline, (colnr_T)0, FALSE);
3745 appended_lines_mark(lnum, 1L);
3746
3747 vim_free(theline);
3748 ++lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003749
3750 if (empty)
3751 {
3752 ml_delete(2L, FALSE);
3753 empty = FALSE;
3754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 State = NORMAL;
3757
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003758 if (eap->forceit)
3759 curbuf->b_p_ai = !curbuf->b_p_ai;
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 /* "start" is set to eap->line2+1 unless that position is invalid (when
3762 * eap->line2 pointed to the end of the buffer and nothig was appended)
3763 * "end" is set to lnum when something has been appended, otherwise
3764 * it is the same than "start" -- Acevedo */
3765 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
3766 eap->line2 + 1 : curbuf->b_ml.ml_line_count;
3767 if (eap->cmdidx != CMD_append)
3768 --curbuf->b_op_start.lnum;
3769 curbuf->b_op_end.lnum = (eap->line2 < lnum)
3770 ? lnum : curbuf->b_op_start.lnum;
3771 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
3772 curwin->w_cursor.lnum = lnum;
3773 check_cursor_lnum();
3774 beginline(BL_SOL | BL_FIX);
3775
3776 need_wait_return = FALSE; /* don't use wait_return() now */
3777 ex_no_reprint = TRUE;
3778}
3779
3780/*
3781 * ":change"
3782 */
3783 void
3784ex_change(eap)
3785 exarg_T *eap;
3786{
3787 linenr_T lnum;
3788
3789 if (eap->line2 >= eap->line1
3790 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
3791 return;
3792
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003793 /* the ! flag toggles autoindent */
3794 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
3795 append_indent = get_indent_lnum(eap->line1);
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 for (lnum = eap->line2; lnum >= eap->line1; --lnum)
3798 {
3799 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
3800 break;
3801 ml_delete(eap->line1, FALSE);
3802 }
3803 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
3804
3805 /* ":append" on the line above the deleted lines. */
3806 eap->line2 = eap->line1;
3807 ex_append(eap);
3808}
3809
3810 void
3811ex_z(eap)
3812 exarg_T *eap;
3813{
3814 char_u *x;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003815 int bigness;
3816 char_u *kind;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 int minus = 0;
3818 linenr_T start, end, curs, i;
3819 int j;
3820 linenr_T lnum = eap->line2;
3821
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003822 /* Vi compatible: ":z!" uses display height, without a count uses
3823 * 'scroll' */
3824 if (eap->forceit)
3825 bigness = curwin->w_height;
3826 else if (firstwin == lastwin)
3827 bigness = curwin->w_p_scr * 2;
3828 else
3829 bigness = curwin->w_height - 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (bigness < 1)
3831 bigness = 1;
3832
3833 x = eap->arg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003834 kind = x;
3835 if (*kind == '-' || *kind == '+' || *kind == '='
3836 || *kind == '^' || *kind == '.')
3837 ++x;
3838 while (*x == '-' || *x == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 ++x;
3840
3841 if (*x != 0)
3842 {
3843 if (!VIM_ISDIGIT(*x))
3844 {
3845 EMSG(_("E144: non-numeric argument to :z"));
3846 return;
3847 }
3848 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 bigness = atoi((char *)x);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003851 p_window = bigness;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003852 if (*kind == '=')
3853 bigness += 2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003857 /* the number of '-' and '+' multiplies the distance */
3858 if (*kind == '-' || *kind == '+')
3859 for (x = kind + 1; *x == *kind; ++x)
3860 ;
3861
3862 switch (*kind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
3864 case '-':
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003865 start = lnum - bigness * (x - kind);
3866 end = start + bigness;
3867 curs = end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 break;
3869
3870 case '=':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003871 start = lnum - (bigness + 1) / 2 + 1;
3872 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 curs = lnum;
3874 minus = 1;
3875 break;
3876
3877 case '^':
3878 start = lnum - bigness * 2;
3879 end = lnum - bigness;
3880 curs = lnum - bigness;
3881 break;
3882
3883 case '.':
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003884 start = lnum - (bigness + 1) / 2 + 1;
3885 end = lnum + (bigness + 1) / 2 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 curs = end;
3887 break;
3888
3889 default: /* '+' */
3890 start = lnum;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003891 if (*kind == '+')
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003892 start += bigness * (x - kind - 1) + 1;
3893 else if (eap->addr_count == 0)
3894 ++start;
3895 end = start + bigness - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 curs = end;
3897 break;
3898 }
3899
3900 if (start < 1)
3901 start = 1;
3902
3903 if (end > curbuf->b_ml.ml_line_count)
3904 end = curbuf->b_ml.ml_line_count;
3905
3906 if (curs > curbuf->b_ml.ml_line_count)
3907 curs = curbuf->b_ml.ml_line_count;
3908
3909 for (i = start; i <= end; i++)
3910 {
3911 if (minus && i == lnum)
3912 {
3913 msg_putchar('\n');
3914
3915 for (j = 1; j < Columns; j++)
3916 msg_putchar('-');
3917 }
3918
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003919 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 if (minus && i == lnum)
3922 {
3923 msg_putchar('\n');
3924
3925 for (j = 1; j < Columns; j++)
3926 msg_putchar('-');
3927 }
3928 }
3929
3930 curwin->w_cursor.lnum = curs;
3931 ex_no_reprint = TRUE;
3932}
3933
3934/*
3935 * Check if the restricted flag is set.
3936 * If so, give an error message and return TRUE.
3937 * Otherwise, return FALSE.
3938 */
3939 int
3940check_restricted()
3941{
3942 if (restricted)
3943 {
3944 EMSG(_("E145: Shell commands not allowed in rvim"));
3945 return TRUE;
3946 }
3947 return FALSE;
3948}
3949
3950/*
3951 * Check if the secure flag is set (.exrc or .vimrc in current directory).
3952 * If so, give an error message and return TRUE.
3953 * Otherwise, return FALSE.
3954 */
3955 int
3956check_secure()
3957{
3958 if (secure)
3959 {
3960 secure = 2;
3961 EMSG(_(e_curdir));
3962 return TRUE;
3963 }
3964#ifdef HAVE_SANDBOX
3965 /*
3966 * In the sandbox more things are not allowed, including the things
3967 * disallowed in secure mode.
3968 */
3969 if (sandbox != 0)
3970 {
3971 EMSG(_(e_sandbox));
3972 return TRUE;
3973 }
3974#endif
3975 return FALSE;
3976}
3977
3978static char_u *old_sub = NULL; /* previous substitute pattern */
3979static int global_need_beginline; /* call beginline() after ":g" */
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981/* do_sub()
3982 *
3983 * Perform a substitution from line eap->line1 to line eap->line2 using the
3984 * command pointed to by eap->arg which should be of the form:
3985 *
3986 * /pattern/substitution/{flags}
3987 *
3988 * The usual escapes are supported as described in the regexp docs.
3989 */
3990 void
3991do_sub(eap)
3992 exarg_T *eap;
3993{
3994 linenr_T lnum;
3995 long i = 0;
3996 regmmatch_T regmatch;
3997 static int do_all = FALSE; /* do multiple substitutions per line */
3998 static int do_ask = FALSE; /* ask for confirmation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003999 static int do_count = FALSE; /* count only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 static int do_error = TRUE; /* if false, ignore errors */
4001 static int do_print = FALSE; /* print last line with subs. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004002 static int do_list = FALSE; /* list last line with subs. */
4003 static int do_number = FALSE; /* list last line with line nr*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 static int do_ic = 0; /* ignore case flag */
4005 char_u *pat = NULL, *sub = NULL; /* init for GCC */
4006 int delimiter;
4007 int sublen;
4008 int got_quit = FALSE;
4009 int got_match = FALSE;
4010 int temp;
4011 int which_pat;
4012 char_u *cmd;
4013 int save_State;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004014 linenr_T first_line = 0; /* first changed line */
4015 linenr_T last_line= 0; /* below last changed line AFTER the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * change */
4017 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
4018 linenr_T line2;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004019 long nmatch; /* number of lines in match */
4020 linenr_T sub_firstlnum; /* nr of first sub line */
4021 char_u *sub_firstline; /* allocated copy of first sub line */
4022 int endcolumn = FALSE; /* cursor in last column when done */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004023 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 cmd = eap->arg;
4026 if (!global_busy)
4027 {
4028 sub_nsubs = 0;
4029 sub_nlines = 0;
4030 }
4031
4032#ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */
4033 if (p_altkeymap && curwin->w_p_rl)
4034 lrF_sub(cmd);
4035#endif
4036
4037 if (eap->cmdidx == CMD_tilde)
4038 which_pat = RE_LAST; /* use last used regexp */
4039 else
4040 which_pat = RE_SUBST; /* use last substitute regexp */
4041
4042 /* new pattern and substitution */
4043 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd)
4044 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
4045 {
4046 /* don't accept alphanumeric for separator */
4047 if (isalpha(*cmd))
4048 {
4049 EMSG(_("E146: Regular expressions can't be delimited by letters"));
4050 return;
4051 }
4052 /*
4053 * undocumented vi feature:
4054 * "\/sub/" and "\?sub?" use last used search pattern (almost like
4055 * //sub/r). "\&sub&" use last substitute pattern (like //sub/).
4056 */
4057 if (*cmd == '\\')
4058 {
4059 ++cmd;
4060 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4061 {
4062 EMSG(_(e_backslash));
4063 return;
4064 }
4065 if (*cmd != '&')
4066 which_pat = RE_SEARCH; /* use last '/' pattern */
4067 pat = (char_u *)""; /* empty search pattern */
4068 delimiter = *cmd++; /* remember delimiter character */
4069 }
4070 else /* find the end of the regexp */
4071 {
4072 which_pat = RE_LAST; /* use last used regexp */
4073 delimiter = *cmd++; /* remember delimiter character */
4074 pat = cmd; /* remember start of search pat */
4075 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
4076 if (cmd[0] == delimiter) /* end delimiter found */
4077 *cmd++ = NUL; /* replace it with a NUL */
4078 }
4079
4080 /*
4081 * Small incompatibility: vi sees '\n' as end of the command, but in
4082 * Vim we want to use '\n' to find/substitute a NUL.
4083 */
4084 sub = cmd; /* remember the start of the substitution */
4085
4086 while (cmd[0])
4087 {
4088 if (cmd[0] == delimiter) /* end delimiter found */
4089 {
4090 *cmd++ = NUL; /* replace it with a NUL */
4091 break;
4092 }
4093 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */
4094 ++cmd;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004095 mb_ptr_adv(cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 if (!eap->skip)
4099 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004100 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */
4101 if (STRCMP(sub, "%") == 0
4102 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
4103 {
4104 if (old_sub == NULL) /* there is no previous command */
4105 {
4106 EMSG(_(e_nopresub));
4107 return;
4108 }
4109 sub = old_sub;
4110 }
4111 else
4112 {
4113 vim_free(old_sub);
4114 old_sub = vim_strsave(sub);
4115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
4118 else if (!eap->skip) /* use previous pattern and substitution */
4119 {
4120 if (old_sub == NULL) /* there is no previous command */
4121 {
4122 EMSG(_(e_nopresub));
4123 return;
4124 }
4125 pat = NULL; /* search_regcomp() will use previous pattern */
4126 sub = old_sub;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004127
4128 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the
4129 * last column after using "$". */
4130 endcolumn = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132
4133 /*
4134 * Find trailing options. When '&' is used, keep old options.
4135 */
4136 if (*cmd == '&')
4137 ++cmd;
4138 else
4139 {
4140 if (!p_ed)
4141 {
4142 if (p_gd) /* default is global on */
4143 do_all = TRUE;
4144 else
4145 do_all = FALSE;
4146 do_ask = FALSE;
4147 }
4148 do_error = TRUE;
4149 do_print = FALSE;
Bram Moolenaarcc016f52005-12-10 20:23:46 +00004150 do_count = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 do_ic = 0;
4152 }
4153 while (*cmd)
4154 {
4155 /*
4156 * Note that 'g' and 'c' are always inverted, also when p_ed is off.
4157 * 'r' is never inverted.
4158 */
4159 if (*cmd == 'g')
4160 do_all = !do_all;
4161 else if (*cmd == 'c')
4162 do_ask = !do_ask;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004163 else if (*cmd == 'n')
4164 do_count = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 else if (*cmd == 'e')
4166 do_error = !do_error;
4167 else if (*cmd == 'r') /* use last used regexp */
4168 which_pat = RE_LAST;
4169 else if (*cmd == 'p')
4170 do_print = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004171 else if (*cmd == '#')
4172 {
4173 do_print = TRUE;
4174 do_number = TRUE;
4175 }
4176 else if (*cmd == 'l')
4177 {
4178 do_print = TRUE;
4179 do_list = TRUE;
4180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else if (*cmd == 'i') /* ignore case */
4182 do_ic = 'i';
4183 else if (*cmd == 'I') /* don't ignore case */
4184 do_ic = 'I';
4185 else
4186 break;
4187 ++cmd;
4188 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004189 if (do_count)
4190 do_ask = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 /*
4193 * check for a trailing count
4194 */
4195 cmd = skipwhite(cmd);
4196 if (VIM_ISDIGIT(*cmd))
4197 {
4198 i = getdigits(&cmd);
4199 if (i <= 0 && !eap->skip && do_error)
4200 {
4201 EMSG(_(e_zerocount));
4202 return;
4203 }
4204 eap->line1 = eap->line2;
4205 eap->line2 += i - 1;
4206 if (eap->line2 > curbuf->b_ml.ml_line_count)
4207 eap->line2 = curbuf->b_ml.ml_line_count;
4208 }
4209
4210 /*
4211 * check for trailing command or garbage
4212 */
4213 cmd = skipwhite(cmd);
4214 if (*cmd && *cmd != '"') /* if not end-of-line or comment */
4215 {
4216 eap->nextcmd = check_nextcmd(cmd);
4217 if (eap->nextcmd == NULL)
4218 {
4219 EMSG(_(e_trailing));
4220 return;
4221 }
4222 }
4223
4224 if (eap->skip) /* not executing commands, only parsing */
4225 return;
4226
4227 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, &regmatch) == FAIL)
4228 {
4229 if (do_error)
4230 EMSG(_(e_invcmd));
4231 return;
4232 }
4233
4234 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
4235 if (do_ic == 'i')
4236 regmatch.rmm_ic = TRUE;
4237 else if (do_ic == 'I')
4238 regmatch.rmm_ic = FALSE;
4239
4240 sub_firstline = NULL;
4241
4242 /*
4243 * ~ in the substitute pattern is replaced with the old pattern.
4244 * We do it here once to avoid it to be replaced over and over again.
4245 * But don't do it when it starts with "\=", then it's an expression.
4246 */
4247 if (!(sub[0] == '\\' && sub[1] == '='))
4248 sub = regtilde(sub, p_magic);
4249
4250 /*
4251 * Check for a match on each line.
4252 */
4253 line2 = eap->line2;
4254 for (lnum = eap->line1; lnum <= line2 && !(got_quit
4255#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
4256 || aborting()
4257#endif
4258 ); ++lnum)
4259 {
4260 sub_firstlnum = lnum;
4261 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
4262 if (nmatch)
4263 {
4264 colnr_T copycol;
4265 colnr_T matchcol;
4266 colnr_T prev_matchcol = MAXCOL;
4267 char_u *new_end, *new_start = NULL;
4268 unsigned new_start_len = 0;
4269 char_u *p1;
4270 int did_sub = FALSE;
4271 int lastone;
4272 unsigned len, needed_len;
4273 long nmatch_tl = 0; /* nr of lines matched below lnum */
4274 int do_again; /* do it again after joining lines */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004275 int skip_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /*
4278 * The new text is build up step by step, to avoid too much
4279 * copying. There are these pieces:
4280 * sub_firstline The old text, unmodifed.
4281 * copycol Column in the old text where we started
4282 * looking for a match; from here old text still
4283 * needs to be copied to the new text.
4284 * matchcol Column number of the old text where to look
4285 * for the next match. It's just after the
4286 * previous match or one further.
4287 * prev_matchcol Column just after the previous match (if any).
4288 * Mostly equal to matchcol, except for the first
4289 * match and after skipping an empty match.
4290 * regmatch.*pos Where the pattern matched in the old text.
4291 * new_start The new text, all that has been produced so
4292 * far.
4293 * new_end The new text, where to append new text.
4294 *
4295 * lnum The line number where we were looking for the
4296 * first match in the old line.
4297 * sub_firstlnum The line number in the buffer where to look
4298 * for a match. Can be different from "lnum"
4299 * when the pattern or substitute string contains
4300 * line breaks.
4301 *
4302 * Special situations:
4303 * - When the substitute string contains a line break, the part up
4304 * to the line break is inserted in the text, but the copy of
4305 * the original line is kept. "sub_firstlnum" is adjusted for
4306 * the inserted lines.
4307 * - When the matched pattern contains a line break, the old line
4308 * is taken from the line at the end of the pattern. The lines
4309 * in the match are deleted later, "sub_firstlnum" is adjusted
4310 * accordingly.
4311 *
4312 * The new text is built up in new_start[]. It has some extra
4313 * room to avoid using alloc()/free() too often. new_start_len is
4314 * the lenght of the allocated memory at new_start.
4315 *
4316 * Make a copy of the old line, so it won't be taken away when
4317 * updating the screen or handling a multi-line match. The "old_"
4318 * pointers point into this copy.
4319 */
4320 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4321 if (sub_firstline == NULL)
4322 {
4323 vim_free(new_start);
4324 goto outofmem;
4325 }
4326 copycol = 0;
4327 matchcol = 0;
4328
4329 /* At first match, remember current cursor position. */
4330 if (!got_match)
4331 {
4332 setpcmark();
4333 got_match = TRUE;
4334 }
4335
4336 /*
4337 * Loop until nothing more to replace in this line.
4338 * 1. Handle match with empty string.
4339 * 2. If do_ask is set, ask for confirmation.
4340 * 3. substitute the string.
4341 * 4. if do_all is set, find next match
4342 * 5. break if there isn't another match in this line
4343 */
4344 for (;;)
4345 {
4346 /* Save the line number of the last change for the final
4347 * cursor position (just like Vi). */
4348 curwin->w_cursor.lnum = lnum;
4349 do_again = FALSE;
4350
4351 /*
4352 * 1. Match empty string does not count, except for first
4353 * match. This reproduces the strange vi behaviour.
4354 * This also catches endless loops.
4355 */
4356 if (matchcol == prev_matchcol
4357 && regmatch.endpos[0].lnum == 0
4358 && matchcol == regmatch.endpos[0].col)
4359 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00004360 if (sub_firstline[matchcol] == NUL)
4361 /* We already were at the end of the line. Don't look
4362 * for a match in this line again. */
4363 skip_match = TRUE;
4364 else
4365 ++matchcol; /* search for a match at next column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 goto skip;
4367 }
4368
4369 /* Normally we continue searching for a match just after the
4370 * previous match. */
4371 matchcol = regmatch.endpos[0].col;
4372 prev_matchcol = matchcol;
4373
4374 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004375 * 2. If do_count is set only increase the counter.
4376 * If do_ask is set, ask for confirmation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004378 if (do_count)
4379 {
4380 /* For a multi-line match, put matchcol at the NUL at
4381 * the end of the line and set nmatch to one, so that
4382 * we continue looking for a match on the next line.
4383 * Avoids that ":s/\nB\@=//gc" get stuck. */
4384 if (nmatch > 1)
4385 {
4386 matchcol = STRLEN(sub_firstline);
4387 nmatch = 1;
4388 }
4389 sub_nsubs++;
4390 did_sub = TRUE;
4391 goto skip;
4392 }
4393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (do_ask)
4395 {
4396 /* change State to CONFIRM, so that the mouse works
4397 * properly */
4398 save_State = State;
4399 State = CONFIRM;
4400#ifdef FEAT_MOUSE
4401 setmouse(); /* disable mouse in xterm */
4402#endif
4403 curwin->w_cursor.col = regmatch.startpos[0].col;
4404
4405 /* When 'cpoptions' contains "u" don't sync undo when
4406 * asking for confirmation. */
4407 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4408 ++no_u_sync;
4409
4410 /*
4411 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
4412 */
4413 while (do_ask)
4414 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004415 if (exmode_active)
4416 {
4417 char_u *resp;
4418 colnr_T sc, ec;
4419
4420 print_line_no_prefix(lnum, FALSE, FALSE);
4421
4422 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
4423 curwin->w_cursor.col = regmatch.endpos[0].col - 1;
4424 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
4425 msg_start();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004426 for (i = 0; i < (long)sc; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004427 msg_putchar(' ');
Bram Moolenaar05159a02005-02-26 23:04:13 +00004428 for ( ; i <= (long)ec; ++i)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004429 msg_putchar('^');
4430
4431 resp = getexmodeline('?', NULL, 0);
4432 if (resp != NULL)
4433 {
4434 i = *resp;
4435 vim_free(resp);
4436 }
4437 }
4438 else
4439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004441 int save_p_fen = curwin->w_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004443 curwin->w_p_fen = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004445 /* Invert the matched string.
4446 * Remove the inversion afterwards. */
4447 temp = RedrawingDisabled;
4448 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004450 search_match_lines = regmatch.endpos[0].lnum;
4451 search_match_endcol = regmatch.endpos[0].col;
4452 highlight_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004454 update_topline();
4455 validate_cursor();
4456 update_screen(NOT_VALID);
4457 highlight_match = FALSE;
4458 redraw_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460#ifdef FEAT_FOLDING
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004461 curwin->w_p_fen = save_p_fen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004463 if (msg_row == Rows - 1)
4464 msg_didout = FALSE; /* avoid a scroll-up */
4465 msg_starthere();
4466 i = msg_scroll;
4467 msg_scroll = 0; /* truncate msg when
4468 needed */
4469 msg_no_more = TRUE;
4470 /* write message same highlighting as for
4471 * wait_return */
4472 smsg_attr(hl_attr(HLF_R),
4473 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
4474 msg_no_more = FALSE;
4475 msg_scroll = i;
4476 showruler(TRUE);
4477 windgoto(msg_row, msg_col);
4478 RedrawingDisabled = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004481 dont_scroll = FALSE; /* allow scrolling here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004483 ++no_mapping; /* don't map this key */
4484 ++allow_keys; /* allow special keys */
4485 i = safe_vgetc();
4486 --allow_keys;
4487 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004489 /* clear the question */
4490 msg_didout = FALSE; /* don't scroll up */
4491 msg_col = 0;
4492 gotocmdline(TRUE);
4493 }
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 need_wait_return = FALSE; /* no hit-return prompt */
4496 if (i == 'q' || i == ESC || i == Ctrl_C
4497#ifdef UNIX
4498 || i == intr_char
4499#endif
4500 )
4501 {
4502 got_quit = TRUE;
4503 break;
4504 }
4505 if (i == 'n')
4506 break;
4507 if (i == 'y')
4508 break;
4509 if (i == 'l')
4510 {
4511 /* last: replace and then stop */
4512 do_all = FALSE;
4513 line2 = lnum;
4514 break;
4515 }
4516 if (i == 'a')
4517 {
4518 do_ask = FALSE;
4519 break;
4520 }
4521#ifdef FEAT_INS_EXPAND
4522 if (i == Ctrl_E)
4523 scrollup_clamp();
4524 else if (i == Ctrl_Y)
4525 scrolldown_clamp();
4526#endif
4527 }
4528 State = save_State;
4529#ifdef FEAT_MOUSE
4530 setmouse();
4531#endif
4532 if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
4533 --no_u_sync;
4534
4535 if (i == 'n')
4536 {
4537 /* For a multi-line match, put matchcol at the NUL at
4538 * the end of the line and set nmatch to one, so that
4539 * we continue looking for a match on the next line.
4540 * Avoids that ":s/\nB\@=//gc" get stuck. */
4541 if (nmatch > 1)
4542 {
4543 matchcol = STRLEN(sub_firstline);
4544 nmatch = 1;
4545 }
4546 goto skip;
4547 }
4548 if (got_quit)
4549 break;
4550 }
4551
4552 /* Move the cursor to the start of the match, so that we can
4553 * use "\=col("."). */
4554 curwin->w_cursor.col = regmatch.startpos[0].col;
4555
4556 /*
4557 * 3. substitute the string.
4558 */
4559 /* get length of substitution part */
4560 sublen = vim_regsub_multi(&regmatch, sub_firstlnum,
4561 sub, sub_firstline, FALSE, p_magic, TRUE);
4562
Bram Moolenaar4463f292005-09-25 22:20:24 +00004563 /* When the match included the "$" of the last line it may
4564 * include one line too much. */
4565 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
4566 {
4567 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
4568 skip_match = TRUE;
4569 }
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 /* Need room for:
4572 * - result so far in new_start (not for first sub in line)
4573 * - original text up to match
4574 * - length of substituted part
4575 * - original text after match
4576 */
4577 if (nmatch == 1)
4578 p1 = sub_firstline;
4579 else
4580 {
4581 p1 = ml_get(sub_firstlnum + nmatch - 1);
4582 nmatch_tl += nmatch - 1;
4583 }
4584 i = regmatch.startpos[0].col - copycol;
4585 needed_len = i + ((unsigned)STRLEN(p1) - regmatch.endpos[0].col)
4586 + sublen + 1;
4587 if (new_start == NULL)
4588 {
4589 /*
4590 * Get some space for a temporary buffer to do the
4591 * substitution into (and some extra space to avoid
4592 * too many calls to alloc()/free()).
4593 */
4594 new_start_len = needed_len + 50;
4595 if ((new_start = alloc_check(new_start_len)) == NULL)
4596 goto outofmem;
4597 *new_start = NUL;
4598 new_end = new_start;
4599 }
4600 else
4601 {
4602 /*
4603 * Check if the temporary buffer is long enough to do the
4604 * substitution into. If not, make it larger (with a bit
4605 * extra to avoid too many calls to alloc()/free()).
4606 */
4607 len = (unsigned)STRLEN(new_start);
4608 needed_len += len;
4609 if (needed_len > new_start_len)
4610 {
4611 new_start_len = needed_len + 50;
4612 if ((p1 = alloc_check(new_start_len)) == NULL)
4613 {
4614 vim_free(new_start);
4615 goto outofmem;
4616 }
4617 mch_memmove(p1, new_start, (size_t)(len + 1));
4618 vim_free(new_start);
4619 new_start = p1;
4620 }
4621 new_end = new_start + len;
4622 }
4623
4624 /*
4625 * copy the text up to the part that matched
4626 */
4627 mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
4628 new_end += i;
4629
4630 (void)vim_regsub_multi(&regmatch, sub_firstlnum,
4631 sub, new_end, TRUE, p_magic, TRUE);
4632 sub_nsubs++;
4633 did_sub = TRUE;
4634
4635 /* Move the cursor to the start of the line, to avoid that it
4636 * is beyond the end of the line after the substitution. */
4637 curwin->w_cursor.col = 0;
4638
4639 /* For a multi-line match, make a copy of the last matched
4640 * line and continue in that one. */
4641 if (nmatch > 1)
4642 {
4643 sub_firstlnum += nmatch - 1;
4644 vim_free(sub_firstline);
4645 sub_firstline = vim_strsave(ml_get(sub_firstlnum));
4646 /* When going beyond the last line, stop substituting. */
4647 if (sub_firstlnum <= line2)
4648 do_again = TRUE;
4649 else
4650 do_all = FALSE;
4651 }
4652
4653 /* Remember next character to be copied. */
4654 copycol = regmatch.endpos[0].col;
4655
4656 /*
4657 * Now the trick is to replace CTRL-M chars with a real line
4658 * break. This would make it impossible to insert a CTRL-M in
4659 * the text. The line break can be avoided by preceding the
4660 * CTRL-M with a backslash. To be able to insert a backslash,
4661 * they must be doubled in the string and are halved here.
4662 * That is Vi compatible.
4663 */
4664 for (p1 = new_end; *p1; ++p1)
4665 {
4666 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
4667 mch_memmove(p1, p1 + 1, STRLEN(p1));
4668 else if (*p1 == CAR)
4669 {
4670 if (u_inssub(lnum) == OK) /* prepare for undo */
4671 {
4672 *p1 = NUL; /* truncate up to the CR */
4673 ml_append(lnum - 1, new_start,
4674 (colnr_T)(p1 - new_start + 1), FALSE);
4675 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
4676 if (do_ask)
4677 appended_lines(lnum - 1, 1L);
4678 else
4679 {
4680 if (first_line == 0)
4681 first_line = lnum;
4682 last_line = lnum + 1;
4683 }
4684 /* All line numbers increase. */
4685 ++sub_firstlnum;
4686 ++lnum;
4687 ++line2;
4688 /* move the cursor to the new line, like Vi */
4689 ++curwin->w_cursor.lnum;
4690 STRCPY(new_start, p1 + 1); /* copy the rest */
4691 p1 = new_start - 1;
4692 }
4693 }
4694#ifdef FEAT_MBYTE
4695 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004696 p1 += (*mb_ptr2len)(p1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
4698 }
4699
4700 /*
4701 * 4. If do_all is set, find next match.
4702 * Prevent endless loop with patterns that match empty
4703 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
4704 * But ":s/\n/#/" is OK.
4705 */
4706skip:
4707 /* We already know that we did the last subst when we are at
4708 * the end of the line, except that a pattern like
4709 * "bar\|\nfoo" may match at the NUL. */
Bram Moolenaar8299df92004-07-10 09:47:34 +00004710 lastone = (skip_match
4711 || got_int
4712 || got_quit
4713 || !(do_all || do_again)
4714 || (sub_firstline[matchcol] == NUL && nmatch <= 1
4715 && !re_multiline(regmatch.regprog)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 nmatch = -1;
4717
4718 /*
4719 * Replace the line in the buffer when needed. This is
4720 * skipped when there are more matches.
4721 * The check for nmatch_tl is needed for when multi-line
4722 * matching must replace the lines before trying to do another
4723 * match, otherwise "\@<=" won't work.
4724 * When asking the user we like to show the already replaced
4725 * text, but don't do it when "\<@=" or "\<@!" is used, it
4726 * changes what matches.
4727 */
4728 if (lastone
4729 || (do_ask && !re_lookbehind(regmatch.regprog))
4730 || nmatch_tl > 0
4731 || (nmatch = vim_regexec_multi(&regmatch, curwin,
4732 curbuf, sub_firstlnum, matchcol)) == 0)
4733 {
4734 if (new_start != NULL)
4735 {
4736 /*
4737 * Copy the rest of the line, that didn't match.
4738 * "matchcol" has to be adjusted, we use the end of
4739 * the line as reference, because the substitute may
4740 * have changed the number of characters. Same for
4741 * "prev_matchcol".
4742 */
4743 STRCAT(new_start, sub_firstline + copycol);
4744 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4745 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4746 - prev_matchcol;
4747
4748 if (u_savesub(lnum) != OK)
4749 break;
4750 ml_replace(lnum, new_start, TRUE);
4751
4752 if (nmatch_tl > 0)
4753 {
4754 /*
4755 * Matched lines have now been substituted and are
4756 * useless, delete them. The part after the match
4757 * has been appended to new_start, we don't need
4758 * it in the buffer.
4759 */
4760 ++lnum;
4761 if (u_savedel(lnum, nmatch_tl) != OK)
4762 break;
4763 for (i = 0; i < nmatch_tl; ++i)
4764 ml_delete(lnum, (int)FALSE);
4765 mark_adjust(lnum, lnum + nmatch_tl - 1,
4766 (long)MAXLNUM, -nmatch_tl);
4767 if (do_ask)
4768 deleted_lines(lnum, nmatch_tl);
4769 --lnum;
4770 line2 -= nmatch_tl; /* nr of lines decreases */
4771 nmatch_tl = 0;
4772 }
4773
4774 /* When asking, undo is saved each time, must also set
4775 * changed flag each time. */
4776 if (do_ask)
4777 changed_bytes(lnum, 0);
4778 else
4779 {
4780 if (first_line == 0)
4781 first_line = lnum;
4782 last_line = lnum + 1;
4783 }
4784
4785 sub_firstlnum = lnum;
4786 vim_free(sub_firstline); /* free the temp buffer */
4787 sub_firstline = new_start;
4788 new_start = NULL;
4789 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
4790 prev_matchcol = (colnr_T)STRLEN(sub_firstline)
4791 - prev_matchcol;
4792 copycol = 0;
4793 }
4794 if (nmatch == -1 && !lastone)
4795 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf,
4796 sub_firstlnum, matchcol);
4797
4798 /*
4799 * 5. break if there isn't another match in this line
4800 */
4801 if (nmatch <= 0)
4802 break;
4803 }
4804
4805 line_breakcheck();
4806 }
4807
4808 if (did_sub)
4809 ++sub_nlines;
4810 vim_free(sub_firstline); /* free the copy of the original line */
4811 sub_firstline = NULL;
4812 }
4813
4814 line_breakcheck();
4815 }
4816
4817 if (first_line != 0)
4818 {
4819 /* Need to subtract the number of added lines from "last_line" to get
4820 * the line number before the change (same as adding the number of
4821 * deleted lines). */
4822 i = curbuf->b_ml.ml_line_count - old_line_count;
4823 changed_lines(first_line, 0, last_line - i, i);
4824 }
4825
4826outofmem:
4827 vim_free(sub_firstline); /* may have to free allocated copy of the line */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004828
4829 /* ":s/pat//n" doesn't move the cursor */
4830 if (do_count)
4831 curwin->w_cursor = old_cursor;
4832
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (sub_nsubs)
4834 {
4835 /* Set the '[ and '] marks. */
4836 curbuf->b_op_start.lnum = eap->line1;
4837 curbuf->b_op_end.lnum = line2;
4838 curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4839
4840 if (!global_busy)
4841 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004842 if (endcolumn)
4843 coladvance((colnr_T)MAXCOL);
4844 else
4845 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004846 if (!do_sub_msg(do_count) && do_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 MSG("");
4848 }
4849 else
4850 global_need_beginline = TRUE;
4851 if (do_print)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00004852 print_line(curwin->w_cursor.lnum, do_number, do_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else if (!global_busy)
4855 {
4856 if (got_int) /* interrupted */
4857 EMSG(_(e_interr));
4858 else if (got_match) /* did find something but nothing substituted */
4859 MSG("");
4860 else if (do_error) /* nothing found */
4861 EMSG2(_(e_patnotf2), get_search_pat());
4862 }
4863
4864 vim_free(regmatch.regprog);
4865}
4866
4867/*
4868 * Give message for number of substitutions.
4869 * Can also be used after a ":global" command.
4870 * Return TRUE if a message was given.
4871 */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004872 int
Bram Moolenaar05159a02005-02-26 23:04:13 +00004873do_sub_msg(count_only)
4874 int count_only; /* used 'n' flag for ":s" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar555b2802005-05-19 21:08:39 +00004876 int len = 0;
4877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /*
4879 * Only report substitutions when:
4880 * - more than 'report' substitutions
4881 * - command was typed by user, or number of changed lines > 'report'
4882 * - giving messages is not disabled by 'lazyredraw'
4883 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004884 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
4885 || count_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 && messaging())
4887 {
4888 if (got_int)
Bram Moolenaar555b2802005-05-19 21:08:39 +00004889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 STRCPY(msg_buf, _("(Interrupted) "));
Bram Moolenaar555b2802005-05-19 21:08:39 +00004891 len = STRLEN(msg_buf);
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (sub_nsubs == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00004894 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
4895 "%s", count_only ? _("1 match") : _("1 substitution"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00004897 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004898 count_only ? _("%ld matches") : _("%ld substitutions"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 sub_nsubs);
Bram Moolenaar555b2802005-05-19 21:08:39 +00004900 len = STRLEN(msg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (sub_nlines == 1)
Bram Moolenaar555b2802005-05-19 21:08:39 +00004902 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
4903 "%s", _(" on 1 line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00004905 vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len,
4906 _(" on %ld lines"), (long)sub_nlines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (msg(msg_buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 /* save message to display it after redraw */
Bram Moolenaar238a5642006-02-21 22:12:05 +00004909 set_keep_msg(msg_buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return TRUE;
4911 }
4912 if (got_int)
4913 {
4914 EMSG(_(e_interr));
4915 return TRUE;
4916 }
4917 return FALSE;
4918}
4919
4920/*
4921 * Execute a global command of the form:
4922 *
4923 * g/pattern/X : execute X on all lines where pattern matches
4924 * v/pattern/X : execute X on all lines where pattern does not match
4925 *
4926 * where 'X' is an EX command
4927 *
4928 * The command character (as well as the trailing slash) is optional, and
4929 * is assumed to be 'p' if missing.
4930 *
4931 * This is implemented in two passes: first we scan the file for the pattern and
4932 * set a mark for each line that (not) matches. secondly we execute the command
4933 * for each line that has a mark. This is required because after deleting
4934 * lines we do not know where to search for the next match.
4935 */
4936 void
4937ex_global(eap)
4938 exarg_T *eap;
4939{
4940 linenr_T lnum; /* line number according to old situation */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004941 int ndone = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 int type; /* first char of cmd: 'v' or 'g' */
4943 char_u *cmd; /* command argument */
4944
4945 char_u delim; /* delimiter, normally '/' */
4946 char_u *pat;
4947 regmmatch_T regmatch;
4948 int match;
4949 int which_pat;
4950
4951 if (global_busy)
4952 {
4953 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */
4954 return;
4955 }
4956
4957 if (eap->forceit) /* ":global!" is like ":vglobal" */
4958 type = 'v';
4959 else
4960 type = *eap->cmd;
4961 cmd = eap->arg;
4962 which_pat = RE_LAST; /* default: use last used regexp */
4963 sub_nsubs = 0;
4964 sub_nlines = 0;
4965
4966 /*
4967 * undocumented vi feature:
4968 * "\/" and "\?": use previous search pattern.
4969 * "\&": use previous substitute pattern.
4970 */
4971 if (*cmd == '\\')
4972 {
4973 ++cmd;
4974 if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
4975 {
4976 EMSG(_(e_backslash));
4977 return;
4978 }
4979 if (*cmd == '&')
4980 which_pat = RE_SUBST; /* use previous substitute pattern */
4981 else
4982 which_pat = RE_SEARCH; /* use previous search pattern */
4983 ++cmd;
4984 pat = (char_u *)"";
4985 }
4986 else if (*cmd == NUL)
4987 {
4988 EMSG(_("E148: Regular expression missing from global"));
4989 return;
4990 }
4991 else
4992 {
4993 delim = *cmd; /* get the delimiter */
4994 if (delim)
4995 ++cmd; /* skip delimiter if there is one */
4996 pat = cmd; /* remember start of pattern */
4997 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
4998 if (cmd[0] == delim) /* end delimiter found */
4999 *cmd++ = NUL; /* replace it with a NUL */
5000 }
5001
5002#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
5003 if (p_altkeymap && curwin->w_p_rl)
5004 lrFswap(pat,0);
5005#endif
5006
5007 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, &regmatch) == FAIL)
5008 {
5009 EMSG(_(e_invcmd));
5010 return;
5011 }
5012
5013 /*
5014 * pass 1: set marks for each (not) matching line
5015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
5017 {
5018 /* a match on this line? */
5019 match = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
5020 if ((type == 'g' && match) || (type == 'v' && !match))
5021 {
5022 ml_setmarked(lnum);
5023 ndone++;
5024 }
5025 line_breakcheck();
5026 }
5027
5028 /*
5029 * pass 2: execute the command for each line that has been marked
5030 */
5031 if (got_int)
5032 MSG(_(e_interr));
5033 else if (ndone == 0)
5034 {
5035 if (type == 'v')
Bram Moolenaar555b2802005-05-19 21:08:39 +00005036 smsg((char_u *)_("Pattern found in every line: %s"), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00005038 smsg((char_u *)_(e_patnotf2), pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040 else
5041 global_exe(cmd);
5042
5043 ml_clearmarked(); /* clear rest of the marks */
5044 vim_free(regmatch.regprog);
5045}
5046
5047/*
5048 * Execute "cmd" on lines marked with ml_setmarked().
5049 */
5050 void
5051global_exe(cmd)
5052 char_u *cmd;
5053{
5054 linenr_T old_lcount; /* b_ml.ml_line_count before the command */
5055 linenr_T lnum; /* line number according to old situation */
5056
5057 /*
5058 * Set current position only once for a global command.
5059 * If global_busy is set, setpcmark() will not do anything.
5060 * If there is an error, global_busy will be incremented.
5061 */
5062 setpcmark();
5063
5064 /* When the command writes a message, don't overwrite the command. */
5065 msg_didout = TRUE;
5066
5067 global_need_beginline = FALSE;
5068 global_busy = 1;
5069 old_lcount = curbuf->b_ml.ml_line_count;
5070 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
5071 {
5072 curwin->w_cursor.lnum = lnum;
5073 curwin->w_cursor.col = 0;
5074 if (*cmd == NUL || *cmd == '\n')
5075 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
5076 else
5077 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
5078 ui_breakcheck();
5079 }
5080
5081 global_busy = 0;
5082 if (global_need_beginline)
5083 beginline(BL_WHITE | BL_FIX);
5084 else
5085 check_cursor(); /* cursor may be beyond the end of the line */
5086
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005087 /* the cursor may not have moved in the text but a change in a previous
5088 * line may move it on the screen */
5089 changed_line_abv_curs();
5090
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 /* If it looks like no message was written, allow overwriting the
5092 * command with the report for number of changes. */
5093 if (msg_col == 0 && msg_scrolled == 0)
5094 msg_didout = FALSE;
5095
5096 /* If subsitutes done, report number of substitues, otherwise report
5097 * number of extra or deleted lines. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005098 if (!do_sub_msg(FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
5100}
5101
5102#ifdef FEAT_VIMINFO
5103 int
5104read_viminfo_sub_string(virp, force)
5105 vir_T *virp;
5106 int force;
5107{
5108 if (old_sub != NULL && force)
5109 vim_free(old_sub);
5110 if (force || old_sub == NULL)
5111 old_sub = viminfo_readstring(virp, 1, TRUE);
5112 return viminfo_readline(virp);
5113}
5114
5115 void
5116write_viminfo_sub_string(fp)
5117 FILE *fp;
5118{
5119 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
5120 {
5121 fprintf(fp, _("\n# Last Substitute String:\n$"));
5122 viminfo_writestring(fp, old_sub);
5123 }
5124}
5125#endif /* FEAT_VIMINFO */
5126
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005127#if defined(EXITFREE) || defined(PROTO)
5128 void
5129free_old_sub()
5130{
5131 vim_free(old_sub);
5132}
5133#endif
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135#if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO)
5136/*
5137 * Set up for a tagpreview.
5138 */
5139 void
5140prepare_tagpreview()
5141{
5142 win_T *wp;
5143
5144# ifdef FEAT_GUI
5145 need_mouse_correct = TRUE;
5146# endif
5147
5148 /*
5149 * If there is already a preview window open, use that one.
5150 */
5151 if (!curwin->w_p_pvw)
5152 {
5153 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5154 if (wp->w_p_pvw)
5155 break;
5156 if (wp != NULL)
5157 win_enter(wp, TRUE);
5158 else
5159 {
5160 /*
5161 * There is no preview window open yet. Create one.
5162 */
5163 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
5164 == FAIL)
5165 return;
5166 curwin->w_p_pvw = TRUE;
5167 curwin->w_p_wfh = TRUE;
5168# ifdef FEAT_SCROLLBIND
5169 curwin->w_p_scb = FALSE; /* don't take over 'scrollbind' */
5170# endif
5171# ifdef FEAT_DIFF
5172 curwin->w_p_diff = FALSE; /* no 'diff' */
5173# endif
5174# ifdef FEAT_FOLDING
5175 curwin->w_p_fdc = 0; /* no 'foldcolumn' */
5176# endif
5177 }
5178 }
5179}
5180
5181#endif
5182
5183
5184/*
5185 * ":help": open a read-only window on a help file
5186 */
5187 void
5188ex_help(eap)
5189 exarg_T *eap;
5190{
5191 char_u *arg;
5192 char_u *tag;
5193 FILE *helpfd; /* file descriptor of help file */
5194 int n;
5195 int i;
5196#ifdef FEAT_WINDOWS
5197 win_T *wp;
5198#endif
5199 int num_matches;
5200 char_u **matches;
5201 char_u *p;
5202 int empty_fnum = 0;
5203 int alt_fnum = 0;
5204 buf_T *buf;
5205#ifdef FEAT_MULTI_LANG
5206 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005207 char_u *lang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#endif
5209
5210 if (eap != NULL)
5211 {
5212 /*
5213 * A ":help" command ends at the first LF, or at a '|' that is
5214 * followed by some text. Set nextcmd to the following command.
5215 */
5216 for (arg = eap->arg; *arg; ++arg)
5217 {
5218 if (*arg == '\n' || *arg == '\r'
5219 || (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
5220 {
5221 *arg++ = NUL;
5222 eap->nextcmd = arg;
5223 break;
5224 }
5225 }
5226 arg = eap->arg;
5227
5228 if (eap->forceit && *arg == NUL)
5229 {
5230 EMSG(_("E478: Don't panic!"));
5231 return;
5232 }
5233
5234 if (eap->skip) /* not executing commands */
5235 return;
5236 }
5237 else
5238 arg = (char_u *)"";
5239
5240 /* remove trailing blanks */
5241 p = arg + STRLEN(arg) - 1;
5242 while (p > arg && vim_iswhite(*p) && p[-1] != '\\')
5243 *p-- = NUL;
5244
5245#ifdef FEAT_MULTI_LANG
5246 /* Check for a specified language */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005247 lang = check_help_lang(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#endif
5249
5250 /* When no argument given go to the index. */
5251 if (*arg == NUL)
5252 arg = (char_u *)"help.txt";
5253
5254 /*
5255 * Check if there is a match for the argument.
5256 */
5257 n = find_help_tags(arg, &num_matches, &matches,
5258 eap != NULL && eap->forceit);
5259
5260 i = 0;
5261#ifdef FEAT_MULTI_LANG
5262 if (n != FAIL && lang != NULL)
5263 /* Find first item with the requested language. */
5264 for (i = 0; i < num_matches; ++i)
5265 {
5266 len = STRLEN(matches[i]);
5267 if (len > 3 && matches[i][len - 3] == '@'
5268 && STRICMP(matches[i] + len - 2, lang) == 0)
5269 break;
5270 }
5271#endif
5272 if (i >= num_matches || n == FAIL)
5273 {
5274#ifdef FEAT_MULTI_LANG
5275 if (lang != NULL)
5276 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
5277 else
5278#endif
5279 EMSG2(_("E149: Sorry, no help for %s"), arg);
5280 if (n != FAIL)
5281 FreeWild(num_matches, matches);
5282 return;
5283 }
5284
5285 /* The first match (in the requested language) is the best match. */
5286 tag = vim_strsave(matches[i]);
5287 FreeWild(num_matches, matches);
5288
5289#ifdef FEAT_GUI
5290 need_mouse_correct = TRUE;
5291#endif
5292
5293 /*
5294 * Re-use an existing help window or open a new one.
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005295 * Always open a new one for ":tab help".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 */
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005297 if (!curwin->w_buffer->b_help
5298#ifdef FEAT_WINDOWS
5299 || cmdmod.tab != 0
5300#endif
5301 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
5303#ifdef FEAT_WINDOWS
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005304 if (cmdmod.tab != 0)
5305 wp = NULL;
5306 else
5307 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5308 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
5311 win_enter(wp, TRUE);
5312 else
5313#endif
5314 {
5315 /*
5316 * There is no help window yet.
5317 * Try to open the file specified by the "helpfile" option.
5318 */
5319 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
5320 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00005321 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 goto erret;
5323 }
5324 fclose(helpfd);
5325
5326#ifdef FEAT_WINDOWS
5327 /* Split off help window; put it at far top if no position
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005328 * specified, the current window is vertically split and
5329 * narrow. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 n = WSP_HELP;
5331# ifdef FEAT_VERTSPLIT
5332 if (cmdmod.split == 0 && curwin->w_width != Columns
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005333 && curwin->w_width < 80)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 n |= WSP_TOP;
5335# endif
5336 if (win_split(0, n) == FAIL)
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005337 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#else
5339 /* use current window */
5340 if (!can_abandon(curbuf, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 goto erret;
Bram Moolenaar2a3f7ee2006-02-23 21:34:44 +00005342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344#ifdef FEAT_WINDOWS
5345 if (curwin->w_height < p_hh)
5346 win_setheight((int)p_hh);
5347#endif
5348
5349 /*
5350 * Open help file (do_ecmd() will set b_help flag, readfile() will
5351 * set b_p_ro flag).
5352 * Set the alternate file to the previously edited file.
5353 */
5354 alt_fnum = curbuf->b_fnum;
5355 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
5356 ECMD_HIDE + ECMD_SET_HELP);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005357 if (!cmdmod.keepalt)
5358 curwin->w_alt_fnum = alt_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 empty_fnum = curbuf->b_fnum;
5360 }
5361 }
5362
5363 if (!p_im)
5364 restart_edit = 0; /* don't want insert mode in help file */
5365
5366 if (tag != NULL)
5367 do_tag(tag, DT_HELP, 1, FALSE, TRUE);
5368
5369 /* Delete the empty buffer if we're not using it. */
5370 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
5371 {
5372 buf = buflist_findnr(empty_fnum);
5373 if (buf != NULL)
5374 wipe_buffer(buf, TRUE);
5375 }
5376
5377 /* keep the previous alternate file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005378 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 curwin->w_alt_fnum = alt_fnum;
5380
5381erret:
5382 vim_free(tag);
5383}
5384
5385
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005386#if defined(FEAT_MULTI_LANG) || defined(PROTO)
5387/*
5388 * In an argument search for a language specifiers in the form "@xx".
5389 * Changes the "@" to NUL if found, and returns a pointer to "xx".
5390 * Returns NULL if not found.
5391 */
5392 char_u *
5393check_help_lang(arg)
5394 char_u *arg;
5395{
5396 int len = STRLEN(arg);
5397
5398 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
5399 && ASCII_ISALPHA(arg[len - 1]))
5400 {
5401 arg[len - 3] = NUL; /* remove the '@' */
5402 return arg + len - 2;
5403 }
5404 return NULL;
5405}
5406#endif
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408/*
5409 * Return a heuristic indicating how well the given string matches. The
5410 * smaller the number, the better the match. This is the order of priorities,
5411 * from best match to worst match:
5412 * - Match with least alpha-numeric characters is better.
5413 * - Match with least total characters is better.
5414 * - Match towards the start is better.
5415 * - Match starting with "+" is worse (feature instead of command)
5416 * Assumption is made that the matched_string passed has already been found to
5417 * match some string for which help is requested. webb.
5418 */
5419 int
5420help_heuristic(matched_string, offset, wrong_case)
5421 char_u *matched_string;
5422 int offset; /* offset for match */
5423 int wrong_case; /* no matching case */
5424{
5425 int num_letters;
5426 char_u *p;
5427
5428 num_letters = 0;
5429 for (p = matched_string; *p; p++)
5430 if (ASCII_ISALNUM(*p))
5431 num_letters++;
5432
5433 /*
5434 * Multiply the number of letters by 100 to give it a much bigger
5435 * weighting than the number of characters.
5436 * If there only is a match while ignoring case, add 5000.
5437 * If the match starts in the middle of a word, add 10000 to put it
5438 * somewhere in the last half.
5439 * If the match is more than 2 chars from the start, multiply by 200 to
5440 * put it after matches at the start.
5441 */
5442 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
5443 && ASCII_ISALNUM(matched_string[offset - 1]))
5444 offset += 10000;
5445 else if (offset > 2)
5446 offset *= 200;
5447 if (wrong_case)
5448 offset += 5000;
5449 /* Features are less interesting than the subjects themselves, but "+"
5450 * alone is not a feature. */
5451 if (matched_string[0] == '+' && matched_string[1] != NUL)
5452 offset += 100;
5453 return (int)(100 * num_letters + STRLEN(matched_string) + offset);
5454}
5455
5456/*
5457 * Compare functions for qsort() below, that checks the help heuristics number
5458 * that has been put after the tagname by find_tags().
5459 */
5460 static int
5461#ifdef __BORLANDC__
5462_RTLENTRYF
5463#endif
5464help_compare(s1, s2)
5465 const void *s1;
5466 const void *s2;
5467{
5468 char *p1;
5469 char *p2;
5470
5471 p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
5472 p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
5473 return strcmp(p1, p2);
5474}
5475
5476/*
5477 * Find all help tags matching "arg", sort them and return in matches[], with
5478 * the number of matches in num_matches.
5479 * The matches will be sorted with a "best" match algorithm.
5480 * When "keep_lang" is TRUE try keeping the language of the current buffer.
5481 */
5482 int
5483find_help_tags(arg, num_matches, matches, keep_lang)
5484 char_u *arg;
5485 int *num_matches;
5486 char_u ***matches;
5487 int keep_lang;
5488{
5489 char_u *s, *d;
5490 int i;
5491 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005492 "/*", "/\\*", "\"*", "**",
5493 "/\\(\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005494 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005495 "/\\?", "/\\z(\\)", "\\=", ":s\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 "[count]", "[quotex]", "[range]",
5497 "[pattern]", "\\|", "\\%$"};
5498 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
Bram Moolenaar231334e2005-07-25 20:46:57 +00005499 "/star", "/\\\\star", "quotestar", "starstar",
5500 "/\\\\(\\\\)",
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005501 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?",
Bram Moolenaarf4630b62005-05-20 21:31:17 +00005502 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 "\\[count]", "\\[quotex]", "\\[range]",
5504 "\\[pattern]", "\\\\bar", "/\\\\%\\$"};
5505 int flags;
5506
5507 d = IObuff; /* assume IObuff is long enough! */
5508
5509 /*
5510 * Recognize a few exceptions to the rule. Some strings that contain '*'
5511 * with "star". Otherwise '*' is recognized as a wildcard.
5512 */
5513 for (i = sizeof(mtable) / sizeof(char *); --i >= 0; )
5514 if (STRCMP(arg, mtable[i]) == 0)
5515 {
5516 STRCPY(d, rtable[i]);
5517 break;
5518 }
5519
5520 if (i < 0) /* no match in table */
5521 {
5522 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched.
5523 * Also replace "\%^" and "\%(", they match every tag too.
5524 * Also "\zs", "\z1", etc.
5525 * Also "\@<", "\@=", "\@<=", etc.
5526 * And also "\_$" and "\_^". */
5527 if (arg[0] == '\\'
5528 && ((arg[1] != NUL && arg[2] == NUL)
5529 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
5530 && arg[2] != NUL)))
5531 {
5532 STRCPY(d, "/\\\\");
5533 STRCPY(d + 3, arg + 1);
5534 /* Check for "/\\_$", should be "/\\_\$" */
5535 if (d[3] == '_' && d[4] == '$')
5536 STRCPY(d + 4, "\\$");
5537 }
5538 else
5539 {
5540 /* replace "[:...:]" with "\[:...:]"; "[+...]" with "\[++...]" */
5541 if (arg[0] == '[' && (arg[1] == ':'
5542 || (arg[1] == '+' && arg[2] == '+')))
5543 *d++ = '\\';
5544
5545 for (s = arg; *s; ++s)
5546 {
5547 /*
5548 * Replace "|" with "bar" and '"' with "quote" to match the name of
5549 * the tags for these commands.
5550 * Replace "*" with ".*" and "?" with "." to match command line
5551 * completion.
5552 * Insert a backslash before '~', '$' and '.' to avoid their
5553 * special meaning.
5554 */
5555 if (d - IObuff > IOSIZE - 10) /* getting too long!? */
5556 break;
5557 switch (*s)
5558 {
5559 case '|': STRCPY(d, "bar");
5560 d += 3;
5561 continue;
5562 case '"': STRCPY(d, "quote");
5563 d += 5;
5564 continue;
5565 case '*': *d++ = '.';
5566 break;
5567 case '?': *d++ = '.';
5568 continue;
5569 case '$':
5570 case '.':
5571 case '~': *d++ = '\\';
5572 break;
5573 }
5574
5575 /*
5576 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
5577 * ":help i_^_CTRL-D" work.
5578 * Insert '-' before and after "CTRL-X" when applicable.
5579 */
5580 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
5581 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
5582 {
5583 if (d > IObuff && d[-1] != '_')
5584 *d++ = '_'; /* prepend a '_' */
5585 STRCPY(d, "CTRL-");
5586 d += 5;
5587 if (*s < ' ')
5588 {
5589#ifdef EBCDIC
5590 *d++ = CtrlChar(*s);
5591#else
5592 *d++ = *s + '@';
5593#endif
5594 if (d[-1] == '\\')
5595 *d++ = '\\'; /* double a backslash */
5596 }
5597 else
5598 *d++ = *++s;
5599 if (s[1] != NUL && s[1] != '_')
5600 *d++ = '_'; /* append a '_' */
5601 continue;
5602 }
5603 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */
5604 *d++ = '\\';
5605
5606 /*
5607 * Insert a backslash before a backslash after a slash, for search
5608 * pattern tags: "/\|" --> "/\\|".
5609 */
5610 else if (s[0] == '\\' && s[1] != '\\'
5611 && *arg == '/' && s == arg + 1)
5612 *d++ = '\\';
5613
5614 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in
5615 * "CTRL-\_CTRL-N" */
5616 if (STRNICMP(s, "CTRL-\\_", 7) == 0)
5617 {
5618 STRCPY(d, "CTRL-\\\\");
5619 d += 7;
5620 s += 6;
5621 }
5622
5623 *d++ = *s;
5624
5625 /*
5626 * If tag starts with ', toss everything after a second '. Fixes
5627 * CTRL-] on 'option'. (would include the trailing '.').
5628 */
5629 if (*s == '\'' && s > arg && *arg == '\'')
5630 break;
5631 }
5632 *d = NUL;
5633 }
5634 }
5635
5636 *matches = (char_u **)"";
5637 *num_matches = 0;
5638 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
5639 if (keep_lang)
5640 flags |= TAG_KEEP_LANG;
5641 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
5642 && *num_matches > 0)
5643 /* Sort the matches found on the heuristic number that is after the
5644 * tag name. */
5645 qsort((void *)*matches, (size_t)*num_matches,
5646 sizeof(char_u *), help_compare);
5647 return OK;
5648}
5649
5650/*
5651 * After reading a help file: May cleanup a help buffer when syntax
5652 * highlighting is not used.
5653 */
5654 void
5655fix_help_buffer()
5656{
5657 linenr_T lnum;
5658 char_u *line;
5659 int in_example = FALSE;
5660 int len;
5661 char_u *p;
5662 char_u *rt;
5663 int mustfree;
5664
5665 /* set filetype to "help". */
5666 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
5667
5668#ifdef FEAT_SYN_HL
5669 if (!syntax_present(curbuf))
5670#endif
5671 {
5672 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5673 {
5674 line = ml_get_buf(curbuf, lnum, FALSE);
5675 len = (int)STRLEN(line);
5676 if (in_example && len > 0 && !vim_iswhite(line[0]))
5677 {
5678 /* End of example: non-white or '<' in first column. */
5679 if (line[0] == '<')
5680 {
5681 /* blank-out a '<' in the first column */
5682 line = ml_get_buf(curbuf, lnum, TRUE);
5683 line[0] = ' ';
5684 }
5685 in_example = FALSE;
5686 }
5687 if (!in_example && len > 0)
5688 {
5689 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
5690 {
5691 /* blank-out a '>' in the last column (start of example) */
5692 line = ml_get_buf(curbuf, lnum, TRUE);
5693 line[len - 1] = ' ';
5694 in_example = TRUE;
5695 }
5696 else if (line[len - 1] == '~')
5697 {
5698 /* blank-out a '~' at the end of line (header marker) */
5699 line = ml_get_buf(curbuf, lnum, TRUE);
5700 line[len - 1] = ' ';
5701 }
5702 }
5703 }
5704 }
5705
5706 /*
5707 * In the "help.txt" file, add the locally added help files.
5708 * This uses the very first line in the help file.
5709 */
5710 if (fnamecmp(gettail(curbuf->b_fname), "help.txt") == 0)
5711 {
5712 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
5713 {
5714 line = ml_get_buf(curbuf, lnum, FALSE);
5715 if (strstr((char *)line, "*local-additions*") != NULL)
5716 {
5717 /* Go through all directories in 'runtimepath', skipping
5718 * $VIMRUNTIME. */
5719 p = p_rtp;
5720 while (*p != NUL)
5721 {
5722 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5723 mustfree = FALSE;
5724 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
5725 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
5726 {
5727 int fcount;
5728 char_u **fnames;
5729 FILE *fd;
5730 char_u *s;
5731 int fi;
5732#ifdef FEAT_MBYTE
5733 vimconv_T vc;
5734 char_u *cp;
5735#endif
5736
5737 /* Find all "doc/ *.txt" files in this directory. */
5738 add_pathsep(NameBuff);
5739 STRCAT(NameBuff, "doc/*.txt");
5740 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5741 &fnames, EW_FILE|EW_SILENT) == OK
5742 && fcount > 0)
5743 {
5744 for (fi = 0; fi < fcount; ++fi)
5745 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005746 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (fd != NULL)
5748 {
5749 vim_fgets(IObuff, IOSIZE, fd);
5750 if (IObuff[0] == '*'
5751 && (s = vim_strchr(IObuff + 1, '*'))
5752 != NULL)
5753 {
5754#ifdef FEAT_MBYTE
5755 int this_utf = MAYBE;
5756#endif
5757 /* Change tag definition to a
5758 * reference and remove <CR>/<NL>. */
5759 IObuff[0] = '|';
5760 *s = '|';
5761 while (*s != NUL)
5762 {
5763 if (*s == '\r' || *s == '\n')
5764 *s = NUL;
5765#ifdef FEAT_MBYTE
5766 /* The text is utf-8 when a byte
5767 * above 127 is found and no
5768 * illegal byte sequence is found.
5769 */
5770 if (*s >= 0x80 && this_utf != FALSE)
5771 {
5772 int l;
5773
5774 this_utf = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005775 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 if (l == 1)
5777 this_utf = FALSE;
5778 s += l - 1;
5779 }
5780#endif
5781 ++s;
5782 }
5783#ifdef FEAT_MBYTE
5784 /* The help file is latin1 or utf-8;
5785 * conversion to the current
5786 * 'encoding' may be required. */
5787 vc.vc_type = CONV_NONE;
5788 convert_setup(&vc, (char_u *)(
5789 this_utf == TRUE ? "utf-8"
5790 : "latin1"), p_enc);
5791 if (vc.vc_type == CONV_NONE)
5792 /* No conversion needed. */
5793 cp = IObuff;
5794 else
5795 {
5796 /* Do the conversion. If it fails
5797 * use the unconverted text. */
5798 cp = string_convert(&vc, IObuff,
5799 NULL);
5800 if (cp == NULL)
5801 cp = IObuff;
5802 }
5803 convert_setup(&vc, NULL, NULL);
5804
5805 ml_append(lnum, cp, (colnr_T)0, FALSE);
5806 if (cp != IObuff)
5807 vim_free(cp);
5808#else
5809 ml_append(lnum, IObuff, (colnr_T)0,
5810 FALSE);
5811#endif
5812 ++lnum;
5813 }
5814 fclose(fd);
5815 }
5816 }
5817 FreeWild(fcount, fnames);
5818 }
5819 }
5820 if (mustfree)
5821 vim_free(rt);
5822 }
5823 break;
5824 }
5825 }
5826 }
5827}
5828
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005829/*
5830 * ":exusage"
5831 */
5832/*ARGSUSED*/
5833 void
5834ex_exusage(eap)
5835 exarg_T *eap;
5836{
5837 do_cmdline_cmd((char_u *)"help ex-cmd-index");
5838}
5839
5840/*
5841 * ":viusage"
5842 */
5843/*ARGSUSED*/
5844 void
5845ex_viusage(eap)
5846 exarg_T *eap;
5847{
5848 do_cmdline_cmd((char_u *)"help normal-index");
5849}
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#if defined(FEAT_EX_EXTRA) || defined(PROTO)
5852static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang));
5853
5854/*
5855 * ":helptags"
5856 */
5857 void
5858ex_helptags(eap)
5859 exarg_T *eap;
5860{
5861 garray_T ga;
5862 int i, j;
5863 int len;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005864#ifdef FEAT_MULTI_LANG
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 char_u lang[2];
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 char_u ext[5];
5868 char_u fname[8];
5869 int filecount;
5870 char_u **files;
5871
5872 if (!mch_isdir(eap->arg))
5873 {
5874 EMSG2(_("E150: Not a directory: %s"), eap->arg);
5875 return;
5876 }
5877
5878#ifdef FEAT_MULTI_LANG
5879 /* Get a list of all files in the directory. */
5880 STRCPY(NameBuff, eap->arg);
5881 add_pathsep(NameBuff);
5882 STRCAT(NameBuff, "*");
5883 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
5884 EW_FILE|EW_SILENT) == FAIL
5885 || filecount == 0)
5886 {
5887 EMSG2("E151: No match: %s", NameBuff);
5888 return;
5889 }
5890
5891 /* Go over all files in the directory to find out what languages are
5892 * present. */
5893 ga_init2(&ga, 1, 10);
5894 for (i = 0; i < filecount; ++i)
5895 {
5896 len = STRLEN(files[i]);
5897 if (len > 4)
5898 {
5899 if (STRICMP(files[i] + len - 4, ".txt") == 0)
5900 {
5901 /* ".txt" -> language "en" */
5902 lang[0] = 'e';
5903 lang[1] = 'n';
5904 }
5905 else if (files[i][len - 4] == '.'
5906 && ASCII_ISALPHA(files[i][len - 3])
5907 && ASCII_ISALPHA(files[i][len - 2])
5908 && TOLOWER_ASC(files[i][len - 1]) == 'x')
5909 {
5910 /* ".abx" -> language "ab" */
5911 lang[0] = TOLOWER_ASC(files[i][len - 3]);
5912 lang[1] = TOLOWER_ASC(files[i][len - 2]);
5913 }
5914 else
5915 continue;
5916
5917 /* Did we find this language already? */
5918 for (j = 0; j < ga.ga_len; j += 2)
5919 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
5920 break;
5921 if (j == ga.ga_len)
5922 {
5923 /* New language, add it. */
5924 if (ga_grow(&ga, 2) == FAIL)
5925 break;
5926 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
5927 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 }
5929 }
5930 }
5931
5932 /*
5933 * Loop over the found languages to generate a tags file for each one.
5934 */
5935 for (j = 0; j < ga.ga_len; j += 2)
5936 {
5937 STRCPY(fname, "tags-xx");
5938 fname[5] = ((char_u *)ga.ga_data)[j];
5939 fname[6] = ((char_u *)ga.ga_data)[j + 1];
5940 if (fname[5] == 'e' && fname[6] == 'n')
5941 {
5942 /* English is an exception: use ".txt" and "tags". */
5943 fname[4] = NUL;
5944 STRCPY(ext, ".txt");
5945 }
5946 else
5947 {
5948 /* Language "ab" uses ".abx" and "tags-ab". */
5949 STRCPY(ext, ".xxx");
5950 ext[1] = fname[5];
5951 ext[2] = fname[6];
5952 }
5953 helptags_one(eap->arg, ext, fname);
5954 }
5955
5956 ga_clear(&ga);
5957 FreeWild(filecount, files);
5958
5959#else
5960 /* No language support, just use "*.txt" and "tags". */
5961 helptags_one(eap->arg, (char_u *)".txt", (char_u *)"tags");
5962#endif
5963}
5964
5965 static void
5966helptags_one(dir, ext, tagfname)
5967 char_u *dir; /* doc directory */
5968 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */
5969 char_u *tagfname; /* "tags" for English, "tags-it" for Italian. */
5970{
5971 FILE *fd_tags;
5972 FILE *fd;
5973 garray_T ga;
5974 int filecount;
5975 char_u **files;
5976 char_u *p1, *p2;
5977 int fi;
5978 char_u *s;
5979 int i;
5980 char_u *fname;
5981# ifdef FEAT_MBYTE
5982 int utf8 = MAYBE;
5983 int this_utf8;
5984 int firstline;
5985 int mix = FALSE; /* detected mixed encodings */
5986# endif
5987
5988 /*
5989 * Find all *.txt files.
5990 */
5991 STRCPY(NameBuff, dir);
5992 add_pathsep(NameBuff);
5993 STRCAT(NameBuff, "*");
5994 STRCAT(NameBuff, ext);
5995 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
5996 EW_FILE|EW_SILENT) == FAIL
5997 || filecount == 0)
5998 {
5999 if (!got_int)
6000 EMSG2("E151: No match: %s", NameBuff);
6001 return;
6002 }
6003
6004 /*
6005 * Open the tags file for writing.
6006 * Do this before scanning through all the files.
6007 */
6008 STRCPY(NameBuff, dir);
6009 add_pathsep(NameBuff);
6010 STRCAT(NameBuff, tagfname);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006011 fd_tags = mch_fopen((char *)NameBuff, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (fd_tags == NULL)
6013 {
6014 EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
6015 FreeWild(filecount, files);
6016 return;
6017 }
6018
6019 /*
6020 * If generating tags for "$VIMRUNTIME/doc" add the "help-tags" tag.
6021 */
6022 ga_init2(&ga, (int)sizeof(char_u *), 100);
6023 if (fullpathcmp((char_u *)"$VIMRUNTIME/doc", dir, FALSE) == FPC_SAME)
6024 {
6025 if (ga_grow(&ga, 1) == FAIL)
6026 got_int = TRUE;
6027 else
6028 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006029 s = alloc(18 + STRLEN(tagfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (s == NULL)
6031 got_int = TRUE;
6032 else
6033 {
6034 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
6035 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6036 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 }
6038 }
6039 }
6040
6041 /*
6042 * Go over all the files and extract the tags.
6043 */
6044 for (fi = 0; fi < filecount && !got_int; ++fi)
6045 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006046 fd = mch_fopen((char *)files[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (fd == NULL)
6048 {
6049 EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
6050 continue;
6051 }
6052 fname = gettail(files[fi]);
6053
6054# ifdef FEAT_MBYTE
6055 firstline = TRUE;
6056# endif
6057 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6058 {
6059# ifdef FEAT_MBYTE
6060 if (firstline)
6061 {
6062 /* Detect utf-8 file by a non-ASCII char in the first line. */
6063 this_utf8 = MAYBE;
6064 for (s = IObuff; *s != NUL; ++s)
6065 if (*s >= 0x80)
6066 {
6067 int l;
6068
6069 this_utf8 = TRUE;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006070 l = utf_ptr2len(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (l == 1)
6072 {
6073 /* Illegal UTF-8 byte sequence. */
6074 this_utf8 = FALSE;
6075 break;
6076 }
6077 s += l - 1;
6078 }
6079 if (this_utf8 == MAYBE) /* only ASCII characters found */
6080 this_utf8 = FALSE;
6081 if (utf8 == MAYBE) /* first file */
6082 utf8 = this_utf8;
6083 else if (utf8 != this_utf8)
6084 {
6085 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
6086 mix = !got_int;
6087 got_int = TRUE;
6088 }
6089 firstline = FALSE;
6090 }
6091# endif
6092 p1 = vim_strchr(IObuff, '*'); /* find first '*' */
6093 while (p1 != NULL)
6094 {
6095 p2 = vim_strchr(p1 + 1, '*'); /* find second '*' */
6096 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */
6097 {
6098 for (s = p1 + 1; s < p2; ++s)
6099 if (*s == ' ' || *s == '\t' || *s == '|')
6100 break;
6101
6102 /*
6103 * Only accept a *tag* when it consists of valid
6104 * characters, there is white space before it and is
6105 * followed by a white character or end-of-line.
6106 */
6107 if (s == p2
6108 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
6109 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
6110 || s[1] == '\0'))
6111 {
6112 *p2 = '\0';
6113 ++p1;
6114 if (ga_grow(&ga, 1) == FAIL)
6115 {
6116 got_int = TRUE;
6117 break;
6118 }
6119 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
6120 if (s == NULL)
6121 {
6122 got_int = TRUE;
6123 break;
6124 }
6125 ((char_u **)ga.ga_data)[ga.ga_len] = s;
6126 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 sprintf((char *)s, "%s\t%s", p1, fname);
6128
6129 /* find next '*' */
6130 p2 = vim_strchr(p2 + 1, '*');
6131 }
6132 }
6133 p1 = p2;
6134 }
6135 line_breakcheck();
6136 }
6137
6138 fclose(fd);
6139 }
6140
6141 FreeWild(filecount, files);
6142
6143 if (!got_int)
6144 {
6145 /*
6146 * Sort the tags.
6147 */
6148 sort_strings((char_u **)ga.ga_data, ga.ga_len);
6149
6150 /*
6151 * Check for duplicates.
6152 */
6153 for (i = 1; i < ga.ga_len; ++i)
6154 {
6155 p1 = ((char_u **)ga.ga_data)[i - 1];
6156 p2 = ((char_u **)ga.ga_data)[i];
6157 while (*p1 == *p2)
6158 {
6159 if (*p2 == '\t')
6160 {
6161 *p2 = NUL;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006162 vim_snprintf((char *)NameBuff, MAXPATHL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 _("E154: Duplicate tag \"%s\" in file %s/%s"),
6164 ((char_u **)ga.ga_data)[i], dir, p2 + 1);
6165 EMSG(NameBuff);
6166 *p2 = '\t';
6167 break;
6168 }
6169 ++p1;
6170 ++p2;
6171 }
6172 }
6173
6174# ifdef FEAT_MBYTE
6175 if (utf8 == TRUE)
6176 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
6177# endif
6178
6179 /*
6180 * Write the tags into the file.
6181 */
6182 for (i = 0; i < ga.ga_len; ++i)
6183 {
6184 s = ((char_u **)ga.ga_data)[i];
6185 if (STRNCMP(s, "help-tags", 9) == 0)
6186 /* help-tags entry was added in formatted form */
6187 fprintf(fd_tags, (char *)s);
6188 else
6189 {
6190 fprintf(fd_tags, "%s\t/*", s);
6191 for (p1 = s; *p1 != '\t'; ++p1)
6192 {
6193 /* insert backslash before '\\' and '/' */
6194 if (*p1 == '\\' || *p1 == '/')
6195 putc('\\', fd_tags);
6196 putc(*p1, fd_tags);
6197 }
6198 fprintf(fd_tags, "*\n");
6199 }
6200 }
6201 }
6202#ifdef FEAT_MBYTE
6203 if (mix)
6204 got_int = FALSE; /* continue with other languages */
6205#endif
6206
6207 for (i = 0; i < ga.ga_len; ++i)
6208 vim_free(((char_u **)ga.ga_data)[i]);
6209 ga_clear(&ga);
6210 fclose(fd_tags); /* there is no check for an error... */
6211}
6212#endif
6213
6214#if defined(FEAT_SIGNS) || defined(PROTO)
6215
6216/*
6217 * Struct to hold the sign properties.
6218 */
6219typedef struct sign sign_T;
6220
6221struct sign
6222{
6223 sign_T *sn_next; /* next sign in list */
6224 int sn_typenr; /* type number of sign (negative if not equal
6225 to name) */
6226 char_u *sn_name; /* name of sign */
6227 char_u *sn_icon; /* name of pixmap */
6228#ifdef FEAT_SIGN_ICONS
6229 void *sn_image; /* icon image */
6230#endif
6231 char_u *sn_text; /* text used instead of pixmap */
6232 int sn_line_hl; /* highlight ID for line */
6233 int sn_text_hl; /* highlight ID for text */
6234};
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236static sign_T *first_sign = NULL;
6237static int last_sign_typenr = MAX_TYPENR; /* is decremented */
6238
6239static void sign_list_defined __ARGS((sign_T *sp));
6240
6241/*
6242 * ":sign" command
6243 */
6244 void
6245ex_sign(eap)
6246 exarg_T *eap;
6247{
6248 char_u *arg = eap->arg;
6249 char_u *p;
6250 int idx;
6251 sign_T *sp;
6252 sign_T *sp_prev;
6253 buf_T *buf;
6254 static char *cmds[] = {
6255 "define",
6256#define SIGNCMD_DEFINE 0
6257 "undefine",
6258#define SIGNCMD_UNDEFINE 1
6259 "list",
6260#define SIGNCMD_LIST 2
6261 "place",
6262#define SIGNCMD_PLACE 3
6263 "unplace",
6264#define SIGNCMD_UNPLACE 4
6265 "jump",
6266#define SIGNCMD_JUMP 5
6267#define SIGNCMD_LAST 6
6268 };
6269
6270 /* Parse the subcommand. */
6271 p = skiptowhite(arg);
6272 if (*p != NUL)
6273 *p++ = NUL;
6274 for (idx = 0; ; ++idx)
6275 {
6276 if (idx == SIGNCMD_LAST)
6277 {
6278 EMSG2(_("E160: Unknown sign command: %s"), arg);
6279 return;
6280 }
6281 if (STRCMP(arg, cmds[idx]) == 0)
6282 break;
6283 }
6284 arg = skipwhite(p);
6285
6286 if (idx <= SIGNCMD_LIST)
6287 {
6288 /*
6289 * Define, undefine or list signs.
6290 */
6291 if (idx == SIGNCMD_LIST && *arg == NUL)
6292 {
6293 /* ":sign list": list all defined signs */
6294 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6295 sign_list_defined(sp);
6296 }
6297 else if (*arg == NUL)
6298 EMSG(_("E156: Missing sign name"));
6299 else
6300 {
6301 p = skiptowhite(arg);
6302 if (*p != NUL)
6303 *p++ = NUL;
6304 sp_prev = NULL;
6305 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6306 {
6307 if (STRCMP(sp->sn_name, arg) == 0)
6308 break;
6309 sp_prev = sp;
6310 }
6311 if (idx == SIGNCMD_DEFINE)
6312 {
6313 /* ":sign define {name} ...": define a sign */
6314 if (sp == NULL)
6315 {
6316 /* Allocate a new sign. */
6317 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
6318 if (sp == NULL)
6319 return;
6320 if (sp_prev == NULL)
6321 first_sign = sp;
6322 else
6323 sp_prev->sn_next = sp;
6324 sp->sn_name = vim_strnsave(arg, (int)(p - arg));
6325
6326 /* If the name is a number use that for the typenr,
6327 * otherwise use a negative number. */
6328 if (VIM_ISDIGIT(*arg))
6329 sp->sn_typenr = atoi((char *)arg);
6330 else
6331 {
6332 sign_T *lp;
6333 int start = last_sign_typenr;
6334
6335 for (lp = first_sign; lp != NULL; lp = lp->sn_next)
6336 {
6337 if (lp->sn_typenr == last_sign_typenr)
6338 {
6339 --last_sign_typenr;
6340 if (last_sign_typenr == 0)
6341 last_sign_typenr = MAX_TYPENR;
6342 if (last_sign_typenr == start)
6343 {
6344 EMSG(_("E612: Too many signs defined"));
6345 return;
6346 }
6347 lp = first_sign;
6348 continue;
6349 }
6350 }
6351
6352 sp->sn_typenr = last_sign_typenr--;
6353 if (last_sign_typenr == 0)
6354 last_sign_typenr = MAX_TYPENR; /* wrap around */
6355 }
6356 }
6357
6358 /* set values for a defined sign. */
6359 for (;;)
6360 {
6361 arg = skipwhite(p);
6362 if (*arg == NUL)
6363 break;
6364 p = skiptowhite_esc(arg);
6365 if (STRNCMP(arg, "icon=", 5) == 0)
6366 {
6367 arg += 5;
6368 vim_free(sp->sn_icon);
6369 sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
6370 backslash_halve(sp->sn_icon);
6371#ifdef FEAT_SIGN_ICONS
6372 if (gui.in_use)
6373 {
6374 out_flush();
6375 if (sp->sn_image != NULL)
6376 gui_mch_destroy_sign(sp->sn_image);
6377 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6378 }
6379#endif
6380 }
6381 else if (STRNCMP(arg, "text=", 5) == 0)
6382 {
6383 char_u *s;
6384 int cells;
6385 int len;
6386
6387 arg += 5;
6388#ifdef FEAT_MBYTE
6389 /* Count cells and check for non-printable chars */
6390 if (has_mbyte)
6391 {
6392 cells = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006393 for (s = arg; s < p; s += (*mb_ptr2len)(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 {
6395 if (!vim_isprintc((*mb_ptr2char)(s)))
6396 break;
6397 cells += (*mb_ptr2cells)(s);
6398 }
6399 }
6400 else
6401#endif
6402 {
6403 for (s = arg; s < p; ++s)
6404 if (!vim_isprintc(*s))
6405 break;
6406 cells = s - arg;
6407 }
6408 /* Currently must be one or two display cells */
6409 if (s != p || cells < 1 || cells > 2)
6410 {
6411 *p = NUL;
6412 EMSG2(_("E239: Invalid sign text: %s"), arg);
6413 return;
6414 }
6415
6416 vim_free(sp->sn_text);
6417 /* Allocate one byte more if we need to pad up
6418 * with a space. */
6419 len = p - arg + ((cells == 1) ? 1 : 0);
6420 sp->sn_text = vim_strnsave(arg, len);
6421
6422 if (sp->sn_text != NULL && cells == 1)
6423 STRCPY(sp->sn_text + len - 1, " ");
6424 }
6425 else if (STRNCMP(arg, "linehl=", 7) == 0)
6426 {
6427 arg += 7;
6428 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
6429 }
6430 else if (STRNCMP(arg, "texthl=", 7) == 0)
6431 {
6432 arg += 7;
6433 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
6434 }
6435 else
6436 {
6437 EMSG2(_(e_invarg2), arg);
6438 return;
6439 }
6440 }
6441 }
6442 else if (sp == NULL)
6443 EMSG2(_("E155: Unknown sign: %s"), arg);
6444 else if (idx == SIGNCMD_LIST)
6445 /* ":sign list {name}" */
6446 sign_list_defined(sp);
6447 else
6448 {
6449 /* ":sign undefine {name}" */
6450 vim_free(sp->sn_name);
6451 vim_free(sp->sn_icon);
6452#ifdef FEAT_SIGN_ICONS
6453 if (sp->sn_image != NULL)
6454 {
6455 out_flush();
6456 gui_mch_destroy_sign(sp->sn_image);
6457 }
6458#endif
6459 vim_free(sp->sn_text);
6460 if (sp_prev == NULL)
6461 first_sign = sp->sn_next;
6462 else
6463 sp_prev->sn_next = sp->sn_next;
6464 vim_free(sp);
6465 }
6466 }
6467 }
6468 else
6469 {
6470 int id = -1;
6471 linenr_T lnum = -1;
6472 char_u *sign_name = NULL;
6473 char_u *arg1;
6474
6475 if (*arg == NUL)
6476 {
6477 if (idx == SIGNCMD_PLACE)
6478 {
6479 /* ":sign place": list placed signs in all buffers */
6480 sign_list_placed(NULL);
6481 }
6482 else if (idx == SIGNCMD_UNPLACE)
6483 {
6484 /* ":sign unplace": remove placed sign at cursor */
6485 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
6486 if (id > 0)
6487 {
6488 buf_delsign(curwin->w_buffer, id);
6489 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
6490 }
6491 else
6492 EMSG(_("E159: Missing sign number"));
6493 }
6494 else
6495 EMSG(_(e_argreq));
6496 return;
6497 }
6498
6499 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
6500 {
6501 /* ":sign unplace *": remove all placed signs */
6502 buf_delete_all_signs();
6503 return;
6504 }
6505
6506 /* first arg could be placed sign id */
6507 arg1 = arg;
6508 if (VIM_ISDIGIT(*arg))
6509 {
6510 id = getdigits(&arg);
6511 if (!vim_iswhite(*arg) && *arg != NUL)
6512 {
6513 id = -1;
6514 arg = arg1;
6515 }
6516 else
6517 {
6518 arg = skipwhite(arg);
6519 if (idx == SIGNCMD_UNPLACE && *arg == NUL)
6520 {
6521 /* ":sign unplace {id}": remove placed sign by number */
6522 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6523 if ((lnum = buf_delsign(buf, id)) != 0)
6524 update_debug_sign(buf, lnum);
6525 return;
6526 }
6527 }
6528 }
6529
6530 /*
6531 * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
6532 * Leave "arg" pointing to {fname}.
6533 */
6534 for (;;)
6535 {
6536 if (STRNCMP(arg, "line=", 5) == 0)
6537 {
6538 arg += 5;
6539 lnum = atoi((char *)arg);
6540 arg = skiptowhite(arg);
6541 }
6542 else if (STRNCMP(arg, "name=", 5) == 0)
6543 {
6544 arg += 5;
6545 sign_name = arg;
6546 arg = skiptowhite(arg);
6547 if (*arg != NUL)
6548 *arg++ = NUL;
6549 }
6550 else if (STRNCMP(arg, "file=", 5) == 0)
6551 {
6552 arg += 5;
6553 buf = buflist_findname(arg);
6554 break;
6555 }
6556 else if (STRNCMP(arg, "buffer=", 7) == 0)
6557 {
6558 arg += 7;
6559 buf = buflist_findnr((int)getdigits(&arg));
6560 if (*skipwhite(arg) != NUL)
6561 EMSG(_(e_trailing));
6562 break;
6563 }
6564 else
6565 {
6566 EMSG(_(e_invarg));
6567 return;
6568 }
6569 arg = skipwhite(arg);
6570 }
6571
6572 if (buf == NULL)
6573 {
6574 EMSG2(_("E158: Invalid buffer name: %s"), arg);
6575 }
6576 else if (id <= 0)
6577 {
6578 if (lnum >= 0 || sign_name != NULL)
6579 EMSG(_(e_invarg));
6580 else
6581 /* ":sign place file={fname}": list placed signs in one file */
6582 sign_list_placed(buf);
6583 }
6584 else if (idx == SIGNCMD_JUMP)
6585 {
6586 /* ":sign jump {id} file={fname}" */
6587 if (lnum >= 0 || sign_name != NULL)
6588 EMSG(_(e_invarg));
6589 else if ((lnum = buf_findsign(buf, id)) > 0)
6590 { /* goto a sign ... */
6591 if (buf_jump_open_win(buf) != NULL)
6592 { /* ... in a current window */
6593 curwin->w_cursor.lnum = lnum;
6594 check_cursor_lnum();
6595 beginline(BL_WHITE);
6596 }
6597 else
6598 { /* ... not currently in a window */
6599 char_u *cmd;
6600
6601 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
6602 if (cmd == NULL)
6603 return;
6604 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
6605 do_cmdline_cmd(cmd);
6606 vim_free(cmd);
6607 }
6608#ifdef FEAT_FOLDING
6609 foldOpenCursor();
6610#endif
6611 }
6612 else
6613 EMSGN(_("E157: Invalid sign ID: %ld"), id);
6614 }
6615 else if (idx == SIGNCMD_UNPLACE)
6616 {
6617 /* ":sign unplace {id} file={fname}" */
6618 if (lnum >= 0 || sign_name != NULL)
6619 EMSG(_(e_invarg));
6620 else
6621 {
6622 lnum = buf_delsign(buf, id);
6623 update_debug_sign(buf, lnum);
6624 }
6625 }
6626 /* idx == SIGNCMD_PLACE */
6627 else if (sign_name != NULL)
6628 {
6629 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6630 if (STRCMP(sp->sn_name, sign_name) == 0)
6631 break;
6632 if (sp == NULL)
6633 {
6634 EMSG2(_("E155: Unknown sign: %s"), sign_name);
6635 return;
6636 }
6637 if (lnum > 0)
6638 /* ":sign place {id} line={lnum} name={name} file={fname}":
6639 * place a sign */
6640 buf_addsign(buf, id, lnum, sp->sn_typenr);
6641 else
6642 /* ":sign place {id} file={fname}": change sign type */
6643 lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
6644 update_debug_sign(buf, lnum);
6645 }
6646 else
6647 EMSG(_(e_invarg));
6648 }
6649}
6650
6651#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6652/*
6653 * Allocate the icons. Called when the GUI has started. Allows defining
6654 * signs before it starts.
6655 */
6656 void
6657sign_gui_started()
6658{
6659 sign_T *sp;
6660
6661 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6662 if (sp->sn_icon != NULL)
6663 sp->sn_image = gui_mch_register_sign(sp->sn_icon);
6664}
6665#endif
6666
6667/*
6668 * List one sign.
6669 */
6670 static void
6671sign_list_defined(sp)
6672 sign_T *sp;
6673{
6674 char_u *p;
6675
Bram Moolenaar555b2802005-05-19 21:08:39 +00006676 smsg((char_u *)"sign %s", sp->sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (sp->sn_icon != NULL)
6678 {
6679 MSG_PUTS(" icon=");
6680 msg_outtrans(sp->sn_icon);
6681#ifdef FEAT_SIGN_ICONS
6682 if (sp->sn_image == NULL)
6683 MSG_PUTS(_(" (NOT FOUND)"));
6684#else
6685 MSG_PUTS(_(" (not supported)"));
6686#endif
6687 }
6688 if (sp->sn_text != NULL)
6689 {
6690 MSG_PUTS(" text=");
6691 msg_outtrans(sp->sn_text);
6692 }
6693 if (sp->sn_line_hl > 0)
6694 {
6695 MSG_PUTS(" linehl=");
6696 p = get_highlight_name(NULL, sp->sn_line_hl - 1);
6697 if (p == NULL)
6698 MSG_PUTS("NONE");
6699 else
6700 msg_puts(p);
6701 }
6702 if (sp->sn_text_hl > 0)
6703 {
6704 MSG_PUTS(" texthl=");
6705 p = get_highlight_name(NULL, sp->sn_text_hl - 1);
6706 if (p == NULL)
6707 MSG_PUTS("NONE");
6708 else
6709 msg_puts(p);
6710 }
6711}
6712
6713/*
6714 * Get highlighting attribute for sign "typenr".
6715 * If "line" is TRUE: line highl, if FALSE: text highl.
6716 */
6717 int
6718sign_get_attr(typenr, line)
6719 int typenr;
6720 int line;
6721{
6722 sign_T *sp;
6723
6724 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6725 if (sp->sn_typenr == typenr)
6726 {
6727 if (line)
6728 {
6729 if (sp->sn_line_hl > 0)
6730 return syn_id2attr(sp->sn_line_hl);
6731 }
6732 else
6733 {
6734 if (sp->sn_text_hl > 0)
6735 return syn_id2attr(sp->sn_text_hl);
6736 }
6737 break;
6738 }
6739 return 0;
6740}
6741
6742/*
6743 * Get text mark for sign "typenr".
6744 * Returns NULL if there isn't one.
6745 */
6746 char_u *
6747sign_get_text(typenr)
6748 int typenr;
6749{
6750 sign_T *sp;
6751
6752 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6753 if (sp->sn_typenr == typenr)
6754 return sp->sn_text;
6755 return NULL;
6756}
6757
6758#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6759 void *
6760sign_get_image(typenr)
6761 int typenr; /* the attribute which may have a sign */
6762{
6763 sign_T *sp;
6764
6765 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6766 if (sp->sn_typenr == typenr)
6767 return sp->sn_image;
6768 return NULL;
6769}
6770#endif
6771
6772/*
6773 * Get the name of a sign by its typenr.
6774 */
6775 char_u *
6776sign_typenr2name(typenr)
6777 int typenr;
6778{
6779 sign_T *sp;
6780
6781 for (sp = first_sign; sp != NULL; sp = sp->sn_next)
6782 if (sp->sn_typenr == typenr)
6783 return sp->sn_name;
6784 return (char_u *)_("[Deleted]");
6785}
6786
6787#endif
6788
6789#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
6790/*
6791 * ":drop"
6792 * Opens the first argument in a window. When there are two or more arguments
6793 * the argument list is redefined.
6794 */
6795 void
6796ex_drop(eap)
6797 exarg_T *eap;
6798{
6799 int split = FALSE;
6800 int incurwin = FALSE;
6801 char_u *arg;
6802 char_u *first = NULL;
6803 win_T *wp;
6804 buf_T *buf;
6805
6806 /*
6807 * Check if the first argument is already being edited in a window. If
6808 * so, jump to that window.
6809 * We would actually need to check all arguments, but that's complicated
6810 * and mostly only one file is dropped.
6811 * This also ignores wildcards, since it is very unlikely the user is
6812 * editing a file name with a wildcard character.
6813 */
6814 arg = vim_strsave(eap->arg);
6815 if (arg != NULL)
6816 {
6817 /* Get the first argument, remove quotes, make it a full path. */
6818 first = fix_fname(arg);
6819 if (first != NULL)
6820 {
6821 buf = buflist_findname(first);
6822 FOR_ALL_WINDOWS(wp)
6823 {
6824 if (wp->w_buffer == buf)
6825 {
6826 incurwin = TRUE;
6827# ifdef FEAT_WINDOWS
6828 win_enter(wp, TRUE);
6829 break;
6830# endif
6831 }
6832 }
6833 vim_free(first);
6834
6835 if (incurwin)
6836 {
6837 /* Already editing the file. Redefine the argument list. */
6838 set_arglist(eap->arg);
6839 curwin->w_arg_idx = 0;
6840 vim_free(arg);
6841 return;
6842 }
6843 }
6844 vim_free(arg);
6845 }
6846
6847 /*
6848 * Check whether the current buffer is changed. If so, we will need
6849 * to split the current window or data could be lost.
6850 * Skip the check if the 'hidden' option is set, as in this case the
6851 * buffer won't be lost.
6852 */
6853 if (!P_HID(curbuf))
6854 {
6855# ifdef FEAT_WINDOWS
6856 ++emsg_off;
6857# endif
6858 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6859# ifdef FEAT_WINDOWS
6860 --emsg_off;
6861# else
6862 if (split)
6863 return;
6864# endif
6865 }
6866
6867 /* Fake a ":snext" or ":next" command, redefine the arglist. */
6868 if (split)
6869 {
6870 eap->cmdidx = CMD_snext;
6871 eap->cmd[0] = 's';
6872 }
6873 else
6874 eap->cmdidx = CMD_next;
6875 ex_next(eap);
6876}
6877#endif