eugen0329 / vim-esearch

Perform search in files easily
352 stars 12 forks source link

Apply replace without opening buffers #188

Closed shlomocarmeter closed 3 years ago

shlomocarmeter commented 3 years ago

Hi, This is a great plugin, very nice work.

I'm using neovim (v0.6.0-dev+131-g2c60f7c99) with lua file config, I tried using example from README:

" Save silently and wipeout buffers if they didn't exist. let g:esearch.write_cb = {buf, bang -> buf.write(bang) && (!buf.existed && buf.bwipeout())}

first tried:

vim.cmd([[
  let g:esearch = {}
  let g:esearch.regex   = 1
  let g:esearch.textobj = 0
  let g:esearch.case    = 'smart'
  let g:esearch.default_mappings = 0
  let g:esearch.write_cb = {buf, bang -> buf.write(bang) && (!buf.existed && buf.bwipeout())}

But this gets an error after write confirm:

Error detected while processing function 217_write_cmd[20]..esearch#writer#do[1]..376[33]..1[1]..282[1]..190_bufdo[6].. 284: line 1: E16: Invalid range: 5 buffer!

Then tried config in lua like so:

local function esearch_write_cb(buf)
  -- Not sure what to write here...
  -- vim.cmd('{buf, bang -> buf.write(bang) && (!buf.existed && buf.bwipeout())}')
end

vim.g.esearch = {
  regex = 1
  textobj = 0
  case = 'smart'
  textobj = 0,
  default_mappings = 0,
  write_cb = esearch_write_cb
}

Still no go.

Can you provide an example on how to do this in lua? (Maybe update the README?)

Thanks

eugen0329 commented 3 years ago

Your solution:

vim.g.esearch = {
  regex = 1
  textobj = 0
  case = 'smart'
  textobj = 0,
  default_mappings = 0,
}
vim.cmd('let g:esearch.write_cb = {buf, bang -> buf.write(bang) && (!buf.existed && buf.bwipeout())}')

TLDR buf is a buffer handle to work with them using OOP API and not to deal with commands that are much harder to use. bang is true when :write! is used. With :write it's set to be false. The problem is that neovim still cannot dump viml objects properly to work with them inside lua. E.g.

vim.g.esearch = {write_cb = function(buf, bang) print(vim.inspect(buf)) end}

outputs

{
  bufnr = 3,
  bwipeout = vim.NIL,
  write = vim.NIL,
  -- ...
}

you can see that all the methods are NIL at the moment. The first snippet with your solution is completely legit, but probably in later neovim versions viml and lua will work seamlessly and your code will look like:

vim.g.esearch = {
  -- ...
  write_cb = function(buf, bang) return buf:write(bang) and (not buf.existed and buf:wipeout()) end,
}
shlomocarmeter commented 3 years ago

Thank you for taking the time to reply. Keep up the good work!