diff options
author | Gabriel Holodak <gthepiper@gmail.com> | 2017-11-20 00:19:08 -0500 |
---|---|---|
committer | Gabriel Holodak <gthepiper@gmail.com> | 2017-12-27 23:27:14 -0500 |
commit | c28ce5f6191d233228d1688de300727e1ae42831 (patch) | |
tree | 409b1488d3f50369a0dd861ec97a20459d2ba604 /runtime/lua/man.lua | |
parent | 0446d4d6916d27041de5ac24ba0c741ae4ad5a39 (diff) | |
download | rneovim-c28ce5f6191d233228d1688de300727e1ae42831.tar.gz rneovim-c28ce5f6191d233228d1688de300727e1ae42831.tar.bz2 rneovim-c28ce5f6191d233228d1688de300727e1ae42831.zip |
Switch to processing in Lua
Diffstat (limited to 'runtime/lua/man.lua')
-rw-r--r-- | runtime/lua/man.lua | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/runtime/lua/man.lua b/runtime/lua/man.lua new file mode 100644 index 0000000000..63c2c14480 --- /dev/null +++ b/runtime/lua/man.lua @@ -0,0 +1,75 @@ +local function highlight_backspaced(line, linenr) + local chars = {} + local prev_char = '' + local overstrike = false + local hls = {} -- Store highlight groups as { attr, start, end } + local NONE, BOLD, UNDERLINE = 0, 1, 2 + local attr = NONE + local byte = 0 -- byte offset + + -- Break input into UTF8 characters + for char in line:gmatch("[^\128-\191][\128-\191]*") do + if overstrike then + local last_hl = hls[#hls] + if char == prev_char then + if char == '_' and attr == UNDERLINE and last_hl and last_hl[3] == byte then + -- This underscore is in the middle of an underlined word + attr = UNDERLINE + else + attr = BOLD + end + elseif prev_char == '_' then + -- char is underlined + attr = UNDERLINE + elseif prev_char == '+' and char == 'o' then + -- bullet (overstrike text '+^Ho') + attr = BOLD + char = [[·]] + elseif prev_char == [[·]] and char == 'o' then + -- bullet (additional handling for '+^H+^Ho^Ho') + attr = BOLD + char = [[·]] + else + -- use plain char + attr = NONE + end + + -- Grow the previous highlight group if possible + if last_hl and last_hl[1] == attr and last_hl[3] == byte then + last_hl[3] = byte + #char + else + hls[#hls + 1] = {attr, byte, byte + #char} + end + + overstrike = false + prev_char = '' + byte = byte + #char + chars[#chars + 1] = char + elseif char == "\b" then + overstrike = true + prev_char = chars[#chars] + byte = byte - #prev_char + chars[#chars] = nil + else + byte = byte + #char + chars[#chars + 1] = char + end + end + + for i, hl in ipairs(hls) do + if hl[1] ~= NONE then + vim.api.nvim_buf_add_highlight( + 0, + -1, + hl[1] == BOLD and "manBold" or "manUnderline", + linenr - 1, + hl[2], + hl[3] + ) + end + end + + return table.concat(chars, '') +end + +return { highlight_backspaced = highlight_backspaced } |