diff --git a/data/apps/red.lua b/data/apps/red.lua index fcd93b6..79d99d8 100644 --- a/data/apps/red.lua +++ b/data/apps/red.lua @@ -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 }, @@ -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) @@ -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 @@ -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 @@ -828,8 +843,7 @@ function framemenu.cmd:Run(t) end if not t then - w.buf:save() - w:nodirty() + w:save() end prepare() diff --git a/data/lib/red/buf.lua b/data/lib/red/buf.lua index 2be8783..326ef1a 100644 --- a/data/lib/red/buf.lua +++ b/data/lib/red/buf.lua @@ -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 diff --git a/data/lib/red/presets.lua b/data/lib/red/presets.lua index e7a918c..7860ae6 100644 --- a/data/lib/red/presets.lua +++ b/data/lib/red/presets.lua @@ -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 diff --git a/data/lib/red/win.lua b/data/lib/red/win.lua index 0835fc6..22e49e8 100644 --- a/data/lib/red/win.lua +++ b/data/lib/red/win.lua @@ -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 @@ -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