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.
1 "TODO print messages when on visual mode. I only see VISUAL, not the messages.
3 " This is how you should view things:
30 let s:headerExpr = '\v^(\s*#|.+\n(\=+|-+)$)'
32 " Return 0 if not found, else the actual line number.
34 function! b:Markdown_GetLineNumCurHeader()
36 return search(s:headerExpr, 'bcnW')
39 " - if inside a header goes to it.
40 " Return its line number.
42 " - if on top level outside any headers,
46 function! b:Markdown_GoCurHeader()
47 let l:lineNum = b:Markdown_GetLineNumCurHeader()
49 call cursor(l:lineNum, 1)
51 echo 'error: outside any header'
57 " Put cursor on next header of any level.
59 " If there are no more headers, print a warning.
61 function! b:Markdown_GoNextHeader()
62 if search(s:headerExpr, 'W') == 0
64 echo 'error: no next header'
68 " Put cursor on previous header (before current) of any level.
70 " If it does not exist, print a warning.
72 function! b:Markdown_GoPreviousHeader()
73 let l:oldPos = getpos('.')
74 let l:curHeaderLineNumber = b:Markdown_GoCurHeader()
75 if l:curHeaderLineNumber == 0
76 call setpos('.', l:oldPos)
78 if search(s:headerExpr, 'bW') == 0
80 call setpos('.', l:oldPos)
81 echo 'error: no previous header'
85 "- if inside a header, cursor goes to it.
88 "- if on top level outside any headers,
92 function! b:Markdown_GoCurHeaderGetHashes()
93 let l:linenum = b:Markdown_GetLineNumCurHeader()
95 call cursor(l:linenum, 1)
96 return matchlist(getline(linenum), '\v^\s*(#+)')[1]
102 " Put cursor on previous header of any level.
104 " If it exists, return its lines number.
106 " Otherwise, print a warning and return `0`.
108 function! b:Markdown_GoHeaderUp()
109 let l:oldPos = getpos('.')
110 let l:hashes = b:Markdown_GoCurHeaderGetHashes()
112 call search('^\s*' . l:hashes[1:] . '[^#]', 'b')
114 call setpos('.', l:oldPos)
115 echo 'error: already at top level'
119 " If no more next siblings, print error message and do nothing.
121 function! b:Markdown_GoNextSiblingHeader()
122 let l:oldPos = getpos('.')
123 let l:hashes = b:Markdown_GoCurHeaderGetHashes()
128 let l:nhashes = len(l:hashes)
130 "special case, just add the largest possible value
131 let l:nextLowerLevelLine = line('$') + 1
133 let l:nextLowerLevelLine = search('\v^\s*#{1,' . (l:nhashes - 1) . '}[^#]' , 'nW')
135 let l:nextSameLevelLine = search('\v^\s*' . l:hashes . '[^#]', 'nW')
137 \ l:nextSameLevelLine > 0
140 \ l:nextLowerLevelLine == 0
142 \ l:nextLowerLevelLine > l:nextSameLevelLine
145 call cursor(l:nextSameLevelLine, 1)
151 call setpos('.', l:oldPos)
152 echo 'error: no next sibling'
156 "if no more next siblings, print error message and do nothing.
157 function! b:Markdown_GoPreviousSiblingHeader()
158 let l:oldPos = getpos('.')
159 let l:hashes = b:Markdown_GoCurHeaderGetHashes()
164 let l:nhashes = len(l:hashes)
166 "special case, just add the largest possible value
167 let l:prevLowerLevelLine = -1
169 let l:prevLowerLevelLine = search('\v^\s*#{1,' . (l:nhashes - 1) . '}[^#]' , 'bnW')
171 let l:prevSameLevelLine = search('\v^\s*' . l:hashes . '[^#]', 'bnW')
173 \ l:prevSameLevelLine > 0
176 \ l:prevLowerLevelLine == 0
178 \ l:prevLowerLevelLine < l:prevSameLevelLine
181 call cursor(l:prevSameLevelLine, 1)
187 call setpos('.', l:oldPos)
188 echo 'error: no previous sibling'
192 "wrapper to do move commands in visual mode
193 function! s:VisMove(f)
198 "map in both normal and visual modes
199 function! s:MapNormVis(rhs,lhs)
200 execute 'nn <buffer><silent> ' . a:rhs . ' :call ' . a:lhs . '()<cr>'
201 execute 'vn <buffer><silent> ' . a:rhs . ' <esc>:call <sid>VisMove(''' . a:lhs . ''')<cr>'
204 call <sid>MapNormVis(']]', 'b:Markdown_GoNextHeader')
205 call <sid>MapNormVis('[[', 'b:Markdown_GoPreviousHeader')
206 call <sid>MapNormVis('][', 'b:Markdown_GoNextSiblingHeader')
207 call <sid>MapNormVis('[]', 'b:Markdown_GoPreviousSiblingHeader')
209 call <sid>MapNormVis(']u', 'b:Markdown_GoHeaderUp')
211 call <sid>MapNormVis(']c', 'b:Markdown_GoCurHeader')