Make global definitions override group shadows
A group-local \def stayed visible after a later \gdef of the same name because scoped lookup preferred the newest scope entry, and an inline \global\def was unsupported. Route \gdef/\xdef and \global\def/\global\let/\global\chardef through an upsert that removes same-name entries from every live scope before updating the global table, so the global meaning wins inside the group and after it. A test defines a group-local L, replaces it with G via \gdef inside the group, and checks the group prints G and the following \a prints G; an inline \global\def also expands.
This commit is contained in:
1 parent
16b61cd368
commit
c3e02f0586
4 files changed
+64
-7
No files matched your search
+56
-1
@@ -768,6 +768,17 @@ fn expand_links_line(line: &str) -> String {
|
||||
output
|
||||
}
|
||||
|
||||
fn upsert_global(
|
||||
runtime: &mut Vec<MacroDefinition>,
|
||||
scopes: &mut [Vec<MacroDefinition>],
|
||||
definition: MacroDefinition,
|
||||
) {
|
||||
for scope in scopes.iter_mut() {
|
||||
scope.retain(|known| known.name != definition.name);
|
||||
}
|
||||
upsert_macro(runtime, definition);
|
||||
}
|
||||
|
||||
fn upsert_macro(table: &mut Vec<MacroDefinition>, definition: MacroDefinition) {
|
||||
if let Some(existing) = table.iter_mut().find(|known| known.name == definition.name) {
|
||||
*existing = definition;
|
||||
@@ -1752,7 +1763,9 @@ fn expand_macros_inner(
|
||||
continue;
|
||||
}
|
||||
if let Some((definition, used, global)) = runtime_definition(rest) {
|
||||
if global || scopes.is_empty() {
|
||||
if global {
|
||||
upsert_global(&mut runtime, &mut scopes, definition);
|
||||
} else if scopes.is_empty() {
|
||||
upsert_macro(&mut runtime, definition);
|
||||
} else if let Some(scope) = scopes.last_mut() {
|
||||
upsert_macro(scope, definition);
|
||||
@@ -1761,6 +1774,27 @@ fn expand_macros_inner(
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some(after) = rest.strip_prefix("\\global")
|
||||
&& !after
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|character| character.is_ascii_alphabetic())
|
||||
{
|
||||
let trimmed = after.trim_start();
|
||||
let skipped = after.len() - trimmed.len();
|
||||
if let Some((definition, used, _)) = runtime_definition(trimmed) {
|
||||
upsert_global(&mut runtime, &mut scopes, definition);
|
||||
index += "\\global".len() + skipped + used;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some((definition, used)) = runtime_plain_definition(trimmed) {
|
||||
upsert_global(&mut runtime, &mut scopes, definition);
|
||||
index += "\\global".len() + skipped + used;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Some((definition, used)) = runtime_plain_definition(rest) {
|
||||
if scopes.is_empty() {
|
||||
upsert_macro(&mut runtime, definition);
|
||||
@@ -16333,6 +16367,27 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_definitions_win_over_group_locals() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{A}{\\def\\a{L}\\gdef\\a{G}\\a}\\a and \\global\\def\\b{B}\\b\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
value["warnings"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"{}",
|
||||
value["warnings"]
|
||||
);
|
||||
let svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
let text: String = svg
|
||||
.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>()
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
assert!(text.contains("GGandB"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unexpanded_freezes_edef_body() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{A}\n\\edef\\b{\\unexpanded{\\a}}\n\\def\\a{C}\n\n\\b\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user