diff --git a/lua/definitions.lua b/lua/definitions.lua index 29fc60f..6cb3d26 100644 --- a/lua/definitions.lua +++ b/lua/definitions.lua @@ -128,6 +128,9 @@ --- Enable/Disable stylized headings ---@field enable boolean? --- +--- Fallback textoff value +---@field textoff? number +--- --- Number of characters to shift per heading level ---@field shift_width number? --- @@ -145,6 +148,9 @@ --- Name of the style to use ---@field style string --- +--- Alignment for label styled headings +---@field align? string +--- --- Default highlight group used by the rest of the options --- Used for highlighting the line when the style is "simple" ---@field hl string? diff --git a/lua/markview/presets.lua b/lua/markview/presets.lua index 5a798b7..13fb4c8 100644 --- a/lua/markview/presets.lua +++ b/lua/markview/presets.lua @@ -672,428 +672,4 @@ presets.tables = { }; - ---- These are deprecated now - -presets.heading = { - ---@type { group_name: string, value: table }[] - glow_hls = { - { - group_name = "glow_h1", - value = { bg = "#74c7ec", fg = "#1e1e2e", bold = true } - }, - }, - - ---@type markview.render_config.headings - glow = { - enable = true, - - heading_1 = { - style = "label", - - hl = "glow_h1", - icon = "", - - padding_left = " ", - padding_right = " " - }, - }, - - ---@type { group_name: string, value: table }[] - simple_hls = { - { - group_name = "simple_h1", - value = { bg = "#453244", fg = "#f38ba8", bold = true } - }, - { - group_name = "simple_h2", - value = { bg = "#46393e", fg = "#fab387", bold = true } - }, - { - group_name = "simple_h3", - value = { bg = "#464245", fg = "#f9e2af", bold = true } - }, - { - group_name = "simple_h4", - value = { bg = "#374243", fg = "#a6e3a1", bold = true } - }, - { - group_name = "simple_h5", - value = { bg = "#2e3d51", fg = "#74c7ec", bold = true } - }, - { - group_name = "simple_h6", - value = { bg = "#393b54", fg = "#b4befe", bold = true } - }, - }, - - ---@type markview.render_config.headings - simple = { - enable = true, - - heading_1 = { - style = "simple", - - hl = "simple_h1" - }, - heading_2 = { - style = "simple", - - hl = "simple_h2" - }, - heading_3 = { - style = "simple", - - hl = "simple_h3" - }, - heading_4 = { - style = "simple", - - hl = "simple_h4" - }, - heading_5 = { - style = "simple", - - hl = "simple_h5" - }, - heading_6 = { - style = "simple", - - hl = "simple_h6" - }, - }, - - ---@type markview.render_config.headings - simple_better = { - enable = true, - shift_width = 0, - - heading_1 = { - style = "icon", - - icon = " ", - hl = "simple_h1" - }, - heading_2 = { - style = "icon", - - icon = " ", - hl = "simple_h2" - }, - heading_3 = { - style = "icon", - - icon = " ", - hl = "simple_h3" - }, - heading_4 = { - style = "icon", - - icon = " ", - hl = "simple_h4" - }, - heading_5 = { - style = "icon", - - icon = " ", - hl = "simple_h5" - }, - heading_6 = { - style = "icon", - - icon = " ", - hl = "simple_h6" - }, - }, - - ---@type { group_name: string, value: table }[] - label_hls = { - { - group_name = "label_h1", - value = { bg = "#f38ba8", fg = "#313244", bold = true } - }, - { - group_name = "label_h2", - value = { bg = "#fab387", fg = "#313244", bold = true } - }, - { - group_name = "label_h3", - value = { bg = "#f9e2af", fg = "#313244", bold = true } - }, - { - group_name = "label_h4", - value = { bg = "#a6e3a1", fg = "#313244", bold = true } - }, - { - group_name = "label_h5", - value = { bg = "#74c7ec", fg = "#313244", bold = true } - }, - { - group_name = "label_h6", - value = { bg = "#b4befe", fg = "#313244", bold = true } - }, - }, - - ---@type { group_name: string, value: table }[] - label_hls_dark = { - { - group_name = "label_h1", - value = { bg = "#453244", fg = "#f38ba8", bold = true } - }, - { - group_name = "label_h2", - value = { bg = "#46393e", fg = "#fab387", bold = true } - }, - { - group_name = "label_h3", - value = { bg = "#464245", fg = "#f9e2af", bold = true } - }, - { - group_name = "label_h4", - value = { bg = "#374243", fg = "#a6e3a1", bold = true } - }, - { - group_name = "label_h5", - value = { bg = "#2e3d51", fg = "#74c7ec", bold = true } - }, - { - group_name = "label_h6", - value = { bg = "#393b54", fg = "#b4befe", bold = true } - }, - }, - - ---@type markview.render_config.headings - label = { - enable = true, - shift_width = 0, - - heading_1 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - hl = "label_h1" - }, - heading_2 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - hl = "label_h2" - }, - heading_3 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - hl = "label_h3" - }, - heading_4 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - hl = "label_h4" - }, - heading_5 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - hl = "label_h5" - }, - heading_6 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - hl = "label_h6" - }, - }, - - ---@type { group_name: string, value: table }[] - decorated_hls = { - { - group_name = "decorated_h1", - value = { bg = "#f38ba8", fg = "#313244", bold = true } - }, - { - group_name = "decorated_h1_inv", - value = { fg = "#f38ba8", bold = true } - }, - { - group_name = "decorated_h2", - value = { bg = "#fab387", fg = "#313244", bold = true } - }, - { - group_name = "decorated_h2_inv", - value = { fg = "#fab387", bold = true } - }, - { - group_name = "decorated_h3", - value = { bg = "#f9e2af", fg = "#313244", bold = true } - }, - { - group_name = "decorated_h3_inv", - value = { fg = "#f9e2af", bold = true } - }, - { - group_name = "decorated_h4", - value = { bg = "#a6e3a1", fg = "#313244", bold = true } - }, - { - group_name = "decorated_h4_inv", - value = { fg = "#a6e3a1", bold = true } - }, - { - group_name = "decorated_h5", - value = { bg = "#74c7ec", fg = "#313244", bold = true } - }, - { - group_name = "decorated_h5_inv", - value = { fg = "#74c7ec", bold = true } - }, - { - group_name = "decorated_h6", - value = { bg = "#b4befe", fg = "#313244", bold = true } - }, - { - group_name = "decorated_h6_inv", - value = { fg = "#b4befe", bold = true } - }, - }, - - ---@type { group_name: string, value: table }[] - decorated_hls_dark = { - { - group_name = "decorated_h1", - value = { bg = "#453244", fg = "#f38ba8", bold = true } - }, - { - group_name = "decorated_h1_inv", - value = { fg = "#453244", bold = true } - }, - { - group_name = "decorated_h2", - value = { bg = "#46393e", fg = "#fab387", bold = true } - }, - { - group_name = "decorated_h2_inv", - value = { fg = "#46393e", bold = true } - }, - { - group_name = "decorated_h3", - value = { bg = "#464245", fg = "#f9e2af", bold = true } - }, - { - group_name = "decorated_h3_inv", - value = { fg = "#464245", bold = true } - }, - { - group_name = "decorated_h4", - value = { bg = "#374243", fg = "#a6e3a1", bold = true } - }, - { - group_name = "decorated_h4_inv", - value = { fg = "#374243", bold = true } - }, - { - group_name = "decorated_h5", - value = { bg = "#2e3d51", fg = "#74c7ec", bold = true } - }, - { - group_name = "decorated_h5_inv", - value = { fg = "#2e3d51", bold = true } - }, - { - group_name = "decorated_h6", - value = { bg = "#393b54", fg = "#b4befe", bold = true } - }, - { - group_name = "decorated_h6_inv", - value = { fg = "#393b54", bold = true } - }, - }, - - - ---@type markview.render_config.headings - decorated = { - enable = true, - shift_width = 0, - - heading_1 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - corner_right = "", - corner_right_hl = "decorated_h1_inv", - - hl = "decorated_h1" - }, - heading_2 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - corner_right = "", - corner_right_hl = "decorated_h2_inv", - - hl = "decorated_h2" - }, - heading_3 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - corner_right = "", - corner_right_hl = "decorated_h3_inv", - - hl = "decorated_h3" - }, - heading_4 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - corner_right = "", - corner_right_hl = "decorated_h4_inv", - - hl = "decorated_h4" - }, - heading_5 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - corner_right = "", - corner_right_hl = "decorated_h5_inv", - - hl = "decorated_h5" - }, - heading_6 = { - style = "label", - - padding_left = " ", - padding_right = " ", - - corner_right = "", - corner_right_hl = "decorated_h6_inv", - - hl = "decorated_h6" - }, - }, -}; - return presets; diff --git a/lua/markview/renderer.lua b/lua/markview/renderer.lua index 5f51f20..138c7bf 100644 --- a/lua/markview/renderer.lua +++ b/lua/markview/renderer.lua @@ -885,13 +885,57 @@ renderer.render_headings = function (buffer, content, config) local conceal_start = string.match(content.line, "^[#]+(%s*)"); local line_length = #content.line; + local space = shift * (content.level - 1); + + if conf.align then + if conf.align == "left" then + space = 0; + elseif conf.align == "center" then + local win = vim.api.nvim_get_current_win(); + local textoff = config.textoff or vim.fn.getwininfo(win)[1].textoff; + + local w = vim.api.nvim_win_get_width(win) - textoff; + + local t = vim.fn.strdisplaywidth(table.concat({ + conf.corner_left or "", + conf.padding_left or "", + conf.icon or "", + + content.title, + + conf.padding_right or "", + conf.corner_right or "", + })); + + space = math.floor((w - t) / 2); + else + local win = vim.api.nvim_get_current_win(); + local textoff = config.textoff or vim.fn.getwininfo(win)[1].textoff; + + local w = vim.api.nvim_win_get_width(win) - textoff; + + local t = vim.fn.strdisplaywidth(table.concat({ + conf.corner_left or "", + conf.padding_left or "", + conf.icon or "", + + content.title, + + conf.padding_right or "", + conf.corner_right or "", + })); + + space = w - t; + end + end + -- Heading rules -- 1. Must start at the first column -- 2. Must have 1 space between the marker and the title vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start, { virt_text_pos = "inline", virt_text = { - { string.rep(conf.shift_char or " ", shift * (content.level - 1)), conf.shift_hl }, + { string.rep(conf.shift_char or " ", space), conf.shift_hl }, { conf.corner_left or "", set_hl(conf.corner_left_hl) or set_hl(conf.hl) }, { conf.padding_left or "", set_hl(conf.padding_left_hl) or set_hl(conf.hl) }, diff --git a/markview.nvim.wiki b/markview.nvim.wiki index bc16d65..c0cd541 160000 --- a/markview.nvim.wiki +++ b/markview.nvim.wiki @@ -1 +1 @@ -Subproject commit bc16d6519494612460ce37dd3908b8680c7e22a3 +Subproject commit c0cd5413f5154313a7c0ad1705ec77e0fb578768