Saving current program as Global Data

I’m not sure if this would be of any use to anyone, but I thought I would share it anyways. It reads the current program, saves it as Global Data, and then reads it back. Since it’s Global Data, it can also be read back with another program. I guess if you saved your programs with this, you would be able to read all of them back with another program and do whatever you want with them. You could then save them back as Global Data.

--# Main

function setup()

Program saved as global data")  
Reading global data back")

function dumpreg(info)
    local x, y
    for x,y in pairs(info) do
        if x == "source" then

I understand that the next version of Codea will implement listGlobalData(). With your idea and that combined, this will enable libraries of code to be held conveniently in Global Data.

The extended example below is a variation on @dave1707’s idea - with the intention of handling code (that is to be preserved in Global Data) that is distributed over more than one tab.

Added as the first tab in the project:

captureCode = debug.getinfo -- Give this function a friendly name
local code = ""
function captureTab(tabName, tabCode)
    if #code > 0 then code = code.."\
" end
    code = code.."--\
-- "..tabName.."\
    code = code...string.match(tabCode.source, ".-%-%-%-%-\
(.*)") -- Remove header

function saveCode(key)
    saveGlobalData(key, code)

The following then can be added to the top of each tab that is to be preserved in Global Data. This ‘header’ is ended by ----:

captureTab("Name for the tab", captureCode(1))

-- Rest of code in tab...

The code is then preserved by calling in setup():

function setup()
    saveCode("ProjectName") -- "p1" in the original example

Thanks @mpilgrem
I didn’t think about programs with tabs. I tried your suggestion and it worked just fine. I wonder how long before Codea gets the use of the file io functions. But like you said, this might work OK with the listGlobalData function.