Vim脚本编译TeX源并仅在没有错误时启动PDF

Jee*_*eet 6 vim latex pdflatex

我正在切换到使用Vim进行我的LaTeX编辑环境.我希望能够在Vim中编写源文件,并在编译成功时启动外部查看.

我知道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
Run Code Online (Sandbox Code Playgroud)

问题是v:shell_error没有设置,即使存在编译错误.任何有关如何检测编译是否成功的建议或见解将不胜感激!谢谢!


在这里给出的答案之间,加上对其他方法的一些研究,我认为这已经得到了令人满意的解决.我在这里发布解决方案以防其他人感兴趣.

基本上,最好的解决方案似乎是使用Rubber,一个围绕LaTeX的包装,通常"正常工作",并提供非常干净的输出/错误.我在下面提出的解决方案优先使用Rubber,如果它在系统中找到并且在当前目录中找不到Makefile.如果找到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: http://stackoverflow.com/questions/2679475/vim-script-to-compile-tex-source-and-launch-pdf-only-if-no-errors
        " 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>
Run Code Online (Sandbox Code Playgroud)

更新:我已将其打包并发布为Vim文件类型插件脚本,可从以下网址获得:http://www.vim.org/scripts/script.php?script_id = 3230.

Cur*_*son 3

假设您陷入了 else-theres-no-makefile 部分,问题可能出在shellpipe变量上。

在我的系统(Ubuntu)上,如果失败, shellpipe=2>&1| tee内置make调用不会设置。v:shell_error

的返回状态| tee可能就是v:shell_error正在设置的状态。

如果您的 shell 是 bash,则可以使用${PIPESTATUS}数组变量来获取正确的退出代码(借用另一个问题的答案)。

:set shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}
Run Code Online (Sandbox Code Playgroud)

否则,您可以尝试:

:set shellpipe=\>
:make %
Run Code Online (Sandbox Code Playgroud)

这会v:shell_error在失败时设置,但我不确定这是否会扰乱“转到错误行号”功能(如果有)。

要查看变量设置的内容:

:set shellpipe?
Run Code Online (Sandbox Code Playgroud)