Accept DeclareRobustCommand and honour providecommand semantics

This commit is contained in:
gloamxun committed 2026-10-26 03:18:48 +08:00
1 parent 7dbcea2319
commit 77d76af4a7
4 files changed
+31 -5

No files matched your search

+27 -1
View File
@@ -494,6 +494,7 @@ fn macro_definition(line: &str) -> Option<MacroDefinition> {
let rest = line
.strip_prefix("\\newcommand")
.or_else(|| line.strip_prefix("\\renewcommand"))
.or_else(|| line.strip_prefix("\\DeclareRobustCommand"))
.or_else(|| line.strip_prefix("\\providecommand"))?;
let rest = rest.trim_start();
let rest = rest.strip_prefix('*').unwrap_or(rest).trim_start();
@@ -1959,12 +1960,21 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
};
if is_macro_command("\\newcommand")
|| is_macro_command("\\renewcommand")
|| is_macro_command("\\DeclareRobustCommand")
|| is_macro_command("\\providecommand")
|| is_macro_command("\\def")
|| is_macro_command("\\gdef")
{
if let Some(definition) = macro_definition(definition_line) {
geometry.macros.push(definition);
let provided = definition_line.trim_start().starts_with("\\providecommand");
if !provided
|| !geometry
.macros
.iter()
.any(|known| known.name == definition.name)
{
geometry.macros.push(definition);
}
}
continue;
}
@@ -10201,6 +10211,22 @@ a & b
);
}
#[test]
fn declare_robust_command_defines_a_macro() {
let (body, geometry) =
geometry_and_body("\\DeclareRobustCommand{\\hello}{Hi}\n\n\\hello there.");
assert!(body.contains("Hi there."), "{body:?}");
assert!(geometry.macros.iter().any(|m| m.name == "\\hello"));
}
#[test]
fn providecommand_keeps_an_existing_definition() {
let (body, _) = geometry_and_body("\\newcommand{\\a}{A}\n\\providecommand{\\a}{B}\n\n\\a.");
assert!(body.contains("A."), "{body:?}");
let (body, _) = geometry_and_body("\\providecommand{\\b}{B}\n\n\\b.");
assert!(body.contains("B."), "{body:?}");
}
#[test]
fn setlength_assigns_skip_registers() {
let (body, geometry) = geometry_and_body(