folding.vim 7.8 KB

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