(2022-12-26, 5:10)N. W. Perry Wrote: On the first loop, we would look for the first instance of a BUFEXCHG RETRIEVE statement (being the last one in the file). When we find one, we would return its buffer ID (A, B, C, etc.) since only the lines stored in that buffer will display at the final step of the model.
Progress…I realized that only taking the final buffer ID won't work, because the active "final" buffer can change within a file (as I found in one of my models, where I retrieved A but stored to B…).
Turns out, this actually makes things easier. Now, given the initial visibility state of true, we look for a RETRIEVE statement, and when we find one we simply take its ID, and switch the visibility to false. This causes the script to ignore any further RETRIEVE statements, and now looks for a STORE statement with a matching ID. This turns the visibility true again, and so when we next find a RETRIEVE statement, we take its ID again, in case it is different, and so on…
I've tested this on a few models with complex buffer exchange statements, and the results seem to be correct. The resulting script is below—right now, it only prints the visible lines (or their index) in the console; the next step is to actually toggle them as comments.
If anyone has any models that use a lot of buffer exchange, I'd love to have some test files!
Code:
genTools=require('genTools')
function printVis()
local ses=ldc.session()
if not ses:isLinked() then
ldc.dialog.runMessage('No active session')
return
end
local sf=ldc.subfile()
local cnt=sf:getLineCount()
local lines={}
local isVis=true
for i=cnt, 1, -1 do
local line=sf:getLine(i)
local index=line:getLineIndex()
local strBeg=line:getString():sub(1,11):upper()
local strEnd=line:getString():sub(13):upper()
local strInit=line:getString():sub(12,12):upper()
local isBufEx=strBeg=='0 BUFEXCHG '
local isStor=isBufEx and strEnd==' STORE'
local isRetr=isBufEx and strEnd==' RETRIEVE'
if isRetr then
if isVis then
id=strInit
end
isVis=false
elseif isStor and strInit==id then
isVis=true
elseif line:isRef() and isVis then
table.insert(lines, index)
end
end
for k, v in ipairs(lines) do
print(v)
end
end
function onRun()
printVis()
end
function register()
local macro=ldc.macro('Print visible')
macro:setEvent('run', 'onRun')
end
register()