我现在正在使用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的bug,并且不知道如何解决它...任何帮助
今天我为'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
我希望这个糟糕的例子可以帮助其他面临类似问题的人。
答案 0 :(得分:2)
有:help format-comments
的提示:
Vim会在行首识别特定字符串的评论 (忽略空格)。
虽然在使用gq
格式化时似乎对三件式评论有一些特殊处理,但是不能在一行开头处开始的评论处理得不好。您必须将gq
格式的范围限制在评论周围的文本中。
答案 1 :(得分: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
我希望这个糟糕的例子可以帮助其他面临类似问题的人。