folding.vim 8.1 KB

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