eruby.vim 4.6 KB

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