blob: b5c4665d243095ccad61bd57be3069e6f816d81b [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
Doug Kearns93197fd2024-01-14 20:59:02 +01007" 2024 Jan 14 by Vim Project (browsefilter)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00008
9" Only do this when not done yet for this buffer
Bram Moolenaar29466f22007-05-10 17:45:37 +000010if exists("b:did_ftplugin")
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011 finish
12endif
13
14let s:save_cpo = &cpo
15set cpo-=C
16
17" Define some defaults in case the included ftplugins don't set them.
18let s:undo_ftplugin = ""
Doug Kearns93197fd2024-01-14 20:59:02 +010019if has("win32")
20 let s:browsefilter = "All Files (*.*)\t*\n"
21else
22 let s:browsefilter = "All Files (*)\t*\n"
23endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +000024let s:match_words = ""
25
Bram Moolenaar29466f22007-05-10 17:45:37 +000026if !exists("g:eruby_default_subtype")
27 let g:eruby_default_subtype = "html"
28endif
29
Bram Moolenaarec7944a2013-06-12 21:29:15 +020030if &filetype =~ '^eruby\.'
31 let b:eruby_subtype = matchstr(&filetype,'^eruby\.\zs\w\+')
32elseif !exists("b:eruby_subtype")
Bram Moolenaar29466f22007-05-10 17:45:37 +000033 let s:lines = getline(1)."\n".getline(2)."\n".getline(3)."\n".getline(4)."\n".getline(5)."\n".getline("$")
34 let b:eruby_subtype = matchstr(s:lines,'eruby_subtype=\zs\w\+')
35 if b:eruby_subtype == ''
Bram Moolenaard09091d2019-01-17 16:07:22 +010036 let b:eruby_subtype = matchstr(substitute(expand("%:t"),'\c\%(\.erb\|\.eruby\|\.erubis\|\.example\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
Bram Moolenaar29466f22007-05-10 17:45:37 +000037 endif
38 if b:eruby_subtype == 'rhtml'
39 let b:eruby_subtype = 'html'
40 elseif b:eruby_subtype == 'rb'
41 let b:eruby_subtype = 'ruby'
42 elseif b:eruby_subtype == 'yml'
43 let b:eruby_subtype = 'yaml'
44 elseif b:eruby_subtype == 'js'
45 let b:eruby_subtype = 'javascript'
46 elseif b:eruby_subtype == 'txt'
47 " Conventional; not a real file type
48 let b:eruby_subtype = 'text'
49 elseif b:eruby_subtype == ''
50 let b:eruby_subtype = g:eruby_default_subtype
51 endif
52endif
53
Bram Moolenaard09091d2019-01-17 16:07:22 +010054if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=? 'eruby'
Bram Moolenaar29466f22007-05-10 17:45:37 +000055 exe "runtime! ftplugin/".b:eruby_subtype.".vim ftplugin/".b:eruby_subtype."_*.vim ftplugin/".b:eruby_subtype."/*.vim"
56else
57 runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
58endif
59unlet! b:did_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000060
61" Override our defaults if these were set by an included ftplugin.
62if exists("b:undo_ftplugin")
Bram Moolenaar29466f22007-05-10 17:45:37 +000063 let s:undo_ftplugin = b:undo_ftplugin
64 unlet b:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000065endif
66if exists("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +000067 let s:browsefilter = b:browsefilter
68 unlet b:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +000069endif
70if exists("b:match_words")
Bram Moolenaar29466f22007-05-10 17:45:37 +000071 let s:match_words = b:match_words
72 unlet b:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +000073endif
74
Bram Moolenaard09091d2019-01-17 16:07:22 +010075let s:cfilemap = v:version >= 704 ? maparg('<Plug><cfile>', 'c', 0, 1) : {}
76if !get(s:cfilemap, 'buffer') || !s:cfilemap.expr || s:cfilemap.rhs =~# 'ErubyAtCursor()'
77 let s:cfilemap = {}
78endif
79if !has_key(s:cfilemap, 'rhs')
80 let s:cfilemap.rhs = "substitute(&l:inex =~# '\\<v:fname\\>' && len(expand('<cfile>')) ? eval(substitute(&l:inex, '\\<v:fname\\>', '\\=string(expand(\"<cfile>\"))', 'g')) : '', '^$', \"\\022\\006\",'')"
81endif
82let s:ctagmap = v:version >= 704 ? maparg('<Plug><ctag>', 'c', 0, 1) : {}
83if !get(s:ctagmap, 'buffer') || !s:ctagmap.expr || s:ctagmap.rhs =~# 'ErubyAtCursor()'
84 let s:ctagmap = {}
85endif
86let s:include = &l:include
87let s:path = &l:path
88let s:suffixesadd = &l:suffixesadd
89
Bram Moolenaar6b730e12005-09-16 21:47:57 +000090runtime! ftplugin/ruby.vim ftplugin/ruby_*.vim ftplugin/ruby/*.vim
91let b:did_ftplugin = 1
92
93" Combine the new set of values with those previously included.
Doug Kearnsda16a1b2023-09-01 18:33:33 +020094if !exists('b:undo_ftplugin')
95 " No-op
96 let b:undo_ftplugin = 'exe'
97endif
98if !empty(s:undo_ftplugin)
99 let b:undo_ftplugin .= '|' . s:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000100endif
101if exists ("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000102 let s:browsefilter = substitute(b:browsefilter,'\cAll Files (\*\.\*)\t\*\.\*\n','','') . s:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000103endif
104if exists("b:match_words")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000105 let s:match_words = b:match_words . ',' . s:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000106endif
107
Bram Moolenaard09091d2019-01-17 16:07:22 +0100108if len(s:include)
109 let &l:include = s:include
110endif
111let &l:path = s:path . (s:path =~# ',$\|^$' ? '' : ',') . &l:path
112let &l:suffixesadd = s:suffixesadd . (s:suffixesadd =~# ',$\|^$' ? '' : ',') . &l:suffixesadd
113exe 'cmap <buffer><script><expr> <Plug><cfile> ErubyAtCursor() ? ' . maparg('<Plug><cfile>', 'c') . ' : ' . s:cfilemap.rhs
114exe 'cmap <buffer><script><expr> <Plug><ctag> ErubyAtCursor() ? ' . maparg('<Plug><ctag>', 'c') . ' : ' . get(s:ctagmap, 'rhs', '"\022\027"')
115unlet s:cfilemap s:ctagmap s:include s:path s:suffixesadd
116
Doug Kearns93197fd2024-01-14 20:59:02 +0100117" Change the browse dialog on Win32 and GTK to show mainly eRuby-related files
118if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000119 let b:browsefilter="eRuby Files (*.erb, *.rhtml)\t*.erb;*.rhtml\n" . s:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000120endif
121
122" Load the combined list of match_words for matchit.vim
123if exists("loaded_matchit")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000124 let b:match_words = s:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000125endif
126
127" TODO: comments=
128setlocal commentstring=<%#%s%>
129
Bram Moolenaar4d8f4762021-06-27 15:18:56 +0200130let b:undo_ftplugin = "setl cms< " .
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200131 \ " | unlet! b:browsefilter b:match_words | " . b:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000132
133let &cpo = s:save_cpo
Bram Moolenaar84f72352012-03-11 15:57:40 +0100134unlet s:save_cpo
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000135
Bram Moolenaard09091d2019-01-17 16:07:22 +0100136function! ErubyAtCursor() abort
137 let groups = map(['erubyBlock', 'erubyComment', 'erubyExpression', 'erubyOneLiner'], 'hlID(v:val)')
138 return !empty(filter(synstack(line('.'), col('.')), 'index(groups, v:val) >= 0'))
139endfunction
140
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000141" vim: nowrap sw=2 sts=2 ts=8: