diff --git a/README.md b/README.md index 019261c..86778ec 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ $ greet hello world ``` -Use backslash `\` for multi-line bodies: +Use backslash `\` for multi-line bodies. The body can start on the same line or on the next continuation line: ``` @def serve \ diff --git a/src/parse.test.ts b/src/parse.test.ts index 8c59714..8bf6cad 100644 --- a/src/parse.test.ts +++ b/src/parse.test.ts @@ -204,6 +204,14 @@ describe("parse", () => { expect(result.commands).toHaveLength(1) }) + test("@def with body starting on continuation line", () => { + const content = "@def serve \\\n python3 -m http.server\n$ serve\n" + const result = parse("test.shout", content) + expect(result.directives).toEqual([ + { type: "def", name: "serve", body: "python3 -m http.server", line: 1 }, + ]) + }) + test("@def multiple macros", () => { const content = "@def foo echo foo\n@def bar echo bar\n$ foo\nfoo\n" const result = parse("test.shout", content) diff --git a/src/parse.ts b/src/parse.ts index 28a392d..18a70f0 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -91,7 +91,8 @@ function parseDefDirective( if (next.startsWith("$") || next.startsWith("@") || next.trim() === "" || next.startsWith("#")) { throw new Error(`${path}:${i + extra + 2}: @def continuation consumed a command or directive line`) } - body = body.slice(0, -1).trimEnd() + "\n" + next.trim() + const prefix = body.slice(0, -1).trimEnd() + body = prefix ? prefix + "\n" + next.trim() : next.trim() extra++ } diff --git a/web/index.html b/web/index.html index e7761f6..b24cd84 100644 --- a/web/index.html +++ b/web/index.html @@ -225,7 +225,7 @@ $ greet hello world -

If a command matches a macro name exactly, the body is substituted. Use \ for multi-line bodies. Macros from setup files are inherited; user-file macros override them.

+

If a command matches a macro name exactly, the body is substituted. Use \ for multi-line bodies — the body can start on the same line or on the next continuation line. Macros from setup files are inherited; user-file macros override them.