- retu
- en
-
- 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, 0 )
- el
- ec 'no more siblings'
- en
-
-endf
-
-"mnemonics: ']' next (like a right arrow)
-nn <buffer><silent> ]] :cal b:Markdown_GoNextHeader()<cr>
-"vnoremap <buffer><silent> ]] /^#<cr><esc>:nohl<cr>gv
-
-"mnemonics: '[' next (like a left arrow)
-nn <buffer><silent> ][ :cal b:Markdown_GoNextSiblingHeader()<cr>
-"vnoremap <buffer><silent> ][ <esc>:cal b:Markdown_GoNextHeaderSameLevel()<cr>
-
-nn <buffer><silent> [] :cal b:Markdown_GoPreviousSiblingHeader()<cr>
-
-nn <buffer><silent> [[ :cal b:Markdown_GoPreviousHeader()<cr>
-"vnoremap <buffer><silent> [[ ?^#<cr><esc>:nohl<cr>gv
-
-"go up one level. Menmonic: Up.
-nn <buffer><silent> ]u :cal b:Markdown_GoHeaderUp()<cr>
+ let l:noSibling = 1
+ else
+ let l:nhashes = len(l:hashes)
+ if l:nhashes == 1
+ "special case, just add the largest possible value
+ let l:prevLowerLevelLine = -1
+ else
+ let l:prevLowerLevelLine = search('\v^\s*#{1,' . (l:nhashes - 1) . '}[^#]' , 'bnW')
+ end
+ let l:prevSameLevelLine = search('\v^\s*' . l:hashes . '[^#]', 'bnW')
+ if (
+ \ l:prevSameLevelLine > 0
+ \ &&
+ \ (
+ \ l:prevLowerLevelLine == 0
+ \ ||
+ \ l:prevLowerLevelLine < l:prevSameLevelLine
+ \ )
+ \)
+ call cursor(l:prevSameLevelLine, 1)
+ else
+ let l:noSibling = 1
+ end
+ end
+ if l:noSibling
+ call setpos('.', l:oldPos)
+ echo 'error: no previous sibling'
+ end
+endfunction
+
+"wrapper to do move commands in visual mode
+function! s:VisMove(f)
+ norm! gv
+ call function(a:f)()
+endfunction
+
+"map in both normal and visual modes
+function! s:MapNormVis(rhs,lhs)
+ execute 'nn <buffer><silent> ' . a:rhs . ' :call ' . a:lhs . '()<cr>'
+ execute 'vn <buffer><silent> ' . a:rhs . ' <esc>:call <sid>VisMove(''' . a:lhs . ''')<cr>'
+endfunction
+
+call <sid>MapNormVis(']]', 'b:Markdown_GoNextHeader')
+call <sid>MapNormVis('[[', 'b:Markdown_GoPreviousHeader')
+call <sid>MapNormVis('][', 'b:Markdown_GoNextSiblingHeader')
+call <sid>MapNormVis('[]', 'b:Markdown_GoPreviousSiblingHeader')
+"menmonic: Up
+call <sid>MapNormVis(']u', 'b:Markdown_GoHeaderUp')
+"menmonic: Current
+call <sid>MapNormVis(']c', 'b:Markdown_GoCurHeader')