aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-15 23:14:34 +0200
committeruser <user@clank>2026-08-15 23:14:34 +0200
commita25a83d9493d552dbfbe62b12eb9f6070d9e6230 (patch)
treeb8ea4bc48070850dcc093f365f3984ba082cdbb2 /src
parentsl0ppty: shaders on the chrome, and two keys for the arrangement (diff)
downloadslosh-a25a83d9493d552dbfbe62b12eb9f6070d9e6230.tar.gz
slosh-a25a83d9493d552dbfbe62b12eb9f6070d9e6230.tar.xz
slosh-a25a83d9493d552dbfbe62b12eb9f6070d9e6230.zip
sl0ppty: a config can be made of files
include "themes/amber.kdl" include "keys/vim.kdl" "shaders/crt.kdl" which is how you switch a theme by editing one line rather than pasting one in. Nothing about it is theme-specific: an include is another config file applied here, so the same line composes keys, shader chains, states or geometry, and contrib/themes stops being a directory you copy out of. A relative path is relative to *the file doing the including*, never to the directory the session was started from -- a config is a thing on disk that refers to its neighbours, and where you were standing is not part of what it means. A leading `~` is a home directory, an absolute path is itself, and an included file may include others. What you include is the base: the file with the `include` line in it wins wherever they disagree, and a later include wins over an earlier one. That holds wherever the line sits, because the loader reads a document by asking it for the keys it knows rather than walking it top to bottom -- so "here" is not a position it could honour, and "the file you are editing wins" is the rule worth having anyway. It is what makes a theme something you can put two lines on top of. A depth limit rather than a set of files already seen, because the two failures it has to stop are the same failure: a config that includes itself and a config eleven files deep are both a mistake, and the message says which one you have. A file that will not load is skipped and the rest of the config still applies (D9) -- losing your keybindings over a mistyped theme name would be the worse answer. The loader was already shaped for this: `config_load` applied one parsed root onto a config_t, so it becomes a recursive `load_into` that applies its includes first and then itself. Errors from an include get their own buffer, so a broken theme cannot overwrite a message the file that included it has already produced. tests/test_include.py covers the two rules that carry the weight -- the outer file winning either way round, and a nested include resolving against the file that wrote it -- plus multiple files on one line, `~`, a binding from an included file, a missing file, a cycle, switching theme through a reload, and every file in contrib/themes applying as an include, checked against each file's own frame_focus so it cannot pass by looking default. Not yet: an included file is not watched, so editing the theme itself does not reload the session. Editing the include line does.
Diffstat (limited to 'src')
-rw-r--r--src/config.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
index 3a27152..257047c 100644
--- a/src/config.c
+++ b/src/config.c
@@ -978,11 +978,86 @@ const char *config_default_path(void) {
return path;
}
+/* How deep a chain of includes may go. A depth limit rather than a set of files
+ * already seen, because the two failures it has to stop are the same failure:
+ * a config that includes itself and a config eleven files deep are both a
+ * mistake, and the message says which one you have. */
+#define INCLUDE_MAX_DEPTH 8
+
+static bool load_into(config_t *c, const char *path, int depth, char *err,
+ size_t errcap);
+
+/* Where `include "themes/nord.kdl"` points, given the file doing the
+ * including. Relative to *that file*, not to the working directory: a config is
+ * a thing on disk that refers to its neighbours, and where you happened to be
+ * standing when you started the session is not part of what it means. */
+static void include_path(const char *base_file, const char *ref, char *out,
+ size_t cap) {
+ char buf[512];
+ const char *r = path_expand(ref, buf, sizeof buf);
+ if (r[0] == '/') {
+ snprintf(out, cap, "%s", r);
+ return;
+ }
+ char dir[512];
+ snprintf(dir, sizeof dir, "%s", base_file);
+ char *slash = strrchr(dir, '/');
+ if (slash) *slash = 0;
+ else snprintf(dir, sizeof dir, ".");
+ snprintf(out, cap, "%s/%s", dir, r);
+}
+
+/* Every `include` at the top level of this file, in order, applied *before* the
+ * file's own settings — wherever the line happens to sit. The loader reads a
+ * document by asking it for the keys it knows rather than walking it in order,
+ * so "here" is not a position it could honour; and the useful rule is the
+ * simple one anyway: what you include is the base, what you write beside the
+ * include wins. Later includes win over earlier ones, which is the one bit of
+ * order that survives.
+ *
+ * A file that will not load is a line and no more (D9): the rest of the config
+ * still applies, because losing your keybindings over a mistyped theme name
+ * would be a worse answer than a session that says so. */
+static void apply_includes(config_t *c, const kdl_node_t *root, const char *path,
+ int depth, char *err, size_t errcap) {
+ for (size_t i = 0; i < root->nkids; i++) {
+ const kdl_node_t *n = root->kids[i];
+ if (!n || !n->name || strcmp(n->name, "include") != 0) continue;
+ if (!n->nargs && err && !err[0]) {
+ snprintf(err, errcap, "line %d: include needs a file", n->line);
+ continue;
+ }
+ for (size_t j = 0; j < n->nargs; j++) {
+ char resolved[512];
+ include_path(path, n->args[j], resolved, sizeof resolved);
+ /* Its own buffer: an include's failure must not overwrite a message the
+ * file that included it has already produced. */
+ char ierr[256] = {0};
+ if (!load_into(c, resolved, depth + 1, ierr, sizeof ierr) && err &&
+ !err[0])
+ snprintf(err, errcap, "line %d: %s", n->line,
+ ierr[0] ? ierr : "cannot include it");
+ }
+ }
+}
+
bool config_load(config_t *c, const char *path, char *err, size_t errcap) {
if (err && errcap) err[0] = 0;
+ return load_into(c, path, 0, err, errcap);
+}
+
+static bool load_into(config_t *c, const char *path, int depth, char *err,
+ size_t errcap) {
+ if (depth > INCLUDE_MAX_DEPTH) {
+ if (err && errcap)
+ snprintf(err, errcap, "%s: includes nested too deep (a cycle?)", path);
+ return false;
+ }
kdl_node_t *root = kdl_parse_file(path, err, errcap);
if (!root) return false; /* defaults stand; the caller reports why */
+ apply_includes(c, root, path, depth, err, errcap);
+
c->gap = (uint16_t)kdl_arg_int(kdl_child(root, "gap"), 0, c->gap);
c->gap_aspect =
(uint16_t)kdl_arg_int(kdl_child(root, "gap_aspect"), 0, c->gap_aspect);