Add keymaps support
This commit is contained in:
27
README.md
27
README.md
@@ -48,6 +48,7 @@ Use your prefferred package manager to install PicVim.
|
|||||||
|
|
||||||
If you use [lazy.nvim](https://github.com/folke/lazy.nvim), you can install PicVim by adding the following to your configuration:
|
If you use [lazy.nvim](https://github.com/folke/lazy.nvim), you can install PicVim by adding the following to your configuration:
|
||||||
|
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
{
|
{
|
||||||
"Toprun123/picvim",
|
"Toprun123/picvim",
|
||||||
@@ -57,26 +58,40 @@ If you use [lazy.nvim](https://github.com/folke/lazy.nvim), you can install PicV
|
|||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🔧 Setup
|
## 🔧 Setup and Configuration
|
||||||
|
|
||||||
To activate the plugin, add the following to your init.lua configuration file:
|
To configure the plugin, add the following to your init.lua or any other configuration file:
|
||||||
|
You can set custom keymaps using the `keymap` option.
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
require'picvim'.setup()
|
require'picvim'.setup({
|
||||||
|
keymap = { -- Default keymaps
|
||||||
|
move_left = { "<Left>", "h" }, -- Pan left
|
||||||
|
move_right = { "<Right>", "l" }, -- Pan right
|
||||||
|
move_down = { "<Down>", "j" }, -- Pan down
|
||||||
|
move_up = { "<Up>", "k" }, -- Pan up
|
||||||
|
zoom_in = { "=", "+" }, -- Zoom in
|
||||||
|
zoom_out = { "-", "_" }, -- Zoom out
|
||||||
|
rotate_clockwise = "t", -- Rotate clockwise by 30 degrees
|
||||||
|
rotate_counterclockwise = "T", -- Rotate counterclockwise by 30 degrees
|
||||||
|
reset = "o", -- Reset image
|
||||||
|
rerender = "r", -- Rerender image
|
||||||
|
}
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🛠️ Autocommands
|
## 🛠️ Autocommands
|
||||||
|
|
||||||
The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bmp) upon opening. It sets the buffer to a "non-file" type to display the image correctly.
|
The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bmp) upon opening. It sets the buffer to a "non-file" type to display the image correctly.
|
||||||
|
|
||||||
## ⌨️ Keybindings
|
## ⌨️ DefaultKeybindings
|
||||||
|
|
||||||
h, Left Arrow – Pan left.
|
h, Left Arrow – Pan left.
|
||||||
l, Right Arrow – Pan right.
|
l, Right Arrow – Pan right.
|
||||||
j, Down Arrow – Pan down.
|
j, Down Arrow – Pan down.
|
||||||
k, Up Arrow – Pan up.
|
k, Up Arrow – Pan up.
|
||||||
=, + – Zoom in.
|
=, + – Zoom in.
|
||||||
- – Zoom out.
|
-, _ – Zoom out.
|
||||||
t – Rotate clockwise (30 degrees).
|
t – Rotate clockwise (30 degrees).
|
||||||
T – Rotate counterclockwise (30 degrees).
|
T – Rotate counterclockwise (30 degrees).
|
||||||
o – Reset image position and rotation.
|
o – Reset image position and rotation.
|
||||||
@@ -94,7 +109,7 @@ The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bm
|
|||||||
- [x] Allow panning across the image.
|
- [x] Allow panning across the image.
|
||||||
- [x] Support image rotation.
|
- [x] Support image rotation.
|
||||||
- [x] Add support for non-png raster images.
|
- [x] Add support for non-png raster images.
|
||||||
|
- [x] Make Keybinds configurable.
|
||||||
- [ ] Add support for svg files.
|
- [ ] Add support for svg files.
|
||||||
- [ ] Make Keybinds configurable.
|
|
||||||
- [ ] Expose functions to handle the images.
|
- [ ] Expose functions to handle the images.
|
||||||
|
|
||||||
|
|||||||
@@ -131,7 +131,8 @@ function Image:pngify()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup()
|
function M.setup(config)
|
||||||
|
local opts = config or {}
|
||||||
vim.api.nvim_create_autocmd("BufEnter", {
|
vim.api.nvim_create_autocmd("BufEnter", {
|
||||||
pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp",
|
pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp",
|
||||||
callback = function()
|
callback = function()
|
||||||
@@ -185,69 +186,72 @@ function M.setup()
|
|||||||
debounce_timer = nil
|
debounce_timer = nil
|
||||||
end, debounce_interval)
|
end, debounce_interval)
|
||||||
end
|
end
|
||||||
vim.keymap.set("n", "<Left>", function()
|
local keymaps = {
|
||||||
|
move_left = { "<Left>", "h" },
|
||||||
|
move_right = { "<Right>", "l" },
|
||||||
|
move_down = { "<Down>", "j" },
|
||||||
|
move_up = { "<Up>", "k" },
|
||||||
|
zoom_in = { "=", "+" },
|
||||||
|
zoom_out = { "-", "_" },
|
||||||
|
rotate_clockwise = "t",
|
||||||
|
rotate_counterclockwise = "T",
|
||||||
|
reset = "o",
|
||||||
|
rerender = "r",
|
||||||
|
}
|
||||||
|
local actions = {
|
||||||
|
move_left = function()
|
||||||
keypress_state.o_x = keypress_state.o_x - 30
|
keypress_state.o_x = keypress_state.o_x - 30
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "<Right>", function()
|
move_right = function()
|
||||||
keypress_state.o_x = keypress_state.o_x + 30
|
keypress_state.o_x = keypress_state.o_x + 30
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "<Down>", function()
|
move_down = function()
|
||||||
keypress_state.o_y = keypress_state.o_y + 30
|
keypress_state.o_y = keypress_state.o_y + 30
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "<Up>", function()
|
move_up = function()
|
||||||
keypress_state.o_y = keypress_state.o_y - 30
|
keypress_state.o_y = keypress_state.o_y - 30
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "h", function()
|
zoom_in = function()
|
||||||
keypress_state.o_x = keypress_state.o_x - 30
|
|
||||||
schedule_redraw()
|
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
|
||||||
vim.keymap.set("n", "l", function()
|
|
||||||
keypress_state.o_x = keypress_state.o_x + 30
|
|
||||||
schedule_redraw()
|
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
|
||||||
vim.keymap.set("n", "j", function()
|
|
||||||
keypress_state.o_y = keypress_state.o_y + 30
|
|
||||||
schedule_redraw()
|
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
|
||||||
vim.keymap.set("n", "k", function()
|
|
||||||
keypress_state.o_y = keypress_state.o_y - 30
|
|
||||||
schedule_redraw()
|
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
|
||||||
vim.keymap.set("n", "=", function()
|
|
||||||
keypress_state.zoom = keypress_state.zoom + 0.2
|
keypress_state.zoom = keypress_state.zoom + 0.2
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "+", function()
|
zoom_out = function()
|
||||||
keypress_state.zoom = keypress_state.zoom + 0.2
|
|
||||||
schedule_redraw()
|
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
|
||||||
vim.keymap.set("n", "-", function()
|
|
||||||
keypress_state.zoom = keypress_state.zoom - 0.2
|
keypress_state.zoom = keypress_state.zoom - 0.2
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "t", function()
|
rotate_clockwise = function()
|
||||||
keypress_state.rotation = keypress_state.rotation + 30
|
keypress_state.rotation = keypress_state.rotation + 30
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "T", function()
|
rotate_counterclockwise = function()
|
||||||
keypress_state.rotation = keypress_state.rotation - 30
|
keypress_state.rotation = keypress_state.rotation - 30
|
||||||
schedule_redraw()
|
schedule_redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "o", function()
|
reset = function()
|
||||||
local image = vim.b.img
|
local image = vim.b.img
|
||||||
image.properties.o_x = 0
|
image.properties.o_x = 0
|
||||||
image.properties.o_y = 0
|
image.properties.o_y = 0
|
||||||
image.properties.rotation = 0
|
image.properties.rotation = 0
|
||||||
vim.b.img = image
|
vim.b.img = image
|
||||||
redraw()
|
redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
vim.keymap.set("n", "r", function()
|
rerender = function()
|
||||||
redraw()
|
redraw()
|
||||||
end, { buffer = buf, noremap = true, silent = true })
|
end,
|
||||||
|
}
|
||||||
|
for action, default_keys in pairs(keymaps) do
|
||||||
|
local keys = opts.keymap and opts.keymap[action] or default_keys
|
||||||
|
if type(keys) ~= "table" then
|
||||||
|
keys = { keys }
|
||||||
|
end
|
||||||
|
for _, key in ipairs(keys) do
|
||||||
|
vim.keymap.set("n", key, actions[action], { buffer = buf, noremap = true, silent = true })
|
||||||
|
end
|
||||||
|
end
|
||||||
vim.b.no_git_diff = true
|
vim.b.no_git_diff = true
|
||||||
redraw()
|
redraw()
|
||||||
end,
|
end,
|
||||||
|
|||||||
Reference in New Issue
Block a user