help.vim 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. " Vim filetype plugin file
  2. " Language: Vim help file
  3. " Previous Maintainer: Nikolai Weibull <now@bitwi.se>
  4. " Latest Revision: 2008-07-09
  5. if exists("b:did_ftplugin")
  6. finish
  7. endif
  8. let b:did_ftplugin = 1
  9. let s:cpo_save = &cpo
  10. set cpo&vim
  11. let b:undo_ftplugin = "setl fo< tw< cole< cocu< keywordprg<"
  12. setlocal formatoptions+=tcroql textwidth=78
  13. if has("conceal")
  14. setlocal cole=2 cocu=nc
  15. endif
  16. " Prefer Vim help instead of manpages.
  17. setlocal keywordprg=:help
  18. if !exists('g:no_plugin_maps')
  19. function! s:show_toc() abort
  20. let bufname = bufname('%')
  21. let info = getloclist(0, {'winid': 1})
  22. if !empty(info) && getwinvar(info.winid, 'qf_toc') ==# bufname
  23. lopen
  24. return
  25. endif
  26. let toc = []
  27. let lnum = 2
  28. let last_line = line('$') - 1
  29. let last_added = 0
  30. let has_section = 0
  31. let has_sub_section = 0
  32. while lnum && lnum <= last_line
  33. let level = 0
  34. let add_text = ''
  35. let text = getline(lnum)
  36. if text =~# '^=\+$' && lnum + 1 < last_line
  37. " A de-facto section heading. Other headings are inferred.
  38. let has_section = 1
  39. let has_sub_section = 0
  40. let lnum = nextnonblank(lnum + 1)
  41. let text = getline(lnum)
  42. let add_text = text
  43. while add_text =~# '\*[^*]\+\*\s*$'
  44. let add_text = matchstr(add_text, '.*\ze\*[^*]\+\*\s*$')
  45. endwhile
  46. elseif text =~# '^[A-Z0-9][-A-ZA-Z0-9 .][-A-Z0-9 .():]*\%([ \t]\+\*.\+\*\)\?$'
  47. " Any line that's yelling is important.
  48. let has_sub_section = 1
  49. let level = has_section
  50. let add_text = matchstr(text, '.\{-}\ze\s*\%([ \t]\+\*.\+\*\)\?$')
  51. elseif text =~# '\~$'
  52. \ && matchstr(text, '^\s*\zs.\{-}\ze\s*\~$') !~# '\t\|\s\{2,}'
  53. \ && getline(lnum - 1) =~# '^\s*<\?$\|^\s*\*.*\*$'
  54. \ && getline(lnum + 1) =~# '^\s*>\?$\|^\s*\*.*\*$'
  55. " These lines could be headers or code examples. We only want the
  56. " ones that have subsequent lines at the same indent or more.
  57. let l = nextnonblank(lnum + 1)
  58. if getline(l) =~# '\*[^*]\+\*$'
  59. " Ignore tag lines
  60. let l = nextnonblank(l + 1)
  61. endif
  62. if indent(lnum) <= indent(l)
  63. let level = has_section + has_sub_section
  64. let add_text = matchstr(text, '\S.*')
  65. endif
  66. endif
  67. let add_text = substitute(add_text, '\s\+$', '', 'g')
  68. if !empty(add_text) && last_added != lnum
  69. let last_added = lnum
  70. call add(toc, {'bufnr': bufnr('%'), 'lnum': lnum,
  71. \ 'text': repeat(' ', level) . add_text})
  72. endif
  73. let lnum = nextnonblank(lnum + 1)
  74. endwhile
  75. call setloclist(0, toc, ' ')
  76. call setloclist(0, [], 'a', {'title': 'Help TOC'})
  77. lopen
  78. let w:qf_toc = bufname
  79. endfunction
  80. nnoremap <silent><buffer> gO :call <sid>show_toc()<cr>
  81. endif
  82. let &cpo = s:cpo_save
  83. unlet s:cpo_save