Skip to main content

State​

dropterm.state is a plugin's own key-value store, kept by the host: a script remembers something across dispatches, across reloads, and across restarts without writing a file itself.

local dropterm = require('dropterm')
local runs = dropterm.state.get('runs') or 0
dropterm.state.set('runs', runs + 1)
dropterm.state.set('last-run', { at = os.time(), ok = true })

state is the store's name on the port, and it has two calls:

CallDoes
getstate.get(key) returns the plugin's stored value for key, or nil when it never set one
setstate.set(key, value) stores value under key, replacing what stood

state.set(key, nil) deletes the key, and so does a set with no value; deleting a key that was never set costs nothing. A key is a string - anything else costs one dropterm: line and reads or writes nothing.

State is each plugin's own. A call inside a handler belongs to the plugin that registered the handler, even when another plugin's handler called it - the rule output channels follow. Two plugins may both keep runs, and each counts its own.

A table get hands back is the plugin's own copy: changing it changes nothing in the store. Change it, then set it back.

What a value may be​

A stored value is JSON, so the shapes are closed. These store:

Lua valueStored asReads back as
true, falsea JSON booleantrue, false
an integer (7, os.time())a JSON integeran integer
a finite number (2.5, 3.0)a JSON numbera number - 3.0 reads back as 3.0, not 3
a UTF-8 stringa JSON stringthe same string
{}{}an empty table
{1, 2, 'x'}[1, 2, "x"]an array
{ a = 1, b = 'x' }{"a": 1, "b": "x"}a table with string keys
tables nested in either shapenested JSONthe same shape

and these cost one dropterm: line each and store nothing:

Lua valueThe line says
a function, a coroutinea value of type function (or thread) cannot be stored
a table mixing an array and keyed parts, as {1, x = 2}a table must be an array (keys exactly 1..n) or an object (string keys only)
integer keys off 1..n, as {[0] = 'z'}the same
a key that is not a string, as {[true] = 1}the same
0/0, 1/0, -1/0a number must be finite
a string that is not UTF-8 texta string must be UTF-8 text
a table that contains itselfa table that contains itself cannot be stored
a value nested past 125 tablesa value may nest at most 125 tables deep

Every refusal names the plugin and the key, and the handler runs on past it.

The caps​

CapLimit
keys per plugin256
one key128 bytes
one value64 KiB of JSON

Past a cap the set costs one dropterm: line and writes nothing. At the key-count cap an overwrite passes; only a new key refuses.

The file, and when a write lands​

State lives in plugins-state.json under the config dir, beside plugins/ and plugins-settings.json:

{
"uptime-helpers": {
"last-run": {
"at": 1735000000,
"ok": true
},
"runs": 4
}
}

The host loads it once at startup and never watches it, so a hand edit loads at the next launch. Only the host writes it, atomically (a sibling temp file, then a rename).

A set lands in the host's memory at once and on disk when the dispatch that made it ends - one write per dispatch, however many set calls it made. The write comes before the host waits for its next command, before it rebuilds the interpreter after a runaway script, and when it shuts down, so a value survives a hot reload, a hard-cap teardown, and a restart.

Two limits follow. A process killed mid-dispatch loses that dispatch's writes, and only those. A script blocked inside a C call never ends its dispatch, so its writes never land - the full-trust limit the timeout section states.

Removing a plugin's directory leaves its block in the file: nothing deletes stored data because a directory went missing. Delete the block by hand to reclaim it. A plugin that deletes its own last key takes its block with it.

Settings and state​

SettingsState
Who declares itthe manifest's settings rowsnothing - any key, any time
Who writes ityou, on the Plugins pagethe plugin, through state.set
Who reads itthe plugin, on every dispatch's ctxthe plugin, through state.get
Its shapetyped rows with defaultsJSON under string keys
Its fileplugins-settings.jsonplugins-state.json

Settings are config the user owns; state is memory the plugin owns.

Nothing here protects a secret​

The file is plain JSON, readable by anything that runs as you. Do not keep a token, a password, or a key in state - a plugin can already read and write your files, and this is one of them.

Clipboard​

A plugin can read the clipboard's text and put text on it: a command that reformats what you copied, or a selection you want to keep. dropterm.clipboard has two plain calls, and each answers at once.

local dropterm = require('dropterm')

dropterm.command('upper-case', 'Upper-case what the clipboard holds', function()
local text = dropterm.clipboard.read() -- the text, or nil when the clipboard holds none
if text then
dropterm.clipboard.write(text:upper()) -- true, or false
end
end)

-- the primary selection, either way
local selection = dropterm.clipboard.read({ primary = true })
dropterm.clipboard.write('chosen', { primary = true })

The command's handler reads the clipboard and puts its upper-cased text back. The last two lines run when the script loads, so in a real plugin they belong inside a handler.

clipboard is the table's name on the port, and it has two calls:

CallDoes
readclipboard.read(opts) returns the clipboard's text as a string, or nil when it holds none
writeclipboard.write(text, opts) puts text on the clipboard and returns true, or false when it did not land

The options are one key:

OptionHolds
primarytrue or false; false when absent. true addresses the primary selection instead of the clipboard

Text only, as UTF-8: an image is neither read nor written. An empty clipboard, or one holding only an image, reads nil and costs no line, because an empty clipboard is a state, not a failure. An empty string is text - write('') returns true, and an empty offer reads back as '', where nil means no text was offered.

One cap bounds both calls: 1 MiB, 1,048,576 bytes. A write over it is refused before anything reaches the compositor and answers false. A read stops one byte past it, so a larger offer never waits for the rest, and answers nil. At exactly the cap both pass.

A wrong argument, a refused call, and a failed one each cost one dropterm: line and answer nil or false. The handler runs on past the call, and nothing raises into the terminal. No line carries clipboard text: a line names a size or a reason.

The callThe line says
write(), write(nil)clipboard write rejected: the text is of type nil, not string
write(7)clipboard write rejected: the text is of type integer, not string
write('\xff')clipboard write rejected: the text is not UTF-8 text
write('x', 'regular')clipboard write rejected: the options are a string, not a table
write('x', { [1] = 'y' })clipboard write rejected: an option key is of type integer, not string
write('x', { regular = true })clipboard write rejected: unknown option 'regular' (primary)
write('x', { primary = 'yes' })clipboard write rejected: primary is of type string, not boolean
a write of 1,048,577 bytesclipboard write refused: 1048577 bytes is over the 1048576-byte clipboard cap
write('x', { primary = true }) where the compositor has no primary selectionclipboard write failed: The compositor does not support primary selection
read('regular')clipboard read rejected: the options are a string, not a table
read({ primary = 1 })clipboard read rejected: primary is of type integer, not boolean
read() where the compositor has no data-controlclipboard read failed: A required Wayland protocol (ext-data-control, or wlr-data-control version 1) is not supported by the compositor
a read past the capclipboard read refused: the offer is over the 1048576-byte clipboard cap
a read whose bytes are not UTF-8clipboard read failed: the offer is not UTF-8 text

Three words sort the lines: rejected for an argument, refused for the cap, failed for the compositor.

A write is an ordinary copy. A clipboard manager and other apps see the text, as with any copy.

Both calls run on the host thread, and every other plugin waits behind them. A read waits on the app that holds the clipboard: one that never answers parks every plugin until it does, and the timeout machinery cannot cut the call short. Keep the calls in a command handler or an event handler. A band view's ask shares one 100 ms round trip with every other plugin's, and one slow round drops them all.

Where the compositor offers no data-control protocol, both calls answer empty: nil and false, at one line each, and nothing else changes. The terminal's own paste still works there.

Clipboard change​

A plugin can ask to be told each time the clipboard's text changes - a history, a reformatter that reacts to every copy, a note of what you copied. It declares the clipboard-watch permission and registers for the event:

local dropterm = require('dropterm')

dropterm.on('clipboard-change', function(p)
print('copied', #p.text) -- { text }: what the clipboard holds now
end)

The payload has one key. p.text is what clipboard.read() would return: the clipboard's new text as a string, trailing newline included, at most 1 MiB. The settings ctx rides the second argument as on every event. The handler has no ctx tab, so a send_text inside it drops with its standing line: an event nobody typed cannot type into a pane.

Text copies only. An image, a change to the primary selection, a cleared clipboard, and an empty copy fire nothing and cost no line. Which types count as text is the clipboard's own choice, the same one clipboard.read() makes.

One event per change. A copy DropTerm makes itself counts, and so does a plugin's clipboard.write. Nothing is deduplicated, and each change is its own dispatch, in order.

Only a plugin that declares the permission hears it. A plugin without clipboard-watch gets no event and no line. A lazy plugin that declares it hears nothing until its script has loaded, which is why a watcher declares on_startup. The text that stood before the watch began is never an event: a plugin that wants it calls clipboard.read().

The cap binds. An offer past 1 MiB, or one whose bytes are not UTF-8, fires nothing and costs one line. The handler runs on the host thread as every handler does, under the timeout machinery.

Where the compositor offers no data-control protocol, one line and no events. Nothing else changes, and the terminal's own paste still works there.

One thread of DropTerm's own watches the clipboard, and it stands only while a plugin that declares the permission is in the scan. Every line it and the event can cost is one of these, in the dropterm: plugins: namespace, and none carries clipboard text:

The momentThe line says
the watch startsclipboard watch armed
the last permitted plugin goesclipboard watch stopped
the watch could not startclipboard watch unavailable: <the compositor's words>
the transport fails, or the watch's thread panicsclipboard watch failed: (a panic: its thread panicked)
the watch's thread cannot startclipboard watch failed: its thread could not start:
an offer past the capclipboard change refused: the offer is over the 1048576-byte clipboard cap
bytes that are not UTF-8, or a transfer that failsclipboard change failed: (the offer is not UTF-8 text)
a handler raisesa handler for 'clipboard-change' raised:
send_text in the handlersend_text dropped: no ctx tab on this dispatch

Three cautions:

  • A handler that writes back what it read loops. Each write fires the event again.
  • A plugin with the permission hears every text copy made in any application while it stands, a password manager's copy included. That is what the permission declares and what the card says.
  • A clipboard manager that takes ownership of each copy can make two events of one copy. An applet that cares compares the text.