Question

I'm writing a LaTeX doc using vim now, and I met a problem when using the 'gq' command to format paragraphs. For example, if I have a paragraph like this:

some
text% this is a comment
some
text

The result of 'gqap' is:

some text% this is a comment some text

And I hope it would be:

some text% this is a comment
some text

However, if the comment is standalone, 'gq' works fine:

some
text
% this is a comment
some
text

gets:

some text
% this is a comment
some text

I just don't know whether it is a bug of vim or not, and don't know how to fix it...Any help

Update:

Today I have written a vim function for 'formatexpr' to prevent vim break lines ending with "%%":

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

I hope this poor example could help other guys who is facing similar problems.

Was it helpful?

Solution

There's a hint at :help format-comments:

Vim recognizes a comment by a specific string at the start of the line (ignoring white space).

Though there seems to be some special handling for three-piece comments when formatting with gq, comments that do not start at the beginning of a line aren't handled well. You have to limit the scope of your gq formatting to the text around the comments.

OTHER TIPS

Today I have written a vim function for 'formatexpr' to prevent vim break lines ending with "%%":

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

I hope this poor example could help other guys who is facing similar problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top