comparison src/fold.c @ 17928:34639bba19b0 v8.1.1960

patch 8.1.1960: fold code is spread out Commit: https://github.com/vim/vim/commit/db022f3ffb57c5ff4b97b4e93a994d11e4c10466 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Sep 1 17:52:32 2019 +0200 patch 8.1.1960: fold code is spread out Problem: Fold code is spread out. Solution: Move fold functions to fold.c.
author Bram Moolenaar <Bram@vim.org>
date Sun, 01 Sep 2019 18:00:03 +0200
parents ba63a184e6b6
children e0ec4cd7a865
comparison
equal deleted inserted replaced
17927:a7e84c39e8de 17928:34639bba19b0
242 } 242 }
243 return TRUE; 243 return TRUE;
244 } 244 }
245 245
246 /* foldLevel() {{{2 */ 246 /* foldLevel() {{{2 */
247 #ifdef FEAT_EVAL
247 /* 248 /*
248 * Return fold level at line number "lnum" in the current window. 249 * Return fold level at line number "lnum" in the current window.
249 */ 250 */
250 int 251 static int
251 foldLevel(linenr_T lnum) 252 foldLevel(linenr_T lnum)
252 { 253 {
253 /* While updating the folds lines between invalid_top and invalid_bot have 254 /* While updating the folds lines between invalid_top and invalid_bot have
254 * an undefined fold level. Otherwise update the folds first. */ 255 * an undefined fold level. Otherwise update the folds first. */
255 if (invalid_top == (linenr_T)0) 256 if (invalid_top == (linenr_T)0)
263 if (!hasAnyFolding(curwin)) 264 if (!hasAnyFolding(curwin))
264 return 0; 265 return 0;
265 266
266 return foldLevelWin(curwin, lnum); 267 return foldLevelWin(curwin, lnum);
267 } 268 }
269 #endif
268 270
269 /* lineFolded() {{{2 */ 271 /* lineFolded() {{{2 */
270 /* 272 /*
271 * Low level function to check if a line is folded. Doesn't use any caching. 273 * Low level function to check if a line is folded. Doesn't use any caching.
272 * Return TRUE if line is folded. 274 * Return TRUE if line is folded.
1987 } 1989 }
1988 return text; 1990 return text;
1989 } 1991 }
1990 1992
1991 /* foldtext_cleanup() {{{2 */ 1993 /* foldtext_cleanup() {{{2 */
1994 #ifdef FEAT_EVAL
1992 /* 1995 /*
1993 * Remove 'foldmarker' and 'commentstring' from "str" (in-place). 1996 * Remove 'foldmarker' and 'commentstring' from "str" (in-place).
1994 */ 1997 */
1995 void 1998 static void
1996 foldtext_cleanup(char_u *str) 1999 foldtext_cleanup(char_u *str)
1997 { 2000 {
1998 char_u *cms_start; /* first part or the whole comment */ 2001 char_u *cms_start; /* first part or the whole comment */
1999 int cms_slen = 0; /* length of cms_start */ 2002 int cms_slen = 0; /* length of cms_start */
2000 char_u *cms_end; /* last part of the comment or NULL */ 2003 char_u *cms_end; /* last part of the comment or NULL */
2076 { 2079 {
2077 MB_PTR_ADV(s); 2080 MB_PTR_ADV(s);
2078 } 2081 }
2079 } 2082 }
2080 } 2083 }
2084 #endif
2081 2085
2082 /* Folding by indent, expr, marker and syntax. {{{1 */ 2086 /* Folding by indent, expr, marker and syntax. {{{1 */
2083 /* Define "fline_T", passed to get fold level for a line. {{{2 */ 2087 /* Define "fline_T", passed to get fold level for a line. {{{2 */
2084 typedef struct 2088 typedef struct
2085 { 2089 {
3614 return OK; 3618 return OK;
3615 } 3619 }
3616 #endif /* FEAT_SESSION */ 3620 #endif /* FEAT_SESSION */
3617 3621
3618 /* }}}1 */ 3622 /* }}}1 */
3619 #endif /* defined(FEAT_FOLDING) || defined(PROTO) */ 3623 #endif // defined(FEAT_FOLDING) || defined(PROTO)
3624
3625 #if defined(FEAT_EVAL) || defined(PROTO)
3626
3627 /*
3628 * "foldclosed()" and "foldclosedend()" functions
3629 */
3630 static void
3631 foldclosed_both(
3632 typval_T *argvars UNUSED,
3633 typval_T *rettv,
3634 int end UNUSED)
3635 {
3636 # ifdef FEAT_FOLDING
3637 linenr_T lnum;
3638 linenr_T first, last;
3639
3640 lnum = tv_get_lnum(argvars);
3641 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3642 {
3643 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3644 {
3645 if (end)
3646 rettv->vval.v_number = (varnumber_T)last;
3647 else
3648 rettv->vval.v_number = (varnumber_T)first;
3649 return;
3650 }
3651 }
3652 #endif
3653 rettv->vval.v_number = -1;
3654 }
3655
3656 /*
3657 * "foldclosed()" function
3658 */
3659 void
3660 f_foldclosed(typval_T *argvars, typval_T *rettv)
3661 {
3662 foldclosed_both(argvars, rettv, FALSE);
3663 }
3664
3665 /*
3666 * "foldclosedend()" function
3667 */
3668 void
3669 f_foldclosedend(typval_T *argvars, typval_T *rettv)
3670 {
3671 foldclosed_both(argvars, rettv, TRUE);
3672 }
3673
3674 /*
3675 * "foldlevel()" function
3676 */
3677 void
3678 f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3679 {
3680 # ifdef FEAT_FOLDING
3681 linenr_T lnum;
3682
3683 lnum = tv_get_lnum(argvars);
3684 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3685 rettv->vval.v_number = foldLevel(lnum);
3686 # endif
3687 }
3688
3689 /*
3690 * "foldtext()" function
3691 */
3692 void
3693 f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3694 {
3695 # ifdef FEAT_FOLDING
3696 linenr_T foldstart;
3697 linenr_T foldend;
3698 char_u *dashes;
3699 linenr_T lnum;
3700 char_u *s;
3701 char_u *r;
3702 int len;
3703 char *txt;
3704 long count;
3705 # endif
3706
3707 rettv->v_type = VAR_STRING;
3708 rettv->vval.v_string = NULL;
3709 # ifdef FEAT_FOLDING
3710 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3711 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3712 dashes = get_vim_var_str(VV_FOLDDASHES);
3713 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3714 && dashes != NULL)
3715 {
3716 // Find first non-empty line in the fold.
3717 for (lnum = foldstart; lnum < foldend; ++lnum)
3718 if (!linewhite(lnum))
3719 break;
3720
3721 // Find interesting text in this line.
3722 s = skipwhite(ml_get(lnum));
3723 // skip C comment-start
3724 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3725 {
3726 s = skipwhite(s + 2);
3727 if (*skipwhite(s) == NUL
3728 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3729 {
3730 s = skipwhite(ml_get(lnum + 1));
3731 if (*s == '*')
3732 s = skipwhite(s + 1);
3733 }
3734 }
3735 count = (long)(foldend - foldstart + 1);
3736 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
3737 r = alloc(STRLEN(txt)
3738 + STRLEN(dashes) // for %s
3739 + 20 // for %3ld
3740 + STRLEN(s)); // concatenated
3741 if (r != NULL)
3742 {
3743 sprintf((char *)r, txt, dashes, count);
3744 len = (int)STRLEN(r);
3745 STRCAT(r, s);
3746 // remove 'foldmarker' and 'commentstring'
3747 foldtext_cleanup(r + len);
3748 rettv->vval.v_string = r;
3749 }
3750 }
3751 # endif
3752 }
3753
3754 /*
3755 * "foldtextresult(lnum)" function
3756 */
3757 void
3758 f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3759 {
3760 # ifdef FEAT_FOLDING
3761 linenr_T lnum;
3762 char_u *text;
3763 char_u buf[FOLD_TEXT_LEN];
3764 foldinfo_T foldinfo;
3765 int fold_count;
3766 static int entered = FALSE;
3767 # endif
3768
3769 rettv->v_type = VAR_STRING;
3770 rettv->vval.v_string = NULL;
3771 # ifdef FEAT_FOLDING
3772 if (entered)
3773 return; // reject recursive use
3774 entered = TRUE;
3775
3776 lnum = tv_get_lnum(argvars);
3777 // treat illegal types and illegal string values for {lnum} the same
3778 if (lnum < 0)
3779 lnum = 0;
3780 fold_count = foldedCount(curwin, lnum, &foldinfo);
3781 if (fold_count > 0)
3782 {
3783 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3784 &foldinfo, buf);
3785 if (text == buf)
3786 text = vim_strsave(text);
3787 rettv->vval.v_string = text;
3788 }
3789
3790 entered = FALSE;
3791 # endif
3792 }
3793
3794 #endif // FEAT_EVAL