Skip to content

Commit

Permalink
red: markdown syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeping committed Oct 22, 2023
1 parent a105bee commit 4b8ade9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion data/lib/red/presets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ return {
{"%.[cChH]$", { ts = 8, spaces_tab = false, trim_spaces = true, syntax = "c" } },
{"%.cpp$", { ts = 8, spaces_tab = false, trim_spaces = true, syntax = "c" } },
{"%.lua$", { ts = 2, spaces_tab = true, trim_spaces = true, syntax = "lua" } },
{"%.md$", { ts = 2, spaces_tab = true } },
{"%.go$", { ts = 8, spaces_tab = false, trim_spaces = true } },
{"%.py$", { ts = 4, spaces_tab = true, trim_spaces = true } },
{"%.md$", { ts = 2, spaces_tab = true, trim_spaces = true, syntax = "markdown" } },
}
42 changes: 38 additions & 4 deletions data/lib/red/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ local function isalpha(a, alpha)
end
end

local function isspace(a, spaces)
if a and a:find(spaces or "[ \t]") then
return true
end
end

local function startswith(txt, pos, pfx)
for i = 1, #pfx do
if txt[pos+i-1] ~= pfx[i] then
Expand All @@ -22,16 +28,31 @@ local function startswith(txt, pos, pfx)
return true
end

local function isword(txt, pos, pfx)
local function isword(v, txt, pos, pfx)
if not startswith(txt, pos, pfx) then
return false
end
if isalpha(txt[pos + #pfx]) or isalpha(txt[pos - 1]) then
if isalpha(txt[pos + #pfx], v.alpha) or
isalpha(txt[pos - 1], v.alpha) then
return false
end
return true
end

local function checkword(v, txt, pos, pfx)
if not pfx then return false end
if not v.word then
return startswith(txt, pos, pfx)
elseif v.word == 'left' then
return startswith(txt, pos, pfx) and
not isalpha(txt[pos-1], v.alpha)
elseif v.word == 'right' then
return startswith(txt, pos, pfx) and
not isalpha(txt[pos+#pfx], v.alpha)
end
return isword(v, txt, pos, pfx)
end

function syntax:match_fn(ctx, txt, i, fn, ...)
if not ctx[fn] then return end
if type(ctx[fn]) == 'string' then
Expand All @@ -46,6 +67,20 @@ function syntax:match_fn(ctx, txt, i, fn, ...)
end

function syntax:match_start(ctx, txt, i)
local r = self:match_fn(ctx, txt, i, 'linestart')
if r then
local ok = true
for pos = i-1, 1, -1 do
if txt[pos] == '\n' then
break
end
if not isspace(txt[pos], ctx.spaces) then
ok = false
break
end
end
if ok then return r end
end
return self:match_fn(ctx, txt, i, 'start')
end

Expand All @@ -71,8 +106,7 @@ function syntax:context(pos)
found_len = r
found_col = v.col or ctx.col
end
elseif ((not v.word and startswith(txt, pos, word)) or
(v.word and isword(txt, pos, word, v.alpha))) then
elseif checkword(v, txt, pos, word) then
if not found_len or found_len < #word then
found_len = #word
found_col = v.col or ctx.col
Expand Down
2 changes: 1 addition & 1 deletion data/lib/red/syntax/scheme.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
return {
keyword = { 122, 112, 72 },
keyword = { 102, 102, 22 },
comment = { 64, 136, 64 },
string = { 124, 102, 187 },
number = { 2, 135, 200 },
Expand Down
8 changes: 7 additions & 1 deletion data/lib/red/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,13 @@ function win:show()

self.epos = #text + 1
if conf.syntax then
colorizer = syntax.new(text, self.pos, self:getconf 'syntax')
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
end
for i = self.pos, #text + 1 do
if colorizer then
Expand Down
2 changes: 1 addition & 1 deletion doc/api-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ lfo_assign 0 width
@synth параметры - частный случай set, меняет
параметр у всех коробок synth на канале.

В качестве канала можно указать -1 или *, что будет
В качестве канала можно указать -1 или \*, что будет
означать все дорожки композиции.

## sfx
Expand Down

0 comments on commit 4b8ade9

Please sign in to comment.