From: Ciro Santilli Date: Mon, 7 Apr 2014 05:57:25 +0000 (+0200) Subject: Merge pull request #69 from cirosantilli/use-markdown-testsuite X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/290d61b8387ae5bb6b471b643127aa568e55a77e?hp=1bd9d546835633ca39da8f8e2bac5be40450f942 Merge pull request #69 from cirosantilli/use-markdown-testsuite Use https://github.com/karlcow/markdown-testsuite for the tests wherever possible. --- diff --git a/README.md b/README.md index 9478b5a..b004bbc 100644 --- a/README.md +++ b/README.md @@ -48,16 +48,36 @@ Add the following line to your `.vimrc` to set the initial foldlevel. This optio let g:vim_markdown_initial_foldlevel=1 ``` +**Disable Default Key Mappings** + +Add the following line to your `.vimrc` to disable default key mappings. You can map them by yourself with `` mappings + +```vim +let g:markdown_no_default_key_mappings=1 +``` + ## Mappings The following work on normal and visual modes: -- `]]`: go to next header. -- `[[`: go to previous header. Contrast with `]c`. -- `][`: go to next sibling header if any. -- `[]`: go to previous sibling header if any. -- `]c`: go to Current header. -- `]u`: go to parent header (Up). +- `]]`: go to next header. `(Markdown_MoveToNextHeader)` +- `[[`: go to previous header. Contrast with `]c`. `(Markdown_MoveToPreviousHeader)` +- `][`: go to next sibling header if any. `(Markdown_MoveToNextSiblingHeader)` +- `[]`: go to previous sibling header if any. `(Markdown_MoveToPreviousSiblingHeader)` +- `]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 diff --git a/ftplugin/mkd.vim b/ftplugin/mkd.vim index 4e9f448..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) @@ -308,11 +341,34 @@ function! s:MapNormVis(rhs,lhs) execute 'vn ' . a:rhs . ' :call VisMove(''' . a:lhs . ''')' endfunction -call MapNormVis(']]', 'b:Markdown_MoveToNextHeader') -call MapNormVis('[[', 'b:Markdown_MoveToPreviousHeader') -call MapNormVis('][', 'b:Markdown_MoveToNextSiblingHeader') -call MapNormVis('[]', 'b:Markdown_MoveToPreviousSiblingHeader') + +call MapNormVis('(Markdown_MoveToNextHeader)', 'b:Markdown_MoveToNextHeader') +call MapNormVis('(Markdown_MoveToPreviousHeader)', 'b:Markdown_MoveToPreviousHeader') +call MapNormVis('(Markdown_MoveToNextSiblingHeader)', 'b:Markdown_MoveToNextSiblingHeader') +call MapNormVis('(Markdown_MoveToPreviousSiblingHeader)', 'b:Markdown_MoveToPreviousSiblingHeader') " Menmonic: Up -call MapNormVis(']u', 'b:Markdown_MoveToParentHeader') +call MapNormVis('(Markdown_MoveToParentHeader)', 'b:Markdown_MoveToParentHeader') " Menmonic: Current -call MapNormVis(']c', 'b:Markdown_MoveToCurHeader') +call MapNormVis('(Markdown_MoveToCurHeader)', 'b:Markdown_MoveToCurHeader') + +if ! exists('g:markdown_no_default_key_mappings') +\ || !g:markdown_no_default_key_mappings + nmap ]] (Markdown_MoveToNextHeader) + nmap [[ (Markdown_MoveToPreviousHeader) + nmap ][ (Markdown_MoveToNextSiblingHeader) + nmap [] (Markdown_MoveToPreviousSiblingHeader) + nmap ]u (Markdown_MoveToParentHeader) + nmap ]c (Markdown_MoveToCurHeader) + + vmap ]] (Markdown_MoveToNextHeader) + vmap [[ (Markdown_MoveToPreviousHeader) + vmap ][ (Markdown_MoveToNextSiblingHeader) + vmap [] (Markdown_MoveToPreviousSiblingHeader) + 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