mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
style: autoformat all the things
Namely: applied stylua to the whole repo and ran marksman on README.md.
This commit is contained in:
138
README.md
138
README.md
@@ -1,18 +1,18 @@
|
||||
<p align="center">
|
||||
<h2 align="center">🧶 auto-save.nvim</h2>
|
||||
<h2 align="center">🧶 auto-save.nvim</h2>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
Automatically save your changes in NeoVim
|
||||
Automatically save your changes in NeoVim
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/okuuva/auto-save.nvim/stargazers">
|
||||
<img alt="Stars" src="https://img.shields.io/github/stars/okuuva/auto-save.nvim?style=for-the-badge&logo=starship&color=C9CBFF&logoColor=D9E0EE&labelColor=302D41"></a>
|
||||
<a href="https://github.com/okuuva/auto-save.nvim/issues">
|
||||
<img alt="Issues" src="https://img.shields.io/github/issues/okuuva/auto-save.nvim?style=for-the-badge&logo=bilibili&color=F5E0DC&logoColor=D9E0EE&labelColor=302D41"></a>
|
||||
<a href="https://github.com/okuuva/auto-save.nvim">
|
||||
<img alt="Repo Size" src="https://img.shields.io/github/repo-size/okuuva/auto-save.nvim?color=%23DDB6F2&label=SIZE&logo=codesandbox&style=for-the-badge&logoColor=D9E0EE&labelColor=302D41"/></a>
|
||||
<a href="https://github.com/okuuva/auto-save.nvim/stargazers">
|
||||
<img alt="Stars" src="https://img.shields.io/github/stars/okuuva/auto-save.nvim?style=for-the-badge&logo=starship&color=C9CBFF&logoColor=D9E0EE&labelColor=302D41"></a>
|
||||
<a href="https://github.com/okuuva/auto-save.nvim/issues">
|
||||
<img alt="Issues" src="https://img.shields.io/github/issues/okuuva/auto-save.nvim?style=for-the-badge&logo=bilibili&color=F5E0DC&logoColor=D9E0EE&labelColor=302D41"></a>
|
||||
<a href="https://github.com/okuuva/auto-save.nvim">
|
||||
<img alt="Repo Size" src="https://img.shields.io/github/repo-size/okuuva/auto-save.nvim?color=%23DDB6F2&label=SIZE&logo=codesandbox&style=for-the-badge&logoColor=D9E0EE&labelColor=302D41"/></a>
|
||||
</p>
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
|
||||
- automatically save your changes so the world doesn't collapse
|
||||
- highly customizable:
|
||||
- conditionals to assert whether to save or not
|
||||
- execution message (it can be dimmed and personalized)
|
||||
- events that trigger auto-save
|
||||
- conditionals to assert whether to save or not
|
||||
- execution message (it can be dimmed and personalized)
|
||||
- events that trigger auto-save
|
||||
- debounce the save with a delay
|
||||
- multiple callbacks
|
||||
- automatically clean the message area
|
||||
@@ -41,49 +41,49 @@
|
||||
Install the plugin with your favourite package manager:
|
||||
|
||||
<details>
|
||||
<summary><a href="https://github.com/folke/lazy.nvim">Lazy.nvim</a></summary>
|
||||
<summary><a href="https://github.com/folke/lazy.nvim">Lazy.nvim</a></summary>
|
||||
|
||||
```lua
|
||||
{
|
||||
"okuuva/auto-save.nvim",
|
||||
cmd = "ASToggle", -- optional for lazy loading on command
|
||||
event = { "InsertLeave", "TextChanged" } -- optional for lazy loading on trigger events
|
||||
opts = {
|
||||
-- your config goes here
|
||||
-- or just leave it empty :)
|
||||
},
|
||||
"okuuva/auto-save.nvim",
|
||||
cmd = "ASToggle", -- optional for lazy loading on command
|
||||
event = { "InsertLeave", "TextChanged" } -- optional for lazy loading on trigger events
|
||||
opts = {
|
||||
-- your config goes here
|
||||
-- or just leave it empty :)
|
||||
},
|
||||
},
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary><a href="https://github.com/wbthomason/packer.nvim">Packer.nvim</a></summary>
|
||||
<summary><a href="https://github.com/wbthomason/packer.nvim">Packer.nvim</a></summary>
|
||||
|
||||
```lua
|
||||
use({
|
||||
"okuuva/auto-save.nvim",
|
||||
config = function()
|
||||
require("auto-save").setup {
|
||||
-- your config goes here
|
||||
-- or just leave it empty :)
|
||||
}
|
||||
end,
|
||||
"okuuva/auto-save.nvim",
|
||||
config = function()
|
||||
require("auto-save").setup {
|
||||
-- your config goes here
|
||||
-- or just leave it empty :)
|
||||
}
|
||||
end,
|
||||
})
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary><a href="https://github.com/junegunn/vim-plug">vim-plug</a></summary>
|
||||
<summary><a href="https://github.com/junegunn/vim-plug">vim-plug</a></summary>
|
||||
|
||||
```vim
|
||||
Plug 'okuuva/auto-save.nvim'
|
||||
lua << EOF
|
||||
require("auto-save").setup {
|
||||
-- your config goes here
|
||||
-- or just leave it empty :)
|
||||
}
|
||||
require("auto-save").setup {
|
||||
-- your config goes here
|
||||
-- or just leave it empty :)
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
@@ -97,39 +97,39 @@ EOF
|
||||
|
||||
```lua
|
||||
{
|
||||
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
|
||||
execution_message = {
|
||||
enabled = true,
|
||||
message = function() -- message to print on save
|
||||
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
|
||||
end,
|
||||
dim = 0.18, -- dim the color of `message`
|
||||
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
|
||||
},
|
||||
trigger_events = {"InsertLeave", "TextChanged"}, -- vim events that trigger auto-save. See :h events
|
||||
-- function that determines whether to save the current buffer or not
|
||||
-- return true: if buffer is ok to be saved
|
||||
-- return false: if it's not ok to be saved
|
||||
condition = function(buf)
|
||||
local fn = vim.fn
|
||||
local utils = require("auto-save.utils.data")
|
||||
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
|
||||
execution_message = {
|
||||
enabled = true,
|
||||
message = function() -- message to print on save
|
||||
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
|
||||
end,
|
||||
dim = 0.18, -- dim the color of `message`
|
||||
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
|
||||
},
|
||||
trigger_events = {"InsertLeave", "TextChanged"}, -- vim events that trigger auto-save. See :h events
|
||||
-- function that determines whether to save the current buffer or not
|
||||
-- return true: if buffer is ok to be saved
|
||||
-- return false: if it's not ok to be saved
|
||||
condition = function(buf)
|
||||
local fn = vim.fn
|
||||
local utils = require("auto-save.utils.data")
|
||||
|
||||
if
|
||||
fn.getbufvar(buf, "&modifiable") == 1 and
|
||||
utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
|
||||
return true -- met condition(s), can save
|
||||
end
|
||||
return false -- can't save
|
||||
end,
|
||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||
debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
|
||||
callbacks = { -- functions to be executed at different intervals
|
||||
enabling = nil, -- ran when enabling auto-save
|
||||
disabling = nil, -- ran when disabling auto-save
|
||||
before_asserting_save = nil, -- ran before checking `condition`
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
after_saving = nil -- ran after doing the actual save
|
||||
}
|
||||
if
|
||||
fn.getbufvar(buf, "&modifiable") == 1 and
|
||||
utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
|
||||
return true -- met condition(s), can save
|
||||
end
|
||||
return false -- can't save
|
||||
end,
|
||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||
debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
|
||||
callbacks = { -- functions to be executed at different intervals
|
||||
enabling = nil, -- ran when enabling auto-save
|
||||
disabling = nil, -- ran when disabling auto-save
|
||||
before_asserting_save = nil, -- ran before checking `condition`
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
after_saving = nil -- ran after doing the actual save
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -143,11 +143,11 @@ or as part of the `lazy.nvim` plugin spec:
|
||||
|
||||
```lua
|
||||
{
|
||||
"okuuva/auto-save.nvim",
|
||||
keys = {
|
||||
{ "<leader>n", ":ASToggle<CR>", desc = "Toggle auto-save" },
|
||||
},
|
||||
...
|
||||
"okuuva/auto-save.nvim",
|
||||
keys = {
|
||||
{ "<leader>n", ":ASToggle<CR>", desc = "Toggle auto-save" },
|
||||
},
|
||||
...
|
||||
},
|
||||
|
||||
```
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
Config = {
|
||||
opts = {
|
||||
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
|
||||
execution_message = {
|
||||
enabled = true,
|
||||
message = function() -- message to print on save
|
||||
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
|
||||
end,
|
||||
dim = 0.18, -- dim the color of `message`
|
||||
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
|
||||
},
|
||||
trigger_events = { "InsertLeave", "TextChanged" }, -- vim events that trigger auto-save. See :h events
|
||||
-- function that determines whether to save the current buffer or not
|
||||
-- return true: if buffer is ok to be saved
|
||||
-- return false: if it's not ok to be saved
|
||||
condition = function(buf)
|
||||
local fn = vim.fn
|
||||
local utils = require("auto-save.utils.data")
|
||||
opts = {
|
||||
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
|
||||
execution_message = {
|
||||
enabled = true,
|
||||
message = function() -- message to print on save
|
||||
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
|
||||
end,
|
||||
dim = 0.18, -- dim the color of `message`
|
||||
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
|
||||
},
|
||||
trigger_events = { "InsertLeave", "TextChanged" }, -- vim events that trigger auto-save. See :h events
|
||||
-- function that determines whether to save the current buffer or not
|
||||
-- return true: if buffer is ok to be saved
|
||||
-- return false: if it's not ok to be saved
|
||||
condition = function(buf)
|
||||
local fn = vim.fn
|
||||
local utils = require("auto-save.utils.data")
|
||||
|
||||
if fn.getbufvar(buf, "&modifiable") == 1 and utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
|
||||
return true -- met condition(s), can save
|
||||
end
|
||||
return false -- can't save
|
||||
end,
|
||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||
debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
|
||||
callbacks = { -- functions to be executed at different intervals
|
||||
enabling = nil, -- ran when enabling auto-save
|
||||
disabling = nil, -- ran when disabling auto-save
|
||||
before_asserting_save = nil, -- ran before checking `condition`
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
after_saving = nil, -- ran after doing the actual save
|
||||
},
|
||||
},
|
||||
if fn.getbufvar(buf, "&modifiable") == 1 and utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
|
||||
return true -- met condition(s), can save
|
||||
end
|
||||
return false -- can't save
|
||||
end,
|
||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||
debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
|
||||
callbacks = { -- functions to be executed at different intervals
|
||||
enabling = nil, -- ran when enabling auto-save
|
||||
disabling = nil, -- ran when disabling auto-save
|
||||
before_asserting_save = nil, -- ran before checking `condition`
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
after_saving = nil, -- ran after doing the actual save
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function Config:set_options(opts)
|
||||
opts = opts or {}
|
||||
self.opts = vim.tbl_deep_extend("keep", opts, self.opts)
|
||||
opts = opts or {}
|
||||
self.opts = vim.tbl_deep_extend("keep", opts, self.opts)
|
||||
end
|
||||
|
||||
function Config:get_options()
|
||||
return self.opts
|
||||
return self.opts
|
||||
end
|
||||
|
||||
return Config
|
||||
|
||||
@@ -15,164 +15,164 @@ local BLACK = "#000000"
|
||||
local WHITE = "#ffffff"
|
||||
|
||||
api.nvim_create_augroup("AutoSave", {
|
||||
clear = true,
|
||||
clear = true,
|
||||
})
|
||||
|
||||
local global_vars = {}
|
||||
|
||||
local function set_buf_var(buf, name, value)
|
||||
if buf == nil then
|
||||
global_vars[name] = value
|
||||
else
|
||||
if api.nvim_buf_is_valid(buf) then
|
||||
api.nvim_buf_set_var(buf, "autosave_" .. name, value)
|
||||
end
|
||||
end
|
||||
if buf == nil then
|
||||
global_vars[name] = value
|
||||
else
|
||||
if api.nvim_buf_is_valid(buf) then
|
||||
api.nvim_buf_set_var(buf, "autosave_" .. name, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function get_buf_var(buf, name)
|
||||
if buf == nil then
|
||||
return global_vars[name]
|
||||
end
|
||||
local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name)
|
||||
return success and mod or nil
|
||||
if buf == nil then
|
||||
return global_vars[name]
|
||||
end
|
||||
local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name)
|
||||
return success and mod or nil
|
||||
end
|
||||
|
||||
local function debounce(lfn, duration)
|
||||
local function inner_debounce()
|
||||
local buf = api.nvim_get_current_buf()
|
||||
if not get_buf_var(buf, "queued") then
|
||||
vim.defer_fn(function()
|
||||
set_buf_var(buf, "queued", false)
|
||||
lfn(buf)
|
||||
end, duration)
|
||||
set_buf_var(buf, "queued", true)
|
||||
end
|
||||
end
|
||||
local function inner_debounce()
|
||||
local buf = api.nvim_get_current_buf()
|
||||
if not get_buf_var(buf, "queued") then
|
||||
vim.defer_fn(function()
|
||||
set_buf_var(buf, "queued", false)
|
||||
lfn(buf)
|
||||
end, duration)
|
||||
set_buf_var(buf, "queued", true)
|
||||
end
|
||||
end
|
||||
|
||||
return inner_debounce
|
||||
return inner_debounce
|
||||
end
|
||||
|
||||
local function echo_execution_message()
|
||||
local msg = type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message()
|
||||
or cnf.opts.execution_message.message
|
||||
api.nvim_echo({ { msg, AUTO_SAVE_COLOR } }, true, {})
|
||||
if cnf.opts.execution_message.cleaning_interval > 0 then
|
||||
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
|
||||
cmd([[echon '']])
|
||||
end)
|
||||
end
|
||||
local msg = type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message()
|
||||
or cnf.opts.execution_message.message
|
||||
api.nvim_echo({ { msg, AUTO_SAVE_COLOR } }, true, {})
|
||||
if cnf.opts.execution_message.cleaning_interval > 0 then
|
||||
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
|
||||
cmd([[echon '']])
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function M.save(buf)
|
||||
buf = buf or api.nvim_get_current_buf()
|
||||
buf = buf or api.nvim_get_current_buf()
|
||||
|
||||
callback("before_asserting_save")
|
||||
callback("before_asserting_save")
|
||||
|
||||
if cnf.opts.condition(buf) == false then
|
||||
return
|
||||
end
|
||||
if cnf.opts.condition(buf) == false then
|
||||
return
|
||||
end
|
||||
|
||||
if not api.nvim_buf_get_option(buf, "modified") then
|
||||
return
|
||||
end
|
||||
if not api.nvim_buf_get_option(buf, "modified") then
|
||||
return
|
||||
end
|
||||
|
||||
callback("before_saving")
|
||||
callback("before_saving")
|
||||
|
||||
if g.auto_save_abort == true then
|
||||
return
|
||||
end
|
||||
if g.auto_save_abort == true then
|
||||
return
|
||||
end
|
||||
|
||||
if cnf.opts.write_all_buffers then
|
||||
cmd("silent! wall")
|
||||
else
|
||||
api.nvim_buf_call(buf, function()
|
||||
cmd("silent! write")
|
||||
end)
|
||||
end
|
||||
if cnf.opts.write_all_buffers then
|
||||
cmd("silent! wall")
|
||||
else
|
||||
api.nvim_buf_call(buf, function()
|
||||
cmd("silent! write")
|
||||
end)
|
||||
end
|
||||
|
||||
callback("after_saving")
|
||||
callback("after_saving")
|
||||
|
||||
if cnf.opts.execution_message.enabled == true then
|
||||
echo_execution_message()
|
||||
end
|
||||
if cnf.opts.execution_message.enabled == true then
|
||||
echo_execution_message()
|
||||
end
|
||||
end
|
||||
|
||||
local save_func = nil
|
||||
|
||||
local function perform_save()
|
||||
g.auto_save_abort = false
|
||||
if save_func == nil then
|
||||
save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save)
|
||||
end
|
||||
save_func()
|
||||
g.auto_save_abort = false
|
||||
if save_func == nil then
|
||||
save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save)
|
||||
end
|
||||
save_func()
|
||||
end
|
||||
|
||||
function M.on()
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events, {
|
||||
callback = function()
|
||||
perform_save()
|
||||
end,
|
||||
pattern = "*",
|
||||
group = "AutoSave",
|
||||
})
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events, {
|
||||
callback = function()
|
||||
perform_save()
|
||||
end,
|
||||
pattern = "*",
|
||||
group = "AutoSave",
|
||||
})
|
||||
|
||||
api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, {
|
||||
callback = function()
|
||||
vim.schedule(function()
|
||||
if cnf.opts.execution_message.dim > 0 then
|
||||
MSG_AREA = colors.get_hl("MsgArea")
|
||||
if MSG_AREA.foreground ~= nil then
|
||||
MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"])
|
||||
local foreground = (
|
||||
o.background == "dark"
|
||||
and colors.darken(
|
||||
(MSG_AREA.background or BLACK),
|
||||
cnf.opts.execution_message.dim,
|
||||
MSG_AREA.foreground or BLACK
|
||||
)
|
||||
or colors.lighten(
|
||||
(MSG_AREA.background or WHITE),
|
||||
cnf.opts.execution_message.dim,
|
||||
MSG_AREA.foreground or WHITE
|
||||
)
|
||||
)
|
||||
api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, {
|
||||
callback = function()
|
||||
vim.schedule(function()
|
||||
if cnf.opts.execution_message.dim > 0 then
|
||||
MSG_AREA = colors.get_hl("MsgArea")
|
||||
if MSG_AREA.foreground ~= nil then
|
||||
MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"])
|
||||
local foreground = (
|
||||
o.background == "dark"
|
||||
and colors.darken(
|
||||
(MSG_AREA.background or BLACK),
|
||||
cnf.opts.execution_message.dim,
|
||||
MSG_AREA.foreground or BLACK
|
||||
)
|
||||
or colors.lighten(
|
||||
(MSG_AREA.background or WHITE),
|
||||
cnf.opts.execution_message.dim,
|
||||
MSG_AREA.foreground or WHITE
|
||||
)
|
||||
)
|
||||
|
||||
colors.highlight("AutoSaveText", { fg = foreground })
|
||||
AUTO_SAVE_COLOR = "AutoSaveText"
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
pattern = "*",
|
||||
group = "AutoSave",
|
||||
})
|
||||
colors.highlight("AutoSaveText", { fg = foreground })
|
||||
AUTO_SAVE_COLOR = "AutoSaveText"
|
||||
end
|
||||
end
|
||||
end)
|
||||
end,
|
||||
pattern = "*",
|
||||
group = "AutoSave",
|
||||
})
|
||||
|
||||
callback("enabling")
|
||||
autosave_running = true
|
||||
callback("enabling")
|
||||
autosave_running = true
|
||||
end
|
||||
|
||||
function M.off()
|
||||
api.nvim_create_augroup("AutoSave", {
|
||||
clear = true,
|
||||
})
|
||||
api.nvim_create_augroup("AutoSave", {
|
||||
clear = true,
|
||||
})
|
||||
|
||||
callback("disabling")
|
||||
autosave_running = false
|
||||
callback("disabling")
|
||||
autosave_running = false
|
||||
end
|
||||
|
||||
function M.toggle()
|
||||
if autosave_running then
|
||||
M.off()
|
||||
echo("off")
|
||||
else
|
||||
M.on()
|
||||
echo("on")
|
||||
end
|
||||
if autosave_running then
|
||||
M.off()
|
||||
echo("off")
|
||||
else
|
||||
M.on()
|
||||
echo("on")
|
||||
end
|
||||
end
|
||||
|
||||
function M.setup(custom_opts)
|
||||
cnf:set_options(custom_opts)
|
||||
cnf:set_options(custom_opts)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
local M = {}
|
||||
---@param hex_str string hexadecimal value of a color
|
||||
local hex_to_rgb = function(hex_str)
|
||||
local hex = "[abcdef0-9][abcdef0-9]"
|
||||
local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
|
||||
hex_str = string.lower(hex_str)
|
||||
local hex = "[abcdef0-9][abcdef0-9]"
|
||||
local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
|
||||
hex_str = string.lower(hex_str)
|
||||
|
||||
assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str))
|
||||
assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str))
|
||||
|
||||
local red, green, blue = string.match(hex_str, pat)
|
||||
return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) }
|
||||
local red, green, blue = string.match(hex_str, pat)
|
||||
return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) }
|
||||
end
|
||||
|
||||
function M.highlight(group, color, force)
|
||||
if color.link then
|
||||
vim.api.nvim_set_hl(0, group, {
|
||||
link = color.link,
|
||||
})
|
||||
else
|
||||
if color.style then
|
||||
for _, style in ipairs(color.style) do
|
||||
color[style] = true
|
||||
end
|
||||
end
|
||||
color.style = nil
|
||||
if force then
|
||||
vim.cmd("hi " .. group .. " guifg=" .. (color.fg or "NONE") .. " guibg=" .. (color.bg or "NONE"))
|
||||
return
|
||||
end
|
||||
vim.api.nvim_set_hl(0, group, color)
|
||||
end
|
||||
if color.link then
|
||||
vim.api.nvim_set_hl(0, group, {
|
||||
link = color.link,
|
||||
})
|
||||
else
|
||||
if color.style then
|
||||
for _, style in ipairs(color.style) do
|
||||
color[style] = true
|
||||
end
|
||||
end
|
||||
color.style = nil
|
||||
if force then
|
||||
vim.cmd("hi " .. group .. " guifg=" .. (color.fg or "NONE") .. " guibg=" .. (color.bg or "NONE"))
|
||||
return
|
||||
end
|
||||
vim.api.nvim_set_hl(0, group, color)
|
||||
end
|
||||
end
|
||||
|
||||
function M.get_hl(name)
|
||||
local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true)
|
||||
if not ok then
|
||||
return
|
||||
end
|
||||
for _, key in pairs({ "foreground", "background", "special" }) do
|
||||
if hl[key] then
|
||||
hl[key] = string.format("#%06x", hl[key])
|
||||
end
|
||||
end
|
||||
return hl
|
||||
local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true)
|
||||
if not ok then
|
||||
return
|
||||
end
|
||||
for _, key in pairs({ "foreground", "background", "special" }) do
|
||||
if hl[key] then
|
||||
hl[key] = string.format("#%06x", hl[key])
|
||||
end
|
||||
end
|
||||
return hl
|
||||
end
|
||||
|
||||
---@param fg string forecrust color
|
||||
---@param bg string background color
|
||||
---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg
|
||||
function M.blend(fg, bg, alpha)
|
||||
bg = hex_to_rgb(bg)
|
||||
fg = hex_to_rgb(fg)
|
||||
bg = hex_to_rgb(bg)
|
||||
fg = hex_to_rgb(fg)
|
||||
|
||||
local blendChannel = function(i)
|
||||
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
|
||||
return math.floor(math.min(math.max(0, ret), 255) + 0.5)
|
||||
end
|
||||
local blendChannel = function(i)
|
||||
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
|
||||
return math.floor(math.min(math.max(0, ret), 255) + 0.5)
|
||||
end
|
||||
|
||||
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
|
||||
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
|
||||
end
|
||||
|
||||
function M.darken(hex, amount, bg)
|
||||
return M.blend(hex, bg or M.bg, math.abs(amount))
|
||||
return M.blend(hex, bg or M.bg, math.abs(amount))
|
||||
end
|
||||
|
||||
function M.lighten(hex, amount, fg)
|
||||
return M.blend(hex, fg or M.fg, math.abs(amount))
|
||||
return M.blend(hex, fg or M.fg, math.abs(amount))
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
local M = {}
|
||||
|
||||
function M.set_of(list)
|
||||
local set = {}
|
||||
for i = 1, #list do
|
||||
set[list[i]] = true
|
||||
end
|
||||
return set
|
||||
local set = {}
|
||||
for i = 1, #list do
|
||||
set[list[i]] = true
|
||||
end
|
||||
return set
|
||||
end
|
||||
|
||||
function M.not_in(var, arr)
|
||||
if M.set_of(arr)[var] == nil then
|
||||
return true
|
||||
end
|
||||
if M.set_of(arr)[var] == nil then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
function M.do_callback(callback_name)
|
||||
local cnf = require("auto-save.config").opts
|
||||
local cnf = require("auto-save.config").opts
|
||||
|
||||
if type(cnf.callbacks[callback_name]) == "function" then
|
||||
cnf.callbacks[callback_name]()
|
||||
end
|
||||
if type(cnf.callbacks[callback_name]) == "function" then
|
||||
cnf.callbacks[callback_name]()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
local TITLE = "auto-save"
|
||||
|
||||
return function(msg, kind)
|
||||
local has_notify_plugin = pcall(require, "notify")
|
||||
local level = {}
|
||||
local has_notify_plugin = pcall(require, "notify")
|
||||
local level = {}
|
||||
|
||||
if kind == "error" then
|
||||
level.log = vim.log.levels.ERROR
|
||||
level.type = "error"
|
||||
elseif kind == "warn" then
|
||||
level.log = vim.log.levels.WARN
|
||||
level.type = "error"
|
||||
else
|
||||
level.log = kind or vim.log.levels.INFO
|
||||
level.type = "info"
|
||||
end
|
||||
if kind == "error" then
|
||||
level.log = vim.log.levels.ERROR
|
||||
level.type = "error"
|
||||
elseif kind == "warn" then
|
||||
level.log = vim.log.levels.WARN
|
||||
level.type = "error"
|
||||
else
|
||||
level.log = kind or vim.log.levels.INFO
|
||||
level.type = "info"
|
||||
end
|
||||
|
||||
if has_notify_plugin then
|
||||
vim.notify(msg, level.log, {
|
||||
title = TITLE,
|
||||
})
|
||||
else
|
||||
vim.notify(("%s: %s"):format(TITLE, msg), level.log)
|
||||
end
|
||||
if has_notify_plugin then
|
||||
vim.notify(msg, level.log, {
|
||||
title = TITLE,
|
||||
})
|
||||
else
|
||||
vim.notify(("%s: %s"):format(TITLE, msg), level.log)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
if vim.g.loaded_auto_save then
|
||||
return
|
||||
return
|
||||
end
|
||||
vim.g.loaded_auto_save = true
|
||||
|
||||
@@ -7,9 +7,9 @@ local command = vim.api.nvim_create_user_command
|
||||
local cnf = require("auto-save.config").opts
|
||||
|
||||
command("ASToggle", function()
|
||||
require("auto-save").toggle()
|
||||
require("auto-save").toggle()
|
||||
end, {})
|
||||
|
||||
if cnf.enabled then
|
||||
require("auto-save").on()
|
||||
require("auto-save").on()
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user