]> git.madduck.net Git - etc/vim.git/blobdiff - ftplugin/mkd.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #70 from beyondwdq/master
[etc/vim.git] / ftplugin / mkd.vim
index 0627cc525f2f307ecf9bf31ab532ed85b98a526d..07f81db54e31d95d4d64944791f286d1609fac1e 100644 (file)
@@ -294,6 +294,39 @@ function! b:Markdown_MoveToPreviousSiblingHeader()
     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)
@@ -318,8 +351,8 @@ call <sid>MapNormVis('<Plug>(Markdown_MoveToParentHeader)', 'b:Markdown_MoveToPa
 " Menmonic: Current
 call <sid>MapNormVis('<Plug>(Markdown_MoveToCurHeader)', 'b:Markdown_MoveToCurHeader')
 
-if ! exists('g:markdown_no_default_key_mappings')
-\ || !g:markdown_no_default_key_mappings
+if ! exists('g:vim_markdown_no_default_key_mappings')
+\ || !g:vim_markdown_no_default_key_mappings
     nmap ]] <Plug>(Markdown_MoveToNextHeader)
     nmap [[ <Plug>(Markdown_MoveToPreviousHeader)
     nmap ][ <Plug>(Markdown_MoveToNextSiblingHeader)
@@ -334,3 +367,8 @@ if ! exists('g:markdown_no_default_key_mappings')
     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')