Skip to content

Commit b7690c2

Browse files
committed
merge: build_dir → (out_dir, aux_dir)
This changes build_dir to out_dir in general and adds support for setting aux_dir for latexmk. refer: #2534
2 parents 127c0a2 + 3ddeafc commit b7690c2

File tree

30 files changed

+346
-179
lines changed

30 files changed

+346
-179
lines changed

autoload/vimtex/compiler.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function! vimtex#compiler#compile_selected(type) abort range " {{{1
142142
" Create and initialize temporary compiler
143143
let l:compiler = s:init_compiler({
144144
\ 'state': l:file,
145-
\ 'build_dir': '',
145+
\ 'out_dir': '',
146146
\ 'continuous': 0,
147147
\ 'callback': 0,
148148
\})
@@ -266,7 +266,7 @@ function! vimtex#compiler#clean(full) abort " {{{1
266266

267267
call b:vimtex.compiler.clean(a:full)
268268
sleep 100m
269-
call b:vimtex.compiler.remove_build_dir()
269+
call b:vimtex.compiler.remove_dirs()
270270
call vimtex#log#info('Compiler clean finished' . (a:full ? ' (full)' : ''))
271271

272272

autoload/vimtex/compiler/_template.vim

Lines changed: 84 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endfunction
1414
let s:compiler = {
1515
\ 'name': '__template__',
1616
\ 'enabled': v:true,
17-
\ 'build_dir': '',
17+
\ 'out_dir': '',
1818
\ 'continuous': 0,
1919
\ 'hooks': [],
2020
\ 'output': tempname(),
@@ -30,9 +30,21 @@ function! s:compiler.new(options) abort dict " {{{1
3030

3131
call l:compiler.__check_requirements()
3232

33-
call s:build_dir_materialize(l:compiler)
33+
call vimtex#util#materialize_property(l:compiler, 'out_dir')
3434
call l:compiler.__init()
35-
call s:build_dir_respect_envvar(l:compiler)
35+
36+
" $VIMTEX_OUTPUT_DIRECTORY overrides configured compiler.out_dir
37+
if !empty($VIMTEX_OUTPUT_DIRECTORY)
38+
if !empty(l:compiler.out_dir)
39+
\ && (l:compiler.out_dir !=# $VIMTEX_OUTPUT_DIRECTORY)
40+
call vimtex#log#warning(
41+
\ 'Setting VIMTEX_OUTPUT_DIRECTORY overrides out_dir!',
42+
\ 'Changed out_dir from: ' . l:compiler.out_dir,
43+
\ 'Changed out_dir to: ' . $VIMTEX_OUTPUT_DIRECTORY)
44+
endif
45+
46+
let l:compiler.out_dir = $VIMTEX_OUTPUT_DIRECTORY
47+
endif
3648

3749
" Remove init methods
3850
unlet l:compiler.new
@@ -74,8 +86,8 @@ function! s:compiler.__pprint() abort dict " {{{1
7486
call add(l:list, ['options', self.options])
7587
endif
7688

77-
if !empty(self.build_dir)
78-
call add(l:list, ['build_dir', self.build_dir])
89+
if !empty(self.out_dir)
90+
call add(l:list, ['out_dir', self.out_dir])
7991
endif
8092

8193
if has_key(self, '__pprint_append')
@@ -98,16 +110,75 @@ endfunction
98110

99111
" }}}1
100112

113+
function! s:compiler._create_build_dir(path) abort dict " {{{1
114+
" Create build dir "path" if it does not exist
115+
" Note: This may need to create a hierarchical structure!
116+
if empty(a:path) | return | endif
117+
118+
if has_key(self.state, 'get_sources')
119+
let l:dirs = self.state.get_sources()
120+
call filter(map(
121+
\ l:dirs, "fnamemodify(v:val, ':h')"),
122+
\ {_, x -> x !=# '.'})
123+
call filter(l:dirs, {_, x -> stridx(x, '../') != 0})
124+
else
125+
let l:dirs = glob(self.state.root . '/**/*.tex', v:false, v:true)
126+
call map(l:dirs, "fnamemodify(v:val, ':h')")
127+
call map(l:dirs, 'strpart(v:val, strlen(self.state.root) + 1)')
128+
endif
129+
call uniq(sort(filter(l:dirs, '!empty(v:val)')))
130+
131+
call map(l:dirs, {_, x ->
132+
\ (vimtex#paths#is_abs(a:path) ? '' : self.state.root . '/')
133+
\ . a:path . '/' . x})
134+
call filter(l:dirs, '!isdirectory(v:val)')
135+
if empty(l:dirs) | return | endif
136+
137+
" Create the non-existing directories
138+
call vimtex#log#warning(["Creating directorie(s):"]
139+
\ + map(copy(l:dirs), {_, x -> '* ' . x}))
140+
141+
for l:dir in l:dirs
142+
call mkdir(l:dir, 'p')
143+
endfor
144+
endfunction
145+
146+
" }}}1
147+
function! s:compiler._remove_dir(path) abort dict " {{{1
148+
if empty(a:path) | return | endif
149+
150+
let l:out_dir = vimtex#paths#is_abs(a:path)
151+
\ ? a:path
152+
\ : self.state.root . '/' . a:path
153+
if !isdirectory(l:out_dir) | return | endif
154+
155+
let l:tree = glob(l:out_dir . '/**/*', 0, 1)
156+
let l:files = filter(copy(l:tree), 'filereadable(v:val)')
157+
158+
if empty(l:files)
159+
for l:dir in sort(l:tree) + [l:out_dir]
160+
call delete(l:dir, 'd')
161+
endfor
162+
endif
163+
endfunction
164+
165+
" }}}1
166+
167+
function! s:compiler.create_dirs() abort dict " {{{1
168+
call self._create_build_dir(self.out_dir)
169+
endfunction
170+
171+
" }}}1
172+
function! s:compiler.remove_dirs() abort dict " {{{1
173+
call self._remove_dir(self.out_dir)
174+
endfunction
175+
176+
" }}}1
177+
101178
function! s:compiler.get_file(ext) abort dict " {{{1
102-
" Check for various output directories
103-
" * Environment variable VIMTEX_OUTPUT_DIRECTORY. Note that this overrides
104-
" any VimTeX settings like g:vimtex_compiler_latexmk.build_dir!
105-
" * Compiler settings, such as g:vimtex_compiler_latexmk.build_dir, which is
106-
" available as b:vimtex.compiler.build_dir.
107-
" * Fallback to the main root directory
108179
for l:root in [
109180
\ $VIMTEX_OUTPUT_DIRECTORY,
110-
\ self.build_dir,
181+
\ self.out_dir,
111182
\ self.state.root
112183
\]
113184
if empty(l:root) | continue | endif
@@ -143,7 +214,7 @@ endfunction
143214
function! s:compiler.start(...) abort dict " {{{1
144215
if self.is_running() | return | endif
145216

146-
call self.create_build_dir()
217+
call self.create_dirs()
147218

148219
" Initialize output file
149220
call writefile([], self.output, 'a')
@@ -211,62 +282,6 @@ endfunction
211282

212283
" }}}1
213284

214-
function! s:compiler.create_build_dir() abort dict " {{{1
215-
" Create build dir if it does not exist
216-
" Note: This may need to create a hierarchical structure!
217-
if empty(self.build_dir) | return | endif
218-
219-
if has_key(self.state, 'get_sources')
220-
let l:dirs = self.state.get_sources()
221-
call filter(map(
222-
\ l:dirs, "fnamemodify(v:val, ':h')"),
223-
\ {_, x -> x !=# '.'})
224-
call filter(l:dirs, {_, x -> stridx(x, '../') != 0})
225-
else
226-
let l:dirs = glob(self.state.root . '/**/*.tex', v:false, v:true)
227-
call map(l:dirs, "fnamemodify(v:val, ':h')")
228-
call map(l:dirs, 'strpart(v:val, strlen(self.state.root) + 1)')
229-
endif
230-
call uniq(sort(filter(l:dirs, '!empty(v:val)')))
231-
232-
call map(l:dirs, {_, x ->
233-
\ (vimtex#paths#is_abs(self.build_dir) ? '' : self.state.root . '/')
234-
\ . self.build_dir . '/' . x})
235-
call filter(l:dirs, '!isdirectory(v:val)')
236-
if empty(l:dirs) | return | endif
237-
238-
" Create the non-existing directories
239-
call vimtex#log#warning(["Creating build_dir directorie(s):"]
240-
\ + map(copy(l:dirs), {_, x -> '* ' . x}))
241-
242-
for l:dir in l:dirs
243-
call mkdir(l:dir, 'p')
244-
endfor
245-
endfunction
246-
247-
" }}}1
248-
function! s:compiler.remove_build_dir() abort dict " {{{1
249-
" Remove auxilliary output directories (only if they are empty)
250-
if empty(self.build_dir) | return | endif
251-
252-
if vimtex#paths#is_abs(self.build_dir)
253-
let l:build_dir = self.build_dir
254-
else
255-
let l:build_dir = self.state.root . '/' . self.build_dir
256-
endif
257-
258-
let l:tree = glob(l:build_dir . '/**/*', 0, 1)
259-
let l:files = filter(copy(l:tree), 'filereadable(v:val)')
260-
261-
if empty(l:files)
262-
for l:dir in sort(l:tree) + [l:build_dir]
263-
call delete(l:dir, 'd')
264-
endfor
265-
endif
266-
endfunction
267-
268-
" }}}1
269-
270285

271286
let s:compiler_jobs = {}
272287
function! s:compiler_jobs.exec(cmd) abort dict " {{{1
@@ -461,38 +476,6 @@ endfunction
461476
" }}}1
462477

463478

464-
function! s:build_dir_materialize(compiler) abort " {{{1
465-
if type(a:compiler.build_dir) != v:t_func | return | endif
466-
467-
try
468-
let a:compiler.build_dir = a:compiler.build_dir()
469-
catch
470-
call vimtex#log#error(
471-
\ 'Could not expand build_dir function!',
472-
\ v:exception)
473-
let a:compiler.build_dir = ''
474-
endtry
475-
endfunction
476-
477-
" }}}1
478-
function! s:build_dir_respect_envvar(compiler) abort " {{{1
479-
" Specifying the build_dir by environment variable should override the
480-
" current value.
481-
if empty($VIMTEX_OUTPUT_DIRECTORY) | return | endif
482-
483-
if !empty(a:compiler.build_dir)
484-
\ && (a:compiler.build_dir !=# $VIMTEX_OUTPUT_DIRECTORY)
485-
call vimtex#log#warning(
486-
\ 'Setting VIMTEX_OUTPUT_DIRECTORY overrides build_dir!',
487-
\ 'Changed build_dir from: ' . a:compiler.build_dir,
488-
\ 'Changed build_dir to: ' . $VIMTEX_OUTPUT_DIRECTORY)
489-
endif
490-
491-
let a:compiler.build_dir = $VIMTEX_OUTPUT_DIRECTORY
492-
endfunction
493-
494-
" }}}1
495-
496479
function! s:check_callback(line) abort " {{{1
497480
let l:status = get(s:callbacks, substitute(a:line, '\r', '', ''))
498481
if l:status <= 0 | return | endif

0 commit comments

Comments
 (0)