Vim'中使用了什么算法;s`gq`段落填充?

Vim'中使用了什么算法;s`gq`段落填充?,vim,Vim,Vim使用什么算法将段落分成行以填充段落(gq)? 它是一个简单的贪婪算法,就像Emacs中使用的算法一样,还是像TeX中使用的更复杂的算法? 最重要的是,我应该在哪里查看源代码以确认所使用的算法?第一步是:help gq和:help formatoptions。在那之后,我只需下载整个源代码,然后将其彻底删除。如果所有这些都失败了,我会搜索vim dev的档案,并可能在那里询问更多细节。与许多vim产品一样,它是可定制的。默认情况下是贪婪算法(“在空格后会断开一条长于[code>textwid

Vim使用什么算法将段落分成行以填充段落(
gq
)? 它是一个简单的贪婪算法,就像Emacs中使用的算法一样,还是像TeX中使用的更复杂的算法?
最重要的是,我应该在哪里查看源代码以确认所使用的算法?

第一步是
:help gq
:help formatoptions
。在那之后,我只需下载整个源代码,然后将其彻底删除。如果所有这些都失败了,我会搜索vim dev的档案,并可能在那里询问更多细节。

与许多vim产品一样,它是可定制的。默认情况下是贪婪算法(“在空格后会断开一条长于[code>textwidth]的行以获得此宽度”),但通过设置
formatexpr
,您可以使用vimscript格式化段落,并通过设置
formatprg
(例如,设置为
fmt
par
)您可以将控制权交给外部程序。

您可以获得vim的源代码。下载它,格式化功能位于
vim/src/ops.c行:4710
。您可能还想看看

格式化行(行计数,避免)
行数(行数);;
int避免_fex;/*不要使用“formatexpr”*/
{
int max_len;
int不是par;/*当前行不是parag的一部分*/
int next_是_not_par;/*下一行不是段落的一部分*/
int是段落末尾的_end _par;/**/
int prev_是_end_par=FALSE;/*上一行不是段落的一部分*/
int next_为_start_par=FALSE;
#ifdef壮举注释
int leader_len=0;/*当前行的leader len*/
int next_leader_len;/*下一行的leader len*/
char_*leader_flags=NULL;/*当前行的前导标志*/
char_*next_leader_flags;/*下一行的leader标志*/
int do_注释;/*格式注释*/
int do_comments_list=0;/*将注释格式化为“n”或“2”*/
#恩迪夫
int advance=真;
int second_indent=-1;/*第二行的缩进(注释
*意识到)*/
int do_第二次缩进;
int do_编号\u缩进;
int do_trail_white;
int first_par_line=TRUE;
int smd_save;
长计数;
int need_set_indent=TRUE;/*设置下一段的缩进*/
int-force_格式=FALSE;
int old_State=状态;
/*强制格式化的行长度:3*“tw”*/
最大长度=复合文本宽度(真)*3;
/*检查“formatoptions”中的“q”、“2”和“1”*/
#ifdef壮举注释
do_comments=具有格式选项(FO_Q_COMS);
#恩迪夫
do_second_indent=具有格式选项(FO_Q_second);
do_number_indent=具有格式选项(FO_Q_number);
do_trail_white=具有格式选项(FO_white_PAR);
/*
*获取有关上一行和当前行的信息。
*/
如果(curwin->w_cursor.lnum>1)
不是吗?参数=fmt\u check\u par(curwin->w\u cursor.lnum-1
#ifdef壮举注释
,&leader\u len,&leader\u标志,do\u注释
#恩迪夫
);
其他的
是不是;
下一步是\u not \u par=fmt\u check\u par(curwin->w\u cursor.lnum
#ifdef壮举注释
,&下一个领导人,下一个领导人,做评论
#恩迪夫
);
is_end_par=(is_not_par | next_not_par);
如果(!是否为尾翼和尾翼为白色)
is_end_par=!end_为白色(curwin->w_cursor.lnum-1);
curwin->w_cursor.lnum--;
for(count=line\u count;count!=0&&!got\u int;--count)
{
/*
*进入下一段。
*/
如果(预付款)
{
curwin->w_cursor.lnum++;
prev_is_end_par=is_end_par;
is_not_par=下一个_not_par;
#ifdef壮举注释
leader\u len=下一个\u leader\u len;
领导标志=下一个领导标志;
#恩迪夫
}
/*
*要格式化的最后一行。
*/
if(count==1 | | curwin->w_cursor.lnum==curbuf->b|ml.ml_line_count)
{
下一步是不正确的;
#ifdef壮举注释
下一个\u leader\u len=0;
next_leader_flags=NULL;
#恩迪夫
}
其他的
{
下一步是\u not \u par=fmt\u check\u par(curwin->w\u cursor.lnum+1
#ifdef壮举注释
,&下一个领导人,下一个领导人,做评论
#恩迪夫
);
如果(不缩进编号)
下一步是开始=
(获取数字缩进(curwin->w\u cursor.lnum+1)>0);
}
前进=正确;
is_end_par=(is_not_par | next_not_par | next_not|u par | next_is_start_par);
如果(!是否为尾翼和尾翼为白色)
is_end_par=!end_为白色(curwin->w_cursor.lnum);
/*
*跳过不在段落中的行。
*/
如果(不是标准)
{
如果(行计数<0)
打破
}
其他的
{
/*
*对于段落的第一行,检查第二行的缩进。
*不要对注释和空行执行此操作。
*/
如果(第一条基准线)
&&(第二次缩进|第二次缩进|第二次缩进)
&&上一页是最后一页
&&curwin->w_cursor.lnumb_ml.ml_line_count)
{
if(第二次缩进和第二行为空(curwin->w\u cursor.lnum+1))
{
#ifdef壮举注释
if(leader\u len==0和next\u leader\u len==0)
{
/*未找到任何评论*/
#恩迪夫
第二缩进=
获取缩进值(curwin->w\u cursor.lnum+1);
#ifdef壮举注释
}
其他的
{
第二个缩进=下一个领导者;
do_comments_list=1;
}
#恩迪夫
}
else if(是否进行编号缩进)
{
#ifdef壮举注释
if(leader\u len==0和next\u leader\u len==0)
{
/*未找到任何评论*/
#恩迪夫
第二缩进=
获取数字缩进(curwin->w\u cursor.lnum);
#ifdef壮举注释
}
format_lines(line_count, avoid_fex)
    linenr_T    line_count;
    int     avoid_fex;      /* don't use 'formatexpr' */
{
    int     max_len;
    int     is_not_par;     /* current line not part of parag. */
    int     next_is_not_par;    /* next line not part of paragraph */
    int     is_end_par;     /* at end of paragraph */
    int     prev_is_end_par = FALSE;/* prev. line not part of parag. */
    int     next_is_start_par = FALSE;
#ifdef FEAT_COMMENTS
    int     leader_len = 0;     /* leader len of current line */
    int     next_leader_len;    /* leader len of next line */
    char_u  *leader_flags = NULL;   /* flags for leader of current line */
    char_u  *next_leader_flags; /* flags for leader of next line */
    int     do_comments;        /* format comments */
    int     do_comments_list = 0;   /* format comments with 'n' or '2' */
#endif
    int     advance = TRUE;
    int     second_indent = -1; /* indent for second line (comment
                     * aware) */
    int     do_second_indent;
    int     do_number_indent;
    int     do_trail_white;
    int     first_par_line = TRUE;
    int     smd_save;
    long    count;
    int     need_set_indent = TRUE; /* set indent of next paragraph */
    int     force_format = FALSE;
    int     old_State = State;

    /* length of a line to force formatting: 3 * 'tw' */
    max_len = comp_textwidth(TRUE) * 3;

    /* check for 'q', '2' and '1' in 'formatoptions' */
#ifdef FEAT_COMMENTS
    do_comments = has_format_option(FO_Q_COMS);
#endif
    do_second_indent = has_format_option(FO_Q_SECOND);
    do_number_indent = has_format_option(FO_Q_NUMBER);
    do_trail_white = has_format_option(FO_WHITE_PAR);

    /*
     * Get info about the previous and current line.
     */
    if (curwin->w_cursor.lnum > 1)
    is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
#ifdef FEAT_COMMENTS
                , &leader_len, &leader_flags, do_comments
#endif
                );
    else
    is_not_par = TRUE;
    next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
#ifdef FEAT_COMMENTS
               , &next_leader_len, &next_leader_flags, do_comments
#endif
                );
    is_end_par = (is_not_par || next_is_not_par);
    if (!is_end_par && do_trail_white)
    is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);

    curwin->w_cursor.lnum--;
    for (count = line_count; count != 0 && !got_int; --count)
    {
    /*
     * Advance to next paragraph.
     */
    if (advance)
    {
        curwin->w_cursor.lnum++;
        prev_is_end_par = is_end_par;
        is_not_par = next_is_not_par;
#ifdef FEAT_COMMENTS
        leader_len = next_leader_len;
        leader_flags = next_leader_flags;
#endif
    }

    /*
     * The last line to be formatted.
     */
    if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
    {
        next_is_not_par = TRUE;
#ifdef FEAT_COMMENTS
        next_leader_len = 0;
        next_leader_flags = NULL;
#endif
    }
    else
    {
        next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
#ifdef FEAT_COMMENTS
               , &next_leader_len, &next_leader_flags, do_comments
#endif
                    );
        if (do_number_indent)
        next_is_start_par =
               (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
    }
    advance = TRUE;
    is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
    if (!is_end_par && do_trail_white)
        is_end_par = !ends_in_white(curwin->w_cursor.lnum);

    /*
     * Skip lines that are not in a paragraph.
     */
    if (is_not_par)
    {
        if (line_count < 0)
        break;
    }
    else
    {
        /*
         * For the first line of a paragraph, check indent of second line.
         * Don't do this for comments and empty lines.
         */
        if (first_par_line
            && (do_second_indent || do_number_indent)
            && prev_is_end_par
            && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
        {
        if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
        {
#ifdef FEAT_COMMENTS
            if (leader_len == 0 && next_leader_len == 0)
            {
            /* no comment found */
#endif
            second_indent =
                   get_indent_lnum(curwin->w_cursor.lnum + 1);
#ifdef FEAT_COMMENTS
            }
            else
            {
            second_indent = next_leader_len;
            do_comments_list = 1;
            }
#endif
        }
        else if (do_number_indent)
        {
#ifdef FEAT_COMMENTS
            if (leader_len == 0 && next_leader_len == 0)
            {
            /* no comment found */
#endif
            second_indent =
                     get_number_indent(curwin->w_cursor.lnum);
#ifdef FEAT_COMMENTS
            }
            else
            {
            /* get_number_indent() is now "comment aware"... */
            second_indent =
                     get_number_indent(curwin->w_cursor.lnum);
            do_comments_list = 1;
            }
#endif
        }
        }

        /*
         * When the comment leader changes, it's the end of the paragraph.
         */
        if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
#ifdef FEAT_COMMENTS
            || !same_leader(curwin->w_cursor.lnum,
                    leader_len, leader_flags,
                      next_leader_len, next_leader_flags)
#endif
            )
        is_end_par = TRUE;

        /*
         * If we have got to the end of a paragraph, or the line is
         * getting long, format it.
         */
        if (is_end_par || force_format)
        {
        if (need_set_indent)
            /* replace indent in first line with minimal number of
             * tabs and spaces, according to current options */
            (void)set_indent(get_indent(), SIN_CHANGED);

        /* put cursor on last non-space */
        State = NORMAL; /* don't go past end-of-line */
        coladvance((colnr_T)MAXCOL);
        while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
            dec_cursor();

        /* do the formatting, without 'showmode' */
        State = INSERT; /* for open_line() */
        smd_save = p_smd;
        p_smd = FALSE;
        insertchar(NUL, INSCHAR_FORMAT
#ifdef FEAT_COMMENTS
            + (do_comments ? INSCHAR_DO_COM : 0)
            + (do_comments && do_comments_list
                               ? INSCHAR_COM_LIST : 0)
#endif
            + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
        State = old_State;
        p_smd = smd_save;
        second_indent = -1;
        /* at end of par.: need to set indent of next par. */
        need_set_indent = is_end_par;
        if (is_end_par)
        {
            /* When called with a negative line count, break at the
             * end of the paragraph. */
            if (line_count < 0)
            break;
            first_par_line = TRUE;
        }
        force_format = FALSE;
        }

        /*
         * When still in same paragraph, join the lines together.  But
         * first delete the comment leader from the second line.
         */
        if (!is_end_par)
        {
        advance = FALSE;
        curwin->w_cursor.lnum++;
        curwin->w_cursor.col = 0;
        if (line_count < 0 && u_save_cursor() == FAIL)
            break;
#ifdef FEAT_COMMENTS
        (void)del_bytes((long)next_leader_len, FALSE, FALSE);
        if (next_leader_len > 0)
            mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
                              (long)-next_leader_len);
#endif
        curwin->w_cursor.lnum--;
        if (do_join(2, TRUE, FALSE, FALSE) == FAIL)
        {
            beep_flush();
            break;
        }
        first_par_line = FALSE;
        /* If the line is getting long, format it next time */
        if (STRLEN(ml_get_curline()) > (size_t)max_len)
            force_format = TRUE;
        else
            force_format = FALSE;
        }
    }
    line_breakcheck();
    }
}