aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua')
-rw-r--r--lua/colorizer.lua38
-rw-r--r--lua/nvim.lua193
2 files changed, 222 insertions, 9 deletions
diff --git a/lua/colorizer.lua b/lua/colorizer.lua
index c53057d..65d1c64 100644
--- a/lua/colorizer.lua
+++ b/lua/colorizer.lua
@@ -55,10 +55,15 @@ local function color_is_bright(r, g, b)
end
end
-local HIGHLIGHT_NAME_PREFIX = "colorizer_"
+local HIGHLIGHT_NAME_PREFIX = "colorizer"
+local MODE_NAMES = {
+ background = 'mb';
+ foreground = 'mf';
+}
+
--- Make a deterministic name for a highlight given these attributes
-local function make_highlight_name(rgb)
- return table.concat {HIGHLIGHT_NAME_PREFIX, rgb}
+local function make_highlight_name(rgb, mode)
+ return table.concat({HIGHLIGHT_NAME_PREFIX, MODE_NAMES[mode], rgb}, '_')
end
local highlight_cache = {}
@@ -69,9 +74,11 @@ local function table_is_empty(t)
end
local function create_highlight(rgb_hex, options)
+ local mode = options.mode or 'background'
-- TODO validate rgb format?
rgb_hex = rgb_hex:lower()
- local highlight_name = highlight_cache[rgb_hex]
+ local cache_key = table.concat({MODE_NAMES[mode], rgb_hex}, "_")
+ local highlight_name = highlight_cache[cache_key]
-- Look up in our cache.
if not highlight_name then
if #rgb_hex == 3 then
@@ -82,8 +89,8 @@ local function create_highlight(rgb_hex, options)
}
end
-- Create the highlight
- highlight_name = make_highlight_name(rgb_hex)
- if options.mode == 'foreground' then
+ highlight_name = make_highlight_name(rgb_hex, mode)
+ if mode == 'foreground' then
nvim.ex.highlight(highlight_name, "guifg=#"..rgb_hex)
else
local r, g, b = rgb_hex:sub(1,2), rgb_hex:sub(3,4), rgb_hex:sub(5,6)
@@ -96,7 +103,7 @@ local function create_highlight(rgb_hex, options)
end
nvim.ex.highlight(highlight_name, "guifg="..fg_color, "guibg=#"..rgb_hex)
end
- highlight_cache[rgb_hex] = highlight_name
+ highlight_cache[cache_key] = highlight_name
end
return highlight_name
end
@@ -197,7 +204,6 @@ local function highlight_buffer(buf, ns, lines, line_start, options)
if prefix then
local rgb = COLOR_MAP[prefix]
local rgb_hex = bit.tohex(rgb):sub(-6)
- -- TODO figure out proper background/foreground
local highlight_name = create_highlight(rgb_hex, options)
nvim.buf_add_highlight(buf, ns, highlight_name, current_linenum, i-1, i+#prefix-1)
i = i + #prefix
@@ -240,6 +246,15 @@ local function attach_to_buffer(buf, options)
})
end
+local filetype_options = {}
+local function reload_buffer()
+ local options = filetype_options[nvim.bo.filetype] or filetype_options.default
+ local ns = DEFAULT_NAMESPACE
+ nvim.buf_clear_namespace(0, ns, 0, -1)
+ local lines = nvim.buf_get_lines(0, 0, -1, true)
+ highlight_buffer(0, ns, lines, 0, options)
+end
+
--- Easy to use function if you want the full setup without fine grained control.
-- Setup an autocmd which enables colorizing for the filetypes and options specified.
--
@@ -265,7 +280,10 @@ local function setup(filetypes, default_options)
return
end
initialize_trie()
- local filetype_options = {}
+ filetype_options = {}
+ if default_options then
+ filetype_options.default = default_options
+ end
function COLORIZER_SETUP_HOOK()
local filetype = nvim.bo.filetype
local options = filetype_options[filetype] or default_options
@@ -285,6 +303,7 @@ local function setup(filetypes, default_options)
nvim.err_writeln("colorizer: Invalid option type for filetype "..filetype)
else
options = vim.tbl_extend("keep", v, default_options)
+ assert(MODE_NAMES[options.mode or 'background'], "colorizer: Invalid mode: "..tostring(options.mode))
end
else
filetype = v
@@ -303,6 +322,7 @@ return {
setup = setup;
attach_to_buffer = attach_to_buffer;
highlight_buffer = highlight_buffer;
+ reload_buffer = reload_buffer;
-- initialize = initialize_trie;
}
diff --git a/lua/nvim.lua b/lua/nvim.lua
new file mode 100644
index 0000000..c51d2ca
--- /dev/null
+++ b/lua/nvim.lua
@@ -0,0 +1,193 @@
+--- 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
+})
+
+