Add keymaps support

This commit is contained in:
2025-02-08 17:02:43 +03:00
parent b71ae6db76
commit d50b1b1807
2 changed files with 89 additions and 70 deletions

View File

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

View File

@@ -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 = {
keypress_state.o_x = keypress_state.o_x - 30 move_left = { "<Left>", "h" },
schedule_redraw() move_right = { "<Right>", "l" },
end, { buffer = buf, noremap = true, silent = true }) move_down = { "<Down>", "j" },
vim.keymap.set("n", "<Right>", function() move_up = { "<Up>", "k" },
keypress_state.o_x = keypress_state.o_x + 30 zoom_in = { "=", "+" },
schedule_redraw() zoom_out = { "-", "_" },
end, { buffer = buf, noremap = true, silent = true }) rotate_clockwise = "t",
vim.keymap.set("n", "<Down>", function() rotate_counterclockwise = "T",
keypress_state.o_y = keypress_state.o_y + 30 reset = "o",
schedule_redraw() rerender = "r",
end, { buffer = buf, noremap = true, silent = true }) }
vim.keymap.set("n", "<Up>", function() local actions = {
keypress_state.o_y = keypress_state.o_y - 30 move_left = function()
schedule_redraw() keypress_state.o_x = keypress_state.o_x - 30
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "h", function() end,
keypress_state.o_x = keypress_state.o_x - 30 move_right = function()
schedule_redraw() keypress_state.o_x = keypress_state.o_x + 30
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "l", function() end,
keypress_state.o_x = keypress_state.o_x + 30 move_down = function()
schedule_redraw() keypress_state.o_y = keypress_state.o_y + 30
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "j", function() end,
keypress_state.o_y = keypress_state.o_y + 30 move_up = function()
schedule_redraw() keypress_state.o_y = keypress_state.o_y - 30
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "k", function() end,
keypress_state.o_y = keypress_state.o_y - 30 zoom_in = function()
schedule_redraw() keypress_state.zoom = keypress_state.zoom + 0.2
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "=", function() end,
keypress_state.zoom = keypress_state.zoom + 0.2 zoom_out = function()
schedule_redraw() keypress_state.zoom = keypress_state.zoom - 0.2
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "+", function() end,
keypress_state.zoom = keypress_state.zoom + 0.2 rotate_clockwise = function()
schedule_redraw() keypress_state.rotation = keypress_state.rotation + 30
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "-", function() end,
keypress_state.zoom = keypress_state.zoom - 0.2 rotate_counterclockwise = function()
schedule_redraw() keypress_state.rotation = keypress_state.rotation - 30
end, { buffer = buf, noremap = true, silent = true }) schedule_redraw()
vim.keymap.set("n", "t", function() end,
keypress_state.rotation = keypress_state.rotation + 30 reset = function()
schedule_redraw() local image = vim.b.img
end, { buffer = buf, noremap = true, silent = true }) image.properties.o_x = 0
vim.keymap.set("n", "T", function() image.properties.o_y = 0
keypress_state.rotation = keypress_state.rotation - 30 image.properties.rotation = 0
schedule_redraw() vim.b.img = image
end, { buffer = buf, noremap = true, silent = true }) redraw()
vim.keymap.set("n", "o", function() end,
local image = vim.b.img rerender = function()
image.properties.o_x = 0 redraw()
image.properties.o_y = 0 end,
image.properties.rotation = 0 }
vim.b.img = image for action, default_keys in pairs(keymaps) do
redraw() local keys = opts.keymap and opts.keymap[action] or default_keys
end, { buffer = buf, noremap = true, silent = true }) if type(keys) ~= "table" then
vim.keymap.set("n", "r", function() keys = { keys }
redraw() end
end, { buffer = buf, noremap = true, silent = true }) 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,