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