Skip to content

Commit

Permalink
feat: Markview now listens to BifWinEnter & OptionSet autocmds
Browse files Browse the repository at this point in the history
Buffers can now be attached & detached by cahnging the filetype. This
also fixes issues with `:e`.

Closes #287
  • Loading branch information
OXY2DEV committed Feb 14, 2025
1 parent c095a7c commit c7e70ab
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ return {
- `code span`s don't get recognized when on the line after a `code block`(if the line after the `code span` is empty).
This is most likely due to some bug in either the `markdown` or the `markdown_inline` parser.

- Incorrect wrapping when setting `wrap` using `modeline`.
This is due to `textoff` being 0(instead of the size of the `statuscolumn`) when entering a buffer.

## 🧭 Configuration

Check the [wiki](https://github.com/OXY2DEV/markview.nvim/wiki) for the entire configuration table. A simplified version is given below.
Expand Down
76 changes: 75 additions & 1 deletion plugin/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ local function register_source()
end

--- Registers buffers.
vim.api.nvim_create_autocmd({ "BufAdd", "BufEnter" }, {
vim.api.nvim_create_autocmd({
"BufAdd", "BufEnter",
"BufWinEnter"
}, {
group = markview.augroup,
callback = function (event)
---+${lua}
Expand Down Expand Up @@ -153,6 +156,77 @@ vim.api.nvim_create_autocmd({ "BufAdd", "BufEnter" }, {
end
});

--- Timer for 'filetype', 'buftype' changes.
local bf_timer = vim.uv.new_timer();

vim.api.nvim_create_autocmd({
"OptionSet"
}, {
group = markview.augroup,
callback = function ()
---+${lua}
bf_timer:stop()

local buffer = vim.api.nvim_get_current_buf();
local option = vim.fn.expand("<amatch>");

local valid_options = { "filetype", "buftype" };

---@type string, string
local bt, ft = vim.bo[buffer].buftype, vim.bo[buffer].filetype;
local attach_ft = spec.get({ "preview", "filetypes" }, { fallback = {}, ignore_enable = true });
local ignore_bt = spec.get({ "preview", "ignore_buftypes" }, { fallback = {}, ignore_enable = true });

local condition = spec.get({ "preview", "condition" }, { eval_args = { buffer } });

if markview.state.enable == false then
--- New buffers shouldn't be registered.
return;
elseif markview.actions.__is_attached(buffer) == true then
--- Already attached to this buffer!
--- We should check if the buffer is still valid.

if vim.list_contains(ignore_bt, bt) == true then
--- Ignored buffer type.
bf_timer:start(5, 0, vim.schedule_wrap(function ()
markview.actions.detach(buffer);
end));
elseif vim.list_contains(attach_ft, ft) == false then
--- Ignored file type.
bf_timer:start(5, 0, vim.schedule_wrap(function ()
markview.actions.detach(buffer);
end));
elseif condition == false then
bf_timer:start(5, 0, vim.schedule_wrap(function ()
markview.actions.detach(buffer);
end));
end

return;
elseif vim.list_contains(valid_options, option) == false then
--- We shouldn't do anything on other events.
return;
end

if vim.list_contains(ignore_bt, bt) == true then
--- Ignored buffer type.
return;
elseif vim.list_contains(attach_ft, ft) == false then
--- Ignored file type.
return;
elseif condition == false then
return;
end

bf_timer:start(5, 0, vim.schedule_wrap(function ()
markview.actions.attach(buffer);
register_source();
end));
---_
end
});

--- Timer for 'wrap', 'linebreak' changes.
local o_timer = vim.uv.new_timer();

--- Option changes(e.g. wrap, linebreak)
Expand Down

0 comments on commit c7e70ab

Please sign in to comment.