#include "vim.h"
#include "version.h"
#ifdef FEAT_FLOAT
# include <float.h>
#endif
static int linelen(int *has_tab);
static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out);
#ifdef FEAT_VIMINFO
static char_u *viminfo_filename(char_u *);
static void do_viminfo(FILE *fp_in, FILE *fp_out, int flags);
static int viminfo_encoding(vir_T *virp);
static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing);
#endif
static int check_readonly(int *forceit, buf_T *buf);
#ifdef FEAT_AUTOCMD
static void delbuf_msg(char_u *name);
#endif
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
help_compare(const void *s1, const void *s2);
static void prepare_help_buffer(void);
void
do_ascii(exarg_T *eap UNUSED)
{
int c;
int cval;
char buf1[20];
char buf2[20];
char_u buf3[7];
#ifdef FEAT_MBYTE
int cc[MAX_MCO];
int ci = 0;
int len;
if (enc_utf8)
c = utfc_ptr2char(ml_get_cursor(), cc);
else
#endif
c = gchar_cursor();
if (c == NUL)
{
MSG("NUL");
return;
}
#ifdef FEAT_MBYTE
IObuff[0] = NUL;
if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
#endif
{
if (c == NL)
c = NUL;
if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
cval = NL;
else
cval = c;
if (vim_isprintc_strict(c) && (c < ' '
#ifndef EBCDIC
|| c > '~'
#endif
))
{
transchar_nonprint(buf3, c);
vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
}
else
buf1[0] = NUL;
#ifndef EBCDIC
if (c >= 0x80)
vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
(char *)transchar(c & 0x7f));
else
#endif
buf2[0] = NUL;
vim_snprintf((char *)IObuff, IOSIZE,
_("<%s>%s%s %d, Hex %02x, Octal %03o"),
transchar(c), buf1, buf2, cval, cval, cval);
#ifdef FEAT_MBYTE
if (enc_utf8)
c = cc[ci++];
else
c = 0;
#endif
}
#ifdef FEAT_MBYTE
while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
{
len = (int)STRLEN(IObuff);
if (len > 0)
IObuff[len++] = ' ';
IObuff[len++] = '<';
if (enc_utf8 && utf_iscomposing(c)
# ifdef USE_GUI
&& !gui.in_use
# endif
)
IObuff[len++] = ' ';
len += (*mb_char2bytes)(c, IObuff + len);
vim_snprintf((char *)IObuff + len, IOSIZE - len,
c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
: _("> %d, Hex %08x, Octal %o"), c, c, c);
if (ci == MAX_MCO)
break;
if (enc_utf8)
c = cc[ci++];
else
c = 0;
}
#endif
msg(IObuff);
}
void
ex_align(exarg_T *eap)
{
pos_T save_curpos;
int len;
int indent = 0;
int new_indent;
int has_tab;
int width;
#ifdef FEAT_RIGHTLEFT
if (curwin->w_p_rl)
{
if (eap->cmdidx == CMD_right)
eap->cmdidx = CMD_left;
else if (eap->cmdidx == CMD_left)
eap->cmdidx = CMD_right;
}
#endif
width = atoi((char *)eap->arg);
save_curpos = curwin->w_cursor;
if (eap->cmdidx == CMD_left)
{
if (width >= 0)
indent = width;
}
else
{
if (width <= 0)
width = curbuf->b_p_tw;
if (width == 0 && curbuf->b_p_wm > 0)
width = curwin->w_width - curbuf->b_p_wm;
if (width <= 0)
width = 80;
}
if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
return;
for (curwin->w_cursor.lnum = eap->line1;
curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
{
if (eap->cmdidx == CMD_left)
new_indent = indent;
else
{
has_tab = FALSE;
len = linelen(eap->cmdidx == CMD_right ? &has_tab
: NULL) - get_indent();
if (len <= 0)
continue;
if (eap->cmdidx == CMD_center)
new_indent = (width - len) / 2;
else
{
new_indent = width - len;
if (has_tab)
while (new_indent > 0)
{
(void)set_indent(new_indent, 0);
if (linelen(NULL) <= width)
{
do
(void)set_indent(++new_indent, 0);
while (linelen(NULL) <= width);
--new_indent;
break;
}
--new_indent;
}
}
}
if (new_indent < 0)
new_indent = 0;
(void)set_indent(new_indent, 0);
}
changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
curwin->w_cursor = save_curpos;
beginline(BL_WHITE | BL_FIX);
}
static int
linelen(int *has_tab)
{
char_u *line;
char_u *first;
char_u *last;
int save;
int len;
line = ml_get_curline();
first = skipwhite(line);
for (last = first + STRLEN(first);
last > first && VIM_ISWHITE(last[-1]); --last)
;
save = *last;
*last = NUL;
len = linetabsize(line);
if (has_tab != NULL)
*has_tab = (vim_strrchr(first, TAB) != NULL);
*last = save;
return len;
}
static char_u *sortbuf1;
static char_u *sortbuf2;
static int sort_ic;
static int sort_nr;
static int sort_rx;
#ifdef FEAT_FLOAT
static int sort_flt;
#endif
static int sort_abort;
typedef struct
{
linenr_T lnum;
union {
struct
{
varnumber_T start_col_nr;
varnumber_T end_col_nr;
} line;
varnumber_T value;
#ifdef FEAT_FLOAT
float_T value_flt;
#endif
} st_u;
} sorti_T;
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
sort_compare(const void *s1, const void *s2);
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
sort_compare(const void *s1, const void *s2)
{
sorti_T l1 = *(sorti_T *)s1;
sorti_T l2 = *(sorti_T *)s2;
int result = 0;
if (sort_abort)
return 0;
fast_breakcheck();
if (got_int)
sort_abort = TRUE;
if (sort_nr)
result = l1.st_u.value == l2.st_u.value ? 0
: l1.st_u.value > l2.st_u.value ? 1 : -1;
#ifdef FEAT_FLOAT
else if (sort_flt)
result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
: l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
#endif
else
{
STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
: STRCMP(sortbuf1, sortbuf2);
}
if (result == 0)
return (int)(l1.lnum - l2.lnum);
return result;
}
void
ex_sort(exarg_T *eap)
{
regmatch_T regmatch;
int len;
linenr_T lnum;
long maxlen = 0;
sorti_T *nrs;
size_t count = (size_t)(eap->line2 - eap->line1 + 1);
size_t i;
char_u *p;
char_u *s;
char_u *s2;
char_u c;
int unique = FALSE;
long deleted;
colnr_T start_col;
colnr_T end_col;
int sort_what = 0;
int format_found = 0;
if (count <= 1)
return;
if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
return;
sortbuf1 = NULL;
sortbuf2 = NULL;
regmatch.regprog = NULL;
nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
if (nrs == NULL)
goto sortend;
sort_abort = sort_ic = sort_rx = sort_nr = 0;
#ifdef FEAT_FLOAT
sort_flt = 0;
#endif
for (p = eap->arg; *p != NUL; ++p)
{
if (VIM_ISWHITE(*p))
;
else if (*p == 'i')
sort_ic = TRUE;
else if (*p == 'r')
sort_rx = TRUE;
else if (*p == 'n')
{
sort_nr = 1;
++format_found;
}
#ifdef FEAT_FLOAT
else if (*p == 'f')
{
sort_flt = 1;
++format_found;
}
#endif
else if (*p == 'b')
{
sort_what = STR2NR_BIN + STR2NR_FORCE;
++format_found;
}
else if (*p == 'o')
{
sort_what = STR2NR_OCT + STR2NR_FORCE;
++format_found;
}
else if (*p == 'x')
{
sort_what = STR2NR_HEX + STR2NR_FORCE;
++format_found;
}
else if (*p == 'u')
unique = TRUE;
else if (*p == '"')
break;
else if (check_nextcmd(p) != NULL)
{
eap->nextcmd = check_nextcmd(p);
break;
}
else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
{
s = skip_regexp(p + 1, *p, TRUE, NULL);
if (*s != *p)
{
EMSG(_(e_invalpat));
goto sortend;
}
*s = NUL;
if (s == p + 1)
{
if (last_search_pat() == NULL)
{
EMSG(_(e_noprevre));
goto sortend;
}
regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
}
else
regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
if (regmatch.regprog == NULL)
goto sortend;
p = s;
regmatch.rm_ic = p_ic;
}
else
{
EMSG2(_(e_invarg2), p);
goto sortend;
}
}
if (format_found > 1)
{
EMSG(_(e_invarg));
goto sortend;
}
sort_nr += sort_what;
for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
{
s = ml_get(lnum);
len = (int)STRLEN(s);
if (maxlen < len)
maxlen = len;
start_col = 0;
end_col = len;
if (regmatch.regprog != NULL && vim_regexec(®match, s, 0))
{
if (sort_rx)
{
start_col = (colnr_T)(regmatch.startp[0] - s);
end_col = (colnr_T)(regmatch.endp[0] - s);
}
else
start_col = (colnr_T)(regmatch.endp[0] - s);
}
else
if (regmatch.regprog != NULL)
end_col = 0;
if (sort_nr
#ifdef FEAT_FLOAT
|| sort_flt
#endif
)
{
s2 = s + end_col;
c = *s2;
*s2 = NUL;
p = s + start_col;
if (sort_nr)
{
if (sort_what & STR2NR_HEX)
s = skiptohex(p);
else if (sort_what & STR2NR_BIN)
s = skiptobin(p);
else
s = skiptodigit(p);
if (s > p && s[-1] == '-')
--s;
if (*s == NUL)
nrs[lnum - eap->line1].st_u.value = -MAXLNUM;
else
vim_str2nr(s, NULL, NULL, sort_what,
&nrs[lnum - eap->line1].st_u.value, NULL, 0);
}
#ifdef FEAT_FLOAT
else
{
s = skipwhite(p);
if (*s == '+')
s = skipwhite(s + 1);
if (*s == NUL)
nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
else
nrs[lnum - eap->line1].st_u.value_flt =
strtod((char *)s, NULL);
}
#endif
*s2 = c;
}
else
{
nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
}
nrs[lnum - eap->line1].lnum = lnum;
if (regmatch.regprog != NULL)
fast_breakcheck();
if (got_int)
goto sortend;
}
sortbuf1 = alloc((unsigned)maxlen + 1);
if (sortbuf1 == NULL)
goto sortend;
sortbuf2 = alloc((unsigned)maxlen + 1);
if (sortbuf2 == NULL)
goto sortend;
qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
if (sort_abort)
goto sortend;
lnum = eap->line2;
for (i = 0; i < count; ++i)
{
s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
if (!unique || i == 0
|| (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
{
STRCPY(sortbuf1, s);
if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
break;
}
fast_breakcheck();
if (got_int)
goto sortend;
}
if (i == count)
for (i = 0; i < count; ++i)
ml_delete(eap->line1, FALSE);
else
count = 0;
deleted = (long)(count - (lnum - eap->line2));
if (deleted > 0)
mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
else if (deleted < 0)
mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
curwin->w_cursor.lnum = eap->line1;
beginline(BL_WHITE | BL_FIX);
sortend:
vim_free(nrs);
vim_free(sortbuf1);
vim_free(sortbuf2);
vim_regfree(regmatch.regprog);
if (got_int)
EMSG(_(e_interr));
}
void
ex_retab(exarg_T *eap)
{
linenr_T lnum;
int got_tab = FALSE;
long num_spaces = 0;
long num_tabs;
long len;
long col;
long vcol;
long start_col = 0;
long start_vcol = 0;
int temp;
long old_len;
char_u *ptr;
char_u *new_line = (char_u *)1;
int did_undo;
int new_ts;
int save_list;
linenr_T first_line = 0;
linenr_T last_line = 0;
save_list = curwin->w_p_list;
curwin->w_p_list = 0;
new_ts = getdigits(&(eap->arg));
if (new_ts < 0)
{
EMSG(_(e_positive));
return;
}
if (new_ts == 0)
new_ts = curbuf->b_p_ts;
for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
{
ptr = ml_get(lnum);
col = 0;
vcol = 0;
did_undo = FALSE;
for (;;)
{
if (VIM_ISWHITE(ptr[col]))
{
if (!got_tab && num_spaces == 0)
{
start_vcol = vcol;
start_col = col;
}
if (ptr[col] == ' ')
num_spaces++;
else
got_tab = TRUE;
}
else
{
if (got_tab || (eap->forceit && num_spaces > 1))
{
len = num_spaces = vcol - start_vcol;
num_tabs = 0;
if (!curbuf->b_p_et)
{
temp = new_ts - (start_vcol % new_ts);
if (num_spaces >= temp)
{
num_spaces -= temp;
num_tabs++;
}
num_tabs += num_spaces / new_ts;
num_spaces -= (num_spaces / new_ts) * new_ts;
}
if (curbuf->b_p_et || got_tab ||
(num_spaces + num_tabs < len))
{
if (did_undo == FALSE)
{
did_undo = TRUE;
if (u_save((linenr_T)(lnum - 1),
(linenr_T)(lnum + 1)) == FAIL)
{
new_line = NULL;
break;
}
}
len = num_spaces + num_tabs;
old_len = (long)STRLEN(ptr);
new_line = lalloc(old_len - col + start_col + len + 1,
TRUE);
if (new_line == NULL)
break;
if (start_col > 0)
mch_memmove(new_line, ptr, (size_t)start_col);
mch_memmove(new_line + start_col + len,
ptr + col, (size_t)(old_len - col + 1));
ptr = new_line + start_col;
for (col = 0; col < len; col++)
ptr[col] = (col < num_tabs) ? '\t' : ' ';
ml_replace(lnum, new_line, FALSE);
if (first_line == 0)
first_line = lnum;
last_line = lnum;
ptr = new_line;
col = start_col + len;
}
}
got_tab = FALSE;
num_spaces = 0;
}
if (ptr[col] == NUL)
break;
vcol += chartabsize(ptr + col, (colnr_T)vcol);
#ifdef FEAT_MBYTE
if (has_mbyte)
col += (*mb_ptr2len)(ptr + col);
else
#endif
++col;
}
if (new_line == NULL)
break;
line_breakcheck();
}
if (got_int)
EMSG(_(e_interr));
if (curbuf->b_p_ts != new_ts)
redraw_curbuf_later(NOT_VALID);
if (first_line != 0)
changed_lines(first_line, 0, last_line + 1, 0L);
curwin->w_p_list = save_list;
curbuf->b_p_ts = new_ts;
coladvance(curwin->w_curswant);
u_clearline();
}
int
do_move(linenr_T line1, linenr_T line2, linenr_T dest)
{
char_u *str;
linenr_T l;
linenr_T extra;
linenr_T num_lines;
linenr_T last_line;
#ifdef FEAT_FOLDING
win_T *win;
tabpage_T *tp;
#endif
if (dest >= line1 && dest < line2)
{
EMSG(_("E134: Move lines into themselves"));
return FAIL;
}
num_lines = line2 - line1 + 1;
if (u_save(dest, dest + 1) == FAIL)
return FAIL;
for (extra = 0, l = line1; l <= line2; l++)
{
str = vim_strsave(ml_get(l + extra));
if (str != NULL)
{
ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
vim_free(str);
if (dest < line1)
extra++;
}
}
last_line = curbuf->b_ml.ml_line_count;
mark_adjust_nofold(line1, line2, last_line - line2, 0L);
if (dest >= line2)
{
mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
#ifdef FEAT_FOLDING
FOR_ALL_TAB_WINDOWS(tp, win) {
if (win->w_buffer == curbuf)
foldMoveRange(&win->w_folds, line1, line2, dest);
}
#endif
curbuf->b_op_start.lnum = dest - num_lines + 1;
curbuf->b_op_end.lnum = dest;
}
else
{
mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
#ifdef FEAT_FOLDING
FOR_ALL_TAB_WINDOWS(tp, win) {
if (win->w_buffer == curbuf)
foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
}
#endif
curbuf->b_op_start.lnum = dest + 1;
curbuf->b_op_end.lnum = dest + num_lines;
}
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
mark_adjust_nofold(last_line - num_lines + 1, last_line,
-(last_line - dest - extra), 0L);
if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
return FAIL;
for (l = line1; l <= line2; l++)
ml_delete(line1 + extra, TRUE);
if (!global_busy && num_lines > p_report)
{
if (num_lines == 1)
MSG(_("1 line moved"));
else
smsg((char_u *)_("%ld lines moved"), num_lines);
}
if (dest >= line1)
curwin->w_cursor.lnum = dest;
else
curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
if (line1 < dest)
{
dest += num_lines + 1;
last_line = curbuf->b_ml.ml_line_count;
if (dest > last_line + 1)
dest = last_line + 1;
changed_lines(line1, 0, dest, 0L);
}
else
changed_lines(dest + 1, 0, line1 + num_lines, 0L);
return OK;
}
void
ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
{
linenr_T count;
char_u *p;
count = line2 - line1 + 1;
curbuf->b_op_start.lnum = n + 1;
curbuf->b_op_end.lnum = n + count;
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
if (u_save(n, n + 1) == FAIL)
return;
curwin->w_cursor.lnum = n;
while (line1 <= line2)
{
p = vim_strsave(ml_get(line1));
if (p != NULL)
{
ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
vim_free(p);
}
if (line1 == n)
line1 = curwin->w_cursor.lnum;
++line1;
if (curwin->w_cursor.lnum < line1)
++line1;
if (curwin->w_cursor.lnum < line2)
++line2;
++curwin->w_cursor.lnum;
}
appended_lines_mark(n, count);
msgmore((long)count);
}
static char_u *prevcmd = NULL;
#if defined(EXITFREE) || defined(PROTO)
void
free_prev_shellcmd(void)
{
vim_free(prevcmd);
}
#endif
void
do_bang(
int addr_count,
exarg_T *eap,
int forceit,
int do_in,
int do_out)
{
char_u *arg = eap->arg;
linenr_T line1 = eap->line1;
linenr_T line2 = eap->line2;
char_u *newcmd = NULL;
int free_newcmd = FALSE;
int ins_prevcmd;
char_u *t;
char_u *p;
char_u *trailarg;
int len;
int scroll_save = msg_scroll;
if (check_restricted() || check_secure())
return;
if (addr_count == 0)
{
msg_scroll = FALSE;
autowrite_all();
msg_scroll = scroll_save;
}
ins_prevcmd = forceit;
trailarg = arg;
do
{
len = (int)STRLEN(trailarg) + 1;
if (newcmd != NULL)
len += (int)STRLEN(newcmd);
if (ins_prevcmd)
{
if (prevcmd == NULL)
{
EMSG(_(e_noprev));
vim_free(newcmd);
return;
}
len += (int)STRLEN(prevcmd);
}
if ((t = alloc((unsigned)len)) == NULL)
{
vim_free(newcmd);
return;
}
*t = NUL;
if (newcmd != NULL)
STRCAT(t, newcmd);
if (ins_prevcmd)
STRCAT(t, prevcmd);
p = t + STRLEN(t);
STRCAT(t, trailarg);
vim_free(newcmd);
newcmd = t;
trailarg = NULL;
while (*p)
{
if (*p == '!')
{
if (p > newcmd && p[-1] == '\\')
STRMOVE(p - 1, p);
else
{
trailarg = p;
*trailarg++ = NUL;
ins_prevcmd = TRUE;
break;
}
}
++p;
}
} while (trailarg != NULL);
vim_free(prevcmd);
prevcmd = newcmd;
if (bangredo)
{
char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
if (cmd != NULL)
{
AppendToRedobuffLit(cmd, -1);
vim_free(cmd);
}
else
AppendToRedobuffLit(prevcmd, -1);
AppendToRedobuff((char_u *)"\n");
bangredo = FALSE;
}
if (*p_shq != NUL)
{
newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
if (newcmd == NULL)
return;
STRCPY(newcmd, p_shq);
STRCAT(newcmd, prevcmd);
STRCAT(newcmd, p_shq);
free_newcmd = TRUE;
}
if (addr_count == 0)
{
msg_start();
msg_putchar(':');
msg_putchar('!');
msg_outtrans(newcmd);
msg_clr_eos();
windgoto(msg_row, msg_col);
do_shell(newcmd, 0);
}
else
{
do_filter(line1, line2, eap, newcmd, do_in, do_out);
#ifdef FEAT_AUTOCMD
apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
#endif
}
if (free_newcmd)
vim_free(newcmd);
}
static void
do_filter(
linenr_T line1,
linenr_T line2,
exarg_T *eap,
char_u *cmd,
int do_in,
int do_out)
{
char_u *itmp = NULL;
char_u *otmp = NULL;
linenr_T linecount;
linenr_T read_linecount;
pos_T cursor_save;
char_u *cmd_buf;
#ifdef FEAT_AUTOCMD
buf_T *old_curbuf = curbuf;
#endif
int shell_flags = 0;
if (*cmd == NUL)
return;
cursor_save = curwin->w_cursor;
linecount = line2 - line1 + 1;
curwin->w_cursor.lnum = line1;
curwin->w_cursor.col = 0;
changed_line_abv_curs();
invalidate_botline();
if (do_out)
shell_flags |= SHELL_DOOUT;
#ifdef FEAT_FILTERPIPE
if (!do_in && do_out && !p_stmp)
{
shell_flags |= SHELL_READ;
curwin->w_cursor.lnum = line2;
}
else if (do_in && !do_out && !p_stmp)
{
shell_flags |= SHELL_WRITE;
curbuf->b_op_start.lnum = line1;
curbuf->b_op_end.lnum = line2;
}
else if (do_in && do_out && !p_stmp)
{
shell_flags |= SHELL_READ|SHELL_WRITE;
curbuf->b_op_start.lnum = line1;
curbuf->b_op_end.lnum = line2;
curwin->w_cursor.lnum = line2;
}
else
#endif
if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL)
|| (do_out && (otmp = vim_tempname('o', FALSE)) == NULL))
{
EMSG(_(e_notmp));
goto filterend;
}
++no_wait_return;
if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
FALSE, FALSE, FALSE, TRUE) == FAIL)
{
msg_putchar('\n');
--no_wait_return;
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (!aborting())
#endif
(void)EMSG2(_(e_notcreate), itmp);
goto filterend;
}
#ifdef FEAT_AUTOCMD
if (curbuf != old_curbuf)
goto filterend;
#endif
if (!do_out)
msg_putchar('\n');
cmd_buf = make_filter_cmd(cmd, itmp, otmp);
if (cmd_buf == NULL)
goto filterend;
windgoto((int)Rows - 1, 0);
cursor_on();
if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
redraw_later_clear();
if (do_out)
{
if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
{
vim_free(cmd_buf);
goto error;
}
redraw_curbuf_later(VALID);
}
read_linecount = curbuf->b_ml.ml_line_count;
if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
{
redraw_later_clear();
wait_return(FALSE);
}
vim_free(cmd_buf);
did_check_timestamps = FALSE;
need_check_timestamps = TRUE;
ui_breakcheck();
got_int = FALSE;
if (do_out)
{
if (otmp != NULL)
{
if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
eap, READ_FILTER) != OK)
{
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (!aborting())
#endif
{
msg_putchar('\n');
EMSG2(_(e_notread), otmp);
}
goto error;
}
#ifdef FEAT_AUTOCMD
if (curbuf != old_curbuf)
goto filterend;
#endif
}
read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
if (shell_flags & SHELL_READ)
{
curbuf->b_op_start.lnum = line2 + 1;
curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
appended_lines_mark(line2, read_linecount);
}
if (do_in)
{
if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
{
if (read_linecount >= linecount)
mark_adjust(line1, line2, linecount, 0L);
else
{
mark_adjust(line1, line1 + read_linecount - 1,
linecount, 0L);
mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
}
}
curwin->w_cursor.lnum = line1;
del_lines(linecount, TRUE);
curbuf->b_op_start.lnum -= linecount;
curbuf->b_op_end.lnum -= linecount;
write_lnum_adjust(-linecount);
#ifdef FEAT_FOLDING
foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
#endif
}
else
{
linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
}
beginline(BL_WHITE | BL_FIX);
--no_wait_return;
if (linecount > p_report)
{
if (do_in)
{
vim_snprintf((char *)msg_buf, sizeof(msg_buf),
_("%ld lines filtered"), (long)linecount);
if (msg(msg_buf) && !msg_scroll)
set_keep_msg(msg_buf, 0);
}
else
msgmore((long)linecount);
}
}
else
{
error:
curwin->w_cursor = cursor_save;
--no_wait_return;
wait_return(FALSE);
}
filterend:
#ifdef FEAT_AUTOCMD
if (curbuf != old_curbuf)
{
--no_wait_return;
EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
}
#endif
if (itmp != NULL)
mch_remove(itmp);
if (otmp != NULL)
mch_remove(otmp);
vim_free(itmp);
vim_free(otmp);
}
void
do_shell(
char_u *cmd,
int flags)
{
buf_T *buf;
#ifndef FEAT_GUI_MSWIN
int save_nwr;
#endif
#ifdef MSWIN
int winstart = FALSE;
#endif
if (check_restricted() || check_secure())
{
msg_end();
return;
}
#ifdef MSWIN
if (cmd != NULL)
winstart = (STRNICMP(cmd, "start ", 6) == 0);
#endif
msg_putchar('\r');
#ifdef FEAT_AUTOCMD
if (!autocmd_busy)
#endif
{
#ifdef MSWIN
if (!winstart)
#endif
stoptermcap();
}
#ifdef MSWIN
if (!winstart)
#endif
msg_putchar('\n');
if (p_warn
#ifdef FEAT_AUTOCMD
&& !autocmd_busy
#endif
&& msg_silent == 0)
FOR_ALL_BUFFERS(buf)
if (bufIsChanged(buf))
{
#ifdef FEAT_GUI_MSWIN
if (!winstart)
starttermcap();
#endif
MSG_PUTS(_("[No write since last change]\n"));
#ifdef FEAT_GUI_MSWIN
if (!winstart)
stoptermcap();
#endif
break;
}
if (!swapping_screen())
windgoto(msg_row, msg_col);
cursor_on();
(void)call_shell(cmd, SHELL_COOKED | flags);
did_check_timestamps = FALSE;
need_check_timestamps = TRUE;
if (!swapping_screen())
{
msg_row = Rows - 1;
msg_col = 0;
}
#ifdef FEAT_AUTOCMD
if (autocmd_busy)
{
if (msg_silent == 0)
redraw_later_clear();
}
else
#endif
{
#ifndef FEAT_GUI_MSWIN
if (cmd == NULL
# ifdef WIN3264
|| (winstart && !need_wait_return)
# endif
)
{
if (msg_silent == 0)
redraw_later_clear();
need_wait_return = FALSE;
}
else
{
save_nwr = no_wait_return;
if (swapping_screen())
no_wait_return = FALSE;
# ifdef AMIGA
wait_return(term_console ? -1 : msg_silent == 0);
# else
wait_return(msg_silent == 0);
# endif
no_wait_return = save_nwr;
}
#endif
#ifdef MSWIN
if (!winstart)
#endif
starttermcap();
#ifdef AMIGA
if (skip_redraw)
{
if (msg_silent == 0)
redraw_later_clear();
}
else if (term_console)
{
OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q"));
if (got_int && msg_silent == 0)
redraw_later_clear();
else
must_redraw = 0;
}
#endif
}
display_errors();
#ifdef FEAT_AUTOCMD
apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
#endif
}
char_u *
make_filter_cmd(
char_u *cmd,
char_u *itmp,
char_u *otmp)
{
char_u *buf;
long_u len;
#if defined(UNIX)
int is_fish_shell;
char_u *shell_name = get_isolated_shell_name();
is_fish_shell = (fnamecmp(shell_name, "fish") == 0);
vim_free(shell_name);
if (is_fish_shell)
len = (long_u)STRLEN(cmd) + 13;
else
#endif
len = (long_u)STRLEN(cmd) + 3;
if (itmp != NULL)
len += (long_u)STRLEN(itmp) + 9;
if (otmp != NULL)
len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2;
buf = lalloc(len, TRUE);
if (buf == NULL)
return NULL;
#if defined(UNIX)
if (itmp != NULL || otmp != NULL)
{
if (is_fish_shell)
vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd);
else
vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
}
else
STRCPY(buf, cmd);
if (itmp != NULL)
{
STRCAT(buf, " < ");
STRCAT(buf, itmp);
}
#else
STRCPY(buf, cmd);
if (itmp != NULL)
{
char_u *p;
if (*p_shq == NUL)
{
p = vim_strchr(buf, '|');
if (p != NULL)
*p = NUL;
}
STRCAT(buf, " <");
STRCAT(buf, itmp);
if (*p_shq == NUL)
{
p = vim_strchr(cmd, '|');
if (p != NULL)
{
STRCAT(buf, " ");
STRCAT(buf, p);
}
}
}
#endif
if (otmp != NULL)
append_redir(buf, (int)len, p_srr, otmp);
return buf;
}
void
append_redir(
char_u *buf,
int buflen,
char_u *opt,
char_u *fname)
{
char_u *p;
char_u *end;
end = buf + STRLEN(buf);
for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
{
if (p[1] == 's')
break;
if (p[1] == '%')
++p;
}
if (p != NULL)
{
*end = ' ';
vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
(char *)opt, (char *)fname);
}
else
vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
#ifdef FEAT_QUICKFIX
" %s %s",
#else
" %s%s",
#endif
(char *)opt, (char *)fname);
}
#if defined(FEAT_VIMINFO) || defined(PROTO)
static int no_viminfo(void);
static int read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing);
static void write_viminfo_version(FILE *fp_out);
static void write_viminfo_barlines(vir_T *virp, FILE *fp_out);
static int viminfo_errcnt;
static int
no_viminfo(void)
{
return STRCMP(p_viminfofile, "NONE") == 0;
}
int
viminfo_error(char *errnum, char *message, char_u *line)
{
vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
errnum, message);
STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
if (IObuff[STRLEN(IObuff) - 1] == '\n')
IObuff[STRLEN(IObuff) - 1] = NUL;
emsg(IObuff);
if (++viminfo_errcnt >= 10)
{
EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
return TRUE;
}
return FALSE;
}
int
read_viminfo(
char_u *file,
int flags)
{
FILE *fp;
char_u *fname;
if (no_viminfo())
return FAIL;
fname = viminfo_filename(file);
if (fname == NULL)
return FAIL;
fp = mch_fopen((char *)fname, READBIN);
if (p_verbose > 0)
{
verbose_enter();
smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
fname,
(flags & VIF_WANT_INFO) ? _(" info") : "",
(flags & VIF_WANT_MARKS) ? _(" marks") : "",
(flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
fp == NULL ? _(" FAILED") : "");
verbose_leave();
}
vim_free(fname);
if (fp == NULL)
return FAIL;
viminfo_errcnt = 0;
do_viminfo(fp, NULL, flags);
fclose(fp);
return OK;
}
void
write_viminfo(char_u *file, int forceit)
{
char_u *fname;
FILE *fp_in = NULL;
FILE *fp_out = NULL;
char_u *tempname = NULL;
stat_T st_new;
char_u *wp;
#if defined(UNIX) || defined(VMS)
mode_t umask_save;
#endif
#ifdef UNIX
int shortname = FALSE;
stat_T st_old;
#endif
#ifdef WIN3264
int hidden = FALSE;
#endif
if (no_viminfo())
return;
fname = viminfo_filename(file);
if (fname == NULL)
return;
fp_in = mch_fopen((char *)fname, READBIN);
if (fp_in == NULL)
{
if (mch_stat((char *)fname, &st_new) == 0)
goto end;
#if defined(UNIX) || defined(VMS)
umask_save = umask(077);
#endif
fp_out = mch_fopen((char *)fname, WRITEBIN);
#if defined(UNIX) || defined(VMS)
(void)umask(umask_save);
#endif
}
else
{
#ifdef UNIX
st_old.st_dev = (dev_t)0;
st_old.st_ino = 0;
st_old.st_mode = 0600;
if (mch_stat((char *)fname, &st_old) == 0
&& getuid() != ROOT_UID
&& !(st_old.st_uid == getuid()
? (st_old.st_mode & 0200)
: (st_old.st_gid == getgid()
? (st_old.st_mode & 0020)
: (st_old.st_mode & 0002))))
{
int tt = msg_didany;
EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
msg_didany = tt;
fclose(fp_in);
goto end;
}
#endif
#ifdef WIN3264
hidden = mch_ishidden(fname);
#endif
for (;;)
{
tempname = buf_modname(
#ifdef UNIX
shortname,
#else
FALSE,
#endif
fname,
#ifdef VMS
(char_u *)"-tmp",
#else
(char_u *)".tmp",
#endif
FALSE);
if (tempname == NULL)
break;
if (mch_stat((char *)tempname, &st_new) == 0)
{
#ifdef UNIX
if (!shortname && st_new.st_dev == st_old.st_dev
&& st_new.st_ino == st_old.st_ino)
{
vim_free(tempname);
tempname = NULL;
shortname = TRUE;
continue;
}
#endif
wp = tempname + STRLEN(tempname) - 5;
if (wp < gettail(tempname))
wp = gettail(tempname);
for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
--*wp)
{
if (*wp == 'a')
{
EMSG2(_("E929: Too many viminfo temp files, like %s!"),
tempname);
vim_free(tempname);
tempname = NULL;
break;
}
}
}
break;
}
if (tempname != NULL)
{
#ifdef VMS
umask_save = umask(077);
fp_out = mch_fopen((char *)tempname, WRITEBIN);
(void)umask(umask_save);
#else
int fd;
# ifdef UNIX
umask_save = umask(0);
fd = mch_open((char *)tempname,
O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
(int)((st_old.st_mode & 0777) | 0600));
(void)umask(umask_save);
# else
fd = mch_open((char *)tempname,
O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
# endif
if (fd < 0)
fp_out = NULL;
else
fp_out = fdopen(fd, WRITEBIN);
#endif
if (fp_out == NULL)
{
vim_free(tempname);
if ((tempname = vim_tempname('o', TRUE)) != NULL)
fp_out = mch_fopen((char *)tempname, WRITEBIN);
}
#if defined(UNIX) && defined(HAVE_FCHOWN)
if (fp_out != NULL)
ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
#endif
}
}
if (fp_out == NULL)
{
EMSG2(_("E138: Can't write viminfo file %s!"),
(fp_in == NULL || tempname == NULL) ? fname : tempname);
if (fp_in != NULL)
fclose(fp_in);
goto end;
}
if (p_verbose > 0)
{
verbose_enter();
smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
verbose_leave();
}
viminfo_errcnt = 0;
do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
if (fclose(fp_out) == EOF)
++viminfo_errcnt;
if (fp_in != NULL)
{
fclose(fp_in);
if (viminfo_errcnt == 0)
{
if (vim_rename(tempname, fname) == -1)
{
++viminfo_errcnt;
EMSG2(_("E886: Can't rename viminfo file to %s!"), fname);
}
# ifdef WIN3264
else if (hidden)
mch_hide(fname);
# endif
}
if (viminfo_errcnt > 0)
mch_remove(tempname);
}
end:
vim_free(fname);
vim_free(tempname);
}
static char_u *
viminfo_filename(char_u *file)
{
if (file == NULL || *file == NUL)
{
if (*p_viminfofile != NUL)
file = p_viminfofile;
else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
{
#ifdef VIMINFO_FILE2
# ifdef VMS
if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
# else
# ifdef MSWIN
if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
&& mch_getenv((char_u *)"HOME") == NULL)
# else
if (mch_getenv((char_u *)"HOME") == NULL)
# endif
# endif
{
expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
if (STRCMP("$VIM", NameBuff) != 0)
file = (char_u *)VIMINFO_FILE2;
else
file = (char_u *)VIMINFO_FILE;
}
else
#endif
file = (char_u *)VIMINFO_FILE;
}
expand_env(file, NameBuff, MAXPATHL);
file = NameBuff;
}
return vim_strsave(file);
}
static void
do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
{
int eof = FALSE;
vir_T vir;
int merge = FALSE;
int do_copy_marks = FALSE;
garray_T buflist;
if ((vir.vir_line = alloc(LSIZE)) == NULL)
return;
vir.vir_fd = fp_in;
#ifdef FEAT_MBYTE
vir.vir_conv.vc_type = CONV_NONE;
#endif
ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100);
vir.vir_version = -1;
if (fp_in != NULL)
{
if (flags & VIF_WANT_INFO)
{
if (fp_out != NULL)
{
prepare_viminfo_registers();
prepare_viminfo_marks();
}
eof = read_viminfo_up_to_marks(&vir,
flags & VIF_FORCEIT, fp_out != NULL);
merge = TRUE;
}
else if (flags != 0)
while (!(eof = viminfo_readline(&vir))
&& vir.vir_line[0] != '>')
;
do_copy_marks = (flags &
(VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT));
}
if (fp_out != NULL)
{
fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
VIM_VERSION_MEDIUM);
fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
write_viminfo_version(fp_out);
#ifdef FEAT_MBYTE
fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
fprintf(fp_out, "*encoding=%s\n\n", p_enc);
#endif
write_viminfo_search_pattern(fp_out);
write_viminfo_sub_string(fp_out);
#ifdef FEAT_CMDHIST
write_viminfo_history(fp_out, merge);
#endif
write_viminfo_registers(fp_out);
finish_viminfo_registers();
#ifdef FEAT_EVAL
write_viminfo_varlist(fp_out);
#endif
write_viminfo_filemarks(fp_out);
finish_viminfo_marks();
write_viminfo_bufferlist(fp_out);
write_viminfo_barlines(&vir, fp_out);
if (do_copy_marks)
ga_init2(&buflist, sizeof(buf_T *), 50);
write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
}
if (do_copy_marks)
{
copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
if (fp_out != NULL)
ga_clear(&buflist);
}
vim_free(vir.vir_line);
#ifdef FEAT_MBYTE
if (vir.vir_conv.vc_type != CONV_NONE)
convert_setup(&vir.vir_conv, NULL, NULL);
#endif
ga_clear_strings(&vir.vir_barlines);
}
static int
read_viminfo_up_to_marks(
vir_T *virp,
int forceit,
int writing)
{
int eof;
buf_T *buf;
int got_encoding = FALSE;
#ifdef FEAT_CMDHIST
prepare_viminfo_history(forceit ? 9999 : 0, writing);
#endif
eof = viminfo_readline(virp);
while (!eof && virp->vir_line[0] != '>')
{
switch (virp->vir_line[0])
{
case '+':
case '^':
case '<':
case NUL:
case '\r':
case '\n':
case '#':
eof = viminfo_readline(virp);
break;
case '|':
eof = read_viminfo_barline(virp, got_encoding,
forceit, writing);
break;
case '*':
got_encoding = TRUE;
eof = viminfo_encoding(virp);
break;
case '!':
#ifdef FEAT_EVAL
eof = read_viminfo_varlist(virp, writing);
#else
eof = viminfo_readline(virp);
#endif
break;
case '%':
eof = read_viminfo_bufferlist(virp, writing);
break;
case '"':
if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
eof = read_viminfo_register(virp, forceit);
else
do {
eof = viminfo_readline(virp);
} while (!eof && (virp->vir_line[0] == TAB
|| virp->vir_line[0] == '<'));
break;
case '/':
case '&':
case '~':
eof = read_viminfo_search_pattern(virp, forceit);
break;
case '$':
eof = read_viminfo_sub_string(virp, forceit);
break;
case ':':
case '?':
case '=':
case '@':
#ifdef FEAT_CMDHIST
if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
eof = read_viminfo_history(virp, writing);
else
#endif
eof = viminfo_readline(virp);
break;
case '-':
case '\'':
if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
eof = read_viminfo_filemark(virp, forceit);
else
eof = viminfo_readline(virp);
break;
default:
if (viminfo_error("E575: ", _("Illegal starting char"),
virp->vir_line))
eof = TRUE;
else
eof = viminfo_readline(virp);
break;
}
}
#ifdef FEAT_CMDHIST
if (!writing)
finish_viminfo_history(virp);
#endif
FOR_ALL_BUFFERS(buf)
fmarks_check_names(buf);
return eof;
}
static int
viminfo_encoding(vir_T *virp)
{
#ifdef FEAT_MBYTE
char_u *p;
int i;
if (get_viminfo_parameter('c') != 0)
{
p = vim_strchr(virp->vir_line, '=');
if (p != NULL)
{
++p;
for (i = 0; vim_isprintc(p[i]); ++i)
;
p[i] = NUL;
convert_setup(&virp->vir_conv, p, p_enc);
}
}
#endif
return viminfo_readline(virp);
}
int
viminfo_readline(vir_T *virp)
{
return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
}
char_u *
viminfo_readstring(
vir_T *virp,
int off,
int convert UNUSED)
{
char_u *retval;
char_u *s, *d;
long len;
if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
{
len = atol((char *)virp->vir_line + off + 1);
retval = lalloc(len, TRUE);
if (retval == NULL)
{
(void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
return NULL;
}
(void)vim_fgets(retval, (int)len, virp->vir_fd);
s = retval + 1;
}
else
{
retval = vim_strsave(virp->vir_line + off);
if (retval == NULL)
return NULL;
s = retval;
}
d = retval;
while (*s != NUL && *s != '\n')
{
if (s[0] == Ctrl_V && s[1] != NUL)
{
if (s[1] == 'n')
*d++ = '\n';
else
*d++ = Ctrl_V;
s += 2;
}
else
*d++ = *s++;
}
*d = NUL;
#ifdef FEAT_MBYTE
if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
{
d = string_convert(&virp->vir_conv, retval, NULL);
if (d != NULL)
{
vim_free(retval);
retval = d;
}
}
#endif
return retval;
}
void
viminfo_writestring(FILE *fd, char_u *p)
{
int c;
char_u *s;
int len = 0;
for (s = p; *s != NUL; ++s)
{
if (*s == Ctrl_V || *s == '\n')
++len;
++len;
}
if (len > LSIZE / 2)
fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
while ((c = *p++) != NUL)
{
if (c == Ctrl_V || c == '\n')
{
putc(Ctrl_V, fd);
if (c == '\n')
c = 'n';
}
putc(c, fd);
}
putc('\n', fd);
}
int
barline_writestring(FILE *fd, char_u *s, int remaining_start)
{
char_u *p;
int remaining = remaining_start;
int len = 2;
for (p = s; *p != NUL; ++p)
{
if (*p == NL)
len += 2;
else if (*p == '"' || *p == '\\')
len += 2;
else
++len;
}
if (len > remaining - 2)
{
fprintf(fd, ">%d\n|<", len);
remaining = LSIZE - 20;
}
putc('"', fd);
for (p = s; *p != NUL; ++p)
{
if (*p == NL)
{
putc('\\', fd);
putc('n', fd);
--remaining;
}
else if (*p == '"' || *p == '\\')
{
putc('\\', fd);
putc(*p, fd);
--remaining;
}
else
putc(*p, fd);
--remaining;
if (remaining < 3)
{
putc('\n', fd);
putc('|', fd);
putc('<', fd);
remaining = LSIZE - 20;
}
}
putc('"', fd);
return remaining - 2;
}
static int
barline_parse(vir_T *virp, char_u *text, garray_T *values)
{
char_u *p = text;
char_u *nextp = NULL;
char_u *buf = NULL;
bval_T *value;
int i;
int allocated = FALSE;
int eof;
#ifdef FEAT_MBYTE
char_u *sconv;
int converted;
#endif
while (*p == ',')
{
++p;
if (ga_grow(values, 1) == FAIL)
break;
value = (bval_T *)(values->ga_data) + values->ga_len;
if (*p == '>')
{
if (!allocated)
{
for (i = 0; i < values->ga_len; ++i)
{
bval_T *vp = (bval_T *)(values->ga_data) + i;
if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
{
vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
vp->bv_allocated = TRUE;
}
}
allocated = TRUE;
}
if (vim_isdigit(p[1]))
{
size_t len;
size_t todo;
size_t n;
++p;
len = getdigits(&p);
buf = alloc((int)(len + 1));
if (buf == NULL)
return TRUE;
p = buf;
for (todo = len; todo > 0; todo -= n)
{
eof = viminfo_readline(virp);
if (eof || virp->vir_line[0] != '|'
|| virp->vir_line[1] != '<')
{
vim_free(buf);
return eof || virp->vir_line[0] == '|';
}
n = STRLEN(virp->vir_line);
while (n > 0 && (virp->vir_line[n - 1] == NL
|| virp->vir_line[n - 1] == CAR))
--n;
n -= 2;
if (n > todo)
{
nextp = virp->vir_line + 2 + todo;
n = todo;
}
mch_memmove(p, virp->vir_line + 2, n);
p += n;
}
*p = NUL;
p = buf;
}
else
{
eof = viminfo_readline(virp);
if (eof || virp->vir_line[0] != '|'
|| virp->vir_line[1] != '<')
return eof || virp->vir_line[0] == '|';
p = virp->vir_line + 2;
}
}
if (isdigit(*p))
{
value->bv_type = BVAL_NR;
value->bv_nr = getdigits(&p);
++values->ga_len;
}
else if (*p == '"')
{
int len = 0;
char_u *s = p;
++p;
while (*p != '"')
{
if (*p == NL || *p == NUL)
return TRUE;
if (*p == '\\')
{
++p;
if (*p == 'n')
s[len++] = '\n';
else
s[len++] = *p;
++p;
}
else
s[len++] = *p++;
}
++p;
s[len] = NUL;
#ifdef FEAT_MBYTE
converted = FALSE;
if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
{
sconv = string_convert(&virp->vir_conv, s, NULL);
if (sconv != NULL)
{
if (s == buf)
vim_free(s);
s = sconv;
buf = s;
converted = TRUE;
}
}
#endif
if (s != buf && allocated)
s = vim_strsave(s);
value->bv_string = s;
value->bv_type = BVAL_STRING;
value->bv_len = len;
value->bv_allocated = allocated
#ifdef FEAT_MBYTE
|| converted
#endif
;
++values->ga_len;
if (nextp != NULL)
{
p = nextp;
nextp = NULL;
}
}
else if (*p == ',')
{
value->bv_type = BVAL_EMPTY;
++values->ga_len;
}
else
break;
}
return TRUE;
}
static int
read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
{
char_u *p = virp->vir_line + 1;
int bartype;
garray_T values;
bval_T *vp;
int i;
int read_next = TRUE;
if (*p == '<')
{
if (writing)
ga_add_string(&virp->vir_barlines, virp->vir_line);
}
else
{
ga_init2(&values, sizeof(bval_T), 20);
bartype = getdigits(&p);
switch (bartype)
{
case BARTYPE_VERSION:
if (!got_encoding)
{
read_next = barline_parse(virp, p, &values);
vp = (bval_T *)values.ga_data;
if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
virp->vir_version = vp->bv_nr;
}
break;
case BARTYPE_HISTORY:
read_next = barline_parse(virp, p, &values);
handle_viminfo_history(&values, writing);
break;
case BARTYPE_REGISTER:
read_next = barline_parse(virp, p, &values);
handle_viminfo_register(&values, force);
break;
case BARTYPE_MARK:
read_next = barline_parse(virp, p, &values);
handle_viminfo_mark(&values, force);
break;
default:
if (writing)
ga_add_string(&virp->vir_barlines, virp->vir_line);
}
for (i = 0; i < values.ga_len; ++i)
{
vp = (bval_T *)values.ga_data + i;
if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
vim_free(vp->bv_string);
}
ga_clear(&values);
}
if (read_next)
return viminfo_readline(virp);
return FALSE;
}
static void
write_viminfo_version(FILE *fp_out)
{
fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
BARTYPE_VERSION, VIMINFO_VERSION);
}
static void
write_viminfo_barlines(vir_T *virp, FILE *fp_out)
{
int i;
garray_T *gap = &virp->vir_barlines;
int seen_useful = FALSE;
char *line;
if (gap->ga_len > 0)
{
fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
for (i = 0; i < gap->ga_len; ++i)
{
line = ((char **)(gap->ga_data))[i];
if (seen_useful || line[1] != '<')
{
fputs(line, fp_out);
seen_useful = TRUE;
}
}
}
}
#endif
time_T
vim_time(void)
{
# ifdef FEAT_EVAL
return time_for_testing == 0 ? time(NULL) : time_for_testing;
# else
return time(NULL);
# endif
}
void
do_fixdel(exarg_T *eap UNUSED)
{
char_u *p;
p = find_termcode((char_u *)"kb");
add_termcode((char_u *)"kD", p != NULL
&& *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
}
void
print_line_no_prefix(
linenr_T lnum,
int use_number,
int list)
{
char_u numbuf[30];
if (curwin->w_p_nu || use_number)
{
vim_snprintf((char *)numbuf, sizeof(numbuf),
"%*ld ", number_width(curwin), (long)lnum);
msg_puts_attr(numbuf, HL_ATTR(HLF_N));
}
msg_prt_line(ml_get(lnum), list);
}
void
print_line(linenr_T lnum, int use_number, int list)
{
int save_silent = silent_mode;
if (message_filtered(ml_get(lnum)))
return;
msg_start();
silent_mode = FALSE;
info_message = TRUE;
print_line_no_prefix(lnum, use_number, list);
if (save_silent)
{
msg_putchar('\n');
cursor_on();
out_flush();
silent_mode = save_silent;
}
info_message = FALSE;
}
int
rename_buffer(char_u *new_fname)
{
char_u *fname, *sfname, *xfname;
buf_T *buf;
#ifdef FEAT_AUTOCMD
buf = curbuf;
apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
if (buf != curbuf)
return FAIL;
# ifdef FEAT_EVAL
if (aborting())
return FAIL;
# endif
#endif
fname = curbuf->b_ffname;
sfname = curbuf->b_sfname;
xfname = curbuf->b_fname;
curbuf->b_ffname = NULL;
curbuf->b_sfname = NULL;
if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL)
{
curbuf->b_ffname = fname;
curbuf->b_sfname = sfname;
return FAIL;
}
curbuf->b_flags |= BF_NOTEDITED;
if (xfname != NULL && *xfname != NUL)
{
buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
if (buf != NULL && !cmdmod.keepalt)
curwin->w_alt_fnum = buf->b_fnum;
}
vim_free(fname);
vim_free(sfname);
#ifdef FEAT_AUTOCMD
apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
#endif
DO_AUTOCHDIR
return OK;
}
void
ex_file(exarg_T *eap)
{
if (eap->addr_count > 0
&& (*eap->arg != NUL
|| eap->line2 > 0
|| eap->addr_count > 1))
{
EMSG(_(e_invarg));
return;
}
if (*eap->arg != NUL || eap->addr_count == 1)
{
if (rename_buffer(eap->arg) == FAIL)
return;
}
if (!shortmess(SHM_FILEINFO))
fileinfo(FALSE, FALSE, eap->forceit);
redraw_tabline = TRUE;
}
void
ex_update(exarg_T *eap)
{
if (curbufIsChanged())
(void)do_write(eap);
}
void
ex_write(exarg_T *eap)
{
if (eap->usefilter)
do_bang(1, eap, FALSE, TRUE, FALSE);
else
(void)do_write(eap);
}
int
do_write(exarg_T *eap)
{
int other;
char_u *fname = NULL;
char_u *ffname;
int retval = FAIL;
char_u *free_fname = NULL;
#ifdef FEAT_BROWSE
char_u *browse_file = NULL;
#endif
buf_T *alt_buf = NULL;
int name_was_missing;
if (not_writing())
return FAIL;
ffname = eap->arg;
#ifdef FEAT_BROWSE
if (cmdmod.browse)
{
browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
NULL, NULL, NULL, curbuf);
if (browse_file == NULL)
goto theend;
ffname = browse_file;
}
#endif
if (*ffname == NUL)
{
if (eap->cmdidx == CMD_saveas)
{
EMSG(_(e_argreq));
goto theend;
}
other = FALSE;
}
else
{
fname = ffname;
free_fname = fix_fname(ffname);
if (free_fname != NULL)
ffname = free_fname;
other = otherfile(ffname);
}
if (other)
{
if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
|| eap->cmdidx == CMD_saveas)
alt_buf = setaltfname(ffname, fname, (linenr_T)1);
else
alt_buf = buflist_findname(ffname);
if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
{
EMSG(_(e_bufloaded));
goto theend;
}
}
if (!other && (
#ifdef FEAT_QUICKFIX
bt_dontwrite_msg(curbuf) ||
#endif
check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
goto theend;
if (!other)
{
ffname = curbuf->b_ffname;
fname = curbuf->b_fname;
if ( (eap->line1 != 1
|| eap->line2 != curbuf->b_ml.ml_line_count)
&& !eap->forceit
&& !eap->append
&& !p_wa)
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if (p_confirm || cmdmod.confirm)
{
if (vim_dialog_yesno(VIM_QUESTION, NULL,
(char_u *)_("Write partial file?"), 2) != VIM_YES)
goto theend;
eap->forceit = TRUE;
}
else
#endif
{
EMSG(_("E140: Use ! to write partial buffer"));
goto theend;
}
}
}
if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
{
if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
{
#ifdef FEAT_AUTOCMD
buf_T *was_curbuf = curbuf;
apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
# ifdef FEAT_EVAL
if (curbuf != was_curbuf || aborting())
# else
if (curbuf != was_curbuf)
# endif
{
retval = FAIL;
goto theend;
}
#endif
fname = alt_buf->b_fname;
alt_buf->b_fname = curbuf->b_fname;
curbuf->b_fname = fname;
fname = alt_buf->b_ffname;
alt_buf->b_ffname = curbuf->b_ffname;
curbuf->b_ffname = fname;
fname = alt_buf->b_sfname;
alt_buf->b_sfname = curbuf->b_sfname;
curbuf->b_sfname = fname;
buf_name_changed(curbuf);
#ifdef FEAT_AUTOCMD
apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
if (!alt_buf->b_p_bl)
{
alt_buf->b_p_bl = TRUE;
apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
}
# ifdef FEAT_EVAL
if (curbuf != was_curbuf || aborting())
# else
if (curbuf != was_curbuf)
# endif
{
retval = FAIL;
goto theend;
}
if (*curbuf->b_p_ft == NUL)
{
if (au_has_group((char_u *)"filetypedetect"))
(void)do_doautocmd((char_u *)"filetypedetect BufRead",
TRUE, NULL);
do_modelines(0);
}
fname = curbuf->b_sfname;
#endif
}
name_was_missing = curbuf->b_ffname == NULL;
retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
eap, eap->append, eap->forceit, TRUE, FALSE);
if (eap->cmdidx == CMD_saveas)
{
if (retval == OK)
{
curbuf->b_p_ro = FALSE;
redraw_tabline = TRUE;
}
}
if (eap->cmdidx == CMD_saveas || name_was_missing)
{
DO_AUTOCHDIR
}
}
theend:
#ifdef FEAT_BROWSE
vim_free(browse_file);
#endif
vim_free(free_fname);
return retval;
}
int
check_overwrite(
exarg_T *eap,
buf_T *buf,
char_u *fname,
char_u *ffname,
int other)
{
if ( (other
|| (buf->b_flags & BF_NOTEDITED)
|| ((buf->b_flags & BF_NEW)
&& vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
|| (buf->b_flags & BF_READERR))
&& !p_wa
#ifdef FEAT_QUICKFIX
&& !bt_nofile(buf)
#endif
&& vim_fexists(ffname))
{
if (!eap->forceit && !eap->append)
{
#ifdef UNIX
if (mch_isdir(ffname))
{
EMSG2(_(e_isadir2), ffname);
return FAIL;
}
#endif
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if (p_confirm || cmdmod.confirm)
{
char_u buff[DIALOG_MSG_SIZE];
dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname);
if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES)
return FAIL;
eap->forceit = TRUE;
}
else
#endif
{
EMSG(_(e_exists));
return FAIL;
}
}
if (other && !emsg_silent)
{
char_u *dir;
char_u *p;
int r;
char_u *swapname;
if (*p_dir == NUL)
{
dir = alloc(5);
if (dir == NULL)
return FAIL;
STRCPY(dir, ".");
}
else
{
dir = alloc(MAXPATHL);
if (dir == NULL)
return FAIL;
p = p_dir;
copy_option_part(&p, dir, MAXPATHL, ",");
}
swapname = makeswapname(fname, ffname, curbuf, dir);
vim_free(dir);
r = vim_fexists(swapname);
if (r && !eap->forceit)
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if (p_confirm || cmdmod.confirm)
{
char_u buff[DIALOG_MSG_SIZE];
dialog_msg(buff,
_("Swap file \"%s\" exists, overwrite anyway?"),
swapname);
if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
!= VIM_YES)
{
vim_free(swapname);
return FAIL;
}
eap->forceit = TRUE;
}
else
#endif
{
EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"),
swapname);
vim_free(swapname);
return FAIL;
}
}
vim_free(swapname);
}
}
return OK;
}
void
ex_wnext(exarg_T *eap)
{
int i;
if (eap->cmd[1] == 'n')
i = curwin->w_arg_idx + (int)eap->line2;
else
i = curwin->w_arg_idx - (int)eap->line2;
eap->line1 = 1;
eap->line2 = curbuf->b_ml.ml_line_count;
if (do_write(eap) != FAIL)
do_argfile(eap, i);
}
void
do_wqall(exarg_T *eap)
{
buf_T *buf;
int error = 0;
int save_forceit = eap->forceit;
if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall)
exiting = TRUE;
FOR_ALL_BUFFERS(buf)
{
if (bufIsChanged(buf) && !bt_dontwrite(buf))
{
if (not_writing())
{
++error;
break;
}
#ifdef FEAT_BROWSE
if (buf->b_ffname == NULL && cmdmod.browse)
browse_save_fname(buf);
#endif
if (buf->b_ffname == NULL)
{
EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum);
++error;
}
else if (check_readonly(&eap->forceit, buf)
|| check_overwrite(eap, buf, buf->b_fname, buf->b_ffname,
FALSE) == FAIL)
{
++error;
}
else
{
#ifdef FEAT_AUTOCMD
bufref_T bufref;
set_bufref(&bufref, buf);
#endif
if (buf_write_all(buf, eap->forceit) == FAIL)
++error;
#ifdef FEAT_AUTOCMD
if (!bufref_valid(&bufref))
buf = firstbuf;
#endif
}
eap->forceit = save_forceit;
}
}
if (exiting)
{
if (!error)
getout(0);
not_exiting();
}
}
int
not_writing(void)
{
if (p_write)
return FALSE;
EMSG(_("E142: File not written: Writing is disabled by 'write' option"));
return TRUE;
}
static int
check_readonly(int *forceit, buf_T *buf)
{
stat_T st;
if (!*forceit && (buf->b_p_ro
|| (mch_stat((char *)buf->b_ffname, &st) >= 0
&& check_file_readonly(buf->b_ffname, 0777))))
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
{
char_u buff[DIALOG_MSG_SIZE];
if (buf->b_p_ro)
dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
buf->b_fname);
else
dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"),
buf->b_fname);
if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
{
*forceit = TRUE;
return FALSE;
}
else
return TRUE;
}
else
#endif
if (buf->b_p_ro)
EMSG(_(e_readonly));
else
EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
buf->b_fname);
return TRUE;
}
return FALSE;
}
int
getfile(
int fnum,
char_u *ffname,
char_u *sfname,
int setpm,
linenr_T lnum,
int forceit)
{
int other;
int retval;
char_u *free_me = NULL;
if (text_locked())
return GETFILE_ERROR;
#ifdef FEAT_AUTOCMD
if (curbuf_locked())
return GETFILE_ERROR;
#endif
if (fnum == 0)
{
fname_expand(curbuf, &ffname, &sfname);
other = otherfile(ffname);
free_me = ffname;
}
else
other = (fnum != curbuf->b_fnum);
if (other)
++no_wait_return;
if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf)
&& curbufIsChanged() && autowrite(curbuf, forceit) == FAIL)
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if (p_confirm && p_write)
dialog_changed(curbuf, FALSE);
if (curbufIsChanged())
#endif
{
if (other)
--no_wait_return;
no_write_message();
retval = GETFILE_NOT_WRITTEN;
goto theend;
}
}
if (other)
--no_wait_return;
if (setpm)
setpcmark();
if (!other)
{
if (lnum != 0)
curwin->w_cursor.lnum = lnum;
check_cursor_lnum();
beginline(BL_SOL | BL_FIX);
retval = GETFILE_SAME_FILE;
}
else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
(buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
curwin) == OK)
retval = GETFILE_OPEN_OTHER;
else
retval = GETFILE_ERROR;
theend:
vim_free(free_me);
return retval;
}
int
do_ecmd(
int fnum,
char_u *ffname,
char_u *sfname,
exarg_T *eap,
linenr_T newlnum,
int flags,
win_T *oldwin)
{
int other_file;
int oldbuf;
#ifdef FEAT_AUTOCMD
int auto_buf = FALSE;
char_u *new_name = NULL;
int did_set_swapcommand = FALSE;
#endif
buf_T *buf;
bufref_T bufref;
#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
bufref_T old_curbuf;
#endif
char_u *free_fname = NULL;
#ifdef FEAT_BROWSE
char_u *browse_file = NULL;
#endif
int retval = FAIL;
long n;
pos_T orig_pos;
linenr_T topline = 0;
int newcol = -1;
int solcol = -1;
pos_T *pos;
#ifdef FEAT_SUN_WORKSHOP
char_u *cp;
#endif
char_u *command = NULL;
#ifdef FEAT_SPELL
int did_get_winopts = FALSE;
#endif
int readfile_flags = 0;
int did_inc_redrawing_disabled = FALSE;
if (eap != NULL)
command = eap->do_ecmd_cmd;
#if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
set_bufref(&old_curbuf, curbuf);
#endif
if (fnum != 0)
{
if (fnum == curbuf->b_fnum)
return OK;
other_file = TRUE;
}
else
{
#ifdef FEAT_BROWSE
if (cmdmod.browse)
{
# ifdef FEAT_AUTOCMD
if (
# ifdef FEAT_GUI
!gui.in_use &&
# endif
au_has_group((char_u *)"FileExplorer"))
{
if (ffname == NULL || !mch_isdir(ffname))
ffname = (char_u *)".";
}
else
# endif
{
browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
NULL, NULL, NULL, curbuf);
if (browse_file == NULL)
goto theend;
ffname = browse_file;
}
}
#endif
if (sfname == NULL)
sfname = ffname;
#ifdef USE_FNAME_CASE
# ifdef USE_LONG_FNAME
if (USE_LONG_FNAME)
# endif
if (sfname != NULL)
fname_case(sfname, 0);
#endif
#ifdef FEAT_LISTCMDS
if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
goto theend;
#endif
if (ffname == NULL)
other_file = TRUE;
else if (*ffname == NUL && curbuf->b_ffname == NULL)
other_file = FALSE;
else
{
if (*ffname == NUL)
{
ffname = curbuf->b_ffname;
sfname = curbuf->b_fname;
}
free_fname = fix_fname(ffname);
if (free_fname != NULL)
ffname = free_fname;
other_file = otherfile(ffname);
#ifdef FEAT_SUN_WORKSHOP
if (usingSunWorkShop && p_acd
&& (cp = vim_strrchr(sfname, '/')) != NULL)
sfname = ++cp;
#endif
}
}
if ( ((!other_file && !(flags & ECMD_OLDBUF))
|| (curbuf->b_nwindows == 1
&& !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
&& check_changed(curbuf, (p_awa ? CCGD_AW : 0)
| (other_file ? 0 : CCGD_MULTWIN)
| ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
| (eap == NULL ? 0 : CCGD_EXCMD)))
{
if (fnum == 0 && other_file && ffname != NULL)
(void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum);
goto theend;
}
reset_VIsual();
#ifdef FEAT_AUTOCMD
if ((command != NULL || newlnum > (linenr_T)0)
&& *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
{
int len;
char_u *p;
if (command != NULL)
len = (int)STRLEN(command) + 3;
else
len = 30;
p = alloc((unsigned)len);
if (p != NULL)
{
if (command != NULL)
vim_snprintf((char *)p, len, ":%s\r", command);
else
vim_snprintf((char *)p, len, "%ldG", (long)newlnum);
set_vim_var_string(VV_SWAPCOMMAND, p, -1);
did_set_swapcommand = TRUE;
vim_free(p);
}
}
#endif
if (other_file)
{
#ifdef FEAT_LISTCMDS
if (!(flags & ECMD_ADDBUF))
#endif
{
if (!cmdmod.keepalt)
curwin->w_alt_fnum = curbuf->b_fnum;
if (oldwin != NULL)
buflist_altfpos(oldwin);
}
if (fnum)
buf = buflist_findnr(fnum);
else
{
#ifdef FEAT_LISTCMDS
if (flags & ECMD_ADDBUF)
{
linenr_T tlnum = 1L;
if (command != NULL)
{
tlnum = atol((char *)command);
if (tlnum <= 0)
tlnum = 1L;
}
(void)buflist_new(ffname, sfname, tlnum, BLN_LISTED);
goto theend;
}
#endif
buf = buflist_new(ffname, sfname, 0L,
BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
#ifdef FEAT_AUTOCMD
if (oldwin != NULL)
oldwin = curwin;
set_bufref(&old_curbuf, curbuf);
#endif
}
if (buf == NULL)
goto theend;
if (buf->b_ml.ml_mfp == NULL)
{
oldbuf = FALSE;
}
else
{
oldbuf = TRUE;
set_bufref(&bufref, buf);
(void)buf_check_timestamp(buf, FALSE);
if (!bufref_valid(&bufref)
#ifdef FEAT_AUTOCMD
|| curbuf != old_curbuf.br_buf
#endif
)
goto theend;
#ifdef FEAT_EVAL
if (aborting())
goto theend;
#endif
}
if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST)
{
pos = buflist_findfpos(buf);
newlnum = pos->lnum;
solcol = pos->col;
}
if (buf != curbuf)
{
#ifdef FEAT_AUTOCMD
if (buf->b_fname != NULL)
new_name = vim_strsave(buf->b_fname);
set_bufref(&au_new_curbuf, buf);
apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
if (!bufref_valid(&au_new_curbuf))
{
delbuf_msg(new_name);
goto theend;
}
# ifdef FEAT_EVAL
if (aborting())
{
vim_free(new_name);
goto theend;
}
# endif
if (buf == curbuf)
auto_buf = TRUE;
else
{
win_T *the_curwin = curwin;
the_curwin->w_closing = TRUE;
++buf->b_locked;
if (curbuf == old_curbuf.br_buf)
#endif
buf_copy_options(buf, BCO_ENTER);
u_sync(FALSE);
close_buffer(oldwin, curbuf,
(flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
#ifdef FEAT_AUTOCMD
the_curwin->w_closing = FALSE;
--buf->b_locked;
# ifdef FEAT_EVAL
if (aborting() && curwin->w_buffer != NULL)
{
vim_free(new_name);
goto theend;
}
# endif
if (!bufref_valid(&au_new_curbuf))
{
delbuf_msg(new_name);
goto theend;
}
if (buf == curbuf)
auto_buf = TRUE;
else
#endif
{
#ifdef FEAT_SYN_HL
if (curwin->w_buffer == NULL
|| curwin->w_s == &(curwin->w_buffer->b_s))
curwin->w_s = &(buf->b_s);
#endif
curwin->w_buffer = buf;
curbuf = buf;
++curbuf->b_nwindows;
if (!oldbuf && eap != NULL)
{
set_file_options(TRUE, eap);
#ifdef FEAT_MBYTE
set_forced_fenc(eap);
#endif
}
}
get_winopts(curbuf);
#ifdef FEAT_SPELL
did_get_winopts = TRUE;
#endif
#ifdef FEAT_AUTOCMD
}
vim_free(new_name);
au_new_curbuf.br_buf = NULL;
au_new_curbuf.br_buf_free_count = 0;
#endif
}
curwin->w_pcmark.lnum = 1;
curwin->w_pcmark.col = 0;
}
else
{
if (
#ifdef FEAT_LISTCMDS
(flags & ECMD_ADDBUF) ||
#endif
check_fname() == FAIL)
goto theend;
oldbuf = (flags & ECMD_OLDBUF);
}
++RedrawingDisabled;
did_inc_redrawing_disabled = TRUE;
#ifdef FEAT_AUTOCMD
buf = curbuf;
#endif
if ((flags & ECMD_SET_HELP) || keep_help_flag)
{
prepare_help_buffer();
}
else
{
if (!curbuf->b_help)
set_buflisted(TRUE);
}
#ifdef FEAT_AUTOCMD
if (buf != curbuf)
goto theend;
# ifdef FEAT_EVAL
if (aborting())
goto theend;
# endif
did_filetype = FALSE;
#endif
if (!other_file && !oldbuf)
{
set_last_cursor(curwin);
if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
{
newlnum = curwin->w_cursor.lnum;
solcol = curwin->w_cursor.col;
}
#ifdef FEAT_AUTOCMD
buf = curbuf;
if (buf->b_fname != NULL)
new_name = vim_strsave(buf->b_fname);
else
new_name = NULL;
set_bufref(&bufref, buf);
#endif
if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
{
u_sync(FALSE);
if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
== FAIL)
{
#ifdef FEAT_AUTOCMD
vim_free(new_name);
#endif
goto theend;
}
u_unchanged(curbuf);
buf_freeall(curbuf, BFA_KEEP_UNDO);
readfile_flags = READ_KEEP_UNDO;
}
else
buf_freeall(curbuf, 0);
#ifdef FEAT_AUTOCMD
if (!bufref_valid(&bufref))
{
delbuf_msg(new_name);
goto theend;
}
vim_free(new_name);
if (buf != curbuf)
goto theend;
# ifdef FEAT_EVAL
if (aborting())
goto theend;
# endif
#endif
buf_clear_file(curbuf);
curbuf->b_op_start.lnum = 0;
curbuf->b_op_end.lnum = 0;
}
retval = OK;
check_arg_idx(curwin);
#ifdef FEAT_AUTOCMD
if (!auto_buf)
#endif
{
curwin_init();
#ifdef FEAT_FOLDING
{
win_T *win;
tabpage_T *tp;
FOR_ALL_TAB_WINDOWS(tp, win)
if (win->w_buffer == curbuf)
foldUpdateAll(win);
}
#endif
DO_AUTOCHDIR
orig_pos = curwin->w_cursor;
topline = curwin->w_topline;
if (!oldbuf)
{
#if defined(HAS_SWAP_EXISTS_ACTION)
swap_exists_action = SEA_DIALOG;
#endif
curbuf->b_flags |= BF_CHECK_RO;
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
retval = FAIL;
#else
(void)open_buffer(FALSE, eap, readfile_flags);
#endif
#if defined(HAS_SWAP_EXISTS_ACTION)
if (swap_exists_action == SEA_QUIT)
retval = FAIL;
handle_swap_exists(&old_curbuf);
#endif
}
#ifdef FEAT_AUTOCMD
else
{
do_modelines(OPT_WINONLY);
apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf,
&retval);
apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
&retval);
}
check_arg_idx(curwin);
#endif
if (!EQUAL_POS(curwin->w_cursor, orig_pos))
{
newlnum = curwin->w_cursor.lnum;
newcol = curwin->w_cursor.col;
}
if (curwin->w_topline == topline)
topline = 0;
changed_line_abv_curs();
#ifdef FEAT_TITLE
maketitle();
#endif
}
#ifdef FEAT_DIFF
if (curwin->w_p_diff)
{
diff_buf_add(curbuf);
diff_invalidate(curbuf);
}
#endif
#ifdef FEAT_SPELL
if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
(void)did_set_spelllang(curwin);
#endif
if (command == NULL)
{
if (newcol >= 0)
{
curwin->w_cursor.lnum = newlnum;
curwin->w_cursor.col = newcol;
check_cursor();
}
else if (newlnum > 0)
{
curwin->w_cursor.lnum = newlnum;
check_cursor_lnum();
if (solcol >= 0 && !p_sol)
{
curwin->w_cursor.col = solcol;
check_cursor_col();
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
curwin->w_set_curswant = TRUE;
}
else
beginline(BL_SOL | BL_FIX);
}
else
{
if (exmode_active)
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
beginline(BL_WHITE | BL_FIX);
}
}
check_lnums(FALSE);
if (oldbuf
#ifdef FEAT_AUTOCMD
&& !auto_buf
#endif
)
{
int msg_scroll_save = msg_scroll;
if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
msg_scroll = FALSE;
if (!msg_scroll)
check_for_delay(FALSE);
msg_start();
msg_scroll = msg_scroll_save;
msg_scrolled_ign = TRUE;
if (!shortmess(SHM_FILEINFO))
fileinfo(FALSE, TRUE, FALSE);
msg_scrolled_ign = FALSE;
}
#ifdef FEAT_VIMINFO
curbuf->b_last_used = vim_time();
#endif
if (command != NULL)
do_cmdline(command, NULL, NULL, DOCMD_VERBOSE);
#ifdef FEAT_KEYMAP
if (curbuf->b_kmap_state & KEYMAP_INIT)
(void)keymap_init();
#endif
--RedrawingDisabled;
did_inc_redrawing_disabled = FALSE;
if (!skip_redraw)
{
n = p_so;
if (topline == 0 && command == NULL)
p_so = 999;
update_topline();
#ifdef FEAT_SCROLLBIND
curwin->w_scbind_pos = curwin->w_topline;
#endif
p_so = n;
redraw_curbuf_later(NOT_VALID);
}
if (p_im)
need_start_insertmode = TRUE;
DO_AUTOCHDIR
#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG)
if (curbuf->b_ffname != NULL)
{
# ifdef FEAT_SUN_WORKSHOP
if (gui.in_use && usingSunWorkShop)
workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro);
# endif
# ifdef FEAT_NETBEANS_INTG
if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)
netbeans_file_opened(curbuf);
# endif
}
#endif
theend:
if (did_inc_redrawing_disabled)
--RedrawingDisabled;
#ifdef FEAT_AUTOCMD
if (did_set_swapcommand)
set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
#endif
#ifdef FEAT_BROWSE
vim_free(browse_file);
#endif
vim_free(free_fname);
return retval;
}
#ifdef FEAT_AUTOCMD
static void
delbuf_msg(char_u *name)
{
EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
name == NULL ? (char_u *)"" : name);
vim_free(name);
au_new_curbuf.br_buf = NULL;
au_new_curbuf.br_buf_free_count = 0;
}
#endif
static int append_indent = 0;
void
ex_append(exarg_T *eap)
{
char_u *theline;
int did_undo = FALSE;
linenr_T lnum = eap->line2;
int indent = 0;
char_u *p;
int vcol;
int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
if (eap->forceit)
curbuf->b_p_ai = !curbuf->b_p_ai;
if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0)
append_indent = get_indent_lnum(lnum);
if (eap->cmdidx != CMD_append)
--lnum;
if (empty && lnum == 1)
lnum = 0;
State = INSERT;
if (curbuf->b_p_iminsert == B_IMODE_LMAP)
State |= LANGMAP;
for (;;)
{
msg_scroll = TRUE;
need_wait_return = FALSE;
if (curbuf->b_p_ai)
{
if (append_indent >= 0)
{
indent = append_indent;
append_indent = -1;
}
else if (lnum > 0)
indent = get_indent_lnum(lnum);
}
ex_keep_indent = FALSE;
if (eap->getline == NULL)
{
if (eap->nextcmd == NULL || *eap->nextcmd == NUL)
break;
p = vim_strchr(eap->nextcmd, NL);
if (p == NULL)
p = eap->nextcmd + STRLEN(eap->nextcmd);
theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd));
if (*p != NUL)
++p;
eap->nextcmd = p;
}
else
{
int save_State = State;
State = CMDLINE;
theline = eap->getline(
#ifdef FEAT_EVAL
eap->cstack->cs_looplevel > 0 ? -1 :
#endif
NUL, eap->cookie, indent);
State = save_State;
}
lines_left = Rows - 1;
if (theline == NULL)
break;
if (ex_keep_indent)
append_indent = indent;
vcol = 0;
for (p = theline; indent > vcol; ++p)
{
if (*p == ' ')
++vcol;
else if (*p == TAB)
vcol += 8 - vcol % 8;
else
break;
}
if ((p[0] == '.' && p[1] == NUL)
|| (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0))
== FAIL))
{
vim_free(theline);
break;
}
if (p[0] == NUL)
theline[0] = NUL;
did_undo = TRUE;
ml_append(lnum, theline, (colnr_T)0, FALSE);
appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
vim_free(theline);
++lnum;
if (empty)
{
ml_delete(2L, FALSE);
empty = FALSE;
}
}
State = NORMAL;
if (eap->forceit)
curbuf->b_p_ai = !curbuf->b_p_ai;
curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
eap->line2 + 1 : curbuf->b_ml.ml_line_count;
if (eap->cmdidx != CMD_append)
--curbuf->b_op_start.lnum;
curbuf->b_op_end.lnum = (eap->line2 < lnum)
? lnum : curbuf->b_op_start.lnum;
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
curwin->w_cursor.lnum = lnum;
check_cursor_lnum();
beginline(BL_SOL | BL_FIX);
need_wait_return = FALSE;
ex_no_reprint = TRUE;
}
void
ex_change(exarg_T *eap)
{
linenr_T lnum;
if (eap->line2 >= eap->line1
&& u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
return;
if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai)
append_indent = get_indent_lnum(eap->line1);
for (lnum = eap->line2; lnum >= eap->line1; --lnum)
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
break;
ml_delete(eap->line1, FALSE);
}
check_cursor_lnum();
deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum));
eap->line2 = eap->line1;
ex_append(eap);
}
void
ex_z(exarg_T *eap)
{
char_u *x;
long bigness;
char_u *kind;
int minus = 0;
linenr_T start, end, curs, i;
int j;
linenr_T lnum = eap->line2;
if (eap->forceit)
bigness = curwin->w_height;
else if (!ONE_WINDOW)
bigness = curwin->w_height - 3;
else
bigness = p_window_unix2003 ? p_window_unix2003 : curwin->w_p_scr * 2;
if (bigness < 1)
bigness = 1;
x = eap->arg;
kind = x;
if (*kind == '-' || *kind == '+' || *kind == '='
|| *kind == '^' || *kind == '.')
++x;
while (*x == '-' || *x == '+')
++x;
if (*x != 0)
{
if (!VIM_ISDIGIT(*x))
{
EMSG(_("E144: non-numeric argument to :z"));
return;
}
else
{
bigness = atol((char *)x);
if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0)
bigness = 2 * curbuf->b_ml.ml_line_count;
p_window = bigness;
if (*kind == '=')
bigness += 2;
}
}
if (*kind == '-' || *kind == '+')
for (x = kind + 1; *x == *kind; ++x)
;
switch (*kind)
{
case '-':
start = lnum - bigness * (linenr_T)(x - kind) + 1;
end = start + bigness - 1;
curs = end;
break;
case '=':
start = lnum - (bigness + 1) / 2 + 1;
end = lnum + (bigness + 1) / 2 - 1;
curs = lnum;
minus = 1;
break;
case '^':
start = lnum - bigness * 2;
end = lnum - bigness;
curs = lnum - bigness;
break;
case '.':
start = lnum - (bigness + 1) / 2 + 1;
end = lnum + (bigness + 1) / 2 - 1;
curs = end;
break;
default:
start = lnum;
if (*kind == '+')
start += bigness * (linenr_T)(x - kind - 1) + 1;
else if (eap->addr_count == 0)
++start;
end = start + bigness - 1;
curs = end;
break;
}
if (start < 1)
start = 1;
if (end > curbuf->b_ml.ml_line_count)
end = curbuf->b_ml.ml_line_count;
if (curs > curbuf->b_ml.ml_line_count)
curs = curbuf->b_ml.ml_line_count;
else if (curs < 1)
curs = 1;
for (i = start; i <= end; i++)
{
if (minus && i == lnum)
{
msg_putchar('\n');
for (j = 1; j < Columns; j++)
msg_putchar('-');
}
print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST);
if (minus && i == lnum)
{
msg_putchar('\n');
for (j = 1; j < Columns; j++)
msg_putchar('-');
}
}
if (curwin->w_cursor.lnum != curs)
{
curwin->w_cursor.lnum = curs;
curwin->w_cursor.col = 0;
}
ex_no_reprint = TRUE;
}
int
check_restricted(void)
{
if (restricted)
{
EMSG(_("E145: Shell commands not allowed in rvim"));
return TRUE;
}
return FALSE;
}
int
check_secure(void)
{
if (secure)
{
secure = 2;
EMSG(_(e_curdir));
return TRUE;
}
#ifdef HAVE_SANDBOX
if (sandbox != 0)
{
EMSG(_(e_sandbox));
return TRUE;
}
#endif
return FALSE;
}
static char_u *old_sub = NULL;
static int global_need_beginline;
typedef struct {
int do_all;
int do_ask;
int do_count;
int do_error;
int do_print;
int do_list;
int do_number;
int do_ic;
} subflags_T;
void
do_sub(exarg_T *eap)
{
linenr_T lnum;
long i = 0;
regmmatch_T regmatch;
static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, 0};
#ifdef FEAT_EVAL
subflags_T subflags_save;
#endif
int save_do_all;
int save_do_ask;
char_u *pat = NULL, *sub = NULL;
int delimiter;
int sublen;
int got_quit = FALSE;
int got_match = FALSE;
int temp;
int which_pat;
char_u *cmd;
int save_State;
linenr_T first_line = 0;
linenr_T last_line= 0;
linenr_T old_line_count = curbuf->b_ml.ml_line_count;
linenr_T line2;
long nmatch;
char_u *sub_firstline;
int endcolumn = FALSE;
pos_T old_cursor = curwin->w_cursor;
int start_nsubs;
#ifdef FEAT_EVAL
int save_ma = 0;
#endif
cmd = eap->arg;
if (!global_busy)
{
sub_nsubs = 0;
sub_nlines = 0;
}
start_nsubs = sub_nsubs;
if (eap->cmdidx == CMD_tilde)
which_pat = RE_LAST;
else
which_pat = RE_SUBST;
if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd)
&& vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL)
{
if (isalpha(*cmd))
{
EMSG(_("E146: Regular expressions can't be delimited by letters"));
return;
}
if (*cmd == '\\')
{
++cmd;
if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
{
EMSG(_(e_backslash));
return;
}
if (*cmd != '&')
which_pat = RE_SEARCH;
pat = (char_u *)"";
delimiter = *cmd++;
}
else
{
#ifdef FEAT_FKMAP
if (p_altkeymap && curwin->w_p_rl)
lrF_sub(cmd);
#endif
which_pat = RE_LAST;
delimiter = *cmd++;
pat = cmd;
cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
if (cmd[0] == delimiter)
*cmd++ = NUL;
}
sub = cmd;
while (cmd[0])
{
if (cmd[0] == delimiter)
{
*cmd++ = NUL;
break;
}
if (cmd[0] == '\\' && cmd[1] != 0)
++cmd;
MB_PTR_ADV(cmd);
}
if (!eap->skip)
{
if (STRCMP(sub, "%") == 0
&& vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL)
{
if (old_sub == NULL)
{
EMSG(_(e_nopresub));
return;
}
sub = old_sub;
}
else
{
vim_free(old_sub);
old_sub = vim_strsave(sub);
}
}
}
else if (!eap->skip)
{
if (old_sub == NULL)
{
EMSG(_(e_nopresub));
return;
}
pat = NULL;
sub = old_sub;
endcolumn = (curwin->w_curswant == MAXCOL);
}
if (pat != NULL && STRCMP(pat, "\\n") == 0
&& *sub == NUL
&& (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l'
|| *cmd == 'p' || *cmd == '#'))))
{
linenr_T joined_lines_count;
curwin->w_cursor.lnum = eap->line1;
if (*cmd == 'l')
eap->flags = EXFLAG_LIST;
else if (*cmd == '#')
eap->flags = EXFLAG_NR;
else if (*cmd == 'p')
eap->flags = EXFLAG_PRINT;
joined_lines_count = eap->line2 - eap->line1 + 1;
if (eap->line2 < curbuf->b_ml.ml_line_count)
++joined_lines_count;
if (joined_lines_count > 1)
{
(void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE);
sub_nsubs = joined_lines_count - 1;
sub_nlines = 1;
(void)do_sub_msg(FALSE);
ex_may_print(eap);
}
if (!cmdmod.keeppatterns)
save_re_pat(RE_SUBST, pat, p_magic);
#ifdef FEAT_CMDHIST
add_to_history(HIST_SEARCH, pat, TRUE, NUL);
#endif
return;
}
if (*cmd == '&')
++cmd;
else
{
if (!p_ed)
{
if (p_gd)
subflags.do_all = TRUE;
else
subflags.do_all = FALSE;
subflags.do_ask = FALSE;
}
subflags.do_error = TRUE;
subflags.do_print = FALSE;
subflags.do_count = FALSE;
subflags.do_number = FALSE;
subflags.do_ic = 0;
}
while (*cmd)
{
if (*cmd == 'g')
subflags.do_all = !subflags.do_all;
else if (*cmd == 'c')
subflags.do_ask = !subflags.do_ask;
else if (*cmd == 'n')
subflags.do_count = TRUE;
else if (*cmd == 'e')
subflags.do_error = !subflags.do_error;
else if (*cmd == 'r')
which_pat = RE_LAST;
else if (*cmd == 'p')
subflags.do_print = TRUE;
else if (*cmd == '#')
{
subflags.do_print = TRUE;
subflags.do_number = TRUE;
}
else if (*cmd == 'l')
{
subflags.do_print = TRUE;
subflags.do_list = TRUE;
}
else if (*cmd == 'i')
subflags.do_ic = 'i';
else if (*cmd == 'I')
subflags.do_ic = 'I';
else
break;
++cmd;
}
if (subflags.do_count)
subflags.do_ask = FALSE;
save_do_all = subflags.do_all;
save_do_ask = subflags.do_ask;
cmd = skipwhite(cmd);
if (VIM_ISDIGIT(*cmd))
{
i = getdigits(&cmd);
if (i <= 0 && !eap->skip && subflags.do_error)
{
EMSG(_(e_zerocount));
return;
}
eap->line1 = eap->line2;
eap->line2 += i - 1;
if (eap->line2 > curbuf->b_ml.ml_line_count)
eap->line2 = curbuf->b_ml.ml_line_count;
}
cmd = skipwhite(cmd);
if (*cmd && *cmd != '"')
{
eap->nextcmd = check_nextcmd(cmd);
if (eap->nextcmd == NULL)
{
EMSG(_(e_trailing));
return;
}
}
if (eap->skip)
return;
if (!subflags.do_count && !curbuf->b_p_ma)
{
EMSG(_(e_modifiable));
return;
}
if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL)
{
if (subflags.do_error)
EMSG(_(e_invcmd));
return;
}
if (subflags.do_ic == 'i')
regmatch.rmm_ic = TRUE;
else if (subflags.do_ic == 'I')
regmatch.rmm_ic = FALSE;
sub_firstline = NULL;
if (!(sub[0] == '\\' && sub[1] == '='))
sub = regtilde(sub, p_magic);
line2 = eap->line2;
for (lnum = eap->line1; lnum <= line2 && !(got_quit
#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
|| aborting()
#endif
); ++lnum)
{
nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum,
(colnr_T)0, NULL, NULL);
if (nmatch)
{
colnr_T copycol;
colnr_T matchcol;
colnr_T prev_matchcol = MAXCOL;
char_u *new_end, *new_start = NULL;
unsigned new_start_len = 0;
char_u *p1;
int did_sub = FALSE;
int lastone;
int len, copy_len, needed_len;
long nmatch_tl = 0;
int do_again;
int skip_match = FALSE;
linenr_T sub_firstlnum;
sub_firstlnum = lnum;
copycol = 0;
matchcol = 0;
if (!got_match)
{
setpcmark();
got_match = TRUE;
}
for (;;)
{
if (regmatch.startpos[0].lnum > 0)
{
lnum += regmatch.startpos[0].lnum;
sub_firstlnum += regmatch.startpos[0].lnum;
nmatch -= regmatch.startpos[0].lnum;
vim_free(sub_firstline);
sub_firstline = NULL;
}
if (sub_firstline == NULL)
{
sub_firstline = vim_strsave(ml_get(sub_firstlnum));
if (sub_firstline == NULL)
{
vim_free(new_start);
goto outofmem;
}
}
curwin->w_cursor.lnum = lnum;
do_again = FALSE;
if (matchcol == prev_matchcol
&& regmatch.endpos[0].lnum == 0
&& matchcol == regmatch.endpos[0].col)
{
if (sub_firstline[matchcol] == NUL)
skip_match = TRUE;
else
{
#ifdef FEAT_MBYTE
if (has_mbyte)
matchcol += mb_ptr2len(sub_firstline + matchcol);
else
#endif
++matchcol;
}
goto skip;
}
matchcol = regmatch.endpos[0].col;
prev_matchcol = matchcol;
if (subflags.do_count)
{
if (nmatch > 1)
{
matchcol = (colnr_T)STRLEN(sub_firstline);
nmatch = 1;
skip_match = TRUE;
}
sub_nsubs++;
did_sub = TRUE;
#ifdef FEAT_EVAL
if (!(sub[0] == '\\' && sub[1] == '='))
#endif
goto skip;
}
if (subflags.do_ask)
{
int typed = 0;
save_State = State;
State = CONFIRM;
#ifdef FEAT_MOUSE
setmouse();
#endif
curwin->w_cursor.col = regmatch.startpos[0].col;
#ifdef FEAT_CURSORBIND
if (curwin->w_p_crb)
do_check_cursorbind();
#endif
if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
++no_u_sync;
while (subflags.do_ask)
{
if (exmode_active)
{
char_u *resp;
colnr_T sc, ec;
print_line_no_prefix(lnum,
subflags.do_number, subflags.do_list);
getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
curwin->w_cursor.col = regmatch.endpos[0].col - 1;
if (curwin->w_cursor.col < 0)
curwin->w_cursor.col = 0;
getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
if (subflags.do_number || curwin->w_p_nu)
{
int numw = number_width(curwin) + 1;
sc += numw;
ec += numw;
}
msg_start();
for (i = 0; i < (long)sc; ++i)
msg_putchar(' ');
for ( ; i <= (long)ec; ++i)
msg_putchar('^');
resp = getexmodeline('?', NULL, 0);
if (resp != NULL)
{
typed = *resp;
vim_free(resp);
}
}
else
{
char_u *orig_line = NULL;
int len_change = 0;
#ifdef FEAT_FOLDING
int save_p_fen = curwin->w_p_fen;
curwin->w_p_fen = FALSE;
#endif
temp = RedrawingDisabled;
RedrawingDisabled = 0;
if (new_start != NULL)
{
orig_line = vim_strsave(ml_get(lnum));
if (orig_line != NULL)
{
char_u *new_line = concat_str(new_start,
sub_firstline + copycol);
if (new_line == NULL)
{
vim_free(orig_line);
orig_line = NULL;
}
else
{
len_change = (int)STRLEN(new_line)
- (int)STRLEN(orig_line);
curwin->w_cursor.col += len_change;
ml_replace(lnum, new_line, FALSE);
}
}
}
search_match_lines = regmatch.endpos[0].lnum
- regmatch.startpos[0].lnum;
search_match_endcol = regmatch.endpos[0].col
+ len_change;
highlight_match = TRUE;
update_topline();
validate_cursor();
update_screen(SOME_VALID);
highlight_match = FALSE;
redraw_later(SOME_VALID);
#ifdef FEAT_FOLDING
curwin->w_p_fen = save_p_fen;
#endif
if (msg_row == Rows - 1)
msg_didout = FALSE;
msg_starthere();
i = msg_scroll;
msg_scroll = 0;
msg_no_more = TRUE;
smsg_attr(HL_ATTR(HLF_R),
(char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
msg_no_more = FALSE;
msg_scroll = i;
showruler(TRUE);
windgoto(msg_row, msg_col);
RedrawingDisabled = temp;
#ifdef USE_ON_FLY_SCROLL
dont_scroll = FALSE;
#endif
++no_mapping;
++allow_keys;
typed = plain_vgetc();
--allow_keys;
--no_mapping;
msg_didout = FALSE;
msg_col = 0;
gotocmdline(TRUE);
if (orig_line != NULL)
ml_replace(lnum, orig_line, FALSE);
}
need_wait_return = FALSE;
if (typed == 'q' || typed == ESC || typed == Ctrl_C
#ifdef UNIX
|| typed == intr_char
#endif
)
{
got_quit = TRUE;
break;
}
if (typed == 'n')
break;
if (typed == 'y')
break;
if (typed == 'l')
{
subflags.do_all = FALSE;
line2 = lnum;
break;
}
if (typed == 'a')
{
subflags.do_ask = FALSE;
break;
}
#ifdef FEAT_INS_EXPAND
if (typed == Ctrl_E)
scrollup_clamp();
else if (typed == Ctrl_Y)
scrolldown_clamp();
#endif
}
State = save_State;
#ifdef FEAT_MOUSE
setmouse();
#endif
if (vim_strchr(p_cpo, CPO_UNDO) != NULL)
--no_u_sync;
if (typed == 'n')
{
if (nmatch > 1)
{
matchcol = (colnr_T)STRLEN(sub_firstline);
skip_match = TRUE;
}
goto skip;
}
if (got_quit)
goto skip;
}
curwin->w_cursor.col = regmatch.startpos[0].col;
#ifdef FEAT_EVAL
if (subflags.do_count)
{
save_ma = curbuf->b_p_ma;
curbuf->b_p_ma = FALSE;
sandbox++;
}
subflags_save = subflags;
#endif
sublen = vim_regsub_multi(®match,
sub_firstlnum - regmatch.startpos[0].lnum,
sub, sub_firstline, FALSE, p_magic, TRUE);
#ifdef FEAT_EVAL
subflags = subflags_save;
if (subflags.do_count)
{
curbuf->b_p_ma = save_ma;
if (sandbox > 0)
sandbox--;
goto skip;
}
#endif
if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1)
{
nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
skip_match = TRUE;
}
if (nmatch == 1)
p1 = sub_firstline;
else
{
p1 = ml_get(sub_firstlnum + nmatch - 1);
nmatch_tl += nmatch - 1;
}
copy_len = regmatch.startpos[0].col - copycol;
needed_len = copy_len + ((unsigned)STRLEN(p1)
- regmatch.endpos[0].col) + sublen + 1;
if (new_start == NULL)
{
new_start_len = needed_len + 50;
if ((new_start = alloc_check(new_start_len)) == NULL)
goto outofmem;
*new_start = NUL;
new_end = new_start;
}
else
{
len = (unsigned)STRLEN(new_start);
needed_len += len;
if (needed_len > (int)new_start_len)
{
new_start_len = needed_len + 50;
if ((p1 = alloc_check(new_start_len)) == NULL)
{
vim_free(new_start);
goto outofmem;
}
mch_memmove(p1, new_start, (size_t)(len + 1));
vim_free(new_start);
new_start = p1;
}
new_end = new_start + len;
}
mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
new_end += copy_len;
(void)vim_regsub_multi(®match,
sub_firstlnum - regmatch.startpos[0].lnum,
sub, new_end, TRUE, p_magic, TRUE);
sub_nsubs++;
did_sub = TRUE;
curwin->w_cursor.col = 0;
if (nmatch > 1)
{
sub_firstlnum += nmatch - 1;
vim_free(sub_firstline);
sub_firstline = vim_strsave(ml_get(sub_firstlnum));
if (sub_firstlnum <= line2)
do_again = TRUE;
else
subflags.do_all = FALSE;
}
copycol = regmatch.endpos[0].col;
if (skip_match)
{
vim_free(sub_firstline);
sub_firstline = vim_strsave((char_u *)"");
copycol = 0;
}
for (p1 = new_end; *p1; ++p1)
{
if (p1[0] == '\\' && p1[1] != NUL)
STRMOVE(p1, p1 + 1);
else if (*p1 == CAR)
{
if (u_inssub(lnum) == OK)
{
*p1 = NUL;
ml_append(lnum - 1, new_start,
(colnr_T)(p1 - new_start + 1), FALSE);
mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
if (subflags.do_ask)
appended_lines(lnum - 1, 1L);
else
{
if (first_line == 0)
first_line = lnum;
last_line = lnum + 1;
}
++sub_firstlnum;
++lnum;
++line2;
++curwin->w_cursor.lnum;
STRMOVE(new_start, p1 + 1);
p1 = new_start - 1;
}
}
#ifdef FEAT_MBYTE
else if (has_mbyte)
p1 += (*mb_ptr2len)(p1) - 1;
#endif
}
skip:
lastone = (skip_match
|| got_int
|| got_quit
|| lnum > line2
|| !(subflags.do_all || do_again)
|| (sub_firstline[matchcol] == NUL && nmatch <= 1
&& !re_multiline(regmatch.regprog)));
nmatch = -1;
if (lastone
|| nmatch_tl > 0
|| (nmatch = vim_regexec_multi(®match, curwin,
curbuf, sub_firstlnum,
matchcol, NULL, NULL)) == 0
|| regmatch.startpos[0].lnum > 0)
{
if (new_start != NULL)
{
STRCAT(new_start, sub_firstline + copycol);
matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
prev_matchcol = (colnr_T)STRLEN(sub_firstline)
- prev_matchcol;
if (Unix2003_compat)
beginline(BL_WHITE | BL_FIX);
if (u_savesub(lnum) != OK)
break;
ml_replace(lnum, new_start, TRUE);
if (nmatch_tl > 0)
{
++lnum;
if (u_savedel(lnum, nmatch_tl) != OK)
break;
for (i = 0; i < nmatch_tl; ++i)
ml_delete(lnum, (int)FALSE);
mark_adjust(lnum, lnum + nmatch_tl - 1,
(long)MAXLNUM, -nmatch_tl);
if (subflags.do_ask)
deleted_lines(lnum, nmatch_tl);
--lnum;
line2 -= nmatch_tl;
nmatch_tl = 0;
}
if (subflags.do_ask)
changed_bytes(lnum, 0);
else
{
if (first_line == 0)
first_line = lnum;
last_line = lnum + 1;
}
sub_firstlnum = lnum;
vim_free(sub_firstline);
sub_firstline = new_start;
new_start = NULL;
matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol;
prev_matchcol = (colnr_T)STRLEN(sub_firstline)
- prev_matchcol;
copycol = 0;
}
if (nmatch == -1 && !lastone)
nmatch = vim_regexec_multi(®match, curwin, curbuf,
sub_firstlnum, matchcol, NULL, NULL);
if (nmatch <= 0)
{
if (nmatch == -1)
lnum -= regmatch.startpos[0].lnum;
break;
}
}
line_breakcheck();
}
if (did_sub)
++sub_nlines;
vim_free(new_start);
vim_free(sub_firstline);
sub_firstline = NULL;
}
line_breakcheck();
}
if (first_line != 0)
{
i = curbuf->b_ml.ml_line_count - old_line_count;
changed_lines(first_line, 0, last_line - i, i);
}
outofmem:
vim_free(sub_firstline);
if (subflags.do_count)
curwin->w_cursor = old_cursor;
if (sub_nsubs > start_nsubs)
{
curbuf->b_op_start.lnum = eap->line1;
curbuf->b_op_end.lnum = line2;
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
if (!global_busy)
{
if (!subflags.do_ask)
{
if (endcolumn)
coladvance((colnr_T)MAXCOL);
else
beginline(BL_WHITE | BL_FIX);
}
if (!do_sub_msg(subflags.do_count) && subflags.do_ask)
MSG("");
}
else
global_need_beginline = TRUE;
if (subflags.do_print)
print_line(curwin->w_cursor.lnum,
subflags.do_number, subflags.do_list);
}
else if (!global_busy)
{
if (got_int)
EMSG(_(e_interr));
else if (got_match)
MSG("");
else if (subflags.do_error)
EMSG2(_(e_patnotf2), get_search_pat());
}
#ifdef FEAT_FOLDING
if (subflags.do_ask && hasAnyFolding(curwin))
changed_window_setting();
#endif
vim_regfree(regmatch.regprog);
subflags.do_all = save_do_all;
subflags.do_ask = save_do_ask;
}
int
do_sub_msg(
int count_only)
{
if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1))
|| count_only)
&& messaging())
{
if (got_int)
STRCPY(msg_buf, _("(Interrupted) "));
else
*msg_buf = NUL;
if (sub_nsubs == 1)
vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
"%s", count_only ? _("1 match") : _("1 substitution"));
else
vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
count_only ? _("%ld matches") : _("%ld substitutions"),
sub_nsubs);
if (sub_nlines == 1)
vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
"%s", _(" on 1 line"));
else
vim_snprintf_add((char *)msg_buf, sizeof(msg_buf),
_(" on %ld lines"), (long)sub_nlines);
if (msg(msg_buf))
set_keep_msg(msg_buf, 0);
return TRUE;
}
if (got_int)
{
EMSG(_(e_interr));
return TRUE;
}
return FALSE;
}
static void
global_exe_one(char_u *cmd, linenr_T lnum)
{
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = 0;
if (*cmd == NUL || *cmd == '\n')
do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
else
do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
}
void
ex_global(exarg_T *eap)
{
linenr_T lnum;
int ndone = 0;
int type;
char_u *cmd;
char_u delim;
char_u *pat;
regmmatch_T regmatch;
int match;
int which_pat;
if (global_busy && (eap->line1 != 1
|| eap->line2 != curbuf->b_ml.ml_line_count))
{
EMSG(_("E147: Cannot do :global recursive with a range"));
return;
}
if (eap->forceit)
type = 'v';
else
type = *eap->cmd;
cmd = eap->arg;
which_pat = RE_LAST;
if (*cmd == '\\')
{
++cmd;
if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
{
EMSG(_(e_backslash));
return;
}
if (*cmd == '&')
which_pat = RE_SUBST;
else
which_pat = RE_SEARCH;
++cmd;
pat = (char_u *)"";
}
else if (*cmd == NUL)
{
EMSG(_("E148: Regular expression missing from global"));
return;
}
else
{
delim = *cmd;
if (delim)
++cmd;
pat = cmd;
cmd = skip_regexp(cmd, delim, p_magic, &eap->arg);
if (cmd[0] == delim)
*cmd++ = NUL;
}
#ifdef FEAT_FKMAP
if (p_altkeymap && curwin->w_p_rl)
lrFswap(pat,0);
#endif
if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL)
{
EMSG(_(e_invcmd));
return;
}
if (global_busy)
{
lnum = curwin->w_cursor.lnum;
match = vim_regexec_multi(®match, curwin, curbuf, lnum,
(colnr_T)0, NULL, NULL);
if ((type == 'g' && match) || (type == 'v' && !match))
global_exe_one(cmd, lnum);
}
else
{
for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
{
match = vim_regexec_multi(®match, curwin, curbuf, lnum,
(colnr_T)0, NULL, NULL);
if ((type == 'g' && match) || (type == 'v' && !match))
{
ml_setmarked(lnum);
ndone++;
}
line_breakcheck();
}
if (got_int)
MSG(_(e_interr));
else if (ndone == 0)
{
if (type == 'v')
smsg((char_u *)_("Pattern found in every line: %s"), pat);
else
smsg((char_u *)_("Pattern not found: %s"), pat);
}
else
{
#ifdef FEAT_CLIPBOARD
start_global_changes();
#endif
global_exe(cmd);
#ifdef FEAT_CLIPBOARD
end_global_changes();
#endif
}
ml_clearmarked();
}
vim_regfree(regmatch.regprog);
}
void
global_exe(char_u *cmd)
{
linenr_T old_lcount;
buf_T *old_buf = curbuf;
linenr_T lnum;
setpcmark();
msg_didout = TRUE;
sub_nsubs = 0;
sub_nlines = 0;
global_need_beginline = FALSE;
global_busy = 1;
old_lcount = curbuf->b_ml.ml_line_count;
while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
{
global_exe_one(cmd, lnum);
ui_breakcheck();
}
global_busy = 0;
if (global_need_beginline)
beginline(BL_WHITE | BL_FIX);
else
check_cursor();
changed_line_abv_curs();
if (msg_col == 0 && msg_scrolled == 0)
msg_didout = FALSE;
if (!do_sub_msg(FALSE) && curbuf == old_buf)
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
}
#ifdef FEAT_VIMINFO
int
read_viminfo_sub_string(vir_T *virp, int force)
{
if (force)
vim_free(old_sub);
if (force || old_sub == NULL)
old_sub = viminfo_readstring(virp, 1, TRUE);
return viminfo_readline(virp);
}
void
write_viminfo_sub_string(FILE *fp)
{
if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
{
fputs(_("\n# Last Substitute String:\n$"), fp);
viminfo_writestring(fp, old_sub);
}
}
#endif
#if defined(EXITFREE) || defined(PROTO)
void
free_old_sub(void)
{
vim_free(old_sub);
}
#endif
#if defined(FEAT_QUICKFIX) || defined(PROTO)
int
prepare_tagpreview(
int undo_sync)
{
win_T *wp;
# ifdef FEAT_GUI
need_mouse_correct = TRUE;
# endif
if (!curwin->w_p_pvw)
{
FOR_ALL_WINDOWS(wp)
if (wp->w_p_pvw)
break;
if (wp != NULL)
win_enter(wp, undo_sync);
else
{
if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0)
== FAIL)
return FALSE;
curwin->w_p_pvw = TRUE;
curwin->w_p_wfh = TRUE;
RESET_BINDING(curwin);
# ifdef FEAT_DIFF
curwin->w_p_diff = FALSE;
# endif
# ifdef FEAT_FOLDING
curwin->w_p_fdc = 0;
# endif
return TRUE;
}
}
return FALSE;
}
#endif
void
ex_help(exarg_T *eap)
{
char_u *arg;
char_u *tag;
FILE *helpfd;
int n;
int i;
win_T *wp;
int num_matches;
char_u **matches;
char_u *p;
int empty_fnum = 0;
int alt_fnum = 0;
buf_T *buf;
#ifdef FEAT_MULTI_LANG
int len;
char_u *lang;
#endif
#ifdef FEAT_FOLDING
int old_KeyTyped = KeyTyped;
#endif
if (eap != NULL)
{
for (arg = eap->arg; *arg; ++arg)
{
if (*arg == '\n' || *arg == '\r'
|| (*arg == '|' && arg[1] != NUL && arg[1] != '|'))
{
*arg++ = NUL;
eap->nextcmd = arg;
break;
}
}
arg = eap->arg;
if (eap->forceit && *arg == NUL && !curbuf->b_help)
{
EMSG(_("E478: Don't panic!"));
return;
}
if (eap->skip)
return;
}
else
arg = (char_u *)"";
p = arg + STRLEN(arg) - 1;
while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\')
*p-- = NUL;
#ifdef FEAT_MULTI_LANG
lang = check_help_lang(arg);
#endif
if (*arg == NUL)
arg = (char_u *)"help.txt";
n = find_help_tags(arg, &num_matches, &matches,
eap != NULL && eap->forceit);
i = 0;
#ifdef FEAT_MULTI_LANG
if (n != FAIL && lang != NULL)
for (i = 0; i < num_matches; ++i)
{
len = (int)STRLEN(matches[i]);
if (len > 3 && matches[i][len - 3] == '@'
&& STRICMP(matches[i] + len - 2, lang) == 0)
break;
}
#endif
if (i >= num_matches || n == FAIL)
{
#ifdef FEAT_MULTI_LANG
if (lang != NULL)
EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg);
else
#endif
EMSG2(_("E149: Sorry, no help for %s"), arg);
if (n != FAIL)
FreeWild(num_matches, matches);
return;
}
tag = vim_strsave(matches[i]);
FreeWild(num_matches, matches);
#ifdef FEAT_GUI
need_mouse_correct = TRUE;
#endif
if (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)
{
if (cmdmod.tab != 0)
wp = NULL;
else
FOR_ALL_WINDOWS(wp)
if (bt_help(wp->w_buffer))
break;
if (wp != NULL && wp->w_buffer->b_nwindows > 0)
win_enter(wp, TRUE);
else
{
if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL)
{
smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf);
goto erret;
}
fclose(helpfd);
n = WSP_HELP;
if (cmdmod.split == 0 && curwin->w_width != Columns
&& curwin->w_width < 80)
n |= WSP_TOP;
if (win_split(0, n) == FAIL)
goto erret;
if (curwin->w_height < p_hh)
win_setheight((int)p_hh);
alt_fnum = curbuf->b_fnum;
(void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL,
ECMD_HIDE + ECMD_SET_HELP,
NULL);
if (!cmdmod.keepalt)
curwin->w_alt_fnum = alt_fnum;
empty_fnum = curbuf->b_fnum;
}
}
if (!p_im)
restart_edit = 0;
#ifdef FEAT_FOLDING
KeyTyped = old_KeyTyped;
#endif
if (tag != NULL)
do_tag(tag, DT_HELP, 1, FALSE, TRUE);
if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum)
{
buf = buflist_findnr(empty_fnum);
if (buf != NULL && buf->b_nwindows == 0)
wipe_buffer(buf, TRUE);
}
if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt)
curwin->w_alt_fnum = alt_fnum;
erret:
vim_free(tag);
}
void
ex_helpclose(exarg_T *eap UNUSED)
{
win_T *win;
FOR_ALL_WINDOWS(win)
{
if (bt_help(win->w_buffer))
{
win_close(win, FALSE);
return;
}
}
}
#if defined(FEAT_MULTI_LANG) || defined(PROTO)
char_u *
check_help_lang(char_u *arg)
{
int len = (int)STRLEN(arg);
if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2])
&& ASCII_ISALPHA(arg[len - 1]))
{
arg[len - 3] = NUL;
return arg + len - 2;
}
return NULL;
}
#endif
int
help_heuristic(
char_u *matched_string,
int offset,
int wrong_case)
{
int num_letters;
char_u *p;
num_letters = 0;
for (p = matched_string; *p; p++)
if (ASCII_ISALNUM(*p))
num_letters++;
if (ASCII_ISALNUM(matched_string[offset]) && offset > 0
&& ASCII_ISALNUM(matched_string[offset - 1]))
offset += 10000;
else if (offset > 2)
offset *= 200;
if (wrong_case)
offset += 5000;
if (matched_string[0] == '+' && matched_string[1] != NUL)
offset += 100;
return (int)(100 * num_letters + STRLEN(matched_string) + offset);
}
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
help_compare(const void *s1, const void *s2)
{
char *p1;
char *p2;
p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
return strcmp(p1, p2);
}
int
find_help_tags(
char_u *arg,
int *num_matches,
char_u ***matches,
int keep_lang)
{
char_u *s, *d;
int i;
static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*",
"/*", "/\\*", "\"*", "**",
"cpo-*", "/\\(\\)", "/\\%(\\)",
"?", ":?", "?<CR>", "g?", "g?g?", "g??",
"/\\?", "/\\z(\\)", "\\=", ":s\\=",
"[count]", "[quotex]",
"[range]", ":[range]",
"[pattern]", "\\|", "\\%$",
"s/\\~", "s/\\U", "s/\\L",
"s/\\1", "s/\\2", "s/\\3", "s/\\9"};
static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star",
"/star", "/\\\\star", "quotestar", "starstar",
"cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)",
"?", ":?", "?<CR>", "g?", "g?g?", "g??",
"/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=",
"\\[count]", "\\[quotex]",
"\\[range]", ":\\[range]",
"\\[pattern]", "\\\\bar", "/\\\\%\\$",
"s/\\\\\\~", "s/\\\\U", "s/\\\\L",
"s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"};
int flags;
d = IObuff;
for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
if (STRCMP(arg, mtable[i]) == 0)
{
STRCPY(d, rtable[i]);
break;
}
if (i < 0)
{
if (arg[0] == '\\'
&& ((arg[1] != NUL && arg[2] == NUL)
|| (vim_strchr((char_u *)"%_z@", arg[1]) != NULL
&& arg[2] != NUL)))
{
STRCPY(d, "/\\\\");
STRCPY(d + 3, arg + 1);
if (d[3] == '_' && d[4] == '$')
STRCPY(d + 4, "\\$");
}
else
{
if ((arg[0] == '[' && (arg[1] == ':'
|| (arg[1] == '+' && arg[2] == '+')))
|| (arg[0] == '\\' && arg[1] == '{'))
*d++ = '\\';
if (*arg == '(' && arg[1] == '\'')
arg++;
for (s = arg; *s; ++s)
{
if (d - IObuff > IOSIZE - 10)
break;
switch (*s)
{
case '|': STRCPY(d, "bar");
d += 3;
continue;
case '"': STRCPY(d, "quote");
d += 5;
continue;
case '*': *d++ = '.';
break;
case '?': *d++ = '.';
continue;
case '$':
case '.':
case '~': *d++ = '\\';
break;
}
if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1])
|| vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL)))
{
if (d > IObuff && d[-1] != '_' && d[-1] != '\\')
*d++ = '_';
STRCPY(d, "CTRL-");
d += 5;
if (*s < ' ')
{
#ifdef EBCDIC
*d++ = CtrlChar(*s);
#else
*d++ = *s + '@';
#endif
if (d[-1] == '\\')
*d++ = '\\';
}
else
*d++ = *++s;
if (s[1] != NUL && s[1] != '_')
*d++ = '_';
continue;
}
else if (*s == '^')
*d++ = '\\';
else if (s[0] == '\\' && s[1] != '\\'
&& *arg == '/' && s == arg + 1)
*d++ = '\\';
if (STRNICMP(s, "CTRL-\\_", 7) == 0)
{
STRCPY(d, "CTRL-\\\\");
d += 7;
s += 6;
}
*d++ = *s;
if (*s == '(' && (s[1] == '{' || s[1] =='['))
break;
if (*s == '\'' && s > arg && *arg == '\'')
break;
if (*s == '}' && s > arg && *arg == '{')
break;
}
*d = NUL;
if (*IObuff == '`')
{
if (d > IObuff + 2 && d[-1] == '`')
{
mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
d[-2] = NUL;
}
else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',')
{
mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
d[-3] = NUL;
}
else if (d > IObuff + 4 && d[-3] == '`'
&& d[-2] == '\\' && d[-1] == '.')
{
mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff));
d[-4] = NUL;
}
}
}
}
*matches = (char_u **)"";
*num_matches = 0;
flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE;
if (keep_lang)
flags |= TAG_KEEP_LANG;
if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK
&& *num_matches > 0)
{
qsort((void *)*matches, (size_t)*num_matches,
sizeof(char_u *), help_compare);
while (*num_matches > TAG_MANY)
vim_free((*matches)[--*num_matches]);
}
return OK;
}
static void
prepare_help_buffer(void)
{
char_u *p;
curbuf->b_help = TRUE;
#ifdef FEAT_QUICKFIX
set_string_option_direct((char_u *)"buftype", -1,
(char_u *)"help", OPT_FREE|OPT_LOCAL, 0);
#endif
p =
#ifdef EBCDIC
(char_u *)"65-255,^*,^|,^\"";
#else
(char_u *)"!-~,^*,^|,^\",192-255";
#endif
if (STRCMP(curbuf->b_p_isk, p) != 0)
{
set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0);
check_buf_options(curbuf);
(void)buf_init_chartab(curbuf, FALSE);
}
#ifdef FEAT_FOLDING
set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual",
OPT_FREE|OPT_LOCAL, 0);
#endif
curbuf->b_p_ts = 8;
curwin->w_p_list = FALSE;
curbuf->b_p_ma = FALSE;
curbuf->b_p_bin = FALSE;
curwin->w_p_nu = 0;
curwin->w_p_rnu = 0;
RESET_BINDING(curwin);
#ifdef FEAT_ARABIC
curwin->w_p_arab = FALSE;
#endif
#ifdef FEAT_RIGHTLEFT
curwin->w_p_rl = FALSE;
#endif
#ifdef FEAT_FOLDING
curwin->w_p_fen = FALSE;
#endif
#ifdef FEAT_DIFF
curwin->w_p_diff = FALSE;
#endif
#ifdef FEAT_SPELL
curwin->w_p_spell = FALSE;
#endif
set_buflisted(FALSE);
}
void
fix_help_buffer(void)
{
linenr_T lnum;
char_u *line;
int in_example = FALSE;
int len;
char_u *fname;
char_u *p;
char_u *rt;
int mustfree;
#ifdef FEAT_AUTOCMD
if (STRCMP(curbuf->b_p_ft, "help") != 0)
{
++curbuf_lock;
set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
--curbuf_lock;
}
#endif
#ifdef FEAT_SYN_HL
if (!syntax_present(curwin))
#endif
{
for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
{
line = ml_get_buf(curbuf, lnum, FALSE);
len = (int)STRLEN(line);
if (in_example && len > 0 && !VIM_ISWHITE(line[0]))
{
if (line[0] == '<')
{
line = ml_get_buf(curbuf, lnum, TRUE);
line[0] = ' ';
}
in_example = FALSE;
}
if (!in_example && len > 0)
{
if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' '))
{
line = ml_get_buf(curbuf, lnum, TRUE);
line[len - 1] = ' ';
in_example = TRUE;
}
else if (line[len - 1] == '~')
{
line = ml_get_buf(curbuf, lnum, TRUE);
line[len - 1] = ' ';
}
}
}
}
fname = gettail(curbuf->b_fname);
if (fnamecmp(fname, "help.txt") == 0
#ifdef FEAT_MULTI_LANG
|| (fnamencmp(fname, "help.", 5) == 0
&& ASCII_ISALPHA(fname[5])
&& ASCII_ISALPHA(fname[6])
&& TOLOWER_ASC(fname[7]) == 'x'
&& fname[8] == NUL)
#endif
)
{
for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
{
line = ml_get_buf(curbuf, lnum, FALSE);
if (strstr((char *)line, "*local-additions*") == NULL)
continue;
p = p_rtp;
while (*p != NUL)
{
copy_option_part(&p, NameBuff, MAXPATHL, ",");
mustfree = FALSE;
rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME)
{
int fcount;
char_u **fnames;
FILE *fd;
char_u *s;
int fi;
#ifdef FEAT_MBYTE
vimconv_T vc;
char_u *cp;
#endif
add_pathsep(NameBuff);
#ifdef FEAT_MULTI_LANG
STRCAT(NameBuff, "doc/*.??[tx]");
#else
STRCAT(NameBuff, "doc/*.txt");
#endif
if (gen_expand_wildcards(1, &NameBuff, &fcount,
&fnames, EW_FILE|EW_SILENT) == OK
&& fcount > 0)
{
#ifdef FEAT_MULTI_LANG
int i1;
int i2;
char_u *f1;
char_u *f2;
char_u *t1;
char_u *e1;
char_u *e2;
for (i1 = 0; i1 < fcount; ++i1)
{
for (i2 = 0; i2 < fcount; ++i2)
{
if (i1 == i2)
continue;
if (fnames[i1] == NULL || fnames[i2] == NULL)
continue;
f1 = fnames[i1];
f2 = fnames[i2];
t1 = gettail(f1);
if (fnamencmp(f1, f2, t1 - f1) != 0)
continue;
e1 = vim_strrchr(t1, '.');
e2 = vim_strrchr(gettail(f2), '.');
if (e1 == NULL || e2 == NULL)
continue;
if (fnamecmp(e1, ".txt") != 0
&& fnamecmp(e1, fname + 4) != 0)
{
vim_free(fnames[i1]);
fnames[i1] = NULL;
continue;
}
if (fnamencmp(f1, f2, e1 - f1) != 0)
continue;
if (fnamecmp(e1, ".txt") == 0
&& fnamecmp(e2, fname + 4) == 0)
{
vim_free(fnames[i1]);
fnames[i1] = NULL;
}
}
}
#endif
for (fi = 0; fi < fcount; ++fi)
{
if (fnames[fi] == NULL)
continue;
fd = mch_fopen((char *)fnames[fi], "r");
if (fd != NULL)
{
vim_fgets(IObuff, IOSIZE, fd);
if (IObuff[0] == '*'
&& (s = vim_strchr(IObuff + 1, '*'))
!= NULL)
{
#ifdef FEAT_MBYTE
int this_utf = MAYBE;
#endif
IObuff[0] = '|';
*s = '|';
while (*s != NUL)
{
if (*s == '\r' || *s == '\n')
*s = NUL;
#ifdef FEAT_MBYTE
if (*s >= 0x80 && this_utf != FALSE)
{
int l;
this_utf = TRUE;
l = utf_ptr2len(s);
if (l == 1)
this_utf = FALSE;
s += l - 1;
}
#endif
++s;
}
#ifdef FEAT_MBYTE
vc.vc_type = CONV_NONE;
convert_setup(&vc, (char_u *)(
this_utf == TRUE ? "utf-8"
: "latin1"), p_enc);
if (vc.vc_type == CONV_NONE)
cp = IObuff;
else
{
cp = string_convert(&vc, IObuff,
NULL);
if (cp == NULL)
cp = IObuff;
}
convert_setup(&vc, NULL, NULL);
ml_append(lnum, cp, (colnr_T)0, FALSE);
if (cp != IObuff)
vim_free(cp);
#else
ml_append(lnum, IObuff, (colnr_T)0,
FALSE);
#endif
++lnum;
}
fclose(fd);
}
}
FreeWild(fcount, fnames);
}
}
if (mustfree)
vim_free(rt);
}
break;
}
}
}
void
ex_exusage(exarg_T *eap UNUSED)
{
do_cmdline_cmd((char_u *)"help ex-cmd-index");
}
void
ex_viusage(exarg_T *eap UNUSED)
{
do_cmdline_cmd((char_u *)"help normal-index");
}
static void
helptags_one(
char_u *dir,
char_u *ext,
char_u *tagfname,
int add_help_tags)
{
FILE *fd_tags;
FILE *fd;
garray_T ga;
int filecount;
char_u **files;
char_u *p1, *p2;
int fi;
char_u *s;
int i;
char_u *fname;
int dirlen;
# ifdef FEAT_MBYTE
int utf8 = MAYBE;
int this_utf8;
int firstline;
int mix = FALSE;
# endif
dirlen = (int)STRLEN(dir);
STRCPY(NameBuff, dir);
STRCAT(NameBuff, "/**/*");
STRCAT(NameBuff, ext);
if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
EW_FILE|EW_SILENT) == FAIL
|| filecount == 0)
{
if (!got_int)
EMSG2(_("E151: No match: %s"), NameBuff);
return;
}
STRCPY(NameBuff, dir);
add_pathsep(NameBuff);
STRCAT(NameBuff, tagfname);
fd_tags = mch_fopen((char *)NameBuff, "w");
if (fd_tags == NULL)
{
EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
FreeWild(filecount, files);
return;
}
ga_init2(&ga, (int)sizeof(char_u *), 100);
if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc",
dir, FALSE) == FPC_SAME)
{
if (ga_grow(&ga, 1) == FAIL)
got_int = TRUE;
else
{
s = alloc(18 + (unsigned)STRLEN(tagfname));
if (s == NULL)
got_int = TRUE;
else
{
sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
((char_u **)ga.ga_data)[ga.ga_len] = s;
++ga.ga_len;
}
}
}
for (fi = 0; fi < filecount && !got_int; ++fi)
{
fd = mch_fopen((char *)files[fi], "r");
if (fd == NULL)
{
EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
continue;
}
fname = files[fi] + dirlen + 1;
# ifdef FEAT_MBYTE
firstline = TRUE;
# endif
while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
{
# ifdef FEAT_MBYTE
if (firstline)
{
this_utf8 = MAYBE;
for (s = IObuff; *s != NUL; ++s)
if (*s >= 0x80)
{
int l;
this_utf8 = TRUE;
l = utf_ptr2len(s);
if (l == 1)
{
this_utf8 = FALSE;
break;
}
s += l - 1;
}
if (this_utf8 == MAYBE)
this_utf8 = FALSE;
if (utf8 == MAYBE)
utf8 = this_utf8;
else if (utf8 != this_utf8)
{
EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]);
mix = !got_int;
got_int = TRUE;
}
firstline = FALSE;
}
# endif
p1 = vim_strchr(IObuff, '*');
while (p1 != NULL)
{
p2 = vim_strbyte(p1 + 1, '*');
if (p2 != NULL && p2 > p1 + 1)
{
for (s = p1 + 1; s < p2; ++s)
if (*s == ' ' || *s == '\t' || *s == '|')
break;
if (s == p2
&& (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t')
&& (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL
|| s[1] == '\0'))
{
*p2 = '\0';
++p1;
if (ga_grow(&ga, 1) == FAIL)
{
got_int = TRUE;
break;
}
s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2));
if (s == NULL)
{
got_int = TRUE;
break;
}
((char_u **)ga.ga_data)[ga.ga_len] = s;
++ga.ga_len;
sprintf((char *)s, "%s\t%s", p1, fname);
p2 = vim_strchr(p2 + 1, '*');
}
}
p1 = p2;
}
line_breakcheck();
}
fclose(fd);
}
FreeWild(filecount, files);
if (!got_int)
{
if (ga.ga_data != NULL)
sort_strings((char_u **)ga.ga_data, ga.ga_len);
for (i = 1; i < ga.ga_len; ++i)
{
p1 = ((char_u **)ga.ga_data)[i - 1];
p2 = ((char_u **)ga.ga_data)[i];
while (*p1 == *p2)
{
if (*p2 == '\t')
{
*p2 = NUL;
vim_snprintf((char *)NameBuff, MAXPATHL,
_("E154: Duplicate tag \"%s\" in file %s/%s"),
((char_u **)ga.ga_data)[i], dir, p2 + 1);
EMSG(NameBuff);
*p2 = '\t';
break;
}
++p1;
++p2;
}
}
# ifdef FEAT_MBYTE
if (utf8 == TRUE)
fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
# endif
for (i = 0; i < ga.ga_len; ++i)
{
s = ((char_u **)ga.ga_data)[i];
if (STRNCMP(s, "help-tags\t", 10) == 0)
fputs((char *)s, fd_tags);
else
{
fprintf(fd_tags, "%s\t/*", s);
for (p1 = s; *p1 != '\t'; ++p1)
{
if (*p1 == '\\' || *p1 == '/')
putc('\\', fd_tags);
putc(*p1, fd_tags);
}
fprintf(fd_tags, "*\n");
}
}
}
#ifdef FEAT_MBYTE
if (mix)
got_int = FALSE;
#endif
for (i = 0; i < ga.ga_len; ++i)
vim_free(((char_u **)ga.ga_data)[i]);
ga_clear(&ga);
fclose(fd_tags);
}
static void
do_helptags(char_u *dirname, int add_help_tags)
{
#ifdef FEAT_MULTI_LANG
int len;
int i, j;
garray_T ga;
char_u lang[2];
char_u ext[5];
char_u fname[8];
int filecount;
char_u **files;
STRCPY(NameBuff, dirname);
add_pathsep(NameBuff);
STRCAT(NameBuff, "**");
if (gen_expand_wildcards(1, &NameBuff, &filecount, &files,
EW_FILE|EW_SILENT) == FAIL
|| filecount == 0)
{
EMSG2(_("E151: No match: %s"), NameBuff);
return;
}
ga_init2(&ga, 1, 10);
for (i = 0; i < filecount; ++i)
{
len = (int)STRLEN(files[i]);
if (len > 4)
{
if (STRICMP(files[i] + len - 4, ".txt") == 0)
{
lang[0] = 'e';
lang[1] = 'n';
}
else if (files[i][len - 4] == '.'
&& ASCII_ISALPHA(files[i][len - 3])
&& ASCII_ISALPHA(files[i][len - 2])
&& TOLOWER_ASC(files[i][len - 1]) == 'x')
{
lang[0] = TOLOWER_ASC(files[i][len - 3]);
lang[1] = TOLOWER_ASC(files[i][len - 2]);
}
else
continue;
for (j = 0; j < ga.ga_len; j += 2)
if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
break;
if (j == ga.ga_len)
{
if (ga_grow(&ga, 2) == FAIL)
break;
((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
}
}
}
for (j = 0; j < ga.ga_len; j += 2)
{
STRCPY(fname, "tags-xx");
fname[5] = ((char_u *)ga.ga_data)[j];
fname[6] = ((char_u *)ga.ga_data)[j + 1];
if (fname[5] == 'e' && fname[6] == 'n')
{
fname[4] = NUL;
STRCPY(ext, ".txt");
}
else
{
STRCPY(ext, ".xxx");
ext[1] = fname[5];
ext[2] = fname[6];
}
helptags_one(dirname, ext, fname, add_help_tags);
}
ga_clear(&ga);
FreeWild(filecount, files);
#else
helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags);
#endif
}
static void
helptags_cb(char_u *fname, void *cookie)
{
do_helptags(fname, *(int *)cookie);
}
void
ex_helptags(exarg_T *eap)
{
expand_T xpc;
char_u *dirname;
int add_help_tags = FALSE;
if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3]))
{
add_help_tags = TRUE;
eap->arg = skipwhite(eap->arg + 3);
}
if (STRCMP(eap->arg, "ALL") == 0)
{
do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
helptags_cb, &add_help_tags);
}
else
{
ExpandInit(&xpc);
xpc.xp_context = EXPAND_DIRECTORIES;
dirname = ExpandOne(&xpc, eap->arg, NULL,
WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
if (dirname == NULL || !mch_isdir(dirname))
EMSG2(_("E150: Not a directory: %s"), eap->arg);
else
do_helptags(dirname, add_help_tags);
vim_free(dirname);
}
}
#if defined(FEAT_SIGNS) || defined(PROTO)
typedef struct sign sign_T;
struct sign
{
sign_T *sn_next;
int sn_typenr;
char_u *sn_name;
char_u *sn_icon;
# ifdef FEAT_SIGN_ICONS
void *sn_image;
# endif
char_u *sn_text;
int sn_line_hl;
int sn_text_hl;
};
static sign_T *first_sign = NULL;
static int next_sign_typenr = 1;
static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd);
static void sign_list_defined(sign_T *sp);
static void sign_undefine(sign_T *sp, sign_T *sp_prev);
static char *cmds[] = {
"define",
# define SIGNCMD_DEFINE 0
"undefine",
# define SIGNCMD_UNDEFINE 1
"list",
# define SIGNCMD_LIST 2
"place",
# define SIGNCMD_PLACE 3
"unplace",
# define SIGNCMD_UNPLACE 4
"jump",
# define SIGNCMD_JUMP 5
NULL
# define SIGNCMD_LAST 6
};
static int
sign_cmd_idx(
char_u *begin_cmd,
char_u *end_cmd)
{
int idx;
char save = *end_cmd;
*end_cmd = NUL;
for (idx = 0; ; ++idx)
if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0)
break;
*end_cmd = save;
return idx;
}
void
ex_sign(exarg_T *eap)
{
char_u *arg = eap->arg;
char_u *p;
int idx;
sign_T *sp;
sign_T *sp_prev;
buf_T *buf;
p = skiptowhite(arg);
idx = sign_cmd_idx(arg, p);
if (idx == SIGNCMD_LAST)
{
EMSG2(_("E160: Unknown sign command: %s"), arg);
return;
}
arg = skipwhite(p);
if (idx <= SIGNCMD_LIST)
{
if (idx == SIGNCMD_LIST && *arg == NUL)
{
for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
sign_list_defined(sp);
}
else if (*arg == NUL)
EMSG(_("E156: Missing sign name"));
else
{
p = skiptowhite(arg);
if (*p != NUL)
*p++ = NUL;
while (arg[0] == '0' && arg[1] != NUL)
++arg;
sp_prev = NULL;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
{
if (STRCMP(sp->sn_name, arg) == 0)
break;
sp_prev = sp;
}
if (idx == SIGNCMD_DEFINE)
{
if (sp == NULL)
{
sign_T *lp;
int start = next_sign_typenr;
sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
if (sp == NULL)
return;
for (lp = first_sign; lp != NULL; )
{
if (lp->sn_typenr == next_sign_typenr)
{
++next_sign_typenr;
if (next_sign_typenr == MAX_TYPENR)
next_sign_typenr = 1;
if (next_sign_typenr == start)
{
vim_free(sp);
EMSG(_("E612: Too many signs defined"));
return;
}
lp = first_sign;
continue;
}
lp = lp->sn_next;
}
sp->sn_typenr = next_sign_typenr;
if (++next_sign_typenr == MAX_TYPENR)
next_sign_typenr = 1;
sp->sn_name = vim_strsave(arg);
if (sp->sn_name == NULL)
{
vim_free(sp);
return;
}
if (sp_prev == NULL)
first_sign = sp;
else
sp_prev->sn_next = sp;
}
for (;;)
{
arg = skipwhite(p);
if (*arg == NUL)
break;
p = skiptowhite_esc(arg);
if (STRNCMP(arg, "icon=", 5) == 0)
{
arg += 5;
vim_free(sp->sn_icon);
sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
backslash_halve(sp->sn_icon);
# ifdef FEAT_SIGN_ICONS
if (gui.in_use)
{
out_flush();
if (sp->sn_image != NULL)
gui_mch_destroy_sign(sp->sn_image);
sp->sn_image = gui_mch_register_sign(sp->sn_icon);
}
# endif
}
else if (STRNCMP(arg, "text=", 5) == 0)
{
char_u *s;
int cells;
int len;
arg += 5;
# ifdef FEAT_MBYTE
if (has_mbyte)
{
cells = 0;
for (s = arg; s < p; s += (*mb_ptr2len)(s))
{
if (!vim_isprintc((*mb_ptr2char)(s)))
break;
cells += (*mb_ptr2cells)(s);
}
}
else
# endif
{
for (s = arg; s < p; ++s)
if (!vim_isprintc(*s))
break;
cells = (int)(s - arg);
}
if (s != p || cells < 1 || cells > 2)
{
*p = NUL;
EMSG2(_("E239: Invalid sign text: %s"), arg);
return;
}
vim_free(sp->sn_text);
len = (int)(p - arg + ((cells == 1) ? 1 : 0));
sp->sn_text = vim_strnsave(arg, len);
if (sp->sn_text != NULL && cells == 1)
STRCPY(sp->sn_text + len - 1, " ");
}
else if (STRNCMP(arg, "linehl=", 7) == 0)
{
arg += 7;
sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
}
else if (STRNCMP(arg, "texthl=", 7) == 0)
{
arg += 7;
sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
}
else
{
EMSG2(_(e_invarg2), arg);
return;
}
}
}
else if (sp == NULL)
EMSG2(_("E155: Unknown sign: %s"), arg);
else if (idx == SIGNCMD_LIST)
sign_list_defined(sp);
else
sign_undefine(sp, sp_prev);
}
}
else
{
int id = -1;
linenr_T lnum = -1;
char_u *sign_name = NULL;
char_u *arg1;
if (*arg == NUL)
{
if (idx == SIGNCMD_PLACE)
{
sign_list_placed(NULL);
}
else if (idx == SIGNCMD_UNPLACE)
{
id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
if (id > 0)
{
buf_delsign(curwin->w_buffer, id);
update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
}
else
EMSG(_("E159: Missing sign number"));
}
else
EMSG(_(e_argreq));
return;
}
if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
{
buf_delete_all_signs();
return;
}
arg1 = arg;
if (VIM_ISDIGIT(*arg))
{
id = getdigits(&arg);
if (!VIM_ISWHITE(*arg) && *arg != NUL)
{
id = -1;
arg = arg1;
}
else
{
arg = skipwhite(arg);
if (idx == SIGNCMD_UNPLACE && *arg == NUL)
{
FOR_ALL_BUFFERS(buf)
if ((lnum = buf_delsign(buf, id)) != 0)
update_debug_sign(buf, lnum);
return;
}
}
}
for (;;)
{
if (STRNCMP(arg, "line=", 5) == 0)
{
arg += 5;
lnum = atoi((char *)arg);
arg = skiptowhite(arg);
}
else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
{
if (id != -1)
{
EMSG(_(e_invarg));
return;
}
id = -2;
arg = skiptowhite(arg + 1);
}
else if (STRNCMP(arg, "name=", 5) == 0)
{
arg += 5;
sign_name = arg;
arg = skiptowhite(arg);
if (*arg != NUL)
*arg++ = NUL;
while (sign_name[0] == '0' && sign_name[1] != NUL)
++sign_name;
}
else if (STRNCMP(arg, "file=", 5) == 0)
{
arg += 5;
buf = buflist_findname(arg);
break;
}
else if (STRNCMP(arg, "buffer=", 7) == 0)
{
arg += 7;
buf = buflist_findnr((int)getdigits(&arg));
if (*skipwhite(arg) != NUL)
EMSG(_(e_trailing));
break;
}
else
{
EMSG(_(e_invarg));
return;
}
arg = skipwhite(arg);
}
if (buf == NULL)
{
EMSG2(_("E158: Invalid buffer name: %s"), arg);
}
else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
{
if (lnum >= 0 || sign_name != NULL)
EMSG(_(e_invarg));
else
sign_list_placed(buf);
}
else if (idx == SIGNCMD_JUMP)
{
if (lnum >= 0 || sign_name != NULL)
EMSG(_(e_invarg));
else if ((lnum = buf_findsign(buf, id)) > 0)
{
if (buf_jump_open_win(buf) != NULL)
{
curwin->w_cursor.lnum = lnum;
check_cursor_lnum();
beginline(BL_WHITE);
}
else
{
char_u *cmd;
if (buf->b_fname == NULL)
{
EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
return;
}
cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25);
if (cmd == NULL)
return;
sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname);
do_cmdline_cmd(cmd);
vim_free(cmd);
}
# ifdef FEAT_FOLDING
foldOpenCursor();
# endif
}
else
EMSGN(_("E157: Invalid sign ID: %ld"), id);
}
else if (idx == SIGNCMD_UNPLACE)
{
if (lnum >= 0 || sign_name != NULL)
EMSG(_(e_invarg));
else if (id == -2)
{
redraw_buf_later(buf, NOT_VALID);
buf_delete_signs(buf);
}
else
{
lnum = buf_delsign(buf, id);
update_debug_sign(buf, lnum);
}
}
else if (sign_name != NULL)
{
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (STRCMP(sp->sn_name, sign_name) == 0)
break;
if (sp == NULL)
{
EMSG2(_("E155: Unknown sign: %s"), sign_name);
return;
}
if (lnum > 0)
buf_addsign(buf, id, lnum, sp->sn_typenr);
else
lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
if (lnum > 0)
update_debug_sign(buf, lnum);
else
EMSG2(_("E885: Not possible to change sign %s"), sign_name);
}
else
EMSG(_(e_invarg));
}
}
# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
void
sign_gui_started(void)
{
sign_T *sp;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (sp->sn_icon != NULL)
sp->sn_image = gui_mch_register_sign(sp->sn_icon);
}
# endif
static void
sign_list_defined(sign_T *sp)
{
char_u *p;
smsg((char_u *)"sign %s", sp->sn_name);
if (sp->sn_icon != NULL)
{
MSG_PUTS(" icon=");
msg_outtrans(sp->sn_icon);
# ifdef FEAT_SIGN_ICONS
if (sp->sn_image == NULL)
MSG_PUTS(_(" (NOT FOUND)"));
# else
MSG_PUTS(_(" (not supported)"));
# endif
}
if (sp->sn_text != NULL)
{
MSG_PUTS(" text=");
msg_outtrans(sp->sn_text);
}
if (sp->sn_line_hl > 0)
{
MSG_PUTS(" linehl=");
p = get_highlight_name_ext(NULL, sp->sn_line_hl - 1, FALSE);
if (p == NULL)
MSG_PUTS("NONE");
else
msg_puts(p);
}
if (sp->sn_text_hl > 0)
{
MSG_PUTS(" texthl=");
p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, FALSE);
if (p == NULL)
MSG_PUTS("NONE");
else
msg_puts(p);
}
}
static void
sign_undefine(sign_T *sp, sign_T *sp_prev)
{
vim_free(sp->sn_name);
vim_free(sp->sn_icon);
# ifdef FEAT_SIGN_ICONS
if (sp->sn_image != NULL)
{
out_flush();
gui_mch_destroy_sign(sp->sn_image);
}
# endif
vim_free(sp->sn_text);
if (sp_prev == NULL)
first_sign = sp->sn_next;
else
sp_prev->sn_next = sp->sn_next;
vim_free(sp);
}
int
sign_get_attr(int typenr, int line)
{
sign_T *sp;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (sp->sn_typenr == typenr)
{
if (line)
{
if (sp->sn_line_hl > 0)
return syn_id2attr(sp->sn_line_hl);
}
else
{
if (sp->sn_text_hl > 0)
return syn_id2attr(sp->sn_text_hl);
}
break;
}
return 0;
}
char_u *
sign_get_text(int typenr)
{
sign_T *sp;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (sp->sn_typenr == typenr)
return sp->sn_text;
return NULL;
}
# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
void *
sign_get_image(
int typenr)
{
sign_T *sp;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (sp->sn_typenr == typenr)
return sp->sn_image;
return NULL;
}
# endif
char_u *
sign_typenr2name(int typenr)
{
sign_T *sp;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (sp->sn_typenr == typenr)
return sp->sn_name;
return (char_u *)_("[Deleted]");
}
# if defined(EXITFREE) || defined(PROTO)
void
free_signs(void)
{
while (first_sign != NULL)
sign_undefine(first_sign, NULL);
}
# endif
# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
static enum
{
EXP_SUBCMD,
EXP_DEFINE,
EXP_PLACE,
EXP_UNPLACE,
EXP_SIGN_NAMES
} expand_what;
char_u *
get_sign_name(expand_T *xp UNUSED, int idx)
{
sign_T *sp;
int current_idx;
switch (expand_what)
{
case EXP_SUBCMD:
return (char_u *)cmds[idx];
case EXP_DEFINE:
{
char *define_arg[] =
{
"icon=", "linehl=", "text=", "texthl=", NULL
};
return (char_u *)define_arg[idx];
}
case EXP_PLACE:
{
char *place_arg[] =
{
"line=", "name=", "file=", "buffer=", NULL
};
return (char_u *)place_arg[idx];
}
case EXP_UNPLACE:
{
char *unplace_arg[] = { "file=", "buffer=", NULL };
return (char_u *)unplace_arg[idx];
}
case EXP_SIGN_NAMES:
current_idx = 0;
for (sp = first_sign; sp != NULL; sp = sp->sn_next)
if (current_idx++ == idx)
return sp->sn_name;
return NULL;
default:
return NULL;
}
}
void
set_context_in_sign_cmd(expand_T *xp, char_u *arg)
{
char_u *p;
char_u *end_subcmd;
char_u *last;
int cmd_idx;
char_u *begin_subcmd_args;
xp->xp_context = EXPAND_SIGN;
expand_what = EXP_SUBCMD;
xp->xp_pattern = arg;
end_subcmd = skiptowhite(arg);
if (*end_subcmd == NUL)
return;
cmd_idx = sign_cmd_idx(arg, end_subcmd);
begin_subcmd_args = skipwhite(end_subcmd);
p = skiptowhite(begin_subcmd_args);
if (*p == NUL)
{
xp->xp_pattern = begin_subcmd_args;
switch (cmd_idx)
{
case SIGNCMD_LIST:
case SIGNCMD_UNDEFINE:
expand_what = EXP_SIGN_NAMES;
break;
default:
xp->xp_context = EXPAND_NOTHING;
}
return;
}
do
{
p = skipwhite(p);
last = p;
p = skiptowhite(p);
} while (*p != NUL);
p = vim_strchr(last, '=');
if (p == NULL)
{
xp->xp_pattern = last;
switch (cmd_idx)
{
case SIGNCMD_DEFINE:
expand_what = EXP_DEFINE;
break;
case SIGNCMD_PLACE:
expand_what = EXP_PLACE;
break;
case SIGNCMD_JUMP:
case SIGNCMD_UNPLACE:
expand_what = EXP_UNPLACE;
break;
default:
xp->xp_context = EXPAND_NOTHING;
}
}
else
{
xp->xp_pattern = p + 1;
switch (cmd_idx)
{
case SIGNCMD_DEFINE:
if (STRNCMP(last, "texthl", p - last) == 0 ||
STRNCMP(last, "linehl", p - last) == 0)
xp->xp_context = EXPAND_HIGHLIGHT;
else if (STRNCMP(last, "icon", p - last) == 0)
xp->xp_context = EXPAND_FILES;
else
xp->xp_context = EXPAND_NOTHING;
break;
case SIGNCMD_PLACE:
if (STRNCMP(last, "name", p - last) == 0)
expand_what = EXP_SIGN_NAMES;
else
xp->xp_context = EXPAND_NOTHING;
break;
default:
xp->xp_context = EXPAND_NOTHING;
}
}
}
# endif
#endif
void
ex_smile(exarg_T *eap UNUSED)
{
static char *code[] = {
"\34 \4o\14$\4ox\30 \2o\30$\1ox\25 \2o\36$\1o\11 \1o\1$\3 \2$\1 \1o\1$x\5 \1o\1 \1$\1 \2o\10 \1o\44$\1o\7 \2$\1 \2$\1 \2$\1o\1$x\2 \2o\1 \1$\1 \1$\1 \1\"\1$\6 \1o\11$\4 \15$\4 \11$\1o\7 \3$\1o\2$\1o\1$x\2 \1\"\6$\1o\1$\5 \1o\11$\6 \13$\6 \12$\1o\4 \10$x\4 \7$\4 \13$\6 \13$\6 \27$x\4 \27$\4 \15$\4 \16$\2 \3\"\3$x\5 \1\"\3$\4\"\61$\5 \1\"\3$x\6 \3$\3 \1o\62$\5 \1\"\3$\1ox\5 \1o\2$\1\"\3 \63$\7 \3$\1ox\5 \3$\4 \55$\1\"\1 \1\"\6$",
"\5o\4$\1ox\4 \1o\3$\4o\5$\2 \45$\3 \1o\21$x\4 \10$\1\"\4$\3 \42$\5 \4$\10\"x\3 \4\"\7 \4$\4 \1\"\34$\1\"\6 \1o\3$x\16 \1\"\3$\1o\5 \3\"\22$\1\"\2$\1\"\11 \3$x\20 \3$\1o\12 \1\"\2$\2\"\6$\4\"\13 \1o\3$x\21 \4$\1o\40 \1o\3$\1\"x\22 \1\"\4$\1o\6 \1o\6$\1o\1\"\4$\1o\10 \1o\4$x\24 \1\"\5$\2o\5 \2\"\4$\1o\5$\1o\3 \1o\4$\2\"x\27 \2\"\5$\4o\2 \1\"\3$\1o\11$\3\"x\32 \2\"\7$\2o\1 \12$x\42 \4\"\13$x\46 \14$x\47 \12$\1\"x\50 \1\"\3$\4\"x"
};
char *p;
int n;
int i;
msg_start();
msg_putchar('\n');
for (i = 0; i < 2; ++i)
for (p = code[i]; *p != NUL; ++p)
if (*p == 'x')
msg_putchar('\n');
else
for (n = *p++; n > 0; --n)
if (*p == 'o' || *p == '$')
msg_putchar_attr(*p, HL_ATTR(HLF_L));
else
msg_putchar(*p);
msg_clr_eos();
}
#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
void
ex_drop(exarg_T *eap)
{
int split = FALSE;
win_T *wp;
buf_T *buf;
tabpage_T *tp;
set_arglist(eap->arg);
if (ARGCOUNT == 0)
return;
if (cmdmod.tab)
{
ex_all(eap);
}
else
{
buf = buflist_findnr(ARGLIST[0].ae_fnum);
FOR_ALL_TAB_WINDOWS(tp, wp)
{
if (wp->w_buffer == buf)
{
goto_tabpage_win(tp, wp);
curwin->w_arg_idx = 0;
return;
}
}
if (!buf_hide(curbuf))
{
++emsg_off;
split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
--emsg_off;
}
if (split)
{
eap->cmdidx = CMD_sfirst;
eap->cmd[0] = 's';
}
else
eap->cmdidx = CMD_first;
ex_rewind(eap);
}
}
#endif
char_u *
skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
{
int c;
if (vim_isIDc(*p))
{
if (s != NULL)
*s = p;
p = skiptowhite(p);
if (s != NULL && *p != NUL)
*p++ = NUL;
}
else
{
if (s != NULL)
*s = p + 1;
c = *p;
p = skip_regexp(p + 1, c, TRUE, NULL);
if (*p != c)
return NULL;
if (s != NULL)
*p = NUL;
++p;
while (*p == 'g' || *p == 'j')
{
if (flags != NULL)
{
if (*p == 'g')
*flags |= VGR_GLOBAL;
else
*flags |= VGR_NOJUMP;
}
++p;
}
}
return p;
}
#if defined(FEAT_EVAL) || defined(PROTO)
void
ex_oldfiles(exarg_T *eap UNUSED)
{
list_T *l = get_vim_var_list(VV_OLDFILES);
listitem_T *li;
int nr = 0;
char_u *fname;
if (l == NULL)
msg((char_u *)_("No old files"));
else
{
msg_start();
msg_scroll = TRUE;
for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
{
++nr;
fname = get_tv_string(&li->li_tv);
if (!message_filtered(fname))
{
msg_outnum((long)nr);
MSG_PUTS(": ");
msg_outtrans(fname);
msg_clr_eos();
msg_putchar('\n');
out_flush();
ui_breakcheck();
}
}
got_int = FALSE;
# ifdef FEAT_BROWSE_CMD
if (cmdmod.browse)
{
quit_more = FALSE;
nr = prompt_for_number(FALSE);
msg_starthere();
if (nr > 0)
{
char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
(long)nr);
if (p != NULL)
{
p = expand_env_save(p);
eap->arg = p;
eap->cmdidx = CMD_edit;
cmdmod.browse = FALSE;
do_exedit(eap, NULL);
vim_free(p);
}
}
}
# endif
}
}
#endif