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:
```lua
{
"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
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
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.
l, Right Arrow Pan right.
j, Down Arrow Pan down.
k, Up Arrow Pan up.
=, + Zoom in.
- Zoom out.
-, _ Zoom out.
t Rotate clockwise (30 degrees).
T Rotate counterclockwise (30 degrees).
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] Support image rotation.
- [x] Add support for non-png raster images.
- [x] Make Keybinds configurable.
- [ ] Add support for svg files.
- [ ] Make Keybinds configurable.
- [ ] Expose functions to handle the images.

View File

@@ -131,7 +131,8 @@ function Image:pngify()
end
end
function M.setup()
function M.setup(config)
local opts = config or {}
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp",
callback = function()
@@ -185,69 +186,72 @@ function M.setup()
debounce_timer = nil
end, debounce_interval)
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
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "<Right>", function()
end,
move_right = function()
keypress_state.o_x = keypress_state.o_x + 30
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "<Down>", function()
end,
move_down = function()
keypress_state.o_y = keypress_state.o_y + 30
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "<Up>", function()
end,
move_up = function()
keypress_state.o_y = keypress_state.o_y - 30
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "h", 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()
end,
zoom_in = 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
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "-", function()
end,
zoom_out = function()
keypress_state.zoom = keypress_state.zoom - 0.2
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "t", function()
end,
rotate_clockwise = function()
keypress_state.rotation = keypress_state.rotation + 30
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "T", function()
end,
rotate_counterclockwise = function()
keypress_state.rotation = keypress_state.rotation - 30
schedule_redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "o", function()
end,
reset = function()
local image = vim.b.img
image.properties.o_x = 0
image.properties.o_y = 0
image.properties.rotation = 0
vim.b.img = image
redraw()
end, { buffer = buf, noremap = true, silent = true })
vim.keymap.set("n", "r", function()
end,
rerender = function()
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
redraw()
end,