-
Notifications
You must be signed in to change notification settings - Fork 23
/
init.lua
102 lines (89 loc) · 2.7 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
local M = {}
local config = require("transparent.config")
local cache = require("transparent.cache")
if vim.g.transparent_enabled == nil then
cache.read()
end
---@param group string|string[]
local function clear_group(group)
local groups = type(group) == "string" and { group } or group
for _, v in ipairs(groups) do
if not vim.tbl_contains(config.exclude_groups, v) then
pcall(function()
local attrs = vim.tbl_extend(
"force",
vim.api.nvim_get_hl_by_name(v, true),
{ bg = "NONE", ctermbg = "NONE" }
)
attrs[true] = nil
vim.api.nvim_set_hl(0, v, attrs)
end)
end
end
end
local function clear()
-- local start = vim.loop.hrtime()
if vim.g.transparent_enabled ~= true then
return
end
clear_group(config.groups)
clear_group(config.extra_groups)
clear_group(type(vim.g.transparent_groups) == "table" and vim.g.transparent_groups or {})
-- print((vim.loop.hrtime() - start) / 1e6, "ms")
end
function M.clear()
if vim.g.transparent_enabled ~= true then
return
end
--- ? some plugins calculate colors from basic highlights
--- : clear immediately
-- local start = vim.loop.hrtime()
clear()
-- print((vim.loop.hrtime() - start) / 1e6, 'ms')
--- ? some plugins use autocommands to redefine highlights
--- : clear again after a while
vim.defer_fn(clear, 500)
--- again
vim.defer_fn(clear, 1000)
--- yes, clear 4 times!!!
vim.defer_fn(clear, 5000)
end
function M.toggle(opt)
if opt == nil then
vim.g.transparent_enabled = not vim.g.transparent_enabled
else
vim.g.transparent_enabled = opt
end
cache.write()
-- A standard theme plugin should support the "colorscheme" command and set the g:colors_name
if vim.g.colors_name then
vim.cmd.colorscheme(vim.g.colors_name)
else
clear()
end
end
function M.handle_groups_changed(arg)
local old = arg.old or {}
local new = arg.new or {}
if
type(old) == "table"
and type(new) == "table"
and vim.tbl_islist(old)
and vim.tbl_islist(new)
then
clear_group(vim.tbl_filter(function(v)
-- print(v)
return not vim.tbl_contains(old, v)
end, new))
else
vim.notify("g:transparent_groups must be a list")
end
end
M.setup = config.set
M.clear_group = clear_group
-- Avoid strange issues caused by lazy loading.
-- The main issue is the order of defining transparency options for theme plugins and this plugin.
vim.defer_fn(function()
M.toggle(vim.g.transparent_enabled)
end, 500)
return M