From b1696f0767e88f3e894b543b8414f0b9d5a1e3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Tue, 3 Jan 2017 23:11:02 +0100 Subject: [PATCH] Use findfile to simplify recursive file finding --- autoload/vimtex.vim | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/autoload/vimtex.vim b/autoload/vimtex.vim index 7f1d936..08298fe 100644 --- a/autoload/vimtex.vim +++ b/autoload/vimtex.vim @@ -592,31 +592,23 @@ function! s:get_main_from_subfile() " {{{1 \ '^\C\s*\\documentclass\[\zs.*\ze\]{subfiles}') if len(l:filename) > 0 if l:filename[0] ==# '/' - " Absolute path + " Specified path is absolute if filereadable(l:filename) | return l:filename | endif else - " First try relative path + " Try specified path as relative to current file path let l:candidate = simplify(expand('%:p:h') . '/' . l:filename) if filereadable(l:candidate) | return l:candidate | endif - " Now assume that the path is relative to the main file root. This is + " Try specified path as relative to the project main file. This is " difficult, since the main file is the one we are looking for. We - " therefore assume that the main file lives somewhere in the directory - " tree closer to the root. - let l:path = expand('%:p:h') - let l:candidates = [l:path] - while l:path != fnamemodify(l:path, ':h') - let l:path = fnamemodify(l:path, ':h') - let l:candidates += [l:path] - endwhile - call map(l:candidates, 'v:val . ''/'' . l:filename') - for l:candidate in l:candidates - if filereadable(l:candidate) - let s:root = fnamemodify(l:candidate, ':h') - let s:base = strpart(expand('%:p'), len(s:root) + 1) - return l:candidate - endif - endfor + " therefore assume that the main file lives somewhere upwards in the + " directory tree. + let l:candidate = findfile(l:filename, '.;') + if filereadable(l:candidate) + let s:root = fnamemodify(l:candidate, ':h') + let s:base = strpart(expand('%:p'), len(s:root) + 1) + return l:candidate + endif endif endif endfor