#include "vim.h"
#include "version.h"
static char_u *vim_version_dir(char_u *vimdir);
static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
#if defined(FEAT_CMDL_COMPL)
static void init_users(void);
#endif
static int copy_indent(int size, char_u *src);
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
static garray_T ga_users;
#endif
int
get_indent(void)
{
return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
}
int
get_indent_lnum(linenr_T lnum)
{
return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
}
#if defined(FEAT_FOLDING) || defined(PROTO)
int
get_indent_buf(buf_T *buf, linenr_T lnum)
{
return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
}
#endif
int
get_indent_str(
char_u *ptr,
int ts,
int list)
{
int count = 0;
for ( ; *ptr; ++ptr)
{
if (*ptr == TAB)
{
if (!list || lcs_tab1)
count += ts - (count % ts);
else
count += ptr2cells(ptr);
}
else if (*ptr == ' ')
++count;
else
break;
}
return count;
}
int
set_indent(
int size,
int flags)
{
char_u *p;
char_u *newline;
char_u *oldline;
char_u *s;
int todo;
int ind_len;
int line_len;
int doit = FALSE;
int ind_done = 0;
int tab_pad;
int retval = FALSE;
int orig_char_len = -1;
todo = size;
ind_len = 0;
p = oldline = ml_get_curline();
if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
{
if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
{
ind_done = 0;
while (todo > 0 && VIM_ISWHITE(*p))
{
if (*p == TAB)
{
tab_pad = (int)curbuf->b_p_ts
- (ind_done % (int)curbuf->b_p_ts);
if (todo < tab_pad)
break;
todo -= tab_pad;
++ind_len;
ind_done += tab_pad;
}
else
{
--todo;
++ind_len;
++ind_done;
}
++p;
}
if (curbuf->b_p_et)
orig_char_len = ind_len;
tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
if (todo >= tab_pad && orig_char_len == -1)
{
doit = TRUE;
todo -= tab_pad;
++ind_len;
}
}
while (todo >= (int)curbuf->b_p_ts)
{
if (*p != TAB)
doit = TRUE;
else
++p;
todo -= (int)curbuf->b_p_ts;
++ind_len;
}
}
while (todo > 0)
{
if (*p != ' ')
doit = TRUE;
else
++p;
--todo;
++ind_len;
}
if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
return FALSE;
if (flags & SIN_INSERT)
p = oldline;
else
p = skipwhite(p);
line_len = (int)STRLEN(p) + 1;
if (orig_char_len != -1)
{
newline = alloc(orig_char_len + size - ind_done + line_len);
if (newline == NULL)
return FALSE;
todo = size - ind_done;
ind_len = orig_char_len + todo;
p = oldline;
s = newline;
while (orig_char_len > 0)
{
*s++ = *p++;
orig_char_len--;
}
while (VIM_ISWHITE(*p))
++p;
}
else
{
todo = size;
newline = alloc(ind_len + line_len);
if (newline == NULL)
return FALSE;
s = newline;
}
if (!curbuf->b_p_et)
{
if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
{
p = oldline;
ind_done = 0;
while (todo > 0 && VIM_ISWHITE(*p))
{
if (*p == TAB)
{
tab_pad = (int)curbuf->b_p_ts
- (ind_done % (int)curbuf->b_p_ts);
if (todo < tab_pad)
break;
todo -= tab_pad;
ind_done += tab_pad;
}
else
{
--todo;
++ind_done;
}
*s++ = *p++;
}
tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
if (todo >= tab_pad)
{
*s++ = TAB;
todo -= tab_pad;
}
p = skipwhite(p);
}
while (todo >= (int)curbuf->b_p_ts)
{
*s++ = TAB;
todo -= (int)curbuf->b_p_ts;
}
}
while (todo > 0)
{
*s++ = ' ';
--todo;
}
mch_memmove(s, p, (size_t)line_len);
if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
{
ml_replace(curwin->w_cursor.lnum, newline, FALSE);
if (flags & SIN_CHANGED)
changed_bytes(curwin->w_cursor.lnum, 0);
if (saved_cursor.lnum == curwin->w_cursor.lnum)
{
if (saved_cursor.col >= (colnr_T)(p - oldline))
saved_cursor.col += ind_len - (colnr_T)(p - oldline);
else if (saved_cursor.col >= (colnr_T)(s - newline))
saved_cursor.col = (colnr_T)(s - newline);
}
retval = TRUE;
}
else
vim_free(newline);
curwin->w_cursor.col = ind_len;
return retval;
}
static int
copy_indent(int size, char_u *src)
{
char_u *p = NULL;
char_u *line = NULL;
char_u *s;
int todo;
int ind_len;
int line_len = 0;
int tab_pad;
int ind_done;
int round;
for (round = 1; round <= 2; ++round)
{
todo = size;
ind_len = 0;
ind_done = 0;
s = src;
while (todo > 0 && VIM_ISWHITE(*s))
{
if (*s == TAB)
{
tab_pad = (int)curbuf->b_p_ts
- (ind_done % (int)curbuf->b_p_ts);
if (todo < tab_pad)
break;
todo -= tab_pad;
ind_done += tab_pad;
}
else
{
--todo;
++ind_done;
}
++ind_len;
if (p != NULL)
*p++ = *s;
++s;
}
tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
if (todo >= tab_pad && !curbuf->b_p_et)
{
todo -= tab_pad;
++ind_len;
if (p != NULL)
*p++ = TAB;
}
while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
{
todo -= (int)curbuf->b_p_ts;
++ind_len;
if (p != NULL)
*p++ = TAB;
}
while (todo > 0)
{
--todo;
++ind_len;
if (p != NULL)
*p++ = ' ';
}
if (p == NULL)
{
line_len = (int)STRLEN(ml_get_curline()) + 1;
line = alloc(ind_len + line_len);
if (line == NULL)
return FALSE;
p = line;
}
}
mch_memmove(p, ml_get_curline(), (size_t)line_len);
ml_replace(curwin->w_cursor.lnum, line, FALSE);
curwin->w_cursor.col = ind_len;
return TRUE;
}
int
get_number_indent(linenr_T lnum)
{
colnr_T col;
pos_T pos;
regmatch_T regmatch;
int lead_len = 0;
if (lnum > curbuf->b_ml.ml_line_count)
return -1;
pos.lnum = 0;
#ifdef FEAT_COMMENTS
if ((State & INSERT) || has_format_option(FO_Q_COMS))
lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
#endif
regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
if (regmatch.regprog != NULL)
{
regmatch.rm_ic = FALSE;
if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0))
{
pos.lnum = lnum;
pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
#ifdef FEAT_VIRTUALEDIT
pos.coladd = 0;
#endif
}
vim_regfree(regmatch.regprog);
}
if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
return -1;
getvcol(curwin, &pos, &col, NULL, NULL);
return (int)col;
}
#if defined(FEAT_LINEBREAK) || defined(PROTO)
int
get_breakindent_win(
win_T *wp,
char_u *line)
{
static int prev_indent = 0;
static long prev_ts = 0L;
static char_u *prev_line = NULL;
static varnumber_T prev_tick = 0;
int bri = 0;
const int eff_wwidth = wp->w_width
- ((wp->w_p_nu || wp->w_p_rnu)
&& (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
? number_width(wp) + 1 : 0);
if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
|| prev_tick != CHANGEDTICK(wp->w_buffer))
{
prev_line = line;
prev_ts = wp->w_buffer->b_p_ts;
prev_tick = CHANGEDTICK(wp->w_buffer);
prev_indent = get_indent_str(line,
(int)wp->w_buffer->b_p_ts, wp->w_p_list);
}
bri = prev_indent + wp->w_p_brishift;
if (wp->w_p_brisbr)
bri -= vim_strsize(p_sbr);
bri += win_col_off2(wp);
if (bri < 0)
bri = 0;
else if (bri > eff_wwidth - wp->w_p_brimin)
bri = (eff_wwidth - wp->w_p_brimin < 0)
? 0 : eff_wwidth - wp->w_p_brimin;
return bri;
}
#endif
#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
static int cin_is_cinword(char_u *line);
static int
cin_is_cinword(char_u *line)
{
char_u *cinw;
char_u *cinw_buf;
int cinw_len;
int retval = FALSE;
int len;
cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
cinw_buf = alloc((unsigned)cinw_len);
if (cinw_buf != NULL)
{
line = skipwhite(line);
for (cinw = curbuf->b_p_cinw; *cinw; )
{
len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
if (STRNCMP(line, cinw_buf, len) == 0
&& (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
{
retval = TRUE;
break;
}
}
vim_free(cinw_buf);
}
return retval;
}
#endif
int
open_line(
int dir,
int flags,
int second_line_indent)
{
char_u *saved_line;
char_u *next_line = NULL;
char_u *p_extra = NULL;
int less_cols = 0;
int less_cols_off = 0;
pos_T old_cursor;
int newcol = 0;
int newindent = 0;
int n;
int trunc_line = FALSE;
int retval = FALSE;
#ifdef FEAT_COMMENTS
int extra_len = 0;
int lead_len;
char_u *lead_flags;
char_u *leader = NULL;
#endif
char_u *allocated = NULL;
#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
|| defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
char_u *p;
#endif
int saved_char = NUL;
#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
pos_T *pos;
#endif
#ifdef FEAT_SMARTINDENT
int do_si = (!p_paste && curbuf->b_p_si
# ifdef FEAT_CINDENT
&& !curbuf->b_p_cin
# endif
# ifdef FEAT_EVAL
&& *curbuf->b_p_inde == NUL
# endif
);
int no_si = FALSE;
int first_char = NUL;
#endif
#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
int vreplace_mode;
#endif
int did_append;
int saved_pi = curbuf->b_p_pi;
saved_line = vim_strsave(ml_get_curline());
if (saved_line == NULL)
return FALSE;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
if (curwin->w_cursor.lnum < orig_line_count)
next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
else
next_line = vim_strsave((char_u *)"");
if (next_line == NULL)
goto theend;
replace_push(NUL);
replace_push(NUL);
p = saved_line + curwin->w_cursor.col;
while (*p != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
p += replace_push_mb(p);
else
#endif
replace_push(*p++);
}
saved_line[curwin->w_cursor.col] = NUL;
}
#endif
if ((State & INSERT)
#ifdef FEAT_VREPLACE
&& !(State & VREPLACE_FLAG)
#endif
)
{
p_extra = saved_line + curwin->w_cursor.col;
#ifdef FEAT_SMARTINDENT
if (do_si)
{
p = skipwhite(p_extra);
first_char = *p;
}
#endif
#ifdef FEAT_COMMENTS
extra_len = (int)STRLEN(p_extra);
#endif
saved_char = *p_extra;
*p_extra = NUL;
}
u_clearline();
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
#endif
ai_col = 0;
if (dir == FORWARD && did_ai)
trunc_line = TRUE;
if (curbuf->b_p_ai
#ifdef FEAT_SMARTINDENT
|| do_si
#endif
)
{
newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
newindent = second_line_indent;
#ifdef FEAT_SMARTINDENT
if (!trunc_line && do_si && *saved_line != NUL
&& (p_extra == NULL || first_char != '{'))
{
char_u *ptr;
char_u last_char;
old_cursor = curwin->w_cursor;
ptr = saved_line;
# ifdef FEAT_COMMENTS
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
else
lead_len = 0;
# endif
if (dir == FORWARD)
{
if (
# ifdef FEAT_COMMENTS
lead_len == 0 &&
# endif
ptr[0] == '#')
{
while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
ptr = ml_get(--curwin->w_cursor.lnum);
newindent = get_indent();
}
# ifdef FEAT_COMMENTS
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
else
lead_len = 0;
if (lead_len > 0)
{
p = skipwhite(ptr);
if (p[0] == '/' && p[1] == '*')
p++;
if (p[0] == '*')
{
for (p++; *p; p++)
{
if (p[0] == '/' && p[-1] == '*')
{
curwin->w_cursor.col = (colnr_T)(p - ptr);
if ((pos = findmatch(NULL, NUL)) != NULL)
{
curwin->w_cursor.lnum = pos->lnum;
newindent = get_indent();
}
}
}
}
}
else
# endif
{
p = ptr + STRLEN(ptr) - 1;
while (p > ptr && VIM_ISWHITE(*p))
--p;
last_char = *p;
if (last_char == '{' || last_char == ';')
{
if (p > ptr)
--p;
while (p > ptr && VIM_ISWHITE(*p))
--p;
}
if (*p == ')')
{
curwin->w_cursor.col = (colnr_T)(p - ptr);
if ((pos = findmatch(NULL, '(')) != NULL)
{
curwin->w_cursor.lnum = pos->lnum;
newindent = get_indent();
ptr = ml_get_curline();
}
}
if (last_char == '{')
{
did_si = TRUE;
no_si = TRUE;
}
else if (last_char != ';' && last_char != '}'
&& cin_is_cinword(ptr))
did_si = TRUE;
}
}
else
{
if (
# ifdef FEAT_COMMENTS
lead_len == 0 &&
# endif
ptr[0] == '#')
{
int was_backslashed = FALSE;
while ((ptr[0] == '#' || was_backslashed) &&
curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
{
if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
was_backslashed = TRUE;
else
was_backslashed = FALSE;
ptr = ml_get(++curwin->w_cursor.lnum);
}
if (was_backslashed)
newindent = 0;
else
newindent = get_indent();
}
p = skipwhite(ptr);
if (*p == '}')
did_si = TRUE;
else
can_si_back = TRUE;
}
curwin->w_cursor = old_cursor;
}
if (do_si)
can_si = TRUE;
#endif
did_ai = TRUE;
}
#ifdef FEAT_COMMENTS
end_comment_pending = NUL;
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
else
lead_len = 0;
if (lead_len > 0)
{
char_u *lead_repl = NULL;
int lead_repl_len = 0;
char_u lead_middle[COM_MAX_LEN];
char_u lead_end[COM_MAX_LEN];
char_u *comment_end = NULL;
int extra_space = FALSE;
int current_flag;
int require_blank = FALSE;
char_u *p2;
for (p = lead_flags; *p && *p != ':'; ++p)
{
if (*p == COM_BLANK)
{
require_blank = TRUE;
continue;
}
if (*p == COM_START || *p == COM_MIDDLE)
{
current_flag = *p;
if (*p == COM_START)
{
if (dir == BACKWARD)
{
lead_len = 0;
break;
}
(void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
require_blank = FALSE;
}
while (*p && p[-1] != ':')
{
if (*p == COM_BLANK)
require_blank = TRUE;
++p;
}
(void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
while (*p && p[-1] != ':')
{
if (*p == COM_AUTO_END)
end_comment_pending = -1;
++p;
}
n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
if (end_comment_pending == -1)
end_comment_pending = lead_end[n - 1];
if (dir == FORWARD)
{
for (p = saved_line + lead_len; *p; ++p)
if (STRNCMP(p, lead_end, n) == 0)
{
comment_end = p;
lead_len = 0;
break;
}
}
if (lead_len > 0)
{
if (current_flag == COM_START)
{
lead_repl = lead_middle;
lead_repl_len = (int)STRLEN(lead_middle);
}
if (!VIM_ISWHITE(saved_line[lead_len - 1])
&& ((p_extra != NULL
&& (int)curwin->w_cursor.col == lead_len)
|| (p_extra == NULL
&& saved_line[lead_len] == NUL)
|| require_blank))
extra_space = TRUE;
}
break;
}
if (*p == COM_END)
{
if (dir == FORWARD)
{
comment_end = skipwhite(saved_line);
lead_len = 0;
break;
}
while (p > curbuf->b_p_com && *p != ',')
--p;
for (lead_repl = p; lead_repl > curbuf->b_p_com
&& lead_repl[-1] != ':'; --lead_repl)
;
lead_repl_len = (int)(p - lead_repl);
extra_space = TRUE;
for (p2 = p; *p2 && *p2 != ':'; p2++)
{
if (*p2 == COM_AUTO_END)
end_comment_pending = -1;
}
if (end_comment_pending == -1)
{
while (*p2 && *p2 != ',')
p2++;
end_comment_pending = p2[-1];
}
break;
}
if (*p == COM_FIRST)
{
if (dir == BACKWARD)
lead_len = 0;
else
{
lead_repl = (char_u *)"";
lead_repl_len = 0;
}
break;
}
}
if (lead_len)
{
leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
+ (second_line_indent > 0 ? second_line_indent : 0) + 1);
allocated = leader;
if (leader == NULL)
lead_len = 0;
else
{
vim_strncpy(leader, saved_line, lead_len);
if (lead_repl != NULL)
{
int c = 0;
int off = 0;
for (p = lead_flags; *p != NUL && *p != ':'; )
{
if (*p == COM_RIGHT || *p == COM_LEFT)
c = *p++;
else if (VIM_ISDIGIT(*p) || *p == '-')
off = getdigits(&p);
else
++p;
}
if (c == COM_RIGHT)
{
for (p = leader + lead_len - 1; p > leader
&& VIM_ISWHITE(*p); --p)
;
++p;
#ifdef FEAT_MBYTE
{
int repl_size = vim_strnsize(lead_repl,
lead_repl_len);
int old_size = 0;
char_u *endp = p;
int l;
while (old_size < repl_size && p > leader)
{
MB_PTR_BACK(leader, p);
old_size += ptr2cells(p);
}
l = lead_repl_len - (int)(endp - p);
if (l != 0)
mch_memmove(endp + l, endp,
(size_t)((leader + lead_len) - endp));
lead_len += l;
}
#else
if (p < leader + lead_repl_len)
p = leader;
else
p -= lead_repl_len;
#endif
mch_memmove(p, lead_repl, (size_t)lead_repl_len);
if (p + lead_repl_len > leader + lead_len)
p[lead_repl_len] = NUL;
while (--p >= leader)
{
#ifdef FEAT_MBYTE
int l = mb_head_off(leader, p);
if (l > 1)
{
p -= l;
if (ptr2cells(p) > 1)
{
p[1] = ' ';
--l;
}
mch_memmove(p + 1, p + l + 1,
(size_t)((leader + lead_len) - (p + l + 1)));
lead_len -= l;
*p = ' ';
}
else
#endif
if (!VIM_ISWHITE(*p))
*p = ' ';
}
}
else
{
p = skipwhite(leader);
#ifdef FEAT_MBYTE
{
int repl_size = vim_strnsize(lead_repl,
lead_repl_len);
int i;
int l;
for (i = 0; i < lead_len && p[i] != NUL; i += l)
{
l = (*mb_ptr2len)(p + i);
if (vim_strnsize(p, i + l) > repl_size)
break;
}
if (i != lead_repl_len)
{
mch_memmove(p + lead_repl_len, p + i,
(size_t)(lead_len - i - (p - leader)));
lead_len += lead_repl_len - i;
}
}
#endif
mch_memmove(p, lead_repl, (size_t)lead_repl_len);
for (p += lead_repl_len; p < leader + lead_len; ++p)
if (!VIM_ISWHITE(*p))
{
if (p + 1 < leader + lead_len && p[1] == TAB)
{
--lead_len;
mch_memmove(p, p + 1,
(leader + lead_len) - p);
}
else
{
#ifdef FEAT_MBYTE
int l = (*mb_ptr2len)(p);
if (l > 1)
{
if (ptr2cells(p) > 1)
{
--l;
*p++ = ' ';
}
mch_memmove(p + 1, p + l,
(leader + lead_len) - p);
lead_len -= l - 1;
}
#endif
*p = ' ';
}
}
*p = NUL;
}
if (curbuf->b_p_ai
#ifdef FEAT_SMARTINDENT
|| do_si
#endif
)
newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
if (newindent + off < 0)
{
off = -newindent;
newindent = 0;
}
else
newindent += off;
while (off > 0 && lead_len > 0
&& leader[lead_len - 1] == ' ')
{
if (vim_strchr(skipwhite(leader), '\t') != NULL)
break;
--lead_len;
--off;
}
if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
extra_space = FALSE;
leader[lead_len] = NUL;
}
if (extra_space)
{
leader[lead_len++] = ' ';
leader[lead_len] = NUL;
}
newcol = lead_len;
if (newindent
#ifdef FEAT_SMARTINDENT
|| did_si
#endif
)
{
while (lead_len && VIM_ISWHITE(*leader))
{
--lead_len;
--newcol;
++leader;
}
}
}
#ifdef FEAT_SMARTINDENT
did_si = can_si = FALSE;
#endif
}
else if (comment_end != NULL)
{
if (comment_end[0] == '*' && comment_end[1] == '/' &&
(curbuf->b_p_ai
#ifdef FEAT_SMARTINDENT
|| do_si
#endif
))
{
old_cursor = curwin->w_cursor;
curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
if ((pos = findmatch(NULL, NUL)) != NULL)
{
curwin->w_cursor.lnum = pos->lnum;
newindent = get_indent();
}
curwin->w_cursor = old_cursor;
}
}
}
#endif
if (p_extra != NULL)
{
*p_extra = saved_char;
if (REPLACE_NORMAL(State))
replace_push(NUL);
if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
{
while ((*p_extra == ' ' || *p_extra == '\t')
#ifdef FEAT_MBYTE
&& (!enc_utf8
|| !utf_iscomposing(utf_ptr2char(p_extra + 1)))
#endif
)
{
if (REPLACE_NORMAL(State))
replace_push(*p_extra);
++p_extra;
++less_cols_off;
}
}
if (*p_extra != NUL)
did_ai = FALSE;
less_cols = (int)(p_extra - saved_line);
}
if (p_extra == NULL)
p_extra = (char_u *)"";
#ifdef FEAT_COMMENTS
if (lead_len)
{
if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
{
int i;
int padding = second_line_indent
- (newindent + (int)STRLEN(leader));
for (i = 0; i < padding; i++)
{
STRCAT(leader, " ");
less_cols--;
newcol++;
}
}
STRCAT(leader, p_extra);
p_extra = leader;
did_ai = TRUE;
less_cols -= lead_len;
}
else
end_comment_pending = NUL;
#endif
old_cursor = curwin->w_cursor;
if (dir == BACKWARD)
--curwin->w_cursor.lnum;
#ifdef FEAT_VREPLACE
if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
#endif
{
if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
== FAIL)
goto theend;
if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
#ifdef FEAT_DIFF
|| curwin->w_p_diff
#endif
)
mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
did_append = TRUE;
}
#ifdef FEAT_VREPLACE
else
{
curwin->w_cursor.lnum++;
if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
{
(void)u_save_cursor();
vr_lines_changed++;
}
ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
changed_bytes(curwin->w_cursor.lnum, 0);
curwin->w_cursor.lnum--;
did_append = FALSE;
}
#endif
if (newindent
#ifdef FEAT_SMARTINDENT
|| did_si
#endif
)
{
++curwin->w_cursor.lnum;
#ifdef FEAT_SMARTINDENT
if (did_si)
{
int sw = (int)get_sw_value(curbuf);
if (p_sr)
newindent -= newindent % sw;
newindent += sw;
}
#endif
if (curbuf->b_p_ci)
{
(void)copy_indent(newindent, saved_line);
curbuf->b_p_pi = TRUE;
}
else
(void)set_indent(newindent, SIN_INSERT);
less_cols -= curwin->w_cursor.col;
ai_col = curwin->w_cursor.col;
if (REPLACE_NORMAL(State))
for (n = 0; n < (int)curwin->w_cursor.col; ++n)
replace_push(NUL);
newcol += curwin->w_cursor.col;
#ifdef FEAT_SMARTINDENT
if (no_si)
did_si = FALSE;
#endif
}
#ifdef FEAT_COMMENTS
if (REPLACE_NORMAL(State))
while (lead_len-- > 0)
replace_push(NUL);
#endif
curwin->w_cursor = old_cursor;
if (dir == FORWARD)
{
if (trunc_line || (State & INSERT))
{
saved_line[curwin->w_cursor.col] = NUL;
if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
truncate_spaces(saved_line);
ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
saved_line = NULL;
if (did_append)
{
changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
curwin->w_cursor.lnum + 1, 1L);
did_append = FALSE;
if (flags & OPENLINE_MARKFIX)
mark_col_adjust(curwin->w_cursor.lnum,
curwin->w_cursor.col + less_cols_off,
1L, (long)-less_cols);
}
else
changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
}
curwin->w_cursor.lnum = old_cursor.lnum + 1;
}
if (did_append)
changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
curwin->w_cursor.col = newcol;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
if (State & VREPLACE_FLAG)
{
vreplace_mode = State;
State = INSERT;
}
else
vreplace_mode = 0;
#endif
#ifdef FEAT_LISP
if (!p_paste
# ifdef FEAT_COMMENTS
&& leader == NULL
# endif
&& curbuf->b_p_lisp
&& curbuf->b_p_ai)
{
fixthisline(get_lisp_indent);
ai_col = (colnr_T)getwhitecols_curline();
}
#endif
#ifdef FEAT_CINDENT
if (!p_paste
&& (curbuf->b_p_cin
# ifdef FEAT_EVAL
|| *curbuf->b_p_inde != NUL
# endif
)
&& in_cinkeys(dir == FORWARD
? KEY_OPEN_FORW
: KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
{
do_c_expr_indent();
ai_col = (colnr_T)getwhitecols_curline();
}
#endif
#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
if (vreplace_mode != 0)
State = vreplace_mode;
#endif
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
p_extra = vim_strsave(ml_get_curline());
if (p_extra == NULL)
goto theend;
ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
curwin->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
ins_bytes(p_extra);
vim_free(p_extra);
next_line = NULL;
}
#endif
retval = TRUE;
theend:
curbuf->b_p_pi = saved_pi;
vim_free(saved_line);
vim_free(next_line);
vim_free(allocated);
return retval;
}
#if defined(FEAT_COMMENTS) || defined(PROTO)
int
get_leader_len(
char_u *line,
char_u **flags,
int backward,
int include_space)
{
int i, j;
int result;
int got_com = FALSE;
int found_one;
char_u part_buf[COM_MAX_LEN];
char_u *string;
char_u *list;
int middle_match_len = 0;
char_u *prev_list;
char_u *saved_flags = NULL;
result = i = 0;
while (VIM_ISWHITE(line[i]))
++i;
while (line[i] != NUL)
{
found_one = FALSE;
for (list = curbuf->b_p_com; *list; )
{
if (!got_com && flags != NULL)
*flags = list;
prev_list = list;
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
string = vim_strchr(part_buf, ':');
if (string == NULL)
continue;
*string++ = NUL;
if (middle_match_len != 0
&& vim_strchr(part_buf, COM_MIDDLE) == NULL
&& vim_strchr(part_buf, COM_END) == NULL)
break;
if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
continue;
if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
continue;
if (VIM_ISWHITE(string[0]))
{
if (i == 0 || !VIM_ISWHITE(line[i - 1]))
continue;
while (VIM_ISWHITE(string[0]))
++string;
}
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
;
if (string[j] != NUL)
continue;
if (vim_strchr(part_buf, COM_BLANK) != NULL
&& !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
continue;
if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
{
if (middle_match_len == 0)
{
middle_match_len = j;
saved_flags = prev_list;
}
continue;
}
if (middle_match_len != 0 && j > middle_match_len)
middle_match_len = 0;
if (middle_match_len == 0)
i += j;
found_one = TRUE;
break;
}
if (middle_match_len != 0)
{
if (!got_com && flags != NULL)
*flags = saved_flags;
i += middle_match_len;
found_one = TRUE;
}
if (!found_one)
break;
result = i;
while (VIM_ISWHITE(line[i]))
++i;
if (include_space)
result = i;
got_com = TRUE;
if (vim_strchr(part_buf, COM_NEST) == NULL)
break;
}
return result;
}
int
get_last_leader_offset(char_u *line, char_u **flags)
{
int result = -1;
int i, j;
int lower_check_bound = 0;
char_u *string;
char_u *com_leader;
char_u *com_flags;
char_u *list;
int found_one;
char_u part_buf[COM_MAX_LEN];
i = (int)STRLEN(line);
while (--i >= lower_check_bound)
{
found_one = FALSE;
for (list = curbuf->b_p_com; *list; )
{
char_u *flags_save = list;
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
string = vim_strchr(part_buf, ':');
if (string == NULL)
{
continue;
}
*string++ = NUL;
com_leader = string;
if (VIM_ISWHITE(string[0]))
{
if (i == 0 || !VIM_ISWHITE(line[i - 1]))
continue;
while (VIM_ISWHITE(string[0]))
++string;
}
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
;
if (string[j] != NUL)
continue;
if (vim_strchr(part_buf, COM_BLANK) != NULL
&& !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
{
continue;
}
found_one = TRUE;
if (flags)
*flags = flags_save;
com_flags = flags_save;
break;
}
if (found_one)
{
char_u part_buf2[COM_MAX_LEN];
int len1, len2, off;
result = i;
if (vim_strchr(part_buf, COM_NEST) != NULL)
continue;
lower_check_bound = i;
while (VIM_ISWHITE(*com_leader))
++com_leader;
len1 = (int)STRLEN(com_leader);
for (list = curbuf->b_p_com; *list; )
{
char_u *flags_save = list;
(void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
if (flags_save == com_flags)
continue;
string = vim_strchr(part_buf2, ':');
++string;
while (VIM_ISWHITE(*string))
++string;
len2 = (int)STRLEN(string);
if (len2 == 0)
continue;
for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
{
--off;
if (!STRNCMP(string + off, com_leader, len2 - off))
{
if (i - off < lower_check_bound)
lower_check_bound = i - off;
}
}
}
}
}
return result;
}
#endif
int
plines(linenr_T lnum)
{
return plines_win(curwin, lnum, TRUE);
}
int
plines_win(
win_T *wp,
linenr_T lnum,
int winheight)
{
#if defined(FEAT_DIFF) || defined(PROTO)
return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
}
int
plines_nofill(linenr_T lnum)
{
return plines_win_nofill(curwin, lnum, TRUE);
}
int
plines_win_nofill(
win_T *wp,
linenr_T lnum,
int winheight)
{
#endif
int lines;
if (!wp->w_p_wrap)
return 1;
if (wp->w_width == 0)
return 1;
#ifdef FEAT_FOLDING
if (lineFolded(wp, lnum) == TRUE)
return 1;
#endif
lines = plines_win_nofold(wp, lnum);
if (winheight > 0 && lines > wp->w_height)
return (int)wp->w_height;
return lines;
}
int
plines_win_nofold(win_T *wp, linenr_T lnum)
{
char_u *s;
long col;
int width;
s = ml_get_buf(wp->w_buffer, lnum, FALSE);
if (*s == NUL)
return 1;
col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
if (wp->w_p_list && lcs_eol != NUL)
col += 1;
width = wp->w_width - win_col_off(wp);
if (width <= 0)
return 32000;
if (col <= width)
return 1;
col -= width;
width += win_col_off2(wp);
return (col + (width - 1)) / width + 1;
}
int
plines_win_col(win_T *wp, linenr_T lnum, long column)
{
long col;
char_u *s;
int lines = 0;
int width;
char_u *line;
#ifdef FEAT_DIFF
lines = diff_check_fill(wp, lnum);
#endif
if (!wp->w_p_wrap)
return lines + 1;
if (wp->w_width == 0)
return lines + 1;
line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
col = 0;
while (*s != NUL && --column >= 0)
{
col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
MB_PTR_ADV(s);
}
if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
width = wp->w_width - win_col_off(wp);
if (width <= 0)
return 9999;
lines += 1;
if (col > width)
lines += (col - width) / (width + win_col_off2(wp)) + 1;
return lines;
}
int
plines_m_win(win_T *wp, linenr_T first, linenr_T last)
{
int count = 0;
while (first <= last)
{
#ifdef FEAT_FOLDING
int x;
x = foldedCount(wp, first, NULL);
if (x > 0)
{
++count;
first += x;
}
else
#endif
{
#ifdef FEAT_DIFF
if (first == wp->w_topline)
count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
else
#endif
count += plines_win(wp, first, TRUE);
++first;
}
}
return (count);
}
#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
void
ins_bytes(char_u *p)
{
ins_bytes_len(p, (int)STRLEN(p));
}
#endif
#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
|| defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
void
ins_bytes_len(char_u *p, int len)
{
int i;
# ifdef FEAT_MBYTE
int n;
if (has_mbyte)
for (i = 0; i < len; i += n)
{
if (enc_utf8)
n = utfc_ptr2len_len(p + i, len - i);
else
n = (*mb_ptr2len)(p + i);
ins_char_bytes(p + i, n);
}
else
# endif
for (i = 0; i < len; ++i)
ins_char(p[i]);
}
#endif
void
ins_char(int c)
{
char_u buf[MB_MAXBYTES + 1];
int n = 1;
#ifdef FEAT_MBYTE
n = (*mb_char2bytes)(c, buf);
if (buf[0] == 0)
buf[0] = '\n';
#else
buf[0] = c;
#endif
ins_char_bytes(buf, n);
}
void
ins_char_bytes(char_u *buf, int charlen)
{
int c = buf[0];
int newlen;
int oldlen;
char_u *p;
char_u *newp;
char_u *oldp;
int linelen;
colnr_T col;
linenr_T lnum = curwin->w_cursor.lnum;
int i;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active() && curwin->w_cursor.coladd > 0)
coladvance_force(getviscol());
#endif
col = curwin->w_cursor.col;
oldp = ml_get(lnum);
linelen = (int)STRLEN(oldp) + 1;
oldlen = 0;
newlen = charlen;
if (State & REPLACE_FLAG)
{
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
colnr_T new_vcol = 0;
colnr_T vcol;
int old_list;
#ifndef FEAT_MBYTE
char_u buf[2];
#endif
old_list = curwin->w_p_list;
if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
curwin->w_p_list = FALSE;
getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
#ifndef FEAT_MBYTE
buf[0] = c;
buf[1] = NUL;
#endif
new_vcol = vcol + chartabsize(buf, vcol);
while (oldp[col + oldlen] != NUL && vcol < new_vcol)
{
vcol += chartabsize(oldp + col + oldlen, vcol);
if (vcol > new_vcol && oldp[col + oldlen] == TAB)
break;
#ifdef FEAT_MBYTE
oldlen += (*mb_ptr2len)(oldp + col + oldlen);
#else
++oldlen;
#endif
if (vcol > new_vcol)
newlen += vcol - new_vcol;
}
curwin->w_p_list = old_list;
}
else
#endif
if (oldp[col] != NUL)
{
#ifdef FEAT_MBYTE
oldlen = (*mb_ptr2len)(oldp + col);
#else
oldlen = 1;
#endif
}
replace_push(NUL);
for (i = 0; i < oldlen; ++i)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
i += replace_push_mb(oldp + col + i) - 1;
else
#endif
replace_push(oldp[col + i]);
}
}
newp = alloc_check((unsigned)(linelen + newlen - oldlen));
if (newp == NULL)
return;
if (col > 0)
mch_memmove(newp, oldp, (size_t)col);
p = newp + col;
if (linelen > col + oldlen)
mch_memmove(p + newlen, oldp + col + oldlen,
(size_t)(linelen - col - oldlen));
#ifdef FEAT_MBYTE
mch_memmove(p, buf, charlen);
i = charlen;
#else
*p = c;
i = 1;
#endif
while (i < newlen)
p[i++] = ' ';
ml_replace(lnum, newp, FALSE);
changed_bytes(lnum, col);
if (p_sm && (State & INSERT)
&& msg_silent == 0
#ifdef FEAT_INS_EXPAND
&& !ins_compl_active()
#endif
)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
showmatch(mb_ptr2char(buf));
else
#endif
showmatch(c);
}
#ifdef FEAT_RIGHTLEFT
if (!p_ri || (State & REPLACE_FLAG))
#endif
{
#ifdef FEAT_MBYTE
curwin->w_cursor.col += charlen;
#else
++curwin->w_cursor.col;
#endif
}
}
void
ins_str(char_u *s)
{
char_u *oldp, *newp;
int newlen = (int)STRLEN(s);
int oldlen;
colnr_T col;
linenr_T lnum = curwin->w_cursor.lnum;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active() && curwin->w_cursor.coladd > 0)
coladvance_force(getviscol());
#endif
col = curwin->w_cursor.col;
oldp = ml_get(lnum);
oldlen = (int)STRLEN(oldp);
newp = alloc_check((unsigned)(oldlen + newlen + 1));
if (newp == NULL)
return;
if (col > 0)
mch_memmove(newp, oldp, (size_t)col);
mch_memmove(newp + col, s, (size_t)newlen);
mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
ml_replace(lnum, newp, FALSE);
changed_bytes(lnum, col);
curwin->w_cursor.col += newlen;
}
int
del_char(int fixpos)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
mb_adjust_cursor();
if (*ml_get_cursor() == NUL)
return FAIL;
return del_chars(1L, fixpos);
}
#endif
return del_bytes(1L, fixpos, TRUE);
}
#if defined(FEAT_MBYTE) || defined(PROTO)
int
del_chars(long count, int fixpos)
{
long bytes = 0;
long i;
char_u *p;
int l;
p = ml_get_cursor();
for (i = 0; i < count && *p != NUL; ++i)
{
l = (*mb_ptr2len)(p);
bytes += l;
p += l;
}
return del_bytes(bytes, fixpos, TRUE);
}
#endif
int
del_bytes(
long count,
int fixpos_arg,
int use_delcombine UNUSED)
{
char_u *oldp, *newp;
colnr_T oldlen;
linenr_T lnum = curwin->w_cursor.lnum;
colnr_T col = curwin->w_cursor.col;
int was_alloced;
long movelen;
int fixpos = fixpos_arg;
oldp = ml_get(lnum);
oldlen = (int)STRLEN(oldp);
if (col >= oldlen)
return FAIL;
#ifdef FEAT_MBYTE
if (p_deco && use_delcombine && enc_utf8
&& utfc_ptr2len(oldp + col) >= count)
{
int cc[MAX_MCO];
int n;
(void)utfc_ptr2char(oldp + col, cc);
if (cc[0] != NUL)
{
n = col;
do
{
col = n;
count = utf_ptr2len(oldp + n);
n += count;
} while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
fixpos = 0;
}
}
#endif
movelen = (long)oldlen - (long)col - count + 1;
if (movelen <= 1)
{
if (col > 0 && fixpos && restart_edit == 0
#ifdef FEAT_VIRTUALEDIT
&& (ve_flags & VE_ONEMORE) == 0
#endif
)
{
--curwin->w_cursor.col;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
#ifdef FEAT_MBYTE
if (has_mbyte)
curwin->w_cursor.col -=
(*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
#endif
}
count = oldlen - col;
movelen = 1;
}
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active())
was_alloced = FALSE;
else
#endif
was_alloced = ml_line_alloced();
if (was_alloced)
newp = oldp;
else
{
newp = alloc((unsigned)(oldlen + 1 - count));
if (newp == NULL)
return FAIL;
mch_memmove(newp, oldp, (size_t)col);
}
mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
if (!was_alloced)
ml_replace(lnum, newp, FALSE);
changed_bytes(lnum, curwin->w_cursor.col);
return OK;
}
int
truncate_line(
int fixpos)
{
char_u *newp;
linenr_T lnum = curwin->w_cursor.lnum;
colnr_T col = curwin->w_cursor.col;
if (col == 0)
newp = vim_strsave((char_u *)"");
else
newp = vim_strnsave(ml_get(lnum), col);
if (newp == NULL)
return FAIL;
ml_replace(lnum, newp, FALSE);
changed_bytes(lnum, curwin->w_cursor.col);
if (fixpos && curwin->w_cursor.col > 0)
--curwin->w_cursor.col;
return OK;
}
void
del_lines(
long nlines,
int undo)
{
long n;
linenr_T first = curwin->w_cursor.lnum;
if (nlines <= 0)
return;
if (undo && u_savedel(first, nlines) == FAIL)
return;
for (n = 0; n < nlines; )
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
break;
ml_delete(first, TRUE);
++n;
if (first > curbuf->b_ml.ml_line_count)
break;
}
curwin->w_cursor.col = 0;
check_cursor_lnum();
deleted_lines_mark(first, n);
}
int
gchar_pos(pos_T *pos)
{
char_u *ptr = ml_get_pos(pos);
#ifdef FEAT_MBYTE
if (has_mbyte)
return (*mb_ptr2char)(ptr);
#endif
return (int)*ptr;
}
int
gchar_cursor(void)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
return (*mb_ptr2char)(ml_get_cursor());
#endif
return (int)*ml_get_cursor();
}
void
pchar_cursor(int c)
{
*(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
+ curwin->w_cursor.col) = c;
}
int
inindent(int extra)
{
char_u *ptr;
colnr_T col;
for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
++ptr;
if (col >= curwin->w_cursor.col + extra)
return TRUE;
else
return FALSE;
}
char_u *
skip_to_option_part(char_u *p)
{
if (*p == ',')
++p;
while (*p == ' ')
++p;
return p;
}
void
changed(void)
{
#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
if (p_imst == IM_ON_THE_SPOT)
{
if (im_is_preediting() && !xim_changed_while_preediting)
return;
xim_changed_while_preediting = FALSE;
}
#endif
if (!curbuf->b_changed)
{
int save_msg_scroll = msg_scroll;
change_warning(0);
if (curbuf->b_may_swap
#ifdef FEAT_QUICKFIX
&& !bt_dontwrite(curbuf)
#endif
)
{
int save_need_wait_return = need_wait_return;
need_wait_return = FALSE;
ml_open_file(curbuf);
if (need_wait_return && emsg_silent == 0)
{
out_flush();
ui_delay(2000L, TRUE);
wait_return(TRUE);
msg_scroll = save_msg_scroll;
}
else
need_wait_return = save_need_wait_return;
}
changed_int();
}
++CHANGEDTICK(curbuf);
}
void
changed_int(void)
{
curbuf->b_changed = TRUE;
ml_setflags(curbuf);
check_status(curbuf);
redraw_tabline = TRUE;
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
}
static void changedOneline(buf_T *buf, linenr_T lnum);
static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
void
changed_bytes(linenr_T lnum, colnr_T col)
{
changedOneline(curbuf, lnum);
changed_common(lnum, col, lnum + 1, 0L);
#ifdef FEAT_DIFF
if (curwin->w_p_diff)
{
win_T *wp;
linenr_T wlnum;
FOR_ALL_WINDOWS(wp)
if (wp->w_p_diff && wp != curwin)
{
redraw_win_later(wp, VALID);
wlnum = diff_lnum_win(lnum, wp);
if (wlnum > 0)
changedOneline(wp->w_buffer, wlnum);
}
}
#endif
}
static void
changedOneline(buf_T *buf, linenr_T lnum)
{
if (buf->b_mod_set)
{
if (lnum < buf->b_mod_top)
buf->b_mod_top = lnum;
else if (lnum >= buf->b_mod_bot)
buf->b_mod_bot = lnum + 1;
}
else
{
buf->b_mod_set = TRUE;
buf->b_mod_top = lnum;
buf->b_mod_bot = lnum + 1;
buf->b_mod_xlines = 0;
}
}
void
appended_lines(linenr_T lnum, long count)
{
changed_lines(lnum + 1, 0, lnum + 1, count);
}
void
appended_lines_mark(linenr_T lnum, long count)
{
if (lnum + count < curbuf->b_ml.ml_line_count
#ifdef FEAT_DIFF
|| curwin->w_p_diff
#endif
)
mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
changed_lines(lnum + 1, 0, lnum + 1, count);
}
void
deleted_lines(linenr_T lnum, long count)
{
changed_lines(lnum, 0, lnum + count, -count);
}
void
deleted_lines_mark(linenr_T lnum, long count)
{
mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
changed_lines(lnum, 0, lnum + count, -count);
}
void
changed_lines(
linenr_T lnum,
colnr_T col,
linenr_T lnume,
long xtra)
{
changed_lines_buf(curbuf, lnum, lnume, xtra);
#ifdef FEAT_DIFF
if (xtra == 0 && curwin->w_p_diff)
{
win_T *wp;
linenr_T wlnum;
FOR_ALL_WINDOWS(wp)
if (wp->w_p_diff && wp != curwin)
{
redraw_win_later(wp, VALID);
wlnum = diff_lnum_win(lnum, wp);
if (wlnum > 0)
changed_lines_buf(wp->w_buffer, wlnum,
lnume - lnum + wlnum, 0L);
}
}
#endif
changed_common(lnum, col, lnume, xtra);
}
static void
changed_lines_buf(
buf_T *buf,
linenr_T lnum,
linenr_T lnume,
long xtra)
{
if (buf->b_mod_set)
{
if (lnum < buf->b_mod_top)
buf->b_mod_top = lnum;
if (lnum < buf->b_mod_bot)
{
buf->b_mod_bot += xtra;
if (buf->b_mod_bot < lnum)
buf->b_mod_bot = lnum;
}
if (lnume + xtra > buf->b_mod_bot)
buf->b_mod_bot = lnume + xtra;
buf->b_mod_xlines += xtra;
}
else
{
buf->b_mod_set = TRUE;
buf->b_mod_top = lnum;
buf->b_mod_bot = lnume + xtra;
buf->b_mod_xlines = xtra;
}
}
static void
changed_common(
linenr_T lnum,
colnr_T col,
linenr_T lnume,
long xtra)
{
win_T *wp;
tabpage_T *tp;
int i;
#ifdef FEAT_JUMPLIST
int cols;
pos_T *p;
int add;
#endif
changed();
if (!cmdmod.keepjumps)
{
curbuf->b_last_change.lnum = lnum;
curbuf->b_last_change.col = col;
#ifdef FEAT_JUMPLIST
if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
{
if (curbuf->b_changelistlen == 0)
add = TRUE;
else
{
p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
if (p->lnum != lnum)
add = TRUE;
else
{
cols = comp_textwidth(FALSE);
if (cols == 0)
cols = 79;
add = (p->col + cols < col || col + cols < p->col);
}
}
if (add)
{
curbuf->b_new_change = FALSE;
if (curbuf->b_changelistlen == JUMPLISTSIZE)
{
curbuf->b_changelistlen = JUMPLISTSIZE - 1;
mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
sizeof(pos_T) * (JUMPLISTSIZE - 1));
FOR_ALL_TAB_WINDOWS(tp, wp)
{
if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
--wp->w_changelistidx;
}
}
FOR_ALL_TAB_WINDOWS(tp, wp)
{
if (wp->w_buffer == curbuf
&& wp->w_changelistidx == curbuf->b_changelistlen)
++wp->w_changelistidx;
}
++curbuf->b_changelistlen;
}
}
curbuf->b_changelist[curbuf->b_changelistlen - 1] =
curbuf->b_last_change;
curwin->w_changelistidx = curbuf->b_changelistlen;
#endif
}
FOR_ALL_TAB_WINDOWS(tp, wp)
{
if (wp->w_buffer == curbuf)
{
if (wp->w_redr_type < VALID)
wp->w_redr_type = VALID;
#ifdef FEAT_FOLDING
foldUpdate(wp, lnum, lnume + xtra - 1);
i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
if (wp->w_cursor.lnum == lnum)
wp->w_cline_folded = i;
i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
if (wp->w_cursor.lnum == lnume)
wp->w_cline_folded = i;
if (wp->w_cursor.lnum <= lnum)
{
i = find_wl_entry(wp, lnum);
if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
changed_line_abv_curs_win(wp);
}
#endif
if (wp->w_cursor.lnum > lnum)
changed_line_abv_curs_win(wp);
else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
changed_cline_bef_curs_win(wp);
if (wp->w_botline >= lnum)
{
approximate_botline_win(wp);
}
for (i = 0; i < wp->w_lines_valid; ++i)
if (wp->w_lines[i].wl_valid)
{
if (wp->w_lines[i].wl_lnum >= lnum)
{
if (wp->w_lines[i].wl_lnum < lnume)
{
wp->w_lines[i].wl_valid = FALSE;
}
else if (xtra != 0)
{
wp->w_lines[i].wl_lnum += xtra;
#ifdef FEAT_FOLDING
wp->w_lines[i].wl_lastlnum += xtra;
#endif
}
}
#ifdef FEAT_FOLDING
else if (wp->w_lines[i].wl_lastlnum >= lnum)
{
wp->w_lines[i].wl_valid = FALSE;
}
#endif
}
#ifdef FEAT_FOLDING
if (hasAnyFolding(wp))
set_topline(wp, wp->w_topline);
#endif
if (wp->w_p_rnu)
redraw_win_later(wp, SOME_VALID);
}
}
if (must_redraw < VALID)
must_redraw = VALID;
#ifdef FEAT_AUTOCMD
if (lnum <= curwin->w_cursor.lnum
&& lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
last_cursormoved.lnum = 0;
#endif
}
void
unchanged(
buf_T *buf,
int ff)
{
if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
{
buf->b_changed = 0;
ml_setflags(buf);
if (ff)
save_file_ff(buf);
check_status(buf);
redraw_tabline = TRUE;
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
}
++CHANGEDTICK(buf);
#ifdef FEAT_NETBEANS_INTG
netbeans_unmodified(buf);
#endif
}
void
check_status(buf_T *buf)
{
win_T *wp;
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer == buf && wp->w_status_height)
{
wp->w_redr_status = TRUE;
if (must_redraw < VALID)
must_redraw = VALID;
}
}
void
change_warning(
int col)
{
static char *w_readonly = N_("W10: Warning: Changing a readonly file");
if (curbuf->b_did_warn == FALSE
&& curbufIsChanged() == 0
#ifdef FEAT_AUTOCMD
&& !autocmd_busy
#endif
&& curbuf->b_p_ro)
{
#ifdef FEAT_AUTOCMD
++curbuf_lock;
apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
--curbuf_lock;
if (!curbuf->b_p_ro)
return;
#endif
msg_start();
if (msg_row == Rows - 1)
msg_col = col;
msg_source(HL_ATTR(HLF_W));
MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
#ifdef FEAT_EVAL
set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
#endif
msg_clr_eos();
(void)msg_end();
if (msg_silent == 0 && !silent_mode
#ifdef FEAT_EVAL
&& time_for_testing != 1
#endif
)
{
out_flush();
ui_delay(1000L, TRUE);
}
curbuf->b_did_warn = TRUE;
redraw_cmdline = FALSE;
if (msg_row < Rows - 1)
showmode();
}
}
int
ask_yesno(char_u *str, int direct)
{
int r = ' ';
int save_State = State;
if (exiting)
settmode(TMODE_RAW);
++no_wait_return;
#ifdef USE_ON_FLY_SCROLL
dont_scroll = TRUE;
#endif
State = CONFIRM;
#ifdef FEAT_MOUSE
setmouse();
#endif
++no_mapping;
++allow_keys;
while (r != 'y' && r != 'n')
{
smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
if (direct)
r = get_keystroke();
else
r = plain_vgetc();
if (r == Ctrl_C || r == ESC)
r = 'n';
msg_putchar(r);
out_flush();
}
--no_wait_return;
State = save_State;
#ifdef FEAT_MOUSE
setmouse();
#endif
--no_mapping;
--allow_keys;
return r;
}
#if defined(FEAT_MOUSE) || defined(PROTO)
int
is_mouse_key(int c)
{
return c == K_LEFTMOUSE
|| c == K_LEFTMOUSE_NM
|| c == K_LEFTDRAG
|| c == K_LEFTRELEASE
|| c == K_LEFTRELEASE_NM
|| c == K_MIDDLEMOUSE
|| c == K_MIDDLEDRAG
|| c == K_MIDDLERELEASE
|| c == K_RIGHTMOUSE
|| c == K_RIGHTDRAG
|| c == K_RIGHTRELEASE
|| c == K_MOUSEDOWN
|| c == K_MOUSEUP
|| c == K_MOUSELEFT
|| c == K_MOUSERIGHT
|| c == K_X1MOUSE
|| c == K_X1DRAG
|| c == K_X1RELEASE
|| c == K_X2MOUSE
|| c == K_X2DRAG
|| c == K_X2RELEASE;
}
#endif
int
get_keystroke(void)
{
char_u *buf = NULL;
int buflen = 150;
int maxlen;
int len = 0;
int n;
int save_mapped_ctrl_c = mapped_ctrl_c;
int waited = 0;
mapped_ctrl_c = FALSE;
for (;;)
{
cursor_on();
out_flush();
maxlen = (buflen - 6 - len) / 3;
if (buf == NULL)
buf = alloc(buflen);
else if (maxlen < 10)
{
char_u *t_buf = buf;
buflen += 100;
buf = vim_realloc(buf, buflen);
if (buf == NULL)
vim_free(t_buf);
maxlen = (buflen - 6 - len) / 3;
}
if (buf == NULL)
{
do_outofmem_msg((long_u)buflen);
return ESC;
}
n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
if (n > 0)
{
n = fix_input_buffer(buf + len, n);
len += n;
waited = 0;
}
else if (len > 0)
++waited;
if ((n = check_termcode(1, buf, buflen, &len)) < 0
&& (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
continue;
if (n == KEYLEN_REMOVED)
{
if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
{
update_screen(0);
setcursor();
}
continue;
}
if (n > 0)
len = n;
if (len == 0)
continue;
n = buf[0];
if (n == K_SPECIAL)
{
n = TO_SPECIAL(buf[1], buf[2]);
if (buf[1] == KS_MODIFIER
|| n == K_IGNORE
#ifdef FEAT_MOUSE
|| (is_mouse_key(n) && n != K_LEFTMOUSE)
#endif
#ifdef FEAT_GUI
|| n == K_VER_SCROLLBAR
|| n == K_HOR_SCROLLBAR
#endif
)
{
if (buf[1] == KS_MODIFIER)
mod_mask = buf[2];
len -= 3;
if (len > 0)
mch_memmove(buf, buf + 3, (size_t)len);
continue;
}
break;
}
#ifdef FEAT_MBYTE
if (has_mbyte)
{
if (MB_BYTE2LEN(n) > len)
continue;
buf[len >= buflen ? buflen - 1 : len] = NUL;
n = (*mb_ptr2char)(buf);
}
#endif
#ifdef UNIX
if (n == intr_char)
n = ESC;
#endif
break;
}
vim_free(buf);
mapped_ctrl_c = save_mapped_ctrl_c;
return n;
}
int
get_number(
int colon,
int *mouse_used)
{
int n = 0;
int c;
int typed = 0;
if (mouse_used != NULL)
*mouse_used = FALSE;
if (msg_silent != 0)
return 0;
#ifdef USE_ON_FLY_SCROLL
dont_scroll = TRUE;
#endif
++no_mapping;
++allow_keys;
for (;;)
{
windgoto(msg_row, msg_col);
c = safe_vgetc();
if (VIM_ISDIGIT(c))
{
n = n * 10 + c - '0';
msg_putchar(c);
++typed;
}
else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
{
if (typed > 0)
{
MSG_PUTS("\b \b");
--typed;
}
n /= 10;
}
#ifdef FEAT_MOUSE
else if (mouse_used != NULL && c == K_LEFTMOUSE)
{
*mouse_used = TRUE;
n = mouse_row + 1;
break;
}
#endif
else if (n == 0 && c == ':' && colon)
{
stuffcharReadbuff(':');
if (!exmode_active)
cmdline_row = msg_row;
skip_redraw = TRUE;
do_redraw = FALSE;
break;
}
else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
break;
}
--no_mapping;
--allow_keys;
return n;
}
int
prompt_for_number(int *mouse_used)
{
int i;
int save_cmdline_row;
int save_State;
if (mouse_used != NULL)
MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
else
MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
save_cmdline_row = cmdline_row;
cmdline_row = 0;
save_State = State;
State = ASKMORE;
i = get_number(TRUE, mouse_used);
if (KeyTyped)
{
cmdline_row = msg_row - 1;
need_wait_return = FALSE;
msg_didany = FALSE;
msg_didout = FALSE;
}
else
cmdline_row = save_cmdline_row;
State = save_State;
return i;
}
void
msgmore(long n)
{
long pn;
if (global_busy
|| !messaging())
return;
if (keep_msg != NULL && !keep_msg_more)
return;
if (n > 0)
pn = n;
else
pn = -n;
if (pn > p_report)
{
if (pn == 1)
{
if (n > 0)
vim_strncpy(msg_buf, (char_u *)_("1 more line"),
MSG_BUF_LEN - 1);
else
vim_strncpy(msg_buf, (char_u *)_("1 line less"),
MSG_BUF_LEN - 1);
}
else
{
if (n > 0)
vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
_("%ld more lines"), pn);
else
vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
_("%ld fewer lines"), pn);
}
if (got_int)
vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
if (msg(msg_buf))
{
set_keep_msg(msg_buf, 0);
keep_msg_more = TRUE;
}
}
}
void
beep_flush(void)
{
if (emsg_silent == 0)
{
flush_buffers(FALSE);
vim_beep(BO_ERROR);
}
}
void
vim_beep(
unsigned val)
{
if (emsg_silent == 0)
{
if (!((bo_flags & val) || (bo_flags & BO_ALL)))
{
#ifdef ELAPSED_FUNC
static int did_init = FALSE;
static ELAPSED_TYPE start_tv;
if (!did_init || ELAPSED_FUNC(start_tv) > 500)
{
did_init = TRUE;
ELAPSED_INIT(start_tv);
#endif
if (p_vb
#ifdef FEAT_GUI
&& !(gui.in_use && gui.starting)
#endif
)
out_str_cf(T_VB);
else
out_char(BELL);
#ifdef ELAPSED_FUNC
}
#endif
}
if (vim_strchr(p_debug, 'e') != NULL)
{
msg_source(HL_ATTR(HLF_W));
msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
}
}
}
static char_u *homedir = NULL;
void
init_homedir(void)
{
char_u *var;
vim_free(homedir);
homedir = NULL;
#ifdef VMS
var = mch_getenv((char_u *)"SYS$LOGIN");
#else
var = mch_getenv((char_u *)"HOME");
#endif
#ifdef WIN3264
if (var == NULL || *var == NUL)
{
char_u *homedrive, *homepath;
homedrive = mch_getenv((char_u *)"HOMEDRIVE");
homepath = mch_getenv((char_u *)"HOMEPATH");
if (homepath == NULL || *homepath == NUL)
homepath = (char_u *)"\\";
if (homedrive != NULL
&& STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
{
sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
if (NameBuff[0] != NUL)
var = NameBuff;
}
}
if (var == NULL)
var = mch_getenv((char_u *)"USERPROFILE");
if (var != NULL && *var == '%')
{
char_u *p;
char_u *exp;
p = vim_strchr(var + 1, '%');
if (p != NULL)
{
vim_strncpy(NameBuff, var + 1, p - (var + 1));
exp = mch_getenv(NameBuff);
if (exp != NULL && *exp != NUL
&& STRLEN(exp) + STRLEN(p) < MAXPATHL)
{
vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
var = NameBuff;
}
}
}
if (var != NULL && *var == NUL)
var = NULL;
# ifdef FEAT_MBYTE
if (enc_utf8 && var != NULL)
{
int len;
char_u *pp = NULL;
acp_to_enc(var, (int)STRLEN(var), &pp, &len);
if (pp != NULL)
{
homedir = pp;
return;
}
}
# endif
if (var == NULL)
var = (char_u *)"C:/";
#endif
if (var != NULL)
{
#ifdef UNIX
if (mch_dirname(NameBuff, MAXPATHL) == OK
&& mch_chdir((char *)NameBuff) == 0)
{
if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
var = IObuff;
if (mch_chdir((char *)NameBuff) != 0)
EMSG(_(e_prev_dir));
}
#endif
homedir = vim_strsave(var);
}
}
#if defined(EXITFREE) || defined(PROTO)
void
free_homedir(void)
{
vim_free(homedir);
}
# ifdef FEAT_CMDL_COMPL
void
free_users(void)
{
ga_clear_strings(&ga_users);
}
# endif
#endif
char_u *
expand_env_save(char_u *src)
{
return expand_env_save_opt(src, FALSE);
}
char_u *
expand_env_save_opt(char_u *src, int one)
{
char_u *p;
p = alloc(MAXPATHL);
if (p != NULL)
expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
return p;
}
void
expand_env(
char_u *src,
char_u *dst,
int dstlen)
{
expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
}
void
expand_env_esc(
char_u *srcp,
char_u *dst,
int dstlen,
int esc,
int one,
char_u *startstr)
{
char_u *src;
char_u *tail;
int c;
char_u *var;
int copy_char;
int mustfree;
int at_start = TRUE;
int startstr_len = 0;
if (startstr != NULL)
startstr_len = (int)STRLEN(startstr);
src = skipwhite(srcp);
--dstlen;
while (*src && dstlen > 0)
{
#ifdef FEAT_EVAL
if (src[0] == '`' && src[1] == '=')
{
size_t len;
var = src;
src += 2;
(void)skip_expr(&src);
if (*src == '`')
++src;
len = src - var;
if (len > (size_t)dstlen)
len = dstlen;
vim_strncpy(dst, var, len);
dst += len;
dstlen -= (int)len;
continue;
}
#endif
copy_char = TRUE;
if ((*src == '$'
#ifdef VMS
&& at_start
#endif
)
#if defined(MSWIN)
|| *src == '%'
#endif
|| (*src == '~' && at_start))
{
mustfree = FALSE;
if (*src != '~')
{
tail = src + 1;
var = dst;
c = dstlen - 1;
#ifdef UNIX
if (*tail == '{' && !vim_isIDc('{'))
{
tail++;
while (c-- > 0 && *tail && *tail != '}')
*var++ = *tail++;
}
else
#endif
{
while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
#if defined(MSWIN)
|| (*src == '%' && *tail != '%')
#endif
))
{
*var++ = *tail++;
}
}
#if defined(MSWIN) || defined(UNIX)
# ifdef UNIX
if (src[1] == '{' && *tail != '}')
# else
if (*src == '%' && *tail != '%')
# endif
var = NULL;
else
{
# ifdef UNIX
if (src[1] == '{')
# else
if (*src == '%')
#endif
++tail;
#endif
*var = NUL;
var = vim_getenv(dst, &mustfree);
#if defined(MSWIN) || defined(UNIX)
}
#endif
}
else if ( src[1] == NUL
|| vim_ispathsep(src[1])
|| vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
{
var = homedir;
tail = src + 1;
}
else
{
#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
tail = src;
var = dst;
c = dstlen - 1;
while ( c-- > 0
&& *tail
&& vim_isfilec(*tail)
&& !vim_ispathsep(*tail))
*var++ = *tail++;
*var = NUL;
# ifdef UNIX
# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
{
struct passwd *pw = (*dst == NUL)
? NULL : getpwnam((char *)dst + 1);
var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
}
if (var == NULL)
# endif
{
expand_T xpc;
ExpandInit(&xpc);
xpc.xp_context = EXPAND_FILES;
var = ExpandOne(&xpc, dst, NULL,
WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
mustfree = TRUE;
}
# else
{
char_u test[MAXPATHL], paths[MAXPATHL];
char_u *path, *next_path, *ptr;
stat_T st;
STRCPY(paths, USER_HOME);
next_path = paths;
while (*next_path)
{
for (path = next_path; *next_path && *next_path != ',';
next_path++);
if (*next_path)
*next_path++ = NUL;
STRCPY(test, path);
STRCAT(test, "/");
STRCAT(test, dst + 1);
if (mch_stat(test, &st) == 0)
{
var = alloc(STRLEN(test) + 1);
STRCPY(var, test);
mustfree = TRUE;
break;
}
}
}
# endif
#else
var = NULL;
tail = (char_u *)"";
#endif
}
#ifdef BACKSLASH_IN_FILENAME
if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
{
char_u *p = vim_strsave(var);
if (p != NULL)
{
if (mustfree)
vim_free(var);
var = p;
mustfree = TRUE;
forward_slash(var);
}
}
#endif
if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
{
char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
if (p != NULL)
{
if (mustfree)
vim_free(var);
var = p;
mustfree = TRUE;
}
}
if (var != NULL && *var != NUL
&& (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
{
STRCPY(dst, var);
dstlen -= (int)STRLEN(var);
c = (int)STRLEN(var);
if (*var != NUL && after_pathsep(dst, dst + c)
#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
&& dst[-1] != ':'
#endif
&& vim_ispathsep(*tail))
++tail;
dst += c;
src = tail;
copy_char = FALSE;
}
if (mustfree)
vim_free(var);
}
if (copy_char)
{
at_start = FALSE;
if (src[0] == '\\' && src[1] != NUL)
{
*dst++ = *src++;
--dstlen;
}
else if ((src[0] == ' ' || src[0] == ',') && !one)
at_start = TRUE;
if (dstlen > 0)
{
*dst++ = *src++;
--dstlen;
if (startstr != NULL && src - startstr_len >= srcp
&& STRNCMP(src - startstr_len, startstr,
startstr_len) == 0)
at_start = TRUE;
}
}
}
*dst = NUL;
}
char_u *
vim_getenv(char_u *name, int *mustfree)
{
char_u *p;
char_u *pend;
int vimruntime;
#if defined(MSWIN)
if (STRCMP(name, "HOME") == 0)
return homedir;
#endif
p = mch_getenv(name);
if (p != NULL && *p == NUL)
p = NULL;
if (p != NULL)
{
#if defined(FEAT_MBYTE) && defined(WIN3264)
if (enc_utf8)
{
int len;
char_u *pp = NULL;
acp_to_enc(p, (int)STRLEN(p), &pp, &len);
if (pp != NULL)
{
p = pp;
*mustfree = TRUE;
}
}
#endif
return p;
}
vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
if (!vimruntime && STRCMP(name, "VIM") != 0)
return NULL;
if (vimruntime
#ifdef HAVE_PATHDEF
&& *default_vimruntime_dir == NUL
#endif
)
{
p = mch_getenv((char_u *)"VIM");
if (p != NULL && *p == NUL)
p = NULL;
if (p != NULL)
{
p = vim_version_dir(p);
if (p != NULL)
*mustfree = TRUE;
else
p = mch_getenv((char_u *)"VIM");
#if defined(FEAT_MBYTE) && defined(WIN3264)
if (enc_utf8)
{
int len;
char_u *pp = NULL;
acp_to_enc(p, (int)STRLEN(p), &pp, &len);
if (pp != NULL)
{
if (*mustfree)
vim_free(p);
p = pp;
*mustfree = TRUE;
}
}
#endif
}
}
if (p == NULL)
{
if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
p = p_hf;
#ifdef USE_EXE_NAME
else
p = exe_name;
#endif
if (p != NULL)
{
pend = gettail(p);
if (p == p_hf)
pend = remove_tail(p, pend, (char_u *)"doc");
#ifdef USE_EXE_NAME
# ifdef MACOS_X
if (p == exe_name)
{
char_u *pend1;
char_u *pnew;
pend1 = remove_tail(p, pend, (char_u *)"MacOS");
if (pend1 != pend)
{
pnew = alloc((unsigned)(pend1 - p) + 15);
if (pnew != NULL)
{
STRNCPY(pnew, p, (pend1 - p));
STRCPY(pnew + (pend1 - p), "Resources/vim");
p = pnew;
pend = p + STRLEN(p);
}
}
}
# endif
if (p == exe_name)
pend = remove_tail(p, pend, (char_u *)"src");
#endif
if (!vimruntime)
{
pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
}
if (pend > p && after_pathsep(p, pend))
--pend;
#ifdef MACOS_X
if (p == exe_name || p == p_hf)
#endif
p = vim_strnsave(p, (int)(pend - p));
if (p != NULL && !mch_isdir(p))
{
vim_free(p);
p = NULL;
}
else
{
#ifdef USE_EXE_NAME
if (vimruntime && (pend = vim_version_dir(p)) != NULL)
{
vim_free(p);
p = pend;
}
#endif
*mustfree = TRUE;
}
}
}
#ifdef HAVE_PATHDEF
if (p == NULL)
{
if (vimruntime && *default_vimruntime_dir != NUL)
{
p = default_vimruntime_dir;
*mustfree = FALSE;
}
else if (*default_vim_dir != NUL)
{
if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
*mustfree = TRUE;
else
{
p = default_vim_dir;
*mustfree = FALSE;
}
}
}
#endif
if (p != NULL)
{
if (vimruntime)
{
vim_setenv((char_u *)"VIMRUNTIME", p);
didset_vimruntime = TRUE;
}
else
{
vim_setenv((char_u *)"VIM", p);
didset_vim = TRUE;
}
}
return p;
}
static char_u *
vim_version_dir(char_u *vimdir)
{
char_u *p;
if (vimdir == NULL || *vimdir == NUL)
return NULL;
p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
if (p != NULL && mch_isdir(p))
return p;
vim_free(p);
p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
if (p != NULL && mch_isdir(p))
return p;
vim_free(p);
return NULL;
}
static char_u *
remove_tail(char_u *p, char_u *pend, char_u *name)
{
int len = (int)STRLEN(name) + 1;
char_u *newend = pend - len;
if (newend >= p
&& fnamencmp(newend, name, len - 1) == 0
&& (newend == p || after_pathsep(p, newend)))
return newend;
return pend;
}
void
vim_setenv(char_u *name, char_u *val)
{
#ifdef HAVE_SETENV
mch_setenv((char *)name, (char *)val, 1);
#else
char_u *envbuf;
envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
if (envbuf != NULL)
{
sprintf((char *)envbuf, "%s=%s", name, val);
putenv((char *)envbuf);
}
#endif
#ifdef FEAT_GETTEXT
if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
{
char_u *buf = concat_str(val, (char_u *)"/lang");
if (buf != NULL)
{
bindtextdomain(VIMPACKAGE, (char *)buf);
vim_free(buf);
}
}
#endif
}
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
char_u *
get_env_name(
expand_T *xp UNUSED,
int idx)
{
# if defined(AMIGA)
return NULL;
# else
# ifndef __WIN32__
extern char **environ;
# endif
# define ENVNAMELEN 100
static char_u name[ENVNAMELEN];
char_u *str;
int n;
str = (char_u *)environ[idx];
if (str == NULL)
return NULL;
for (n = 0; n < ENVNAMELEN - 1; ++n)
{
if (str[n] == '=' || str[n] == NUL)
break;
name[n] = str[n];
}
name[n] = NUL;
return name;
# endif
}
static void
init_users(void)
{
static int lazy_init_done = FALSE;
if (lazy_init_done)
return;
lazy_init_done = TRUE;
ga_init2(&ga_users, sizeof(char_u *), 20);
# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
{
char_u* user;
struct passwd* pw;
setpwent();
while ((pw = getpwent()) != NULL)
if (pw->pw_name != NULL)
{
if (ga_grow(&ga_users, 1) == FAIL)
break;
user = vim_strsave((char_u*)pw->pw_name);
if (user == NULL)
break;
((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
}
endpwent();
}
# endif
}
char_u*
get_users(expand_T *xp UNUSED, int idx)
{
init_users();
if (idx < ga_users.ga_len)
return ((char_u **)ga_users.ga_data)[idx];
return NULL;
}
int match_user(char_u* name)
{
int i;
int n = (int)STRLEN(name);
int result = 0;
init_users();
for (i = 0; i < ga_users.ga_len; i++)
{
if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
return 2;
if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
result = 1;
}
return result;
}
#endif
void
home_replace(
buf_T *buf,
char_u *src,
char_u *dst,
int dstlen,
int one)
{
size_t dirlen = 0, envlen = 0;
size_t len;
char_u *homedir_env, *homedir_env_orig;
char_u *p;
if (src == NULL)
{
*dst = NUL;
return;
}
if (buf != NULL && buf->b_help)
{
vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
return;
}
if (homedir != NULL)
dirlen = STRLEN(homedir);
#ifdef VMS
homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
#else
homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
#endif
#ifdef WIN3264
if (homedir_env == NULL)
homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
#endif
if (homedir_env != NULL && *homedir_env == NUL)
homedir_env = NULL;
#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
{
int usedlen = 0;
int flen;
char_u *fbuf = NULL;
flen = (int)STRLEN(homedir_env);
(void)modify_fname((char_u *)":p", &usedlen,
&homedir_env, &fbuf, &flen);
flen = (int)STRLEN(homedir_env);
if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
homedir_env[flen - 1] = NUL;
}
#endif
if (homedir_env != NULL)
envlen = STRLEN(homedir_env);
if (!one)
src = skipwhite(src);
while (*src && dstlen > 0)
{
p = homedir;
len = dirlen;
for (;;)
{
if ( len
&& fnamencmp(src, p, len) == 0
&& (vim_ispathsep(src[len])
|| (!one && (src[len] == ',' || src[len] == ' '))
|| src[len] == NUL))
{
src += len;
if (--dstlen > 0)
*dst++ = '~';
if (!vim_ispathsep(src[0]) && --dstlen > 0)
*dst++ = '/';
break;
}
if (p == homedir_env)
break;
p = homedir_env;
len = envlen;
}
while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
*dst++ = *src++;
while ((*src == ' ' || *src == ',') && --dstlen > 0)
*dst++ = *src++;
}
*dst = NUL;
if (homedir_env != homedir_env_orig)
vim_free(homedir_env);
}
char_u *
home_replace_save(
buf_T *buf,
char_u *src)
{
char_u *dst;
unsigned len;
len = 3;
if (src != NULL)
len += (unsigned)STRLEN(src);
dst = alloc(len);
if (dst != NULL)
home_replace(buf, src, dst, len, TRUE);
return dst;
}
int
fullpathcmp(
char_u *s1,
char_u *s2,
int checkname)
{
#ifdef UNIX
char_u exp1[MAXPATHL];
char_u full1[MAXPATHL];
char_u full2[MAXPATHL];
stat_T st1, st2;
int r1, r2;
expand_env(s1, exp1, MAXPATHL);
r1 = mch_stat((char *)exp1, &st1);
r2 = mch_stat((char *)s2, &st2);
if (r1 != 0 && r2 != 0)
{
if (checkname)
{
if (fnamecmp(exp1, s2) == 0)
return FPC_SAMEX;
r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
return FPC_SAMEX;
}
return FPC_NOTX;
}
if (r1 != 0 || r2 != 0)
return FPC_DIFFX;
if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
return FPC_SAME;
return FPC_DIFF;
#else
char_u *exp1;
char_u *full1;
char_u *full2;
int retval = FPC_DIFF;
int r1, r2;
if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
{
full1 = exp1 + MAXPATHL;
full2 = full1 + MAXPATHL;
expand_env(s1, exp1, MAXPATHL);
r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
if (r1 != OK && r2 != OK)
{
if (checkname && fnamecmp(exp1, s2) == 0)
retval = FPC_SAMEX;
else
retval = FPC_NOTX;
}
else if (r1 != OK || r2 != OK)
retval = FPC_DIFFX;
else if (fnamecmp(full1, full2))
retval = FPC_DIFF;
else
retval = FPC_SAME;
vim_free(exp1);
}
return retval;
#endif
}
char_u *
gettail(char_u *fname)
{
char_u *p1, *p2;
if (fname == NULL)
return (char_u *)"";
for (p1 = p2 = get_past_head(fname); *p2; )
{
if (vim_ispathsep_nocolon(*p2))
p1 = p2 + 1;
MB_PTR_ADV(p2);
}
return p1;
}
#if defined(FEAT_SEARCHPATH)
static char_u *gettail_dir(char_u *fname);
static char_u *
gettail_dir(char_u *fname)
{
char_u *dir_end = fname;
char_u *next_dir_end = fname;
int look_for_sep = TRUE;
char_u *p;
for (p = fname; *p != NUL; )
{
if (vim_ispathsep(*p))
{
if (look_for_sep)
{
next_dir_end = p;
look_for_sep = FALSE;
}
}
else
{
if (!look_for_sep)
dir_end = next_dir_end;
look_for_sep = TRUE;
}
MB_PTR_ADV(p);
}
return dir_end;
}
#endif
char_u *
gettail_sep(char_u *fname)
{
char_u *p;
char_u *t;
p = get_past_head(fname);
t = gettail(fname);
while (t > p && after_pathsep(fname, t))
--t;
#ifdef VMS
++t;
#endif
return t;
}
char_u *
getnextcomp(char_u *fname)
{
while (*fname && !vim_ispathsep(*fname))
MB_PTR_ADV(fname);
if (*fname)
++fname;
return fname;
}
char_u *
get_past_head(char_u *path)
{
char_u *retval;
#if defined(MSWIN)
if (isalpha(path[0]) && path[1] == ':')
retval = path + 2;
else
retval = path;
#else
# if defined(AMIGA)
retval = vim_strchr(path, ':');
if (retval == NULL)
retval = path;
# else
retval = path;
# endif
#endif
while (vim_ispathsep(*retval))
++retval;
return retval;
}
int
vim_ispathsep(int c)
{
#ifdef UNIX
return (c == '/');
#else
# ifdef BACKSLASH_IN_FILENAME
return (c == ':' || c == '/' || c == '\\');
# else
# ifdef VMS
return (c == ':' || c == '[' || c == ']' || c == '/'
|| c == '<' || c == '>' || c == '"' );
# else
return (c == ':' || c == '/');
# endif
# endif
#endif
}
int
vim_ispathsep_nocolon(int c)
{
return vim_ispathsep(c)
#ifdef BACKSLASH_IN_FILENAME
&& c != ':'
#endif
;
}
#if defined(FEAT_SEARCHPATH) || defined(PROTO)
int
vim_ispathlistsep(int c)
{
#ifdef UNIX
return (c == ':');
#else
return (c == ';');
#endif
}
#endif
void
shorten_dir(char_u *str)
{
char_u *tail, *s, *d;
int skip = FALSE;
tail = gettail(str);
d = str;
for (s = str; ; ++s)
{
if (s >= tail)
{
*d++ = *s;
if (*s == NUL)
break;
}
else if (vim_ispathsep(*s))
{
*d++ = *s;
skip = FALSE;
}
else if (!skip)
{
*d++ = *s;
if (*s != '~' && *s != '.')
skip = TRUE;
# ifdef FEAT_MBYTE
if (has_mbyte)
{
int l = mb_ptr2len(s);
while (--l > 0)
*d++ = *++s;
}
# endif
}
}
}
int
dir_of_file_exists(char_u *fname)
{
char_u *p;
int c;
int retval;
p = gettail_sep(fname);
if (p == fname)
return TRUE;
c = *p;
*p = NUL;
retval = mch_isdir(fname);
*p = c;
return retval;
}
int
vim_fnamecmp(char_u *x, char_u *y)
{
#ifdef BACKSLASH_IN_FILENAME
return vim_fnamencmp(x, y, MAXPATHL);
#else
if (p_fic)
return MB_STRICMP(x, y);
return STRCMP(x, y);
#endif
}
int
vim_fnamencmp(char_u *x, char_u *y, size_t len)
{
#ifdef BACKSLASH_IN_FILENAME
char_u *px = x;
char_u *py = y;
int cx = NUL;
int cy = NUL;
while (len > 0)
{
cx = PTR2CHAR(px);
cy = PTR2CHAR(py);
if (cx == NUL || cy == NUL
|| ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
&& !(cx == '/' && cy == '\\')
&& !(cx == '\\' && cy == '/')))
break;
len -= MB_PTR2LEN(px);
px += MB_PTR2LEN(px);
py += MB_PTR2LEN(py);
}
if (len == 0)
return 0;
return (cx - cy);
#else
if (p_fic)
return MB_STRNICMP(x, y, len);
return STRNCMP(x, y, len);
#endif
}
char_u *
concat_fnames(char_u *fname1, char_u *fname2, int sep)
{
char_u *dest;
dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
if (dest != NULL)
{
STRCPY(dest, fname1);
if (sep)
add_pathsep(dest);
STRCAT(dest, fname2);
}
return dest;
}
char_u *
concat_str(char_u *str1, char_u *str2)
{
char_u *dest;
size_t l = STRLEN(str1);
dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
if (dest != NULL)
{
STRCPY(dest, str1);
STRCPY(dest + l, str2);
}
return dest;
}
void
add_pathsep(char_u *p)
{
if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
STRCAT(p, PATHSEPSTR);
}
char_u *
FullName_save(
char_u *fname,
int force)
{
char_u *buf;
char_u *new_fname = NULL;
if (fname == NULL)
return NULL;
buf = alloc((unsigned)MAXPATHL);
if (buf != NULL)
{
if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
new_fname = vim_strsave(buf);
else
new_fname = vim_strsave(fname);
vim_free(buf);
}
return new_fname;
}
#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
static char_u *skip_string(char_u *p);
static pos_T *ind_find_start_comment(void);
static pos_T *ind_find_start_CORS(linenr_T *is_raw);
static pos_T *find_start_rawstring(int ind_maxcomment);
static pos_T *
ind_find_start_comment(void)
{
return find_start_comment(curbuf->b_ind_maxcomment);
}
pos_T *
find_start_comment(int ind_maxcomment)
{
pos_T *pos;
char_u *line;
char_u *p;
int cur_maxcomment = ind_maxcomment;
for (;;)
{
pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
if (pos == NULL)
break;
line = ml_get(pos->lnum);
for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
p = skip_string(p);
if ((colnr_T)(p - line) <= pos->col)
break;
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
if (cur_maxcomment <= 0)
{
pos = NULL;
break;
}
}
return pos;
}
static pos_T *
ind_find_start_CORS(linenr_T *is_raw)
{
static pos_T comment_pos_copy;
pos_T *comment_pos;
pos_T *rs_pos;
comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
if (comment_pos != NULL)
{
comment_pos_copy = *comment_pos;
comment_pos = &comment_pos_copy;
}
rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
if (comment_pos == NULL || (rs_pos != NULL
&& LT_POS(*rs_pos, *comment_pos)))
{
if (is_raw != NULL && rs_pos != NULL)
*is_raw = rs_pos->lnum;
return rs_pos;
}
return comment_pos;
}
static pos_T *
find_start_rawstring(int ind_maxcomment)
{
pos_T *pos;
char_u *line;
char_u *p;
int cur_maxcomment = ind_maxcomment;
for (;;)
{
pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
if (pos == NULL)
break;
line = ml_get(pos->lnum);
for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
p = skip_string(p);
if ((colnr_T)(p - line) <= pos->col)
break;
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
if (cur_maxcomment <= 0)
{
pos = NULL;
break;
}
}
return pos;
}
static char_u *
skip_string(char_u *p)
{
int i;
for ( ; ; ++p)
{
if (p[0] == '\'')
{
if (!p[1])
break;
i = 2;
if (p[1] == '\\')
{
++i;
while (vim_isdigit(p[i - 1]))
++i;
}
if (p[i] == '\'')
{
p += i;
continue;
}
}
else if (p[0] == '"')
{
for (++p; p[0]; ++p)
{
if (p[0] == '\\' && p[1] != NUL)
++p;
else if (p[0] == '"')
break;
}
if (p[0] == '"')
continue;
}
else if (p[0] == 'R' && p[1] == '"')
{
char_u *delim = p + 2;
char_u *paren = vim_strchr(delim, '(');
if (paren != NULL)
{
size_t delim_len = paren - delim;
for (p += 3; *p; ++p)
if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
&& p[delim_len + 1] == '"')
{
p += delim_len + 1;
break;
}
if (p[0] == '"')
continue;
}
}
break;
}
if (!*p)
--p;
return p;
}
#endif
#if defined(FEAT_CINDENT) || defined(PROTO)
void
do_c_expr_indent(void)
{
# ifdef FEAT_EVAL
if (*curbuf->b_p_inde != NUL)
fixthisline(get_expr_indent);
else
# endif
fixthisline(get_c_indent);
}
typedef struct {
int found;
lpos_T lpos;
} cpp_baseclass_cache_T;
static char_u *cin_skipcomment(char_u *);
static int cin_nocode(char_u *);
static pos_T *find_line_comment(void);
static int cin_has_js_key(char_u *text);
static int cin_islabel_skip(char_u **);
static int cin_isdefault(char_u *);
static char_u *after_label(char_u *l);
static int get_indent_nolabel(linenr_T lnum);
static int skip_label(linenr_T, char_u **pp);
static int cin_first_id_amount(void);
static int cin_get_equal_amount(linenr_T lnum);
static int cin_ispreproc(char_u *);
static int cin_iscomment(char_u *);
static int cin_islinecomment(char_u *);
static int cin_isterminated(char_u *, int, int);
static int cin_isinit(void);
static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
static int cin_isif(char_u *);
static int cin_iselse(char_u *);
static int cin_isdo(char_u *);
static int cin_iswhileofdo(char_u *, linenr_T);
static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
static int cin_iswhileofdo_end(int terminated);
static int cin_isbreak(char_u *);
static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
static int get_baseclass_amount(int col);
static int cin_ends_in(char_u *, char_u *, char_u *);
static int cin_starts_with(char_u *s, char *word);
static int cin_skip2pos(pos_T *trypos);
static pos_T *find_start_brace(void);
static pos_T *find_match_paren(int);
static pos_T *find_match_char(int c, int ind_maxparen);
static int corr_ind_maxparen(pos_T *startpos);
static int find_last_paren(char_u *l, int start, int end);
static int find_match(int lookfor, linenr_T ourscope);
static int cin_is_cpp_namespace(char_u *);
static char_u *
cin_skipcomment(char_u *s)
{
while (*s)
{
char_u *prev_s = s;
s = skipwhite(s);
if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
{
s += STRLEN(s);
break;
}
if (*s != '/')
break;
++s;
if (*s == '/')
{
s += STRLEN(s);
break;
}
if (*s != '*')
break;
for (++s; *s; ++s)
if (s[0] == '*' && s[1] == '/')
{
s += 2;
break;
}
}
return s;
}
static int
cin_nocode(char_u *s)
{
return *cin_skipcomment(s) == NUL;
}
static pos_T *
find_line_comment(void)
{
static pos_T pos;
char_u *line;
char_u *p;
pos = curwin->w_cursor;
while (--pos.lnum > 0)
{
line = ml_get(pos.lnum);
p = skipwhite(line);
if (cin_islinecomment(p))
{
pos.col = (int)(p - line);
return &pos;
}
if (*p != NUL)
break;
}
return NULL;
}
static int
cin_has_js_key(char_u *text)
{
char_u *s = skipwhite(text);
int quote = -1;
if (*s == '\'' || *s == '"')
{
quote = *s;
++s;
}
if (!vim_isIDc(*s))
return FALSE;
while (vim_isIDc(*s))
++s;
if (*s == quote)
++s;
s = cin_skipcomment(s);
return (*s == ':' && s[1] != ':');
}
static int
cin_islabel_skip(char_u **s)
{
if (!vim_isIDc(**s))
return FALSE;
while (vim_isIDc(**s))
(*s)++;
*s = cin_skipcomment(*s);
return (**s == ':' && *++*s != ':');
}
int
cin_islabel(void)
{
char_u *s;
s = cin_skipcomment(ml_get_curline());
if (cin_isdefault(s))
return FALSE;
if (cin_isscopedecl(s))
return FALSE;
if (cin_islabel_skip(&s))
{
pos_T cursor_save;
pos_T *trypos;
char_u *line;
cursor_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1)
{
--curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
if ((trypos = ind_find_start_CORS(NULL)) != NULL)
curwin->w_cursor = *trypos;
line = ml_get_curline();
if (cin_ispreproc(line))
continue;
if (*(line = cin_skipcomment(line)) == NUL)
continue;
curwin->w_cursor = cursor_save;
if (cin_isterminated(line, TRUE, FALSE)
|| cin_isscopedecl(line)
|| cin_iscase(line, TRUE)
|| (cin_islabel_skip(&line) && cin_nocode(line)))
return TRUE;
return FALSE;
}
curwin->w_cursor = cursor_save;
return TRUE;
}
return FALSE;
}
static int
cin_isinit(void)
{
char_u *s;
static char *skip[] = {"static", "public", "protected", "private"};
s = cin_skipcomment(ml_get_curline());
if (cin_starts_with(s, "typedef"))
s = cin_skipcomment(s + 7);
for (;;)
{
int i, l;
for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
{
l = (int)strlen(skip[i]);
if (cin_starts_with(s, skip[i]))
{
s = cin_skipcomment(s + l);
l = 0;
break;
}
}
if (l != 0)
break;
}
if (cin_starts_with(s, "enum"))
return TRUE;
if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
return TRUE;
return FALSE;
}
int
cin_iscase(
char_u *s,
int strict)
{
s = cin_skipcomment(s);
if (cin_starts_with(s, "case"))
{
for (s += 4; *s; ++s)
{
s = cin_skipcomment(s);
if (*s == ':')
{
if (s[1] == ':')
++s;
else
return TRUE;
}
if (*s == '\'' && s[1] && s[2] == '\'')
s += 2;
else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
return FALSE;
else if (*s == '"')
{
if (strict)
return FALSE;
else
return TRUE;
}
}
return FALSE;
}
if (cin_isdefault(s))
return TRUE;
return FALSE;
}
static int
cin_isdefault(char_u *s)
{
return (STRNCMP(s, "default", 7) == 0
&& *(s = cin_skipcomment(s + 7)) == ':'
&& s[1] != ':');
}
int
cin_isscopedecl(char_u *s)
{
int i;
s = cin_skipcomment(s);
if (STRNCMP(s, "public", 6) == 0)
i = 6;
else if (STRNCMP(s, "protected", 9) == 0)
i = 9;
else if (STRNCMP(s, "private", 7) == 0)
i = 7;
else
return FALSE;
return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
}
#define FIND_NAMESPACE_LIM 20
static int
cin_is_cpp_namespace(char_u *s)
{
char_u *p;
int has_name = FALSE;
int has_name_start = FALSE;
s = cin_skipcomment(s);
if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
{
p = cin_skipcomment(skipwhite(s + 9));
while (*p != NUL)
{
if (VIM_ISWHITE(*p))
{
has_name = TRUE;
p = cin_skipcomment(skipwhite(p));
}
else if (*p == '{')
{
break;
}
else if (vim_iswordc(*p))
{
has_name_start = TRUE;
if (has_name)
return FALSE;
++p;
}
else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
{
if (!has_name_start || has_name)
return FALSE;
p += 3;
}
else
{
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
static int
cin_is_cpp_extern_c(char_u *s)
{
char_u *p;
int has_string_literal = FALSE;
s = cin_skipcomment(s);
if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
{
p = cin_skipcomment(skipwhite(s + 6));
while (*p != NUL)
{
if (VIM_ISWHITE(*p))
{
p = cin_skipcomment(skipwhite(p));
}
else if (*p == '{')
{
break;
}
else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
{
if (has_string_literal)
return FALSE;
has_string_literal = TRUE;
p += 3;
}
else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
&& p[4] == '"')
{
if (has_string_literal)
return FALSE;
has_string_literal = TRUE;
p += 5;
}
else
{
return FALSE;
}
}
return has_string_literal ? TRUE : FALSE;
}
return FALSE;
}
static char_u *
after_label(char_u *l)
{
for ( ; *l; ++l)
{
if (*l == ':')
{
if (l[1] == ':')
++l;
else if (!cin_iscase(l + 1, FALSE))
break;
}
else if (*l == '\'' && l[1] && l[2] == '\'')
l += 2;
}
if (*l == NUL)
return NULL;
l = cin_skipcomment(l + 1);
if (*l == NUL)
return NULL;
return l;
}
static int
get_indent_nolabel (linenr_T lnum)
{
char_u *l;
pos_T fp;
colnr_T col;
char_u *p;
l = ml_get(lnum);
p = after_label(l);
if (p == NULL)
return 0;
fp.col = (colnr_T)(p - l);
fp.lnum = lnum;
getvcol(curwin, &fp, &col, NULL, NULL);
return (int)col;
}
static int
skip_label(linenr_T lnum, char_u **pp)
{
char_u *l;
int amount;
pos_T cursor_save;
cursor_save = curwin->w_cursor;
curwin->w_cursor.lnum = lnum;
l = ml_get_curline();
if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
{
amount = get_indent_nolabel(lnum);
l = after_label(ml_get_curline());
if (l == NULL)
l = ml_get_curline();
}
else
{
amount = get_indent();
l = ml_get_curline();
}
*pp = l;
curwin->w_cursor = cursor_save;
return amount;
}
static int
cin_first_id_amount(void)
{
char_u *line, *p, *s;
int len;
pos_T fp;
colnr_T col;
line = ml_get_curline();
p = skipwhite(line);
len = (int)(skiptowhite(p) - p);
if (len == 6 && STRNCMP(p, "static", 6) == 0)
{
p = skipwhite(p + 6);
len = (int)(skiptowhite(p) - p);
}
if (len == 6 && STRNCMP(p, "struct", 6) == 0)
p = skipwhite(p + 6);
else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
p = skipwhite(p + 4);
else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
|| (len == 6 && STRNCMP(p, "signed", 6) == 0))
{
s = skipwhite(p + len);
if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
|| (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
|| (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
|| (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
p = s;
}
for (len = 0; vim_isIDc(p[len]); ++len)
;
if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
return 0;
p = skipwhite(p + len);
fp.lnum = curwin->w_cursor.lnum;
fp.col = (colnr_T)(p - line);
getvcol(curwin, &fp, &col, NULL, NULL);
return (int)col;
}
static int
cin_get_equal_amount(linenr_T lnum)
{
char_u *line;
char_u *s;
colnr_T col;
pos_T fp;
if (lnum > 1)
{
line = ml_get(lnum - 1);
if (*line != NUL && line[STRLEN(line) - 1] == '\\')
return -1;
}
line = s = ml_get(lnum);
while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
{
if (cin_iscomment(s))
s = cin_skipcomment(s);
else
++s;
}
if (*s != '=')
return 0;
s = skipwhite(s + 1);
if (cin_nocode(s))
return 0;
if (*s == '"')
++s;
fp.lnum = lnum;
fp.col = (colnr_T)(s - line);
getvcol(curwin, &fp, &col, NULL, NULL);
return (int)col;
}
static int
cin_ispreproc(char_u *s)
{
if (*skipwhite(s) == '#')
return TRUE;
return FALSE;
}
static int
cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
{
char_u *line = *pp;
linenr_T lnum = *lnump;
int retval = FALSE;
int candidate_amount = *amount;
if (*line != NUL && line[STRLEN(line) - 1] == '\\')
candidate_amount = get_indent_lnum(lnum);
for (;;)
{
if (cin_ispreproc(line))
{
retval = TRUE;
*lnump = lnum;
break;
}
if (lnum == 1)
break;
line = ml_get(--lnum);
if (*line == NUL || line[STRLEN(line) - 1] != '\\')
break;
}
if (lnum != *lnump)
*pp = ml_get(*lnump);
if (retval)
*amount = candidate_amount;
return retval;
}
static int
cin_iscomment(char_u *p)
{
return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
}
static int
cin_islinecomment(char_u *p)
{
return (p[0] == '/' && p[1] == '/');
}
static int
cin_isterminated(
char_u *s,
int incl_open,
int incl_comma)
{
char_u found_start = 0;
unsigned n_open = 0;
int is_else = FALSE;
s = cin_skipcomment(s);
if (*s == '{' || (*s == '}' && !cin_iselse(s)))
found_start = *s;
if (!found_start)
is_else = cin_iselse(s);
while (*s)
{
s = skip_string(cin_skipcomment(s));
if (*s == '}' && n_open > 0)
--n_open;
if ((!is_else || n_open == 0)
&& (*s == ';' || *s == '}' || (incl_comma && *s == ','))
&& cin_nocode(s + 1))
return *s;
else if (*s == '{')
{
if (incl_open && cin_nocode(s + 1))
return *s;
else
++n_open;
}
if (*s)
s++;
}
return found_start;
}
static int
cin_isfuncdecl(
char_u **sp,
linenr_T first_lnum,
linenr_T min_lnum)
{
char_u *s;
linenr_T lnum = first_lnum;
linenr_T save_lnum = curwin->w_cursor.lnum;
int retval = FALSE;
pos_T *trypos;
int just_started = TRUE;
if (sp == NULL)
s = ml_get(lnum);
else
s = *sp;
curwin->w_cursor.lnum = lnum;
if (find_last_paren(s, '(', ')')
&& (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
{
lnum = trypos->lnum;
if (lnum < min_lnum)
{
curwin->w_cursor.lnum = save_lnum;
return FALSE;
}
s = ml_get(lnum);
}
curwin->w_cursor.lnum = save_lnum;
if (cin_ispreproc(s))
return FALSE;
while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
{
if (cin_iscomment(s))
s = cin_skipcomment(s);
else if (*s == ':')
{
if (*(s + 1) == ':')
s += 2;
else
return FALSE;
}
else
++s;
}
if (*s != '(')
return FALSE;
while (*s && *s != ';' && *s != '\'' && *s != '"')
{
if (*s == ')' && cin_nocode(s + 1))
{
lnum = first_lnum - 1;
s = ml_get(lnum);
if (*s == NUL || s[STRLEN(s) - 1] != '\\')
retval = TRUE;
goto done;
}
if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
{
int comma = (*s == ',');
for (;;)
{
if (lnum >= curbuf->b_ml.ml_line_count)
break;
s = ml_get(++lnum);
if (!cin_ispreproc(s))
break;
}
if (lnum >= curbuf->b_ml.ml_line_count)
break;
s = skipwhite(s);
if (!just_started && (!comma && *s != ',' && *s != ')'))
break;
just_started = FALSE;
}
else if (cin_iscomment(s))
s = cin_skipcomment(s);
else
{
++s;
just_started = FALSE;
}
}
done:
if (lnum != first_lnum && sp != NULL)
*sp = ml_get(first_lnum);
return retval;
}
static int
cin_isif(char_u *p)
{
return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
}
static int
cin_iselse(
char_u *p)
{
if (*p == '}')
p = cin_skipcomment(p + 1);
return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
}
static int
cin_isdo(char_u *p)
{
return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
}
static int
cin_iswhileofdo (char_u *p, linenr_T lnum)
{
pos_T cursor_save;
pos_T *trypos;
int retval = FALSE;
p = cin_skipcomment(p);
if (*p == '}')
p = cin_skipcomment(p + 1);
if (cin_starts_with(p, "while"))
{
cursor_save = curwin->w_cursor;
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = 0;
p = ml_get_curline();
while (*p && *p != 'w')
{
++p;
++curwin->w_cursor.col;
}
if ((trypos = findmatchlimit(NULL, 0, 0,
curbuf->b_ind_maxparen)) != NULL
&& *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
retval = TRUE;
curwin->w_cursor = cursor_save;
}
return retval;
}
static int
cin_is_if_for_while_before_offset(char_u *line, int *poffset)
{
int offset = *poffset;
if (offset-- < 2)
return 0;
while (offset > 2 && VIM_ISWHITE(line[offset]))
--offset;
offset -= 1;
if (!STRNCMP(line + offset, "if", 2))
goto probablyFound;
if (offset >= 1)
{
offset -= 1;
if (!STRNCMP(line + offset, "for", 3))
goto probablyFound;
if (offset >= 2)
{
offset -= 2;
if (!STRNCMP(line + offset, "while", 5))
goto probablyFound;
}
}
return 0;
probablyFound:
if (!offset || !vim_isIDc(line[offset - 1]))
{
*poffset = offset;
return 1;
}
return 0;
}
static int
cin_iswhileofdo_end(int terminated)
{
char_u *line;
char_u *p;
char_u *s;
pos_T *trypos;
int i;
if (terminated != ';')
return FALSE;
p = line = ml_get_curline();
while (*p != NUL)
{
p = cin_skipcomment(p);
if (*p == ')')
{
s = skipwhite(p + 1);
if (*s == ';' && cin_nocode(s + 1))
{
i = (int)(p - line);
curwin->w_cursor.col = i;
trypos = find_match_paren(curbuf->b_ind_maxparen);
if (trypos != NULL)
{
s = cin_skipcomment(ml_get(trypos->lnum));
if (*s == '}')
s = cin_skipcomment(s + 1);
if (cin_starts_with(s, "while"))
{
curwin->w_cursor.lnum = trypos->lnum;
return TRUE;
}
}
line = ml_get_curline();
p = line + i;
}
}
if (*p != NUL)
++p;
}
return FALSE;
}
static int
cin_isbreak(char_u *p)
{
return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
}
static int
cin_is_cpp_baseclass(
cpp_baseclass_cache_T *cached)
{
lpos_T *pos = &cached->lpos;
char_u *s;
int class_or_struct, lookfor_ctor_init, cpp_base_class;
linenr_T lnum = curwin->w_cursor.lnum;
char_u *line = ml_get_curline();
if (pos->lnum <= lnum)
return cached->found;
pos->col = 0;
s = skipwhite(line);
if (*s == '#')
return FALSE;
s = cin_skipcomment(s);
if (*s == NUL)
return FALSE;
cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
while (lnum > 1)
{
line = ml_get(lnum - 1);
s = skipwhite(line);
if (*s == '#' || *s == NUL)
break;
while (*s != NUL)
{
s = cin_skipcomment(s);
if (*s == '{' || *s == '}'
|| (*s == ';' && cin_nocode(s + 1)))
break;
if (*s != NUL)
++s;
}
if (*s != NUL)
break;
--lnum;
}
pos->lnum = lnum;
line = ml_get(lnum);
s = line;
for (;;)
{
if (*s == NUL)
{
if (lnum == curwin->w_cursor.lnum)
break;
line = ml_get(++lnum);
s = line;
}
if (s == line)
{
if (cin_iscase(s, FALSE))
break;
s = cin_skipcomment(line);
if (*s == NUL)
continue;
}
if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
s = skip_string(s) + 1;
else if (s[0] == ':')
{
if (s[1] == ':')
{
lookfor_ctor_init = FALSE;
s = cin_skipcomment(s + 2);
}
else if (lookfor_ctor_init || class_or_struct)
{
cpp_base_class = TRUE;
lookfor_ctor_init = class_or_struct = FALSE;
pos->col = 0;
s = cin_skipcomment(s + 1);
}
else
s = cin_skipcomment(s + 1);
}
else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
|| (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
{
class_or_struct = TRUE;
lookfor_ctor_init = FALSE;
if (*s == 'c')
s = cin_skipcomment(s + 5);
else
s = cin_skipcomment(s + 6);
}
else
{
if (s[0] == '{' || s[0] == '}' || s[0] == ';')
{
cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
}
else if (s[0] == ')')
{
class_or_struct = FALSE;
lookfor_ctor_init = TRUE;
}
else if (s[0] == '?')
{
return FALSE;
}
else if (!vim_isIDc(s[0]))
{
class_or_struct = FALSE;
lookfor_ctor_init = FALSE;
}
else if (pos->col == 0)
{
lookfor_ctor_init = FALSE;
if (cpp_base_class)
pos->col = (colnr_T)(s - line);
}
if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
pos->col = 0;
s = cin_skipcomment(s + 1);
}
}
cached->found = cpp_base_class;
if (cpp_base_class)
pos->lnum = lnum;
return cpp_base_class;
}
static int
get_baseclass_amount(int col)
{
int amount;
colnr_T vcol;
pos_T *trypos;
if (col == 0)
{
amount = get_indent();
if (find_last_paren(ml_get_curline(), '(', ')')
&& (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
amount = get_indent_lnum(trypos->lnum);
if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
amount += curbuf->b_ind_cpp_baseclass;
}
else
{
curwin->w_cursor.col = col;
getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
amount = (int)vcol;
}
if (amount < curbuf->b_ind_cpp_baseclass)
amount = curbuf->b_ind_cpp_baseclass;
return amount;
}
static int
cin_ends_in(char_u *s, char_u *find, char_u *ignore)
{
char_u *p = s;
char_u *r;
int len = (int)STRLEN(find);
while (*p != NUL)
{
p = cin_skipcomment(p);
if (STRNCMP(p, find, len) == 0)
{
r = skipwhite(p + len);
if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
r = skipwhite(r + STRLEN(ignore));
if (cin_nocode(r))
return TRUE;
}
if (*p != NUL)
++p;
}
return FALSE;
}
static int
cin_starts_with(char_u *s, char *word)
{
int l = (int)STRLEN(word);
return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
}
static int
cin_skip2pos(pos_T *trypos)
{
char_u *line;
char_u *p;
char_u *new_p;
p = line = ml_get(trypos->lnum);
while (*p && (colnr_T)(p - line) < trypos->col)
{
if (cin_iscomment(p))
p = cin_skipcomment(p);
else
{
new_p = skip_string(p);
if (new_p == p)
++p;
else
p = new_p;
}
}
return (int)(p - line);
}
static pos_T *
find_start_brace(void)
{
pos_T cursor_save;
pos_T *trypos;
pos_T *pos;
static pos_T pos_copy;
cursor_save = curwin->w_cursor;
while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
{
pos_copy = *trypos;
trypos = &pos_copy;
curwin->w_cursor = *trypos;
pos = NULL;
if ((colnr_T)cin_skip2pos(trypos) == trypos->col
&& (pos = ind_find_start_CORS(NULL)) == NULL)
break;
if (pos != NULL)
curwin->w_cursor.lnum = pos->lnum;
}
curwin->w_cursor = cursor_save;
return trypos;
}
static pos_T *
find_match_paren(int ind_maxparen)
{
return find_match_char('(', ind_maxparen);
}
static pos_T *
find_match_char (int c, int ind_maxparen)
{
pos_T cursor_save;
pos_T *trypos;
static pos_T pos_copy;
int ind_maxp_wk;
cursor_save = curwin->w_cursor;
ind_maxp_wk = ind_maxparen;
retry:
if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
{
if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
{
ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
if (ind_maxp_wk > 0)
{
curwin->w_cursor = *trypos;
curwin->w_cursor.col = 0;
goto retry;
}
trypos = NULL;
}
else
{
pos_T *trypos_wk;
pos_copy = *trypos;
trypos = &pos_copy;
curwin->w_cursor = *trypos;
if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL)
{
ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
- trypos_wk->lnum);
if (ind_maxp_wk > 0)
{
curwin->w_cursor = *trypos_wk;
goto retry;
}
trypos = NULL;
}
}
}
curwin->w_cursor = cursor_save;
return trypos;
}
static pos_T *
find_match_paren_after_brace (int ind_maxparen)
{
pos_T *trypos = find_match_paren(ind_maxparen);
if (trypos != NULL)
{
pos_T *tryposBrace = find_start_brace();
if (tryposBrace != NULL
&& (trypos->lnum != tryposBrace->lnum
? trypos->lnum < tryposBrace->lnum
: trypos->col < tryposBrace->col))
trypos = NULL;
}
return trypos;
}
static int
corr_ind_maxparen(pos_T *startpos)
{
long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
if (n > 0 && n < curbuf->b_ind_maxparen / 2)
return curbuf->b_ind_maxparen - (int)n;
return curbuf->b_ind_maxparen;
}
static int
find_last_paren(char_u *l, int start, int end)
{
int i;
int retval = FALSE;
int open_count = 0;
curwin->w_cursor.col = 0;
for (i = 0; l[i] != NUL; i++)
{
i = (int)(cin_skipcomment(l + i) - l);
i = (int)(skip_string(l + i) - l);
if (l[i] == start)
++open_count;
else if (l[i] == end)
{
if (open_count > 0)
--open_count;
else
{
curwin->w_cursor.col = i;
retval = TRUE;
}
}
}
return retval;
}
void
parse_cino(buf_T *buf)
{
char_u *p;
char_u *l;
char_u *digits;
int n;
int divider;
int fraction = 0;
int sw = (int)get_sw_value(buf);
buf->b_ind_level = sw;
buf->b_ind_open_imag = 0;
buf->b_ind_no_brace = 0;
buf->b_ind_first_open = 0;
buf->b_ind_open_extra = 0;
buf->b_ind_close_extra = 0;
buf->b_ind_open_left_imag = 0;
buf->b_ind_jump_label = -1;
buf->b_ind_case = sw;
buf->b_ind_case_code = sw;
buf->b_ind_case_break = 0;
buf->b_ind_scopedecl = sw;
buf->b_ind_scopedecl_code = sw;
buf->b_ind_param = sw;
buf->b_ind_func_type = sw;
buf->b_ind_cpp_baseclass = sw;
buf->b_ind_continuation = sw;
buf->b_ind_unclosed = sw * 2;
buf->b_ind_unclosed2 = sw;
buf->b_ind_unclosed_noignore = 0;
buf->b_ind_unclosed_wrapped = 0;
buf->b_ind_unclosed_whiteok = 0;
buf->b_ind_matching_paren = 0;
buf->b_ind_paren_prev = 0;
buf->b_ind_comment = 0;
buf->b_ind_in_comment = 3;
buf->b_ind_in_comment2 = 0;
buf->b_ind_maxparen = 20;
buf->b_ind_maxcomment = 70;
buf->b_ind_java = 0;
buf->b_ind_js = 0;
buf->b_ind_keep_case_label = 0;
buf->b_ind_cpp_namespace = 0;
buf->b_ind_if_for_while = 0;
buf->b_ind_hash_comment = 0;
buf->b_ind_cpp_extern_c = 0;
for (p = buf->b_p_cino; *p; )
{
l = p++;
if (*p == '-')
++p;
digits = p;
n = getdigits(&p);
divider = 0;
if (*p == '.')
{
fraction = atol((char *)++p);
while (VIM_ISDIGIT(*p))
{
++p;
if (divider)
divider *= 10;
else
divider = 10;
}
}
if (*p == 's')
{
if (p == digits)
n = sw;
else
{
n *= sw;
if (divider)
n += (sw * fraction + divider / 2) / divider;
}
++p;
}
if (l[1] == '-')
n = -n;
switch (*l)
{
case '>': buf->b_ind_level = n; break;
case 'e': buf->b_ind_open_imag = n; break;
case 'n': buf->b_ind_no_brace = n; break;
case 'f': buf->b_ind_first_open = n; break;
case '{': buf->b_ind_open_extra = n; break;
case '}': buf->b_ind_close_extra = n; break;
case '^': buf->b_ind_open_left_imag = n; break;
case 'L': buf->b_ind_jump_label = n; break;
case ':': buf->b_ind_case = n; break;
case '=': buf->b_ind_case_code = n; break;
case 'b': buf->b_ind_case_break = n; break;
case 'p': buf->b_ind_param = n; break;
case 't': buf->b_ind_func_type = n; break;
case '/': buf->b_ind_comment = n; break;
case 'c': buf->b_ind_in_comment = n; break;
case 'C': buf->b_ind_in_comment2 = n; break;
case 'i': buf->b_ind_cpp_baseclass = n; break;
case '+': buf->b_ind_continuation = n; break;
case '(': buf->b_ind_unclosed = n; break;
case 'u': buf->b_ind_unclosed2 = n; break;
case 'U': buf->b_ind_unclosed_noignore = n; break;
case 'W': buf->b_ind_unclosed_wrapped = n; break;
case 'w': buf->b_ind_unclosed_whiteok = n; break;
case 'm': buf->b_ind_matching_paren = n; break;
case 'M': buf->b_ind_paren_prev = n; break;
case ')': buf->b_ind_maxparen = n; break;
case '*': buf->b_ind_maxcomment = n; break;
case 'g': buf->b_ind_scopedecl = n; break;
case 'h': buf->b_ind_scopedecl_code = n; break;
case 'j': buf->b_ind_java = n; break;
case 'J': buf->b_ind_js = n; break;
case 'l': buf->b_ind_keep_case_label = n; break;
case '#': buf->b_ind_hash_comment = n; break;
case 'N': buf->b_ind_cpp_namespace = n; break;
case 'k': buf->b_ind_if_for_while = n; break;
case 'E': buf->b_ind_cpp_extern_c = n; break;
}
if (*p == ',')
++p;
}
}
int
get_c_indent(void)
{
pos_T cur_curpos;
int amount;
int scope_amount;
int cur_amount = MAXCOL;
colnr_T col;
char_u *theline;
char_u *linecopy;
pos_T *trypos;
pos_T *comment_pos;
pos_T *tryposBrace = NULL;
pos_T tryposCopy;
pos_T our_paren_pos;
char_u *start;
int start_brace;
#define BRACE_IN_COL0 1
#define BRACE_AT_START 2
#define BRACE_AT_END 3
linenr_T ourscope;
char_u *l;
char_u *look;
char_u terminated;
int lookfor;
#define LOOKFOR_INITIAL 0
#define LOOKFOR_IF 1
#define LOOKFOR_DO 2
#define LOOKFOR_CASE 3
#define LOOKFOR_ANY 4
#define LOOKFOR_TERM 5
#define LOOKFOR_UNTERM 6
#define LOOKFOR_SCOPEDECL 7
#define LOOKFOR_NOBREAK 8
#define LOOKFOR_CPP_BASECLASS 9
#define LOOKFOR_ENUM_OR_INIT 10
#define LOOKFOR_JS_KEY 11
#define LOOKFOR_COMMA 12
int whilelevel;
linenr_T lnum;
int n;
int iscase;
int lookfor_break;
int lookfor_cpp_namespace = FALSE;
int cont_amount = 0;
int original_line_islabel;
int added_to_amount = 0;
int js_cur_has_key = 0;
linenr_T raw_string_start = 0;
cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
int ind_continuation = curbuf->b_ind_continuation;
cur_curpos = curwin->w_cursor;
if (cur_curpos.lnum == 1)
return 0;
linecopy = vim_strsave(ml_get(cur_curpos.lnum));
if (linecopy == NULL)
return 0;
if ((State & INSERT)
&& curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
&& linecopy[curwin->w_cursor.col] == ')')
linecopy[curwin->w_cursor.col] = NUL;
theline = skipwhite(linecopy);
curwin->w_cursor.col = 0;
original_line_islabel = cin_islabel();
comment_pos = ind_find_start_comment();
if (comment_pos != NULL)
{
tryposCopy = *comment_pos;
comment_pos = &tryposCopy;
}
trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
if (trypos != NULL && (comment_pos == NULL
|| LT_POS(*trypos, *comment_pos)))
{
amount = -1;
goto laterend;
}
if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
{
amount = curbuf->b_ind_hash_comment;
goto theend;
}
if (original_line_islabel && !curbuf->b_ind_js
&& curbuf->b_ind_jump_label < 0)
{
amount = 0;
goto theend;
}
if (cin_islinecomment(theline)
&& (trypos = find_line_comment()) != NULL)
{
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
goto theend;
}
if (!cin_iscomment(theline) && comment_pos != NULL)
{
int lead_start_len = 2;
int lead_middle_len = 1;
char_u lead_start[COM_MAX_LEN];
char_u lead_middle[COM_MAX_LEN];
char_u lead_end[COM_MAX_LEN];
char_u *p;
int start_align = 0;
int start_off = 0;
int done = FALSE;
getvcol(curwin, comment_pos, &col, NULL, NULL);
amount = col;
*lead_start = NUL;
*lead_middle = NUL;
p = curbuf->b_p_com;
while (*p != NUL)
{
int align = 0;
int off = 0;
int what = 0;
while (*p != NUL && *p != ':')
{
if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
what = *p++;
else if (*p == COM_LEFT || *p == COM_RIGHT)
align = *p++;
else if (VIM_ISDIGIT(*p) || *p == '-')
off = getdigits(&p);
else
++p;
}
if (*p == ':')
++p;
(void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
if (what == COM_START)
{
STRCPY(lead_start, lead_end);
lead_start_len = (int)STRLEN(lead_start);
start_off = off;
start_align = align;
}
else if (what == COM_MIDDLE)
{
STRCPY(lead_middle, lead_end);
lead_middle_len = (int)STRLEN(lead_middle);
}
else if (what == COM_END)
{
if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
&& STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
{
done = TRUE;
if (curwin->w_cursor.lnum > 1)
{
look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
if (STRNCMP(look, lead_start, lead_start_len) == 0)
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
else if (STRNCMP(look, lead_middle,
lead_middle_len) == 0)
{
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
break;
}
else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
lead_start, lead_start_len) != 0)
continue;
}
if (start_off != 0)
amount += start_off;
else if (start_align == COM_RIGHT)
amount += vim_strsize(lead_start)
- vim_strsize(lead_middle);
break;
}
if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
&& STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
{
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
if (off != 0)
amount += off;
else if (align == COM_RIGHT)
amount += vim_strsize(lead_start)
- vim_strsize(lead_middle);
done = TRUE;
break;
}
}
}
if (done)
;
else if (theline[0] == '*')
amount += 1;
else
{
amount = -1;
for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
{
if (linewhite(lnum))
continue;
amount = get_indent_lnum(lnum);
break;
}
if (amount == -1)
{
if (!curbuf->b_ind_in_comment2)
{
start = ml_get(comment_pos->lnum);
look = start + comment_pos->col + 2;
if (*look != NUL)
comment_pos->col = (colnr_T)(skipwhite(look) - start);
}
getvcol(curwin, comment_pos, &col, NULL, NULL);
amount = col;
if (curbuf->b_ind_in_comment2 || *look == NUL)
amount += curbuf->b_ind_in_comment;
}
}
goto theend;
}
if (*skipwhite(theline) == ']'
&& (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
{
amount = get_indent_lnum(trypos->lnum);
goto theend;
}
if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
&& curbuf->b_ind_java == 0)
|| (tryposBrace = find_start_brace()) != NULL
|| trypos != NULL)
{
if (trypos != NULL && tryposBrace != NULL)
{
if (trypos->lnum != tryposBrace->lnum
? trypos->lnum < tryposBrace->lnum
: trypos->col < tryposBrace->col)
trypos = NULL;
else
tryposBrace = NULL;
}
if (trypos != NULL)
{
if (theline[0] == ')' && curbuf->b_ind_paren_prev)
{
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
}
else
{
amount = -1;
our_paren_pos = *trypos;
for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
{
l = skipwhite(ml_get(lnum));
if (cin_nocode(l))
continue;
if (cin_ispreproc_cont(&l, &lnum, &amount))
continue;
curwin->w_cursor.lnum = lnum;
if ((trypos = ind_find_start_CORS(NULL)) != NULL)
{
lnum = trypos->lnum + 1;
continue;
}
if ((trypos = find_match_paren(
corr_ind_maxparen(&cur_curpos))) != NULL
&& trypos->lnum == our_paren_pos.lnum
&& trypos->col == our_paren_pos.col)
{
amount = get_indent_lnum(lnum);
if (theline[0] == ')')
{
if (our_paren_pos.lnum != lnum
&& cur_amount > amount)
cur_amount = amount;
amount = -1;
}
break;
}
}
}
if (amount == -1)
{
int ignore_paren_col = 0;
int is_if_for_while = 0;
if (curbuf->b_ind_if_for_while)
{
pos_T cursor_save = curwin->w_cursor;
pos_T outermost;
char_u *line;
trypos = &our_paren_pos;
do {
outermost = *trypos;
curwin->w_cursor.lnum = outermost.lnum;
curwin->w_cursor.col = outermost.col;
trypos = find_match_paren(curbuf->b_ind_maxparen);
} while (trypos && trypos->lnum == outermost.lnum);
curwin->w_cursor = cursor_save;
line = ml_get(outermost.lnum);
is_if_for_while =
cin_is_if_for_while_before_offset(line, &outermost.col);
}
amount = skip_label(our_paren_pos.lnum, &look);
look = skipwhite(look);
if (*look == '(')
{
linenr_T save_lnum = curwin->w_cursor.lnum;
char_u *line;
int look_col;
curwin->w_cursor.lnum = our_paren_pos.lnum;
line = ml_get_curline();
look_col = (int)(look - line);
curwin->w_cursor.col = look_col + 1;
if ((trypos = findmatchlimit(NULL, ')', 0,
curbuf->b_ind_maxparen))
!= NULL
&& trypos->lnum == our_paren_pos.lnum
&& trypos->col < our_paren_pos.col)
ignore_paren_col = trypos->col + 1;
curwin->w_cursor.lnum = save_lnum;
look = ml_get(our_paren_pos.lnum) + look_col;
}
if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
&& is_if_for_while == 0)
|| (!curbuf->b_ind_unclosed_noignore && *look == '('
&& ignore_paren_col == 0))
{
if (theline[0] != ')')
{
cur_amount = MAXCOL;
l = ml_get(our_paren_pos.lnum);
if (curbuf->b_ind_unclosed_wrapped
&& cin_ends_in(l, (char_u *)"(", NULL))
{
n = 1;
for (col = 0; col < our_paren_pos.col; ++col)
{
switch (l[col])
{
case '(':
case '{': ++n;
break;
case ')':
case '}': if (n > 1)
--n;
break;
}
}
our_paren_pos.col = 0;
amount += n * curbuf->b_ind_unclosed_wrapped;
}
else if (curbuf->b_ind_unclosed_whiteok)
our_paren_pos.col++;
else
{
col = our_paren_pos.col + 1;
while (VIM_ISWHITE(l[col]))
col++;
if (l[col] != NUL)
our_paren_pos.col = col;
else
our_paren_pos.col++;
}
}
if (our_paren_pos.col > 0)
{
getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
if (cur_amount > (int)col)
cur_amount = col;
}
}
if (theline[0] == ')' && curbuf->b_ind_matching_paren)
{
}
else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
|| (!curbuf->b_ind_unclosed_noignore
&& *look == '(' && ignore_paren_col == 0))
{
if (cur_amount != MAXCOL)
amount = cur_amount;
}
else
{
col = our_paren_pos.col;
while ((int)our_paren_pos.col > ignore_paren_col)
{
--our_paren_pos.col;
switch (*ml_get_pos(&our_paren_pos))
{
case '(': amount += curbuf->b_ind_unclosed2;
col = our_paren_pos.col;
break;
case ')': amount -= curbuf->b_ind_unclosed2;
col = MAXCOL;
break;
}
}
if (col == MAXCOL)
amount += curbuf->b_ind_unclosed;
else
{
curwin->w_cursor.lnum = our_paren_pos.lnum;
curwin->w_cursor.col = col;
if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
!= NULL)
amount += curbuf->b_ind_unclosed2;
else
{
if (is_if_for_while)
amount += curbuf->b_ind_if_for_while;
else
amount += curbuf->b_ind_unclosed;
}
}
if (cur_amount < amount)
amount = cur_amount;
}
}
if (cin_iscomment(theline))
amount += curbuf->b_ind_comment;
}
else
{
tryposCopy = *tryposBrace;
tryposBrace = &tryposCopy;
trypos = tryposBrace;
ourscope = trypos->lnum;
start = ml_get(ourscope);
look = skipwhite(start);
if (*look == '{')
{
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
if (*start == '{')
start_brace = BRACE_IN_COL0;
else
start_brace = BRACE_AT_START;
}
else
{
curwin->w_cursor.lnum = ourscope;
lnum = ourscope;
if (find_last_paren(start, '(', ')')
&& (trypos = find_match_paren(curbuf->b_ind_maxparen))
!= NULL)
lnum = trypos->lnum;
if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
&& cin_iscase(skipwhite(ml_get_curline()), FALSE))
amount = get_indent();
else if (curbuf->b_ind_js)
amount = get_indent_lnum(lnum);
else
amount = skip_label(lnum, &l);
start_brace = BRACE_AT_END;
}
if (curbuf->b_ind_js)
js_cur_has_key = cin_has_js_key(theline);
if (theline[0] == '}')
{
amount += curbuf->b_ind_close_extra;
}
else
{
lookfor = LOOKFOR_INITIAL;
if (cin_iselse(theline))
lookfor = LOOKFOR_IF;
else if (cin_iswhileofdo(theline, cur_curpos.lnum))
lookfor = LOOKFOR_DO;
if (lookfor != LOOKFOR_INITIAL)
{
curwin->w_cursor.lnum = cur_curpos.lnum;
if (find_match(lookfor, ourscope) == OK)
{
amount = get_indent();
goto theend;
}
}
if (start_brace == BRACE_IN_COL0)
{
amount = curbuf->b_ind_open_left_imag;
lookfor_cpp_namespace = TRUE;
}
else if (start_brace == BRACE_AT_START &&
lookfor_cpp_namespace)
{
lookfor_cpp_namespace = TRUE;
}
else
{
if (start_brace == BRACE_AT_END)
{
amount += curbuf->b_ind_open_imag;
l = skipwhite(ml_get_curline());
if (cin_is_cpp_namespace(l))
amount += curbuf->b_ind_cpp_namespace;
else if (cin_is_cpp_extern_c(l))
amount += curbuf->b_ind_cpp_extern_c;
}
else
{
amount -= curbuf->b_ind_open_extra;
if (amount < 0)
amount = 0;
}
}
lookfor_break = FALSE;
if (cin_iscase(theline, FALSE))
{
lookfor = LOOKFOR_CASE;
amount += curbuf->b_ind_case;
}
else if (cin_isscopedecl(theline))
{
lookfor = LOOKFOR_SCOPEDECL;
amount += curbuf->b_ind_scopedecl;
}
else
{
if (curbuf->b_ind_case_break && cin_isbreak(theline))
lookfor_break = TRUE;
lookfor = LOOKFOR_INITIAL;
amount += curbuf->b_ind_level;
}
scope_amount = amount;
whilelevel = 0;
curwin->w_cursor = cur_curpos;
for (;;)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
if (curwin->w_cursor.lnum <= ourscope)
{
if (lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (curwin->w_cursor.lnum == 0
|| curwin->w_cursor.lnum
< ourscope - curbuf->b_ind_maxparen)
{
if (cont_amount > 0)
amount = cont_amount;
else if (!curbuf->b_ind_js)
amount += ind_continuation;
break;
}
l = ml_get_curline();
trypos = ind_find_start_CORS(NULL);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
&amount))
continue;
if (cin_nocode(l))
continue;
terminated = cin_isterminated(l, FALSE, TRUE);
if (start_brace != BRACE_IN_COL0
|| !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
{
if (terminated == ',')
break;
if (terminated != ';' && cin_isinit())
break;
if (terminated == 0 || terminated == '{')
continue;
}
if (terminated != ';')
{
trypos = NULL;
if (find_last_paren(l, '(', ')'))
trypos = find_match_paren(
curbuf->b_ind_maxparen);
if (trypos == NULL && find_last_paren(l, '{', '}'))
trypos = find_start_brace();
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
}
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
}
else if (lookfor == LOOKFOR_UNTERM)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
}
else
{
if (lookfor != LOOKFOR_TERM
&& lookfor != LOOKFOR_CPP_BASECLASS
&& lookfor != LOOKFOR_COMMA)
{
amount = scope_amount;
if (theline[0] == '{')
{
amount += curbuf->b_ind_open_extra;
added_to_amount = curbuf->b_ind_open_extra;
}
}
if (lookfor_cpp_namespace)
{
if (curwin->w_cursor.lnum == ourscope)
continue;
if (curwin->w_cursor.lnum == 0
|| curwin->w_cursor.lnum
< ourscope - FIND_NAMESPACE_LIM)
break;
l = ml_get_curline();
trypos = ind_find_start_CORS(NULL);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
&amount))
continue;
if (cin_is_cpp_namespace(l))
{
amount += curbuf->b_ind_cpp_namespace
- added_to_amount;
break;
}
else if (cin_is_cpp_extern_c(l))
{
amount += curbuf->b_ind_cpp_extern_c
- added_to_amount;
break;
}
if (cin_nocode(l))
continue;
}
}
break;
}
if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
l = ml_get_curline();
iscase = cin_iscase(l, FALSE);
if (iscase || cin_isscopedecl(l))
{
if (lookfor == LOOKFOR_CPP_BASECLASS)
break;
if (whilelevel > 0)
continue;
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
if ( (iscase && lookfor == LOOKFOR_CASE)
|| (iscase && lookfor_break)
|| (!iscase && lookfor == LOOKFOR_SCOPEDECL))
{
if ((trypos = find_start_brace()) == NULL
|| trypos->lnum == ourscope)
{
amount = get_indent();
break;
}
continue;
}
n = get_indent_nolabel(curwin->w_cursor.lnum);
if (lookfor == LOOKFOR_TERM)
{
if (n)
amount = n;
if (!lookfor_break)
break;
}
if (n)
{
amount = n;
l = after_label(ml_get_curline());
if (l != NULL && cin_is_cinword(l))
{
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
else
amount += curbuf->b_ind_level
+ curbuf->b_ind_no_brace;
}
break;
}
scope_amount = get_indent() + (iscase
? curbuf->b_ind_case_code
: curbuf->b_ind_scopedecl_code);
lookfor = curbuf->b_ind_case_break
? LOOKFOR_NOBREAK : LOOKFOR_ANY;
continue;
}
if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
{
if (find_last_paren(l, '{', '}')
&& (trypos = find_start_brace()) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
}
continue;
}
if (!curbuf->b_ind_js && cin_islabel())
{
l = after_label(ml_get_curline());
if (l == NULL || cin_nocode(l))
continue;
}
l = ml_get_curline();
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
|| cin_nocode(l))
continue;
n = FALSE;
if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
{
n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
l = ml_get_curline();
}
if (n)
{
if (lookfor == LOOKFOR_UNTERM)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
}
else if (theline[0] == '{')
{
lookfor = LOOKFOR_UNTERM;
ind_continuation = 0;
continue;
}
else
amount = get_baseclass_amount(
cache_cpp_baseclass.lpos.col);
break;
}
else if (lookfor == LOOKFOR_CPP_BASECLASS)
{
if (cin_isterminated(l, TRUE, FALSE))
break;
else
continue;
}
terminated = cin_isterminated(l, FALSE, TRUE);
if (js_cur_has_key)
{
js_cur_has_key = 0;
if (curbuf->b_ind_js && terminated == ',')
{
lookfor = LOOKFOR_JS_KEY;
}
}
if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
{
amount = get_indent();
break;
}
if (lookfor == LOOKFOR_COMMA)
{
if (tryposBrace != NULL && tryposBrace->lnum
>= curwin->w_cursor.lnum)
break;
if (terminated == ',')
break;
else
{
amount = get_indent();
if (curwin->w_cursor.lnum - 1 == ourscope)
break;
}
}
if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
&& terminated == ','))
{
if (lookfor != LOOKFOR_ENUM_OR_INIT &&
(*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
amount += ind_continuation;
(void)find_last_paren(l, '(', ')');
trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
|| (trypos->lnum == tryposBrace->lnum
&& trypos->col < tryposBrace->col)))
trypos = NULL;
if (trypos == NULL && terminated == ','
&& find_last_paren(l, '{', '}'))
trypos = find_start_brace();
if (trypos != NULL)
{
curwin->w_cursor = *trypos;
l = ml_get_curline();
if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
{
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
continue;
}
}
if (terminated == ',')
{
while (curwin->w_cursor.lnum > 1)
{
l = ml_get(curwin->w_cursor.lnum - 1);
if (*l == NUL || l[STRLEN(l) - 1] != '\\')
break;
--curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
}
if (curbuf->b_ind_js)
cur_amount = get_indent();
else
cur_amount = skip_label(curwin->w_cursor.lnum, &l);
if (terminated != ',' && lookfor != LOOKFOR_TERM
&& theline[0] == '{')
{
amount = cur_amount;
if (*skipwhite(l) != '{')
amount += curbuf->b_ind_open_extra;
if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
{
lookfor = LOOKFOR_CPP_BASECLASS;
continue;
}
break;
}
if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
{
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
amount = cur_amount;
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
if (lookfor != LOOKFOR_TERM)
{
amount += curbuf->b_ind_level
+ curbuf->b_ind_no_brace;
break;
}
l = skipwhite(ml_get_curline());
if (cin_isdo(l))
{
if (whilelevel == 0)
break;
--whilelevel;
}
if (cin_iselse(l) && whilelevel == 0)
{
if (*l == '}')
curwin->w_cursor.col =
(colnr_T)(l - ml_get_curline()) + 1;
if ((trypos = find_start_brace()) == NULL
|| find_match(LOOKFOR_IF, trypos->lnum)
== FAIL)
break;
}
}
else
{
if (lookfor == LOOKFOR_UNTERM)
{
if (terminated == ',')
amount += ind_continuation;
break;
}
if (lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (terminated == ',')
{
if (curbuf->b_ind_cpp_baseclass == 0)
break;
lookfor = LOOKFOR_CPP_BASECLASS;
continue;
}
if (amount > cur_amount)
amount = cur_amount;
}
else
{
l = ml_get_curline();
amount = cur_amount;
n = (int)STRLEN(l);
if (terminated == ',' && (*skipwhite(l) == ']'
|| (n >=2 && l[n - 2] == ']')))
break;
if (lookfor == LOOKFOR_INITIAL && terminated == ',')
{
if (curbuf->b_ind_js)
{
if (cin_iscomment(skipwhite(l)))
break;
lookfor = LOOKFOR_COMMA;
trypos = find_match_char('[',
curbuf->b_ind_maxparen);
if (trypos != NULL)
{
if (trypos->lnum
== curwin->w_cursor.lnum - 1)
{
break;
}
ourscope = trypos->lnum;
}
}
else
{
lookfor = LOOKFOR_ENUM_OR_INIT;
cont_amount = cin_first_id_amount();
}
}
else
{
if (lookfor == LOOKFOR_INITIAL
&& *l != NUL
&& l[STRLEN(l) - 1] == '\\')
cont_amount = cin_get_equal_amount(
curwin->w_cursor.lnum);
if (lookfor != LOOKFOR_TERM
&& lookfor != LOOKFOR_JS_KEY
&& lookfor != LOOKFOR_COMMA
&& raw_string_start != curwin->w_cursor.lnum)
lookfor = LOOKFOR_UNTERM;
}
}
}
}
else if (cin_iswhileofdo_end(terminated))
{
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
if (whilelevel == 0)
{
lookfor = LOOKFOR_TERM;
amount = get_indent();
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
}
++whilelevel;
}
else
{
if (lookfor == LOOKFOR_NOBREAK
&& cin_isbreak(skipwhite(ml_get_curline())))
{
lookfor = LOOKFOR_ANY;
continue;
}
if (whilelevel > 0)
{
l = cin_skipcomment(ml_get_curline());
if (cin_isdo(l))
{
amount = get_indent();
--whilelevel;
continue;
}
}
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
if (lookfor == LOOKFOR_TERM)
{
if (!lookfor_break && whilelevel == 0)
break;
}
else
{
term_again:
l = ml_get_curline();
if (find_last_paren(l, '(', ')')
&& (trypos = find_match_paren(
curbuf->b_ind_maxparen)) != NULL)
{
curwin->w_cursor = *trypos;
l = ml_get_curline();
if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
{
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
continue;
}
}
iscase = (curbuf->b_ind_keep_case_label
&& cin_iscase(l, FALSE));
amount = skip_label(curwin->w_cursor.lnum, &l);
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
l = skipwhite(l);
if (*l == '{')
amount -= curbuf->b_ind_open_extra;
lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
if (lookfor == LOOKFOR_TERM
&& *l != '}'
&& cin_iselse(l)
&& whilelevel == 0)
{
if ((trypos = find_start_brace()) == NULL
|| find_match(LOOKFOR_IF, trypos->lnum)
== FAIL)
break;
continue;
}
l = ml_get_curline();
if (find_last_paren(l, '{', '}')
&& (trypos = find_start_brace()) != NULL)
{
curwin->w_cursor = *trypos;
l = cin_skipcomment(ml_get_curline());
if (*l == '}' || !cin_iselse(l))
goto term_again;
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
}
}
}
}
}
if (cin_iscomment(theline))
amount += curbuf->b_ind_comment;
if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
amount -= curbuf->b_ind_jump_label;
goto theend;
}
if (theline[0] == '{')
{
amount = curbuf->b_ind_first_open;
goto theend;
}
if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
&& !cin_nocode(theline)
&& vim_strchr(theline, '{') == NULL
&& vim_strchr(theline, '}') == NULL
&& !cin_ends_in(theline, (char_u *)":", NULL)
&& !cin_ends_in(theline, (char_u *)",", NULL)
&& cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
cur_curpos.lnum + 1)
&& !cin_isterminated(theline, FALSE, TRUE))
{
amount = curbuf->b_ind_func_type;
goto theend;
}
amount = 0;
curwin->w_cursor = cur_curpos;
while (curwin->w_cursor.lnum > 1)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
l = ml_get_curline();
if ((trypos = ind_find_start_CORS(NULL)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
n = FALSE;
if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
{
n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
l = ml_get_curline();
}
if (n)
{
amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
break;
}
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
continue;
if (cin_nocode(l))
continue;
n = 0;
if (cin_ends_in(l, (char_u *)",", NULL)
|| (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
{
if (find_last_paren(l, '(', ')')
&& (trypos = find_match_paren(
curbuf->b_ind_maxparen)) != NULL)
curwin->w_cursor = *trypos;
while (n == 0 && curwin->w_cursor.lnum > 1)
{
l = ml_get(curwin->w_cursor.lnum - 1);
if (*l == NUL || l[STRLEN(l) - 1] != '\\')
break;
--curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
amount = get_indent();
if (amount == 0)
amount = cin_first_id_amount();
if (amount == 0)
amount = ind_continuation;
break;
}
if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0))
break;
l = ml_get_curline();
if (*skipwhite(l) == '}')
break;
if (cin_ends_in(l, (char_u *)"};", NULL))
break;
if (cin_ends_in(l, (char_u *)"[", NULL))
{
amount = get_indent() + ind_continuation;
break;
}
if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
{
pos_T curpos_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1)
{
look = ml_get(--curwin->w_cursor.lnum);
if (!(cin_nocode(look) || cin_ispreproc_cont(
&look, &curwin->w_cursor.lnum, &amount)))
break;
}
if (curwin->w_cursor.lnum > 0
&& cin_ends_in(look, (char_u *)"}", NULL))
break;
curwin->w_cursor = curpos_save;
}
if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
{
amount = curbuf->b_ind_param;
break;
}
if (cin_ends_in(l, (char_u *)";", NULL))
{
l = ml_get(curwin->w_cursor.lnum - 1);
if (cin_ends_in(l, (char_u *)",", NULL)
|| (*l != NUL && l[STRLEN(l) - 1] == '\\'))
break;
l = ml_get_curline();
}
find_last_paren(l, '(', ')');
if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
curwin->w_cursor = *trypos;
amount = get_indent();
break;
}
if (cin_iscomment(theline))
amount += curbuf->b_ind_comment;
if (cur_curpos.lnum > 1)
{
l = ml_get(cur_curpos.lnum - 1);
if (*l != NUL && l[STRLEN(l) - 1] == '\\')
{
cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
if (cur_amount > 0)
amount = cur_amount;
else if (cur_amount == 0)
amount += ind_continuation;
}
}
theend:
if (amount < 0)
amount = 0;
laterend:
curwin->w_cursor = cur_curpos;
vim_free(linecopy);
return amount;
}
static int
find_match(int lookfor, linenr_T ourscope)
{
char_u *look;
pos_T *theirscope;
char_u *mightbeif;
int elselevel;
int whilelevel;
if (lookfor == LOOKFOR_IF)
{
elselevel = 1;
whilelevel = 0;
}
else
{
elselevel = 0;
whilelevel = 1;
}
curwin->w_cursor.col = 0;
while (curwin->w_cursor.lnum > ourscope + 1)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
look = cin_skipcomment(ml_get_curline());
if (cin_iselse(look)
|| cin_isif(look)
|| cin_isdo(look)
|| cin_iswhileofdo(look, curwin->w_cursor.lnum))
{
theirscope = find_start_brace();
if (theirscope == NULL)
break;
if (theirscope->lnum < ourscope)
break;
if (theirscope->lnum > ourscope)
continue;
look = cin_skipcomment(ml_get_curline());
if (cin_iselse(look))
{
mightbeif = cin_skipcomment(look + 4);
if (!cin_isif(mightbeif))
++elselevel;
continue;
}
if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
{
++whilelevel;
continue;
}
look = cin_skipcomment(ml_get_curline());
if (cin_isif(look))
{
elselevel--;
if (elselevel == 0 && lookfor == LOOKFOR_IF)
whilelevel = 0;
}
if (cin_isdo(look))
whilelevel--;
if (elselevel <= 0 && whilelevel <= 0)
{
return OK;
}
}
}
return FAIL;
}
# if defined(FEAT_EVAL) || defined(PROTO)
int
get_expr_indent(void)
{
int indent = -1;
char_u *inde_copy;
pos_T save_pos;
colnr_T save_curswant;
int save_set_curswant;
int save_State;
int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
OPT_LOCAL);
save_pos = curwin->w_cursor;
save_curswant = curwin->w_curswant;
save_set_curswant = curwin->w_set_curswant;
set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
if (use_sandbox)
++sandbox;
++textlock;
inde_copy = vim_strsave(curbuf->b_p_inde);
if (inde_copy != NULL)
{
indent = (int)eval_to_number(inde_copy);
vim_free(inde_copy);
}
if (use_sandbox)
--sandbox;
--textlock;
save_State = State;
State = INSERT;
curwin->w_cursor = save_pos;
curwin->w_curswant = save_curswant;
curwin->w_set_curswant = save_set_curswant;
check_cursor();
State = save_State;
if (indent < 0)
indent = get_indent();
return indent;
}
# endif
#endif
#if defined(FEAT_LISP) || defined(PROTO)
static int lisp_match(char_u *p);
static int
lisp_match(char_u *p)
{
char_u buf[LSIZE];
int len;
char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
while (*word != NUL)
{
(void)copy_option_part(&word, buf, LSIZE, ",");
len = (int)STRLEN(buf);
if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
return TRUE;
}
return FALSE;
}
int
get_lisp_indent(void)
{
pos_T *pos, realpos, paren;
int amount;
char_u *that;
colnr_T col;
colnr_T firsttry;
int parencount, quotecount;
int vi_lisp;
vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
realpos = curwin->w_cursor;
curwin->w_cursor.col = 0;
if ((pos = findmatch(NULL, '(')) == NULL)
pos = findmatch(NULL, '[');
else
{
paren = *pos;
pos = findmatch(NULL, '[');
if (pos == NULL || LT_POSP(pos, &paren))
pos = &paren;
}
if (pos != NULL)
{
amount = -1;
parencount = 0;
while (--curwin->w_cursor.lnum >= pos->lnum)
{
if (linewhite(curwin->w_cursor.lnum))
continue;
for (that = ml_get_curline(); *that != NUL; ++that)
{
if (*that == ';')
{
while (*(that + 1) != NUL)
++that;
continue;
}
if (*that == '\\')
{
if (*(that + 1) != NUL)
++that;
continue;
}
if (*that == '"' && *(that + 1) != NUL)
{
while (*++that && *that != '"')
{
if (*that == '\\')
{
if (*++that == NUL)
break;
if (that[1] == NUL)
{
++that;
break;
}
}
}
}
if (*that == '(' || *that == '[')
++parencount;
else if (*that == ')' || *that == ']')
--parencount;
}
if (parencount == 0)
{
amount = get_indent();
break;
}
}
if (amount == -1)
{
curwin->w_cursor.lnum = pos->lnum;
curwin->w_cursor.col = pos->col;
col = pos->col;
that = ml_get_curline();
if (vi_lisp && get_indent() == 0)
amount = 2;
else
{
char_u *line = that;
amount = 0;
while (*that && col)
{
amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
col--;
}
if (!vi_lisp && (*that == '(' || *that == '[')
&& lisp_match(that + 1))
amount += 2;
else
{
that++;
amount++;
firsttry = amount;
while (VIM_ISWHITE(*that))
{
amount += lbr_chartabsize(line, that, (colnr_T)amount);
++that;
}
if (*that && *that != ';')
{
if (!vi_lisp && *that != '(' && *that != '[')
firsttry++;
parencount = 0;
quotecount = 0;
if (vi_lisp
|| (*that != '"'
&& *that != '\''
&& *that != '#'
&& (*that < '0' || *that > '9')))
{
while (*that
&& (!VIM_ISWHITE(*that)
|| quotecount
|| parencount)
&& (!((*that == '(' || *that == '[')
&& !quotecount
&& !parencount
&& vi_lisp)))
{
if (*that == '"')
quotecount = !quotecount;
if ((*that == '(' || *that == '[')
&& !quotecount)
++parencount;
if ((*that == ')' || *that == ']')
&& !quotecount)
--parencount;
if (*that == '\\' && *(that+1) != NUL)
amount += lbr_chartabsize_adv(
line, &that, (colnr_T)amount);
amount += lbr_chartabsize_adv(
line, &that, (colnr_T)amount);
}
}
while (VIM_ISWHITE(*that))
{
amount += lbr_chartabsize(
line, that, (colnr_T)amount);
that++;
}
if (!*that || *that == ';')
amount = firsttry;
}
}
}
}
}
else
amount = 0;
curwin->w_cursor = realpos;
return amount;
}
#endif
void
prepare_to_exit(void)
{
#if defined(SIGHUP) && defined(SIG_IGN)
signal(SIGHUP, SIG_IGN);
#endif
#ifdef FEAT_GUI
if (gui.in_use)
{
gui.dying = TRUE;
out_trash();
}
else
#endif
{
windgoto((int)Rows - 1, 0);
settmode(TMODE_COOK);
stoptermcap();
out_flush();
}
}
void
preserve_exit(void)
{
buf_T *buf;
prepare_to_exit();
really_exiting = TRUE;
out_str(IObuff);
screen_start();
out_flush();
ml_close_notmod();
FOR_ALL_BUFFERS(buf)
{
if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
{
OUT_STR("Vim: preserving files...\n");
screen_start();
out_flush();
ml_sync_all(FALSE, FALSE);
break;
}
}
ml_close_all(FALSE);
OUT_STR("Vim: Finished.\n");
getout(1);
}
int
vim_fexists(char_u *fname)
{
stat_T st;
if (mch_stat((char *)fname, &st))
return FALSE;
return TRUE;
}
#ifndef BREAKCHECK_SKIP
# ifdef FEAT_GUI
# define BREAKCHECK_SKIP 200
# else
# define BREAKCHECK_SKIP 32
# endif
#endif
static int breakcheck_count = 0;
void
line_breakcheck(void)
{
if (++breakcheck_count >= BREAKCHECK_SKIP)
{
breakcheck_count = 0;
ui_breakcheck();
}
}
void
fast_breakcheck(void)
{
if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
{
breakcheck_count = 0;
ui_breakcheck();
}
}
int
expand_wildcards_eval(
char_u **pat,
int *num_file,
char_u ***file,
int flags)
{
int ret = FAIL;
char_u *eval_pat = NULL;
char_u *exp_pat = *pat;
char_u *ignored_msg;
int usedlen;
if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
{
++emsg_off;
eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
NULL, &ignored_msg, NULL);
--emsg_off;
if (eval_pat != NULL)
exp_pat = concat_str(eval_pat, exp_pat + usedlen);
}
if (exp_pat != NULL)
ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
if (eval_pat != NULL)
{
vim_free(exp_pat);
vim_free(eval_pat);
}
return ret;
}
int
expand_wildcards(
int num_pat,
char_u **pat,
int *num_files,
char_u ***files,
int flags)
{
int retval;
int i, j;
char_u *p;
int non_suf_match;
retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
if ((flags & EW_KEEPALL) || retval == FAIL)
return retval;
#ifdef FEAT_WILDIGN
if (*p_wig)
{
char_u *ffname;
for (i = 0; i < *num_files; ++i)
{
ffname = FullName_save((*files)[i], FALSE);
if (ffname == NULL)
break;
# ifdef VMS
vms_remove_version(ffname);
# endif
if (match_file_list(p_wig, (*files)[i], ffname))
{
vim_free((*files)[i]);
for (j = i; j + 1 < *num_files; ++j)
(*files)[j] = (*files)[j + 1];
--*num_files;
--i;
}
vim_free(ffname);
}
if (*num_files == 0)
{
vim_free(*files);
*files = NULL;
return FAIL;
}
}
#endif
if (*num_files > 1)
{
non_suf_match = 0;
for (i = 0; i < *num_files; ++i)
{
if (!match_suffix((*files)[i]))
{
p = (*files)[i];
for (j = i; j > non_suf_match; --j)
(*files)[j] = (*files)[j - 1];
(*files)[non_suf_match++] = p;
}
}
}
return retval;
}
int
match_suffix(char_u *fname)
{
int fnamelen, setsuflen;
char_u *setsuf;
#define MAXSUFLEN 30
char_u suf_buf[MAXSUFLEN];
fnamelen = (int)STRLEN(fname);
setsuflen = 0;
for (setsuf = p_su; *setsuf; )
{
setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
if (setsuflen == 0)
{
char_u *tail = gettail(fname);
if (vim_strchr(tail, '.') == NULL)
{
setsuflen = 1;
break;
}
}
else
{
if (fnamelen >= setsuflen
&& fnamencmp(suf_buf, fname + fnamelen - setsuflen,
(size_t)setsuflen) == 0)
break;
setsuflen = 0;
}
}
return (setsuflen != 0);
}
#if !defined(NO_EXPANDPATH) || defined(PROTO)
# ifdef VIM_BACKTICK
static int vim_backtick(char_u *p);
static int expand_backtick(garray_T *gap, char_u *pat, int flags);
# endif
# if defined(WIN3264)
# if defined(PROTO)
# define _cdecl
# else
# ifdef __BORLANDC__
# define _cdecl _RTLENTRYF
# endif
# endif
static int _cdecl
pstrcmp(const void *a, const void *b)
{
return (pathcmp(*(char **)a, *(char **)b, -1));
}
static int
dos_expandpath(
garray_T *gap,
char_u *path,
int wildoff,
int flags,
int didstar)
{
char_u *buf;
char_u *path_end;
char_u *p, *s, *e;
int start_len = gap->ga_len;
char_u *pat;
regmatch_T regmatch;
int starts_with_dot;
int matches;
int len;
int starstar = FALSE;
static int stardepth = 0;
WIN32_FIND_DATA fb;
HANDLE hFind = (HANDLE)0;
# ifdef FEAT_MBYTE
WIN32_FIND_DATAW wfb;
WCHAR *wn = NULL;
# endif
char_u *matchname;
int ok;
if (stardepth > 0)
{
ui_breakcheck();
if (got_int)
return 0;
}
buf = alloc((int)MAXPATHL);
if (buf == NULL)
return 0;
p = buf;
s = buf;
e = NULL;
path_end = path;
while (*path_end != NUL)
{
if (path_end >= path + wildoff && rem_backslash(path_end))
*p++ = *path_end++;
else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
{
if (e != NULL)
break;
s = p + 1;
}
else if (path_end >= path + wildoff
&& vim_strchr((char_u *)"*?[~", *path_end) != NULL)
e = p;
# ifdef FEAT_MBYTE
if (has_mbyte)
{
len = (*mb_ptr2len)(path_end);
STRNCPY(p, path_end, len);
p += len;
path_end += len;
}
else
# endif
*p++ = *path_end++;
}
e = p;
*e = NUL;
for (p = buf + wildoff; p < s; ++p)
if (rem_backslash(p))
{
STRMOVE(p, p + 1);
--e;
--s;
}
for (p = s; p < e; ++p)
if (p[0] == '*' && p[1] == '*')
starstar = TRUE;
starts_with_dot = *s == '.';
pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
if (pat == NULL)
{
vim_free(buf);
return 0;
}
if (flags & (EW_NOERROR | EW_NOTWILD))
++emsg_silent;
regmatch.rm_ic = TRUE;
regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
if (flags & (EW_NOERROR | EW_NOTWILD))
--emsg_silent;
vim_free(pat);
if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
{
vim_free(buf);
return 0;
}
matchname = vim_strsave(s);
if (!didstar && stardepth < 100 && starstar && e - s == 2
&& *path_end == '/')
{
STRCPY(s, path_end + 1);
++stardepth;
(void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
--stardepth;
}
STRCPY(s, "*.*");
# ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
{
wn = enc_to_utf16(buf, NULL);
if (wn != NULL)
{
hFind = FindFirstFileW(wn, &wfb);
if (hFind == INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
{
vim_free(wn);
wn = NULL;
}
}
}
if (wn == NULL)
# endif
hFind = FindFirstFile((LPCSTR)buf, &fb);
ok = (hFind != INVALID_HANDLE_VALUE);
while (ok)
{
# ifdef FEAT_MBYTE
if (wn != NULL)
p = utf16_to_enc(wfb.cFileName, NULL);
else
# endif
p = (char_u *)fb.cFileName;
if ((p[0] != '.' || starts_with_dot
|| ((flags & EW_DODOT)
&& p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
&& (matchname == NULL
|| (regmatch.regprog != NULL
&& vim_regexec(®match, p, (colnr_T)0))
|| ((flags & EW_NOTWILD)
&& fnamencmp(path + (s - buf), p, e - s) == 0)))
{
STRCPY(s, p);
len = (int)STRLEN(buf);
if (starstar && stardepth < 100)
{
STRCPY(buf + len, "/**");
STRCPY(buf + len + 3, path_end);
++stardepth;
(void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
--stardepth;
}
STRCPY(buf + len, path_end);
if (mch_has_exp_wildcard(path_end))
{
(void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
}
else
{
if (*path_end != 0)
backslash_halve(buf + len + 1);
if (mch_getperm(buf) >= 0)
addfile(gap, buf, flags);
}
}
# ifdef FEAT_MBYTE
if (wn != NULL)
{
vim_free(p);
ok = FindNextFileW(hFind, &wfb);
}
else
# endif
ok = FindNextFile(hFind, &fb);
if (!ok && matchname != NULL && gap->ga_len == start_len)
{
STRCPY(s, matchname);
FindClose(hFind);
# ifdef FEAT_MBYTE
if (wn != NULL)
{
vim_free(wn);
wn = enc_to_utf16(buf, NULL);
if (wn != NULL)
hFind = FindFirstFileW(wn, &wfb);
}
if (wn == NULL)
# endif
hFind = FindFirstFile((LPCSTR)buf, &fb);
ok = (hFind != INVALID_HANDLE_VALUE);
vim_free(matchname);
matchname = NULL;
}
}
FindClose(hFind);
# ifdef FEAT_MBYTE
vim_free(wn);
# endif
vim_free(buf);
vim_regfree(regmatch.regprog);
vim_free(matchname);
matches = gap->ga_len - start_len;
if (matches > 0)
qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
sizeof(char_u *), pstrcmp);
return matches;
}
int
mch_expandpath(
garray_T *gap,
char_u *path,
int flags)
{
return dos_expandpath(gap, path, 0, flags, FALSE);
}
# endif
#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
|| defined(PROTO)
static int pstrcmp(const void *, const void *);
static int
pstrcmp(const void *a, const void *b)
{
return (pathcmp(*(char **)a, *(char **)b, -1));
}
int
unix_expandpath(
garray_T *gap,
char_u *path,
int wildoff,
int flags,
int didstar)
{
char_u *buf;
char_u *path_end;
char_u *p, *s, *e;
int start_len = gap->ga_len;
char_u *pat;
regmatch_T regmatch;
int starts_with_dot;
int matches;
int len;
int starstar = FALSE;
static int stardepth = 0;
DIR *dirp;
struct dirent *dp;
if (stardepth > 0)
{
ui_breakcheck();
if (got_int)
return 0;
}
buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
if (buf == NULL)
return 0;
p = buf;
s = buf;
e = NULL;
path_end = path;
while (*path_end != NUL)
{
if (path_end >= path + wildoff && rem_backslash(path_end))
*p++ = *path_end++;
else if (*path_end == '/')
{
if (e != NULL)
break;
s = p + 1;
}
else if (path_end >= path + wildoff
&& (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
|| (!p_fic && (flags & EW_ICASE)
&& isalpha(PTR2CHAR(path_end)))))
e = p;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
len = (*mb_ptr2len)(path_end);
STRNCPY(p, path_end, len);
p += len;
path_end += len;
}
else
#endif
*p++ = *path_end++;
}
e = p;
*e = NUL;
for (p = buf + wildoff; p < s; ++p)
if (rem_backslash(p))
{
STRMOVE(p, p + 1);
--e;
--s;
}
for (p = s; p < e; ++p)
if (p[0] == '*' && p[1] == '*')
starstar = TRUE;
starts_with_dot = *s == '.';
pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
if (pat == NULL)
{
vim_free(buf);
return 0;
}
if (flags & EW_ICASE)
regmatch.rm_ic = TRUE;
else
regmatch.rm_ic = p_fic;
if (flags & (EW_NOERROR | EW_NOTWILD))
++emsg_silent;
regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
if (flags & (EW_NOERROR | EW_NOTWILD))
--emsg_silent;
vim_free(pat);
if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
{
vim_free(buf);
return 0;
}
if (!didstar && stardepth < 100 && starstar && e - s == 2
&& *path_end == '/')
{
STRCPY(s, path_end + 1);
++stardepth;
(void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
--stardepth;
}
*s = NUL;
dirp = opendir(*buf == NUL ? "." : (char *)buf);
if (dirp != NULL)
{
for (;;)
{
dp = readdir(dirp);
if (dp == NULL)
break;
if ((dp->d_name[0] != '.' || starts_with_dot
|| ((flags & EW_DODOT)
&& dp->d_name[1] != NUL
&& (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
&& ((regmatch.regprog != NULL && vim_regexec(®match,
(char_u *)dp->d_name, (colnr_T)0))
|| ((flags & EW_NOTWILD)
&& fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
{
STRCPY(s, dp->d_name);
len = STRLEN(buf);
if (starstar && stardepth < 100)
{
STRCPY(buf + len, "/**");
STRCPY(buf + len + 3, path_end);
++stardepth;
(void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
--stardepth;
}
STRCPY(buf + len, path_end);
if (mch_has_exp_wildcard(path_end))
{
(void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
}
else
{
stat_T sb;
if (*path_end != NUL)
backslash_halve(buf + len + 1);
if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
: mch_getperm(buf) >= 0)
{
#ifdef MACOS_CONVERT
size_t precomp_len = STRLEN(buf)+1;
char_u *precomp_buf =
mac_precompose_path(buf, precomp_len, &precomp_len);
if (precomp_buf)
{
mch_memmove(buf, precomp_buf, precomp_len);
vim_free(precomp_buf);
}
#endif
addfile(gap, buf, flags);
}
}
}
}
closedir(dirp);
}
vim_free(buf);
vim_regfree(regmatch.regprog);
matches = gap->ga_len - start_len;
if (matches > 0)
qsort(((char_u **)gap->ga_data) + start_len, matches,
sizeof(char_u *), pstrcmp);
return matches;
}
#endif
#if defined(FEAT_SEARCHPATH)
static int find_previous_pathsep(char_u *path, char_u **psep);
static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
static void expand_path_option(char_u *curdir, garray_T *gap);
static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
static void uniquefy_paths(garray_T *gap, char_u *pattern);
static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
static int
find_previous_pathsep(char_u *path, char_u **psep)
{
if (*psep > path && vim_ispathsep(**psep))
--*psep;
while (*psep > path)
{
if (vim_ispathsep(**psep))
return OK;
MB_PTR_BACK(path, *psep);
}
return FAIL;
}
static int
is_unique(char_u *maybe_unique, garray_T *gap, int i)
{
int j;
int candidate_len;
int other_path_len;
char_u **other_paths = (char_u **)gap->ga_data;
char_u *rival;
for (j = 0; j < gap->ga_len; j++)
{
if (j == i)
continue;
candidate_len = (int)STRLEN(maybe_unique);
other_path_len = (int)STRLEN(other_paths[j]);
if (other_path_len < candidate_len)
continue;
rival = other_paths[j] + other_path_len - candidate_len;
if (fnamecmp(maybe_unique, rival) == 0
&& (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
return FALSE;
}
return TRUE;
}
static void
expand_path_option(char_u *curdir, garray_T *gap)
{
char_u *path_option = *curbuf->b_p_path == NUL
? p_path : curbuf->b_p_path;
char_u *buf;
char_u *p;
int len;
if ((buf = alloc((int)MAXPATHL)) == NULL)
return;
while (*path_option != NUL)
{
copy_option_part(&path_option, buf, MAXPATHL, " ,");
if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
{
if (curbuf->b_ffname == NULL)
continue;
p = gettail(curbuf->b_ffname);
len = (int)(p - curbuf->b_ffname);
if (len + (int)STRLEN(buf) >= MAXPATHL)
continue;
if (buf[1] == NUL)
buf[len] = NUL;
else
STRMOVE(buf + len, buf + 2);
mch_memmove(buf, curbuf->b_ffname, len);
simplify_filename(buf);
}
else if (buf[0] == NUL)
STRCPY(buf, curdir);
else if (path_with_url(buf))
continue;
else if (!mch_isFullName(buf))
{
len = (int)STRLEN(curdir);
if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
continue;
STRMOVE(buf + len + 1, buf);
STRCPY(buf, curdir);
buf[len] = PATHSEP;
simplify_filename(buf);
}
if (ga_grow(gap, 1) == FAIL)
break;
# if defined(MSWIN)
len = (int)STRLEN(buf);
if (buf[len - 1] == '\\')
buf[len - 1] = '/';
# endif
p = vim_strsave(buf);
if (p == NULL)
break;
((char_u **)gap->ga_data)[gap->ga_len++] = p;
}
vim_free(buf);
}
static char_u *
get_path_cutoff(char_u *fname, garray_T *gap)
{
int i;
int maxlen = 0;
char_u **path_part = (char_u **)gap->ga_data;
char_u *cutoff = NULL;
for (i = 0; i < gap->ga_len; i++)
{
int j = 0;
while ((fname[j] == path_part[i][j]
# if defined(MSWIN)
|| (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
#endif
) && fname[j] != NUL && path_part[i][j] != NUL)
j++;
if (j > maxlen)
{
maxlen = j;
cutoff = &fname[j];
}
}
if (cutoff != NULL)
while (vim_ispathsep(*cutoff))
MB_PTR_ADV(cutoff);
return cutoff;
}
static void
uniquefy_paths(garray_T *gap, char_u *pattern)
{
int i;
int len;
char_u **fnames = (char_u **)gap->ga_data;
int sort_again = FALSE;
char_u *pat;
char_u *file_pattern;
char_u *curdir;
regmatch_T regmatch;
garray_T path_ga;
char_u **in_curdir = NULL;
char_u *short_name;
remove_duplicates(gap);
ga_init2(&path_ga, (int)sizeof(char_u *), 1);
len = (int)STRLEN(pattern);
file_pattern = alloc(len + 2);
if (file_pattern == NULL)
return;
file_pattern[0] = '*';
file_pattern[1] = NUL;
STRCAT(file_pattern, pattern);
pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
vim_free(file_pattern);
if (pat == NULL)
return;
regmatch.rm_ic = TRUE;
regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
vim_free(pat);
if (regmatch.regprog == NULL)
return;
if ((curdir = alloc((int)(MAXPATHL))) == NULL)
goto theend;
mch_dirname(curdir, MAXPATHL);
expand_path_option(curdir, &path_ga);
in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
if (in_curdir == NULL)
goto theend;
for (i = 0; i < gap->ga_len && !got_int; i++)
{
char_u *path = fnames[i];
int is_in_curdir;
char_u *dir_end = gettail_dir(path);
char_u *pathsep_p;
char_u *path_cutoff;
len = (int)STRLEN(path);
is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
&& curdir[dir_end - path] == NUL;
if (is_in_curdir)
in_curdir[i] = vim_strsave(path);
path_cutoff = get_path_cutoff(path, &path_ga);
if (pattern[0] == '*' && pattern[1] == '*'
&& vim_ispathsep_nocolon(pattern[2])
&& path_cutoff != NULL
&& vim_regexec(®match, path_cutoff, (colnr_T)0)
&& is_unique(path_cutoff, gap, i))
{
sort_again = TRUE;
mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
}
else
{
pathsep_p = path + len - 1;
while (find_previous_pathsep(path, &pathsep_p))
if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0)
&& is_unique(pathsep_p + 1, gap, i)
&& path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
{
sort_again = TRUE;
mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
break;
}
}
if (mch_isFullName(path))
{
short_name = shorten_fname(path, curdir);
if (short_name != NULL && short_name > path + 1
#if defined(MSWIN)
&& !vim_ispathsep(*short_name)
#endif
)
{
STRCPY(path, ".");
add_pathsep(path);
STRMOVE(path + STRLEN(path), short_name);
}
}
ui_breakcheck();
}
for (i = 0; i < gap->ga_len && !got_int; i++)
{
char_u *rel_path;
char_u *path = in_curdir[i];
if (path == NULL)
continue;
short_name = shorten_fname(path, curdir);
if (short_name == NULL)
short_name = path;
if (is_unique(short_name, gap, i))
{
STRCPY(fnames[i], short_name);
continue;
}
rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
if (rel_path == NULL)
goto theend;
STRCPY(rel_path, ".");
add_pathsep(rel_path);
STRCAT(rel_path, short_name);
vim_free(fnames[i]);
fnames[i] = rel_path;
sort_again = TRUE;
ui_breakcheck();
}
theend:
vim_free(curdir);
if (in_curdir != NULL)
{
for (i = 0; i < gap->ga_len; i++)
vim_free(in_curdir[i]);
vim_free(in_curdir);
}
ga_clear_strings(&path_ga);
vim_regfree(regmatch.regprog);
if (sort_again)
remove_duplicates(gap);
}
static int
expand_in_path(
garray_T *gap,
char_u *pattern,
int flags)
{
char_u *curdir;
garray_T path_ga;
char_u *paths = NULL;
if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
return 0;
mch_dirname(curdir, MAXPATHL);
ga_init2(&path_ga, (int)sizeof(char_u *), 1);
expand_path_option(curdir, &path_ga);
vim_free(curdir);
if (path_ga.ga_len == 0)
return 0;
paths = ga_concat_strings(&path_ga, ",");
ga_clear_strings(&path_ga);
if (paths == NULL)
return 0;
globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
vim_free(paths);
return gap->ga_len;
}
#endif
#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
void
remove_duplicates(garray_T *gap)
{
int i;
int j;
char_u **fnames = (char_u **)gap->ga_data;
sort_strings(fnames, gap->ga_len);
for (i = gap->ga_len - 1; i > 0; --i)
if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
{
vim_free(fnames[i]);
for (j = i + 1; j < gap->ga_len; ++j)
fnames[j - 1] = fnames[j];
--gap->ga_len;
}
}
#endif
static int has_env_var(char_u *p);
static int
has_env_var(char_u *p)
{
for ( ; *p; MB_PTR_ADV(p))
{
if (*p == '\\' && p[1] != NUL)
++p;
else if (vim_strchr((char_u *)
#if defined(MSWIN)
"$%"
#else
"$"
#endif
, *p) != NULL)
return TRUE;
}
return FALSE;
}
#ifdef SPECIAL_WILDCHAR
static int has_special_wildchar(char_u *p);
static int
has_special_wildchar(char_u *p)
{
for ( ; *p; MB_PTR_ADV(p))
{
if (*p == '\\' && p[1] != NUL)
++p;
else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
return TRUE;
}
return FALSE;
}
#endif
int
gen_expand_wildcards(
int num_pat,
char_u **pat,
int *num_file,
char_u ***file,
int flags)
{
int i;
garray_T ga;
char_u *p;
static int recursive = FALSE;
int add_pat;
int retval = OK;
#if defined(FEAT_SEARCHPATH)
int did_expand_in_path = FALSE;
#endif
if (recursive)
#ifdef SPECIAL_WILDCHAR
return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
#else
return FAIL;
#endif
#ifdef SPECIAL_WILDCHAR
for (i = 0; i < num_pat; i++)
{
if (has_special_wildchar(pat[i])
# ifdef VIM_BACKTICK
&& !(vim_backtick(pat[i]) && pat[i][1] == '=')
# endif
)
return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
}
#endif
recursive = TRUE;
ga_init2(&ga, (int)sizeof(char_u *), 30);
for (i = 0; i < num_pat; ++i)
{
add_pat = -1;
p = pat[i];
#ifdef VIM_BACKTICK
if (vim_backtick(p))
{
add_pat = expand_backtick(&ga, p, flags);
if (add_pat == -1)
retval = FAIL;
}
else
#endif
{
if (has_env_var(p) || *p == '~')
{
p = expand_env_save_opt(p, TRUE);
if (p == NULL)
p = pat[i];
#ifdef UNIX
else if (has_env_var(p) || *p == '~')
{
vim_free(p);
ga_clear_strings(&ga);
i = mch_expand_wildcards(num_pat, pat, num_file, file,
flags|EW_KEEPDOLLAR);
recursive = FALSE;
return i;
}
#endif
}
if (mch_has_exp_wildcard(p))
{
#if defined(FEAT_SEARCHPATH)
if ((flags & EW_PATH)
&& !mch_isFullName(p)
&& !(p[0] == '.'
&& (vim_ispathsep(p[1])
|| (p[1] == '.' && vim_ispathsep(p[2]))))
)
{
recursive = FALSE;
add_pat = expand_in_path(&ga, p, flags);
recursive = TRUE;
did_expand_in_path = TRUE;
}
else
#endif
add_pat = mch_expandpath(&ga, p, flags);
}
}
if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
{
char_u *t = backslash_halve_save(p);
if (flags & EW_NOTFOUND)
addfile(&ga, t, flags | EW_DIR | EW_FILE);
else
addfile(&ga, t, flags);
vim_free(t);
}
#if defined(FEAT_SEARCHPATH)
if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
uniquefy_paths(&ga, p);
#endif
if (p != pat[i])
vim_free(p);
}
*num_file = ga.ga_len;
*file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
recursive = FALSE;
return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
}
# ifdef VIM_BACKTICK
static int
vim_backtick(char_u *p)
{
return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
}
static int
expand_backtick(
garray_T *gap,
char_u *pat,
int flags)
{
char_u *p;
char_u *cmd;
char_u *buffer;
int cnt = 0;
int i;
cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
if (cmd == NULL)
return -1;
#ifdef FEAT_EVAL
if (*cmd == '=')
buffer = eval_to_string(cmd + 1, &p, TRUE);
else
#endif
buffer = get_cmd_output(cmd, NULL,
(flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
vim_free(cmd);
if (buffer == NULL)
return -1;
cmd = buffer;
while (*cmd != NUL)
{
cmd = skipwhite(cmd);
p = cmd;
while (*p != NUL && *p != '\r' && *p != '\n')
++p;
if (p > cmd)
{
i = *p;
*p = NUL;
addfile(gap, cmd, flags);
*p = i;
++cnt;
}
cmd = p;
while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
++cmd;
}
vim_free(buffer);
return cnt;
}
# endif
void
addfile(
garray_T *gap,
char_u *f,
int flags)
{
char_u *p;
int isdir;
stat_T sb;
if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
return;
#ifdef FNAME_ILLEGAL
if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
return;
#endif
isdir = mch_isdir(f);
if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
return;
if (!isdir && (flags & EW_EXEC)
&& !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
return;
if (ga_grow(gap, 1) == FAIL)
return;
p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
if (p == NULL)
return;
STRCPY(p, f);
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(p);
#endif
#ifndef DONT_ADD_PATHSEP_TO_DIR
if (isdir && (flags & EW_ADDSLASH))
add_pathsep(p);
#endif
((char_u **)gap->ga_data)[gap->ga_len++] = p;
}
#endif
#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
#ifndef SEEK_SET
# define SEEK_SET 0
#endif
#ifndef SEEK_END
# define SEEK_END 2
#endif
char_u *
get_cmd_output(
char_u *cmd,
char_u *infile,
int flags,
int *ret_len)
{
char_u *tempname;
char_u *command;
char_u *buffer = NULL;
int len;
int i = 0;
FILE *fd;
if (check_restricted() || check_secure())
return NULL;
if ((tempname = vim_tempname('o', FALSE)) == NULL)
{
EMSG(_(e_notmp));
return NULL;
}
command = make_filter_cmd(cmd, infile, tempname);
if (command == NULL)
goto done;
++no_check_timestamps;
call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
--no_check_timestamps;
vim_free(command);
# ifdef VMS
fd = mch_fopen((char *)tempname, "r");
# else
fd = mch_fopen((char *)tempname, READBIN);
# endif
if (fd == NULL)
{
EMSG2(_(e_notopen), tempname);
goto done;
}
fseek(fd, 0L, SEEK_END);
len = ftell(fd);
fseek(fd, 0L, SEEK_SET);
buffer = alloc(len + 1);
if (buffer != NULL)
i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
fclose(fd);
mch_remove(tempname);
if (buffer == NULL)
goto done;
#ifdef VMS
len = i;
#endif
if (i != len)
{
EMSG2(_(e_notread), tempname);
vim_free(buffer);
buffer = NULL;
}
else if (ret_len == NULL)
{
for (i = 0; i < len; ++i)
if (buffer[i] == NUL)
buffer[i] = 1;
buffer[len] = NUL;
}
else
*ret_len = len;
done:
vim_free(tempname);
return buffer;
}
#endif
void
FreeWild(int count, char_u **files)
{
if (count <= 0 || files == NULL)
return;
while (count--)
vim_free(files[count]);
vim_free(files);
}
int
goto_im(void)
{
return (p_im && stuff_empty() && typebuf_typed());
}
char_u *
get_isolated_shell_name(void)
{
char_u *p;
#ifdef WIN3264
p = gettail(p_sh);
p = vim_strnsave(p, (int)(skiptowhite(p) - p));
#else
p = skiptowhite(p_sh);
if (*p == NUL)
{
p = vim_strsave(gettail(p_sh));
}
else
{
char_u *p1, *p2;
p1 = p_sh;
for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
if (vim_ispathsep(*p2))
p1 = p2 + 1;
p = vim_strnsave(p1, (int)(p - p1));
}
#endif
return p;
}