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のバグかどうかわからないだけで、修正方法もわかりません...何か助けてください

アップデート:

今日、「%%」で終わる vim 改行を防ぐために、「formatexpr」の 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 は、行頭の特定の文字列によってコメントを認識します( 空白は無視されます)。

で整形する場合、 3 ピース コメントの特別な処理があるようですがgq、行頭以外のコメントは適切に処理されません。gqフォーマットの範囲をコメントの周りのテキストに制限する必要があります。

于 2013-04-06T12:36:29.503 に答える
0

今日、「%%」で終わる vim 改行を防ぐために、「formatexpr」の 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 に答える