Register finder (#275)

builtin: Registers finder. view and edit vim registers.
This commit is contained in:
Senghan Bright
2020-11-23 11:07:53 +01:00
committed by GitHub
parent 874139ee0b
commit 124655608f
4 changed files with 102 additions and 2 deletions

View File

@@ -374,6 +374,7 @@ Built-in function ready to be bound to any key you like :smile:.
| `builtin.loclist` | Lists items from current window's location list. |
| `builtin.reloader` | Lists lua modules and reload them on enter. |
| `builtin.vim_options` | Lists vim options and on enter edit the options value. |
| `builtin.registers` | Lists vim registers and edit or paste selection. |
| `builtin.keymaps` | Lists normal-mode mappings. |
| `builtin.filetypes` | Lists all filetypes. |
| `builtin.highlights` | Lists all highlights. |

View File

@@ -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)

View File

@@ -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 {}

View File

@@ -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 = {