An output policy for the three commands, and @cond as a true special form

A snapshot of the development tree.  The substantial changes since the last one:

COMMAND OUTPUT POLICY.  The three commands display text in exactly three cases,
and each owns a stream: LOGGING under "-v" greater than 0 and an ERROR before
termination go to STDERR; OUTPUT THE USER ASKED FOR goes to STDOUT.  For ktext
that output is a document, so "ktext doc.kt -d | ..." is now safe -- logging
used to share the stream and land inside the document.  A bare command prints
its usage and succeeds rather than failing.  Colour is emitted only to a
terminal, per stream, and NO_COLOR is honoured.

"-v 1" reports every decision whose outcome you could not have read off your own
input: the klammerset that was loaded and from which file, a font's directory, a
":files" name's file, how "-o" was expanded.  Higher levels are the trace.

The commands no longer warn and continue: an anomaly is an error, described with
its location.  Two exceptions remain, each for a stated reason -- a condition
that is expected and temporary by design, and a judgment that is a heuristic
rather than exact.

@cond IS NOW A TRUE SPECIAL FORM, resolved at APPLICATION time rather than when
the file is read.  Two consequences for a writer:

  * a state variable reaches the predicate.  "@@@state Flag :value true @@@
    @cond *Flag* | T | F @" renders "T"; it used to see the literal "*Flag*" and
    silently take the false branch.  The document now behaves like a klammer
    body, whose arguments are bound before its conditionals are decided.
  * nothing in a discarded branch happens -- it is not read, not evaluated, not
    expanded.  An @eval in the branch not taken used to run anyway.

Its predicate relation is total and strict: true, True, 1; false, False, 0, and
empty; anything else is an error at the @cond rather than silently false.

@eval REACHING OUTSIDE.  ":shell" and ":haskell" now keep the command's standard
error out of the document (it appears under "-v 1") and treat a nonzero exit as
an error naming what the command reported.  A command that exits nonzero on
purpose -- "grep" finding no match -- says so with "|| true".

KLAMMER SETS.  Several combine: "--klammersets a b c" loads all three in the
order given, sharing one namespace, with the definition modes deciding
collisions.  "none" means none and may not be combined with other symbols.  A
klammerset with symbol X is declared in a file X/X.k, which is what lets two
sets require the same third set without loading it twice.

TESTS.  Four new suites: the kdiag command's interface, the @eval primitive's
contract with the outside world, and verbosity at both tiers.  Three suites
that could not run on macOS at all now do.

Assembled from dev commit 6c8ee6c22fca.
This commit is contained in:
2026-08-16 01:37:59 +02:00
parent 59c1599bc9
commit 240cff4278
84 changed files with 2523 additions and 494 deletions

View File

@@ -16,18 +16,18 @@ static void font_usage()
{
std::cout <<
"Font maintenance commands:\n"
" --font List the available fonts\n"
" --font list The same\n"
" --font samples <output-dir> Write a sample page of the available fonts\n"
" to <output-dir>/index.html\n"
" --font List the available fonts\n"
" --font list The same\n"
" --font samples <output-dir> Write a sample page of the available fonts\n"
" to <output-dir>/index.html\n"
" --font samples <input-dir> <output-dir>\n"
" Write a sample page for the (not yet\n"
" installed) font files under <input-dir>\n"
" --font install <input-dir> Install the font files found under\n"
" <input-dir> into the user font directory\n"
" Write a sample page for the (not yet\n"
" installed) font files under <input-dir>\n"
" --font install <input-dir> Install the font files found under\n"
" <input-dir> into the user font directory\n"
" --font install <input-dir> <output-dir>\n"
" Install into <output-dir> instead\n"
" --font help This description\n"
" Install into <output-dir> instead\n"
" --font help This description\n"
"\n"
"Fonts are searched in the directories of the KLAMMERTEXT_FONTS\n"
"environment variable (colon-separated; default $HOME/.klammertext/fonts)\n"
@@ -38,11 +38,11 @@ static void coverage_usage()
{
std::cout <<
"Coverage commands:\n"
" --coverage Report the targets each klammer covers\n"
" --coverage all Also name the file each klammer is written\n"
" in, and show every \"needs attention\"\n"
" category, including the empty ones\n"
" --coverage help This description\n"
" --coverage Report the targets each klammer covers\n"
" --coverage all Also name the file each klammer is written\n"
" in, and show every \"needs attention\"\n"
" category, including the empty ones\n"
" --coverage help This description\n"
"\n"
"Coverage is a fact about a klammer: the targets it can render to. It is\n"
"DERIVED where the definitions determine it (a general body of klammer\n"
@@ -60,10 +60,9 @@ static void katom_usage()
{
std::cout <<
"Katom commands:\n"
" --katoms List the katom types\n"
" --katoms full The same, with the regex each type is\n"
" matched by\n"
" --katoms help This description\n";
" --katoms List the katom types\n"
" --katoms full The list, with the regex syntax used for a march\n"
" --katoms help This description\n";
}
// The words of a variadic option, with the empty ones dropped.
@@ -82,33 +81,47 @@ static void klammerset_usage()
{
std::cout <<
"Klammerset commands:\n"
" --klammerset List the klammersets on the search path\n"
" --klammerset list The same\n"
" --klammerset help This description\n"
" --klammersets List the klammersets on the search path\n"
" --klammersets <symbols> Load the klammersets specified by <symbols>\n"
" --klammersets help This description\n"
"\n"
"Without an argument, all available klammersets are listed. With one or more\n"
"klammerset symbols, those klammersets are loaded for analysis by the -k, -t,\n"
"--argtypes, --optionsets, and --coverage arguments.\n\n"
"SEVERAL SETS COMBINE. The symbols are a list, loaded in the order given, and\n"
"the klammers of all of them share ONE flat namespace -- membership in a set is\n"
"provenance, not containment. Where two sets define the same klammer, the\n"
"definition modes decide: \":\" is an error if the name is already defined,\n"
"\":::\" replaces the earlier definition (with a warning), and \"::::\" yields to\n"
"any later one. So a set of house overrides is loaded after the set it\n"
"adjusts, and a set of defaults before it.\n\n"
"The Standard Klammer Set is loaded by default. If the <symbols> argument is\n"
"\"none\", no klammerset is loaded.\n\n"
"A klammerset symbol x names the declaration file x/x.k, searched for in:\n"
"the current directory (for ktext, the input document's directory), the\n"
"KLAMMERTEXT_KLAMMERSETS directories (colon-separated; default\n"
"$HOME/.klammertext/klammersets), and $KLAMMERTEXT_HOME. The first hit\n"
"1) the current directory (for ktext, the input document's directory); 2) the\n"
"KLAMMERTEXT_KLAMMERSETS directories (colon-separated; the default is\n"
"$HOME/.klammertext/klammersets); and 3) $KLAMMERTEXT_HOME. The first hit\n"
"wins, so a document-local klammerset shadows an installed one, which\n"
"shadows a distributed one.\n";
}
static void klammerset_command(const strings_t& words)
static void klammerset_command(Machine& machine, const strings_t& words)
{
std::string verb = words.empty() ? "list" : words[0];
if (verb == "list") {
if (words.empty()) {
std::cout << boldblack << "Klammersets\n" << black
<< describe_klammerset_search(fs::current_path().string());
} else if (verb == "help") {
} else if (words[0] == "help") {
klammerset_usage();
} else {
std::cout << "Unrecognized klammerset command: --klammerset "
<< join(words, " ") << "\n\n";
klammerset_usage();
// "none" is not filtered out here: load_klammersets() is the single
// place that decides what the word means, including that it may not be
// combined with other symbols. Deciding it twice is how kdesc came to
// accept "--klammersets none sks" and silently load neither.
load_klammersets(machine, words);
}
}
static void font_command(const strings_t& words)
{
std::string verb = words.empty() ? "list" : words[0];
@@ -147,21 +160,24 @@ int main(int argc, char* argv[])
// regexes and the coverage file column are words of their own options
// rather than verbosity levels.
args.var("k", "Show klammers. With <text>, only those whose name or description contains <text>.", "text");
args.flag("t", "Show the targets for Klammertext output");
args.flag("c", "Show the codes for accented vowels and other special characters");
args.opt("i", "Input filename. If given, it is used instead of the SKS.", "filename", "", "'text'");
args.flag("t", "Show the targets for Klammertext output");
args.opt("i", "Input filename to analyze containing klammer and other definitions", "filename", "", "'text'");
args.var("klammersets", "List the available klammersets, or load one or more for analysis. Enter \"--klammersets help\" for details.", "symbols");
args.var("font", "List installed fonts. Enter \"--font help\" for font maintenance commands.");
args.flag("argtypes", "Show the klammer argument types");
args.var("katoms", "Show the katom types. Enter \"--katoms help\" for details.");
args.flag("rewrite", "Show the katom rewrite patterns");
args.flag("optionsets", "Show the option sets declared by the input");
args.var("coverage", "Show the targets each klammer covers. Enter \"--coverage help\" for details.");
args.var("klammerset", "List the klammersets on the search path. Enter \"--klammerset help\" for details.");
args.opt("v", "'verbosity'", "n", "0", "'verbosity'");
if (show_usage(argc, argv)) {
// A bare command is a REQUEST for information, not a failure:
// usage goes to stdout (it is the result being asked for) and the
// exit status is 0, so "kdesc && echo ok" reports what happened.
args.usage(file_basename(argv[0]));
exit(1);
exit(0);
}
auto p = [&](const std::string& name) { return args.get(name) == "true"; };
@@ -208,11 +224,45 @@ int main(int argc, char* argv[])
return 0;
}
// The --klammerset subcommands operate on the search path
// (filesystem enumeration) and load no klammer set.
if (args.given("klammerset")) {
klammerset_command(option_words(args, "klammerset"));
return 0;
Machine M;
// Same rule as kdiag: a command that specifies no target evaluates
// under the GENERAL target (Andy, 2026-08-15). kdesc has no target
// argument either -- it describes what a klammer set provides for ALL
// targets -- and a designer's ".k" holding a top-level @eval hit the
// identical "Variable "K_target" not defined".
M.m_state.set("K_target", Target_registry::general_name);
strings_t input_filenames = resolve_filename_list(args.get("i"));
if (verbose_level > 0 && !input_filenames.empty()) {
std::cout << "input_filenames: " << input_filenames << "\n";
}
if (!input_filenames.empty()) {
M.m_state.set(
"K_input_dir", absolute_pathname(file_directory(input_filenames[0])));
} else {
M.m_state.set("K_input_dir", fs::current_path().string());
}
// --klammersets both lists and loads: with no words it enumerates the
// search path, with symbols it loads them for the analyses below.
if (args.given("klammersets")) {
klammerset_command(M, option_words(args, "klammersets"));
} else { // By default, SKS is loaded for analysis
M.read(fs::path(M.m_state.value("KLAMMERTEXT_HOME") + "/sks/sks.k"));
}
// Snapshot BEFORE the input is read. The distinction is not "declared
// by a klammerset" but "loaded as context": an input file may itself
// declare @@@klammerset -- that is how a designer writes one -- and
// its klammers are still the ones being asked about.
strings_t context_files = M.m_klammersets.loaded_files();
for (auto fname : input_filenames) {
if (verbose_level > 0) {
std::cout << "Read " << fname << "\n";
}
M.read(fs::path(absolute_pathname(fname)));
}
// --coverage is validated here, before a klammer set is read, so a
@@ -236,41 +286,31 @@ int main(int argc, char* argv[])
}
}
Machine M;
strings_t input_filenames = resolve_filename_list(args.get("i"));
if (verbose_level > 0) {
std::cout << "input_filenames: " << input_filenames << "\n";
}
if (input_filenames.empty()) {
M.read(fs::path(M.m_state.value("KLAMMERTEXT_HOME") + "/sks/sks.k"));
} else {
for (auto fname : input_filenames) {
if (is_klammerset_symbol(fname)) {
// A bare symbol resolves on the klammerset search
// path; kdesc has no input document, so the local
// stage is the cwd.
fname = resolve_klammerset_symbol(
fname, fs::current_path().string(), Locator()).string();
}
if (verbose_level > 0) {
std::cout << "Read " << fname << "\n";
}
M.read(fs::path(absolute_pathname(fname)));
}
strings_t own_klammers_only {};
if (!input_filenames.empty()) {
own_klammers_only = context_files;
}
if (p("t")) {
std::cout << boldblack << "Targets\n" << black << M.m_targets.describe(2, true);
std::cout << boldblack << "Targets\n" << black
<< M.m_targets.describe(2, true, own_klammers_only);
}
// "-k <text>" searches names AND descriptions, case-insensitively,
// with whitespace collapsed on both sides. An empty listing for a
// search that was actually made is reported: silence would read as a
// broken command. It is not an error -- finding nothing is a result.
// With an input file, the klammersets are loaded so the input can be
// ANALYSED against them, but they are not what the user is asking
// about: a designer wants the klammers this file defines, and a reader
// of an unfamiliar document wants the custom klammers it carries. So
// the listing is restricted to klammers with a definition outside the
// klammerset files. Without -i there is nothing else to show, so the
// klammerset itself is the answer and no filter applies.
if (args.given("k")) {
std::string search = join(option_words(args, "k"), " ");
std::string listing = M.m_klammers.describe(2, search);
std::string listing = M.m_klammers.describe(2, search, own_klammers_only);
if (listing.empty() && !search.empty()) {
std::cout << "No klammer names or descriptions contained "
<< q_(collapse_whitespace(search)) << ".\n";
@@ -280,15 +320,15 @@ int main(int argc, char* argv[])
}
if (p("optionsets")) {
std::cout << boldblack << "Option sets\n" << black << M.m_option_sets.describe(2);
std::cout << boldblack << "Option sets\n" << black
<< M.m_option_sets.describe(2, own_klammers_only);
}
// Analysis only: klammer_coverage() reads the registry and modifies
// nothing, so what a document renders to is unaffected by asking.
if (args.given("coverage")) {
report_coverage(M, klammer_coverage(M), coverage_all, std::cout);
report_coverage(M, klammer_coverage(M, own_klammers_only), coverage_all, std::cout);
}
}
catch (Error& e) {
// Nonzero, as ktext does: a command that prints an error and exits 0