2

我现在正在使用 vim 编写 LaTeX 文档,但在使用“gq”命令格式化段落时遇到了问题。例如,如果我有这样的段落:

some
text% this is a comment
some
text

“gqap”的结果是:

some text% this is a comment some text

我希望它是:

some text% this is a comment
some text

但是,如果评论是独立的,“gq”可以正常工作:

some
text
% this is a comment
some
text

得到:

some text
% this is a comment
some text

我只是不知道这是否是vim的错误,也不知道如何修复它......任何帮助

更新:

今天我为 'formatexpr' 写了一个 vim 函数来防止 vim 断行以 "%%" 结尾:

function FormatTeX()
    let lnum = v:lnum                             " I found that v:lnum and v:count may change before exiting this function, so I made a copy here
    let lcount = v:count
    let lb = lnum + lcount - 1
    let le = lb
    while lb >= lnum                              " process the file in inverse order, or we have to deal with line number changes
        if match(getline(lb), '%%$') >= 0
            if lb < le
                exec "normal! ".(lb + 1)."GzR"    " the zR here opens all fold, or the result may be wrong 
                exec "normal! gw".le."G"
            endif
            let le = lb - 1
        elseif lb == lnum
            if lcount > 1
                exec "normal! ".lb."GzR"
                exec "normal! gw".le."G"
            else
                return 1                          " when 'formatoptions' has an 'a' flag, this branch is necessary or the cursor will jump unpredictable...
                                                  " according to the source code of vim, if the return value of 'formatexpr' is non-zero, the build-in formatter is used.
            endif
        endif
        let lb = lb - 1
    endwhile
    return 0
endfunction

我希望这个糟糕的例子可以帮助其他面临类似问题的人。

4

2 回答 2

2

有一个提示:help format-comments

Vim 通过行首的特定字符串识别注释 (忽略空格)。

尽管在使用 格式化时似乎对三段式注释gq进行了一些特殊处理,但未在行首开始的注释处理得不好。您必须将gq格式的范围限制在注释周围的文本中。

于 2013-04-06T12:36:29.503 回答
0

今天我为 'formatexpr' 写了一个 vim 函数来防止 vim 断行以 "%%" 结尾:

function FormatTeX()
    let lnum = v:lnum                             " I found that v:lnum and v:count may change before exiting this function, so I made a copy here
    let lcount = v:count
    let lb = lnum + lcount - 1
    let le = lb
    while lb >= lnum                              " process the file in inverse order, or we have to deal with line number changes
        if match(getline(lb), '%%$') >= 0
            if lb < le
                exec "normal! ".(lb + 1)."GzR"    " the zR here opens all fold, or the result may be wrong 
                exec "normal! gw".le."G"
            endif
            let le = lb - 1
        elseif lb == lnum
            if lcount > 1
                exec "normal! ".lb."GzR"
                exec "normal! gw".le."G"
            else
                return 1                          " when 'formatoptions' has an 'a' flag, this branch is necessary or the cursor will jump unpredictable...
                                                  " according to the source code of vim, if the return value of 'formatexpr' is non-zero, the build-in formatter is used.
            endif
        endif
        let lb = lb - 1
    endwhile
    return 0
endfunction

我希望这个糟糕的例子可以帮助其他面临类似问题的人。

于 2013-04-07T07:38:19.347 回答