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 " Function interface phylosophy:
5 " - functions take arbitrary line numbers as parameters.
6 " Current cursor line is only a suitable default parameter.
8 " - only functions that bind directly to user actions:
10 " - print error messages.
11 " All intermediate functions limit themselves return `0` to indicate an error.
13 " - move the cursor. All other functions do not move the cursor.
15 " This is how you should view headers:
48 " For each level, contains the regexp that matches at that level only.
49 let s:levelRegexpDict = {
50 \ 1: '\v^(#[^#]|.+\n\=+$)',
51 \ 2: '\v^(##[^#]|.+\n-+$)',
58 " Maches any header level of any type.
60 " This could be deduced from `s:levelRegexpDict`, but it is more
61 " efficient to have a single regexp for this.
63 let s:headersRegexp = '\v^(#|.+\n(\=+|-+)$)'
65 " Returns the line number of the first header before `line`, called the
68 " If there is no current header, return `0`.
70 " @param a:1 The line to look the header of. Default value: `getpos('.')`.
72 function! s:Markdown_GetHeaderLineNum(...)
79 if join(getline(l:l, l:l + 1), "\n") =~ s:headersRegexp
87 " - if inside a header goes to it.
88 " Return its line number.
90 " - if on top level outside any headers,
94 function! s:Markdown_MoveToCurHeader()
95 let l:lineNum = s:Markdown_GetHeaderLineNum()
97 call cursor(l:lineNum, 1)
99 echo 'outside any header'
105 " Move cursor to next header of any level.
107 " If there are no more headers, print a warning.
109 function! s:Markdown_MoveToNextHeader()
110 if search(s:headersRegexp, 'W') == 0
112 echo 'no next header'
116 " Move cursor to previous header (before current) of any level.
118 " If it does not exist, print a warning.
120 function! s:Markdown_MoveToPreviousHeader()
121 let l:curHeaderLineNumber = s:Markdown_GetHeaderLineNum()
122 let l:noPreviousHeader = 0
123 if l:curHeaderLineNumber <= 1
124 let l:noPreviousHeader = 1
126 let l:previousHeaderLineNumber = s:Markdown_GetHeaderLineNum(l:curHeaderLineNumber - 1)
127 if l:previousHeaderLineNumber == 0
128 let l:noPreviousHeader = 1
130 call cursor(l:previousHeaderLineNumber, 1)
133 if l:noPreviousHeader
134 echo 'no previous header'
138 " - if line is inside a header, return the header level (h1 -> 1, h2 -> 2, etc.).
140 " - if line is at top level outside any headers, return `0`.
142 function! s:Markdown_GetHeaderLevel(...)
144 let l:line = line('.')
148 let l:linenum = s:Markdown_GetHeaderLineNum(l:line)
150 return s:Markdown_GetLevelOfHeaderAtLine(l:linenum)
156 " Returns the level of the header at the given line.
158 " If there is no header at the given line, returns `0`.
160 function! s:Markdown_GetLevelOfHeaderAtLine(linenum)
161 let l:lines = join(getline(a:linenum, a:linenum + 1), "\n")
162 for l:key in keys(s:levelRegexpDict)
163 if l:lines =~ get(s:levelRegexpDict, l:key)
170 " Move cursor to parent header of the current header.
172 " If it does not exit, print a warning and do nothing.
174 function! s:Markdown_MoveToParentHeader()
175 let l:linenum = s:Markdown_GetParentHeaderLineNumber()
177 call cursor(l:linenum, 1)
179 echo 'no parent header'
183 " Return the line number of the parent header of line `line`.
185 " If it has no parent, return `0`.
187 function! s:Markdown_GetParentHeaderLineNumber(...)
189 let l:line = line('.')
193 let l:level = s:Markdown_GetHeaderLevel(l:line)
195 let l:linenum = s:Markdown_GetPreviousHeaderLineNumberAtLevel(l:level - 1, l:line)
201 " Return the line number of the previous header of given level.
202 " in relation to line `a:1`. If not given, `a:1 = getline()`
204 " `a:1` line is included, and this may return the current header.
208 function! s:Markdown_GetNextHeaderLineNumberAtLevel(level, ...)
210 let l:line = line('.')
215 while(l:l <= line('$'))
216 if join(getline(l:l, l:l + 1), "\n") =~ get(s:levelRegexpDict, a:level)
224 " Return the line number of the previous header of given level.
225 " in relation to line `a:1`. If not given, `a:1 = getline()`
227 " `a:1` line is included, and this may return the current header.
231 function! s:Markdown_GetPreviousHeaderLineNumberAtLevel(level, ...)
233 let l:line = line('.')
239 if join(getline(l:l, l:l + 1), "\n") =~ get(s:levelRegexpDict, a:level)
247 " Move cursor to next sibling header.
249 " If there is no next siblings, print a warning and don't move.
251 function! s:Markdown_MoveToNextSiblingHeader()
252 let l:curHeaderLineNumber = s:Markdown_GetHeaderLineNum()
253 let l:curHeaderLevel = s:Markdown_GetLevelOfHeaderAtLine(l:curHeaderLineNumber)
254 let l:curHeaderParentLineNumber = s:Markdown_GetParentHeaderLineNumber()
255 let l:nextHeaderSameLevelLineNumber = s:Markdown_GetNextHeaderLineNumberAtLevel(l:curHeaderLevel, l:curHeaderLineNumber + 1)
256 let l:noNextSibling = 0
257 if l:nextHeaderSameLevelLineNumber == 0
258 let l:noNextSibling = 1
260 let l:nextHeaderSameLevelParentLineNumber = s:Markdown_GetParentHeaderLineNumber(l:nextHeaderSameLevelLineNumber)
261 if l:curHeaderParentLineNumber == l:nextHeaderSameLevelParentLineNumber
262 call cursor(l:nextHeaderSameLevelLineNumber, 1)
264 let l:noNextSibling = 1
268 echo 'no next sibling header'
272 " Move cursor to previous sibling header.
274 " If there is no previous siblings, print a warning and do nothing.
276 function! s:Markdown_MoveToPreviousSiblingHeader()
277 let l:curHeaderLineNumber = s:Markdown_GetHeaderLineNum()
278 let l:curHeaderLevel = s:Markdown_GetLevelOfHeaderAtLine(l:curHeaderLineNumber)
279 let l:curHeaderParentLineNumber = s:Markdown_GetParentHeaderLineNumber()
280 let l:previousHeaderSameLevelLineNumber = s:Markdown_GetPreviousHeaderLineNumberAtLevel(l:curHeaderLevel, l:curHeaderLineNumber - 1)
281 let l:noPreviousSibling = 0
282 if l:previousHeaderSameLevelLineNumber == 0
283 let l:noPreviousSibling = 1
285 let l:previousHeaderSameLevelParentLineNumber = s:Markdown_GetParentHeaderLineNumber(l:previousHeaderSameLevelLineNumber)
286 if l:curHeaderParentLineNumber == l:previousHeaderSameLevelParentLineNumber
287 call cursor(l:previousHeaderSameLevelLineNumber, 1)
289 let l:noPreviousSibling = 1
292 if l:noPreviousSibling
293 echo 'no previous sibling header'
297 function! s:Markdown_Toc(...)
299 let l:window_type = a:1
301 let l:window_type = 'vertical'
305 silent lvimgrep /\(^\S.*\(\n[=-]\+\n\)\@=\|^#\+\)/ %
307 echom "Toc: No headers."
311 if l:window_type ==# 'horizontal'
313 elseif l:window_type ==# 'vertical'
315 let &winwidth=(&columns/2)
316 elseif l:window_type ==# 'tab'
322 %s/\v^([^|]*\|){2,2} #//
323 for i in range(1, line('$'))
324 " this is the quickfix data for the current item
325 let d = getqflist()[i-1]
327 if match(d.text, "^#") > -1
328 let l:level = len(matchstr(d.text, '#*', 'g'))-1
329 let d.text = substitute(d.text, '\v^#*[ ]*', '', '')
330 let d.text = substitute(d.text, '\v[ ]*#*$', '', '')
333 let l:next_line = getbufline(bufname(d.bufnr), d.lnum+1)
334 if match(l:next_line, "=") > -1
336 elseif match(l:next_line, "-") > -1
340 call setline(i, repeat(' ', l:level). d.text)
347 " Wrapper to do move commands in visual mode.
349 function! s:VisMove(f)
354 " Map in both normal and visual modes.
356 function! s:MapNormVis(rhs,lhs)
357 execute 'nn <buffer><silent> ' . a:rhs . ' :call ' . a:lhs . '()<cr>'
358 execute 'vn <buffer><silent> ' . a:rhs . ' <esc>:call <sid>VisMove(''' . a:lhs . ''')<cr>'
361 " Convert Setex headers in range `line1 .. line2` to Atx.
362 " Returns the number of conversions.
363 function! s:SetexToAtx(line1, line2)
364 let l:originalNumLines = line('$')
365 execute 'silent! ' . a:line1 . ',' . a:line2 . 'substitute/\v(.*\S.*)\n\=+$/# \1/'
366 execute 'silent! ' . a:line1 . ',' . a:line2 . 'substitute/\v(.*\S.*)\n-+$/## \1/'
367 return l:originalNumLines - line('$')
370 " If `a:1` is 0, decrease the level of all headers in range `line1 .. line2`.
371 " Otherwise, increase the level. `a:1` defaults to `0`.
372 function! s:HeaderDecrease(line1, line2, ...)
379 let l:forbiddenLevel = 6
380 let l:replaceLevels = [5, 1]
383 let l:forbiddenLevel = 1
384 let l:replaceLevels = [2, 6]
385 let l:levelDelta = -1
387 for l:line in range(a:line1, a:line2)
388 if join(getline(l:line, l:line + 1), "\n") =~ s:levelRegexpDict[l:forbiddenLevel]
389 echomsg 'There is an h' . l:forbiddenLevel . ' at line ' . l:line . '. Aborting.'
393 let l:numSubstitutions = s:SetexToAtx(a:line1, a:line2)
394 for l:level in range(replaceLevels[0], replaceLevels[1], -l:levelDelta)
395 execute 'silent! ' . a:line1 . ',' . (a:line2 - l:numSubstitutions) . 'substitute/' . s:levelRegexpDict[l:level] . '/' . repeat('#', l:level + l:levelDelta) . '\1/g'
399 " Format table under cursor.
400 " Depends on Tabularize.
401 function! s:TableFormat()
402 let l:pos = getpos('.')
404 " Search instead of `normal! j` because of the table at beginning of file edge case.
407 " Remove everything that is not a pipe othewise well formated tables would grow
408 " because of addition of 2 spaces on the separator line by Tabularize /|.
412 call setpos('.', l:pos)
415 call <sid>MapNormVis('<Plug>(Markdown_MoveToNextHeader)', '<sid>Markdown_MoveToNextHeader')
416 call <sid>MapNormVis('<Plug>(Markdown_MoveToPreviousHeader)', '<sid>Markdown_MoveToPreviousHeader')
417 call <sid>MapNormVis('<Plug>(Markdown_MoveToNextSiblingHeader)', '<sid>Markdown_MoveToNextSiblingHeader')
418 call <sid>MapNormVis('<Plug>(Markdown_MoveToPreviousSiblingHeader)', '<sid>Markdown_MoveToPreviousSiblingHeader')
420 call <sid>MapNormVis('<Plug>(Markdown_MoveToParentHeader)', '<sid>Markdown_MoveToParentHeader')
422 call <sid>MapNormVis('<Plug>(Markdown_MoveToCurHeader)', '<sid>Markdown_MoveToCurHeader')
424 if !get(g:, 'vim_markdown_no_default_key_mappings', 0)
425 nmap <buffer> ]] <Plug>(Markdown_MoveToNextHeader)
426 nmap <buffer> [[ <Plug>(Markdown_MoveToPreviousHeader)
427 nmap <buffer> ][ <Plug>(Markdown_MoveToNextSiblingHeader)
428 nmap <buffer> [] <Plug>(Markdown_MoveToPreviousSiblingHeader)
429 nmap <buffer> ]u <Plug>(Markdown_MoveToParentHeader)
430 nmap <buffer> ]c <Plug>(Markdown_MoveToCurHeader)
432 vmap <buffer> ]] <Plug>(Markdown_MoveToNextHeader)
433 vmap <buffer> [[ <Plug>(Markdown_MoveToPreviousHeader)
434 vmap <buffer> ][ <Plug>(Markdown_MoveToNextSiblingHeader)
435 vmap <buffer> [] <Plug>(Markdown_MoveToPreviousSiblingHeader)
436 vmap <buffer> ]u <Plug>(Markdown_MoveToParentHeader)
437 vmap <buffer> ]c <Plug>(Markdown_MoveToCurHeader)
440 command! -buffer -range=% HeaderDecrease call s:HeaderDecrease(<line1>, <line2>)
441 command! -buffer -range=% HeaderIncrease call s:HeaderDecrease(<line1>, <line2>, 1)
442 command! -buffer -range=% SetexToAtx call s:SetexToAtx(<line1>, <line2>)
443 command! -buffer TableFormat call s:TableFormat()
444 command! -buffer Toc call s:Markdown_Toc()
445 command! -buffer Toch call s:Markdown_Toc('horizontal')
446 command! -buffer Tocv call s:Markdown_Toc('vertical')
447 command! -buffer Toct call s:Markdown_Toc('tab')