Improved code for s:get_main()

This commit is contained in:
Karl Yngve Lervåg 2016-01-13 23:31:59 +01:00
parent 39d06eec48
commit a0e116efdb

View File

@ -427,52 +427,38 @@ function! s:get_main() " {{{1
endif endif
" "
" Search for main file specifier at the beginning of file. Recognized " Search for TEX root specifier at the beginning of file. This is used by
" specifiers are: " several other plugins and editors.
" "
" 1. The TEX root specifier, which is used by by several other plugins and let l:candidate = s:get_main_from_specifier(
" editors. \ '^\c\s*%\s*!\?\s*tex\s\+root\s*=\s*\zs.*\ze\s*$')
" 2. Subfiles package specifier. This parses the main tex file option in the if l:candidate !=# ''
" \documentclass line for the subfiles package. return l:candidate
endif
" "
for regexp in [ " Support for subfiles package
\ '^\c\s*%\s*!\?\s*tex\s\+root\s*=\s*\zs.*\ze\s*$', "
\ '^\C\s*\\documentclass\[\zs.*\ze\]{subfiles}', let l:candidate = s:get_main_from_specifier(
\ ] \ '^\C\s*\\documentclass\[\zs.*\ze\]{subfiles}')
for line in getline(1, 5) if l:candidate !=# ''
let filename = matchstr(line, regexp) return l:candidate
if len(filename) > 0 endif
if filename[0] !=# '/'
let candidates = [
\ expand('%:h') . '/' . filename,
\ getcwd() . '/' . filename,
\ ]
else
let candidates = [fnamemodify(filename, ':p')]
endif
for main in candidates
if filereadable(main)
return main
endif
endfor
endif
endfor
endfor
" "
" Search for .latexmain-specifier " Search for .latexmain-specifier
" "
let main = s:get_main_latexmain(expand('%:p')) let l:candidate = s:get_main_latexmain(expand('%:p'))
if filereadable(main) if l:candidate !=# ''
return main return l:candidate
endif endif
" "
" Search for main file recursively through include specifiers " Search for main file recursively through include specifiers
" "
let main = s:get_main_recurse(expand('%:p')) let l:candidate = s:get_main_recurse(expand('%:p'))
if filereadable(main) if l:candidate !=# ''
return main return l:candidate
endif endif
" "
@ -481,9 +467,30 @@ function! s:get_main() " {{{1
return expand('%:p') return expand('%:p')
endfunction endfunction
" }}}1
function! s:get_main_from_specifier(spec) " {{{1
for l:line in getline(1, 5)
let l:filename = matchstr(l:line, a:spec)
if len(l:filename) > 0
if l:filename[0] ==# '/'
if filereadable(l:filename) | return l:filename | endif
else
for l:candidate in [
\ expand('%:p:h') . '/' . l:filename,
\ getcwd() . '/' . l:filename
\]
if filereadable(l:candidate) | return l:candidate | endif
endfor
endif
endif
endfor
return ''
endfunction
" }}}1 " }}}1
function! s:get_main_latexmain(file) " {{{1 function! s:get_main_latexmain(file) " {{{1
if !filereadable(a:file) | return | endif if !filereadable(a:file) | return '' | endif
" "
" Gather candidate files " Gather candidate files
@ -501,12 +508,13 @@ function! s:get_main_latexmain(file) " {{{1
" closest to the current file in the directory tree) " closest to the current file in the directory tree)
" "
if len(l:candidates) > 0 if len(l:candidates) > 0
return fnamemodify(l:candidates[0], ':p:r') let l:candidate = fnamemodify(l:candidates[0], ':p:r')
return filereadable(l:candidate) ? l:candidate : ''
endif endif
endfunction endfunction
function! s:get_main_recurse(file) " {{{1 function! s:get_main_recurse(file) " {{{1
if !filereadable(a:file) | return | endif if !filereadable(a:file) | return '' | endif
" "
" Check if current file is a main file " Check if current file is a main file