blob: 893fa58d32cc4d6de3e98f38f8b351ea01f51cdb [file] [log] [blame]
Bram Moolenaar6b730e12005-09-16 21:47:57 +00001" Vim filetype plugin
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00002" Language: eRuby
Bram Moolenaar1d689522010-05-28 20:54:39 +02003" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
Bram Moolenaarec7944a2013-06-12 21:29:15 +02004" URL: https://github.com/vim-ruby/vim-ruby
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00005" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Doug Kearnsda16a1b2023-09-01 18:33:33 +02006" Last Change: 2022 May 15
Bram Moolenaar6b730e12005-09-16 21:47:57 +00007
8" Only do this when not done yet for this buffer
Bram Moolenaar29466f22007-05-10 17:45:37 +00009if exists("b:did_ftplugin")
Bram Moolenaar6b730e12005-09-16 21:47:57 +000010 finish
11endif
12
13let s:save_cpo = &cpo
14set cpo-=C
15
16" Define some defaults in case the included ftplugins don't set them.
17let s:undo_ftplugin = ""
Bram Moolenaar29466f22007-05-10 17:45:37 +000018let s:browsefilter = "All Files (*.*)\t*.*\n"
Bram Moolenaar6b730e12005-09-16 21:47:57 +000019let s:match_words = ""
20
Bram Moolenaar29466f22007-05-10 17:45:37 +000021if !exists("g:eruby_default_subtype")
22 let g:eruby_default_subtype = "html"
23endif
24
Bram Moolenaarec7944a2013-06-12 21:29:15 +020025if &filetype =~ '^eruby\.'
26 let b:eruby_subtype = matchstr(&filetype,'^eruby\.\zs\w\+')
27elseif !exists("b:eruby_subtype")
Bram Moolenaar29466f22007-05-10 17:45:37 +000028 let s:lines = getline(1)."\n".getline(2)."\n".getline(3)."\n".getline(4)."\n".getline(5)."\n".getline("$")
29 let b:eruby_subtype = matchstr(s:lines,'eruby_subtype=\zs\w\+')
30 if b:eruby_subtype == ''
Bram Moolenaard09091d2019-01-17 16:07:22 +010031 let b:eruby_subtype = matchstr(substitute(expand("%:t"),'\c\%(\.erb\|\.eruby\|\.erubis\|\.example\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
Bram Moolenaar29466f22007-05-10 17:45:37 +000032 endif
33 if b:eruby_subtype == 'rhtml'
34 let b:eruby_subtype = 'html'
35 elseif b:eruby_subtype == 'rb'
36 let b:eruby_subtype = 'ruby'
37 elseif b:eruby_subtype == 'yml'
38 let b:eruby_subtype = 'yaml'
39 elseif b:eruby_subtype == 'js'
40 let b:eruby_subtype = 'javascript'
41 elseif b:eruby_subtype == 'txt'
42 " Conventional; not a real file type
43 let b:eruby_subtype = 'text'
44 elseif b:eruby_subtype == ''
45 let b:eruby_subtype = g:eruby_default_subtype
46 endif
47endif
48
Bram Moolenaard09091d2019-01-17 16:07:22 +010049if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=? 'eruby'
Bram Moolenaar29466f22007-05-10 17:45:37 +000050 exe "runtime! ftplugin/".b:eruby_subtype.".vim ftplugin/".b:eruby_subtype."_*.vim ftplugin/".b:eruby_subtype."/*.vim"
51else
52 runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
53endif
54unlet! b:did_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000055
56" Override our defaults if these were set by an included ftplugin.
57if exists("b:undo_ftplugin")
Bram Moolenaar29466f22007-05-10 17:45:37 +000058 let s:undo_ftplugin = b:undo_ftplugin
59 unlet b:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000060endif
61if exists("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +000062 let s:browsefilter = b:browsefilter
63 unlet b:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +000064endif
65if exists("b:match_words")
Bram Moolenaar29466f22007-05-10 17:45:37 +000066 let s:match_words = b:match_words
67 unlet b:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +000068endif
69
Bram Moolenaard09091d2019-01-17 16:07:22 +010070let s:cfilemap = v:version >= 704 ? maparg('<Plug><cfile>', 'c', 0, 1) : {}
71if !get(s:cfilemap, 'buffer') || !s:cfilemap.expr || s:cfilemap.rhs =~# 'ErubyAtCursor()'
72 let s:cfilemap = {}
73endif
74if !has_key(s:cfilemap, 'rhs')
75 let s:cfilemap.rhs = "substitute(&l:inex =~# '\\<v:fname\\>' && len(expand('<cfile>')) ? eval(substitute(&l:inex, '\\<v:fname\\>', '\\=string(expand(\"<cfile>\"))', 'g')) : '', '^$', \"\\022\\006\",'')"
76endif
77let s:ctagmap = v:version >= 704 ? maparg('<Plug><ctag>', 'c', 0, 1) : {}
78if !get(s:ctagmap, 'buffer') || !s:ctagmap.expr || s:ctagmap.rhs =~# 'ErubyAtCursor()'
79 let s:ctagmap = {}
80endif
81let s:include = &l:include
82let s:path = &l:path
83let s:suffixesadd = &l:suffixesadd
84
Bram Moolenaar6b730e12005-09-16 21:47:57 +000085runtime! ftplugin/ruby.vim ftplugin/ruby_*.vim ftplugin/ruby/*.vim
86let b:did_ftplugin = 1
87
88" Combine the new set of values with those previously included.
Doug Kearnsda16a1b2023-09-01 18:33:33 +020089if !exists('b:undo_ftplugin')
90 " No-op
91 let b:undo_ftplugin = 'exe'
92endif
93if !empty(s:undo_ftplugin)
94 let b:undo_ftplugin .= '|' . s:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000095endif
96if exists ("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +000097 let s:browsefilter = substitute(b:browsefilter,'\cAll Files (\*\.\*)\t\*\.\*\n','','') . s:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +000098endif
99if exists("b:match_words")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000100 let s:match_words = b:match_words . ',' . s:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000101endif
102
Bram Moolenaard09091d2019-01-17 16:07:22 +0100103if len(s:include)
104 let &l:include = s:include
105endif
106let &l:path = s:path . (s:path =~# ',$\|^$' ? '' : ',') . &l:path
107let &l:suffixesadd = s:suffixesadd . (s:suffixesadd =~# ',$\|^$' ? '' : ',') . &l:suffixesadd
108exe 'cmap <buffer><script><expr> <Plug><cfile> ErubyAtCursor() ? ' . maparg('<Plug><cfile>', 'c') . ' : ' . s:cfilemap.rhs
109exe 'cmap <buffer><script><expr> <Plug><ctag> ErubyAtCursor() ? ' . maparg('<Plug><ctag>', 'c') . ' : ' . get(s:ctagmap, 'rhs', '"\022\027"')
110unlet s:cfilemap s:ctagmap s:include s:path s:suffixesadd
111
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000112" Change the browse dialog on Win32 to show mainly eRuby-related files
113if has("gui_win32")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000114 let b:browsefilter="eRuby Files (*.erb, *.rhtml)\t*.erb;*.rhtml\n" . s:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000115endif
116
117" Load the combined list of match_words for matchit.vim
118if exists("loaded_matchit")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000119 let b:match_words = s:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000120endif
121
122" TODO: comments=
123setlocal commentstring=<%#%s%>
124
Bram Moolenaar4d8f4762021-06-27 15:18:56 +0200125let b:undo_ftplugin = "setl cms< " .
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200126 \ " | unlet! b:browsefilter b:match_words | " . b:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000127
128let &cpo = s:save_cpo
Bram Moolenaar84f72352012-03-11 15:57:40 +0100129unlet s:save_cpo
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000130
Bram Moolenaard09091d2019-01-17 16:07:22 +0100131function! ErubyAtCursor() abort
132 let groups = map(['erubyBlock', 'erubyComment', 'erubyExpression', 'erubyOneLiner'], 'hlID(v:val)')
133 return !empty(filter(synstack(line('.'), col('.')), 'index(groups, v:val) >= 0'))
134endfunction
135
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000136" vim: nowrap sw=2 sts=2 ts=8: