From: Ben Williams Date: Tue, 1 Oct 2013 13:30:17 +0000 (-0400) Subject: Merge branch 'list_items' of https://github.com/shirosaki/vim-markdown into shirosaki... X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/bb1bd0a2f2a0634c8635ee146ad378bc68364191?hp=276518b9371261efbbdb2f370f33f3cb8e8371d2 Merge branch 'list_items' of https://github.com/shirosaki/vim-markdown into shirosaki-list_items --- diff --git a/README.md b/README.md index 2d7e587..175d3a4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Markdown Vim Mode -Syntax highlighting and matching rules for [Markdown](http://daringfireball.net/projects/markdown/). +Syntax highlighting, matching rules and mappings for [Markdown](http://daringfireball.net/projects/markdown/). ## Installation @@ -26,6 +26,27 @@ Add the following line to your `.vimrc` to disable folding. let g:vim_markdown_folding_disabled=1 ``` +**Set Initial Foldlevel** + +Add the following line to your `.vimrc` to set the initial foldlevel. This +option defaults to 0 (i.e. all folds are closed) and is ignored if folding +is disabled. + +```vim +let g:vim_markdown_initial_foldlevel=1 +``` + +## Mappings + +the following work on normal and visual modes: + +- `]]`: go to next header +- `[[`: go to previous header +- `][`: 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) + ## License The MIT License (MIT) diff --git a/after/ftplugin/mkd.vim b/after/ftplugin/mkd.vim index ed78895..26ccb23 100644 --- a/after/ftplugin/mkd.vim +++ b/after/ftplugin/mkd.vim @@ -40,11 +40,16 @@ if !exists("g:vim_markdown_folding_disabled") setlocal foldexpr=Foldexpr_markdown(v:lnum) setlocal foldmethod=expr + " allow the initial foldlevel to be configured in .vimrc + if !exists("g:vim_markdown_initial_foldlevel") + let g:vim_markdown_initial_foldlevel=0 + endif + let &l:foldlevel=g:vim_markdown_initial_foldlevel + "---------- everything after this is optional ----------------------- " change the following fold options to your liking " see ':help fold-options' for more setlocal foldenable - setlocal foldlevel=0 setlocal foldcolumn=0 set foldmethod=expr set foldopen-=search diff --git a/ftplugin/mkd.vim b/ftplugin/mkd.vim new file mode 100644 index 0000000..c859565 --- /dev/null +++ b/ftplugin/mkd.vim @@ -0,0 +1,208 @@ +"TODO print messages when on visual mode. I only see VISUAL, not the messages. + +"this is how you should view things: +" +" |BUFFER +" | +" |outside any header +" | +"a-+# a +" | +" |inside a +" | +"a-+ +"b-+## b +" | +" |inside b +" | +"b-+ +"c-+### c +" | +" |inside c +" | +"c-+ +"d-|# d +" | +" |inside d +" | +"d-+ + +let s:headerExpr = '\v^#' + +"0 if not found +fu! b:Markdown_GetLineNumCurHeader() + retu search( s:headerExpr, 'bcnW' ) +endf + +"- if inside a header goes to it +" returns its hashes +"- if on top level outside any headers, +" print a warning +" return '' +fu! b:Markdown_GoCurHeaderGetHashes() + let l:lineNum = b:Markdown_GetLineNumCurHeader() + if l:lineNum != 0 + cal cursor( l:lineNum, 1 ) + retu matchstr( getline( lineNum ), '\v^#+' ) + el + retu '' + en +endf + +"- if inside a header goes to it +" returns its line number +"- if on top level outside any headers, +" print a warning +" return 0 +fu! b:Markdown_GoCurHeader() + let l:lineNum = b:Markdown_GetLineNumCurHeader() + if l:lineNum != 0 + cal cursor( l:lineNum, 1 ) + el + ec 'outside any header' + "norm! gg + en + retu l:lineNum +endf + +"goes to next header of any level +" +"if no there are no more headers print a warning +fu! b:Markdown_GoNextHeader() + if search( s:headerExpr, 'W' ) == 0 + "norm! G + ec 'no next header' + en +endf + +"goes to previous header of any level +" +"if it does not exist, print a warning +fu! b:Markdown_GoPreviousHeader() + let l:oldPos = getpos('.') + let l:curHeaderLineNumber = b:Markdown_GoCurHeader() + if l:curHeaderLineNumber == 0 + cal setpos('.',l:oldPos) + en + if search( s:headerExpr, 'bW' ) == 0 + "norm! gg + cal setpos('.',l:oldPos) + ec 'no previous header' + en +endf + +"goes to previous header of any level +" +"if it exists, return its lines number +" +"otherwise, print a warning and return 0 +fu! b:Markdown_GoHeaderUp() + let l:oldPos = getpos('.') + let l:hashes = b:Markdown_GoCurHeaderGetHashes() + if len( l:hashes ) > 1 + cal search( '^' . l:hashes[1:] . '[^#]', 'b' ) + el + cal setpos('.',l:oldPos) + ec 'already at top level' + en +endf + +"if no more next siblings, print error message and do nothing. +fu! b:Markdown_GoNextSiblingHeader() + let l:oldPos = getpos('.') + let l:hashes = b:Markdown_GoCurHeaderGetHashes() + let l:noSibling = 0 + + if l:hashes ==# '' + let l:noSibling = 1 + el + let l:nhashes = len(l:hashes) + if l:nhashes == 1 + "special case, just add the largest possible value + let l:nextLowerLevelLine = line('$') + 1 + el + let l:nextLowerLevelLine = search( '\v^#{1,' . ( l:nhashes - 1 ) . '}[^#]' , 'nW' ) + en + + let l:nextSameLevelLine = search( '\v^' . l:hashes . '[^#]', 'nW' ) + if ( + \ l:nextSameLevelLine > 0 + \ && + \ ( + \ l:nextLowerLevelLine == 0 + \ || + \ l:nextLowerLevelLine > l:nextSameLevelLine + \ ) + \ ) + cal cursor( l:nextSameLevelLine, 1 ) + el + let l:noSibling = 1 + en + en + + if l:noSibling + cal setpos('.',l:oldPos) + ec 'no next sibling' + en +endf + +"if no more next siblings, print error message and do nothing. +fu! b:Markdown_GoPreviousSiblingHeader() + let l:oldPos = getpos('.') + let l:hashes = b:Markdown_GoCurHeaderGetHashes() + let l:noSibling = 0 + + if l:hashes ==# '' + let l:noSibling = 1 + el + let l:nhashes = len(l:hashes) + if l:nhashes == 1 + "special case, just add the largest possible value + let l:prevLowerLevelLine = -1 + el + let l:prevLowerLevelLine = search( '\v^#{1,' . ( l:nhashes - 1 ) . '}[^#]' , 'bnW' ) + en + + let l:prevSameLevelLine = search( '\v^' . l:hashes . '[^#]', 'bnW' ) + if ( + \ l:prevSameLevelLine > 0 + \ && + \ ( + \ l:prevLowerLevelLine == 0 + \ || + \ l:prevLowerLevelLine < l:prevSameLevelLine + \ ) + \ ) + cal cursor( l:prevSameLevelLine, 1 ) + el + let l:noSibling = 1 + en + en + + if l:noSibling + cal setpos('.',l:oldPos) + ec 'no previous sibling' + en + +endf + +"wrapper to do move commands in visual mode +fu! s:VisMove(f) + norm! gv + cal function(a:f)() +endf + +"map in both normal and visual modes +fu! s:MapNormVis(rhs,lhs) + exe 'nn ' . a:rhs . ' :cal ' . a:lhs . '()' + exe 'vn ' . a:rhs . ' :cal VisMove(''' . a:lhs . ''')' +endf + +cal MapNormVis( ']]', 'b:Markdown_GoNextHeader' ) +cal MapNormVis( '[[', 'b:Markdown_GoPreviousHeader' ) +cal MapNormVis( '][', 'b:Markdown_GoNextSiblingHeader' ) +cal MapNormVis( '[]', 'b:Markdown_GoPreviousSiblingHeader' ) +"menmonic: Up +cal MapNormVis( ']u', 'b:Markdown_GoHeaderUp' ) +"menmonic: Current +cal MapNormVis( ']c', 'b:Markdown_GoCurHeader' ) diff --git a/syntax/mkd.vim b/syntax/mkd.vim index 8ace27c..575466b 100644 --- a/syntax/mkd.vim +++ b/syntax/mkd.vim @@ -114,6 +114,11 @@ HtmlHiLink mkdLinkTitle htmlString HtmlHiLink mkdDelimiter Delimiter +" Automatically insert bullets +setlocal formatoptions+=r +" Accept various markers as bullets +setlocal comments=b:*,b:+,b:- + let b:current_syntax = "mkd" delcommand HtmlHiLink