Skip to content

Commit

Permalink
red: trim spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeping committed Sep 11, 2023
1 parent 6d5f66e commit 7cae564
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
28 changes: 21 additions & 7 deletions data/apps/red.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local conf = {
font_sz = 14,
ts = 4,
spaces_tab = false, -- don't be evil!
trim_spaces = false,
brd = { 0xde, 0xde, 0xde },
menu = 17,
hl = { 0xee, 0xee, 0x9e },
Expand Down Expand Up @@ -184,6 +185,23 @@ local io_delim = {
['|'] = true,
}

function win:save()
if not self.buf:isfile() then
return
end
local trim = self:getconf 'trim_spaces'
if trim then
local cur = self:cur()
self.buf:set(self.buf:gettext():gsub('[ \t]+\n', '\n'):gsub("[ \t\n]+$", "\n"))
self:cur(cur)
end
local r, e = self.buf:save()
if r then
self:nodirty()
end
return r, e
end

function win:proc(t)
local a = t:split(1)

Expand Down Expand Up @@ -450,8 +468,7 @@ function framemenu.cmd:Put()
end
local f = self.frame:getfilename()
if f then
b.buf:save(f)
b:nodirty()
b:save(f)
end
self.frame:update()
end
Expand Down Expand Up @@ -529,11 +546,9 @@ function mainmenu.cmd:PutAll()
for f in self.frame:for_win() do
for w in f:for_win() do
if w.buf:isfile() and w.buf:dirty() then
local r, e = w.buf:save()
local r, e = w:save()
if not r then
f:err(e)
else
w:nodirty()
end
end
end
Expand Down Expand Up @@ -828,8 +843,7 @@ function framemenu.cmd:Run(t)
end

if not t then
w.buf:save()
w:nodirty()
w:save()
end

prepare()
Expand Down
2 changes: 2 additions & 0 deletions data/lib/red/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ function buf:set(text)
self:resetsel()
self.cur = math.min(#self.text + 1, self.cur)
end

function buf:tail()
self.cur = #self.text + 1
end

function buf:append(text, cur)
local u = utf.chars(text)
for i = 1, #u do
Expand Down
5 changes: 3 additions & 2 deletions data/lib/red/presets.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local pre = {
{"%.[cC]$", { ts = 8, spaces_tab = false } },
{"%.lua$", { ts = 2, spaces_tab = true } },
{"%.[cC]$", { ts = 8, spaces_tab = false, trim_spaces = true } },
{"%.lua$", { ts = 2, spaces_tab = true, trim_spaces = true } },
{"%.md$", { ts = 2, spaces_tab = true } },
}
function pre.get(fname)
for _, v in ipairs(pre) do
Expand Down
5 changes: 2 additions & 3 deletions data/lib/red/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ end
function win:cur(pos)
local o = self.buf.cur
if pos then
self.buf.cur = pos
self.buf.cur = math.min(pos, #self.buf.text + 1)
end
return o
end
Expand Down Expand Up @@ -931,8 +931,7 @@ function win:event(r, v, a, b)
self:kill()
elseif v == 's' and input.keydown 'ctrl' then
if self.buf:isfile() then
self.buf:save()
self:nodirty()
self:save()
end
elseif v == 'tab' then
if self:visible() then
Expand Down

0 comments on commit 7cae564

Please sign in to comment.