【发布时间】:2013-04-06 09:53:58
【问题描述】:
我现在正在使用 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
我希望这个糟糕的例子可以帮助其他面临类似问题的人。
【问题讨论】:
-
顺便说一句,我想知道如何防止某些特定行的自动格式化:例如,我有一个代表数学表达式的长行,我不希望它被拆分。
标签: vim comments latex format tex