Skip to content

Instantly share code, notes, and snippets.

@tacahiroy
Created February 15, 2013 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tacahiroy/4961768 to your computer and use it in GitHub Desktop.
Save tacahiroy/4961768 to your computer and use it in GitHub Desktop.
Chef's attribute notation converter
let s:chef = {}
function! s:chef.convert_attr_notation() abort
if !search('\>', 'bcnW')
return
endif
let [bufnbr, lnum, col, offset] = getpos('.')
let line = getline(lnum)
if match(line, '\.') == -1
return
endif
let sc = self.find_attr_pos()
let ec = col
let l = matchstr(line, printf('.*\%%<%dc', sc))
let r = matchstr(line, printf('\%%>%dc.*', ec))
let attrs = split(matchstr(line, printf('\%%%dc.\+\%%%dc', sc - 1, ec + 1)), '\.')
let attr = attrs[0] . join(map(attrs[1:], '"[:".v:val."]"'), '')
call setline(lnum, l . attr . r)
call setpos('.', [bufnbr, lnum, len(l . attr) + 1, 0])
execute 'startinsert'
endfunction
function! s:chef.find_attr_pos()
let pos = searchpos('\(^\|\s\)', 'bnW')
return pos[1] + 1
endfunction
command! ChefConvertAttrNotation call s:chef.convert_attr_notation()
inoremap <silent> <C-y>x <C-g>u<Esc>:ChefConvertAttrNotation<Cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment