From f65f5ed530172aa083e5e5521acc0f16c9374ecd Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Sun, 9 Feb 2025 14:37:07 +0300 Subject: [PATCH] Add support for setting filetypes --- README.md | 22 +++++++++++++++++++--- doc/picvim.txt | 14 ++++++++++++++ lua/picvim.lua | 12 ++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6585ec0..f17ee95 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ To use PicVim, simply open an image file in Neovim and the image will be display - Rotate the image with configurable keybindings. - Pan the image using arrow keys or specific keybindings. - Automatically scale and adjust images for optimal viewing. +- Configure keybindings and image types to work with your liking. ## ⚙️ Installation @@ -64,6 +65,13 @@ You can set custom keymaps using the `keymap` option. ```lua require'picvim'.setup({ + filetypes = { -- Default filetypes + "png", -- For now only these are supported: + "jpg", -- > PNG, JPG, JPEG, GIF, BMP + "jpeg", + "gif", -- No need to set these if you want to + "bmp", -- support all of these image formats. + } keymap = { -- Default keymaps move_left = { "", "h" }, -- Pan left move_right = { "", "l" }, -- Pan right @@ -81,9 +89,9 @@ require'picvim'.setup({ ## 🛠️ 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) (configured in the `filetypes` option) upon opening. It sets the buffer to a "non-file" type to display the image correctly. -## ⌨️ Default Keybindings +## ⌨️ Default Keymaps | Options within `keymap` | Default Value | Description | |-----------------------------|-----------------------|--------------------------| @@ -98,6 +106,14 @@ The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bm | `reset` | `"o"` | Resets the image | | `rerender` | `"r"` | Rerenders the image | +## Filetypes + +Defaults to: +- `{ "png", "jpg", "jpeg", "gif", "bmp" }` + +- You can set custom filetypes using the `filetypes` option. + - The value must be a table of strings that includes the file extension for the image. + - Filetypes not in the default list will not work (They'll be ignored). ## 📦 Dependencies @@ -112,9 +128,9 @@ The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bm - [x] Support image rotation. - [x] Add support for non-png raster images. - [x] Make Keybinds configurable. +- [x] Add config for filetypes to work with. - [ ] Add support for svg files. - [ ] Add PDF viewer. - [ ] Add support for gif animations. -- [ ] Add config for filetypes to work with. - [ ] Expose functions to handle the images. diff --git a/doc/picvim.txt b/doc/picvim.txt index fc329d5..b460bc1 100644 --- a/doc/picvim.txt +++ b/doc/picvim.txt @@ -40,6 +40,13 @@ Use your preferred plugin manager, e.g., with lazy.nvim: The plugin can be configured using: >lua require'picvim'.setup({ + filetypes = { -- Default filetypes + "png", -- For now only these are supported: + "jpg", -- > PNG, JPG, JPEG, GIF, BMP + "jpeg", + "gif", -- No need to set these if you want to + "bmp", -- support all of these image formats. + } keymap = { move_left = { "", "h" }, -- Pan left move_right = { "", "l" }, -- Pan right @@ -69,6 +76,13 @@ The plugin can be configured using: │ `rerender` │ `"r"` │ Rerenders the image │ └─────────────────────────┴──────────────────┴──────────────────────────┘ +Filetypes defaults to: +- `{ "png", "jpg", "jpeg", "gif", "bmp" }` + +- You can set custom filetypes using the `filetypes` option. + - The value must be a table of strings. + - Filetypes not in the default list will not work (They'll be ignored). + ============================================================================== 4. *USAGE* diff --git a/lua/picvim.lua b/lua/picvim.lua index 1f0d4c7..78be1cd 100644 --- a/lua/picvim.lua +++ b/lua/picvim.lua @@ -133,8 +133,16 @@ end function M.setup(config) local opts = config or {} + local allowed_types = { png = true, jpg = true, jpeg = true, gif = true, bmp = true } + local filetypes = {} + for _, ft in ipairs(opts.filetypes or { "png", "jpg", "jpeg", "gif", "bmp" }) do + if allowed_types[ft] then + table.insert(filetypes, ft) + end + end + local formatted = "*." .. table.concat(filetypes, ",*.") vim.api.nvim_create_autocmd("BufEnter", { - pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp", + pattern = formatted, callback = function() if not vim.b.img then vim.b.img = Image:new(vim.fn.expand("%:p")) @@ -257,7 +265,7 @@ function M.setup(config) end, }) vim.api.nvim_create_autocmd("VimResized", { - pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp", + pattern = formatted, callback = function() if not vim.b.img then vim.b.img = Image:new(vim.fn.expand("%:p"))