Skip to content

Commit

Permalink
Merge pull request #4 from iamxiaojianzheng/main
Browse files Browse the repository at this point in the history
feat: add filetype config
  • Loading branch information
sarrisv authored Aug 25, 2024
2 parents 75eaf80 + 2c586c3 commit 98cb111
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lua/readermode/init.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
local M = {}

---@class ReaderModeOptions
---@field enabled? boolean
---@field keymap? string
---@field desc? string
---@field allow_filetypes? string[]
---@field denied_filetypes? string[]
local defaults = {
enabled = false,
keymap = "<leader>R",
desc = "Toggle Reader Mode",
allow_filetypes = {},
denied_filetypes = {},
}

---@type ReaderModeOptions
M.opts = {}

---@param opts? ReaderModeOptions
function M.setup(opts)
M.opts = opts or defaults
M.opts = vim.tbl_deep_extend("force", defaults, opts or {}) or {}

-- Define ways to toggle readermode
vim.api.nvim_create_user_command("ReaderMode", require("readermode").toggle, {})
Expand All @@ -24,7 +31,25 @@ function M.setup(opts)
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
group = vim.api.nvim_create_augroup("ReaderMode", { clear = true }),
callback = function()
if M.opts.enabled then
local enabled = M.opts.enabled
local allow_filetypes = M.opts.allow_filetypes
---@cast allow_filetypes table
local denied_filetypes = M.opts.denied_filetypes
---@cast denied_filetypes table

if enabled then
local filetype = vim.bo.filetype

-- denied filetypes contains filetype
if vim.tbl_contains(denied_filetypes, filetype) then
return
end

-- allow_filetypes is not empty and not contains filetype
if vim.tbl_isempty(allow_filetypes) ~= true and vim.tbl_contains(allow_filetypes, filetype) then
return
end

local current_line = M.getCurrentLineNumber()
if current_line ~= last_line then
M.center()
Expand Down

0 comments on commit 98cb111

Please sign in to comment.