Better support for multi-file documents #6

This commit is contained in:
Karl Yngve Lervåg 2013-12-10 17:42:25 +01:00
parent 798d0561a2
commit 4beac696c0
2 changed files with 72 additions and 15 deletions

View File

@ -112,7 +112,7 @@ function! s:init_environment()
" "
" Create new or link to old blob " Create new or link to old blob
" "
let main = s:get_main_tex() let main = s:get_main()
let id = s:get_id(main) let id = s:get_id(main)
if id >= 0 if id >= 0
let b:latex.id = id let b:latex.id = id
@ -204,22 +204,62 @@ function! s:get_id(main)
return -1 return -1
endfunction endfunction
" {{{1 s:get_main_tex " {{{1 s:get_main
function! s:get_main_tex() function! s:get_main()
let re_begin = '\C\\begin\_\s*{document}' "
let re_input = '\v\\(input|include)\{' . expand('%:t:r') . '(\.tex)?' " Search for main file specifier at the beginning of file. This is similar
" to the method used by several other plugins and editors, such as vim with
if search(re_begin, 'nw') " LaTeX-Box, TextMate, TexWorks, and texmaker.
return expand('%:p') "
else for line in getline(1, 5)
for l:file in glob('*.tex', 0, 1) + glob('../*.tex', 0, 1) let candidate = matchstr(line,
let lines = readfile(l:file) \ '^\s*%\s*!\s*[tT][eE][xX]\s\+root\s*=\s*\zs.*\ze\s*$')
if len(filter(copy(lines), 'v:val =~ re_input')) > 0 if len(candidate) > 0
\ && len(filter(lines, 'v:val =~ re_begin')) > 0 let main = fnamemodify(candidate, ':p')
return fnamemodify(l:file, ':p') if filereadable(main)
return main
endif endif
endfor endif
endfor
"
" Search for main file recursively through \input and \include specifiers
"
let main = s:get_main_recurse(expand('%:p'))
if filereadable(main)
return main
endif endif
"
" If not found, use current file
"
return expand('%:p')
endfunction
" {{{1 s:get_main_recurse
function! s:get_main_recurse(file)
"
" Check if current file is a main file
"
if len(filter(readfile(a:file),
\ 'v:val =~ ''\C\\begin\_\s*{document}''')) > 0
return fnamemodify(a:file, ':p')
endif
"
" Search for files that include the current file
"
for l:file in glob('*.tex', 0, 1) + glob('../*.tex', 0, 1)
if len(filter(readfile(l:file), 'v:val =~ ''\v\\(input|include)\{'
\ . fnamemodify(a:file, ':t:r') . '(\.tex)?''')) > 0
return s:get_main_recurse(l:file)
endif
endfor
"
" If not found, return 0
"
return 0
endfunction endfunction
" {{{1 s:get_main_ext " {{{1 s:get_main_ext

View File

@ -126,6 +126,23 @@ Functions:
|latex#reinit| |latex#reinit|
|latex#view| |latex#view|
------------------------------------------------------------------------------
Support for multi-file projects:~
*vim-latex-multi-file*
*tex-root*
|vim-latex| supports multi-file documents in the sense that the plugin
automatically detects the root file of the project. This detection is based
on a recursive search for files that include the current tex file, but it is
naive in the sense that it assumes unique file names. It also assumes that
the main `tex` file lives in the same folder or the parent folder of the
current file. The method is simple, but it should work in most cases.
It is also possible to specify the main TeX file with a comment in one of the
first five lines of the current file similar to this: >
%! TEX root = my-main.tex
============================================================================== ==============================================================================
DEFAULT MAPPINGS *vim-latex-mappings* DEFAULT MAPPINGS *vim-latex-mappings*