Merge branch 'issue-640/feature/improve-img-completion'

This commit is contained in:
Karl Yngve Lervåg 2017-01-18 19:16:17 +01:00
commit 10372c7d3a
9 changed files with 91 additions and 15 deletions

View File

@ -10,7 +10,6 @@ function! vimtex#complete#init_options() " {{{1
call vimtex#util#set_default('g:vimtex_complete_close_braces', 0)
call vimtex#util#set_default('g:vimtex_complete_recursive_bib', 0)
call vimtex#util#set_default('g:vimtex_complete_img_use_tail', 0)
endfunction
" }}}1
@ -378,26 +377,22 @@ let s:img = {
\ 'patterns' : ['\v\\includegraphics\*?%(\s*\[[^]]*\]){0,2}\s*\{[^}]*$'],
\ 'ext_re' : '\v\.%('
\ . join(['png', 'jpg', 'eps', 'pdf', 'pgf', 'tikz'], '|')
\ . ')$'
\ . ')$',
\}
function! s:img.complete(regex) dict " {{{2
let self.candidates = []
let self.candidates = split(globpath(b:vimtex.root, '**/*.*'), '\n')
call self.get_graphicspaths()
call self.gather_candidates()
let l:output = b:vimtex.out()
call filter(self.candidates, 'v:val !=# l:output')
call filter(self.candidates, 'v:val =~? self.ext_re')
call filter(self.candidates, 'v:val =~# a:regex')
call map(self.candidates, 'strpart(v:val, len(b:vimtex.root)+1)')
call map(self.candidates, '{
\ ''abbr'' : v:val,
\ ''word'' : v:val,
\ ''menu'' : '' [graphics]'',
\ }')
if g:vimtex_complete_img_use_tail
if !empty(self.graphicspaths)
for l:cand in self.candidates
let l:cand.word = fnamemodify(l:cand.word, ':t')
endfor
@ -406,6 +401,36 @@ function! s:img.complete(regex) dict " {{{2
return self.candidates
endfunction
function! s:img.get_graphicspaths() dict " {{{2
" Get preamble text and remove comments
let l:preamble = vimtex#parser#tex(b:vimtex.tex, {
\ 're_stop': '\\begin{document}',
\ 'detailed': 0,
\})
call map(l:preamble, 'substitute(v:val, ''\\\@<!%.*'', '''', '''')')
" Parse preamble for graphicspath command
let l:graphicspath = matchstr(join(l:preamble, ' '),
\ '\\graphicspath{\s*{\s*\zs.*\ze\s*}\s*}')
let self.graphicspaths = map(split(l:graphicspath, '}\s*{'),
\ 'v:val[0] ==# ''/'' ? v:val : simplify(b:vimtex.root . ''/'' . v:val)')
endfunction
function! s:img.gather_candidates() dict " {{{2
if !empty(self.graphicspaths)
let self.candidates = split(globpath(expand('%:p:h'), '*.*'), '\n')
for l:path in self.graphicspaths
let self.candidates += split(globpath(l:path, '*.*'), '\n')
endfor
else
let self.candidates = split(globpath(b:vimtex.root, '**/*.*'), '\n')
endif
call filter(self.candidates, 'v:val !=# b:vimtex.out()')
call filter(self.candidates, 'v:val =~? self.ext_re')
call map(self.candidates, 'vimtex#paths#shorten_relative(v:val)')
endfunction
" }}}1
" {{{1 Filenames (\input and \include)

44
autoload/vimtex/paths.vim Normal file
View File

@ -0,0 +1,44 @@
" vimtex - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"
function! vimtex#paths#shorten_relative(path) " {{{1
" Input: An absolute path
" Output: Relative path with respect to the vimtex root, path relative to
" vimtex root (unless absolute path is shorter)
let l:relative = vimtex#paths#relative(a:path, b:vimtex.root)
return strlen(l:relative) < strlen(a:path)
\ ? l:relative : a:path
endfunction
" }}}1
function! vimtex#paths#relative(path, current) " {{{1
" Note: This algorithm is based on the one presented by @Offirmo at SO,
" http://stackoverflow.com/a/12498485/51634
let l:target = a:path
let l:common = a:current
let l:result = ''
while substitute(l:target, '^' . l:common, '', '') ==# l:target
let l:common = fnamemodify(l:common, ':h')
let l:result = empty(l:result) ? '..' : '../' . l:result
endwhile
if l:common ==# '/'
let l:result .= '/'
endif
let l:forward = substitute(l:target, '^' . l:common, '', '')
if !empty(l:forward)
let l:result = empty(l:result)
\ ? l:forward[1:]
\ : l:result . l:forward
endif
return l:result
endfunction
" }}}1

View File

@ -375,12 +375,6 @@ Options~
Default value: 0
*g:vimtex_complete_img_use_tail*
If enabled, only the tail part of file names are completed. This is useful
if one uses the `graphicx` package and the `\graphicspath` command.
Default value: 0
*g:vimtex_delim_toggle_mod_list*
Define list of delimiter modifiers to toggle through with
|<plug>(vimtex-delim-toggle-modifier)|.

View File

View File

View File

View File

@ -0,0 +1,13 @@
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{{test} {testing}%
{../}
{../testagain/}
}
\begin{document}
\includegraphics[width=0.9\linewidth]{}
\end{document}

View File

View File