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