eruby.vim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. " Vim filetype plugin
  2. " Language: eRuby
  3. " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
  4. " URL: https://github.com/vim-ruby/vim-ruby
  5. " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
  6. " Last Change: 2022 May 15
  7. " 2024 Jan 14 by Vim Project (browsefilter)
  8. " 2024 May 23 by Riley Bruins <ribru17@gmail.com> ('commentstring')
  9. " Only do this when not done yet for this buffer
  10. if exists("b:did_ftplugin")
  11. finish
  12. endif
  13. let s:save_cpo = &cpo
  14. set cpo-=C
  15. " Define some defaults in case the included ftplugins don't set them.
  16. let s:undo_ftplugin = ""
  17. if has("win32")
  18. let s:browsefilter = "All Files (*.*)\t*\n"
  19. else
  20. let s:browsefilter = "All Files (*)\t*\n"
  21. endif
  22. let s:match_words = ""
  23. if !exists("g:eruby_default_subtype")
  24. let g:eruby_default_subtype = "html"
  25. endif
  26. if &filetype =~ '^eruby\.'
  27. let b:eruby_subtype = matchstr(&filetype,'^eruby\.\zs\w\+')
  28. elseif !exists("b:eruby_subtype")
  29. let s:lines = getline(1)."\n".getline(2)."\n".getline(3)."\n".getline(4)."\n".getline(5)."\n".getline("$")
  30. let b:eruby_subtype = matchstr(s:lines,'eruby_subtype=\zs\w\+')
  31. if b:eruby_subtype == ''
  32. let b:eruby_subtype = matchstr(substitute(expand("%:t"),'\c\%(\.erb\|\.eruby\|\.erubis\|\.example\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
  33. endif
  34. if b:eruby_subtype == 'rhtml'
  35. let b:eruby_subtype = 'html'
  36. elseif b:eruby_subtype == 'rb'
  37. let b:eruby_subtype = 'ruby'
  38. elseif b:eruby_subtype == 'yml'
  39. let b:eruby_subtype = 'yaml'
  40. elseif b:eruby_subtype == 'js'
  41. let b:eruby_subtype = 'javascript'
  42. elseif b:eruby_subtype == 'txt'
  43. " Conventional; not a real file type
  44. let b:eruby_subtype = 'text'
  45. elseif b:eruby_subtype == ''
  46. let b:eruby_subtype = g:eruby_default_subtype
  47. endif
  48. endif
  49. if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=? 'eruby'
  50. exe "runtime! ftplugin/".b:eruby_subtype.".vim ftplugin/".b:eruby_subtype."_*.vim ftplugin/".b:eruby_subtype."/*.vim"
  51. else
  52. runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
  53. endif
  54. unlet! b:did_ftplugin
  55. " Override our defaults if these were set by an included ftplugin.
  56. if exists("b:undo_ftplugin")
  57. let s:undo_ftplugin = b:undo_ftplugin
  58. unlet b:undo_ftplugin
  59. endif
  60. if exists("b:browsefilter")
  61. let s:browsefilter = b:browsefilter
  62. unlet b:browsefilter
  63. endif
  64. if exists("b:match_words")
  65. let s:match_words = b:match_words
  66. unlet b:match_words
  67. endif
  68. let s:cfilemap = v:version >= 704 ? maparg('<Plug><cfile>', 'c', 0, 1) : {}
  69. if !get(s:cfilemap, 'buffer') || !s:cfilemap.expr || s:cfilemap.rhs =~# 'ErubyAtCursor()'
  70. let s:cfilemap = {}
  71. endif
  72. if !has_key(s:cfilemap, 'rhs')
  73. let s:cfilemap.rhs = "substitute(&l:inex =~# '\\<v:fname\\>' && len(expand('<cfile>')) ? eval(substitute(&l:inex, '\\<v:fname\\>', '\\=string(expand(\"<cfile>\"))', 'g')) : '', '^$', \"\\022\\006\",'')"
  74. endif
  75. let s:ctagmap = v:version >= 704 ? maparg('<Plug><ctag>', 'c', 0, 1) : {}
  76. if !get(s:ctagmap, 'buffer') || !s:ctagmap.expr || s:ctagmap.rhs =~# 'ErubyAtCursor()'
  77. let s:ctagmap = {}
  78. endif
  79. let s:include = &l:include
  80. let s:path = &l:path
  81. let s:suffixesadd = &l:suffixesadd
  82. runtime! ftplugin/ruby.vim ftplugin/ruby_*.vim ftplugin/ruby/*.vim
  83. let b:did_ftplugin = 1
  84. " Combine the new set of values with those previously included.
  85. if !exists('b:undo_ftplugin')
  86. " No-op
  87. let b:undo_ftplugin = 'exe'
  88. endif
  89. if !empty(s:undo_ftplugin)
  90. let b:undo_ftplugin .= '|' . s:undo_ftplugin
  91. endif
  92. if exists ("b:browsefilter")
  93. let s:browsefilter = substitute(b:browsefilter,'\cAll Files (\*\.\*)\t\*\.\*\n','','') . s:browsefilter
  94. endif
  95. if exists("b:match_words")
  96. let s:match_words = b:match_words . ',' . s:match_words
  97. endif
  98. if len(s:include)
  99. let &l:include = s:include
  100. endif
  101. let &l:path = s:path . (s:path =~# ',$\|^$' ? '' : ',') . &l:path
  102. let &l:suffixesadd = s:suffixesadd . (s:suffixesadd =~# ',$\|^$' ? '' : ',') . &l:suffixesadd
  103. exe 'cmap <buffer><script><expr> <Plug><cfile> ErubyAtCursor() ? ' . maparg('<Plug><cfile>', 'c') . ' : ' . s:cfilemap.rhs
  104. exe 'cmap <buffer><script><expr> <Plug><ctag> ErubyAtCursor() ? ' . maparg('<Plug><ctag>', 'c') . ' : ' . get(s:ctagmap, 'rhs', '"\022\027"')
  105. unlet s:cfilemap s:ctagmap s:include s:path s:suffixesadd
  106. " Change the browse dialog on Win32 and GTK to show mainly eRuby-related files
  107. if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
  108. let b:browsefilter="eRuby Files (*.erb, *.rhtml)\t*.erb;*.rhtml\n" . s:browsefilter
  109. endif
  110. " Load the combined list of match_words for matchit.vim
  111. if exists("loaded_matchit")
  112. let b:match_words = s:match_words
  113. endif
  114. " TODO: comments=
  115. setlocal commentstring=<%#\ %s\ %>
  116. let b:undo_ftplugin = "setl cms< " .
  117. \ " | unlet! b:browsefilter b:match_words | " . b:undo_ftplugin
  118. let &cpo = s:save_cpo
  119. unlet s:save_cpo
  120. function! ErubyAtCursor() abort
  121. let groups = map(['erubyBlock', 'erubyComment', 'erubyExpression', 'erubyOneLiner'], 'hlID(v:val)')
  122. return !empty(filter(synstack(line('.'), col('.')), 'index(groups, v:val) >= 0'))
  123. endfunction
  124. " vim: nowrap sw=2 sts=2 ts=8: