我正在将LaTeX编辑环境切换到Vim上。希望能够在Vim内部对源文件进行编译,并在成功编译后启动外部查看器。
我知道有Vim-Latex套件,但如果可能的话,我宁愿避免使用它:它非常复杂,会占用很多我的按键,并且会让vimruntime中出现大量文件。
以下是我目前的情况:
问题在于,即使有编译错误,也没有设置
在这里给出的答案以及其他方法的研究中,我认为已经得到了令人满意的解决。我在此发布解决方案,以防其他人有兴趣。
基本上,最好的解决方案似乎是使用LaTeX的包装器Rubber,它通常“只需工作”,并提供非常干净的输出/错误。如果在系统上找到Rubber并且当前目录中没有Makefile,则首选下面介绍的解决方案使用Rubber。如果找到Makefile,则使用该文件。如果没有Makefile且未安装Rubber,则使用pdflatex。在所有情况下,如果源代码无法编译,则将(过滤和解析后的)错误发送到QuickFix缓冲区,并自动打开QuickFix窗口。如果成功编译,将写入一条简短的消息,并且如果用户请求,则会打开PDF进行查看。
在我的自己的安装中,我从Vim-Latex中提取了(优秀的)“SetLatexEfm()”函数来解析和过滤tex构建输出。但是,如果找不到此函数,则以下函数默认设置错误消息格式,该格式足以识别并在QuickFix窗口中突出显示错误,尽管有很多垃圾。
更新:我已经将这个脚本打包发布为 Vim 文件类型插件,可以在http://www.vim.org/scripts/script.php?script_id=3230下载。
我知道有Vim-Latex套件,但如果可能的话,我宁愿避免使用它:它非常复杂,会占用很多我的按键,并且会让vimruntime中出现大量文件。
以下是我目前的情况:
if exists('b:tex_build_mapped')
finish
endif
" use maparg or mapcheck to see if key is free
command! -buffer -nargs=* BuildTex call BuildTex(0, <f-args>)
command! -buffer -nargs=* BuildAndViewTex call BuildTex(1, <f-args>)
noremap <buffer> <silent> <F9> <Esc>:call BuildTex(0)<CR>
noremap <buffer> <silent> <S-F9> <Esc>:call BuildTex(1)<CR>
let b:tex_build_mapped = 1
if exists('g:tex_build_loaded')
finish
endif
let g:tex_build_loaded = 1
function! BuildTex(view_results, ...)
write
if filereadable("Makefile")
" If Makefile is available in current working directory, run 'make' with arguments
echo "(using Makefile)"
let l:cmd = "!make ".join(a:000, ' ')
echo l:cmd
execute l:cmd
if a:view_results && v:shell_error == 0
call ViewTexResults()
endif
else
let b:tex_flavor = 'pdflatex'
compiler tex
make %
if a:view_results && v:shell_error == 0
call ViewTexResults()
endif
endif
endfunction
function! ViewTexResults(...)
if a:0 == 0
let l:target = expand("%:p:r") . ".pdf"
else
let l:target = a:1
endif
if has('mac')
execute "! open -a Preview ".l:target
endif
endfunction
问题在于,即使有编译错误,也没有设置
v:shell_error
。如果您有任何建议或见解,如何检测编译是否成功将不胜感激!谢谢!
在这里给出的答案以及其他方法的研究中,我认为已经得到了令人满意的解决。我在此发布解决方案,以防其他人有兴趣。
基本上,最好的解决方案似乎是使用LaTeX的包装器Rubber,它通常“只需工作”,并提供非常干净的输出/错误。如果在系统上找到Rubber并且当前目录中没有Makefile,则首选下面介绍的解决方案使用Rubber。如果找到Makefile,则使用该文件。如果没有Makefile且未安装Rubber,则使用pdflatex。在所有情况下,如果源代码无法编译,则将(过滤和解析后的)错误发送到QuickFix缓冲区,并自动打开QuickFix窗口。如果成功编译,将写入一条简短的消息,并且如果用户请求,则会打开PDF进行查看。
在我的自己的安装中,我从Vim-Latex中提取了(优秀的)“SetLatexEfm()”函数来解析和过滤tex构建输出。但是,如果找不到此函数,则以下函数默认设置错误消息格式,该格式足以识别并在QuickFix窗口中突出显示错误,尽管有很多垃圾。
function! BuildTex(view_results, ...)
" record position
let save_cursor = getpos(".")
" save work
silent write
" From: https://dev59.com/y03Sa4cB1Zd3GeqPuE45
" If your shell is bash, you can use the ${PIPESTATUS} array variable to get
" the correct exit code (borrowed from this answer to another question).
silent setlocal shell=bash
silent setlocal shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}
let success = 1
if filereadable("Makefile")
" If Makefile is available in current working directory, run 'make' with arguments
echon "compiling using Makefile ..."
let l:makecmd = "make\\ ".join(a:000, '\\ ')
silent execute "setlocal makeprg=" . l:makecmd
try
" This function is defined in the Vim-Latex package,
" and provides excellent parsing and filtering of the error messages
" when running latex outside of the Rubber wrapper.
call s:SetLatexEfm()
catch /E117/
set errorformat=%E!\ LaTeX\ %trror:\ %m,
\%E!\ %m,
\%+WLaTeX\ %.%#Warning:\ %.%#line\ %l%.%#,
\%+W%.%#\ at\ lines\ %l--%*\\d,
\%WLaTeX\ %.%#Warning:\ %m,
\%Cl.%l\ %m,
\%+C\ \ %m.,
\%+C%.%#-%.%#,
\%+C%.%#[]%.%#,
\%+C[]%.%#,
\%+C%.%#%[{}\\]%.%#,
\%+C<%.%#>%.%#,
\%C\ \ %m,
\%-GSee\ the\ LaTeX%m,
\%-GType\ \ H\ <return>%m,
\%-G\ ...%.%#,
\%-G%.%#\ (C)\ %.%#,
\%-G(see\ the\ transcript%.%#),
\%-G\\s%#,
\%+O(%f)%r,
\%+P(%f%r,
\%+P\ %\\=(%f%r,
\%+P%*[^()](%f%r,
\%+P[%\\d%[^()]%#(%f%r,
\%+Q)%r,
\%+Q%*[^()])%r,
\%+Q[%\\d%*[^()])%r
endtry
silent make
else
let l:special_tex_compiler = "rubber"
if executable(l:special_tex_compiler)
echon "compiling with Rubber ..."
silent execute "setlocal makeprg=" . l:special_tex_compiler . "\\ -dfs\\ %"
setlocal errorformat=%f:%l:\ %m
silent make %
else
echon "compiling ..."
let b:tex_flavor = 'pdflatex'
compiler tex
silent make %
endif
endif
" set/report compile status
if v:shell_error
let l:success = 0
" let l:wheight = winheight(bufnr("%")) / 2
" execute "copen ".l:wheight
copen
else
let l:success = 1
cclose
redraw
echon "successfully compiled"
endif
" view results if successful compile
if l:success && a:view_results
call ViewTexResults()
endif
" restore position
call setpos('.', save_cursor)
endfunction
function! ViewTexResults(...)
if a:0 == 0
let l:target = expand("%:p:r") . ".pdf"
else
let l:target = a:1
endif
if has('mac')
silent execute "! open -a Preview ".l:target
" obviously, you will need to write specific commands for other systems
" left as an exercise for the reader ...
endif
endfunction
command! -buffer -nargs=* BuildTex call BuildTex(0, <f-args>)
command! -buffer -nargs=* BuildAndViewTex call BuildTex(1, <f-args>)
noremap <buffer> <silent> <F9> <Esc>:call BuildTex(0)<CR>
noremap <buffer> <silent> <S-F9> <Esc>:call BuildTex(1)<CR>
更新:我已经将这个脚本打包发布为 Vim 文件类型插件,可以在http://www.vim.org/scripts/script.php?script_id=3230下载。