endif
endfunction
+function! b:Markdown_Toc(...)
+ if a:0 > 0
+ let l:window_type = a:1
+ else
+ let l:window_type = 'vertical'
+ endif
+ silent vimgrep '^#' %
+ if l:window_type ==# 'horizontal'
+ copen
+ elseif l:window_type ==# 'vertical'
+ vertical copen
+ let &winwidth=(&columns/2)
+ elseif l:window_type ==# 'tab'
+ tab copen
+ else
+ copen
+ endif
+ set modifiable
+ %s/\v^([^|]*\|){2,2} #//
+ for i in range(1, line('$'))
+ let l:line = getline(i)
+ let l:header = matchstr(l:line, '^#*')
+ let l:length = len(l:header)
+ let l:line = substitute(l:line, '\v^#*[ ]*', '', '')
+ let l:line = substitute(l:line, '\v[ ]*#*$', '', '')
+ let l:line = repeat(' ', (2 * l:length)) . l:line
+ call setline(i, l:line)
+ endfor
+ set nomodified
+ set nomodifiable
+ normal! gg
+endfunction
+
" Wrapper to do move commands in visual mode.
"
function! s:VisMove(f)
execute 'vn <buffer><silent> ' . a:rhs . ' <esc>:call <sid>VisMove(''' . a:lhs . ''')<cr>'
endfunction
-call <sid>MapNormVis(']]', 'b:Markdown_MoveToNextHeader')
-call <sid>MapNormVis('[[', 'b:Markdown_MoveToPreviousHeader')
-call <sid>MapNormVis('][', 'b:Markdown_MoveToNextSiblingHeader')
-call <sid>MapNormVis('[]', 'b:Markdown_MoveToPreviousSiblingHeader')
+
+call <sid>MapNormVis('<Plug>(Markdown_MoveToNextHeader)', 'b:Markdown_MoveToNextHeader')
+call <sid>MapNormVis('<Plug>(Markdown_MoveToPreviousHeader)', 'b:Markdown_MoveToPreviousHeader')
+call <sid>MapNormVis('<Plug>(Markdown_MoveToNextSiblingHeader)', 'b:Markdown_MoveToNextSiblingHeader')
+call <sid>MapNormVis('<Plug>(Markdown_MoveToPreviousSiblingHeader)', 'b:Markdown_MoveToPreviousSiblingHeader')
" Menmonic: Up
-call <sid>MapNormVis(']u', 'b:Markdown_MoveToParentHeader')
+call <sid>MapNormVis('<Plug>(Markdown_MoveToParentHeader)', 'b:Markdown_MoveToParentHeader')
" Menmonic: Current
-call <sid>MapNormVis(']c', 'b:Markdown_MoveToCurHeader')
+call <sid>MapNormVis('<Plug>(Markdown_MoveToCurHeader)', 'b:Markdown_MoveToCurHeader')
+
+if ! exists('g:markdown_no_default_key_mappings')
+\ || !g:markdown_no_default_key_mappings
+ nmap ]] <Plug>(Markdown_MoveToNextHeader)
+ nmap [[ <Plug>(Markdown_MoveToPreviousHeader)
+ nmap ][ <Plug>(Markdown_MoveToNextSiblingHeader)
+ nmap [] <Plug>(Markdown_MoveToPreviousSiblingHeader)
+ nmap ]u <Plug>(Markdown_MoveToParentHeader)
+ nmap ]c <Plug>(Markdown_MoveToCurHeader)
+
+ vmap ]] <Plug>(Markdown_MoveToNextHeader)
+ vmap [[ <Plug>(Markdown_MoveToPreviousHeader)
+ vmap ][ <Plug>(Markdown_MoveToNextSiblingHeader)
+ vmap [] <Plug>(Markdown_MoveToPreviousSiblingHeader)
+ vmap ]u <Plug>(Markdown_MoveToParentHeader)
+ vmap ]c <Plug>(Markdown_MoveToCurHeader)
+endif
+
+command! -buffer Toc call b:Markdown_Toc()
+command! -buffer Toch call b:Markdown_Toc('horizontal')
+command! -buffer Tocv call b:Markdown_Toc('vertical')
+command! -buffer Toct call b:Markdown_Toc('tab')