" rails.vim - Detect a rails application " Author: Tim Pope " GetLatestVimScripts: 1567 1 :AutoInstall: rails.vim " URL: http://rails.vim.tpope.net/ " Install this file as plugin/rails.vim. See doc/rails.txt for details. (Grab " it from the URL above if you don't have it.) To access it from Vim, see " :help add-local-help (hint: :helptags ~/.vim/doc) Afterwards, you should be " able to do :help rails " ============================================================================ " Exit quickly when: " - this plugin was already loaded (or disabled) " - when 'compatible' is set if &cp || (exists("g:loaded_rails") && g:loaded_rails) && !(exists("g:rails_debug") && g:rails_debug) finish endif let g:loaded_rails = 1 " Apparently, the nesting level within Vim when the Ruby interface is " initialized determines how much stack space Ruby gets. In previous " versions of rails.vim, sporadic stack overflows occured when omnicomplete " was used. This was apparently due to rails.vim having first initialized " ruby deep in a nested function call. if has("ruby") silent! ruby nil endif " Utility Functions {{{1 function! s:error(str) echohl ErrorMsg echomsg a:str echohl None let v:errmsg = a:str endfunction function! s:autoload(...) if !exists("g:autoloaded_rails") runtime! autoload/rails.vim endif if exists("g:autoloaded_rails") if a:0 exe a:1 endif return 1 endif if !exists("g:rails_no_autoload_warning") let g:rails_no_autoload_warning = 1 call s:error("Disabling rails.vim: autoload/rails.vim is missing") endif return "" endfunction " }}}1 " Configuration {{{ function! s:SetOptDefault(opt,val) if !exists("g:".a:opt) let g:{a:opt} = a:val endif endfunction call s:SetOptDefault("rails_level",3) call s:SetOptDefault("rails_statusline",1) call s:SetOptDefault("rails_syntax",1) call s:SetOptDefault("rails_mappings",1) call s:SetOptDefault("rails_abbreviations",1) call s:SetOptDefault("rails_expensive",1+0*(has("win32")||has("win32unix"))) call s:SetOptDefault("rails_dbext",g:rails_expensive) call s:SetOptDefault("rails_subversion",0) call s:SetOptDefault("rails_default_file","README") call s:SetOptDefault("rails_default_database","") call s:SetOptDefault("rails_root_url",'http://localhost:3000/') call s:SetOptDefault("rails_modelines",1) call s:SetOptDefault("rails_menu",1) call s:SetOptDefault("rails_gnu_screen",1) call s:SetOptDefault("rails_history_size",5) call s:SetOptDefault("rails_debug",0) call s:SetOptDefault("rails_generators","controller\nintegration_test\nmailer\nmigration\nmodel\nobserver\nplugin\nresource\nscaffold\nsession_migration") call s:SetOptDefault("rails_rake_tasks","db:charset\ndb:collation\ndb:create\ndb:create:all\ndb:drop\ndb:drop:all\ndb:fixtures:identify\ndb:fixtures:load\ndb:migrate\ndb:reset\ndb:rollback\ndb:schema:dump\ndb:schema:load\ndb:sessions:clear\ndb:sessions:create\ndb:structure:dump\ndb:test:clone\ndb:test:clone_structure\ndb:test:prepare\ndb:test:purge\ndb:version\ndoc:app\ndoc:clobber_app\ndoc:clobber_plugins\ndoc:clobber_rails\ndoc:plugins\ndoc:rails\ndoc:reapp\ndoc:rerails\nlog:clear\nnotes\nnotes:fixme\nnotes:optimize\nnotes:todo\nrails:freeze:edge\nrails:freeze:gems\nrails:unfreeze\nrails:update\nrails:update:configs\nrails:update:javascripts\nrails:update:scripts\nroutes\nstats\ntest\ntest:functionals\ntest:integration\ntest:plugins\ntest:recent\ntest:uncommitted\ntest:units\ntmp:cache:clear\ntmp:clear\ntmp:create\ntmp:pids:clear\ntmp:sessions:clear\ntmp:sockets:clear") if g:rails_dbext if exists("g:loaded_dbext") && executable("sqlite3") && ! executable("sqlite") " Since dbext can't find it by itself call s:SetOptDefault("dbext_default_SQLITE_bin","sqlite3") endif endif " }}}1 " Detection {{{1 function! s:escvar(r) let r = fnamemodify(a:r,':~') let r = substitute(r,'\W','\="_".char2nr(submatch(0))."_"','g') let r = substitute(r,'^\d','_&','') return r endfunction function! s:Detect(filename) let fn = substitute(fnamemodify(a:filename,":p"),'\c^file://','','') if fn =~ '[\/]config[\/]environment\.rb$' return s:BufInit(strpart(fn,0,strlen(fn)-22)) endif if isdirectory(fn) let fn = fnamemodify(fn,":s?[\/]$??") else let fn = fnamemodify(fn,':s?\(.*\)[\/][^\/]*$?\1?') endif let ofn = "" let nfn = fn while nfn != ofn && nfn != "" if exists("s:_".s:escvar(nfn)) return s:BufInit(nfn) endif let ofn = nfn let nfn = fnamemodify(nfn,':h') endwhile let ofn = "" while fn != ofn if filereadable(fn . "/config/environment.rb") return s:BufInit(fn) endif let ofn = fn let fn = fnamemodify(ofn,':s?\(.*\)[\/]\(app\|config\|db\|doc\|lib\|log\|public\|script\|spec\|test\|tmp\|vendor\)\($\|[\/].*$\)?\1?') endwhile return 0 endfunction function! s:BufInit(path) let s:_{s:escvar(a:path)} = 1 if s:autoload() return RailsBufInit(a:path) endif endfunction " }}}1 " Initialization {{{1 augroup railsPluginDetect autocmd! autocmd BufNewFile,BufRead * call s:Detect(expand(":p")) autocmd VimEnter * if expand("") == "" && !exists("b:rails_root") | call s:Detect(getcwd()) | endif | if exists("b:rails_root") | silent doau User BufEnterRails | endif autocmd FileType netrw if !exists("b:rails_root") | call s:Detect(expand(":p")) | endif | if exists("b:rails_root") | silent doau User BufEnterRails | endif autocmd BufEnter * if exists("b:rails_root")|silent doau User BufEnterRails|endif autocmd BufLeave * if exists("b:rails_root")|silent doau User BufLeaveRails|endif autocmd FileType railslog if s:autoload()|call RailslogSyntax()|endif augroup END command! -bar -bang -nargs=* -complete=dir Rails :if s:autoload()|call RailsNewApp(0,)|endif " }}}1 " Menus {{{1 if !(g:rails_menu && has("menu")) finish endif function! s:sub(str,pat,rep) return substitute(a:str,'\v\C'.a:pat,a:rep,'') endfunction function! s:gsub(str,pat,rep) return substitute(a:str,'\v\C'.a:pat,a:rep,'g') endfunction function! s:CreateMenus() abort if exists("g:rails_installed_menu") && g:rails_installed_menu != "" exe "aunmenu ".s:gsub(g:rails_installed_menu,'\&','') unlet g:rails_installed_menu endif if has("menu") && (exists("g:did_install_default_menus") || exists("$CREAM")) && g:rails_menu if g:rails_menu > 1 let g:rails_installed_menu = '&Rails' else let g:rails_installed_menu = '&Plugin.&Rails' endif if exists("$CREAM") let menucmd = '87anoremenu