Skip to content

Instantly share code, notes, and snippets.

@naquad
Created February 23, 2014 13:25
Show Gist options
  • Save naquad/9171453 to your computer and use it in GitHub Desktop.
Save naquad/9171453 to your computer and use it in GitHub Desktop.
let s:line_cont = '\\\s*$'
"
" lines are something like:
" [
" "/* #!ruby \\",
" " * -w"
" " *",
" " * puts 'Hello, world'"
" "*/"
" ]
"
" and the result of this function is:
"
" {
" "command": "ruby -w",
" "stdin": "
" puts 'Hello, world'
" "
" }
"
" the trick is that stdin text starts at the indentation level of signature
" (#!)
"
" g:partial_command_signature = "#!" by default
" eof = &fileformat == 'dos' ? "\r\n" : "\n"
" s:Error - just a utility function that pretty prints error message
"
function! s:ExtractCommandAndStdin(lines, eof)
let state = 0
let indent = -1
let cmd = {'command': '', 'stdin': ''}
let lidx = 0
let llen = len(a:lines)
while lidx < llen
if state == 0
let indent = stridx(a:lines[lidx], g:partial_command_signature)
if indent != -1
while lidx < llen
let cmd.command .= a:lines[lidx][len(g:partial_command_signature) + indent :]
if match(cmd.command, s:line_cont) == -1
let state = 1
break
endif
let cmd.command = substitute(cmd.command, s:line_cont, '', '')
let lidx += 1
endwhile
if state != 1
call s:Error('unexpected end of command')
return {}
endif
endif
else
let cmd.stdin .= a:lines[lidx][indent :] . a:eof
endif
let lidx += 1
endwhile
if state == 0
call s:Error("can't find command in given range")
return {}
endif
return cmd
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment