Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Created December 26, 2011 10:14
Show Gist options
  • Select an option

  • Save AndrewRadev/1520854 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewRadev/1520854 to your computer and use it in GitHub Desktop.
Edit various embedded scripts in vimscript with the inline_edit.vim plugin
function! inline_edit#VimEmbeddedScript()
let start_pattern = '^\s*\(\%(rub\|py\|pe\|mz\|lua\)\S*\)\s*<<\s*\(.*\)$'
" If we don't find the start by going backwards, bail out.
if search(start_pattern, 'Wb') <= 0
return []
endif
" The start of the code will be the line after the one we just found
let start = line('.') + 1
" Indent will be the same as the initial line
let indent = indent(line('.'))
" The filetype to set would be derived from the first capture group, the
" delimiter -- the second one.
let language = substitute(getline('.'), start_pattern, '\1', '')
let delimiter = substitute(getline('.'), start_pattern, '\2', '')
" The filetype to set to the new buffer is decided from the language
" command.
if language =~ '^rub'
let sub_filetype = 'ruby'
elseif language =~ '^py'
let sub_filetype = 'python'
elseif language =~ '^pe'
let sub_filetype = 'perl'
elseif language =~ '^mz'
let sub_filetype = 'scheme'
elseif language == 'lua'
let sub_filetype = 'lua'
endif
" The delimiter could be blank. In this case, it defaults to "."
if len(delimiter) == 0
let delimiter = '.'
endif
" Search forward for a line that contains only the delimiter, not even with
" any whitespace around it.
"
" In the pattern, "\V" is used to match the delimiter literally, just in
" case it contains any special symbols.
"
" If nothing is found, bail out at this point.
if search('^\V'.delimiter.'\$', 'W') <= 0
return []
endif
" The end of the embedded code is one line above this one.
let end = line('.') - 1
" Return the data and let the plugin handle the rest.
return [start, end, sub_filetype, indent]
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment