Skip to content

Commit

Permalink
feat: hijack image file patterns #56
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Oct 22, 2023
1 parent 431235a commit 7b475c4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ It works wonderfully with Kitty + Tmux, and it handles painful things like rende
at a given position in a buffer, scrolling, windows, etc.

It has built-in Markdown and Neorg integrations that you can use right now.
\
It can also render image files as images when opened.

This comment has been minimized.

Copy link
@jmbuhr

jmbuhr Oct 22, 2023

Contributor

This is amazing!


https://github.com/3rd/image.nvim/assets/59587503/9a9a1792-6476-4d96-8b8e-d3cdd7f5759e

Expand Down Expand Up @@ -116,6 +118,7 @@ require("image").setup({
window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
editor_only_render_when_focused = false, -- auto show/hide images when the editor gains/looses focus
tmux_show_only_in_active_window = false, -- auto show/hide images in the correct Tmux window (needs visual-activity off)
hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp" }, -- render image files as images when opened
})
```

Expand Down
25 changes: 25 additions & 0 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local default_options = {
window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
editor_only_render_when_focused = false,
tmux_show_only_in_active_window = false,
hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp" },
}

---@type State
Expand Down Expand Up @@ -266,6 +267,30 @@ api.setup = function(options)
end,
})
end

-- hijack image filetypes
vim.api.nvim_create_autocmd({ "BufRead" }, {
group = group,
pattern = state.options.hijack_file_patterns,
callback = function(event)
local buf = event.buf
local win = vim.api.nvim_get_current_win()
local path = vim.api.nvim_buf_get_name(buf)

vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { "" })

vim.bo[buf].modifiable = false
vim.bo[buf].buftype = "nowrite"
vim.opt_local.colorcolumn = "0"
vim.opt_local.cursorline = false
vim.opt_local.number = false
vim.opt_local.signcolumn = "no"

local img = api.from_file(path, { window = win, buffer = buf })
img:render()
end,
})
end

local guard_setup = function()
Expand Down
1 change: 1 addition & 0 deletions lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
---@field window_overlap_clear_ft_ignore? string[]
---@field editor_only_render_when_focused? boolean
---@field tmux_show_only_in_active_window? boolean
---@field hijack_file_patterns? string[]

---@class BackendFeatures
---@field crop boolean
Expand Down

3 comments on commit 7b475c4

@LanceMarchetti
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be upgrading to NeoVim soon due to all the good writes-ups I've seen. But I would like to know if this image-view 'hack' replaces the binary byte representation of the loaded image? Because I need to also see the ANSI/UTF-8/code points for manual editing, like I do in Notepad++. It's particularly useful when I need to 'force' the dimensions sector of a Jpeg, GIF. WebP etc for CTF steganoraphy projects. If you require more specifics you're welcome to reach me at bmFyY2hvdGF4QHByb3Rvbi5tZQ
Thanks so much!

@3rd
Copy link
Owner Author

@3rd 3rd commented on 7b475c4 Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be upgrading to NeoVim soon due to all the good writes-ups I've seen. But I would like to know if this image-view 'hack' replaces the binary byte representation of the loaded image? Because I need to also see the ANSI/UTF-8/code points for manual editing, like I do in Notepad++. It's particularly useful when I need to 'force' the dimensions sector of a Jpeg, GIF. WebP etc for CTF steganoraphy projects. If you require more specifics you're welcome to reach me at bmFyY2hvdGF4QHByb3Rvbi5tZQ Thanks so much!

That's definitely an out of scope case, you can disable the plugin / that part when playing CTFs (or use a binary editor or even a ctf tool like veles).

@LanceMarchetti
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...never heard of Veles, much appreciated. Thanks.

Please sign in to comment.