Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created September 25, 2013 07:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alpaca-tc/6696152 to your computer and use it in GitHub Desktop.
Save alpaca-tc/6696152 to your computer and use it in GitHub Desktop.
function! s:separate_defenition_to_each_filetypes(ft_dictionary) "{{{
let result = {}
for [filetypes, value] in items(a:ft_dictionary)
for ft in split(filetypes, ",")
if !has_key(result, ft)
let result[ft] = []
endif
call extend(result[ft], copy(value))
endfor
endfor
return result
endfunction"}}}
" ------------------------------------
" switch.vim
" ------------------------------------
nnoremap ! :Switch<CR>
let s:switch_definition = {
\ '*': [
\ ['is', 'are']
\ ],
\ 'ruby,eruby,haml' : [
\ ['if', 'unless'],
\ ['while', 'until'],
\ ['.blank?', '.present?'],
\ ['include', 'extend'],
\ ['class', 'module'],
\ ['.inject', '.delete_if'],
\ ['.map', '.map!'],
\ ['attr_accessor', 'attr_reader', 'attr_writer'],
\ ],
\ 'Gemfile,Berksfile' : [
\ ['=', '<', '<=', '>', '>=', '~>'],
\ ],
\ 'ruby.application_template' : [
\ ['yes?', 'no?'],
\ ['lib', 'initializer', 'file', 'vendor', 'rakefile'],
\ ['controller', 'model', 'view', 'migration', 'scaffold'],
\ ],
\ 'erb,html,php' : [
\ { '<!--\([a-zA-Z0-9 /]\+\)--></\(div\|ul\|li\|a\)>' : '</\2><!--\1-->' },
\ ],
\ 'rails' : [
\ [100, ':continue', ':information'],
\ [101, ':switching_protocols'],
\ [102, ':processing'],
\ [200, ':ok', ':success'],
\ [201, ':created'],
\ [202, ':accepted'],
\ [203, ':non_authoritative_information'],
\ [204, ':no_content'],
\ [205, ':reset_content'],
\ [206, ':partial_content'],
\ [207, ':multi_status'],
\ [208, ':already_reported'],
\ [226, ':im_used'],
\ [300, ':multiple_choices'],
\ [301, ':moved_permanently'],
\ [302, ':found'],
\ [303, ':see_other'],
\ [304, ':not_modified'],
\ [305, ':use_proxy'],
\ [306, ':reserved'],
\ [307, ':temporary_redirect'],
\ [308, ':permanent_redirect'],
\ [400, ':bad_request'],
\ [401, ':unauthorized'],
\ [402, ':payment_required'],
\ [403, ':forbidden'],
\ [404, ':not_found'],
\ [405, ':method_not_allowed'],
\ [406, ':not_acceptable'],
\ [407, ':proxy_authentication_required'],
\ [408, ':request_timeout'],
\ [409, ':conflict'],
\ [410, ':gone'],
\ [411, ':length_required'],
\ [412, ':precondition_failed'],
\ [413, ':request_entity_too_large'],
\ [414, ':request_uri_too_long'],
\ [415, ':unsupported_media_type'],
\ [416, ':requested_range_not_satisfiable'],
\ [417, ':expectation_failed'],
\ [422, ':unprocessable_entity'],
\ [423, ':precondition_required'],
\ [424, ':too_many_requests'],
\ [426, ':request_header_fields_too_large'],
\ [500, ':internal_server_error'],
\ [501, ':not_implemented'],
\ [502, ':bad_gateway'],
\ [503, ':service_unavailable'],
\ [504, ':gateway_timeout'],
\ [505, ':http_version_not_supported'],
\ [506, ':variant_also_negotiates'],
\ [507, ':insufficient_storage'],
\ [508, ':loop_detected'],
\ [510, ':not_extended'],
\ [511, ':network_authentication_required'],
\ ],
\ 'rspec': [
\ ['describe', 'context', 'specific', 'example'],
\ ['before', 'after'],
\ ['be_true', 'be_false'],
\ ['get', 'post', 'put', 'delete'],
\ ['==', 'eql', 'equal'],
\ { '\.should_not': '\.should' },
\ ['\.to_not', '\.to'],
\ { '\([^. ]\+\)\.should\(_not\|\)': 'expect(\1)\.to\2' },
\ { 'expect(\([^. ]\+\))\.to\(_not\|\)': '\1.should\2' },
\ ],
\ 'markdown' : [
\ ['[ ]', '[x]']
\ ]
\ }
let s:switch_definition = s:separate_defenition_to_each_filetypes(s:switch_definition)
function! s:define_switch_mappings() "{{{
if exists('b:switch_custom_definitions')
unlet b:switch_custom_definitions
endif
let dictionary = []
for filetype in split(&ft, '\.')
if has_key(s:switch_definition, filetype)
let dictionary = extend(dictionary, s:switch_definition[filetype])
endif
endfor
if exists('b:rails_root')
let dictionary = extend(dictionary, s:switch_definition['rails'])
endif
if has_key(s:switch_definition, '*')
let dictionary = extend(dictionary, s:switch_definition['*'])
endif
if !empty('dictionary')
call alpaca#let_b:('switch_custom_definitions', dictionary)
endif
endfunction"}}}
augroup SwitchSetting
autocmd!
autocmd Filetype * if !empty(split(&ft, '\.')) | call <SID>define_switch_mappings() | endif
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment