Search and Replace (dangerous)

@Briarfox Thank for the changes for landscape/portrait. I added that to the code above and removed the original code at the top of this discussion.

Thanks dave1707! I just prefer to cut and paste if a change has been made. I tend to forget that I modify code, paste over it and can’t figure out why something isn’t quite right :stuck_out_tongue:

Here is an updated version of the program. I added several minor changes, but the major change is to hold all of the updates until the end and then ask if you want to discard or update the changes. I also print the project:tabs that were updates. I suggest trying this on a test program until you’re OK with the changes that were made.


function setup()
    parameter.text("project","")
    parameter.text("findStr","")
    parameter.text("replStr","")
    parameter.action("Start Search / Replace",startSearch)    
    setup1()
end

function setup1() 
    showKeyboard()  
    rectMode(CENTER)
    output.clear()
    projTable={}
    dataTable={}
    tableOffset=0
    updateTabs=false
    searchProject=false
    getNextTab=true
    replaced=false
    found=false
    textChanged=false
    next=0
    str2='Enter project, findStr and replStr\
 then press   "Start Search / Replace"'
    str3=""
    hFind=""
    hRepl=""
    func=process
end

function startSearch()
    if project == "" then
        str2="Project name missing"
    elseif findStr =="" or replStr=="" then
        str2="findStr or replStr is empty"
    else
        setup1()
        tab=listProjectTabs(project)
        if #tab>0 then
            searchProject=true
            hideKeyboard()
            hFind=findStr
            hRepl=replStr
        else
            str2="Project not found"
        end
    end
end

function draw()
    background(40,40,50)
    func()
end

function process()
    if hFind~="" or hRepl~="" then
        if hFind~=findStr or hRepl~=replStr then
            setup1()
        end
    end
    font("CourierNewPS-BoldItalicMT")
    noFill()
    strokeWidth(2)
    rect(WIDTH/2,550,460,100)
    fill(255) 
    text(str2,WIDTH/2,550)
    text(str3,WIDTH/2,530)
    if searchProject then
        if getNextTab then
            nextTab()
        end
        if searchProject then
            text("PROJECT:TAB   "..project..":"..tab[next],WIDTH/2,700)
            text("REPLACE".."   "..findStr.."\
WITH    "..replStr,WIDTH/2,650)
            find()
        end
    end
end   

function nextTab()
    next=next+1
    getNextTab=false
    if next > #tab then
        searchProject=false
        str2=""
        str3=""
        if textChanged then
            updateTabs=true
            func=selectChanges
        elseif not found then
            str2=findStr.."    not found "
        else
            str2="You skipped all matches, no updates needed."
        end
    else
        projectTab=string.format("%s:%s",project,tab[next])
        data=readProjectTab(projectTab)
        start=1
    end
end   
    
function find()
    str2=""
    a,b=string.find(data,findStr,start,true)
    if a==nil then
        if replaced then
            tableOffset=tableOffset+1
            projTable[tableOffset]=projectTab
            dataTable[tableOffset]=data
        end
        getNextTab=true
        replaced=false
        return
    end
    found=true
    a1=a-10
    b1=b+10
    a2=10
    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)
    skipReplace()
end

function skipReplace()  
    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 replaceOption()
    replaced=true
    textChanged=true
    data=string.format("%s%s%s",string.sub(data,1,a-1),
    replStr,string.sub(data,b+1,-1))
    start=a+string.len(replStr)
    func=process
end

function skipOption()
    start=a+1
    func=process
end

function selectChanges()
    fill(255)
    rect(WIDTH/2-125,700,200,50)
    rect(WIDTH/2+125,700,200,50)
    fill(0,255,0)
    text("DISCARD CHANGES",WIDTH/2-125,700) 
    fill(255,0,0)
    text("UPDATE CHANGES",WIDTH/2+125,700)
end

function completeOption()
    str2="Update Complete"
    for z=1,#projTable do
        saveProjectTab(projTable[z],dataTable[z])
        print("Updated  "..projTable[z])
    end
    clear()
end

function discardOption()
    str2="Updates discarded"
    clear()
end

function clear()
    findStr=""
    replStr=""
    hFind=""
    hRepl=""
    func=process
end
        
function touched(t)
    if updateTabs then
        if t.state==BEGAN and t.y>675 and t.y<725 then
            if t.x>WIDTH/2-200 and t.x<WIDTH/2-50 then
                func=discardOption
            end
            if t.x>WIDTH/2+50 and t.x<WIDTH/2+200 then
                func=completeOption
            end
        end 
    end
    if searchProject then
        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
                func=skipOption
            end
            if t.x>WIDTH/2+75 and t.x<WIDTH/2+175 then
                func=replaceOption
            end
        end 
    end
end

Just Discovered it today, while using AirCode if you Select something then press Ctrl+alt+left/Right arrow key then it selects all the similar matches. It could be useful to do a mass replace. But ofcourse if you have a vraible called “ion” and then if you try this method it will select the “ion” in “function” too. So best use it for uniquely spelled variables