pdf.vim 2.6 KB

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