diff --git a/RECIPES.md b/RECIPES.md index 7b61202..88c1097 100644 --- a/RECIPES.md +++ b/RECIPES.md @@ -14,6 +14,8 @@ These are some simple examples. You can customise the headings like `statusline` component via the `label` style. +![better_headings](https://github.com/OXY2DEV/markview.nvim/blob/images/Recipes/better_headings.jpg) + This allows adding corners, paddings & icons to them. Let's start with something simple. Run the following code. @@ -123,15 +125,180 @@ vim.cmd("Markview enableAll"); If you want to apply this to the all the headings then `markview` comes a preset for that. +Just add this to your config table. + +>[!CAUTION] +> The colors are meant to only be added once so running it now will not add the highlight groups. + ```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 + headings = heading_presets.decorated_labels +}); +``` + +## 📏 Centered horizontal rules + +By default, the icon in the middle of the `horizontal rules` aren't centered. + +This is due to not being able to check the correct `textoff` value. + +But if you manually set it up you can center it. + +Let's start by creating a repeating part. + +```lua +require("markview").setup({ + horizontal_rules = { + parts = { + { + type = "repeating", + text = "─", + + repeat_amount = function () + local w = vim.api.nvim_win_get_width(0); + local l = vim.api.nvim_buf_line_count(0); + + l = vim.fn.strchars(tostring(l)) + 4; + + return math.floor((w - (l + 3)) / 2); + end + } + } + } }); ``` +Here's a HR to see what it does. + +--- + +Now, let's explain what this does. + +`w` is the current windows width and `l` is the amount of lines in the current buffer. + +Now, we can turn `l` into a string and count how many character it has(using `vim.fn.strchars()`). This is the number of columns the line number will take in the statuscolumn. + +In my case, I have a signcolumn(this takes 2 columns) a foldcolumn(this takes 1 column) and a separator which also takes a character. So we add 2 + 1 + 1 = 4 to the value of `l`. This is how wide my statuscolumn is. + +Now, since I want `  `(3 characters) I also add 3 to `l` in the final result. +This is going to be the left side of the horizontal rule. + +Now, we copy it but this time replace `math.floor` with `math.ceil` to handle when we have an odd number of free columns. + +Finally we make the center part. + +```lua +{ + type = "text", + text = "  " +} +``` + +By combining them we get this, + +```lua +require("markview").setup({ + horizontal_rules = { + parts = { + { + type = "repeating", + text = "─", + + repeat_amount = function () + local w = vim.api.nvim_win_get_width(0); + local l = vim.api.nvim_buf_line_count(0); + + l = vim.fn.strchars(tostring(l)) + 4; + + return math.floor((w - (l + 3)) / 2); + end + }, + { + type = "text", + text = "  " + }, + { + type = "repeating", + text = "─", + + repeat_amount = function () + local w = vim.api.nvim_win_get_width(0); + local l = vim.api.nvim_buf_line_count(0); + + l = vim.fn.strchars(tostring(l)) + 4; + + return math.ceil((w - (l + 3)) / 2); + end + }, + } + } +}); +``` + +Now, use the default `highlight groups` and you are done. + +>[!Tip] +> `direction` can be used to apply highlights to a specific side of the text. + +```lua +require("markview").setup({ + horizontal_rules = { + parts = { + { + type = "repeating", + text = "─", + + direction = "left", + hl = { + "Gradient1", "Gradient2", + "Gradient3", "Gradient4", + "Gradient5", "Gradient6", + "Gradient7", "Gradient8", + "Gradient9", "Gradient10" + }, + + repeat_amount = function () + local w = vim.api.nvim_win_get_width(0); + local l = vim.api.nvim_buf_line_count(0); + + l = vim.fn.strchars(tostring(l)) + 4; + + return math.floor((w - (l + 3)) / 2); + end + }, + { + type = "text", + text = "  " + }, + { + type = "repeating", + text = "─", + + direction = "right", + hl = { + "Gradient1", "Gradient2", + "Gradient3", "Gradient4", + "Gradient5", "Gradient6", + "Gradient7", "Gradient8", + "Gradient9", "Gradient10" + }, + + repeat_amount = function () + local w = vim.api.nvim_win_get_width(0); + local l = vim.api.nvim_buf_line_count(0); + + l = vim.fn.strchars(tostring(l)) + 4; + + return math.ceil((w - (l + 3)) / 2); + end + }, + } + } +}); +``` diff --git a/lua/markview.lua b/lua/markview.lua index d44ba71..5b808d8 100644 --- a/lua/markview.lua +++ b/lua/markview.lua @@ -37,12 +37,16 @@ markview.deep_merge = function (behavior, tbl_1, tbl_2) 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); + if not tbl_1.overwrite or tbl_1.overwrite and not vim.list_contains(tbl_1.overwrite, key) 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 + else + tbl_1[key] = value; end elseif type(value) == "table" then tbl_1[key] = markview.deep_merge(behavior, tbl_1[key], value); @@ -1117,6 +1121,7 @@ markview.configuration = { block_quotes = { enable = true, + overwrite = { "callouts" }, default = { border = "▋", border_hl = "MarkviewBlockQuoteDefault" @@ -1265,6 +1270,7 @@ markview.configuration = { }, horizontal_rules = { enable = true, + overwrite = { "parts" }, parts = { { @@ -1594,6 +1600,8 @@ markview.setup = function (user_config) if vim.islist(markview.configuration.highlight_groups) then markview.add_hls(markview.configuration.highlight_groups); end + + markview.commands.enableAll(); end return markview;