0
0
mirror of https://github.com/vim/vim.git synced 2025-09-27 04:14:06 -04:00

patch 8.1.1960: fold code is spread out

Problem:    Fold code is spread out.
Solution:   Move fold functions to fold.c.
This commit is contained in:
Bram Moolenaar
2019-09-01 17:52:32 +02:00
parent a112f2d003
commit db022f3ffb
4 changed files with 185 additions and 177 deletions

View File

@@ -117,11 +117,6 @@ static void f_fmod(typval_T *argvars, typval_T *rettv);
#endif #endif
static void f_fnameescape(typval_T *argvars, typval_T *rettv); static void f_fnameescape(typval_T *argvars, typval_T *rettv);
static void f_fnamemodify(typval_T *argvars, typval_T *rettv); static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
static void f_foldclosed(typval_T *argvars, typval_T *rettv);
static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
static void f_foldlevel(typval_T *argvars, typval_T *rettv);
static void f_foldtext(typval_T *argvars, typval_T *rettv);
static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
static void f_foreground(typval_T *argvars, typval_T *rettv); static void f_foreground(typval_T *argvars, typval_T *rettv);
static void f_funcref(typval_T *argvars, typval_T *rettv); static void f_funcref(typval_T *argvars, typval_T *rettv);
static void f_function(typval_T *argvars, typval_T *rettv); static void f_function(typval_T *argvars, typval_T *rettv);
@@ -3641,173 +3636,6 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
vim_free(fbuf); vim_free(fbuf);
} }
/*
* "foldclosed()" function
*/
static void
foldclosed_both(
typval_T *argvars UNUSED,
typval_T *rettv,
int end UNUSED)
{
#ifdef FEAT_FOLDING
linenr_T lnum;
linenr_T first, last;
lnum = tv_get_lnum(argvars);
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
{
if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
{
if (end)
rettv->vval.v_number = (varnumber_T)last;
else
rettv->vval.v_number = (varnumber_T)first;
return;
}
}
#endif
rettv->vval.v_number = -1;
}
/*
* "foldclosed()" function
*/
static void
f_foldclosed(typval_T *argvars, typval_T *rettv)
{
foldclosed_both(argvars, rettv, FALSE);
}
/*
* "foldclosedend()" function
*/
static void
f_foldclosedend(typval_T *argvars, typval_T *rettv)
{
foldclosed_both(argvars, rettv, TRUE);
}
/*
* "foldlevel()" function
*/
static void
f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
{
#ifdef FEAT_FOLDING
linenr_T lnum;
lnum = tv_get_lnum(argvars);
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
rettv->vval.v_number = foldLevel(lnum);
#endif
}
/*
* "foldtext()" function
*/
static void
f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
{
#ifdef FEAT_FOLDING
linenr_T foldstart;
linenr_T foldend;
char_u *dashes;
linenr_T lnum;
char_u *s;
char_u *r;
int len;
char *txt;
long count;
#endif
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
#ifdef FEAT_FOLDING
foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
dashes = get_vim_var_str(VV_FOLDDASHES);
if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
&& dashes != NULL)
{
/* Find first non-empty line in the fold. */
for (lnum = foldstart; lnum < foldend; ++lnum)
if (!linewhite(lnum))
break;
/* Find interesting text in this line. */
s = skipwhite(ml_get(lnum));
/* skip C comment-start */
if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
{
s = skipwhite(s + 2);
if (*skipwhite(s) == NUL
&& lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
{
s = skipwhite(ml_get(lnum + 1));
if (*s == '*')
s = skipwhite(s + 1);
}
}
count = (long)(foldend - foldstart + 1);
txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
r = alloc(STRLEN(txt)
+ STRLEN(dashes) // for %s
+ 20 // for %3ld
+ STRLEN(s)); // concatenated
if (r != NULL)
{
sprintf((char *)r, txt, dashes, count);
len = (int)STRLEN(r);
STRCAT(r, s);
/* remove 'foldmarker' and 'commentstring' */
foldtext_cleanup(r + len);
rettv->vval.v_string = r;
}
}
#endif
}
/*
* "foldtextresult(lnum)" function
*/
static void
f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
{
#ifdef FEAT_FOLDING
linenr_T lnum;
char_u *text;
char_u buf[FOLD_TEXT_LEN];
foldinfo_T foldinfo;
int fold_count;
static int entered = FALSE;
#endif
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
#ifdef FEAT_FOLDING
if (entered)
return; /* reject recursive use */
entered = TRUE;
lnum = tv_get_lnum(argvars);
/* treat illegal types and illegal string values for {lnum} the same */
if (lnum < 0)
lnum = 0;
fold_count = foldedCount(curwin, lnum, &foldinfo);
if (fold_count > 0)
{
text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
&foldinfo, buf);
if (text == buf)
text = vim_strsave(text);
rettv->vval.v_string = text;
}
entered = FALSE;
#endif
}
/* /*
* "foreground()" function * "foreground()" function
*/ */

View File

@@ -244,10 +244,11 @@ hasFoldingWin(
} }
/* foldLevel() {{{2 */ /* foldLevel() {{{2 */
#ifdef FEAT_EVAL
/* /*
* Return fold level at line number "lnum" in the current window. * Return fold level at line number "lnum" in the current window.
*/ */
int static int
foldLevel(linenr_T lnum) foldLevel(linenr_T lnum)
{ {
/* While updating the folds lines between invalid_top and invalid_bot have /* While updating the folds lines between invalid_top and invalid_bot have
@@ -265,6 +266,7 @@ foldLevel(linenr_T lnum)
return foldLevelWin(curwin, lnum); return foldLevelWin(curwin, lnum);
} }
#endif
/* lineFolded() {{{2 */ /* lineFolded() {{{2 */
/* /*
@@ -1989,10 +1991,11 @@ get_foldtext(
} }
/* foldtext_cleanup() {{{2 */ /* foldtext_cleanup() {{{2 */
#ifdef FEAT_EVAL
/* /*
* Remove 'foldmarker' and 'commentstring' from "str" (in-place). * Remove 'foldmarker' and 'commentstring' from "str" (in-place).
*/ */
void static void
foldtext_cleanup(char_u *str) foldtext_cleanup(char_u *str)
{ {
char_u *cms_start; /* first part or the whole comment */ char_u *cms_start; /* first part or the whole comment */
@@ -2078,6 +2081,7 @@ foldtext_cleanup(char_u *str)
} }
} }
} }
#endif
/* Folding by indent, expr, marker and syntax. {{{1 */ /* Folding by indent, expr, marker and syntax. {{{1 */
/* Define "fline_T", passed to get fold level for a line. {{{2 */ /* Define "fline_T", passed to get fold level for a line. {{{2 */
@@ -3616,4 +3620,175 @@ put_fold_open_close(FILE *fd, fold_T *fp, linenr_T off)
#endif /* FEAT_SESSION */ #endif /* FEAT_SESSION */
/* }}}1 */ /* }}}1 */
#endif /* defined(FEAT_FOLDING) || defined(PROTO) */ #endif // defined(FEAT_FOLDING) || defined(PROTO)
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* "foldclosed()" and "foldclosedend()" functions
*/
static void
foldclosed_both(
typval_T *argvars UNUSED,
typval_T *rettv,
int end UNUSED)
{
# ifdef FEAT_FOLDING
linenr_T lnum;
linenr_T first, last;
lnum = tv_get_lnum(argvars);
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
{
if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
{
if (end)
rettv->vval.v_number = (varnumber_T)last;
else
rettv->vval.v_number = (varnumber_T)first;
return;
}
}
#endif
rettv->vval.v_number = -1;
}
/*
* "foldclosed()" function
*/
void
f_foldclosed(typval_T *argvars, typval_T *rettv)
{
foldclosed_both(argvars, rettv, FALSE);
}
/*
* "foldclosedend()" function
*/
void
f_foldclosedend(typval_T *argvars, typval_T *rettv)
{
foldclosed_both(argvars, rettv, TRUE);
}
/*
* "foldlevel()" function
*/
void
f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
{
# ifdef FEAT_FOLDING
linenr_T lnum;
lnum = tv_get_lnum(argvars);
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
rettv->vval.v_number = foldLevel(lnum);
# endif
}
/*
* "foldtext()" function
*/
void
f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
{
# ifdef FEAT_FOLDING
linenr_T foldstart;
linenr_T foldend;
char_u *dashes;
linenr_T lnum;
char_u *s;
char_u *r;
int len;
char *txt;
long count;
# endif
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
# ifdef FEAT_FOLDING
foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
dashes = get_vim_var_str(VV_FOLDDASHES);
if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
&& dashes != NULL)
{
// Find first non-empty line in the fold.
for (lnum = foldstart; lnum < foldend; ++lnum)
if (!linewhite(lnum))
break;
// Find interesting text in this line.
s = skipwhite(ml_get(lnum));
// skip C comment-start
if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
{
s = skipwhite(s + 2);
if (*skipwhite(s) == NUL
&& lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
{
s = skipwhite(ml_get(lnum + 1));
if (*s == '*')
s = skipwhite(s + 1);
}
}
count = (long)(foldend - foldstart + 1);
txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
r = alloc(STRLEN(txt)
+ STRLEN(dashes) // for %s
+ 20 // for %3ld
+ STRLEN(s)); // concatenated
if (r != NULL)
{
sprintf((char *)r, txt, dashes, count);
len = (int)STRLEN(r);
STRCAT(r, s);
// remove 'foldmarker' and 'commentstring'
foldtext_cleanup(r + len);
rettv->vval.v_string = r;
}
}
# endif
}
/*
* "foldtextresult(lnum)" function
*/
void
f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
{
# ifdef FEAT_FOLDING
linenr_T lnum;
char_u *text;
char_u buf[FOLD_TEXT_LEN];
foldinfo_T foldinfo;
int fold_count;
static int entered = FALSE;
# endif
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
# ifdef FEAT_FOLDING
if (entered)
return; // reject recursive use
entered = TRUE;
lnum = tv_get_lnum(argvars);
// treat illegal types and illegal string values for {lnum} the same
if (lnum < 0)
lnum = 0;
fold_count = foldedCount(curwin, lnum, &foldinfo);
if (fold_count > 0)
{
text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
&foldinfo, buf);
if (text == buf)
text = vim_strsave(text);
rettv->vval.v_string = text;
}
entered = FALSE;
# endif
}
#endif // FEAT_EVAL

View File

@@ -3,7 +3,6 @@ void copyFoldingState(win_T *wp_from, win_T *wp_to);
int hasAnyFolding(win_T *win); int hasAnyFolding(win_T *win);
int hasFolding(linenr_T lnum, linenr_T *firstp, linenr_T *lastp); int hasFolding(linenr_T lnum, linenr_T *firstp, linenr_T *lastp);
int hasFoldingWin(win_T *win, linenr_T lnum, linenr_T *firstp, linenr_T *lastp, int cache, foldinfo_T *infop); int hasFoldingWin(win_T *win, linenr_T lnum, linenr_T *firstp, linenr_T *lastp, int cache, foldinfo_T *infop);
int foldLevel(linenr_T lnum);
int lineFolded(win_T *win, linenr_T lnum); int lineFolded(win_T *win, linenr_T lnum);
long foldedCount(win_T *win, linenr_T lnum, foldinfo_T *infop); long foldedCount(win_T *win, linenr_T lnum, foldinfo_T *infop);
int foldmethodIsManual(win_T *wp); int foldmethodIsManual(win_T *wp);
@@ -36,7 +35,11 @@ void deleteFoldRecurse(garray_T *gap);
void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, long amount, long amount_after); void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, long amount, long amount_after);
int getDeepestNesting(void); int getDeepestNesting(void);
char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T *foldinfo, char_u *buf); char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T *foldinfo, char_u *buf);
void foldtext_cleanup(char_u *str);
void foldMoveRange(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest); void foldMoveRange(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest);
int put_folds(FILE *fd, win_T *wp); int put_folds(FILE *fd, win_T *wp);
void f_foldclosed(typval_T *argvars, typval_T *rettv);
void f_foldclosedend(typval_T *argvars, typval_T *rettv);
void f_foldlevel(typval_T *argvars, typval_T *rettv);
void f_foldtext(typval_T *argvars, typval_T *rettv);
void f_foldtextresult(typval_T *argvars, typval_T *rettv);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -761,6 +761,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1960,
/**/ /**/
1959, 1959,
/**/ /**/