sections.vim 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. " vim: set fdm=marker et ts=4 sw=4 sts=4:
  2. function! pandoc#keyboard#sections#Init() abort "{{{1
  3. " Defaults: {{{2
  4. " What style to use when applying header styles {{{3
  5. " a: atx headers
  6. " s: setex headers for 1st and 2nd levels
  7. " 2: add hashes at both ends
  8. if !exists('g:pandoc#keyboard#sections#header_style')
  9. let g:pandoc#keyboard#sections#header_style = 'a'
  10. endif
  11. " }}}2
  12. noremap <buffer> <silent> <Plug>(pandoc-keyboard-apply-header) :<C-U>call pandoc#keyboard#sections#ApplyHeader(v:count1)<cr>
  13. noremap <buffer> <silent> <Plug>(pandoc-keyboard-remove-header) :call pandoc#keyboard#sections#RemoveHeader()<cr>
  14. noremap <buffer> <silent> <Plug>(pandoc-keyboard-next-header) :call pandoc#keyboard#sections#NextHeader()<cr>
  15. noremap <buffer> <silent> <Plug>(pandoc-keyboard-prev-header) :call pandoc#keyboard#sections#PrevHeader()<cr>
  16. noremap <buffer> <silent> <Plug>(pandoc-keyboard-ff-header) :<C-U>call pandoc#keyboard#sections#ForwardHeader(v:count1)<cr>
  17. noremap <buffer> <silent> <Plug>(pandoc-keyboard-rw-header) :<C-U>call pandoc#keyboard#sections#BackwardHeader(v:count1)<cr>
  18. noremap <buffer> <silent> <Plug>(pandoc-keyboard-ff-sect-end) :<C-U>call pandoc#keyboard#sections#NextSectionEnd(v:count1)<cr>
  19. noremap <buffer> <silent> <Plug>(pandoc-keyboard-rw-sect-end) :<C-U>call pandoc#keyboard#sections#PrevSectionEnd(v:count1)<cr>
  20. noremap <buffer> <silent> <Plug>(pandoc-keyboard-cur-header) :call pandoc#keyboard#sections#CurrentHeader()<cr>
  21. noremap <buffer> <silent> <Plug>(pandoc-keyboard-cur-header-parent) :call pandoc#keyboard#sections#CurrentHeaderParent()<cr>
  22. vnoremap <buffer> <silent> <Plug>(pandoc-keyboard-select-section-inclusive) :<C-U>call pandoc#keyboard#sections#SelectSection('inclusive')<cr>
  23. vnoremap <buffer> <silent> <Plug>(pandoc-keyboard-select-section-exclusive) :<C-U>call pandoc#keyboard#sections#SelectSection('exclusive')<cr>
  24. noremap <buffer> <silent> <Plug>(pandoc-keyboard-next-header-sibling) :call pandoc#keyboard#sections#NextSiblingHeader()<cr>
  25. noremap <buffer> <silent> <Plug>(pandoc-keyboard-prev-header-sibling) :call pandoc#keyboard#sections#PrevSiblingHeader()<cr>
  26. noremap <buffer> <silent> <Plug>(pandoc-keyboard-first-header-child) :call pandoc#keyboard#sections#FirstChildHeader()<cr>
  27. noremap <buffer> <silent> <Plug>(pandoc-keyboard-last-header-child) :call pandoc#keyboard#sections#LastChildHeader()<cr>
  28. noremap <buffer> <silent> <Plug>(pandoc-keyboard-nth-header-child) :<C-U>call pandoc#keyboard#sections#GotoNthChildHeader(v:count1)<cr>
  29. if g:pandoc#keyboard#use_default_mappings == 1 && index(g:pandoc#keyboard#blacklist_submodule_mappings, 'sections') == -1
  30. nmap <buffer> <localleader># <Plug>(pandoc-keyboard-apply-header)
  31. nmap <buffer> <localleader>hd <Plug>(pandoc-keyboard-remove-header)
  32. nmap <buffer> <localleader>hn <Plug>(pandoc-keyboard-next-header)
  33. nmap <buffer> <localleader>hb <Plug>(pandoc-keyboard-prev-header)
  34. nmap <buffer> <localleader>hh <Plug>(pandoc-keyboard-cur-header)
  35. nmap <buffer> <localleader>hp <Plug>(pandoc-keyboard-cur-header-parent)
  36. nmap <buffer> <localleader>hsn <Plug>(pandoc-keyboard-next-header-sibling)
  37. nmap <buffer> <localleader>hsb <Plug>(pandoc-keyboard-prev-header-sibling)
  38. nmap <buffer> <localleader>hcf <Plug>(pandoc-keyboard-first-header-child)
  39. nmap <buffer> <localleader>hcl <Plug>(pandoc-keyboard-last-header-child)
  40. nmap <buffer> <localleader>hcn <Plug>(pandoc-keyboard-nth-header-child)
  41. nmap <buffer> ]] <Plug>(pandoc-keyboard-ff-header)
  42. nmap <buffer> [[ <Plug>(pandoc-keyboard-rw-header)
  43. nmap <buffer> ][ <Plug>(pandoc-keyboard-ff-sect-end)
  44. nmap <buffer> [] <Plug>(pandoc-keyboard-rw-sect-end)
  45. vmap <buffer> aS <Plug>(pandoc-keyboard-select-section-inclusive)
  46. omap <buffer> aS :normal VaS<cr>
  47. vmap <buffer> iS <Plug>(pandoc-keyboard-select-section-exclusive)
  48. omap <buffer> iS :normal ViS<cr>
  49. endif
  50. endfunction
  51. " Functions: {{{1
  52. " Handling: {{{2
  53. function! pandoc#keyboard#sections#ApplyHeader(level) abort "{{{3
  54. call pandoc#keyboard#sections#RemoveHeader()
  55. if a:level == 0
  56. return
  57. endif
  58. let line_text = getline('.')
  59. if a:level < 3 && (g:pandoc#keyboard#sections#header_style =~# 's') == 1
  60. let text = line_text
  61. else
  62. if (g:pandoc#keyboard#sections#header_style =~# '2') == 1
  63. let tail = ' ' . repeat('#', a:level)
  64. else
  65. let tail = ''
  66. endif
  67. let text = repeat('#', a:level) . ' ' . line_text . tail
  68. endif
  69. call setline(line('.'), text)
  70. if (g:pandoc#keyboard#sections#header_style =~# 's') == 1
  71. let l:len = strlen(substitute(text, '.', 'x', 'g'))
  72. if a:level == 1
  73. call append(line('.'), repeat('=', l:len))
  74. elseif a:level == 2
  75. call append(line('.'), repeat('-', l:len))
  76. endif
  77. endif
  78. endfunction
  79. function! pandoc#keyboard#sections#RemoveHeader() abort "{{{3
  80. let lnum = line('.')
  81. let line_text = getline('.')
  82. if match(line_text, '^#') > -1
  83. let line_text = substitute(line_text, '^#* *', '', '')
  84. if match(line_text, ' #*$') > -1
  85. let line_text = substitute(line_text, ' #*$', '', '')
  86. endif
  87. elseif match(getline(line('.')+1), '^[-=]') > -1
  88. exe line('.')+1.'delete "_'
  89. endif
  90. exe lnum
  91. call setline(line('.'), line_text)
  92. endfunction
  93. " }}}2
  94. " Navigation: {{{2
  95. function! pandoc#keyboard#sections#NextHeader() abort "{{{3
  96. call pandoc#keyboard#MovetoLine(markdown#headers#NextHeader())
  97. endfunction
  98. function! pandoc#keyboard#sections#PrevHeader() abort "{{{3
  99. call pandoc#keyboard#MovetoLine(markdown#headers#PrevHeader())
  100. endfunction
  101. function! pandoc#keyboard#sections#ForwardHeader(count) abort "{{{3
  102. call pandoc#keyboard#MovetoLine(markdown#headers#ForwardHeader(a:count))
  103. endfunction
  104. function! pandoc#keyboard#sections#BackwardHeader(count) abort "{{{3
  105. call pandoc#keyboard#MovetoLine(markdown#headers#BackwardHeader(a:count))
  106. endfunction
  107. function! pandoc#keyboard#sections#NextSectionEnd(count) abort "{{{3
  108. let lnum = line('.')
  109. for i in range(a:count)
  110. let lnum = markdown#sections#NextEndSection(0, lnum)
  111. endfor
  112. call pandoc#keyboard#MovetoLine(lnum)
  113. endfunction
  114. function! pandoc#keyboard#sections#PrevSectionEnd(count) abort "{{{3
  115. let lnum = line('.')
  116. for i in range(a:count)
  117. let lnum = markdown#sections#PrevEndSection(lnum)
  118. endfor
  119. call pandoc#keyboard#MovetoLine(lnum)
  120. endfunction
  121. function! pandoc#keyboard#sections#CurrentHeader() abort "{{{3
  122. call pandoc#keyboard#MovetoLine(markdown#headers#CurrentHeader())
  123. endfunction
  124. function! pandoc#keyboard#sections#CurrentHeaderParent() abort "{{{3
  125. call pandoc#keyboard#MovetoLine(markdown#headers#CurrentHeaderParent())
  126. endfunction
  127. function! pandoc#keyboard#sections#NextSiblingHeader() abort "{{{3
  128. call pandoc#keyboard#MovetoLine(markdown#headers#NextSiblingHeader())
  129. endfunction
  130. function! pandoc#keyboard#sections#PrevSiblingHeader() abort "{{{3
  131. call pandoc#keyboard#MovetoLine(markdown#headers#PrevSiblingHeader())
  132. endfunction
  133. function! pandoc#keyboard#sections#FirstChildHeader() abort "{{{3
  134. call pandoc#keyboard#MovetoLine(markdown#headers#FirstChild())
  135. endfunction
  136. function! pandoc#keyboard#sections#LastChildHeader() abort "{{{3
  137. call pandoc#keyboard#MovetoLine(markdown#headers#LastChild())
  138. endfunction
  139. function! pandoc#keyboard#sections#GotoNthChildHeader(count) abort "{{{3
  140. call pandoc#keyboard#MovetoLine(markdown#headers#NthChild(a:count))
  141. endfunction
  142. " "}}}2
  143. " Objects: {{{2
  144. function! pandoc#keyboard#sections#SelectSection(mode) abort "{{{3
  145. let range = markdown#sections#SectionRange(a:mode)
  146. let start= range[0]
  147. let end = range[1] - 1
  148. exe 'normal! '.start.'GV'.end.'G\<cr>'
  149. endfunction
  150. "}}}2
  151. " }}}1