-function! b:Markdown_GoPreviousHeader()
- let l:oldPos = getpos('.')
- let l:curHeaderLineNumber = b:Markdown_GoCurHeader()
- if l:curHeaderLineNumber == 0
- call setpos('.', l:oldPos)
- end
- if search(s:headerExpr, 'bW') == 0
- "normal! gg
- call setpos('.', l:oldPos)
- echo 'error: no previous header'
- end
+function! b:Markdown_MoveToPreviousHeader()
+ let l:curHeaderLineNumber = b:Markdown_GetHeaderLineNum()
+ let l:noPreviousHeader = 0
+ if l:curHeaderLineNumber <= 1
+ let l:noPreviousHeader = 1
+ else
+ let l:previousHeaderLineNumber = b:Markdown_GetHeaderLineNum(l:curHeaderLineNumber - 1)
+ if l:previousHeaderLineNumber == 0
+ let l:noPreviousHeader = 1
+ else
+ call cursor(l:previousHeaderLineNumber, 1)
+ endif
+ endif
+ if l:noPreviousHeader
+ echo 'no previous header'
+ endif
+endfunction
+
+" - if line is inside a header, return the header level (h1 -> 1, h2 -> 2, etc.).
+"
+" - if line is at top level outside any headers, return `0`.
+"
+function! b:Markdown_GetHeaderLevel(...)
+ if a:0 == 0
+ let l:line = line('.')
+ else
+ let l:line = a:1
+ endif
+ let l:linenum = b:Markdown_GetHeaderLineNum(l:line)
+ if l:linenum != 0
+ return b:Markdown_GetLevelOfHeaderAtLine(l:linenum)
+ else
+ return 0
+ endif
+endfunction
+
+" Returns the level of the header at the given line.
+"
+" If there is no header at the given line, returns `0`.
+"
+function! b:Markdown_GetLevelOfHeaderAtLine(linenum)
+ let l:lines = join(getline(a:linenum, a:linenum + 1), "\n")
+ for l:key in keys(s:levelRegexpDict)
+ if l:lines =~ get(s:levelRegexpDict, l:key)
+ return l:key
+ endif
+ endfor
+ return 0