diff --git a/README.markdown b/README.markdown index 0792fda..1430a0e 100644 --- a/README.markdown +++ b/README.markdown @@ -9,6 +9,7 @@ Enjoy this amalgamation of crap I use for editing runtime files. include guards first. * `:Disarm`: Remove a runtime file's maps, commands, and autocommands, effectively disabling it. +* `:Execute`: Execute part of a Vim script. * `:Scriptnames`: Load `:scriptnames` into the quickfix list. * `:Verbose`: Capture the output of a `:verbose` invocation into the preview window. diff --git a/doc/scriptease.txt b/doc/scriptease.txt index 970f8c0..6faab20 100644 --- a/doc/scriptease.txt +++ b/doc/scriptease.txt @@ -45,6 +45,15 @@ g!{motion} Call |eval()| on the text the motion moves over and an absolute (~/.vim/plugin/scriptease.vim) or runtime-relative (plugin/scriptease.vim) path. + *scriptease-:Execute* +:{range}Execute Execute the VimL given by {range}. This works by + writing the selection to a file and sourcing it. + Script local variables won't work as you expect, + so it's preferable to use |:Runtime| if you can. + +:Execute Execute the top-most VimL structure at the cursor line + (for example, a function definition, or augroup). + *scriptease-:PP* :PP {expr} Pretty print the value of {expr}. diff --git a/plugin/scriptease.vim b/plugin/scriptease.vim index 2aae98b..4b11369 100644 --- a/plugin/scriptease.vim +++ b/plugin/scriptease.vim @@ -295,6 +295,54 @@ endfunction command! -bar Scriptnames call setqflist(s:names())|copen +" }}}1 +" :Execute {{{1 + +if !exists('s:sourceable') + let s:sourceable = tempname().'.vim' +endif + +function! s:execute(first, last, count) abort + if a:count + let lines = getline(a:first, a:last) + else + let first = a:first + while first > 1 && first =~# '^\s\+$' + let first -= 1 + endwhile + if getline(a:first) !~# '^\s' && getline(a:first+1) =~# '^\s' + let first += 1 + elseif getline(a:first) !~# '^\s' && getline(a:first-1) =~# '^\s' + let first -= 1 + endif + let last = first + while first > 1 && getline(first) =~# '^\%(\s\|$\)' + let first -= 1 + endwhile + while last < line('$') && getline(last) =~# '^\%(\s\|$\)' + let last += 1 + endwhile + let lines = getline(first, last) + endif + let len = len(lines) + let id = scriptease#scriptid('%') + if id + call map(lines, 's:gsub(v:val, "\\|s:\\ze\\w+\\s*\\(", "".id."_")') + endif + call writefile(lines, s:sourceable) + let info = len . ' line'. (len==1?'':'s') . ' executed' + if &verbose + echo join(lines, "\n") + endif + return 'source '.s:sourceable.'|echo '.string(info) +endfunction + +augroup scriptease_execute + autocmd! + autocmd FileType vim command! -bar -buffer -range=0 Execute + \ exec s:execute(, , ) +augroup END + " }}}1 " :Runtime {{{1