diff --git a/Recipes.md b/Recipes.md new file mode 100644 index 0000000..7b61202 --- /dev/null +++ b/Recipes.md @@ -0,0 +1,137 @@ +# 🍰 Recipes + +Markview is a highly customisable plugin and allows customisation of everything. + +These are some simple examples. + +>[!NOTE] +> It is recommended that you view this with a `nerd font`. + +>[!Important] +> It is recommended to view this file inside `Neovim`. + +## 🔖 Better headings + +You can customise the headings like `statusline` component via the `label` style. + +This allows adding corners, paddings & icons to them. + +Let's start with something simple. Run the following code. + +>[!Tip] +> Select the code block using visual mode and run the `:lua` command. + +```lua +require("markview").setup({ + headings = { + enable = true, + shift_width = 0, + + heading_1 = { + style = "label", + + padding_left = " ", + padding_right = " ", + + hl = "MarkviewHeading1" + } + } +}); + +-- This is to prevent needing to manually refresh the view. +vim.cmd("Markview enableAll"); +``` + +Now, Let's do a bit more customisation. + +```lua +require("markview").setup({ + headings = { + enable = true, + shift_width = 0, + + heading_1 = { + style = "label", + + padding_left = " ", + padding_right = " ", + + corner_right = "", + corner_right_hl = "Heading1Corner", + + hl = "Heading1" + } + } +}); + +vim.cmd("Markview enableAll"); +``` + +Looks messy right? Let's fix that. + +We first create a new highlight group to make it look less crappy. + +```lua +require("markview").setup({ + highlight_group = { + { + group_name = "Heading1", + value = { fg = "#1e1e2e", bg = "#a6e3a1" } + }, + { + group_name = "Heading1Corner", + value = { fg = "#a6e3a1" } + }, + } +}); +``` + +Now, we modify the previous code block a bit. + +```lua +require("markview").setup({ + highlight_groups = { + { + group_name = "Heading1", + value = { fg = "#1e1e2e", bg = "#a6e3a1" } + }, + { + group_name = "Heading1Corner", + value = { fg = "#a6e3a1" } + }, + }, + headings = { + enable = true, + shift_width = 0, + + heading_1 = { + style = "label", + + padding_left = " ", + padding_right = " ", + + corner_right = "", + corner_right_hl = "Heading1Corner", + + hl = "Heading1" + } + } +}); + +vim.cmd("Markview enableAll"); +``` + +If you want to apply this to the all the headings then `markview` comes a preset for that. + +```lua +local heading_presets = require("markview.presets").headings; +local hl_presets = require("markview.presets").highlight_groups; + +require("markview").setup({ + highlight_groups = hl_presets.h_decorated, + headings = heading_presets.decorated +}); +``` + + + diff --git a/ftplugin/markdown.lua b/ftplugin/markdown.lua index a1de838..8d9aed0 100644 --- a/ftplugin/markdown.lua +++ b/ftplugin/markdown.lua @@ -8,7 +8,7 @@ end -- Check for requirements if vim.fn.has("nvim-0.10") == 0 then - vim.notify("[ markview.nvim ] : Thie plugin is only available on version 0.10.0 and higher!", vim.log.levels.WARN); + vim.notify("[ markview.nvim ] : This plugin is only available on version 0.10.0 and higher!", vim.log.levels.WARN); return; elseif not parser_installed("markdown") then vim.notify("[ markview.nvim ] : Treesitter parser for 'markdown' wasn't found!", vim.log.levels.WARN); diff --git a/lua/markview.lua b/lua/markview.lua index 9de42d1..d44ba71 100644 --- a/lua/markview.lua +++ b/lua/markview.lua @@ -7,14 +7,91 @@ markview.keymaps = require("markview.keymaps"); markview.colors = require("markview.colors"); +markview.list_contains = function (tbl, value) + for index, item in ipairs(tbl) do + if item == value or vim.deep_equal(item, value) then + return true, index; + end + end + + return false; +end + +markview.deep_merge = function (behavior, tbl_1, tbl_2) + if not tbl_1 or type(tbl_1) ~= "table" then + tbl_1 = {}; + end + + if not tbl_2 or type(tbl_2) ~= "table" then + tbl_2 = {}; + end + + for key, value in pairs(tbl_2) do + if not tbl_1[key] then + tbl_1[key] = value; + goto skip; + end + + if type(value) ~= type(tbl_1[key]) then + goto skip; + end + + if vim.islist(value) then + for index, item in ipairs(value) do + if not markview.list_contains(tbl_1[key], item) then + table.insert(tbl_1[key], item); + else + tbl_1[key][index] = markview.deep_merge(behavior, tbl_1[key][index], item); + end + end + elseif type(value) == "table" then + tbl_1[key] = markview.deep_merge(behavior, tbl_1[key], value); + elseif behavior == "force" then + tbl_1[key] = value; + end + + ::skip:: + end + + return tbl_1; +end + +markview.hl_exits = function (hl_list, hl) + for index, item in ipairs(hl_list) do + if item.group_name == hl.group_name then + return true, index; + end + end + + return false; +end + markview.add_hls = function (obj) + local added = {}; local use_hl = {}; for _, hl in ipairs(obj) do if hl.output and type(hl.output) == "function" and pcall(hl.output) then - use_hl = vim.list_extend(use_hl, hl.output()) + local _o = hl.output(); + + for _, item in ipairs(_o) do + local exists, index = markview.hl_exits(use_hl, item); + + if exists == true then + table.remove(use_hl, index); + end + end + + use_hl = vim.list_extend(use_hl, _o) elseif hl.group_name and hl.value then - table.insert(use_hl, hl) + local contains, index = markview.list_contains(added, hl.group_name); + + if contains == true and index then + use_hl[index] = hl; + else + table.insert(use_hl, hl) + table.insert(added, hl.group_name); + end end end @@ -25,7 +102,6 @@ markview.add_hls = function (obj) _opt = hl.value(); end - _opt.default = true; vim.api.nvim_set_hl(0, "Markview" .. hl.group_name, _opt); end end @@ -90,8 +166,8 @@ markview.configuration = { } }, } - elseif markview.colors.get_hl_value(0, "DiagnosticOk", "fg") and markview.colors.get_hl_value(0, "Normal", "bg") then - local bg = markview.colors.get_hl_value(0, "Normal", "bg"); + elseif markview.colors.get_hl_value(0, "DiagnosticOk", "fg") and markview.colors.bg() then + local bg = markview.colors.bg(); local fg = markview.colors.get_hl_value(0, "DiagnosticOk", "fg"); local nr = markview.colors.get_hl_value(0, "LineNr", "bg"); @@ -179,8 +255,8 @@ markview.configuration = { } }, } - elseif markview.colors.get_hl_value(0, "DiagnosticHint", "fg") and markview.colors.get_hl_value(0, "Normal", "bg") then - local bg = markview.colors.get_hl_value(0, "Normal", "bg"); + elseif markview.colors.get_hl_value(0, "DiagnosticHint", "fg") and markview.colors.bg() then + local bg = markview.colors.bg(); local fg = markview.colors.get_hl_value(0, "DiagnosticHint", "fg"); local nr = markview.colors.get_hl_value(0, "LineNr", "bg"); @@ -268,8 +344,8 @@ markview.configuration = { } }, } - elseif markview.colors.get_hl_value(0, "DiagnosticInfo", "fg") and markview.colors.get_hl_value(0, "Normal", "bg") then - local bg = markview.colors.get_hl_value(0, "Normal", "bg"); + elseif markview.colors.get_hl_value(0, "DiagnosticInfo", "fg") and markview.colors.bg() then + local bg = markview.colors.bg(); local fg = markview.colors.get_hl_value(0, "DiagnosticInfo", "fg"); local nr = markview.colors.get_hl_value(0, "LineNr", "bg"); @@ -357,8 +433,8 @@ markview.configuration = { } }, } - elseif markview.colors.get_hl_value(0, "Special", "fg") and markview.colors.get_hl_value(0, "Normal", "bg") then - local bg = markview.colors.get_hl_value(0, "Normal", "bg"); + elseif markview.colors.get_hl_value(0, "Special", "fg") and markview.colors.bg() then + local bg = markview.colors.bg(); local fg = markview.colors.get_hl_value(0, "Special", "fg"); local nr = markview.colors.get_hl_value(0, "LineNr", "bg"); @@ -446,8 +522,8 @@ markview.configuration = { } }, } - elseif markview.colors.get_hl_value(0, "DiagnosticWarn", "fg") and markview.colors.get_hl_value(0, "Normal", "bg") then - local bg = markview.colors.get_hl_value(0, "Normal", "bg"); + elseif markview.colors.get_hl_value(0, "DiagnosticWarn", "fg") and markview.colors.bg() then + local bg = markview.colors.bg(); local fg = markview.colors.get_hl_value(0, "DiagnosticWarn", "fg"); local nr = markview.colors.get_hl_value(0, "LineNr", "bg"); @@ -535,8 +611,8 @@ markview.configuration = { } }, } - elseif markview.colors.get_hl_value(0, "DiagnosticError", "fg") and markview.colors.get_hl_value(0, "Normal", "bg") then - local bg = markview.colors.get_hl_value(0, "Normal", "bg"); + elseif markview.colors.get_hl_value(0, "DiagnosticError", "fg") and markview.colors.bg() then + local bg = markview.colors.bg(); local fg = markview.colors.get_hl_value(0, "DiagnosticError", "fg"); local nr = markview.colors.get_hl_value(0, "LineNr", "bg"); @@ -684,6 +760,8 @@ markview.configuration = { local bg = markview.colors.get({ markview.colors.get_hl_value(0, "Normal", "bg"), markview.colors.get_hl_value(0, "Cursor", "fg"), + markview.colors.get_hl_value(0, "EndOfBuffer", "bg"), + markview.colors.get_hl_value(0, "EndOfBuffer", "fg"), vim.o.background == "dark" and "#1e1e2e" or "#cdd6f4" }); @@ -696,8 +774,9 @@ markview.configuration = { default = true }; else + vim.print(luminosity) return { - bg = markview.colors.mix(bg, bg, 1, math.min(luminosity, 0.25) * -1), + bg = markview.colors.mix(bg, bg, 1, math.max(1 - luminosity, 0.05) * -1), default = true }; end @@ -709,6 +788,8 @@ markview.configuration = { local bg = markview.colors.get({ markview.colors.get_hl_value(0, "Normal", "bg"), markview.colors.get_hl_value(0, "Cursor", "fg"), + markview.colors.get_hl_value(0, "EndOfBuffer", "bg"), + markview.colors.get_hl_value(0, "EndOfBuffer", "fg"), vim.o.background == "dark" and "#1e1e2e" or "#cdd6f4" }); @@ -736,6 +817,8 @@ markview.configuration = { local bg = markview.colors.get({ markview.colors.get_hl_value(0, "Normal", "bg"), markview.colors.get_hl_value(0, "Cursor", "fg"), + markview.colors.get_hl_value(0, "EndOfBuffer", "bg"), + markview.colors.get_hl_value(0, "EndOfBuffer", "fg"), vim.o.background == "dark" and "#1e1e2e" or "#cdd6f4" }); @@ -749,7 +832,7 @@ markview.configuration = { }; else return { - bg = markview.colors.mix(bg, bg, 1, math.min(luminosity, 0.5) * -1), + bg = markview.colors.mix(bg, bg, 1, math.min(luminosity, 0.15) * -1), default = true }; end @@ -1506,7 +1589,7 @@ end, { markview.setup = function (user_config) ---@type markview.config -- Merged configuration tables - markview.configuration = vim.tbl_deep_extend("keep", user_config or {}, markview.configuration); + markview.configuration = markview.deep_merge("force", markview.configuration, user_config or {}); if vim.islist(markview.configuration.highlight_groups) then markview.add_hls(markview.configuration.highlight_groups); diff --git a/lua/markview/colors.lua b/lua/markview/colors.lua index 26552a8..2e41c84 100644 --- a/lua/markview/colors.lua +++ b/lua/markview/colors.lua @@ -8,7 +8,16 @@ colors.lerp = function (x, y, t) return x + ((y - x) * t); end +--- I really hate to do this but this needs to be done +-- colors.bg_map = { +-- ---+ ##code## +-- ["witch-dark"] = "#161f31", +-- ["witch-light"] = "#dee2ea", +-- ---_ +-- }; + colors.name_to_hex = function (name) + ---+ ##code## local lookup = { ["red"] = "#FF0000", ["lightred"] = "#FFBBBB", ["darkred"] = "#8B0000", ["green"] = "#00FF00", ["lightgreen"] = "#90EE90", ["darkgreen"] = "#006400", ["seagreen"] = "#2E8B57", @@ -38,6 +47,7 @@ colors.name_to_hex = function (name) ["nvimdarkred"] = "#590008", ["nvimlightred"] = "#FFC0B9", ["nvimdarkyellow"] = "#6B5300", ["nvimlightyellow"] = "#FCE094", }; + ---_ return lookup[string.lower(name)] or lookup_nvim[string.lower(name)]; end @@ -283,4 +293,12 @@ colors.get = function (col_list) return col_list[1]; end +colors.bg = function () + return colors.get({ + colors.get_hl_value(0, "Normal", "bg"), + colors.get_hl_value(0, "EndOfBuffer", "bg"), + colors.get_hl_value(0, "EndOfBuffer", "fg"), + }) +end + return colors; diff --git a/lua/markview/presets.lua b/lua/markview/presets.lua index b8774bd..5a798b7 100644 --- a/lua/markview/presets.lua +++ b/lua/markview/presets.lua @@ -2,7 +2,7 @@ local presets = {}; local colors = require("markview.colors"); presets.highlight_groups = { - colorful_heading_bg = { + h_decorated = { ---+ ##code## { -- Heading level 1 @@ -11,8 +11,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = colors.get_hl_value(0, "DiagnosticOk", "fg"); - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading1", @@ -24,9 +22,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading1Sign", + group_name = "Heading1Corner", value = { - bg = nr, fg = bg, default = true @@ -37,8 +34,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = vim.o.background == "dark" and "#a6e3a1" or "#40a02b"; - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading1", @@ -50,9 +45,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading1Sign", + group_name = "Heading1Corner", value = { - bg = nr, fg = bg, default = true @@ -69,8 +63,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = colors.get_hl_value(0, "DiagnosticHint", "fg"); - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading2", @@ -82,9 +74,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading2Sign", + group_name = "Heading2Corner", value = { - bg = nr, fg = bg, default = true @@ -95,8 +86,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = vim.o.background == "dark" and "#94e2d5" or "#179299"; - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading2", @@ -108,9 +97,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading2Sign", + group_name = "Heading2Corner", value = { - bg = nr, fg = bg, default = true @@ -127,8 +115,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = colors.get_hl_value(0, "DiagnosticInfo", "fg"); - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading3", @@ -140,9 +126,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading3Sign", + group_name = "Heading3Corner", value = { - bg = nr, fg = bg, default = true @@ -153,8 +138,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = vim.o.background == "dark" and "#89dceb" or "#179299"; - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading3", @@ -166,9 +149,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading3Sign", + group_name = "Heading3Corner", value = { - bg = nr, fg = bg, default = true @@ -185,8 +167,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = colors.get_hl_value(0, "Special", "fg"); - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading4", @@ -198,9 +178,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading4Sign", + group_name = "Heading4Corner", value = { - bg = nr, fg = bg, default = true @@ -211,8 +190,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = vim.o.background == "dark" and "#f5c2e7" or "#ea76cb"; - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading4", @@ -224,9 +201,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading4Sign", + group_name = "Heading4Corner", value = { - bg = nr, fg = bg, default = true @@ -243,8 +219,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = colors.get_hl_value(0, "DiagnosticWarn", "fg"); - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading5", @@ -256,9 +230,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading5Sign", + group_name = "Heading5Corner", value = { - bg = nr, fg = bg, default = true @@ -269,8 +242,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = vim.o.background == "dark" and "#F9E3AF" or "#DF8E1D"; - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading5", @@ -282,9 +253,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading5Sign", + group_name = "Heading5Corner", value = { - bg = nr, fg = bg, default = true @@ -301,8 +271,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = colors.get_hl_value(0, "DiagnosticError", "fg"); - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading6", @@ -314,9 +282,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading6Sign", + group_name = "Heading6Corner", value = { - bg = nr, fg = bg, default = true @@ -327,8 +294,6 @@ presets.highlight_groups = { local fg = colors.get_hl_value(0, "Normal", "bg"); local bg = vim.o.background == "dark" and "#F38BA8" or "#D20F39"; - local nr = colors.get_hl_value(0, "LineNr", "bg"); - return { { group_name = "Heading6", @@ -340,9 +305,8 @@ presets.highlight_groups = { } }, { - group_name = "Heading6Sign", + group_name = "Heading6Corner", value = { - bg = nr, fg = bg, default = true @@ -451,7 +415,7 @@ presets.headings = { padding_right = " ", corner_right = "", - corner_right_hl = "MarkviewHeading1Sign", + corner_right_hl = "MarkviewHeading1Corner", hl = "MarkviewHeading1" }, @@ -462,9 +426,9 @@ presets.headings = { padding_right = " ", corner_right = "", - corner_right_hl = "MarkviewHeading2", + corner_right_hl = "MarkviewHeading2Corner", - hl = "MarkviewHeading2Sign" + hl = "MarkviewHeading2" }, heading_3 = { style = "label", @@ -473,9 +437,9 @@ presets.headings = { padding_right = " ", corner_right = "", - corner_right_hl = "MarkviewHeading3", + corner_right_hl = "MarkviewHeading3Corner", - hl = "MarkviewHeading3Sign" + hl = "MarkviewHeading3" }, heading_4 = { style = "label", @@ -484,9 +448,9 @@ presets.headings = { padding_right = " ", corner_right = "", - corner_right_hl = "MarkviewHeading4", + corner_right_hl = "MarkviewHeading4Corner", - hl = "MarkviewHeading4Sign" + hl = "MarkviewHeading4" }, heading_5 = { style = "label", @@ -495,9 +459,9 @@ presets.headings = { padding_right = " ", corner_right = "", - corner_right_hl = "MarkviewHeading5", + corner_right_hl = "MarkviewHeading5Corner", - hl = "MarkviewHeading5Sign" + hl = "MarkviewHeading5" }, heading_6 = { style = "label", @@ -506,9 +470,9 @@ presets.headings = { padding_right = " ", corner_right = "", - corner_right_hl = "MarkviewHeading6", + corner_right_hl = "MarkviewHeading6Corner", - hl = "MarkviewHeading6Sign" + hl = "MarkviewHeading6" }, }, @@ -1069,7 +1033,7 @@ presets.heading = { padding_left = " ", padding_right = " ", - corner_right = "", + corner_right = "", corner_right_hl = "decorated_h1_inv", hl = "decorated_h1" @@ -1080,7 +1044,7 @@ presets.heading = { padding_left = " ", padding_right = " ", - corner_right = "", + corner_right = "", corner_right_hl = "decorated_h2_inv", hl = "decorated_h2" @@ -1091,7 +1055,7 @@ presets.heading = { padding_left = " ", padding_right = " ", - corner_right = "", + corner_right = "", corner_right_hl = "decorated_h3_inv", hl = "decorated_h3" @@ -1102,7 +1066,7 @@ presets.heading = { padding_left = " ", padding_right = " ", - corner_right = "", + corner_right = "", corner_right_hl = "decorated_h4_inv", hl = "decorated_h4" @@ -1113,7 +1077,7 @@ presets.heading = { padding_left = " ", padding_right = " ", - corner_right = "", + corner_right = "", corner_right_hl = "decorated_h5_inv", hl = "decorated_h5" @@ -1124,7 +1088,7 @@ presets.heading = { padding_left = " ", padding_right = " ", - corner_right = "", + corner_right = "", corner_right_hl = "decorated_h6_inv", hl = "decorated_h6" diff --git a/lua/markview/renderer.lua b/lua/markview/renderer.lua index 08e9f4c..adfbe92 100644 --- a/lua/markview/renderer.lua +++ b/lua/markview/renderer.lua @@ -854,7 +854,7 @@ renderer.render_headings = function (buffer, content, config) }); elseif conf.style == "label" then local conceal_start = string.match(content.line, "^[#]+(%s*)"); - local line_length = vim.fn.strchars(content.line); + local line_length = #content.line; -- Heading rules -- 1. Must start at the first column @@ -876,8 +876,10 @@ renderer.render_headings = function (buffer, content, config) conceal = "" }); + vim.api.nvim_buf_add_highlight(buffer, renderer.namespace, set_hl(conf.hl), content.row_start, 0, line_length); + vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, line_length, { - virt_text_pos = "inline", + virt_text_pos = "overlay", virt_text = { { conf.padding_right or "", set_hl(conf.padding_right_hl) or set_hl(conf.hl) }, { conf.corner_right or "", set_hl(conf.corner_right_hl) or set_hl(conf.hl) } @@ -885,8 +887,6 @@ renderer.render_headings = function (buffer, content, config) hl_mode = "combine" }); - - vim.api.nvim_buf_add_highlight(buffer, renderer.namespace, set_hl(conf.hl), content.row_start, 0, line_length); elseif conf.style == "icon" then local conceal_start = string.match(content.line, "^[#]+(%s*)"); diff --git a/markview.nvim.wiki b/markview.nvim.wiki index 873da3f..29c0ecd 160000 --- a/markview.nvim.wiki +++ b/markview.nvim.wiki @@ -1 +1 @@ -Subproject commit 873da3fa850e608a7c51023bbd54bae453757b66 +Subproject commit 29c0ecd5ca27c5402bafdfee43e131af7235989a