aboutsummaryrefslogtreecommitdiff
path: root/vim/plugin/latexlivepreview.vim
blob: 5f7c6de9f86f2419a07af83ea210d7a0261ec92f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
" Copyright (C) 2012 Hong Xu

" This file is part of vim-live-preview.

" vim-live-preview is free software: you can redistribute it and/or modify it
" under the terms of the GNU General Public License as published by the Free
" Software Foundation, either version 3 of the License, or (at your option)
" any later version.

" vim-live-preview is distributed in the hope that it will be useful, but
" WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
" or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
" more details.

" You should have received a copy of the GNU General Public License along with
" vim-live-preview.  If not, see <http://www.gnu.org/licenses/>.


if v:version < 700
    finish
endif

" check whether this script is already loaded
if exists("g:loaded_vim_live_preview")
    finish
endif
let g:loaded_vim_live_preview = 1

" this plugin requires +python feature
if !has('python')
    finish
endif

let s:saved_cpo = &cpo
set cpo&vim

let s:previewer = ''

" Run a shell command in background
function! s:RunInBackground(cmd)

python << EEOOFF

try:
    subprocess.Popen(
            vim.eval('a:cmd'),
            shell = True,
            universal_newlines = True,
            stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)

except:
    pass
EEOOFF
endfunction

function! s:Compile()

    if !exists('b:livepreview_buf_data') ||
                \ has_key(b:livepreview_buf_data, 'preview_running') == 0 ||
                \ b:livepreview_buf_data['preview_running'] == 0
        return
    endif

    lcd %:p:h

    silent exec 'write! ' . b:livepreview_buf_data['tmp_src_file']

    call s:RunInBackground(
                \ 'pdflatex -shell-escape -interaction=nonstopmode -output-directory=' .
                \ b:livepreview_buf_data['tmp_dir'] . ' ' .
                \ b:livepreview_buf_data['tmp_src_file'])

    if b:livepreview_buf_data['has_bibliography']
        " ToDo: Make the following work in Windows
        call system('cd ' .  b:livepreview_buf_data['tmp_dir'] .
                    \ ' && bibtex *.aux')
    endif

    lcd -
endfunction

function! s:StartPreview()
    lcd %:p:h
    
    let b:livepreview_buf_data = {}

    " Create a temp directory for current buffer
    python << EEOOFF
vim.command("let b:livepreview_buf_data['tmp_dir'] = '" +
        tempfile.mkdtemp() + "'")
EEOOFF

    let b:livepreview_buf_data['tmp_src_file'] =
                \ b:livepreview_buf_data['tmp_dir'] . '/' .
                \ fnameescape(expand('%:r')) . '.' . expand('%:e')

    silent exec 'write! ' . b:livepreview_buf_data['tmp_src_file']

    let l:tmp_out_file = b:livepreview_buf_data['tmp_dir'] . '/' .
                \ fnameescape(expand('%:r')) . '.pdf'

    silent call system('pdflatex -shell-escape -interaction=nonstopmode -output-directory=' .
                \ b:livepreview_buf_data['tmp_dir'] . ' ' .
                \ b:livepreview_buf_data['tmp_src_file'])
    if v:shell_error != 0
        echo 'Failed to compile'
    endif

    " Enable compilation of bibliography:
    let l:bib_files = split( glob( expand( '%:h' ) . '/**/*bib' ) )
    let b:livepreview_buf_data['has_bibliography'] = 0
    if len( l:bib_files ) > 0
        let b:livepreview_buf_data['has_bibliography'] = 1
        for bib_file in l:bib_files
            let bib_fn = fnamemodify(bib_file, ':t')
            call writefile(readfile(bib_file),
                        \ b:livepreview_buf_data['tmp_dir'] . '/' . bib_fn )
        endfor
        " ToDo: Make the following work in Windows
        silent call system('cd ' . b:livepreview_buf_data['tmp_dir'] .
                    \ ' && bibtex *.aux')
        " Bibtex requires multiple latex compilations:
        silent call system(
                    \ 'pdflatex -shell-escape -interaction=nonstopmode -output-directory=' .
                    \ b:livepreview_buf_data['tmp_dir'] . ' ' .
                    \ b:livepreview_buf_data['tmp_src_file'])
    endif
    if v:shell_error != 0
        echo 'Failed to compile bibliography'
    endif

    call s:RunInBackground(s:previewer . ' ' . l:tmp_out_file)

    lcd -
    
    let b:livepreview_buf_data['preview_running'] = 1
endfunction

" Initialization code
function! s:Initialize()
    let l:ret = 0
    python << EEOOFF
try:
    import vim
    import tempfile
    import subprocess
    import os
except:
    vim.command('let l:ret = 1')
EEOOFF

    if l:ret != 0
        return 'Python initialization failed.'
    endif

    " Get the previewer
    if exists('g:livepreview_previewer')
        let s:previewer = g:livepreview_previewer
    else
        for possible_previewer in [
                    \ 'evince',
                    \ 'okular']
            if executable(possible_previewer)
                let s:previewer = possible_previewer
                break
            endif
        endfor
    endif

    return 0
endfunction


let s:init_msg = s:Initialize()

if type(s:init_msg) == type('')
    echohl ErrorMsg
    echo 'vim-live-preview: ' . s:init_msg
    echohl None
endif

unlet! s:init_msg

command! LLPStartPreview call s:StartPreview()

autocmd CursorHold,CursorHoldI,BufWritePost *.tex call s:Compile()

let &cpo = s:saved_cpo
unlet! s:saved_cpo

" vim703: cc=80
" vim:fdm=marker et ts=4 tw=78 sw=4