Skip to content

Plugin Lifecycle

Federico Conticello edited this page Mar 25, 2026 · 3 revisions

Plugin Lifecycle

This page explains what happens to a plugin from discovery to shutdown.

Discovery

FerrisPad scans ~/.config/ferrispad/plugins/ at startup. Directories are sorted alphabetically. A directory is recognized as a plugin only if it contains init.lua.

Loading

All plugins share a single Lua runtime. There is one Lua instance for the entire plugin system, not one per plugin.

For each discovered plugin:

  1. init.lua is read and executed in the sandboxed environment
  2. The script must return a table (the module table, conventionally M)
  3. plugin.toml is parsed for metadata, permissions, menu items, and config
  4. The returned table is stored and reused for all future hook calls

The Module Table

The table returned by init.lua is the core of your plugin. It persists across all hook calls for the entire session.

local M = {}

-- This variable persists between hook calls
M.call_count = 0

function M:on_document_save(api, path, content)
    M.call_count = M.call_count + 1
    api:log("Save #" .. M.call_count)
end

return M

Local variables in init.lua also persist — they live in the module's closure:

local cache = {}  -- persists across hooks, private to this plugin

local M = {}

function M:on_document_open(api, path)
    cache[path] = os.clock()
end

return M

Init

init(api) is called once after all plugins are loaded, before any other hook. Use it for setup — checking tool availability, initializing state, logging.

function M:init(api)
    M.has_ruff = api:command_exists("ruff")
    api:log("Plugin ready, ruff: " .. tostring(M.has_ruff))
end

init is not called for disabled plugins.

Hook Calls

After init, hooks are called as the user interacts with the editor. Each hook call:

  1. Resets the instruction counter (fresh 1M instruction budget)
  2. Looks up the hook function on your module table
  3. If the function exists, calls it with the appropriate arguments
  4. If it doesn't exist, skips silently — not an error

Hooks are called on all enabled plugins in alphabetical order. For fields like status_message or split_view, the last plugin to return a value wins. For diagnostics and line_annotations, results accumulate from all plugins.

Disabling a Plugin

When a plugin is toggled off via Plugins > {name}:

  • enabled is set to false
  • Hook calls skip the plugin
  • Widget sessions are cleared
  • The module table stays in memory — state is preserved
  • No shutdown hook is called

When toggled back on, the plugin resumes with its existing state. No re-initialization happens.

Reload All

Plugins > Reload All destroys and recreates all plugin state:

  1. Current disabled list, approved commands, and config are saved
  2. All plugin tables are dropped
  3. Lua garbage collection runs
  4. All init.lua files are re-read and re-executed from disk
  5. Disabled list, commands, and config are restored
  6. init(api) is called on all enabled plugins

Module state does not survive a reload. This is useful for development — edit your init.lua, hit Reload All, and your changes take effect.

The Lua runtime instance itself persists, but all plugin tables are fresh.

Shutdown

shutdown(api) is called once when FerrisPad is quitting, before the application exits. Use it for cleanup if needed.

function M:shutdown(api)
    api:log("Plugin shutting down")
end

No hooks are called after shutdown.

Global State (_G) — Forbidden

All plugins share one Lua runtime and therefore one global namespace _G. Accessing or modifying _G is forbidden.

  • The static source scanner blocks any _G reference — _G.foo = bar, rawset(_G, ...), rawget(_G, ...), etc.
  • Plugins containing _G in non-comment lines will not load — neither via Plugin Manager install nor when loaded from the plugins directory at startup
  • Official plugins will never be accepted with any _G usage
  • Community plugins using _G will be rejected during security review

Use module-local variables or fields on your module table (M.state = {}) to store state. This keeps your plugin isolated and safe across reloads.

Resource Limits

Resource Limit Scope
Instructions 1,000,000 per hook call Resets each hook
Memory 16 MB Shared across all plugins
Command timeout 30 seconds Per run_command call

Exceeding the instruction or memory limit aborts the current hook call with an error. The plugin stays loaded and future hooks will still be called.

Clone this wiki locally