libtmux.query filters ordinary Lua records without contacting tmux, starting
a loop or loading a codec. Pass a schema for any sequence, an empty one
included. The native query example shows
structured criteria and a Lua predicate over the same records.
query.select(rows, schema)copies the sequence and keeps the record objects. The result is a dense one-based table: indexing,#andipairswork as usual.:where(criteria)and:filter(predicate)return new selections. They keep input order, duplicate records and shared record references.- Input and results stay mutable. They are not live views.
| Operation | Result |
|---|---|
:first() |
First record, or nil |
:one() |
Record, or nil, no_match/multiple_matches error |
:one_or_nil() |
Zero or one record; multiple matches return an error |
:exists() / :count() |
Whether any records exist / sequence length |
:iter() |
Fresh local iterator yielding records |
:to_table() |
Shallow sequence copy without selection methods |
- Free functions take the sequence first and its schema last. A selection carries its schema.
query.compile(schema, criteria)returns a compiled handle, ornil, err.query.where(rows, compiled)uses that handle's schema for ordinary rows. Compilation copies the schema and criteria, so later edits to the originals change nothing.- Convenience validation raises structured error tables;
compileand cardinality errors return them. A predicate runs as ordinary Lua and its errors propagate unchanged.
- Schema fields declare
string,numberorboolean, with optionalnullableandsupportedbooleans. - Relations declare
cardinality = "one"or"many"and a childschema. A to-many array must describe the whole relationship. - A scalar criterion is equality shorthand;
falseis a value. Operators areeq,ne,one_of,none_of,lt,lte,gt,gte,contains,starts_with,ends_withandis_null. Strings compare literal bytes. ANDandORhold dense arrays of criteria,NOTholds one. Several fields or operators mean AND.- To-many relations use
some,everyornone. To-one relations useisoris_notwith criteria orquery.NULL.
query.NULL is a loaded absent nullable value or to-one relation. A missing
key is unloaded. Unloaded and unsupported data is an error, even inside a
branch that would otherwise short-circuit. The whole grammar is validated
first, then every projection it needs, then matching begins. Invalid criteria
fail even for empty input.
| Case | Result |
|---|---|
Empty criteria, empty AND, empty none_of |
Match |
Empty OR, empty one_of |
No match |
some on an empty relation |
False |
every, none on an empty relation |
True |
is = criteria on an absent to-one relation |
False |
is_not = criteria on an absent to-one relation |
True |
is = NULL on an absent to-one relation |
True |
is_not = NULL on an absent to-one relation |
False |
Criteria reject metatables, functions, cycles and non-finite numbers. The limits are depth 32, 4,096 copied nodes, 1,024 members per membership operator, 65,536 aggregate string and key bytes and a 524,288-byte encoding estimate. They bound criteria validation. Row traversal and your predicates are synchronous CPU work.
query.encode_json(schema, criteria, codec)returns a JSON string, ornil, err.query.decode_json(schema, text, codec)returns new plain criteria tables, ornil, err.- Both validate the whole grammar against your schema. The wire carries no schema and no predicates.
- Pass the codec yourself: lunajson's
encode(value, null)andnewparser(text, callbacks)SAX API. A plain JSONdecodecannot see duplicate keys. Core imports and ordinary queries need only Lua, and the library never loads a codec for you. Codec functions are trusted code.
local query = require("libtmux.query")
local json = require("lunajson")
local schema = { fields = { active = { type = "boolean" } } }
local text = assert(query.encode_json(schema, { active = false, AND = {} }, json))
local criteria = assert(query.decode_json(schema, text, json))The envelope has exactly two members:
{"version":"libtmux.where/v1","where":{"active":false,"AND":[]}}- The profile is specific to this Lua API; cross-port conformance has not been established.
- Operators keep their Lua spelling, uppercase
AND,ORandNOTincluded. Unknown members and versions are rejected. - The schema decides array or object encoding: empty
AND,OR,one_ofandnone_ofare[], and empty criteria are{}. Decoding rejects a container of the wrong kind, an empty object in an array position included. query.NULLis JSON null and decodes back to the sentinel.falsestays false. Encoding tags arrays only in private copies.- The decoder rejects duplicate keys, trailing non-whitespace, malformed Unicode, invalid UTF-8 and non-finite numbers. Numbers are at most 9,007,199,254,740,991 in magnitude, and a nonzero token that underflows to zero is rejected. A string of arbitrary non-UTF-8 bytes works in local criteria but needs a separate binary form at a consumer boundary.
- Input is capped at 524,288 bytes before the parser is built. Parsing checks depth 32, 4,096 nodes and 65,536 string and key bytes as events arrive, and membership is capped at 1,024. Encoding applies the same budgets and an output cap. Envelope members and keys count, so criteria at a local limit can exceed a wire limit.
- Errors keep
code,operation,messageandpath:invalid_jsonfor syntax and scalar encoding,invalid_wirefor envelope and container shape,unsupported_wire_versionfor another version, andinvalid_codecorcodec_errorfor a missing or failing codec. Grammar errors andquery_limitstay structured.
server:query_panes(options) returns a Request<LiveQueryResult<Pane>>.
server:query(options) takes kind: session, window, pane, window_link, client
or buffer. Both return:
rows: a canonical Selection, with local collection methods;- the captured
snapshotand the executedplan; - the acquisition interval, the
completeflag and any detectedraces.
Pass structured where criteria and a pushdown mode:
| Mode | Behavior |
|---|---|
never |
Capture the graph and evaluate all criteria locally. |
auto |
Also apply supported necessary pane predicates at the source. |
require |
Reject an incomplete native translation before any listing. |
- Native translation covers bounded equality tests on selected pane IDs, booleans and integer fields. Other criteria stay local.
- AND can supply necessary source predicates. Partial OR, NOT and relationship predicates cannot.
- Every kind supports local evaluation.
requireon another kind reportsunsupported_pushdown.
server:explain_panes(options) and server:explain(options) return Requests
with the ordered command phases, projections, relation hydration paths, pushed
predicates and residual reasons. Explaining does no tmux I/O. Inputs and
projections are validated before any live dispatch and copied, so later edits
cannot change the query.
- The
snapshotoption takes snapshot options. Fields a criterion needs are added to explicit projections. - The query captures the whole relationship graph before any native candidate listing. It keeps canonical order and linked-window context, and filtering candidate IDs never removes children from quantified relationships. This fixes the semantics; it is not a speedup.
- The graph and the candidate listing cover different moments.
candidate_missingreports a candidate absent from the graph, andcandidate_changedone that disagrees with a pushed predicate.complete = truemeans no known inconsistency, not an atomic view. snapshot.strictadds its one topology check, which cannot freeze state across the later candidate listing. The acquisition interval covers both phases. Snapshot and candidate listing each apply the row and byte limits, and retained data shares the runtime byte budget.- The expert
native_filtertakes a pane format of up to 16 KiB. It excludeswhereandpushdown, uses tmux's format language as written, has no local equivalent and no portability guarantee. Use structured criteria for untrusted data.