Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Change filetype for image previewed buffers and export callback function #61

Closed
pysan3 opened this issue Oct 23, 2023 · 3 comments

Comments

@pysan3
Copy link
Contributor

pysan3 commented Oct 23, 2023

Hi, sorry for spamming you with feature requests lol.

While doing the integration to neo-tree I needed to detect the buffers that the images where loaded in.
And I would like to propose two feature requests regarding this issue.

1. Set vim.bo[buf].filetype to something plugin specific.

If you can set a filetype to the buffer on BufRead, it would be much easier to render the preview.

local buf = ...
vim.bo[buf].filetype = "image_nvim" -- or "image_nvim_png" etc would be nice too if possible

2. Export hijack buffer function to be called from outside.

It would be nice if I can call the hijack buffer code (basically the callback of the current autocmd) from outside as an API.

---@param path string
---@param win number? if nil or 0, uses current window
---@param buf number? if nil or 0, uses current buffer
---@param options ImageOptions?
api.hijack_buffer = function(path, win, buf, options)
  if not win or win == 0 then win = vim.api.nvim_get_current_win() end
  if not buf or buf == 0 then buf = vim.api.nvim_get_current_buf() end
  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"

  -- vim.bo[buf].filetype = ...

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

Thanks in advance.

@3rd
Copy link
Owner

3rd commented Oct 23, 2023

Both ideas are great, will get them in!

@3rd
Copy link
Owner

3rd commented Oct 24, 2023

Done, let me know if you find any issues!
The ft is image_nvim, without the extension, but you can call hijack_buffer multiple times on the same window+pair to get the image if you need any internals. All the images are converted to png through magick.

@3rd 3rd closed this as completed Oct 24, 2023
@pysan3
Copy link
Contributor Author

pysan3 commented Oct 25, 2023

Thank you very much for your quick response.

you can call hijack_buffer multiple times on the same window+pair to get the image if you need any internals

This is amazing!

All the images are converted to png through magick.

Ah, now I know why it takes a bit longer to load jpg images even when they are small for the first time (I'm on a very low spec PC lol).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants