From: Hiroshi Shirosaki Date: Thu, 3 Apr 2014 04:30:06 +0000 (+0900) Subject: Merge branch 'mapping' X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/893e8d81388f1c27368e963b61d34285d05f5a4b?hp=52ae39c5624e40bf7875390631e8cbdf12099837 Merge branch 'mapping' Conflicts: ftplugin/mkd.vim --- diff --git a/README.md b/README.md index cc1f7ef..b004bbc 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,18 @@ The following work on normal and visual modes: - `]c`: go to Current header. `(Markdown_MoveToCurHeader)` - `]u`: go to parent header (Up). `(Markdown_MoveToParentHeader)` +## Commands + +The following commands currently only work for atx style headers (`#`). Pull request are welcome to extend them to Setext style headers (`===`). + +- `:Toc`: create a quickfix vertical window navigable table of contents with the headers. + + Hit `` on a line to jump to the corresponding line of the markdown file. + +- `:Toch`: Same as `:Toc` but in an horizontal window. +- `:Toct`: Same as `:Toc` but in a new tab. +- `:Tocv`: Same as `:Toc` for symmetry with `:Toch` and `Tocv`. + ## Credits The main contributors of vim-markdown are: diff --git a/ftplugin/mkd.vim b/ftplugin/mkd.vim index 0627cc5..c7bac4e 100644 --- a/ftplugin/mkd.vim +++ b/ftplugin/mkd.vim @@ -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) @@ -334,3 +367,8 @@ if ! exists('g:markdown_no_default_key_mappings') vmap ]u (Markdown_MoveToParentHeader) vmap ]c (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') diff --git a/test/multiple_headers.md b/test/multiple_headers.md new file mode 100644 index 0000000..b6a403a --- /dev/null +++ b/test/multiple_headers.md @@ -0,0 +1,27 @@ +This file is used for tests which require there to be multiple headers in different relative positions to each other. + +Each header should have an unique text that identifies it. + +# h1 space + +#h1 nospace + +# h1 trailing hash # + +## h2 space + +##h2 nospace + +## h2 trailing hash ## + +### h3 space + +###h3 nospace + +### h3 trailing hash ### + +# h1 before h2 + +## h2 between h1s + +# h1 after h2