Skip to content

Instantly share code, notes, and snippets.

@danr
Created October 22, 2017 11:08
Show Gist options
  • Save danr/eefdcd0bf0bc72af9245debd666116aa to your computer and use it in GitHub Desktop.
Save danr/eefdcd0bf0bc72af9245debd666116aa to your computer and use it in GitHub Desktop.
make a json object and write it to a file
# try for example:
# :expand-write state.json '{"selections":%val{selections},"filetype":%opt{filetype},"yank":%reg{"}}'
def expand -params 1 %{
eval -no-hooks -draft %{
try %{
edit -scratch *expand*
}
}
eval -save-regs s %{
eval -no-hooks -buffer *expand* %{
reg '"' "%arg{1}"
exec -no-hooks '%R'
make-search-and-replace-script
}
eval -no-hooks "%reg{s}"
}
}
def expand-write -params 2 %{
expand %arg{2}
eval -no-hooks -buffer *expand* %{
write %arg{1}
delete-buffer!
}
}
def make-search-and-replace-script %{
reg s ''
exec -no-hooks '%s%\w*?\{.*?\}<ret>'
eval -no-hooks -itersel %{
reg s "replace-eval '%val{selection}' %val{selection};%reg{s}"
}
}
def replace-eval -params 2 %{
eval -no-hooks -save-regs qv %{
set-register q "%arg{1}"
set-register v "%arg{2}"
eval -no-hooks -buffer *expand* %{
exec -no-hooks -save-regs '' '%s\Q<c-r>q<ret>"v<a-R>Zi"<esc>'
try %{ exec -no-hooks 'zs["\\]<ret>i\<esc>' }
try %{ exec -no-hooks 'zs\n<ret>c\n<esc>' }
try %{ exec -no-hooks 'zs\t<ret>c\t<esc>' }
exec -no-hooks 'za"<esc>'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment