#include "vim.h"
#include "version.h"
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
static char_u *vim_version_dir __ARGS((char_u *vimdir));
static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name));
static int copy_indent __ARGS((int size, char_u *src));
int
get_indent()
{
return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts);
}
int
get_indent_lnum(lnum)
linenr_T lnum;
{
return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts);
}
#if defined(FEAT_FOLDING) || defined(PROTO)
int
get_indent_buf(buf, lnum)
buf_T *buf;
linenr_T lnum;
{
return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts);
}
#endif
int
get_indent_str(ptr, ts)
char_u *ptr;
int ts;
{
int count = 0;
for ( ; *ptr; ++ptr)
{
if (*ptr == TAB)
count += ts - (count % ts);
else if (*ptr == ' ')
++count;
else
break;
}
return count;
}
int
set_indent(size, flags)
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;
int tab_pad;
int retval = FALSE;
todo = size;
ind_len = 0;
p = oldline = ml_get_curline();
if (!curbuf->b_p_et)
{
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;
}
tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
if (todo >= tab_pad)
{
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;
newline = alloc(ind_len + line_len);
if (newline == NULL)
return FALSE;
s = newline;
todo = size;
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
&& saved_cursor.col >= (colnr_T)(p - oldline))
saved_cursor.col += ind_len - (colnr_T)(p - oldline);
retval = TRUE;
}
else
vim_free(newline);
curwin->w_cursor.col = ind_len;
return retval;
}
static int
copy_indent(size, src)
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)
{
todo -= tab_pad;
++ind_len;
if (p != NULL)
*p++ = TAB;
}
while (todo >= (int)curbuf->b_p_ts)
{
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(lnum)
linenr_T lnum;
{
colnr_T col;
pos_T pos;
regmmatch_T regmatch;
if (lnum > curbuf->b_ml.ml_line_count)
return -1;
pos.lnum = 0;
regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
if (regmatch.regprog != NULL)
{
regmatch.rmm_ic = FALSE;
regmatch.rmm_maxcol = 0;
if (vim_regexec_multi(®match, curwin, curbuf, lnum, (colnr_T)0))
{
pos.lnum = regmatch.endpos[0].lnum + lnum;
pos.col = regmatch.endpos[0].col;
#ifdef FEAT_VIRTUALEDIT
pos.coladd = 0;
#endif
}
vim_free(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_CINDENT) || defined(FEAT_SMARTINDENT)
static int cin_is_cinword __ARGS((char_u *line));
static int
cin_is_cinword(line)
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(dir, flags, old_indent)
int dir;
int flags;
int old_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
);
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)
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);
if (newindent == 0)
newindent = old_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);
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);
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);
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 + 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 && *p != ':'; ++p)
{
if (*p == COM_RIGHT || *p == COM_LEFT)
c = *p;
else if (VIM_ISDIGIT(*p) || *p == '-')
off = getdigits(&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; p[i] != NUL && i < lead_len; 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 - (leader - p)));
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);
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)
{
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;
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)
{
if (p_sr)
newindent -= newindent % (int)curbuf->b_p_sw;
newindent += (int)curbuf->b_p_sw;
}
#endif
if (curbuf->b_p_ci && !curbuf->b_p_et)
{
(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);
p = ml_get_curline();
ai_col = (colnr_T)(skipwhite(p) - p);
}
#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();
p = ml_get_curline();
ai_col = (colnr_T)(skipwhite(p) - p);
}
#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(line, flags, backward)
char_u *line;
char_u **flags;
int backward;
{
int i, j;
int got_com = FALSE;
int found_one;
char_u part_buf[COM_MAX_LEN];
char_u *string;
char_u *list;
i = 0;
while (vim_iswhite(line[i]))
++i;
while (line[i])
{
found_one = FALSE;
for (list = curbuf->b_p_com; *list; )
{
if (!got_com && flags != NULL)
*flags = list;
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
string = vim_strchr(part_buf, ':');
if (string == NULL)
continue;
*string++ = NUL;
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;
i += j;
got_com = TRUE;
found_one = TRUE;
break;
}
if (!found_one)
break;
while (vim_iswhite(line[i]))
++i;
if (vim_strchr(part_buf, COM_NEST) == NULL)
break;
}
return (got_com ? i : 0);
}
#endif
int
plines(lnum)
linenr_T lnum;
{
return plines_win(curwin, lnum, TRUE);
}
int
plines_win(wp, lnum, winheight)
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(lnum)
linenr_T lnum;
{
return plines_win_nofill(curwin, lnum, TRUE);
}
int
plines_win_nofill(wp, lnum, winheight)
win_T *wp;
linenr_T lnum;
int winheight;
{
#endif
int lines;
if (!wp->w_p_wrap)
return 1;
#ifdef FEAT_VERTSPLIT
if (wp->w_width == 0)
return 1;
#endif
#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(wp, lnum)
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 = W_WIDTH(wp) - 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(wp, lnum, column)
win_T *wp;
linenr_T lnum;
long column;
{
long col;
char_u *s;
int lines = 0;
int width;
#ifdef FEAT_DIFF
lines = diff_check_fill(wp, lnum);
#endif
if (!wp->w_p_wrap)
return lines + 1;
#ifdef FEAT_VERTSPLIT
if (wp->w_width == 0)
return lines + 1;
#endif
s = ml_get_buf(wp->w_buffer, lnum, FALSE);
col = 0;
while (*s != NUL && --column >= 0)
{
col += win_lbr_chartabsize(wp, 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, s, (colnr_T)col, NULL) - 1;
width = W_WIDTH(wp) - win_col_off(wp);
if (width > 0)
{
lines += 1;
if (col >= width)
lines += (col - width) / (width + win_col_off2(wp));
if (lines <= wp->w_height)
return lines;
}
return (int)(wp->w_height);
}
int
plines_m_win(wp, first, last)
win_T *wp;
linenr_T first, 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(p)
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(p, len)
char_u *p;
int len;
{
int i;
# ifdef FEAT_MBYTE
int n;
for (i = 0; i < len; i += n)
{
n = (*mb_ptr2len)(p + i);
ins_char_bytes(p + i, n);
}
# else
for (i = 0; i < len; ++i)
ins_char(p[i]);
# endif
}
#endif
void
ins_char(c)
int c;
{
#if defined(FEAT_MBYTE) || defined(PROTO)
char_u buf[MB_MAXBYTES];
int n;
n = (*mb_char2bytes)(c, buf);
if (buf[0] == 0)
buf[0] = '\n';
ins_char_bytes(buf, n);
}
void
ins_char_bytes(buf, charlen)
char_u *buf;
int charlen;
{
int c = buf[0];
int l, j;
#endif
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;
#ifdef FEAT_MBYTE
newlen = charlen;
#else
newlen = 1;
#endif
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
l = (*mb_ptr2len)(oldp + col + i) - 1;
for (j = l; j >= 0; --j)
replace_push(oldp[col + i + j]);
i += l;
#else
replace_push(oldp[col + i]);
#endif
}
}
newp = alloc_check((unsigned)(linelen + newlen - oldlen));
if (newp == NULL)
return;
if (col > 0)
mch_memmove(newp, oldp, (size_t)col);
p = newp + col;
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_MBYTE
&& charlen == 1
#endif
#ifdef FEAT_INS_EXPAND
&& !ins_compl_active()
#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(s)
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(fixpos)
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(count, fixpos)
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(count, fixpos_arg, use_delcombine)
long count;
int fixpos_arg;
int use_delcombine;
{
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;
}
was_alloced = ml_line_alloced();
#ifdef FEAT_NETBEANS_INTG
if (was_alloced && usingNetbeans)
netbeans_removed(curbuf, lnum, col, count);
#endif
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(fixpos)
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(nlines, undo)
long nlines;
int undo;
{
long n;
if (nlines <= 0)
return;
if (undo && u_savedel(curwin->w_cursor.lnum, nlines) == FAIL)
return;
for (n = 0; n < nlines; )
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
break;
ml_delete(curwin->w_cursor.lnum, TRUE);
++n;
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
break;
}
deleted_lines_mark(curwin->w_cursor.lnum, n);
curwin->w_cursor.col = 0;
check_cursor_lnum();
}
int
gchar_pos(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()
{
#ifdef FEAT_MBYTE
if (has_mbyte)
return (*mb_ptr2char)(ml_get_cursor());
#endif
return (int)*ml_get_cursor();
}
void
pchar_cursor(c)
int c;
{
*(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
+ curwin->w_cursor.col) = c;
}
#if 0
void
goto_endofbuf(pos)
pos_T *pos;
{
char_u *p;
pos->lnum = curbuf->b_ml.ml_line_count;
pos->col = 0;
p = ml_get(pos->lnum);
while (*p++)
++pos->col;
}
#endif
int
inindent(extra)
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(p)
char_u *p;
{
if (*p == ',')
++p;
while (*p == ' ')
++p;
return p;
}
void
changed()
{
#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
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
)
{
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;
}
}
curbuf->b_changed = TRUE;
ml_setflags(curbuf);
#ifdef FEAT_WINDOWS
check_status(curbuf);
redraw_tabline = TRUE;
#endif
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
}
++curbuf->b_changedtick;
}
static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
void
changed_bytes(lnum, col)
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 (wp = firstwin; wp != NULL; wp = wp->w_next)
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, lnum)
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(lnum, count)
linenr_T lnum;
long count;
{
changed_lines(lnum + 1, 0, lnum + 1, count);
}
void
appended_lines_mark(lnum, count)
linenr_T lnum;
long count;
{
mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
changed_lines(lnum + 1, 0, lnum + 1, count);
}
void
deleted_lines(lnum, count)
linenr_T lnum;
long count;
{
changed_lines(lnum, 0, lnum + count, -count);
}
void
deleted_lines_mark(lnum, count)
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(lnum, col, lnume, xtra)
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 (wp = firstwin; wp != NULL; wp = wp->w_next)
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, lnum, lnume, xtra)
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(lnum, col, lnume, xtra)
linenr_T lnum;
colnr_T col;
linenr_T lnume;
long xtra;
{
win_T *wp;
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_WINDOWS(wp)
{
if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
--wp->w_changelistidx;
}
}
FOR_ALL_WINDOWS(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_WINDOWS(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
}
}
}
if (must_redraw < VALID)
must_redraw = VALID;
#ifdef FEAT_AUTOCMD
if (lnum <= curwin->w_cursor.lnum && lnume > curwin->w_cursor.lnum)
last_cursormoved.lnum = 0;
#endif
}
void
unchanged(buf, ff)
buf_T *buf;
int ff;
{
if (buf->b_changed || (ff && file_ff_differs(buf)))
{
buf->b_changed = 0;
ml_setflags(buf);
if (ff)
save_file_ff(buf);
#ifdef FEAT_WINDOWS
check_status(buf);
redraw_tabline = TRUE;
#endif
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
}
++buf->b_changedtick;
#ifdef FEAT_NETBEANS_INTG
netbeans_unmodified(buf);
#endif
}
#if defined(FEAT_WINDOWS) || defined(PROTO)
void
check_status(buf)
buf_T *buf;
{
win_T *wp;
for (wp = firstwin; wp != NULL; wp = wp->w_next)
if (wp->w_buffer == buf && wp->w_status_height)
{
wp->w_redr_status = TRUE;
if (must_redraw < VALID)
must_redraw = VALID;
}
}
#endif
void
change_warning(col)
int col;
{
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(_("W10: Warning: Changing a readonly file"),
hl_attr(HLF_W) | MSG_HIST);
msg_clr_eos();
(void)msg_end();
if (msg_silent == 0 && !silent_mode)
{
out_flush();
ui_delay(1000L, TRUE);
}
curbuf->b_did_warn = TRUE;
redraw_cmdline = FALSE;
if (msg_row < Rows - 1)
showmode();
}
}
int
ask_yesno(str, direct)
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 = safe_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;
}
int
get_keystroke()
{
#define CBUFLEN 151
char_u buf[CBUFLEN];
int len = 0;
int n;
int save_mapped_ctrl_c = mapped_ctrl_c;
mapped_ctrl_c = FALSE;
for (;;)
{
cursor_on();
out_flush();
n = ui_inchar(buf + len, (CBUFLEN - 6 - len) / 3,
len == 0 ? -1L : 100L, 0);
if (n > 0)
{
n = fix_input_buffer(buf + len, n, FALSE);
len += n;
}
if ((n = check_termcode(1, buf, len)) < 0)
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
|| n == K_LEFTMOUSE_NM
|| n == K_LEFTDRAG
|| n == K_LEFTRELEASE
|| n == K_LEFTRELEASE_NM
|| n == K_MIDDLEMOUSE
|| n == K_MIDDLEDRAG
|| n == K_MIDDLERELEASE
|| n == K_RIGHTMOUSE
|| n == K_RIGHTDRAG
|| n == K_RIGHTRELEASE
|| n == K_MOUSEDOWN
|| n == K_MOUSEUP
|| n == K_X1MOUSE
|| n == K_X1DRAG
|| n == K_X1RELEASE
|| n == K_X2MOUSE
|| n == K_X2DRAG
|| n == K_X2RELEASE
# ifdef FEAT_GUI
|| n == K_VER_SCROLLBAR
|| n == K_HOR_SCROLLBAR
# endif
#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 >= CBUFLEN ? CBUFLEN - 1 : len] = NUL;
n = (*mb_ptr2char)(buf);
}
#endif
#ifdef UNIX
if (n == intr_char)
n = ESC;
#endif
break;
}
mapped_ctrl_c = save_mapped_ctrl_c;
return n;
}
int
get_number(colon, mouse_used)
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(mouse_used)
int *mouse_used;
{
int i;
int save_cmdline_row;
int save_State;
if (mouse_used != NULL)
MSG_PUTS(_("Type number or click with mouse (<Enter> cancels): "));
else
MSG_PUTS(_("Choice number (<Enter> cancels): "));
save_cmdline_row = cmdline_row;
cmdline_row = Rows - 1;
save_State = State;
if (mouse_used == NULL)
State = CMDLINE;
else
State = NORMAL;
i = get_number(TRUE, mouse_used);
if (KeyTyped)
{
cmdline_row = msg_row - 1;
need_wait_return = FALSE;
msg_didany = FALSE;
}
else
cmdline_row = save_cmdline_row;
State = save_State;
return i;
}
void
msgmore(n)
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)
STRCPY(msg_buf, _("1 more line"));
else
STRCPY(msg_buf, _("1 line less"));
}
else
{
if (n > 0)
sprintf((char *)msg_buf, _("%ld more lines"), pn);
else
sprintf((char *)msg_buf, _("%ld fewer lines"), pn);
}
if (got_int)
STRCAT(msg_buf, _(" (Interrupted)"));
if (msg(msg_buf))
{
set_keep_msg(msg_buf, 0);
keep_msg_more = TRUE;
}
}
}
void
beep_flush()
{
if (emsg_silent == 0)
{
flush_buffers(FALSE);
vim_beep();
}
}
void
vim_beep()
{
if (emsg_silent == 0)
{
if (p_vb
#ifdef FEAT_GUI
&& !(gui.in_use && gui.starting)
#endif
)
{
out_str(T_VB);
}
else
{
#ifdef MSDOS
if (beep_count == 0 || beep_count == 10)
{
out_char(BELL);
beep_count = 1;
}
else
++beep_count;
#else
out_char(BELL);
#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()
{
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
if (var != NULL && *var == NUL)
var = NULL;
#ifdef WIN3264
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;
vim_setenv((char_u *)"HOME", NameBuff);
}
}
}
if (var == NULL)
{
char_u *homedrive, *homepath;
homedrive = mch_getenv((char_u *)"HOMEDRIVE");
homepath = mch_getenv((char_u *)"HOMEPATH");
if (homedrive != NULL && homepath != NULL
&& STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
{
sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
if (NameBuff[0] != NUL)
{
var = NameBuff;
vim_setenv((char_u *)"HOME", NameBuff);
}
}
}
# if defined(FEAT_MBYTE)
if (enc_utf8 && var != NULL)
{
int len;
char_u *pp;
acp_to_enc(var, (int)STRLEN(var), &pp, &len);
if (pp != NULL)
{
homedir = pp;
return;
}
}
# endif
#endif
#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
if (var == NULL)
var = "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()
{
vim_free(homedir);
}
#endif
void
expand_env(src, dst, dstlen)
char_u *src;
char_u *dst;
int dstlen;
{
expand_env_esc(src, dst, dstlen, FALSE, NULL);
}
void
expand_env_esc(srcp, dst, dstlen, esc, startstr)
char_u *srcp;
char_u *dst;
int dstlen;
int esc;
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)
{
copy_char = TRUE;
if ((*src == '$'
#ifdef VMS
&& at_start
#endif
)
#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
|| *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(MSDOS) || defined(MSWIN) || defined(OS2)
|| (*src == '%' && *tail != '%')
#endif
))
{
#ifdef OS2
*var++ = TOUPPER_LOC(*tail);
tail++;
#else
*var++ = *tail++;
#endif
}
}
#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || 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(MSDOS) || defined(MSWIN) || defined(OS2) || 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;
pw = getpwnam((char *)dst + 1);
if (pw != NULL)
var = (char_u *)pw->pw_dir;
else
var = NULL;
}
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;
struct stat 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] == ',')
at_start = TRUE;
*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(name, mustfree)
char_u *name;
int *mustfree;
{
char_u *p;
char_u *pend;
int vimruntime;
#if defined(OS2) || defined(MSDOS) || 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;
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;
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);
}
#ifndef MACOS_CLASSIC
if (pend > p && after_pathsep(p, pend))
--pend;
#endif
#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;
#ifdef FEAT_GETTEXT
{
char_u *buf = concat_str(p, (char_u *)"/lang");
if (buf != NULL)
{
bindtextdomain(VIMPACKAGE, (char *)buf);
vim_free(buf);
}
}
#endif
}
else
{
vim_setenv((char_u *)"VIM", p);
didset_vim = TRUE;
}
}
return p;
}
static char_u *
vim_version_dir(vimdir)
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(p, pend, name)
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;
}
char_u *
expand_env_save(src)
char_u *src;
{
char_u *p;
p = alloc(MAXPATHL);
if (p != NULL)
expand_env(src, p, MAXPATHL);
return p;
}
void
vim_setenv(name, val)
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
}
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
char_u *
get_env_name(xp, idx)
expand_T *xp;
int idx;
{
# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
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
}
#endif
void
home_replace(buf, src, dst, dstlen, one)
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;
char_u *p;
if (src == NULL)
{
*dst = NUL;
return;
}
if (buf != NULL && buf->b_help)
{
STRCPY(dst, gettail(src));
return;
}
if (homedir != NULL)
dirlen = STRLEN(homedir);
#ifdef VMS
homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
#else
homedir_env = mch_getenv((char_u *)"HOME");
#endif
if (homedir_env != NULL && *homedir_env == NUL)
homedir_env = NULL;
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;
}
char_u *
home_replace_save(buf, src)
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(s1, s2, checkname)
char_u *s1, *s2;
int checkname;
{
#ifdef UNIX
char_u exp1[MAXPATHL];
char_u full1[MAXPATHL];
char_u full2[MAXPATHL];
struct stat 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(fname)
char_u *fname;
{
char_u *p1, *p2;
if (fname == NULL)
return (char_u *)"";
for (p1 = p2 = fname; *p2; )
{
if (vim_ispathsep(*p2))
p1 = p2 + 1;
mb_ptr_adv(p2);
}
return p1;
}
char_u *
gettail_sep(fname)
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(fname)
char_u *fname;
{
while (*fname && !vim_ispathsep(*fname))
mb_ptr_adv(fname);
if (*fname)
++fname;
return fname;
}
char_u *
get_past_head(path)
char_u *path;
{
char_u *retval;
#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
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(c)
int c;
{
#ifdef RISCOS
return (c == '.' || c == ':');
#else
# 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
#endif
}
#if defined(FEAT_SEARCHPATH) || defined(PROTO)
int
vim_ispathlistsep(c)
int c;
{
#ifdef UNIX
return (c == ':');
#else
return (c == ';');
#endif
}
#endif
#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
|| defined(FEAT_EVAL) || defined(PROTO)
void
shorten_dir(str)
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
}
}
}
#endif
int
dir_of_file_exists(fname)
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;
}
#if (defined(CASE_INSENSITIVE_FILENAME) && defined(BACKSLASH_IN_FILENAME)) \
|| defined(PROTO)
int
vim_fnamecmp(x, y)
char_u *x, *y;
{
return vim_fnamencmp(x, y, MAXPATHL);
}
int
vim_fnamencmp(x, y, len)
char_u *x, *y;
size_t len;
{
while (len > 0 && *x && *y)
{
if (TOLOWER_LOC(*x) != TOLOWER_LOC(*y)
&& !(*x == '/' && *y == '\\')
&& !(*x == '\\' && *y == '/'))
break;
++x;
++y;
--len;
}
if (len == 0)
return 0;
return (*x - *y);
}
#endif
char_u *
concat_fnames(fname1, fname2, sep)
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;
}
#if defined(FEAT_EVAL) || defined(FEAT_GETTEXT) || defined(PROTO)
char_u *
concat_str(str1, str2)
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;
}
#endif
void
add_pathsep(p)
char_u *p;
{
if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
STRCAT(p, PATHSEPSTR);
}
char_u *
FullName_save(fname, force)
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 __ARGS((char_u *p));
pos_T *
find_start_comment(ind_maxcomment)
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 && (unsigned)(p - line) < pos->col; ++p)
p = skip_string(p);
if ((unsigned)(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(p)
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;
}
break;
}
if (!*p)
--p;
return p;
}
#endif
#if defined(FEAT_CINDENT) || defined(PROTO)
void
do_c_expr_indent()
{
# ifdef FEAT_EVAL
if (*curbuf->b_p_inde != NUL)
fixthisline(get_expr_indent);
else
# endif
fixthisline(get_c_indent);
}
static char_u *cin_skipcomment __ARGS((char_u *));
static int cin_nocode __ARGS((char_u *));
static pos_T *find_line_comment __ARGS((void));
static int cin_islabel_skip __ARGS((char_u **));
static int cin_isdefault __ARGS((char_u *));
static char_u *after_label __ARGS((char_u *l));
static int get_indent_nolabel __ARGS((linenr_T lnum));
static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
static int cin_first_id_amount __ARGS((void));
static int cin_get_equal_amount __ARGS((linenr_T lnum));
static int cin_ispreproc __ARGS((char_u *));
static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
static int cin_iscomment __ARGS((char_u *));
static int cin_islinecomment __ARGS((char_u *));
static int cin_isterminated __ARGS((char_u *, int, int));
static int cin_isinit __ARGS((void));
static int cin_isfuncdecl __ARGS((char_u **, linenr_T));
static int cin_isif __ARGS((char_u *));
static int cin_iselse __ARGS((char_u *));
static int cin_isdo __ARGS((char_u *));
static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment));
static int cin_isbreak __ARGS((char_u *));
static int cin_is_cpp_baseclass __ARGS((char_u *line, colnr_T *col));
static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass));
static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
static int cin_skip2pos __ARGS((pos_T *trypos));
static pos_T *find_start_brace __ARGS((int));
static pos_T *find_match_paren __ARGS((int, int));
static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
static int find_last_paren __ARGS((char_u *l, int start, int end));
static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
static char_u *
cin_skipcomment(s)
char_u *s;
{
while (*s)
{
s = skipwhite(s);
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(s)
char_u *s;
{
return *cin_skipcomment(s) == NUL;
}
static pos_T *
find_line_comment()
{
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_islabel_skip(s)
char_u **s;
{
if (!vim_isIDc(**s))
return FALSE;
while (vim_isIDc(**s))
(*s)++;
*s = cin_skipcomment(*s);
return (**s == ':' && *++*s != ':');
}
int
cin_islabel(ind_maxcomment)
int ind_maxcomment;
{
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 = find_start_comment(ind_maxcomment)) != 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)
|| (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;
s = cin_skipcomment(ml_get_curline());
if (STRNCMP(s, "typedef", 7) == 0 && !vim_isIDc(s[7]))
s = cin_skipcomment(s + 7);
if (STRNCMP(s, "enum", 4) == 0 && !vim_isIDc(s[4]))
return TRUE;
if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
return TRUE;
return FALSE;
}
int
cin_iscase(s)
char_u *s;
{
s = cin_skipcomment(s);
if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4]))
{
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 == '"')
return FALSE;
}
return FALSE;
}
if (cin_isdefault(s))
return TRUE;
return FALSE;
}
static int
cin_isdefault(s)
char_u *s;
{
return (STRNCMP(s, "default", 7) == 0
&& *(s = cin_skipcomment(s + 7)) == ':'
&& s[1] != ':');
}
int
cin_isscopedecl(s)
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] != ':');
}
static char_u *
after_label(l)
char_u *l;
{
for ( ; *l; ++l)
{
if (*l == ':')
{
if (l[1] == ':')
++l;
else if (!cin_iscase(l + 1))
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(lnum)
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(lnum, pp, ind_maxcomment)
linenr_T lnum;
char_u **pp;
int ind_maxcomment;
{
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) || cin_isscopedecl(l) || cin_islabel(ind_maxcomment))
{
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()
{
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(lnum)
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(s)
char_u *s;
{
s = skipwhite(s);
if (*s == '#')
return TRUE;
return FALSE;
}
static int
cin_ispreproc_cont(pp, lnump)
char_u **pp;
linenr_T *lnump;
{
char_u *line = *pp;
linenr_T lnum = *lnump;
int retval = FALSE;
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);
return retval;
}
static int
cin_iscomment(p)
char_u *p;
{
return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
}
static int
cin_islinecomment(p)
char_u *p;
{
return (p[0] == '/' && p[1] == '/');
}
static int
cin_isterminated(s, incl_open, incl_comma)
char_u *s;
int incl_open;
int incl_comma;
{
char_u found_start = 0;
s = cin_skipcomment(s);
if (*s == '{' || (*s == '}' && !cin_iselse(s)))
found_start = *s;
while (*s)
{
s = skip_string(cin_skipcomment(s));
if ((*s == ';' || (incl_open && *s == '{') || *s == '}'
|| (incl_comma && *s == ','))
&& cin_nocode(s + 1))
return *s;
if (*s)
s++;
}
return found_start;
}
static int
cin_isfuncdecl(sp, first_lnum)
char_u **sp;
linenr_T first_lnum;
{
char_u *s;
linenr_T lnum = first_lnum;
int retval = FALSE;
if (sp == NULL)
s = ml_get(lnum);
else
s = *sp;
while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
{
if (cin_iscomment(s))
s = cin_skipcomment(s);
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))
{
if (lnum >= curbuf->b_ml.ml_line_count)
break;
s = ml_get(++lnum);
}
else if (cin_iscomment(s))
s = cin_skipcomment(s);
else
++s;
}
done:
if (lnum != first_lnum && sp != NULL)
*sp = ml_get(first_lnum);
return retval;
}
static int
cin_isif(p)
char_u *p;
{
return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
}
static int
cin_iselse(p)
char_u *p;
{
if (*p == '}')
p = cin_skipcomment(p + 1);
return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
}
static int
cin_isdo(p)
char_u *p;
{
return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
}
static int
cin_iswhileofdo(p, lnum, ind_maxparen)
char_u *p;
linenr_T lnum;
int ind_maxparen;
{
pos_T cursor_save;
pos_T *trypos;
int retval = FALSE;
p = cin_skipcomment(p);
if (*p == '}')
p = cin_skipcomment(p + 1);
if (STRNCMP(p, "while", 5) == 0 && !vim_isIDc(p[5]))
{
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, ind_maxparen)) != NULL
&& *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
retval = TRUE;
curwin->w_cursor = cursor_save;
}
return retval;
}
static int
cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment)
int terminated;
int ind_maxparen;
int ind_maxcomment;
{
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(ind_maxparen, ind_maxcomment);
if (trypos != NULL)
{
s = cin_skipcomment(ml_get(trypos->lnum));
if (*s == '}')
s = cin_skipcomment(s + 1);
if (STRNCMP(s, "while", 5) == 0 && !vim_isIDc(s[5]))
{
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(p)
char_u *p;
{
return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
}
static int
cin_is_cpp_baseclass(line, col)
char_u *line;
colnr_T *col;
{
char_u *s;
int class_or_struct, lookfor_ctor_init, cpp_base_class;
linenr_T lnum = curwin->w_cursor.lnum;
*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)
{
s = skipwhite(ml_get(lnum - 1));
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;
}
s = cin_skipcomment(ml_get(lnum));
for (;;)
{
if (*s == NUL)
{
if (lnum == curwin->w_cursor.lnum)
break;
s = cin_skipcomment(ml_get(++lnum));
}
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;
*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 (*col == 0)
{
lookfor_ctor_init = FALSE;
if (cpp_base_class)
*col = (colnr_T)(s - line);
}
if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
*col = 0;
s = cin_skipcomment(s + 1);
}
}
return cpp_base_class;
}
static int
get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass)
int col;
int ind_maxparen;
int ind_maxcomment;
int ind_cpp_baseclass;
{
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(ind_maxparen,
ind_maxcomment)) != NULL)
amount = get_indent_lnum(trypos->lnum);
if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
amount += ind_cpp_baseclass;
}
else
{
curwin->w_cursor.col = col;
getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
amount = (int)vcol;
}
if (amount < ind_cpp_baseclass)
amount = ind_cpp_baseclass;
return amount;
}
static int
cin_ends_in(s, find, ignore)
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_skip2pos(trypos)
pos_T *trypos;
{
char_u *line;
char_u *p;
p = line = ml_get(trypos->lnum);
while (*p && (colnr_T)(p - line) < trypos->col)
{
if (cin_iscomment(p))
p = cin_skipcomment(p);
else
{
p = skip_string(p);
++p;
}
}
return (int)(p - line);
}
static pos_T *
find_start_brace(ind_maxcomment)
int ind_maxcomment;
{
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 = find_start_comment(ind_maxcomment)) == NULL)
break;
if (pos != NULL)
curwin->w_cursor.lnum = pos->lnum;
}
curwin->w_cursor = cursor_save;
return trypos;
}
static pos_T *
find_match_paren(ind_maxparen, ind_maxcomment)
int ind_maxparen;
int ind_maxcomment;
{
pos_T cursor_save;
pos_T *trypos;
static pos_T pos_copy;
cursor_save = curwin->w_cursor;
if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
{
if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
trypos = NULL;
else
{
pos_copy = *trypos;
trypos = &pos_copy;
curwin->w_cursor = *trypos;
if (find_start_comment(ind_maxcomment) != NULL)
trypos = NULL;
}
}
curwin->w_cursor = cursor_save;
return trypos;
}
static int
corr_ind_maxparen(ind_maxparen, startpos)
int ind_maxparen;
pos_T *startpos;
{
long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
if (n > 0 && n < ind_maxparen / 2)
return ind_maxparen - (int)n;
return ind_maxparen;
}
static int
find_last_paren(l, start, end)
char_u *l;
int start, end;
{
int i;
int retval = FALSE;
int open_count = 0;
curwin->w_cursor.col = 0;
for (i = 0; l[i]; 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;
}
int
get_c_indent()
{
int ind_level = curbuf->b_p_sw;
int ind_open_imag = 0;
int ind_no_brace = 0;
int ind_first_open = 0;
int ind_open_extra = 0;
int ind_close_extra = 0;
int ind_open_left_imag = 0;
int ind_case = curbuf->b_p_sw;
int ind_case_code = curbuf->b_p_sw;
int ind_case_break = 0;
int ind_scopedecl = curbuf->b_p_sw;
int ind_scopedecl_code = curbuf->b_p_sw;
int ind_param = curbuf->b_p_sw;
int ind_func_type = curbuf->b_p_sw;
int ind_cpp_baseclass = curbuf->b_p_sw;
int ind_continuation = curbuf->b_p_sw;
int ind_unclosed = curbuf->b_p_sw * 2;
int ind_unclosed2 = curbuf->b_p_sw;
int ind_unclosed_noignore = 0;
int ind_unclosed_wrapped = 0;
int ind_unclosed_whiteok = 0;
int ind_matching_paren = 0;
int ind_paren_prev = 0;
int ind_comment = 0;
int ind_in_comment = 3;
int ind_in_comment2 = 0;
int ind_maxparen = 20;
int ind_maxcomment = 70;
int ind_java = 0;
int ind_keep_case_label = 0;
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 *tryposBrace = NULL;
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
int whilelevel;
linenr_T lnum;
char_u *options;
int fraction = 0;
int divider;
int n;
int iscase;
int lookfor_break;
int cont_amount = 0;
for (options = curbuf->b_p_cino; *options; )
{
l = options++;
if (*options == '-')
++options;
n = getdigits(&options);
divider = 0;
if (*options == '.')
{
fraction = atol((char *)++options);
while (VIM_ISDIGIT(*options))
{
++options;
if (divider)
divider *= 10;
else
divider = 10;
}
}
if (*options == 's')
{
if (n == 0 && fraction == 0)
n = curbuf->b_p_sw;
else
{
n *= curbuf->b_p_sw;
if (divider)
n += (curbuf->b_p_sw * fraction + divider / 2) / divider;
}
++options;
}
if (l[1] == '-')
n = -n;
switch (*l)
{
case '>': ind_level = n; break;
case 'e': ind_open_imag = n; break;
case 'n': ind_no_brace = n; break;
case 'f': ind_first_open = n; break;
case '{': ind_open_extra = n; break;
case '}': ind_close_extra = n; break;
case '^': ind_open_left_imag = n; break;
case ':': ind_case = n; break;
case '=': ind_case_code = n; break;
case 'b': ind_case_break = n; break;
case 'p': ind_param = n; break;
case 't': ind_func_type = n; break;
case '/': ind_comment = n; break;
case 'c': ind_in_comment = n; break;
case 'C': ind_in_comment2 = n; break;
case 'i': ind_cpp_baseclass = n; break;
case '+': ind_continuation = n; break;
case '(': ind_unclosed = n; break;
case 'u': ind_unclosed2 = n; break;
case 'U': ind_unclosed_noignore = n; break;
case 'W': ind_unclosed_wrapped = n; break;
case 'w': ind_unclosed_whiteok = n; break;
case 'm': ind_matching_paren = n; break;
case 'M': ind_paren_prev = n; break;
case ')': ind_maxparen = n; break;
case '*': ind_maxcomment = n; break;
case 'g': ind_scopedecl = n; break;
case 'h': ind_scopedecl_code = n; break;
case 'j': ind_java = n; break;
case 'l': ind_keep_case_label = n; break;
}
}
cur_curpos = curwin->w_cursor;
linecopy = vim_strsave(ml_get(cur_curpos.lnum));
if (linecopy == NULL)
return 0;
if ((State & INSERT)
&& curwin->w_cursor.col < STRLEN(linecopy)
&& linecopy[curwin->w_cursor.col] == ')')
linecopy[curwin->w_cursor.col] = NUL;
theline = skipwhite(linecopy);
curwin->w_cursor.col = 0;
if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
{
amount = 0;
}
else if (cin_islabel(ind_maxcomment))
{
amount = 0;
}
else if (cin_islinecomment(theline)
&& (trypos = find_line_comment()) != NULL)
{
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
}
else if (!cin_iscomment(theline)
&& (trypos = find_start_comment(ind_maxcomment)) != 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, trypos, &col, NULL, NULL);
amount = col;
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(trypos->lnum) + trypos->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 > trypos->lnum; --lnum)
{
if (linewhite(lnum))
continue;
amount = get_indent_lnum(lnum);
break;
}
if (amount == -1)
{
if (!ind_in_comment2)
{
start = ml_get(trypos->lnum);
look = start + trypos->col + 2;
if (*look != NUL)
trypos->col = (colnr_T)(skipwhite(look) - start);
}
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
if (ind_in_comment2 || *look == NUL)
amount += ind_in_comment;
}
}
}
else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
&& ind_java == 0)
|| (tryposBrace = find_start_brace(ind_maxcomment)) != 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] == ')' && 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))
continue;
curwin->w_cursor.lnum = lnum;
if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
{
lnum = trypos->lnum + 1;
continue;
}
if ((trypos = find_match_paren(
corr_ind_maxparen(ind_maxparen, &cur_curpos),
ind_maxcomment)) != 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;
amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment);
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, 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] == ')' || ind_unclosed == 0
|| (!ind_unclosed_noignore && *look == '('
&& ignore_paren_col == 0))
{
if (theline[0] != ')')
{
cur_amount = MAXCOL;
l = ml_get(our_paren_pos.lnum);
if (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 * ind_unclosed_wrapped;
}
else if (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] == ')' && ind_matching_paren)
{
}
else if (ind_unclosed == 0 || (!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 += ind_unclosed2;
col = our_paren_pos.col;
break;
case ')': amount -= ind_unclosed2;
col = MAXCOL;
break;
}
}
if (col == MAXCOL)
amount += ind_unclosed;
else
{
curwin->w_cursor.lnum = our_paren_pos.lnum;
curwin->w_cursor.col = col;
if ((trypos = find_match_paren(ind_maxparen,
ind_maxcomment)) != NULL)
amount += ind_unclosed2;
else
amount += ind_unclosed;
}
if (cur_amount < amount)
amount = cur_amount;
}
}
if (cin_iscomment(theline))
amount += ind_comment;
}
else
{
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(ind_maxparen,
ind_maxcomment)) != NULL)
lnum = trypos->lnum;
if (ind_keep_case_label && cin_iscase(skipwhite(ml_get_curline())))
amount = get_indent();
else
amount = skip_label(lnum, &l, ind_maxcomment);
start_brace = BRACE_AT_END;
}
if (theline[0] == '}')
{
amount += ind_close_extra;
}
else
{
lookfor = LOOKFOR_INITIAL;
if (cin_iselse(theline))
lookfor = LOOKFOR_IF;
else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen))
lookfor = LOOKFOR_DO;
if (lookfor != LOOKFOR_INITIAL)
{
curwin->w_cursor.lnum = cur_curpos.lnum;
if (find_match(lookfor, ourscope, ind_maxparen,
ind_maxcomment) == OK)
{
amount = get_indent();
goto theend;
}
}
if (start_brace == BRACE_IN_COL0)
{
amount = ind_open_left_imag;
}
else
{
if (start_brace == BRACE_AT_END)
amount += ind_open_imag;
else
{
amount -= ind_open_extra;
if (amount < 0)
amount = 0;
}
}
lookfor_break = FALSE;
if (cin_iscase(theline))
{
lookfor = LOOKFOR_CASE;
amount += ind_case;
}
else if (cin_isscopedecl(theline))
{
lookfor = LOOKFOR_SCOPEDECL;
amount += ind_scopedecl;
}
else
{
if (ind_case_break && cin_isbreak(theline))
lookfor_break = TRUE;
lookfor = LOOKFOR_INITIAL;
amount += 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 - ind_maxparen)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
l = ml_get_curline();
trypos = find_start_comment(ind_maxcomment);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
continue;
}
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
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))
{
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(ind_maxparen,
ind_maxcomment);
if (trypos == NULL && find_last_paren(l, '{', '}'))
trypos = find_start_brace(ind_maxcomment);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
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)
{
amount = scope_amount;
if (theline[0] == '{')
amount += ind_open_extra;
}
break;
}
if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
continue;
}
l = ml_get_curline();
iscase = cin_iscase(l);
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(ind_maxcomment)) ==
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 += ind_open_extra;
else
amount += ind_level + ind_no_brace;
}
break;
}
scope_amount = get_indent() + (iscase
? ind_case_code : ind_scopedecl_code);
lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
continue;
}
if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
{
if (find_last_paren(l, '{', '}') && (trypos =
find_start_brace(ind_maxcomment)) != NULL)
curwin->w_cursor.lnum = trypos->lnum + 1;
continue;
}
if (cin_islabel(ind_maxcomment))
{
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)
|| cin_nocode(l))
continue;
n = FALSE;
if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
{
n = cin_is_cpp_baseclass(l, &col);
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(col, ind_maxparen,
ind_maxcomment, ind_cpp_baseclass);
break;
}
else if (lookfor == LOOKFOR_CPP_BASECLASS)
{
if (cin_isterminated(l, TRUE, FALSE))
break;
else
continue;
}
terminated = cin_isterminated(l, FALSE, TRUE);
if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
&& terminated == ','))
{
(void)find_last_paren(l, '(', ')');
trypos = find_match_paren(
corr_ind_maxparen(ind_maxparen, &cur_curpos),
ind_maxcomment);
if (trypos == NULL && terminated == ','
&& find_last_paren(l, '{', '}'))
trypos = find_start_brace(ind_maxcomment);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum;
l = ml_get_curline();
if (cin_iscase(l) || cin_isscopedecl(l))
{
++curwin->w_cursor.lnum;
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;
}
}
cur_amount = skip_label(curwin->w_cursor.lnum,
&l, ind_maxcomment);
if (terminated != ',' && lookfor != LOOKFOR_TERM
&& theline[0] == '{')
{
amount = cur_amount;
if (*skipwhite(l) != '{')
amount += ind_open_extra;
if (ind_cpp_baseclass)
{
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 += ind_open_extra;
if (lookfor != LOOKFOR_TERM)
{
amount += ind_level + ind_no_brace;
break;
}
l = skipwhite(ml_get_curline());
if (cin_isdo(l))
{
if (whilelevel == 0)
break;
--whilelevel;
}
if (cin_iselse(l)
&& whilelevel == 0
&& ((trypos = find_start_brace(ind_maxcomment))
== NULL
|| find_match(LOOKFOR_IF, trypos->lnum,
ind_maxparen, ind_maxcomment) == FAIL))
break;
}
else
{
if (lookfor == LOOKFOR_UNTERM)
{
if (terminated == ',')
amount += ind_continuation;
break;
}
if (lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (terminated == ',')
{
if (ind_cpp_baseclass == 0)
break;
lookfor = LOOKFOR_CPP_BASECLASS;
continue;
}
if (amount > cur_amount)
amount = cur_amount;
}
else
{
amount = cur_amount;
if (lookfor == LOOKFOR_INITIAL && terminated == ',')
{
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_UNTERM;
}
}
}
}
else if (cin_iswhileofdo_end(terminated, ind_maxparen,
ind_maxcomment))
{
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 += 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(ind_maxparen,
ind_maxcomment)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum;
l = ml_get_curline();
if (cin_iscase(l) || cin_isscopedecl(l))
{
++curwin->w_cursor.lnum;
continue;
}
}
iscase = (ind_keep_case_label && cin_iscase(l));
amount = skip_label(curwin->w_cursor.lnum,
&l, ind_maxcomment);
if (theline[0] == '{')
amount += ind_open_extra;
l = skipwhite(l);
if (*l == '{')
amount -= ind_open_extra;
lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
if (lookfor == LOOKFOR_TERM
&& *l != '}'
&& cin_iselse(l)
&& whilelevel == 0)
{
if ((trypos = find_start_brace(ind_maxcomment))
== NULL
|| find_match(LOOKFOR_IF, trypos->lnum,
ind_maxparen, ind_maxcomment) == FAIL)
break;
continue;
}
curwin->w_cursor.col = 0;
if (*cin_skipcomment(l) == '}'
&& (trypos = find_start_brace(ind_maxcomment))
!= NULL)
{
curwin->w_cursor.lnum = trypos->lnum;
l = cin_skipcomment(ml_get_curline());
if (*l == '}' || !cin_iselse(l))
goto term_again;
++curwin->w_cursor.lnum;
}
}
}
}
}
}
if (cin_iscomment(theline))
amount += ind_comment;
}
else
{
if (theline[0] == '{')
{
amount = ind_first_open;
}
else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
&& !cin_nocode(theline)
&& !cin_ends_in(theline, (char_u *)":", NULL)
&& !cin_ends_in(theline, (char_u *)",", NULL)
&& cin_isfuncdecl(NULL, cur_curpos.lnum + 1)
&& !cin_isterminated(theline, FALSE, TRUE))
{
amount = ind_func_type;
}
else
{
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 = find_start_comment(ind_maxcomment)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
continue;
}
n = FALSE;
if (ind_cpp_baseclass != 0 && theline[0] != '{')
{
n = cin_is_cpp_baseclass(l, &col);
l = ml_get_curline();
}
if (n)
{
amount = get_baseclass_amount(col, ind_maxparen,
ind_maxcomment, ind_cpp_baseclass);
break;
}
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
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(ind_maxparen,
ind_maxcomment)) != NULL)
curwin->w_cursor.lnum = trypos->lnum;
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;
}
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))
break;
l = ml_get_curline();
if (*skipwhite(l) == '}')
break;
if (cin_ends_in(l, (char_u *)"};", NULL))
break;
if (cin_isfuncdecl(&l, curwin->w_cursor.lnum))
{
amount = 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(ind_maxparen,
ind_maxcomment)) != NULL)
curwin->w_cursor.lnum = trypos->lnum;
amount = get_indent();
break;
}
if (cin_iscomment(theline))
amount += 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:
curwin->w_cursor = cur_curpos;
vim_free(linecopy);
if (amount < 0)
return 0;
return amount;
}
static int
find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
int lookfor;
linenr_T ourscope;
int ind_maxparen;
int ind_maxcomment;
{
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, ind_maxparen))
{
theirscope = find_start_brace(ind_maxcomment);
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, ind_maxparen))
{
++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()
{
int indent;
pos_T pos;
int save_State;
int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
OPT_LOCAL);
pos = curwin->w_cursor;
set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
if (use_sandbox)
++sandbox;
++textlock;
indent = eval_to_number(curbuf->b_p_inde);
if (use_sandbox)
--sandbox;
--textlock;
save_State = State;
State = INSERT;
curwin->w_cursor = pos;
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 __ARGS((char_u *p));
static int
lisp_match(p)
char_u *p;
{
char_u buf[LSIZE];
int len;
char_u *word = 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()
{
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 || ltp(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)
{
that++;
while (*that && (*that != '"' || *(that - 1) == '\\'))
++that;
}
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
{
amount = 0;
while (*that && col)
{
amount += lbr_chartabsize_adv(&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(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(&that,
(colnr_T)amount);
amount += lbr_chartabsize_adv(&that,
(colnr_T)amount);
}
}
while (vim_iswhite(*that))
{
amount += lbr_chartabsize(that, (colnr_T)amount);
that++;
}
if (!*that || *that == ';')
amount = firsttry;
}
}
}
}
}
else
amount = 0;
curwin->w_cursor = realpos;
return amount;
}
#endif
void
prepare_to_exit()
{
#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);
#ifdef WIN3264
if (can_end_termcap_mode(FALSE) == TRUE)
#endif
stoptermcap();
out_flush();
}
}
void
preserve_exit()
{
buf_T *buf;
prepare_to_exit();
really_exiting = TRUE;
out_str(IObuff);
screen_start();
out_flush();
ml_close_notmod();
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
{
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(fname)
char_u *fname;
{
struct stat 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()
{
if (++breakcheck_count >= BREAKCHECK_SKIP)
{
breakcheck_count = 0;
ui_breakcheck();
}
}
void
fast_breakcheck()
{
if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
{
breakcheck_count = 0;
ui_breakcheck();
}
}
int
expand_wildcards(num_pat, pat, num_file, file, flags)
int num_pat;
char_u **pat;
int *num_file;
char_u ***file;
int flags;
{
int retval;
int i, j;
char_u *p;
int non_suf_match;
retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
if (flags & EW_KEEPALL)
return retval;
#ifdef FEAT_WILDIGN
if (*p_wig)
{
char_u *ffname;
for (i = 0; i < *num_file; ++i)
{
ffname = FullName_save((*file)[i], FALSE);
if (ffname == NULL)
break;
# ifdef VMS
vms_remove_version(ffname);
# endif
if (match_file_list(p_wig, (*file)[i], ffname))
{
vim_free((*file)[i]);
for (j = i; j + 1 < *num_file; ++j)
(*file)[j] = (*file)[j + 1];
--*num_file;
--i;
}
vim_free(ffname);
}
}
#endif
if (*num_file > 1)
{
non_suf_match = 0;
for (i = 0; i < *num_file; ++i)
{
if (!match_suffix((*file)[i]))
{
p = (*file)[i];
for (j = i; j > non_suf_match; --j)
(*file)[j] = (*file)[j - 1];
(*file)[non_suf_match++] = p;
}
}
}
return retval;
}
int
match_suffix(fname)
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 (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 __ARGS((char_u *p));
static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
# endif
# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
# if defined(DJGPP) || 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));
}
# ifndef WIN3264
static void
namelowcpy(
char_u *d,
char_u *s)
{
# ifdef DJGPP
if (USE_LONG_FNAME)
while (*s)
*d++ = *s++;
else
# endif
while (*s)
*d++ = TOLOWER_LOC(*s++);
*d = NUL;
}
# endif
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;
#ifdef WIN3264
WIN32_FIND_DATA fb;
HANDLE hFind = (HANDLE)0;
# ifdef FEAT_MBYTE
WIN32_FIND_DATAW wfb;
WCHAR *wn = NULL;
# endif
#else
struct ffblk fb;
#endif
char_u *matchname;
int ok;
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 == '\\' || *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))
{
STRCPY(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;
}
regmatch.rm_ic = TRUE;
regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
vim_free(pat);
if (regmatch.regprog == NULL)
{
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 WIN3264
# ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
{
wn = enc_to_ucs2(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(buf, &fb);
ok = (hFind != INVALID_HANDLE_VALUE);
#else
ok = (findfirst((char *)buf, &fb,
(*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
#endif
while (ok)
{
#ifdef WIN3264
# ifdef FEAT_MBYTE
if (wn != NULL)
p = ucs2_to_enc(wfb.cFileName, NULL);
else
# endif
p = (char_u *)fb.cFileName;
#else
p = (char_u *)fb.ff_name;
#endif
if ((p[0] != '.' || starts_with_dot)
&& (matchname == NULL
|| vim_regexec(®match, p, (colnr_T)0)))
{
#ifdef WIN3264
STRCPY(s, p);
#else
namelowcpy(s, p);
#endif
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 WIN3264
# ifdef FEAT_MBYTE
if (wn != NULL)
{
vim_free(p);
ok = FindNextFileW(hFind, &wfb);
}
else
# endif
ok = FindNextFile(hFind, &fb);
#else
ok = (findnext(&fb) == 0);
#endif
if (!ok && matchname != NULL && gap->ga_len == start_len)
{
STRCPY(s, matchname);
#ifdef WIN3264
FindClose(hFind);
# ifdef FEAT_MBYTE
if (wn != NULL)
{
vim_free(wn);
wn = enc_to_ucs2(buf, NULL);
if (wn != NULL)
hFind = FindFirstFileW(wn, &wfb);
}
if (wn == NULL)
# endif
hFind = FindFirstFile(buf, &fb);
ok = (hFind != INVALID_HANDLE_VALUE);
#else
ok = (findfirst((char *)buf, &fb,
(*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
#endif
vim_free(matchname);
matchname = NULL;
}
}
#ifdef WIN3264
FindClose(hFind);
# ifdef FEAT_MBYTE
vim_free(wn);
# endif
#endif
vim_free(buf);
vim_free(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 __ARGS((const void *, const void *));
static int
pstrcmp(a, b)
const void *a, *b;
{
return (pathcmp(*(char **)a, *(char **)b, -1));
}
int
unix_expandpath(gap, path, wildoff, flags, didstar)
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)
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))
{
STRCPY(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;
}
#ifdef CASE_INSENSITIVE_FILENAME
regmatch.rm_ic = TRUE;
#else
regmatch.rm_ic = FALSE;
#endif
regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
vim_free(pat);
if (regmatch.regprog == NULL)
{
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)
&& vim_regexec(®match, (char_u *)dp->d_name, (colnr_T)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
{
if (*path_end != NUL)
backslash_halve(buf + len + 1);
if (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_free(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
int
gen_expand_wildcards(num_pat, pat, num_file, file, flags)
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;
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 (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL
# 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);
else
#endif
{
if (vim_strpbrk(p, (char_u *)"$~") != NULL)
{
p = expand_env_save(p);
if (p == NULL)
p = pat[i];
#ifdef UNIX
else if (vim_strpbrk(p, (char_u *)"$~") != NULL)
{
vim_free(p);
ga_clear(&ga);
i = mch_expand_wildcards(num_pat, pat, num_file, file,
flags);
recursive = FALSE;
return i;
}
#endif
}
if (mch_has_exp_wildcard(p))
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 defined(MACOS_CLASSIC)
slash_to_colon(t);
#endif
if (flags & EW_NOTFOUND)
addfile(&ga, t, flags | EW_DIR | EW_FILE);
else if (mch_getperm(t) >= 0)
addfile(&ga, t, flags);
vim_free(t);
}
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 (ga.ga_data != NULL) ? OK : FAIL;
}
# ifdef VIM_BACKTICK
static int
vim_backtick(p)
char_u *p;
{
return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
}
static int
expand_backtick(gap, pat, flags)
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 0;
#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);
vim_free(cmd);
if (buffer == NULL)
return 0;
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(gap, f, flags)
garray_T *gap;
char_u *f;
int flags;
{
char_u *p;
int isdir;
if (!(flags & EW_NOTFOUND) && 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))
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(cmd, infile, flags)
char_u *cmd;
char_u *infile;
int flags;
{
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')) == 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
buffer[len] = '\0';
done:
vim_free(tempname);
return buffer;
}
#endif
void
FreeWild(count, files)
int count;
char_u **files;
{
if (count <= 0 || files == NULL)
return;
#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER)
_fnexplodefree((char **)files);
#else
while (count--)
vim_free(files[count]);
vim_free(files);
#endif
}
int
goto_im()
{
return (p_im && stuff_empty() && typebuf_typed());
}