blob: b3e074aa205c1f453108d7b77ce50d0cedd1a7ca [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)
Riley Bruins0a083062024-06-03 20:40:45 +02008" 2024 May 23 by Riley Bruins <ribru17@gmail.com> ('commentstring')
Bram Moolenaar6b730e12005-09-16 21:47:57 +00009
10" Only do this when not done yet for this buffer
Bram Moolenaar29466f22007-05-10 17:45:37 +000011if exists("b:did_ftplugin")
Bram Moolenaar6b730e12005-09-16 21:47:57 +000012 finish
13endif
14
15let s:save_cpo = &cpo
16set cpo-=C
17
18" Define some defaults in case the included ftplugins don't set them.
19let s:undo_ftplugin = ""
Doug Kearns93197fd2024-01-14 20:59:02 +010020if has("win32")
21 let s:browsefilter = "All Files (*.*)\t*\n"
22else
23 let s:browsefilter = "All Files (*)\t*\n"
24endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +000025let s:match_words = ""
26
Bram Moolenaar29466f22007-05-10 17:45:37 +000027if !exists("g:eruby_default_subtype")
28 let g:eruby_default_subtype = "html"
29endif
30
Bram Moolenaarec7944a2013-06-12 21:29:15 +020031if &filetype =~ '^eruby\.'
32 let b:eruby_subtype = matchstr(&filetype,'^eruby\.\zs\w\+')
33elseif !exists("b:eruby_subtype")
Bram Moolenaar29466f22007-05-10 17:45:37 +000034 let s:lines = getline(1)."\n".getline(2)."\n".getline(3)."\n".getline(4)."\n".getline(5)."\n".getline("$")
35 let b:eruby_subtype = matchstr(s:lines,'eruby_subtype=\zs\w\+')
36 if b:eruby_subtype == ''
Bram Moolenaard09091d2019-01-17 16:07:22 +010037 let b:eruby_subtype = matchstr(substitute(expand("%:t"),'\c\%(\.erb\|\.eruby\|\.erubis\|\.example\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
Bram Moolenaar29466f22007-05-10 17:45:37 +000038 endif
39 if b:eruby_subtype == 'rhtml'
40 let b:eruby_subtype = 'html'
41 elseif b:eruby_subtype == 'rb'
42 let b:eruby_subtype = 'ruby'
43 elseif b:eruby_subtype == 'yml'
44 let b:eruby_subtype = 'yaml'
45 elseif b:eruby_subtype == 'js'
46 let b:eruby_subtype = 'javascript'
47 elseif b:eruby_subtype == 'txt'
48 " Conventional; not a real file type
49 let b:eruby_subtype = 'text'
50 elseif b:eruby_subtype == ''
51 let b:eruby_subtype = g:eruby_default_subtype
52 endif
53endif
54
Bram Moolenaard09091d2019-01-17 16:07:22 +010055if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=? 'eruby'
Bram Moolenaar29466f22007-05-10 17:45:37 +000056 exe "runtime! ftplugin/".b:eruby_subtype.".vim ftplugin/".b:eruby_subtype."_*.vim ftplugin/".b:eruby_subtype."/*.vim"
57else
58 runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
59endif
60unlet! b:did_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000061
62" Override our defaults if these were set by an included ftplugin.
63if exists("b:undo_ftplugin")
Bram Moolenaar29466f22007-05-10 17:45:37 +000064 let s:undo_ftplugin = b:undo_ftplugin
65 unlet b:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +000066endif
67if exists("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +000068 let s:browsefilter = b:browsefilter
69 unlet b:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +000070endif
71if exists("b:match_words")
Bram Moolenaar29466f22007-05-10 17:45:37 +000072 let s:match_words = b:match_words
73 unlet b:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +000074endif
75
Bram Moolenaard09091d2019-01-17 16:07:22 +010076let s:cfilemap = v:version >= 704 ? maparg('<Plug><cfile>', 'c', 0, 1) : {}
77if !get(s:cfilemap, 'buffer') || !s:cfilemap.expr || s:cfilemap.rhs =~# 'ErubyAtCursor()'
78 let s:cfilemap = {}
79endif
80if !has_key(s:cfilemap, 'rhs')
81 let s:cfilemap.rhs = "substitute(&l:inex =~# '\\<v:fname\\>' && len(expand('<cfile>')) ? eval(substitute(&l:inex, '\\<v:fname\\>', '\\=string(expand(\"<cfile>\"))', 'g')) : '', '^$', \"\\022\\006\",'')"
82endif
83let s:ctagmap = v:version >= 704 ? maparg('<Plug><ctag>', 'c', 0, 1) : {}
84if !get(s:ctagmap, 'buffer') || !s:ctagmap.expr || s:ctagmap.rhs =~# 'ErubyAtCursor()'
85 let s:ctagmap = {}
86endif
87let s:include = &l:include
88let s:path = &l:path
89let s:suffixesadd = &l:suffixesadd
90
Bram Moolenaar6b730e12005-09-16 21:47:57 +000091runtime! ftplugin/ruby.vim ftplugin/ruby_*.vim ftplugin/ruby/*.vim
92let b:did_ftplugin = 1
93
94" Combine the new set of values with those previously included.
Doug Kearnsda16a1b2023-09-01 18:33:33 +020095if !exists('b:undo_ftplugin')
96 " No-op
97 let b:undo_ftplugin = 'exe'
98endif
99if !empty(s:undo_ftplugin)
100 let b:undo_ftplugin .= '|' . s:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000101endif
102if exists ("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000103 let s:browsefilter = substitute(b:browsefilter,'\cAll Files (\*\.\*)\t\*\.\*\n','','') . s:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000104endif
105if exists("b:match_words")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000106 let s:match_words = b:match_words . ',' . s:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000107endif
108
Bram Moolenaard09091d2019-01-17 16:07:22 +0100109if len(s:include)
110 let &l:include = s:include
111endif
112let &l:path = s:path . (s:path =~# ',$\|^$' ? '' : ',') . &l:path
113let &l:suffixesadd = s:suffixesadd . (s:suffixesadd =~# ',$\|^$' ? '' : ',') . &l:suffixesadd
114exe 'cmap <buffer><script><expr> <Plug><cfile> ErubyAtCursor() ? ' . maparg('<Plug><cfile>', 'c') . ' : ' . s:cfilemap.rhs
115exe 'cmap <buffer><script><expr> <Plug><ctag> ErubyAtCursor() ? ' . maparg('<Plug><ctag>', 'c') . ' : ' . get(s:ctagmap, 'rhs', '"\022\027"')
116unlet s:cfilemap s:ctagmap s:include s:path s:suffixesadd
117
Doug Kearns93197fd2024-01-14 20:59:02 +0100118" Change the browse dialog on Win32 and GTK to show mainly eRuby-related files
119if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000120 let b:browsefilter="eRuby Files (*.erb, *.rhtml)\t*.erb;*.rhtml\n" . s:browsefilter
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000121endif
122
123" Load the combined list of match_words for matchit.vim
124if exists("loaded_matchit")
Bram Moolenaar29466f22007-05-10 17:45:37 +0000125 let b:match_words = s:match_words
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000126endif
127
128" TODO: comments=
Riley Bruins0a083062024-06-03 20:40:45 +0200129setlocal commentstring=<%#\ %s\ %>
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000130
Bram Moolenaar4d8f4762021-06-27 15:18:56 +0200131let b:undo_ftplugin = "setl cms< " .
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200132 \ " | unlet! b:browsefilter b:match_words | " . b:undo_ftplugin
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000133
134let &cpo = s:save_cpo
Bram Moolenaar84f72352012-03-11 15:57:40 +0100135unlet s:save_cpo
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000136
Bram Moolenaard09091d2019-01-17 16:07:22 +0100137function! ErubyAtCursor() abort
138 let groups = map(['erubyBlock', 'erubyComment', 'erubyExpression', 'erubyOneLiner'], 'hlID(v:val)')
139 return !empty(filter(synstack(line('.'), col('.')), 'index(groups, v:val) >= 0'))
140endfunction
141
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000142" vim: nowrap sw=2 sts=2 ts=8: