From d50b1b1807f8178eada3a82f7a9969fa674dd89b Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Sat, 8 Feb 2025 17:02:43 +0300 Subject: [PATCH] Add keymaps support --- README.md | 27 +++++++--- lua/picvim.lua | 132 +++++++++++++++++++++++++------------------------ 2 files changed, 89 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 32c4ce5..026948e 100644 --- a/README.md +++ b/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: + ```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 = { "", "h" }, -- Pan left + move_right = { "", "l" }, -- Pan right + move_down = { "", "j" }, -- Pan down + move_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. diff --git a/lua/picvim.lua b/lua/picvim.lua index 72bbb4d..1f0d4c7 100644 --- a/lua/picvim.lua +++ b/lua/picvim.lua @@ -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", "", function() - keypress_state.o_x = keypress_state.o_x - 30 - schedule_redraw() - end, { buffer = buf, noremap = true, silent = true }) - vim.keymap.set("n", "", function() - keypress_state.o_x = keypress_state.o_x + 30 - schedule_redraw() - end, { buffer = buf, noremap = true, silent = true }) - vim.keymap.set("n", "", 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.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() - 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() - keypress_state.zoom = keypress_state.zoom - 0.2 - schedule_redraw() - end, { buffer = buf, noremap = true, silent = true }) - vim.keymap.set("n", "t", function() - keypress_state.rotation = keypress_state.rotation + 30 - schedule_redraw() - end, { buffer = buf, noremap = true, silent = true }) - vim.keymap.set("n", "T", function() - keypress_state.rotation = keypress_state.rotation - 30 - schedule_redraw() - end, { buffer = buf, noremap = true, silent = true }) - vim.keymap.set("n", "o", 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() - redraw() - end, { buffer = buf, noremap = true, silent = true }) + local keymaps = { + move_left = { "", "h" }, + move_right = { "", "l" }, + move_down = { "", "j" }, + move_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, + move_right = function() + keypress_state.o_x = keypress_state.o_x + 30 + schedule_redraw() + end, + move_down = function() + keypress_state.o_y = keypress_state.o_y + 30 + schedule_redraw() + end, + move_up = function() + keypress_state.o_y = keypress_state.o_y - 30 + schedule_redraw() + end, + zoom_in = function() + keypress_state.zoom = keypress_state.zoom + 0.2 + schedule_redraw() + end, + zoom_out = function() + keypress_state.zoom = keypress_state.zoom - 0.2 + schedule_redraw() + end, + rotate_clockwise = function() + keypress_state.rotation = keypress_state.rotation + 30 + schedule_redraw() + end, + rotate_counterclockwise = function() + keypress_state.rotation = keypress_state.rotation - 30 + schedule_redraw() + 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, + rerender = function() + redraw() + 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,