pdf.vim 2.6 KB

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