clojure-vim / acid.nvim

Asynchronous Clojure Interactive Development
161 stars 12 forks source link

Feature request: a command to evaluate a string of input (i.e programmatic `cqp`) #49

Closed daveyarwood closed 5 years ago

daveyarwood commented 5 years ago

It isn't clear to me how to use cqp in a programmatic (i.e. non-interactive) way.

It would be nice if we had a command called something like :AcidEval that I could call like :AcidEval (+ 1 2 3) and it would be the equivalent of typing cqp(+ 1 2 3)<CR>.

Having that would make it a lot easier for us to add custom keybindings to our vimrc's that do arbitrary things useful to our individual workflows, like refresh, e.g. nmap <leader>rr :AcidEval (refresh)<CR>

hkupty commented 5 years ago

:AcidEval would be very similar to running :call luaeval('require("acid.features").eval_print(_A)', <code>). I'm usually reluctant in adding a command that's just an alias for calling a lua function, unless it's painful to do the latter.

Would using the lua API be an option?

Also, one could use any desired combination of lua commands and handlers if going directly with the API (though that is surely more advanced):

function! MyAcidHandler()
lua << EOF
local acid = require('acid')
local eval = require('acid.ops').eval
local ns = vim.api.nvim_call_function("AcidGetNs", {})

local my_handler = function(data)
   vim.api.nvim_out_write("[My-Handler] " .. data.out or data.value)
end

acid.run(eval{code = "(refresh)", ns = ns}:with_handler(my_handler))
EOF
endfunction

map <keymap> <Cmd>call MyAcidHandler()<CR>
daveyarwood commented 5 years ago

I can understand your hesitance to add commands for things that can already be done by calling directly into functions. I'm already used to writing my own commands in my vimrc that wrap functions exposed by plugins.

I think dipping into Lua might be too much to ask of Vim plugin users, because it makes the model more complex. Now we're not just working in Vimscript, calling functions provided by plugins. Rather, we are eval-ing strings of Lua code, and we might have to worry about string escaping, etc. To make things easier on consumers in the Vim ecosystem, I would strongly consider exposing Vimscript functions (if not commands) for all the bits of common functionality that will be generally useful for the end user.

hkupty commented 5 years ago

You raised a valid point.

I believe eval is a fundamentally basic feature then that is ok to have a 'pass-through' command for it. There might be more.

I'll add that.

hkupty commented 5 years ago

Added! :AcidEval (your fn call) and :call AcidEval("(your fn call)") should work.

daveyarwood commented 5 years ago

Awesome, thanks a lot!

hkupty commented 5 years ago

My pleasure :smiley: