Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions manipulator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ VERSION = "1.4.1"
local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local util = import("micro/util")

-- Returns Loc-tuple w/ current marked text or whole line (begin, end)
function getTextLoc(c)
Expand All @@ -21,29 +22,6 @@ function getTextLoc(c)
return buffer.Loc(a.X, a.Y), buffer.Loc(b.X, b.Y)
end

-- Returns the current marked text or whole line
function getText(a, b)
local txt, buf = {}, micro.CurPane().Buf

-- Editing a single line?
if a.Y == b.Y then
return buf:Line(a.Y):sub(a.X+1, b.X)
end

-- Add first part of text selection (a.X+1 as Lua is 1-indexed)
table.insert(txt, buf:Line(a.Y):sub(a.X+1))

-- Stuff in the middle
for lineNo = a.Y+1, b.Y-1 do
table.insert(txt, buf:Line(lineNo))
end

-- Insert last part of selection
table.insert(txt, buf:Line(b.Y):sub(1, b.X))

return table.concat(txt, "\n")
end

-- Calls 'manipulator'-function on text matching 'regex'
function manipulate(regex, manipulator, num)
local num = math.inf or num
Expand All @@ -53,7 +31,7 @@ function manipulate(regex, manipulator, num)
for i=1, #cs do
local c = cs[i]
local a, b = getTextLoc(c)
local oldTxt = getText(a,b)
local oldTxt = util.String(v.Buf:Substr(a, b))
local newTxt = string.gsub(oldTxt, regex, manipulator, num)

v.Buf:Replace(a, b, newTxt)
Expand Down Expand Up @@ -209,8 +187,8 @@ function init()
config.MakeCommand("decNum", decNum, config.NoComplete)
config.MakeCommand("incNum", incNum, config.NoComplete)
config.MakeCommand("rot13", rot13, config.NoComplete)
config.MakeCommand("upper", function() manipulate("[%a]", string.upper) end, config.NoComplete)
config.MakeCommand("lower", function() manipulate("[%a]", string.lower) end, config.NoComplete)
config.MakeCommand("upper", function() manipulate(".*", string.upper) end, config.NoComplete)
config.MakeCommand("lower", function() manipulate(".*", string.lower) end, config.NoComplete)
config.MakeCommand("reverse", function() manipulate(".*", string.reverse) end, config.NoComplete)
config.MakeCommand("title", title, config.NoComplete)
config.MakeCommand("pascal", pascal, config.NoComplete)
Expand Down