rc/vim/bundle/LaTeX-Box/ftplugin/latex-box/findmain.vim
2015-09-30 23:50:31 +02:00

62 lines
2.2 KiB
VimL

" LatexBox_GetMainFileName: gets the name of the main file being compiled. {{{
" Description: returns the full path name of the main file.
" This function checks for the existence of a .latexmain file
" which might point to the location of a "main" latex file.
" If .latexmain exists, then return the full path name of the
" file being pointed to by it.
"
" Otherwise, return the full path name of the current buffer.
"
" You can supply an optional "modifier" argument to the
" function, which will optionally modify the file name before
" returning.
" NOTE: From version 1.6 onwards, this function always trims
" away the .latexmain part of the file name before applying the
" modifier argument.
" NOTE: This function is copied from the Latex-Suite project!
function! LatexBox_GetMainFileName(...)
if a:0 > 0
let modifier = a:1
else
let modifier = ':p'
endif
let s:origdir = fnameescape(getcwd())
let dirmodifier = '%:p:h'
let dirLast = fnameescape(expand(dirmodifier))
exe 'cd '.dirLast
" move up the directory tree until we find a .latexmain file.
" TODO: Should we be doing this recursion by default, or should there be a
" setting?
while glob('*.latexmain',1) == ''
let dirmodifier = dirmodifier.':h'
let dirNew = fnameescape(expand(dirmodifier))
" break from the loop if we cannot go up any further.
if dirNew == dirLast
break
endif
let dirLast = dirNew
exe 'cd '.dirLast
endwhile
let lheadfile = glob('*.latexmain',1)
if lheadfile != ''
" Remove the trailing .latexmain part of the filename... We never want
" that.
let lheadfile = fnamemodify(substitute(lheadfile, '\.latexmain$', '', ''), modifier)
else
" If we cannot find any main file, just modify the filename of the
" current buffer.
let lheadfile = expand('%'.modifier)
endif
exe 'cd '.s:origdir
" NOTE: The caller of this function needs to escape the file name with
" fnameescape() . The reason its not done here is that escaping is not
" safe if this file is to be used as part of an external command on
" certain platforms.
return lheadfile
endfunction