Skip to content

Commit

Permalink
red: colorize rework
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeping committed Oct 22, 2023
1 parent 4b8ade9 commit 9294d6e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions data/apps/red.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ local conf = {
cr_sym = '^',
nodump = false,
-- syntax = true,
colorize_win = 4096,
-- histfile = true,
emptymenu = '| New ',
}
Expand Down
18 changes: 9 additions & 9 deletions data/lib/red/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function syntax:match_fn(ctx, txt, i, fn, ...)
end
end

function syntax:match_start(ctx, txt, i)
local r = self:match_fn(ctx, txt, i, 'linestart')
function syntax:match_start(ctx, txt, i, epos)
local r = self:match_fn(ctx, txt, i, 'linestart', epos)
if r then
local ok = true
for pos = i-1, 1, -1 do
Expand All @@ -81,15 +81,15 @@ function syntax:match_start(ctx, txt, i)
end
if ok then return r end
end
return self:match_fn(ctx, txt, i, 'start')
return self:match_fn(ctx, txt, i, 'start', epos)
end

function syntax:match_end(ctx, txt, i)
return self:match_fn(ctx, txt, i, 'stop',
self.stack[1] and self.stack[1][2])
end

function syntax:context(pos)
function syntax:context(pos, epos)
local ctx = self.ctx
local txt = self.txt
local cols = self.cols
Expand Down Expand Up @@ -119,7 +119,7 @@ function syntax:context(pos)
return found_len
end

local d = self:match_end(ctx, txt, pos)
local d = self:match_end(ctx, txt, pos, epos)
if d then
colorize(cols, pos, d, ctx.col)
self.ctx = table.remove(self.stack, 1)[1]
Expand All @@ -141,7 +141,7 @@ function syntax.new(txt, pos, scheme)
return s
end

function syntax:process(pos)
function syntax:process(pos, epos)
if not self.ctx then
return
end
Expand All @@ -153,9 +153,9 @@ function syntax:process(pos)
return
end
while i <= #txt do
local r, d, aux
local d, aux
for _, c in ipairs(self.ctx) do
d, aux = self:match_start(c, txt, i)
d, aux = self:match_start(c, txt, i, epos)
if d then
colorize(cols, i, d, c.col)
i = i + d
Expand All @@ -164,7 +164,7 @@ function syntax:process(pos)
break
end
end
i = i + self:context(i)
i = i + self:context(i, epos)
self.pos = i
break
end
Expand Down
4 changes: 2 additions & 2 deletions data/lib/red/syntax/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ local function endsect(ctx, txt, i)
end
end

local function numbersect(ctx, txt, pos)
local function numbersect(ctx, txt, pos, epos)
local n = ''
for i = pos, #txt do
for i = pos, epos do
if txt[i]:find("[0-9]") then
n = n .. txt[i]
elseif txt[i] == '.' then
Expand Down
39 changes: 28 additions & 11 deletions data/lib/red/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,33 @@ function win:flushline(x0, y0)
end
end

function win:colorize()
local scheme = self:getconf 'syntax'
if not scheme then return end
local text = self.buf.text
local start = math.max(self.pos - self:getconf 'colorize_win', 1)
while text[start] and text[start] ~= '\n' do
start = start - 1
end
local colorizer = syntax.new(text, start, scheme)
if not colorizer then
return
end
local x, y = 0, 0
local epos = #text
for i = self.pos, #text do
x, y = self:next(i, x, y)
if y >= self.rows then
epos = i
break
end
end
for i = start, epos do
colorizer:process(i, self.epos)
end
return colorizer
end

function win:show()
local colorizer
if self.w <= 0 or self.h <= 0 or
Expand All @@ -415,21 +442,11 @@ function win:show()
local x, y = 0, 0
local x0, y0 = x, y
local text = self.buf.text

self.epos = #text + 1
if conf.syntax then
local start = math.max(self.pos - 4096, 1)
colorizer = syntax.new(text, start, self:getconf 'syntax')
if colorizer then
for i = start, self.pos - 1 do
colorizer:process(i)
end
end
colorizer = self:colorize()
end
for i = self.pos, #text + 1 do
if colorizer then
colorizer:process(i)
end
self:glyph(x, y, text[i] or false,
(colorizer and colorizer.cols[i]) or conf.fg,
self.buf:insel(i) and conf.hl or self.bg)
Expand Down

0 comments on commit 9294d6e

Please sign in to comment.