aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorAkianonymus <anonymus.aki@gmail.com>2022-08-13 00:08:41 +0530
committerAkianonymus <anonymus.aki@gmail.com>2022-08-13 00:08:41 +0530
commit448e920a0db50e0fd893f7bc7954243039122ee2 (patch)
tree373913a921d0d6baf9aaa745515566612594daad /lua
parentMerge pull request #4 from lasse16/master (diff)
Use native api functions | Convert colorizer.vim to colorizer.lua
Diffstat (limited to 'lua')
-rw-r--r--lua/colorizer.lua98
-rw-r--r--lua/colorizer/nvim.lua251
2 files changed, 63 insertions, 286 deletions
diff --git a/lua/colorizer.lua b/lua/colorizer.lua
index 7ee35bf..e14db85 100644
--- a/lua/colorizer.lua
+++ b/lua/colorizer.lua
@@ -1,15 +1,22 @@
--- Highlights terminal CSI ANSI color codes.
-- @module colorizer
-local nvim = require "colorizer/nvim"
local Trie = require "colorizer/trie"
local bit = require "bit"
local ffi = require "ffi"
+local api = vim.api
+
+local augroup = api.nvim_create_augroup
+local autocmd = api.nvim_create_autocmd
+local set_highlight = api.nvim_set_hl
+
+local buf_add_highlight = api.nvim_buf_add_highlight
+local buf_attach = api.nvim_buf_attach
+local buf_clear_namespace = api.nvim_buf_clear_namespace
+local get_current_buf = api.nvim_get_current_buf
+local buf_get_option = api.nvim_buf_get_option
+local buf_get_lines = api.nvim_buf_get_lines
+local buf_set_virtual_text = api.nvim_buf_set_virtual_text
-local nvim_buf_add_highlight = vim.api.nvim_buf_add_highlight
-local nvim_buf_clear_namespace = vim.api.nvim_buf_clear_namespace
-local nvim_buf_get_lines = vim.api.nvim_buf_get_lines
-local nvim_get_current_buf = vim.api.nvim_get_current_buf
-local nvim_buf_set_virtual_text = vim.api.nvim_buf_set_virtual_text
local band, lshift, bor, tohex = bit.band, bit.lshift, bit.bor, bit.tohex
local rshift = bit.rshift
local floor, min, max = math.floor, math.min, math.max
@@ -27,7 +34,7 @@ local function initialize_trie()
if not COLOR_TRIE then
COLOR_MAP = {}
COLOR_TRIE = Trie()
- for k, v in pairs(nvim.get_color_map()) do
+ for k, v in pairs(api.nvim_get_color_map()) do
if not (COLOR_NAME_SETTINGS.strip_digits and k:match "%d+$") then
COLOR_NAME_MINLEN = COLOR_NAME_MINLEN and min(#k, COLOR_NAME_MINLEN) or #k
COLOR_NAME_MAXLEN = COLOR_NAME_MAXLEN and max(#k, COLOR_NAME_MAXLEN) or #k
@@ -469,7 +476,7 @@ end
-- The name is "terminal_highlight"
-- @see highlight_buffer
-- @see attach_to_buffer
-local DEFAULT_NAMESPACE = nvim.create_namespace "colorizer"
+local DEFAULT_NAMESPACE = api.nvim_create_namespace "colorizer"
local HIGHLIGHT_NAME_PREFIX = "colorizer"
local HIGHLIGHT_MODE_NAMES = {
background = "mb",
@@ -501,7 +508,7 @@ local function create_highlight(rgb_hex, options)
-- Create the highlight
highlight_name = make_highlight_name(rgb_hex, mode)
if mode == "foreground" then
- nvim.ex.highlight(highlight_name, "guifg=#" .. rgb_hex)
+ set_highlight(0, highlight_name, { fg = "#" .. rgb_hex })
else
local r, g, b = rgb_hex:sub(1, 2), rgb_hex:sub(3, 4), rgb_hex:sub(5, 6)
r, g, b = tonumber(r, 16), tonumber(g, 16), tonumber(b, 16)
@@ -511,7 +518,7 @@ local function create_highlight(rgb_hex, options)
else
fg_color = "White"
end
- nvim.ex.highlight(highlight_name, "guifg=" .. fg_color, "guibg=#" .. rgb_hex)
+ set_highlight(0, highlight_name, { fg = fg_color, bg = "#" .. rgb_hex })
end
HIGHLIGHT_CACHE[cache_key] = highlight_name
end
@@ -587,14 +594,14 @@ local function add_highlight(options, buf, ns, data)
for linenr, hls in pairs(data) do
if vim.tbl_contains({ "foreground", "background" }, options.mode) then
for _, hl in ipairs(hls) do
- nvim_buf_add_highlight(buf, ns, hl.name, linenr, hl.range[1], hl.range[2])
+ buf_add_highlight(buf, ns, hl.name, linenr, hl.range[1], hl.range[2])
end
elseif options.mode == "virtualtext" then
local chunks = {}
for _, hl in ipairs(hls) do
table.insert(chunks, { options.virtualtext, hl.name })
end
- nvim_buf_set_virtual_text(buf, ns, linenr, chunks, {})
+ buf_set_virtual_text(buf, ns, linenr, chunks, {})
end
end
end
@@ -651,16 +658,16 @@ local FILETYPE_OPTIONS = {}
local function rehighlight_buffer(buf, options)
local ns = DEFAULT_NAMESPACE
if buf == 0 or buf == nil then
- buf = nvim_get_current_buf()
+ buf = get_current_buf()
end
assert(options)
- nvim_buf_clear_namespace(buf, ns, 0, -1)
- local lines = nvim_buf_get_lines(buf, 0, -1, true)
+ buf_clear_namespace(buf, ns, 0, -1)
+ local lines = buf_get_lines(buf, 0, -1, true)
highlight_buffer(buf, ns, lines, 0, options)
end
local function new_buffer_options(buf)
- local filetype = nvim.buf_get_option(buf, "filetype")
+ local filetype = buf_get_option(buf, "filetype")
return FILETYPE_OPTIONS[filetype] or SETUP_SETTINGS.default_options
end
@@ -669,7 +676,7 @@ end
-- @return true if attached to the buffer, false otherwise.
local function is_buffer_attached(buf)
if buf == 0 or buf == nil then
- buf = nvim_get_current_buf()
+ buf = get_current_buf()
end
return BUFFER_OPTIONS[buf] ~= nil
end
@@ -680,7 +687,7 @@ end
-- @see setup
local function attach_to_buffer(buf, options)
if buf == 0 or buf == nil then
- buf = nvim_get_current_buf()
+ buf = get_current_buf()
end
local already_attached = BUFFER_OPTIONS[buf] ~= nil
local ns = DEFAULT_NAMESPACE
@@ -693,14 +700,14 @@ local function attach_to_buffer(buf, options)
return
end
-- send_buffer: true doesn't actually do anything in Lua (yet)
- nvim.buf_attach(buf, false, {
+ buf_attach(buf, false, {
on_lines = function(event_type, buf, changed_tick, firstline, lastline, new_lastline)
-- This is used to signal stopping the handler highlights
if not BUFFER_OPTIONS[buf] then
return true
end
- nvim_buf_clear_namespace(buf, ns, firstline, new_lastline)
- local lines = nvim_buf_get_lines(buf, firstline, new_lastline, false)
+ buf_clear_namespace(buf, ns, firstline, new_lastline)
+ local lines = buf_get_lines(buf, firstline, new_lastline, false)
highlight_buffer(buf, ns, lines, firstline, BUFFER_OPTIONS[buf])
end,
on_detach = function()
@@ -714,9 +721,9 @@ end
-- @tparam[opt=DEFAULT_NAMESPACE] integer ns the namespace id.
local function detach_from_buffer(buf, ns)
if buf == 0 or buf == nil then
- buf = nvim_get_current_buf()
+ buf = get_current_buf()
end
- nvim_buf_clear_namespace(buf, ns or DEFAULT_NAMESPACE, 0, -1)
+ buf_clear_namespace(buf, ns or DEFAULT_NAMESPACE, 0, -1)
BUFFER_OPTIONS[buf] = nil
end
@@ -740,8 +747,8 @@ end
-- @tparam[opt] {[string]=string} default_options Default options to apply for the filetypes enable.
-- @usage require'colorizer'.setup()
local function setup(filetypes, user_default_options)
- if not nvim.o.termguicolors then
- nvim.err_writeln "&termguicolors must be set"
+ if not vim.opt.termguicolors then
+ vim.notify("&termguicolors must be set", "ErrorMsg")
return
end
FILETYPE_OPTIONS = {}
@@ -752,18 +759,29 @@ local function setup(filetypes, user_default_options)
-- Initialize this AFTER setting COLOR_NAME_SETTINGS
initialize_trie()
function COLORIZER_SETUP_HOOK()
- local filetype = nvim.bo.filetype
+ local filetype = vim.bo.filetype
if SETUP_SETTINGS.exclusions[filetype] then
return
end
local options = FILETYPE_OPTIONS[filetype] or SETUP_SETTINGS.default_options
- attach_to_buffer(nvim_get_current_buf(), options)
+ attach_to_buffer(get_current_buf(), options)
end
- nvim.ex.augroup "ColorizerSetup"
- nvim.ex.autocmd_()
+ local au_group_id = augroup("ColorizerSetup", {})
+ autocmd("FileType", {
+ group = au_group_id,
+ callback = function()
+ COLORIZER_SETUP_HOOK()
+ end,
+ })
+
if not filetypes then
- nvim.ex.autocmd "FileType * lua COLORIZER_SETUP_HOOK()"
+ autocmd("FileType", {
+ group = au_group_id,
+ callback = function()
+ COLORIZER_SETUP_HOOK()
+ end,
+ })
else
for k, v in pairs(filetypes) do
local filetype
@@ -771,7 +789,7 @@ local function setup(filetypes, user_default_options)
if type(k) == "string" then
filetype = k
if type(v) ~= "table" then
- nvim.err_writeln("colorizer: Invalid option type for filetype " .. filetype)
+ vim.notify("colorizer: Invalid option type for filetype " .. filetype, "ErrorMsg")
else
options = merge(SETUP_SETTINGS.default_options, v)
assert(
@@ -788,12 +806,22 @@ local function setup(filetypes, user_default_options)
else
FILETYPE_OPTIONS[filetype] = options
-- TODO What's the right mode for this? BufEnter?
- nvim.ex.autocmd("FileType", filetype, "lua COLORIZER_SETUP_HOOK()")
+ autocmd("FileType", {
+ group = au_group_id,
+ pattern = filetype,
+ callback = function()
+ COLORIZER_SETUP_HOOK()
+ end,
+ })
end
end
end
- nvim.ex.autocmd("ColorScheme", "*", "lua require('colorizer').clear_highlight_cache()")
- nvim.ex.augroup "END"
+ autocmd("ColorScheme", {
+ group = au_group_id,
+ callback = function()
+ require("colorizer").clear_highlight_cache()
+ end,
+ })
end
--- Reload all of the currently active highlighted buffers.
@@ -813,7 +841,7 @@ end
-- @tparam[opt=0|nil] integer buf A value of 0 or nil implies the current buffer.
local function get_buffer_options(buf)
if buf == 0 or buf == nil then
- buf = nvim_get_current_buf()
+ buf = get_current_buf()
end
return merge({}, BUFFER_OPTIONS[buf])
end
diff --git a/lua/colorizer/nvim.lua b/lua/colorizer/nvim.lua
deleted file mode 100644
index 3d83bd0..0000000
--- a/lua/colorizer/nvim.lua
+++ /dev/null
@@ -1,251 +0,0 @@
---- Module of magic functions for nvim
--- @module nvim
-
--- Equivalent to `echo vim.inspect(...)`
-local function nvim_print(...)
- if select("#", ...) == 1 then
- vim.api.nvim_out_write(vim.inspect((...)))
- else
- vim.api.nvim_out_write(vim.inspect { ... })
- end
- vim.api.nvim_out_write "\n"
-end
-
---- Equivalent to `echo` EX command
-local function nvim_echo(...)
- for i = 1, select("#", ...) do
- local part = select(i, ...)
- vim.api.nvim_out_write(tostring(part))
- -- vim.api.nvim_out_write("\n")
- vim.api.nvim_out_write " "
- end
- vim.api.nvim_out_write "\n"
-end
-
-local window_options = {
- arab = true,
- arabic = true,
- breakindent = true,
- breakindentopt = true,
- bri = true,
- briopt = true,
- cc = true,
- cocu = true,
- cole = true,
- colorcolumn = true,
- concealcursor = true,
- conceallevel = true,
- crb = true,
- cuc = true,
- cul = true,
- cursorbind = true,
- cursorcolumn = true,
- cursorline = true,
- diff = true,
- fcs = true,
- fdc = true,
- fde = true,
- fdi = true,
- fdl = true,
- fdm = true,
- fdn = true,
- fdt = true,
- fen = true,
- fillchars = true,
- fml = true,
- fmr = true,
- foldcolumn = true,
- foldenable = true,
- foldexpr = true,
- foldignore = true,
- foldlevel = true,
- foldmarker = true,
- foldmethod = true,
- foldminlines = true,
- foldnestmax = true,
- foldtext = true,
- lbr = true,
- lcs = true,
- linebreak = true,
- list = true,
- listchars = true,
- nu = true,
- number = true,
- numberwidth = true,
- nuw = true,
- previewwindow = true,
- pvw = true,
- relativenumber = true,
- rightleft = true,
- rightleftcmd = true,
- rl = true,
- rlc = true,
- rnu = true,
- scb = true,
- scl = true,
- scr = true,
- scroll = true,
- scrollbind = true,
- signcolumn = true,
- spell = true,
- statusline = true,
- stl = true,
- wfh = true,
- wfw = true,
- winbl = true,
- winblend = true,
- winfixheight = true,
- winfixwidth = true,
- winhighlight = true,
- winhl = true,
- wrap = true,
-}
-
--- `nvim.$method(...)` redirects to `nvim.api.nvim_$method(...)`
--- `nvim.fn.$method(...)` redirects to `vim.api.nvim_call_function($method, {...})`
--- TODO `nvim.ex.$command(...)` is approximately `:$command {...}.join(" ")`
--- `nvim.print(...)` is approximately `echo vim.inspect(...)`
--- `nvim.echo(...)` is approximately `echo table.concat({...}, '\n')`
--- Both methods cache the inital lookup in the metatable, but there is a small overhead regardless.
-return setmetatable({
- print = nvim_print,
- echo = nvim_echo,
- fn = setmetatable({}, {
- __index = function(self, k)
- local mt = getmetatable(self)
- local x = mt[k]
- if x ~= nil then
- return x
- end
- local f = function(...)
- return vim.api.nvim_call_function(k, { ... })
- end
- mt[k] = f
- return f
- end,
- }),
- buf = setmetatable({}, {
- __index = function(self, k)
- local mt = getmetatable(self)
- local x = mt[k]
- if x ~= nil then
- return x
- end
- local f
- if k == "line" then
- f = function()
- local pos = vim.api.nvim_win_get_cursor(0)
- return vim.api.nvim_buf_get_lines(0, pos[1] - 1, pos[1], "line")[1]
- end
- elseif k == "nr" then
- f = vim.api.nvim_get_current_buf
- end
- mt[k] = f
- return f
- end,
- }),
- ex = setmetatable({}, {
- __index = function(self, k)
- local mt = getmetatable(self)
- local x = mt[k]
- if x ~= nil then
- return x
- end
- local command = k:gsub("_$", "!")
- local f = function(...)
- return vim.api.nvim_command(table.concat(vim.tbl_flatten { command, ... }, " "))
- end
- mt[k] = f
- return f
- end,
- }),
- g = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_get_var(k)
- end,
- __newindex = function(_, k, v)
- if v == nil then
- return vim.api.nvim_del_var(k)
- else
- return vim.api.nvim_set_var(k, v)
- end
- end,
- }),
- v = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_get_vvar(k)
- end,
- __newindex = function(_, k, v)
- return vim.api.nvim_set_vvar(k, v)
- end,
- }),
- b = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_buf_get_var(0, k)
- end,
- __newindex = function(_, k, v)
- if v == nil then
- return vim.api.nvim_buf_del_var(0, k)
- else
- return vim.api.nvim_buf_set_var(0, k, v)
- end
- end,
- }),
- w = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_win_get_var(0, k)
- end,
- __newindex = function(_, k, v)
- if v == nil then
- return vim.api.nvim_win_del_var(0, k)
- else
- return vim.api.nvim_win_set_var(0, k, v)
- end
- end,
- }),
- o = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_get_option(k)
- end,
- __newindex = function(_, k, v)
- return vim.api.nvim_set_option(k, v)
- end,
- }),
- -- TODO add warning if you try to use a window option here?
- bo = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_buf_get_option(0, k)
- end,
- __newindex = function(_, k, v)
- return vim.api.nvim_buf_set_option(0, k, v)
- end,
- }),
- wo = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_win_get_option(0, k)
- end,
- __newindex = function(_, k, v)
- -- passing v == nil will clear the value, just like above.
- return vim.api.nvim_win_set_option(0, k, v)
- end,
- }),
- env = setmetatable({}, {
- __index = function(_, k)
- return vim.api.nvim_call_function("getenv", { k })
- end,
- __newindex = function(_, k, v)
- return vim.api.nvim_call_function("setenv", { k, v })
- end,
- }),
-}, {
- __index = function(self, k)
- local mt = getmetatable(self)
- local x = mt[k]
- if x ~= nil then
- return x
- end
- local f = vim.api["nvim_" .. k]
- mt[k] = f
- return f
- end,
-})