folding.vim 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. " Folding - Modeline and Notes {{{
  2. " vim: set sw=2 ts=2 sts=2 et tw=78 foldmarker={{{,}}} foldlevel=1 foldmethod=marker:
  3. "
  4. " cinaeco/dotfiles Folding Settings
  5. "
  6. " These are settings to make folding easier to use and look at:
  7. " - Indented Folds to match their first line.
  8. " - Statbox to right displays line count and fold level.
  9. " - SpaceBar toggles folds, if any.
  10. " (much more convenient than the 'z' commands)
  11. " - SPF13-VIM provides quick foldlevel setting map: <Leader>f[0-9]
  12. " (useful with statbox lvl)
  13. "
  14. " Note that custom foldtext will only work for vim 7.3+. For earlier
  15. " versions of vim, only the colouring and spacebar mapping will take effect.
  16. "
  17. " TODO:
  18. " - Fold description is just the first line found. Haven't really
  19. " understood the regex, but could perhaps make it better.
  20. " - Fold description current truncation rule is 1/3 of the window width.
  21. " Perhaps this could be some less arbitrary rule?
  22. "
  23. " }}}
  24. if has('folding')
  25. " Default Settings {{{
  26. set foldenable
  27. set foldmethod=indent
  28. set foldlevel=10
  29. " }}}
  30. " Keyboard Shortcuts {{{
  31. " Space as a Folding toggle in normal mode.
  32. nnoremap <silent> <Leader><Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
  33. " Code folding options (spf13-vim)
  34. nmap zf0 :set foldlevel=0<CR>
  35. nmap zf1 :set foldlevel=1<CR>
  36. nmap zf2 :set foldlevel=2<CR>
  37. nmap zf3 :set foldlevel=3<CR>
  38. nmap zf4 :set foldlevel=4<CR>
  39. nmap zf5 :set foldlevel=5<CR>
  40. nmap zf6 :set foldlevel=6<CR>
  41. nmap zf7 :set foldlevel=7<CR>
  42. nmap zf8 :set foldlevel=8<CR>
  43. nmap zf9 :set foldlevel=9<CR>
  44. " }}}
  45. " Fold Highlighting {{{
  46. highlight Folded term=none cterm=none ctermfg=darkgrey ctermbg=none guifg=darkgrey guibg=none
  47. " }}}
  48. " Fold Text {{{
  49. set foldtext=CustomFoldText()
  50. function! CustomFoldText(...)
  51. " At least vim 7.3 {{{
  52. " - Requirement for strdisplaywidth().
  53. " - strdisplaywidth() seems to work. If multi-byte characters start to give
  54. " trouble, consider checking the more primitive solution in strlen() help.
  55. if v:version < 703
  56. return foldtext()
  57. endif
  58. " }}}
  59. " Common variables for all foldmethods {{{
  60. let lineCount = v:foldend - v:foldstart + 1
  61. let displayWidth = winwidth(0) - &foldcolumn
  62. if (&number || &relativenumber)
  63. let displayWidth -= &numberwidth
  64. endif
  65. let foldChar = '-'
  66. " }}}
  67. " Set fold fillchar {{{
  68. " - This complicated line is to ensure we replace the fold fillchar only.
  69. let &l:fillchars = substitute(&l:fillchars,',\?fold:.','','gi')
  70. execute 'setlocal fillchars+=fold:' . foldChar
  71. " }}}
  72. " Handle diff foldmethod {{{
  73. " - Display a centre-aligned statbox with the number of lines.
  74. if &foldmethod == 'diff'
  75. " Prepare the statbox {{{
  76. let statBox = printf('[ %s matching lines ]', lineCount)
  77. " }}}
  78. " Prepare filler lines {{{
  79. let filler = repeat(foldChar, (displayWidth - strchars(statBox)) / 2)
  80. " }}}
  81. " Output the combined fold text {{{
  82. return filler.statBox
  83. " }}}
  84. endif
  85. " }}}
  86. " Handle all other foldmethods {{{
  87. " Prepare fold indent and indicator {{{
  88. " - If indent allows, build the indicator into it.
  89. let foldIndicator = '+ '
  90. let indLen = strdisplaywidth(foldIndicator)
  91. if indent(v:foldstart) >= indLen
  92. let indent = repeat(' ', indent(v:foldstart) - indLen) . foldIndicator
  93. else
  94. let indent = repeat(' ', indent(v:foldstart))
  95. endif
  96. " }}}
  97. " Prepare the statbox {{{
  98. " - Fixed statbox width at 18 characters.
  99. " - Count width by display cells instead of bytes if at least vim 7.4
  100. if v:version >= 704
  101. let countType = 'S'
  102. else
  103. let countType = 's'
  104. endif
  105. let statBox = '[ ' . printf('%14'.countType, lineCount.' lns, lv '.v:foldlevel) . ' ]'
  106. " }}}
  107. " Prepare fold description {{{
  108. " - Remove fold markers and comment markers.
  109. " - Truncate to 1/3 of the current window width.
  110. " Use function argument as line text if provided {{{
  111. let line = a:0 > 0 ? a:1 : getline(v:foldstart)
  112. " }}}
  113. " Remove fold markers {{{
  114. let foldmarkers = split(&foldmarker, ',')
  115. let line = substitute(line, '\V' . foldmarkers[0] . '\%(\d\+\)\?\s\*', '', '')
  116. " }}}
  117. " Remove surrounding whitespace {{{
  118. let line = substitute(line, '^\s*\(.\{-}\)\s*$', '\1', '')
  119. " }}}
  120. " Add an extra space at the end {{{
  121. let foldDesc = line.' '
  122. " }}}
  123. " }}}
  124. " Prepare filler lines {{{
  125. " - midFiller is the fill between the description and statbox.
  126. " - midFiller compensates for column widths generated by foldcolumn, number
  127. " and relativenumber.
  128. let endFiller = repeat(foldChar, 1)
  129. let midFillerLength = displayWidth - strdisplaywidth(indent.foldDesc.statBox.endFiller)
  130. let midFiller = repeat(foldChar, midFillerLength)
  131. " }}}
  132. " Output the combined fold text {{{
  133. return indent.foldDesc.midFiller.statBox.endFiller
  134. " }}}
  135. " }}}
  136. endfunction
  137. " }}}
  138. " Lokaltog's Fold Text for learning more stuff about fold description preparation {{{
  139. function! FoldText(...)
  140. " This function uses code from doy's vim-foldtext: https://github.com/doy/vim-foldtext
  141. " Prepare fold variables {{{
  142. " Use function argument as line text if provided
  143. let l:line = a:0 > 0 ? a:1 : getline(v:foldstart)
  144. let l:line_count = v:foldend - v:foldstart + 1
  145. let l:indent = repeat(' ', indent(v:foldstart))
  146. let l:w_win = winwidth(0)
  147. let l:w_num = getwinvar(0, '&number') * getwinvar(0, '&numberwidth')
  148. let l:w_fold = getwinvar(0, '&foldcolumn')
  149. " }}}
  150. " Handle diff foldmethod {{{
  151. if &fdm == 'diff'
  152. let l:text = printf('┤ %s matching lines ├', l:line_count)
  153. " Center-align the foldtext
  154. return repeat('┄', (l:w_win - strchars(l:text) - l:w_num - l:w_fold) / 2) . l:text
  155. endif
  156. " }}}
  157. " Handle other foldmethods {{{
  158. let l:text = l:line
  159. " Remove foldmarkers {{{
  160. let l:foldmarkers = split(&foldmarker, ',')
  161. let l:text = substitute(l:text, '\V' . l:foldmarkers[0] . '\%(\d\+\)\?\s\*', '', '')
  162. " }}}
  163. " Remove comments {{{
  164. let l:comment = split(&commentstring, '%s')
  165. if l:comment[0] != ''
  166. let l:comment_begin = l:comment[0]
  167. let l:comment_end = ''
  168. if len(l:comment) > 1
  169. let l:comment_end = l:comment[1]
  170. endif
  171. let l:pattern = '\V' . l:comment_begin . '\s\*' . l:comment_end . '\s\*\$'
  172. if l:text =~ l:pattern
  173. let l:text = substitute(l:text, l:pattern, ' ', '')
  174. else
  175. let l:text = substitute(l:text, '.*\V' . l:comment_begin, ' ', '')
  176. if l:comment_end != ''
  177. let l:text = substitute(l:text, '\V' . l:comment_end, ' ', '')
  178. endif
  179. endif
  180. endif
  181. " }}}
  182. " Remove preceding non-word characters {{{
  183. let l:text = substitute(l:text, '^\W*', '', '')
  184. " }}}
  185. " Remove surrounding whitespace {{{
  186. let l:text = substitute(l:text, '^\s*\(.\{-}\)\s*$', '\1', '')
  187. " }}}
  188. " Make unmatched block delimiters prettier {{{
  189. let l:text = substitute(l:text, '([^)]*$', '⟯ ⋯ ⟮', '')
  190. let l:text = substitute(l:text, '{[^}]*$', '⟯ ⋯ ⟮', '')
  191. let l:text = substitute(l:text, '\[[^\]]*$', '⟯ ⋯ ⟮', '')
  192. " }}}
  193. " Add arrows when indent level > 2 spaces {{{
  194. if indent(v:foldstart) > 2
  195. let l:cline = substitute(l:line, '^\s*\(.\{-}\)\s*$', '\1', '')
  196. let l:clen = strlen(matchstr(l:cline, '^\W*'))
  197. let l:indent = repeat(' ', indent(v:foldstart) - 2)
  198. let l:text = '▸ ' . l:text
  199. endif
  200. " }}}
  201. " Prepare fold text {{{
  202. let l:fnum = printf('┤ %s ⭡ ', printf('%4s', l:line_count))
  203. let l:ftext = printf('%s%s ', l:indent, l:text)
  204. " }}}
  205. return l:ftext . repeat('┄', l:w_win - strchars(l:fnum) - strchars(l:ftext) - l:w_num - l:w_fold) . l:fnum
  206. " }}}
  207. endfunction
  208. " }}}
  209. endif