6

LaTeX編集環境でVimを使用するように切り替えています。Vim内からソースファイルをtexし、コンパイルが成功した場合は外部表示を起動できるようにしたいと思います。

私は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が見つからない場合、優先的に使用します。Makefileが見つかった場合は、代わりにそれを使用します。Makefileがなく、Rubberがインストールされていない場合は、pdflatexを使用します。いずれの場合も、ソースのコンパイルに失敗すると、(フィルタリングおよび解析された)エラーがQuickFixバッファーに送信され、QuickFixウィンドウが自動的に開きます。正常にコンパイルされると、短いメッセージが書き込まれ、ユーザーが要求すると、PDFが開いて表示されます。

私自身のインストールでは、(優れた) "SetLatexEfm()"関数をVim-Latexから持ち上げて、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>

更新:これをVimファイルタイプのプラグインスクリプトとしてパッケージ化して公開しました。http ://www.vim.org/scripts/script.php?script_id=3230で入手できます。

4

3 に答える 3

3

else-theres-no-makefile セクションに陥っていると仮定すると、問題はshellpipe変数にある可能性があります。

私のシステム(Ubuntu)ではshellpipe=2>&1| tee、組み込みのmake呼び出しがv:shell_error失敗すると設定されません。

の戻りステータスは、設定されて| teeいるものである可能性があります。v:shell_error

シェルが bash の場合、配列変数を使用して正しい終了コードを取得できます (この回答${PIPESTATUS}から別の質問に借用)。

:set shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}

それ以外の場合は、次を試すことができます。

:set shellpipe=\>
:make %

これはv:shell_error失敗したときに設定されますが、エラー行番号に移動する機能がある場合、それが混乱するかどうかはわかりません。

変数が何に設定されているかを確認するには:

:set shellpipe?
于 2010-04-21T16:05:01.283 に答える
1

vimとは関係ないことはわかっていますが、 latexmkがその仕事をしていると思います。

これは、latex ファイルをコンパイルして pdf を更新するスクリプト (perl で作成) です。最も有用な未来は自動更新のものです。ファイルを保存したらすぐに「latexmk」でコンパイルします。pdf ビューアがサポートしている場合は、ビューが更新されます。

latexmk -pdf -pvc

于 2010-04-24T18:18:47.837 に答える
0

latexmk が「-halt-on-error」オプションを指定して (またはノンストップ モードで) latex を実行すると、コンパイルは入力のために一時停止せずに停止します。

于 2010-05-11T03:35:39.660 に答える