diff --git a/lua/ivy/controller.lua b/lua/ivy/controller.lua index 5c26303..6f0caa4 100644 --- a/lua/ivy/controller.lua +++ b/lua/ivy/controller.lua @@ -1,90 +1,94 @@ local window = require "ivy.window" local prompt = require "ivy.prompt" local utils = require "ivy.utils" local controller = {} controller.action = utils.actions controller.items = nil controller.callback = nil controller.run = function(name, items, callback) controller.callback = callback controller.items = items window.initialize() window.set_items { { content = "-- Loading ---" } } vim.api.nvim_buf_set_name(window.get_buffer(), name) controller.input "" end controller.input = function(char) prompt.input(char) controller.update(prompt.text()) end controller.search = function(value) prompt.set(value) controller.update(prompt.text()) end +controller.paste = function() + controller.search(prompt.text() .. vim.fn.getreg "+p") +end + controller.update = function(text) vim.schedule(function() window.set_items(controller.items(text)) vim.cmd "syntax clear IvyMatch" if #text > 0 then -- Escape characters so they do not throw an error when vim tries to use -- the "text" as a regex local escaped_text = string.gsub(text, "([-/\\])", "\\%1") vim.cmd("syntax match IvyMatch '[" .. escaped_text .. "]'") end end) end controller.complete = function(action) vim.api.nvim_set_current_win(window.origin) controller.callback(window.get_current_selection(), action) controller.destroy() end controller.checkpoint = function() vim.api.nvim_set_current_win(window.origin) controller.callback(window.get_current_selection(), controller.action.CHECKPOINT) vim.api.nvim_set_current_win(window.window) end controller.next = function() local max = vim.api.nvim_buf_line_count(window.buffer) - 1 if window.index == max then return end window.index = window.index + 1 window.update() end controller.previous = function() if window.index == 0 then return end window.index = window.index - 1 window.update() end controller.origin = function() return vim.api.nvim_win_get_buf(window.origin) end controller.destroy = function() controller.items = nil controller.callback = nil window.destroy() prompt.destroy() end return controller diff --git a/plugin/ivy.lua b/plugin/ivy.lua index a3a670e..383e866 100644 --- a/plugin/ivy.lua +++ b/plugin/ivy.lua @@ -1,40 +1,50 @@ local controller = require "ivy.controller" -- Put the controller in to the vim global so we can access it in mappings -- better without requires. You can call controller commands like `vim.ivy.xxx`. -- luacheck: ignore vim.ivy = controller local register_backend = function(backend) assert(backend.command, "The backend must have a command") assert(backend.items, "The backend must have a items function") assert(backend.callback, "The backend must have a callback function") local user_command_options = { bang = true } if backend.description ~= nil then user_command_options.desc = backend.description end local name = backend.name or backend.command vim.api.nvim_create_user_command(backend.command, function() vim.ivy.run(name, backend.items, backend.callback) end, user_command_options) if backend.keymap ~= nil then vim.api.nvim_set_keymap("n", backend.keymap, "" .. backend.command .. "", { nowait = true, silent = true }) end end -register_backend(require "ivy.backends.ag") +vim.paste = (function(overridden) + return function(lines, phase) + local file_type = vim.api.nvim_buf_get_option(0, "filetype") + if file_type == "ivy" then + vim.ivy.paste() + else + overridden(lines, phase) + end + end +end)(vim.paste) + register_backend(require "ivy.backends.buffers") register_backend(require "ivy.backends.files") register_backend(require "ivy.backends.lines") register_backend(require "ivy.backends.lsp-workspace-symbols") if vim.fn.executable "rg" then register_backend(require "ivy.backends.rg") elseif vim.fn.executable "ag" then register_backend(require "ivy.backends.ag") end vim.cmd "highlight IvyMatch cterm=bold gui=bold"