diff options
| author | Senghan Bright <senghan.bright@deltaprojects.com> | 2020-11-23 11:07:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-23 11:07:53 +0100 |
| commit | 124655608ff727ecee032bb2eb6f44014e8868eb (patch) | |
| tree | 3cb4a1d5b67487da6e44bc943f171687e46b7c82 /lua | |
| parent | 874139ee0b14fedfadca396884f166fc6eb34d4a (diff) | |
Register finder (#275)
builtin: Registers finder. view and edit vim registers.
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/telescope/actions/init.lua | 39 | ||||
| -rw-r--r-- | lua/telescope/builtin/init.lua | 34 | ||||
| -rw-r--r-- | lua/telescope/make_entry.lua | 30 |
3 files changed, 101 insertions, 2 deletions
diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index c6f2f25..9dabfcb 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -169,6 +169,45 @@ actions.set_command_line = function(prompt_bufnr) vim.cmd(entry.value) end +actions.edit_register = function(prompt_bufnr) + local entry = actions.get_selected_entry(prompt_bufnr) + local picker = actions.get_current_picker(prompt_bufnr) + + vim.fn.inputsave() + local updated_value = vim.fn.input("Edit [" .. entry.value .. "] ❯ ", entry.content) + vim.fn.inputrestore() + if updated_value ~= entry.content then + vim.fn.setreg(entry.value, updated_value) + entry.content = updated_value + end + + -- update entry in results table + -- TODO: find way to redraw finder content + for k, v in pairs(picker.finder.results) do + if v == entry then + v.content = updated_value + end + end + -- print(vim.inspect(picker.finder.results)) +end + +actions.paste_register = function(prompt_bufnr) + local entry = actions.get_selected_entry(prompt_bufnr) + + actions.close(prompt_bufnr) + + -- ensure that the buffer can be written to + if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then + print("Paste!") + -- substitute "^V" for "b" + local reg_type = vim.fn.getregtype(entry.value) + if reg_type:byte(1, 1) == 0x16 then + reg_type = "b" .. reg_type:sub(2, -1) + end + vim.api.nvim_put({entry.content}, reg_type, true, true) + end +end + actions.run_builtin = function(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index faef9d6..86055a2 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -788,7 +788,7 @@ builtin.man_pages = function(opts) end pickers.new(opts, { - prompt_tile = 'Man', + prompt_title = 'Man', finder = finders.new_table { results = lines, entry_maker = make_entry.gen_from_apropos(opts), @@ -859,6 +859,38 @@ builtin.marks = function(opts) }):find() end +builtin.registers = function(opts) + opts = opts or {} + + local registers_table = {"\"", "_", "#", "=", "_", "/", "*", "+", ":", ".", "%"} + + -- named + for i = 0, 9 do + table.insert(registers_table, tostring(i)) + end + + -- alphabetical + for i = 65, 90 do + table.insert(registers_table, string.char(i)) + end + + pickers.new(opts,{ + prompt_title = 'Registers', + finder = finders.new_table { + results = registers_table, + entry_maker = make_entry.gen_from_registers(opts), + }, + -- use levenshtein as n-gram doesn't support <2 char matches + sorter = sorters.get_levenshtein_sorter(), + attach_mappings = function(_, map) + map('i', '<CR>', actions.paste_register) + map('i', '<C-e>', actions.edit_register) + + return true + end, + }):find() +end + -- find normal mode mappings builtin.keymaps = function(opts) opts = opts or {} diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index e63fef2..aa1d3db 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -453,6 +453,33 @@ function make_entry.gen_from_marks(_) end end +function make_entry.gen_from_registers(_) + local displayer = entry_display.create { + separator = ":", + items = { + { width = 4 }, + { remaining = true }, + }, + } + + local make_display = function(entry) + return displayer { + string.format("[%s]", entry.value), + entry.content + } + end + + return function(entry) + return { + valid = true, + value = entry, + ordinal = entry, + content = vim.fn.getreg(entry), + display = make_display + } + end +end + function make_entry.gen_from_highlights() return function(entry) local make_display = function(entry) @@ -475,11 +502,13 @@ function make_entry.gen_from_highlights() ordinal = entry, preview_command = preview_command + } end end function make_entry.gen_from_vimoptions() + -- TODO: Can we just remove this from `options.lua`? function N_(s) return s @@ -546,7 +575,6 @@ function make_entry.gen_from_vimoptions() end end - -- TODO: don't call this 'line' local displayer = entry_display.create { separator = "│", items = { |
