1use std::{cell::Cell, rc::Rc};
13
14use crate::{
15 common::{arena, error::Result, store::Stored},
16 state::{Scope, assign_value, lookup_definition, lookup_definition_stored, lookup_value},
17 token::{Catcode, Token},
18};
19
20pub const STORE_SETTERS: &[(&str, &str)] = &[
24 ("subtitle", "\\lx@add@subtitle{#1}"),
25 ("shorttitle", "\\lx@add@toctitle{#1}"),
26 ("toctitle", "\\lx@add@toctitle{#1}"),
27 ("titlerunning", "\\lx@add@toctitle{#1}"),
28 ("email", "\\lx@add@email{#1}"),
29 ("emailaddr", "\\lx@add@email{#1}"),
30 ("ead", "\\lx@add@email{#1}"),
31 ("address", "\\lx@add@address{#1}"),
32 ("affaddr", "\\lx@add@address{#1}"),
33 ("affil", "\\lx@add@store@affiliations{affil}{#1}"),
34 (
35 "affiliation",
36 "\\lx@add@store@affiliations{affiliation}{#1}",
37 ),
38 (
40 "affiliations",
41 "\\lx@add@store@affiliations{affiliations}{#1}",
42 ),
43 ("inst", "\\lx@add@store@affiliations{inst}{#1}"),
44 ("department", "\\lx@add@store@affiliations{department}{#1}"),
45 ("institute", "\\lx@add@store@affiliations{institute}{#1}"),
46 (
47 "institution",
48 "\\lx@add@store@affiliations{institution}{#1}",
49 ),
50 ("keywords", "\\lx@add@keywords{#1}"),
51 ("keyword", "\\lx@add@keywords{#1}"),
52 ("kword", "\\lx@add@keywords{#1}"),
53 ("kwd", "\\lx@add@keywords{#1}"),
54 ("terms", "\\lx@add@keywords{#1}"),
55 ("pacs", "\\lx@add@classification[scheme=pacs]{#1}"),
56 ("abst", "\\lx@add@abstract{#1}"),
57 ("resumen", "\\lx@add@abstract{#1}"),
58 ("received", "\\lx@add@date[role=received]{#1}"),
59 ("recdate", "\\lx@add@date[role=received]{#1}"),
60 ("revised", "\\lx@add@date[role=revised]{#1}"),
61 ("finalform", "\\lx@add@date[role=revised]{#1}"),
65 ("accepted", "\\lx@add@date[role=accepted]{#1}"),
66 ("availableonline", "\\lx@add@date[role=published]{#1}"),
67 ("pubyear", "\\lx@add@date[role=publication]{#1}"),
68 ("copyrightyear", "\\lx@add@copyrightyear{#1}"),
69 ("communicated", "\\lx@add@date[role=communicated]{#1}"),
70 ("presented", "\\lx@add@date[role=presented]{#1}"),
71 ("preprint", "\\lx@add@pubnote[role=preprint]{#1}"),
72 ("journal", "\\lx@add@pubnote[role=journal]{#1}"),
73 ("jname", "\\lx@add@pubnote[role=journal]{#1}"),
74 ("volume", "\\lx@add@pubnote[role=volume]{#1}"),
75 ("issue", "\\lx@add@pubnote[role=issue]{#1}"),
76 ("pubinfo", "\\lx@add@pubnote{#1}"),
77 ("origin", "\\lx@add@pubnote{#1}"),
78 (
83 "doi",
84 "\\lx@add@pubnote[role=doi]{\\lx@address@textfont{\\detokenize{#1}}}",
85 ),
86 ("conferenceinfo", "\\lx@add@pubnote[role=conference]{#1}"),
87 ("articletype", "\\lx@add@pubnote[role=type]{#1}"),
88 ("papertype", "\\lx@add@pubnote[role=type]{#1}"),
91 ("paperfield", "\\lx@add@pubnote{#1}"),
92 ("orcid", "\\lx@add@orcid{#1}"),
93 ("orcidID", "\\lx@add@orcid{#1}"),
94 ("editor", "\\lx@add@editor{#1}"),
95 ("editors", "\\lx@add@editor{#1}"),
96 ("speaker", "\\lx@add@creator[role=speaker]{#1}"),
97 (
103 "corraddress",
104 "\\lx@add@frontmatter{ltx:note}[role=correspondence]{#1}",
105 ),
106 (
107 "corremail",
108 "\\lx@add@frontmatter{ltx:note}[role=email]{#1}",
109 ),
110 (
111 "fundinginfo",
112 "\\lx@add@frontmatter{ltx:note}[role=funding]{#1}",
113 ),
114 ("ACMCCS", "\\lx@add@classification[scheme=ACMCCS]{#1}"),
117 (
120 "authornote",
121 "\\lx@add@frontmatter{ltx:note}[role=authornote]{#1}",
122 ),
123 (
127 "correspondingauthor",
128 "\\lx@add@frontmatter{ltx:note}[role=corresponding]{#1}",
129 ),
130];
131
132pub const DISPLACED_ONLY_SETTERS: &[&str] = &["affiliations"];
137
138fn setter_definition(name: &str) -> Result<Option<Stored>> {
142 let Some(cs_sym) = arena::get_suffixed("\\", name) else {
144 return Ok(None);
145 };
146 let cs = Token!(sym cs_sym, Catcode::CS);
147 if lookup_definition(&cs)?.is_none() {
148 return Ok(None);
149 }
150 lookup_definition_stored(&cs)
151}
152
153fn same_definition(a: &Stored, b: &Stored) -> bool {
155 match (a, b) {
156 (Stored::Expandable(a), Stored::Expandable(b)) => Rc::ptr_eq(a, b),
157 (Stored::Primitive(a), Stored::Primitive(b)) => Rc::ptr_eq(a, b),
158 (Stored::Constructor(a), Stored::Constructor(b)) => Rc::ptr_eq(a, b),
159 (Stored::MathPrimitive(a), Stored::MathPrimitive(b)) => Rc::ptr_eq(a, b),
160 (Stored::Conditional(a), Stored::Conditional(b)) => Rc::ptr_eq(a, b),
161 (Stored::Register(a), Stored::Register(b)) => Rc::ptr_eq(a, b),
162 _ => false,
163 }
164}
165
166pub fn snapshot() -> Result<Vec<Option<Stored>>> {
168 STORE_SETTERS
169 .iter()
170 .map(|(name, _)| setter_definition(name))
171 .collect()
172}
173
174fn changed_since(before: &[Option<Stored>]) -> Result<Vec<(&'static str, Stored)>> {
176 let mut changed = Vec::new();
177 for ((name, _), before) in STORE_SETTERS.iter().zip(before) {
178 let Some(after) = setter_definition(name)? else {
179 continue;
180 };
181 if !before
182 .as_ref()
183 .is_some_and(|before| same_definition(before, &after))
184 {
185 changed.push((*name, after));
186 }
187 }
188 Ok(changed)
189}
190
191pub fn mark_bound(before: &[Option<Stored>]) -> Result<()> {
196 for (name, after) in changed_since(before)? {
197 let raw =
198 lookup_value(&s!("lx_raw_setter_{name}")).is_some_and(|raw| same_definition(&raw, &after));
199 if !raw {
200 assign_value(&s!("lx_bound_setter_{name}"), after, Some(Scope::Global));
201 }
202 }
203 Ok(())
204}
205
206pub fn mark_raw(before: &[Option<Stored>]) -> Result<()> {
208 for (name, after) in changed_since(before)? {
209 assign_value(&s!("lx_raw_setter_{name}"), after, Some(Scope::Global));
210 }
211 Ok(())
212}
213
214pub fn defined_by_a_raw_file(name: &str) -> Result<bool> {
217 let Some(current) = setter_definition(name)? else {
218 return Ok(false);
219 };
220 Ok(lookup_value(&s!("lx_raw_setter_{name}")).is_some_and(|raw| same_definition(&raw, ¤t)))
221}
222
223pub fn defined_by_a_binding(name: &str) -> Result<bool> {
225 let Some(current) = setter_definition(name)? else {
226 return Ok(false);
227 };
228 Ok(
229 lookup_value(&s!("lx_bound_setter_{name}"))
230 .is_some_and(|bound| same_definition(&bound, ¤t)),
231 )
232}
233
234thread_local! {
235 static RAW_CLASS_READS: Cell<usize> = const { Cell::new(0) };
237}
238
239pub struct RawClassRead;
241
242impl RawClassRead {
243 #[must_use]
245 pub fn begin() -> Self {
246 RAW_CLASS_READS.with(|n| n.set(n.get() + 1));
247 RawClassRead
248 }
249}
250
251impl Drop for RawClassRead {
252 fn drop(&mut self) { RAW_CLASS_READS.with(|n| n.set(n.get().saturating_sub(1))); }
253}
254
255pub fn in_a_raw_class_read() -> bool { RAW_CLASS_READS.with(Cell::get) > 0 }