debcontrol.vim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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: 2024 May 25
  6. " URL: https://salsa.debian.org/vim-team/vim-debian/blob/main/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. setlocal comments=:#
  20. setlocal commentstring=#\ %s
  21. " Clean unloading
  22. let b:undo_ftplugin = 'setlocal tw< foldmethod< foldexpr< foldtext< comments< commentstring<'
  23. " }}}1
  24. " {{{1 folding
  25. function! s:getField(f, lnum)
  26. let line = getline(a:lnum)
  27. let fwdsteps = 0
  28. while line !~ '^'.a:f.':'
  29. let fwdsteps += 1
  30. let line = getline(a:lnum + fwdsteps)
  31. if line ==# ''
  32. return 'unknown'
  33. endif
  34. endwhile
  35. return substitute(line, '^'.a:f.': *', '', '')
  36. endfunction
  37. function! DebControlFoldText()
  38. if v:folddashes ==# '-' " debcontrol entry fold
  39. let type = substitute(getline(v:foldstart), ':.*', '', '')
  40. if type ==# 'Source'
  41. let ftext = substitute(foldtext(), ' *Source: *', ' ', '')
  42. return ftext . ' -- ' . s:getField('Maintainer', v:foldstart) . ' '
  43. endif
  44. let arch = s:getField('Architecture', v:foldstart)
  45. let ftext = substitute(foldtext(), ' *Package: *', ' [' . arch . '] ', '')
  46. return ftext . ': ' . s:getField('Description', v:foldstart) . ' '
  47. endif
  48. return foldtext()
  49. endfunction
  50. function! DebControlFold(l)
  51. " This is for not merging blank lines around folds to them
  52. if getline(a:l) =~# '^Source:'
  53. return '>1'
  54. endif
  55. if getline(a:l) =~# '^Package:'
  56. return '>1'
  57. endif
  58. return '='
  59. endfunction
  60. " }}}1