vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

pdf.vim (2685B)


      1 " Vim filetype plugin file
      2 " Language:	PDF
      3 " Maintainer:	Tim Pope <vimNOSPAM@tpope.info>
      4 " Last Change:	2007 Dec 16
      5 " 		2024 May 23 by Riley Bruins <ribru17@gmail.com> ('commentstring')
      6 
      7 if exists("b:did_ftplugin")
      8     finish
      9 endif
     10 let b:did_ftplugin = 1
     11 
     12 setlocal commentstring=%\ %s
     13 setlocal comments=:%
     14 let b:undo_ftplugin = "setlocal cms< com< | unlet! b:match_words"
     15 
     16 if exists("g:loaded_matchit")
     17     let b:match_words = '\<\%(\d\+\s\+\d\+\s\+\)obj\>:\<endobj\>,\<stream$:\<endstream\>,\<xref\>:\<trailer\>,<<:>>'
     18 endif
     19 
     20 if exists("g:no_plugin_maps") || exists("g:no_pdf_maps") || v:version < 700
     21     finish
     22 endif
     23 
     24 if !exists("b:pdf_tagstack")
     25     let b:pdf_tagstack = []
     26 endif
     27 
     28 let b:undo_ftplugin .= " | silent! nunmap <buffer> <C-]> | silent! nunmap <buffer> <C-T>"
     29 nnoremap <silent><buffer> <C-]> :call <SID>Tag()<CR>
     30 " Inline, so the error from an empty tag stack will be simple.
     31 nnoremap <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 
     33 function! 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
     55 endfunction
     56 
     57 function! 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
     71 endfunction
     72 
     73 function! 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
     83 endfunction
     84 
     85 function! s:notag()
     86     silent! call remove(b:pdf_tagstack,-1)
     87     echohl ErrorMsg
     88     echo "E426: tag not found"
     89     echohl NONE
     90 endfunction