Here’s my original program with parameters added. I’ll let someone else add the backups.
EDIT: Can someone come up with a better way of showing the string being replaced.
EDIT: Added a line of code in the function replace() to replace any newlines character with a space so the line being shown won’t be on multiple lines. That might be easier to determine if the string needs to be replaced or not.
EDIT: I replaced the previous listing here with this one. I changed the code so that I show asterisks below the string that will be replaced. I think that’s easier to see what would be replaced.
EDIT: I made corrections for the asterisks if the findStr was close to the beginning or end of the program.
function setup()
parameter.text("project","")
parameter.text("findStr","")
parameter.text("replStr","")
parameter.action("Start Search / Replace",sr)
setup1()
end
function setup1()
projectDone=true
getNextTab=true
replaced=false
found=false
next=0
rep=0
str2=""
str3=""
rectMode(CENTER)
end
function sr()
if project == "" then
str2="Project name missing"
elseif findStr =="" or replStr=="" then
str2="Find string or Replace string\
is empty"
else
setup1()
tab=listProjectTabs(project)
if #tab>0 then
projectDone=false
else
str2="Project not found"
end
end
end
function draw()
background(40,40,50)
fill(255)
if not projectDone then
if getNextTab then
nextTab()
end
if not projectDone then
find(st)
text("PROJECT:TAB "..project..":"..tab[next],WIDTH/2,700)
text("REPLACE".." "..findStr.."\
WITH "..replStr,WIDTH/2,650)
end
end
font("Courier-BoldOblique")
noFill()
strokeWidth(2)
rect(WIDTH/2,550,460,100)
fill(255)
text(str2,WIDTH/2,550)
text(str3,WIDTH/2,530)
rect(WIDTH/2-125,400,100,50)
rect(WIDTH/2 +125,400,100,50)
fill(0,255,0)
text("SKIP",WIDTH/2 - 125,400)
fill(255,0,0)
text("REPLACE",WIDTH/2+125,400)
end
function nextTab()
next=next+1
getNextTab=false
if next > #tab then
projectDone=true
str2="Find / Replace Complete"
str3=""
if not found then
str2=findStr.." not found "
end
else
projectTab=string.format("%s:%s",project,tab[next])
data=readProjectTab(projectTab)
st=1
rep=0
done=false
end
end
function find(s)
str2=""
a,b=string.find(data,findStr,s,true)
if a==nil then
done=true
if replaced then
saveProjectTab(projectTab,data)
end
getNextTab=true
replaced=false
return
end
replace(a,b)
found=true
if rep==2 then
replaced=true
data=string.format("%s%s%s",string.sub(data,1,a-1),
replStr,string.sub(data,b+1,-1))
st=a+string.len(replStr)
rep=0
elseif rep==1 then
st=a+1
rep=0
end
end
function replace(a,b)
local a1=a-10
local b1=b+10
local a2=10
local b2=10
if a1<1 then
a1=1
a2=a-1
end
if b1>string.len(data) then
b1=string.len(data)
b2=string.len(data)-b
end
str2=string.sub(data,a1,b1)
str2=string.gsub(str2,"\
"," ")
str3=string.rep(" ",a2)
str3=str3..string.rep("*",string.len(findStr))
str3=str3..string.rep(" ",b2)
end
function touched(t)
if t.state==BEGAN and t.y>375 and t.y<425 then
if t.x>WIDTH/2-175 and t.x<WIDTH/2-75 then
rep=1
end
if t.x>WIDTH/2+75 and t.x<WIDTH/2+175 then
rep=2
end
end
end