debcontrol.vim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. " Vim filetype plugin file (GUI menu and folding)
  2. " Language: Debian control files
  3. " Maintainer: Debian Vim Maintainers
  4. " Former Maintainer: Pierre Habouzit <madcoder@debian.org>
  5. " Last Change: 2018-01-28
  6. " URL: https://salsa.debian.org/vim-team/vim-debian/blob/master/ftplugin/debcontrol.vim
  7. " Do these settings once per buffer
  8. if exists('b:did_ftplugin')
  9. finish
  10. endif
  11. let b:did_ftplugin=1
  12. " {{{1 Local settings (do on every load)
  13. if exists('g:debcontrol_fold_enable')
  14. setlocal foldmethod=expr
  15. setlocal foldexpr=DebControlFold(v:lnum)
  16. setlocal foldtext=DebControlFoldText()
  17. endif
  18. setlocal textwidth=0
  19. " Clean unloading
  20. let b:undo_ftplugin = 'setlocal tw< foldmethod< foldexpr< foldtext<'
  21. " }}}1
  22. " {{{1 folding
  23. function! s:getField(f, lnum)
  24. let line = getline(a:lnum)
  25. let fwdsteps = 0
  26. while line !~ '^'.a:f.':'
  27. let fwdsteps += 1
  28. let line = getline(a:lnum + fwdsteps)
  29. if line ==# ''
  30. return 'unknown'
  31. endif
  32. endwhile
  33. return substitute(line, '^'.a:f.': *', '', '')
  34. endfunction
  35. function! DebControlFoldText()
  36. if v:folddashes ==# '-' " debcontrol entry fold
  37. let type = substitute(getline(v:foldstart), ':.*', '', '')
  38. if type ==# 'Source'
  39. let ftext = substitute(foldtext(), ' *Source: *', ' ', '')
  40. return ftext . ' -- ' . s:getField('Maintainer', v:foldstart) . ' '
  41. endif
  42. let arch = s:getField('Architecture', v:foldstart)
  43. let ftext = substitute(foldtext(), ' *Package: *', ' [' . arch . '] ', '')
  44. return ftext . ': ' . s:getField('Description', v:foldstart) . ' '
  45. endif
  46. return foldtext()
  47. endfunction
  48. function! DebControlFold(l)
  49. " This is for not merging blank lines around folds to them
  50. if getline(a:l) =~# '^Source:'
  51. return '>1'
  52. endif
  53. if getline(a:l) =~# '^Package:'
  54. return '>1'
  55. endif
  56. return '='
  57. endfunction
  58. " }}}1