#include "vim.h"
static int cls(void);
static int skip_chars(int, int);
int
findsent(int dir, long count)
{
pos_T pos, tpos;
int c;
int (*func)(pos_T *);
int startlnum;
int noskip = FALSE; int cpo_J;
int found_dot;
pos = curwin->w_cursor;
if (dir == FORWARD)
func = incl;
else
func = decl;
while (count--)
{
if (gchar_pos(&pos) == NUL)
{
do
if ((*func)(&pos) == -1)
break;
while (gchar_pos(&pos) == NUL);
if (dir == FORWARD)
goto found;
}
else if (dir == FORWARD && pos.col == 0 &&
startPS(pos.lnum, NUL, FALSE))
{
if (pos.lnum == curbuf->b_ml.ml_line_count)
return FAIL;
++pos.lnum;
goto found;
}
else if (dir == BACKWARD)
decl(&pos);
found_dot = FALSE;
while (c = gchar_pos(&pos), VIM_ISWHITE(c)
|| vim_strchr((char_u *)".!?)]\"'", c) != NULL)
{
tpos = pos;
if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
break;
if (found_dot)
break;
if (vim_strchr((char_u *) ".!?", c) != NULL)
found_dot = TRUE;
if (vim_strchr((char_u *) ")]\"'", c) != NULL
&& vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
break;
decl(&pos);
}
startlnum = pos.lnum;
cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
for (;;) {
c = gchar_pos(&pos);
if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
{
if (dir == BACKWARD && pos.lnum != startlnum)
++pos.lnum;
break;
}
if (c == '.' || c == '!' || c == '?')
{
tpos = pos;
do
if ((c = inc(&tpos)) == -1)
break;
while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
!= NULL);
if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
|| (cpo_J && (c == ' ' && inc(&tpos) >= 0
&& gchar_pos(&tpos) == ' ')))
{
pos = tpos;
if (gchar_pos(&pos) == NUL) inc(&pos);
break;
}
}
if ((*func)(&pos) == -1)
{
if (count)
return FAIL;
noskip = TRUE;
break;
}
}
found:
while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
if (incl(&pos) == -1)
break;
}
setpcmark();
curwin->w_cursor = pos;
return OK;
}
int
findpar(
int *pincl, int dir,
long count,
int what,
int both)
{
linenr_T curr;
int did_skip; int first; int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
#ifdef FEAT_FOLDING
linenr_T fold_first; linenr_T fold_last; int fold_skipped; #endif
curr = curwin->w_cursor.lnum;
while (count--)
{
did_skip = FALSE;
for (first = TRUE; ; first = FALSE)
{
if (*ml_get(curr) != NUL)
did_skip = TRUE;
#ifdef FEAT_FOLDING
fold_skipped = FALSE;
if (first && hasFolding(curr, &fold_first, &fold_last))
{
curr = ((dir > 0) ? fold_last : fold_first) + dir;
fold_skipped = TRUE;
}
#endif
if (!first && did_skip && (startPS(curr, what, both)
|| (posix && what == NUL && *ml_get(curr) == '{')))
break;
#ifdef FEAT_FOLDING
if (fold_skipped)
curr -= dir;
#endif
if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
{
if (count)
return FALSE;
curr -= dir;
break;
}
}
}
setpcmark();
if (both && *ml_get(curr) == '}') ++curr;
curwin->w_cursor.lnum = curr;
if (curr == curbuf->b_ml.ml_line_count && what != '}')
{
char_u *line = ml_get(curr);
if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
{
--curwin->w_cursor.col;
curwin->w_cursor.col -=
(*mb_head_off)(line, line + curwin->w_cursor.col);
*pincl = TRUE;
}
}
else
curwin->w_cursor.col = 0;
return TRUE;
}
static int
inmacro(char_u *opt, char_u *s)
{
char_u *macro;
for (macro = opt; macro[0]; ++macro)
{
if ( (macro[0] == s[0]
|| (macro[0] == ' '
&& (s[0] == NUL || s[0] == ' ')))
&& (macro[1] == s[1]
|| ((macro[1] == NUL || macro[1] == ' ')
&& (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
break;
++macro;
if (macro[0] == NUL)
break;
}
return (macro[0] != NUL);
}
int
startPS(linenr_T lnum, int para, int both)
{
char_u *s;
s = ml_get(lnum);
if (*s == para || *s == '\f' || (both && *s == '}'))
return TRUE;
if (*s == '.' && (inmacro(p_sections, s + 1) ||
(!para && inmacro(p_para, s + 1))))
return TRUE;
return FALSE;
}
static int cls_bigword;
static int
cls(void)
{
int c;
c = gchar_cursor();
if (c == ' ' || c == '\t' || c == NUL)
return 0;
if (enc_dbcs != 0 && c > 0xFF)
{
if (enc_dbcs == DBCS_KOR && cls_bigword)
return 1;
return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
}
if (enc_utf8)
{
c = utf_class(c);
if (c != 0 && cls_bigword)
return 1;
return c;
}
if (cls_bigword)
return 1;
if (vim_iswordc(c))
return 2;
return 1;
}
int
fwd_word(
long count,
int bigword, int eol)
{
int sclass; int i;
int last_line;
curwin->w_cursor.coladd = 0;
cls_bigword = bigword;
while (--count >= 0)
{
#ifdef FEAT_FOLDING
if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
coladvance((colnr_T)MAXCOL);
#endif
sclass = cls();
last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
i = inc_cursor();
if (i == -1 || (i >= 1 && last_line)) return FAIL;
if (i >= 1 && eol && count == 0) return OK;
if (sclass != 0)
while (cls() == sclass)
{
i = inc_cursor();
if (i == -1 || (i >= 1 && eol && count == 0))
return OK;
}
while (cls() == 0)
{
if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
break;
i = inc_cursor();
if (i == -1 || (i >= 1 && eol && count == 0))
return OK;
}
}
return OK;
}
int
bck_word(long count, int bigword, int stop)
{
int sclass;
curwin->w_cursor.coladd = 0;
cls_bigword = bigword;
while (--count >= 0)
{
#ifdef FEAT_FOLDING
if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
curwin->w_cursor.col = 0;
#endif
sclass = cls();
if (dec_cursor() == -1) return FAIL;
if (!stop || sclass == cls() || sclass == 0)
{
while (cls() == 0)
{
if (curwin->w_cursor.col == 0
&& LINEEMPTY(curwin->w_cursor.lnum))
goto finished;
if (dec_cursor() == -1) return OK;
}
if (skip_chars(cls(), BACKWARD))
return OK;
}
inc_cursor(); finished:
stop = FALSE;
}
return OK;
}
int
end_word(
long count,
int bigword,
int stop,
int empty)
{
int sclass;
curwin->w_cursor.coladd = 0;
cls_bigword = bigword;
while (--count >= 0)
{
#ifdef FEAT_FOLDING
if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
coladvance((colnr_T)MAXCOL);
#endif
sclass = cls();
if (inc_cursor() == -1)
return FAIL;
if (cls() == sclass && sclass != 0)
{
if (skip_chars(sclass, FORWARD))
return FAIL;
}
else if (!stop || sclass == 0)
{
while (cls() == 0)
{
if (empty && curwin->w_cursor.col == 0
&& LINEEMPTY(curwin->w_cursor.lnum))
goto finished;
if (inc_cursor() == -1) return FAIL;
}
if (skip_chars(cls(), FORWARD))
return FAIL;
}
dec_cursor(); finished:
stop = FALSE; }
return OK;
}
int
bckend_word(
long count,
int bigword, int eol) {
int sclass; int i;
curwin->w_cursor.coladd = 0;
cls_bigword = bigword;
while (--count >= 0)
{
sclass = cls();
if ((i = dec_cursor()) == -1)
return FAIL;
if (eol && i == 1)
return OK;
if (sclass != 0)
{
while (cls() == sclass)
if ((i = dec_cursor()) == -1 || (eol && i == 1))
return OK;
}
while (cls() == 0)
{
if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
break;
if ((i = dec_cursor()) == -1 || (eol && i == 1))
return OK;
}
}
return OK;
}
static int
skip_chars(int cclass, int dir)
{
while (cls() == cclass)
if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
return TRUE;
return FALSE;
}
#if defined(FEAT_TEXTOBJ) || defined(PROTO)
static void
back_in_line(void)
{
int sclass;
sclass = cls();
for (;;)
{
if (curwin->w_cursor.col == 0) break;
dec_cursor();
if (cls() != sclass) {
inc_cursor();
break;
}
}
}
static void
find_first_blank(pos_T *posp)
{
int c;
while (decl(posp) != -1)
{
c = gchar_pos(posp);
if (!VIM_ISWHITE(c))
{
incl(posp);
break;
}
}
}
static void
findsent_forward(
long count,
int at_start_sent) {
while (count--)
{
findsent(FORWARD, 1L);
if (at_start_sent)
find_first_blank(&curwin->w_cursor);
if (count == 0 || at_start_sent)
decl(&curwin->w_cursor);
at_start_sent = !at_start_sent;
}
}
int
current_word(
oparg_T *oap,
long count,
int include, int bigword) {
pos_T start_pos;
pos_T pos;
int inclusive = TRUE;
int include_white = FALSE;
cls_bigword = bigword;
CLEAR_POS(&start_pos);
if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
dec_cursor();
if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
{
back_in_line();
start_pos = curwin->w_cursor;
if ((cls() == 0) == include)
{
if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
return FAIL;
}
else
{
fwd_word(1L, bigword, TRUE);
if (curwin->w_cursor.col == 0)
decl(&curwin->w_cursor);
else
oneleft();
if (include)
include_white = TRUE;
}
if (VIsual_active)
{
VIsual = start_pos;
redraw_curbuf_later(INVERTED); }
else
{
oap->start = start_pos;
oap->motion_type = MCHAR;
}
--count;
}
while (count > 0)
{
inclusive = TRUE;
if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
{
if (decl(&curwin->w_cursor) == -1)
return FAIL;
if (include != (cls() != 0))
{
if (bck_word(1L, bigword, TRUE) == FAIL)
return FAIL;
}
else
{
if (bckend_word(1L, bigword, TRUE) == FAIL)
return FAIL;
(void)incl(&curwin->w_cursor);
}
}
else
{
if (incl(&curwin->w_cursor) == -1)
return FAIL;
if (include != (cls() == 0))
{
if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
return FAIL;
if (oneleft() == FAIL)
inclusive = FALSE;
}
else
{
if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
return FAIL;
}
}
--count;
}
if (include_white && (cls() != 0
|| (curwin->w_cursor.col == 0 && !inclusive)))
{
pos = curwin->w_cursor; curwin->w_cursor = start_pos;
if (oneleft() == OK)
{
back_in_line();
if (cls() == 0 && curwin->w_cursor.col > 0)
{
if (VIsual_active)
VIsual = curwin->w_cursor;
else
oap->start = curwin->w_cursor;
}
}
curwin->w_cursor = pos; }
if (VIsual_active)
{
if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
inc_cursor();
if (VIsual_mode == 'V')
{
VIsual_mode = 'v';
redraw_cmdline = TRUE; }
}
else
oap->inclusive = inclusive;
return OK;
}
int
current_sent(oparg_T *oap, long count, int include)
{
pos_T start_pos;
pos_T pos;
int start_blank;
int c;
int at_start_sent;
long ncount;
start_pos = curwin->w_cursor;
pos = start_pos;
findsent(FORWARD, 1L);
if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
{
extend:
if (LT_POS(start_pos, VIsual))
{
at_start_sent = TRUE;
decl(&pos);
while (LT_POS(pos, curwin->w_cursor))
{
c = gchar_pos(&pos);
if (!VIM_ISWHITE(c))
{
at_start_sent = FALSE;
break;
}
incl(&pos);
}
if (!at_start_sent)
{
findsent(BACKWARD, 1L);
if (EQUAL_POS(curwin->w_cursor, start_pos))
at_start_sent = TRUE; else
findsent(FORWARD, 1L);
}
if (include) count *= 2;
while (count--)
{
if (at_start_sent)
find_first_blank(&curwin->w_cursor);
c = gchar_cursor();
if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
findsent(BACKWARD, 1L);
at_start_sent = !at_start_sent;
}
}
else
{
incl(&pos);
at_start_sent = TRUE;
if (!EQUAL_POS(pos, curwin->w_cursor))
{
at_start_sent = FALSE;
while (LT_POS(pos, curwin->w_cursor))
{
c = gchar_pos(&pos);
if (!VIM_ISWHITE(c))
{
at_start_sent = TRUE;
break;
}
incl(&pos);
}
if (at_start_sent) findsent(BACKWARD, 1L);
else curwin->w_cursor = start_pos;
}
if (include) count *= 2;
findsent_forward(count, at_start_sent);
if (*p_sel == 'e')
++curwin->w_cursor.col;
}
return OK;
}
while (c = gchar_pos(&pos), VIM_ISWHITE(c)) incl(&pos);
if (EQUAL_POS(pos, curwin->w_cursor))
{
start_blank = TRUE;
find_first_blank(&start_pos); }
else
{
start_blank = FALSE;
findsent(BACKWARD, 1L);
start_pos = curwin->w_cursor;
}
if (include)
ncount = count * 2;
else
{
ncount = count;
if (start_blank)
--ncount;
}
if (ncount > 0)
findsent_forward(ncount, TRUE);
else
decl(&curwin->w_cursor);
if (include)
{
if (start_blank)
{
find_first_blank(&curwin->w_cursor);
c = gchar_pos(&curwin->w_cursor); if (VIM_ISWHITE(c))
decl(&curwin->w_cursor);
}
else if (c = gchar_cursor(), !VIM_ISWHITE(c))
find_first_blank(&start_pos);
}
if (VIsual_active)
{
if (EQUAL_POS(start_pos, curwin->w_cursor))
goto extend;
if (*p_sel == 'e')
++curwin->w_cursor.col;
VIsual = start_pos;
VIsual_mode = 'v';
redraw_cmdline = TRUE; redraw_curbuf_later(INVERTED); }
else
{
if (incl(&curwin->w_cursor) == -1)
oap->inclusive = TRUE;
else
oap->inclusive = FALSE;
oap->start = start_pos;
oap->motion_type = MCHAR;
}
return OK;
}
int
current_block(
oparg_T *oap,
long count,
int include, int what, int other) {
pos_T old_pos;
pos_T *pos = NULL;
pos_T start_pos;
pos_T *end_pos;
pos_T old_start, old_end;
char_u *save_cpo;
int sol = FALSE;
old_pos = curwin->w_cursor;
old_end = curwin->w_cursor; old_start = old_end;
if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
{
setpcmark();
if (what == '{') while (inindent(1))
if (inc_cursor() != 0)
break;
if (gchar_cursor() == what)
++curwin->w_cursor.col;
}
else if (LT_POS(VIsual, curwin->w_cursor))
{
old_start = VIsual;
curwin->w_cursor = VIsual; }
else
old_end = VIsual;
save_cpo = p_cpo;
p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
while (count-- > 0)
{
if ((pos = findmatch(NULL, what)) == NULL)
break;
curwin->w_cursor = *pos;
start_pos = *pos; }
p_cpo = save_cpo;
if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
{
curwin->w_cursor = old_pos;
return FAIL;
}
curwin->w_cursor = *end_pos;
while (!include)
{
incl(&start_pos);
sol = (curwin->w_cursor.col == 0);
decl(&curwin->w_cursor);
while (inindent(1))
{
sol = TRUE;
if (decl(&curwin->w_cursor) != 0)
break;
}
if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
&& VIsual_active)
{
curwin->w_cursor = old_start;
decl(&curwin->w_cursor);
if ((pos = findmatch(NULL, what)) == NULL)
{
curwin->w_cursor = old_pos;
return FAIL;
}
start_pos = *pos;
curwin->w_cursor = *pos;
if ((end_pos = findmatch(NULL, other)) == NULL)
{
curwin->w_cursor = old_pos;
return FAIL;
}
curwin->w_cursor = *end_pos;
}
else
break;
}
if (VIsual_active)
{
if (*p_sel == 'e')
inc(&curwin->w_cursor);
if (sol && gchar_cursor() != NUL)
inc(&curwin->w_cursor); VIsual = start_pos;
VIsual_mode = 'v';
redraw_curbuf_later(INVERTED); showmode();
}
else
{
oap->start = start_pos;
oap->motion_type = MCHAR;
oap->inclusive = FALSE;
if (sol)
incl(&curwin->w_cursor);
else if (LTOREQ_POS(start_pos, curwin->w_cursor))
oap->inclusive = TRUE;
else
curwin->w_cursor = start_pos;
}
return OK;
}
static int
in_html_tag(
int end_tag)
{
char_u *line = ml_get_curline();
char_u *p;
int c;
int lc = NUL;
pos_T pos;
if (enc_dbcs)
{
char_u *lp = NULL;
for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
if (*p == '>' || *p == '<')
{
lc = *p;
lp = p;
}
if (*p != '<') {
if (lc != '<')
return FALSE;
p = lp;
}
}
else
{
for (p = line + curwin->w_cursor.col; p > line; )
{
if (*p == '<') break;
MB_PTR_BACK(line, p);
if (*p == '>') break;
}
if (*p != '<')
return FALSE;
}
pos.lnum = curwin->w_cursor.lnum;
pos.col = (colnr_T)(p - line);
MB_PTR_ADV(p);
if (end_tag)
return *p == '/';
if (*p == '/')
return FALSE;
for (;;)
{
if (inc(&pos) < 0)
return FALSE;
c = *ml_get_pos(&pos);
if (c == '>')
break;
lc = c;
}
return lc != '/';
}
int
current_tagblock(
oparg_T *oap,
long count_arg,
int include) {
long count = count_arg;
long n;
pos_T old_pos;
pos_T start_pos;
pos_T end_pos;
pos_T old_start, old_end;
char_u *spat, *epat;
char_u *p;
char_u *cp;
int len;
int r;
int do_include = include;
int save_p_ws = p_ws;
int retval = FAIL;
int is_inclusive = TRUE;
p_ws = FALSE;
old_pos = curwin->w_cursor;
old_end = curwin->w_cursor; old_start = old_end;
if (!VIsual_active || *p_sel == 'e')
decl(&old_end);
if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
{
setpcmark();
while (inindent(1))
if (inc_cursor() != 0)
break;
if (in_html_tag(FALSE))
{
while (*ml_get_cursor() != '>')
if (inc_cursor() < 0)
break;
}
else if (in_html_tag(TRUE))
{
while (*ml_get_cursor() != '<')
if (dec_cursor() < 0)
break;
dec_cursor();
old_end = curwin->w_cursor;
}
}
else if (LT_POS(VIsual, curwin->w_cursor))
{
old_start = VIsual;
curwin->w_cursor = VIsual; }
else
old_end = VIsual;
again:
for (n = 0; n < count; ++n)
{
if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
(char_u *)"",
(char_u *)"</[^>]*>", BACKWARD, NULL, 0,
NULL, (linenr_T)0, 0L) <= 0)
{
curwin->w_cursor = old_pos;
goto theend;
}
}
start_pos = curwin->w_cursor;
inc_cursor();
p = ml_get_cursor();
for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
;
len = (int)(cp - p);
if (len == 0)
{
curwin->w_cursor = old_pos;
goto theend;
}
spat = alloc(len + 31);
epat = alloc(len + 9);
if (spat == NULL || epat == NULL)
{
vim_free(spat);
vim_free(epat);
curwin->w_cursor = old_pos;
goto theend;
}
sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
sprintf((char *)epat, "</%.*s>\\c", len, p);
r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
0, NULL, (linenr_T)0, 0L);
vim_free(spat);
vim_free(epat);
if (r < 1 || LT_POS(curwin->w_cursor, old_end))
{
count = 1;
curwin->w_cursor = start_pos;
goto again;
}
if (do_include)
{
while (*ml_get_cursor() != '>')
if (inc_cursor() < 0)
break;
}
else
{
char_u *c = ml_get_cursor();
if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
is_inclusive = FALSE;
else if (*c == '<')
dec_cursor();
}
end_pos = curwin->w_cursor;
if (!do_include)
{
curwin->w_cursor = start_pos;
while (inc_cursor() >= 0)
if (*ml_get_cursor() == '>')
{
inc_cursor();
start_pos = curwin->w_cursor;
break;
}
curwin->w_cursor = end_pos;
if (VIsual_active && EQUAL_POS(start_pos, old_start)
&& EQUAL_POS(end_pos, old_end))
{
do_include = TRUE;
curwin->w_cursor = old_start;
count = count_arg;
goto again;
}
}
if (VIsual_active)
{
if (LT_POS(end_pos, start_pos))
curwin->w_cursor = start_pos;
else if (*p_sel == 'e')
inc_cursor();
VIsual = start_pos;
VIsual_mode = 'v';
redraw_curbuf_later(INVERTED); showmode();
}
else
{
oap->start = start_pos;
oap->motion_type = MCHAR;
if (LT_POS(end_pos, start_pos))
{
curwin->w_cursor = start_pos;
oap->inclusive = FALSE;
}
else
oap->inclusive = is_inclusive;
}
retval = OK;
theend:
p_ws = save_p_ws;
return retval;
}
int
current_par(
oparg_T *oap,
long count,
int include, int type) {
linenr_T start_lnum;
linenr_T end_lnum;
int white_in_front;
int dir;
int start_is_white;
int prev_start_is_white;
int retval = OK;
int do_white = FALSE;
int t;
int i;
if (type == 'S') return FAIL;
start_lnum = curwin->w_cursor.lnum;
if (VIsual_active && start_lnum != VIsual.lnum)
{
extend:
if (start_lnum < VIsual.lnum)
dir = BACKWARD;
else
dir = FORWARD;
for (i = count; --i >= 0; )
{
if (start_lnum ==
(dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
{
retval = FAIL;
break;
}
prev_start_is_white = -1;
for (t = 0; t < 2; ++t)
{
start_lnum += dir;
start_is_white = linewhite(start_lnum);
if (prev_start_is_white == start_is_white)
{
start_lnum -= dir;
break;
}
for (;;)
{
if (start_lnum == (dir == BACKWARD
? 1 : curbuf->b_ml.ml_line_count))
break;
if (start_is_white != linewhite(start_lnum + dir)
|| (!start_is_white
&& startPS(start_lnum + (dir > 0
? 1 : 0), 0, 0)))
break;
start_lnum += dir;
}
if (!include)
break;
if (start_lnum == (dir == BACKWARD
? 1 : curbuf->b_ml.ml_line_count))
break;
prev_start_is_white = start_is_white;
}
}
curwin->w_cursor.lnum = start_lnum;
curwin->w_cursor.col = 0;
return retval;
}
white_in_front = linewhite(start_lnum);
while (start_lnum > 1)
{
if (white_in_front) {
if (!linewhite(start_lnum - 1))
break;
}
else {
if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
break;
}
--start_lnum;
}
end_lnum = start_lnum;
while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
++end_lnum;
--end_lnum;
i = count;
if (!include && white_in_front)
--i;
while (i--)
{
if (end_lnum == curbuf->b_ml.ml_line_count)
return FAIL;
if (!include)
do_white = linewhite(end_lnum + 1);
if (include || !do_white)
{
++end_lnum;
while (end_lnum < curbuf->b_ml.ml_line_count
&& !linewhite(end_lnum + 1)
&& !startPS(end_lnum + 1, 0, 0))
++end_lnum;
}
if (i == 0 && white_in_front && include)
break;
if (include || do_white)
while (end_lnum < curbuf->b_ml.ml_line_count
&& linewhite(end_lnum + 1))
++end_lnum;
}
if (!white_in_front && !linewhite(end_lnum) && include)
while (start_lnum > 1 && linewhite(start_lnum - 1))
--start_lnum;
if (VIsual_active)
{
if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
goto extend;
if (VIsual.lnum != start_lnum)
{
VIsual.lnum = start_lnum;
VIsual.col = 0;
}
VIsual_mode = 'V';
redraw_curbuf_later(INVERTED); showmode();
}
else
{
oap->start.lnum = start_lnum;
oap->start.col = 0;
oap->motion_type = MLINE;
}
curwin->w_cursor.lnum = end_lnum;
curwin->w_cursor.col = 0;
return OK;
}
static int
find_next_quote(
char_u *line,
int col,
int quotechar,
char_u *escape) {
int c;
for (;;)
{
c = line[col];
if (c == NUL)
return -1;
else if (escape != NULL && vim_strchr(escape, c))
++col;
else if (c == quotechar)
break;
if (has_mbyte)
col += (*mb_ptr2len)(line + col);
else
++col;
}
return col;
}
static int
find_prev_quote(
char_u *line,
int col_start,
int quotechar,
char_u *escape) {
int n;
while (col_start > 0)
{
--col_start;
col_start -= (*mb_head_off)(line, line + col_start);
n = 0;
if (escape != NULL)
while (col_start - n > 0 && vim_strchr(escape,
line[col_start - n - 1]) != NULL)
++n;
if (n & 1)
col_start -= n; else if (line[col_start] == quotechar)
break;
}
return col_start;
}
int
current_quote(
oparg_T *oap,
long count,
int include, int quotechar) {
char_u *line = ml_get_curline();
int col_end;
int col_start = curwin->w_cursor.col;
int inclusive = FALSE;
int vis_empty = TRUE; int vis_bef_curs = FALSE; int did_exclusive_adj = FALSE; int inside_quotes = FALSE; int selected_quote = FALSE; int i;
int restore_vis_bef = FALSE;
if (VIsual_active)
{
if (VIsual.lnum != curwin->w_cursor.lnum)
return FALSE;
vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
if (*p_sel == 'e')
{
if (vis_bef_curs)
{
dec_cursor();
did_exclusive_adj = TRUE;
}
else if (!vis_empty)
{
dec(&VIsual);
did_exclusive_adj = TRUE;
}
vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
if (!vis_bef_curs && !vis_empty)
{
pos_T t = curwin->w_cursor;
curwin->w_cursor = VIsual;
VIsual = t;
vis_bef_curs = TRUE;
restore_vis_bef = TRUE;
}
}
}
if (!vis_empty)
{
if (vis_bef_curs)
{
inside_quotes = VIsual.col > 0
&& line[VIsual.col - 1] == quotechar
&& line[curwin->w_cursor.col] != NUL
&& line[curwin->w_cursor.col + 1] == quotechar;
i = VIsual.col;
col_end = curwin->w_cursor.col;
}
else
{
inside_quotes = curwin->w_cursor.col > 0
&& line[curwin->w_cursor.col - 1] == quotechar
&& line[VIsual.col] != NUL
&& line[VIsual.col + 1] == quotechar;
i = curwin->w_cursor.col;
col_end = VIsual.col;
}
while (i <= col_end)
if (line[i++] == quotechar)
{
selected_quote = TRUE;
break;
}
}
if (!vis_empty && line[col_start] == quotechar)
{
if (vis_bef_curs)
{
col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
if (col_start < 0)
goto abort_search;
col_end = find_next_quote(line, col_start + 1, quotechar,
curbuf->b_p_qe);
if (col_end < 0)
{
col_end = col_start;
col_start = curwin->w_cursor.col;
}
}
else
{
col_end = find_prev_quote(line, col_start, quotechar, NULL);
if (line[col_end] != quotechar)
goto abort_search;
col_start = find_prev_quote(line, col_end, quotechar,
curbuf->b_p_qe);
if (line[col_start] != quotechar)
{
col_start = col_end;
col_end = curwin->w_cursor.col;
}
}
}
else
if (line[col_start] == quotechar || !vis_empty)
{
int first_col = col_start;
if (!vis_empty)
{
if (vis_bef_curs)
first_col = find_next_quote(line, col_start, quotechar, NULL);
else
first_col = find_prev_quote(line, col_start, quotechar, NULL);
}
col_start = 0;
for (;;)
{
col_start = find_next_quote(line, col_start, quotechar, NULL);
if (col_start < 0 || col_start > first_col)
goto abort_search;
col_end = find_next_quote(line, col_start + 1, quotechar,
curbuf->b_p_qe);
if (col_end < 0)
goto abort_search;
if (col_start <= first_col && first_col <= col_end)
break;
col_start = col_end + 1;
}
}
else
{
col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
if (line[col_start] != quotechar)
{
col_start = find_next_quote(line, col_start, quotechar, NULL);
if (col_start < 0)
goto abort_search;
}
col_end = find_next_quote(line, col_start + 1, quotechar,
curbuf->b_p_qe);
if (col_end < 0)
goto abort_search;
}
if (include)
{
if (VIM_ISWHITE(line[col_end + 1]))
while (VIM_ISWHITE(line[col_end + 1]))
++col_end;
else
while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
--col_start;
}
if (!include && count < 2 && (vis_empty || !inside_quotes))
++col_start;
curwin->w_cursor.col = col_start;
if (VIsual_active)
{
if (vis_empty
|| (vis_bef_curs
&& !selected_quote
&& (inside_quotes
|| (line[VIsual.col] != quotechar
&& (VIsual.col == 0
|| line[VIsual.col - 1] != quotechar)))))
{
VIsual = curwin->w_cursor;
redraw_curbuf_later(INVERTED);
}
}
else
{
oap->start = curwin->w_cursor;
oap->motion_type = MCHAR;
}
curwin->w_cursor.col = col_end;
if ((include || count > 1 || (!vis_empty && inside_quotes)
) && inc_cursor() == 2)
inclusive = TRUE;
if (VIsual_active)
{
if (vis_empty || vis_bef_curs)
{
if (*p_sel != 'e')
dec_cursor();
}
else
{
if (inside_quotes
|| (!selected_quote
&& line[VIsual.col] != quotechar
&& (line[VIsual.col] == NUL
|| line[VIsual.col + 1] != quotechar)))
{
dec_cursor();
VIsual = curwin->w_cursor;
}
curwin->w_cursor.col = col_start;
}
if (VIsual_mode == 'V')
{
VIsual_mode = 'v';
redraw_cmdline = TRUE; }
}
else
{
oap->inclusive = inclusive;
}
return OK;
abort_search:
if (VIsual_active && *p_sel == 'e')
{
if (did_exclusive_adj)
inc_cursor();
if (restore_vis_bef)
{
pos_T t = curwin->w_cursor;
curwin->w_cursor = VIsual;
VIsual = t;
}
}
return FALSE;
}
#endif // FEAT_TEXTOBJ