So I tried this to fold on page loads. Works well, except editor.getCursor() returns nil. So after the page loads, the cursor is not set by SB to its correct position, but remains at the last line that was folded. I don’t understand when and how SB sets the cursor position (to what it was when this page was last visited). Help appreciated.
event.listen{
name = "editor:pageLoaded",
run = function()
local text = editor.getText()
if not text then
return
end
-- Save cursor position
local originalCursor = editor.getCursor()
local lineNum = 0
for line in (text .. "\n"):gmatch("(.-)\n") do
lineNum = lineNum + 1
-- Simple substring match
if line:find("[folded: true]", 1, true) then
-- Move cursor to the line so the folding can be done
editor.moveCursorToLine(lineNum, 1, false)
-- Fold the block at cursor
editor.fold()
end
end
-- Restore cursor
if originalCursor then
editor.setCursor(originalCursor)
end
end
}