From 4490d2a8824c465aa4b94fad6269f68d0d74777f Mon Sep 17 00:00:00 2001 From: Chris Ford Date: Sun, 21 Apr 2013 13:08:55 +0300 Subject: [PATCH] Macroexpand motions Bind cm{motion} to full macro expansion of the form described by the motion (using clojure.core/macroexpand), and bind c1m{motion} to partial macro expansion of the form described by the motion (using clojure.core/macroexpand-1). cmm and c1mm will apply the expansion to the form under the cursor. --- README.markdown | 3 ++- doc/fireplace.txt | 12 ++++++++++++ plugin/fireplace.vim | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 4d285f5..8ebac2a 100644 --- a/README.markdown +++ b/README.markdown @@ -68,7 +68,8 @@ Standard stuff here. `:Eval` evaluates a range (`:%Eval` gets the whole file), `:Require` requires a namespace with `:reload` (`:Require!` does `:reload-all`), either the current buffer or a given argument. There's a `cp` operator that evaluates a given motion (`cpp` for the expression under the -cursor). +cursor). `cm` and `c1m` are similar, but they only run `macroexpand` and +`macroexpand-1` instead of evaluating the form entirely. Any failed evaluation loads the stack trace into the location list, which can be easily accessed with `:lopen`. diff --git a/doc/fireplace.txt b/doc/fireplace.txt index 9d98a05..e04ac4a 100644 --- a/doc/fireplace.txt +++ b/doc/fireplace.txt @@ -139,6 +139,18 @@ c!{motion} Eval/replace the code indicated by {motion}. c!! Eval/replace the inner-most expr at the cursor. + *fireplace-cm* +cm{motion} Fully macroexpand the code indicated by {motion}. + + *fireplace-cmm* +cmm Fully macroexpand the inner-most expr at the cursor. + + *fireplace-c1m* +c1m{motion} Macroexpand the code indicated by {motion} once. + + *fireplace-c1mm* +c1mm Macroexpand the inner-most expr at the cursor once. + *fireplace-cqp* cqp Bring up a prompt for code to eval/print. diff --git a/plugin/fireplace.vim b/plugin/fireplace.vim index 3aa00db..eab27d7 100644 --- a/plugin/fireplace.vim +++ b/plugin/fireplace.vim @@ -538,6 +538,10 @@ function! fireplace#evalprint(expr) abort return fireplace#echo_session_eval(a:expr) endfunction +function! fireplace#macroexpand(fn, form) abort + return fireplace#evalprint('(clojure.core/'.a:fn.' (quote '.a:form.'))') +endfunction + let g:fireplace#reader = \ '(symbol ((fn *vimify [x]' . \ ' (cond' . @@ -613,6 +617,14 @@ function! s:filterop(type) abort endtry endfunction +function! s:macroexpandop(type) abort + call fireplace#macroexpand("macroexpand", s:opfunc(a:type)) +endfunction + +function! s:macroexpand1op(type) abort + call fireplace#macroexpand("macroexpand-1", s:opfunc(a:type)) +endfunction + function! s:printop(type) abort let s:todo = s:opfunc(a:type) call feedkeys("\FireplacePrintLast") @@ -749,6 +761,11 @@ xnoremap FireplacePrint :call printop(visualmode()) FireplaceFilter :set opfunc=filteropg@ xnoremap FireplaceFilter :call filterop(visualmode()) +nnoremap FireplaceMacroExpand :set opfunc=macroexpandopg@ +xnoremap FireplaceMacroExpand :call macroexpandop(visualmode()) +nnoremap FireplaceMacroExpand1 :set opfunc=macroexpand1opg@ +xnoremap FireplaceMacroExpand1 :call macroexpand1op(visualmode()) + nnoremap FireplaceEdit :set opfunc=editopg@ xnoremap FireplaceEdit :call editop(visualmode()) @@ -787,6 +804,11 @@ function! s:setup_eval() abort nmap c! FireplaceFilter nmap c!! FireplaceFilterab + nmap cm FireplaceMacroExpand + nmap cmm FireplaceMacroExpandab + nmap c1m FireplaceMacroExpand1 + nmap c1mm FireplaceMacroExpand1ab + nmap cq FireplaceEdit nmap cqq FireplaceEditab