1" Vim filetype plugin file
2" Language:	PDF
3" Maintainer:	Tim Pope <vimNOSPAM@tpope.info>
4" Last Change:	2007 Dec 16
5
6if exists("b:did_ftplugin")
7    finish
8endif
9let b:did_ftplugin = 1
10
11setlocal commentstring=%%s
12setlocal comments=:%
13let b:undo_ftplugin = "setlocal cms< com< | unlet! b:match_words"
14
15if exists("g:loaded_matchit")
16    let b:match_words = '\<\%(\d\+\s\+\d\+\s\+\)obj\>:\<endobj\>,\<stream$:\<endstream\>,\<xref\>:\<trailer\>,<<:>>'
17endif
18
19if exists("g:no_plugin_maps") || exists("g:no_pdf_maps") || v:version < 700
20    finish
21endif
22
23if !exists("b:pdf_tagstack")
24    let b:pdf_tagstack = []
25endif
26
27let b:undo_ftplugin .= " | silent! nunmap <buffer> <C-]> | silent! nunmap <buffer> <C-T>"
28nnoremap <silent><buffer> <C-]> :call <SID>Tag()<CR>
29" Inline, so the error from an empty tag stack will be simple.
30nnoremap <silent><buffer> <C-T> :if len(b:pdf_tagstack) > 0 <Bar> call setpos('.',remove(b:pdf_tagstack, -1)) <Bar> else <Bar> exe "norm! \<Lt>C-T>" <Bar> endif<CR>
31
32function! s:Tag()
33    call add(b:pdf_tagstack,getpos('.'))
34    if getline('.') =~ '^\d\+$' && getline(line('.')-1) == 'startxref'
35	return s:dodigits(getline('.'))
36    elseif getline('.') =~ '/Prev\s\+\d\+\>\%(\s\+\d\)\@!' && expand("<cword>") =~ '^\d\+$'
37	return s:dodigits(expand("<cword>"))
38    elseif getline('.') =~ '^\d\{10\} \d\{5\} '
39	return s:dodigits(matchstr(getline('.'),'^\d\+'))
40    else
41	let line = getline(".")
42	let lastend = 0
43	let pat = '\<\d\+\s\+\d\+\s\+R\>'
44	while lastend >= 0
45	    let beg = match(line,'\C'.pat,lastend)
46	    let end = matchend(line,'\C'.pat,lastend)
47	    if beg < col(".") && end >= col(".")
48		return s:doobject(matchstr(line,'\C'.pat,lastend))
49	    endif
50	    let lastend = end
51	endwhile
52	return s:notag()
53    endif
54endfunction
55
56function! s:doobject(string)
57    let first = matchstr(a:string,'^\s*\zs\d\+')
58    let second = matchstr(a:string,'^\s*\d\+\s\+\zs\d\+')
59    norm! m'
60    if first != '' && second != ''
61	let oldline = line('.')
62	let oldcol = col('.')
63	1
64	if !search('^\s*'.first.'\s\+'.second.'\s\+obj\>')
65	    exe oldline
66	    exe 'norm! '.oldcol.'|'
67	    return s:notag()
68	endif
69    endif
70endfunction
71
72function! s:dodigits(digits)
73    let digits = 0 + substitute(a:digits,'^0*','','')
74    norm! m'
75    if digits <= 0
76	norm! 1go
77    else
78	" Go one character before the destination and advance.  This method
79	" lands us after a newline rather than before, if that is our target.
80	exe "goto ".(digits)."|norm! 1 "
81    endif
82endfunction
83
84function! s:notag()
85    silent! call remove(b:pdf_tagstack,-1)
86    echohl ErrorMsg
87    echo "E426: tag not found"
88    echohl NONE
89endfunction
90