Skip to content

Commit

Permalink
Redefinitions (brucemiller#2343)
Browse files Browse the repository at this point in the history
* Allow redefinitions of locked from .ltxml bindings and from AddToMacro

* Rename (nonstd) bibliography counter (used for IDs) to @Lx@bibliography, to avoid apparent redefinitions of \thebibliography

* Add (convoluted) test case to test definition locks

* Pearrange testlocks test case file locations
  • Loading branch information
brucemiller authored Apr 3, 2024
1 parent 5e04788 commit fcfe73d
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 14 deletions.
6 changes: 6 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,12 @@ t/daemon/api/port.xml
t/daemon/citations.tex
t/daemon/complex/exhaustive.spec
t/daemon/complex/exhaustive.xml
t/daemon/complex/testlocks.spec
t/daemon/complex/testlocks.xml
t/daemon/testlocks.tex
t/daemon/testlocks.sty.ltxml
t/daemon/testlocks-a.sty
t/daemon/testlocks-b.sty.ltxml
t/daemon/document.tex
t/daemon/fatal_100.tex
t/daemon/fatal_misdefined.tex
Expand Down
2 changes: 2 additions & 0 deletions lib/LaTeXML/Package.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,7 @@ sub loadLTXML {
AssignValue($request . '_loaded' => 1, 'global');
AssignValue($ltxname . '_loaded' => 1, 'global') if $ltxname ne $request;
ClearAutoLoad($request);
local $LaTeXML::Core::State::UNLOCKED = 1;
$STATE->getStomach->getGullet->readingFromMouth(LaTeXML::Core::Mouth::Binding->new($pathname), sub {
do $pathname;
Fatal('die', $pathname, $STATE->getStomach->getGullet,
Expand Down Expand Up @@ -2446,6 +2447,7 @@ sub AddToMacro {
Warn('unexpected', $cs, $STATE->getStomach->getGullet,
ToString($cs) . " is not an expandable control sequence", "Ignoring addition"); }
else {
local $LaTeXML::Core::State::UNLOCKED = 1; # ALLOW redefinitions that only adding to the macro
DefMacroI($cs, undef, Tokens(map { $_->unlist }
map { (blessed $_ ? $_ : TokenizeInternal($_)) } ($defn->getExpansion, @tokens)),
nopackParameters => 1, scope => 'global', locked => $$defn{locked}); }
Expand Down
16 changes: 8 additions & 8 deletions lib/LaTeXML/Package/BibTeX.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ DefPrimitive('\bibentry@create Semiverbatim', sub {
# Processing Entries
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Math ID's within the bibliography.
DefMacro('\the@XMARG@ID', '\thebibliography@ID.XM\arabic{@XMARG}');
DefMacro('\the@XMARG@ID', '\the@lx@bibliography@ID.XM\arabic{@XMARG}');

DefEnvironment('{bibtex@bibliography}',
"<ltx:bibliography xml:id='#id' "
Expand Down Expand Up @@ -524,7 +524,7 @@ DefMacro('\bib@field@proceedings@booktitle', sub {
# required: author, title, institution, year
# optional: type, number, address, month, note.
DefMacro('\bib@entry@techreport@alias', 'report');
DefMacro('\bib@entry@report@prepare', sub { copyCrossrefFields(qw(author title institution)); });
DefMacro('\bib@entry@report@prepare', sub { copyCrossrefFields(qw(author title institution)); });
DefMacro('\bib@entry@techreport@complete', '\bib@addtype{Technical report}');

#======================================================================
Expand Down Expand Up @@ -616,18 +616,18 @@ our %months = ( # Standard abbreviations (usually would appear in raw form of
jan => "1", feb => "2", mar => "3", apr => "4", may => "5", jun => "6",
jul => "7", aug => "8", sep => "9", oct => "10", nov => "11", dec => "12",
# English month names, perhaps in expanded form of month field
january => "1", february => "2", march => "3", april => "4", may => "5", june => "6",
july => "7", august => "8", september => "9", october => "10", november => "11", december => "12");
january => "1", february => "2", march => "3", april => "4", may => "5", june => "6",
july => "7", august => "8", september => "9", october => "10", november => "11", december => "12");
# Is there any sane way of looking up month names from other languages; maybe something from babel?

DefMacro('\bib@field@default@year {}', sub {
if (currentBibEntryField('date')) {
(); } # Ignore this, if we've already been given a date field.
(); } # Ignore this, if we've already been given a date field.
else {
my $date = currentBibEntryField('year');
my $x;
my $month;
if (($x = currentBibEntryRawField('month')) && ($x = $months{ lc($x) })) { $month = $x; }
if (($x = currentBibEntryRawField('month')) && ($x = $months{ lc($x) })) { $month = $x; }
elsif ($x = currentBibEntryField('month')) { $month = $months{ lc($x) } || $x; }
my $day = currentBibEntryField('day');
# What if month or day is given, but not year ???
Expand Down Expand Up @@ -913,8 +913,8 @@ sub processBibNameList {
($given ? Invocation(T_CS('\bib@given'), Tokenize($given)) : ()),
($jr ? Invocation(T_CS('\bib@lineage'), Tokenize($jr)) : ())));
}
if($etal){
push(@names, Invocation(T_CS('\bib@surname'),Tokenize('others'))); }
if ($etal) {
push(@names, Invocation(T_CS('\bib@surname'), Tokenize('others'))); }
return @names; }

# Split into space separated words, but ignore spaces within {}
Expand Down
8 changes: 4 additions & 4 deletions lib/LaTeXML/Package/LaTeX.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -4011,7 +4011,8 @@ Let('\pageref', '\ref');
# Note that it's called \refname in LaTeX's article, but \bibname in report & book.
# And likewise, mixed up in various other classes!

DefMacroI('\thebibliography@ID', undef, Tokens());
NewCounter('@lx@bibliography'); # Just for @ID
DefMacroI('\the@lx@bibliography@ID', undef, Tokens());

# Do this before digesting the body of a bibliography
sub beforeDigestBibliography {
Expand Down Expand Up @@ -4059,8 +4060,7 @@ sub beginBibliography_clean {
AssignValue(n_bibliographies => ++$bibnumber, 'global');
my $docid = ToString(Expand(T_CS('\thedocument@ID')));
my $bibid = ($docid ? $docid . '.' : '') . 'bib' . radix_alpha($bibnumber - 1);
DefMacroI(T_CS('\thebibliography@ID'), undef, T_OTHER($bibid), scope => 'global');
# $whatsit->setProperty(id=>ToString(Expand(T_CS('\thebibliography@ID'))));
DefMacroI(T_CS('\the@lx@bibliography@ID'), undef, T_OTHER($bibid), scope => 'global');
$whatsit->setProperty(id => $bibid);
my $title = ($bibtitle ? Digest($bibtitle)
: DigestIf('\refname') || DigestIf('\bibname'));
Expand Down Expand Up @@ -4243,7 +4243,7 @@ DefMacroI('\item@in@bibliography', undef, '\save@bibitem{}');
DefMacro('\restoring@bibitem',
'\let\bibitem\save@bibitem\let\par\save@par\let\\\\\save@backbackslash\bibitem');

NewCounter('@bibitem', 'bibliography', idprefix => 'bib');
NewCounter('@bibitem', '@lx@bibliography', idprefix => 'bib');
DefMacroI('\the@bibitem', undef, '\arabic{@bibitem}');
DefMacro('\@biblabel{}', '[#1]');
DefMacroI('\fnum@@bibitem', undef, '{\@biblabel{\the@bibitem}}');
Expand Down
4 changes: 2 additions & 2 deletions lib/LaTeXML/Package/amsppt.sty.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ DefMacro('\Refsname', 'References');
DefConstructor('\cite Semiverbatim', "<ltx:cite>[<ltx:bibref show='refnum' bibrefs='#1'/>]</ltx:cite>");

# arg or no arg?
DefMacro('\thebibliography@ID', '\ifx.\thedocument@id.\thedocument@id.bib\else bib\fi');
DefMacro('\the@lx@bibliography@ID', '\ifx.\thedocument@id.\thedocument@id.bib\else bib\fi');

DefMacro('\@auto@Refs', '\Refs');
DefMacro('\Refs', '\@Refs\def\@auto@Refs{}');
Expand All @@ -344,7 +344,7 @@ DefConstructor('\@Refs',
. "<ltx:title>References</ltx:title>"
. "<ltx:biblist>",
afterDigest => sub {
$_[1]->setProperty(id => ToString(Expand(T_CS('\thebibliography@ID')))); });
$_[1]->setProperty(id => ToString(Expand(T_CS('\the@lx@bibliography@ID')))); });
DefMacro('\endRefs', '\def\@auto@Refs{\Refs}\end@Refs');
DefConstructor('\end@Refs', "</ltx:bibliography>");

Expand Down
4 changes: 4 additions & 0 deletions t/daemon/complex/testlocks.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
path = ../
preload = testlocks.sty
source = ../testlocks.tex
format = xml
23 changes: 23 additions & 0 deletions t/daemon/complex/testlocks.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<?latexml package="testlocks"?>
<?latexml class="article"?>
<?latexml RelaxNGSchema="LaTeXML"?>
<document xmlns="http://dlmf.nist.gov/LaTeXML">
<resource src="LaTeXML.css" type="text/css"/>
<resource src="ltx-article.css" type="text/css"/>
<para xml:id="p1">
<p>testlocks FOO-A:testlocks FOO-A</p>
</para>
<para xml:id="p2">
<p>testlocks FOO-B:testlocks FOO-B</p>
</para>
<para xml:id="p3">
<p>testlocks FOO-C:testlocks FOO-C</p>
</para>
<para xml:id="p4">
<p>testlocks-b FOO-D:testlocks-b FOO-D</p>
</para>
<para xml:id="p5">
<p>testlocks FOO-E:testlocks FOO-E</p>
</para>
</document>
4 changes: 4 additions & 0 deletions t/daemon/testlocks-a.sty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

% Ignored attempt to redefine \fooC
\def\fooC{testlocks-a FOO-C}
\RequirePackage{testlocks-b};
12 changes: 12 additions & 0 deletions t/daemon/testlocks-b.sty.ltxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

# RE-defintions (allowed)
DefMacro('\fooD', "testlocks-b FOO-D", locked => 1);

# Definition, BUT will be RE-defined by testlocks.sty
DefMacro('\fooE', "testlocks-b FOO-E", locked => 1);

1;
16 changes: 16 additions & 0 deletions t/daemon/testlocks.sty.ltxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

DefMacro('\fooA', "testlocks FOO-A", locked => 1);
DefMacro('\fooB', "testlocks FOO-B", locked => 1);
DefMacro('\fooC', "testlocks FOO-C", locked => 1);
DefMacro('\fooD', "testlocks FOO-D", locked => 1);

InputDefinitions("testlocks-a", type => 'sty', noltxml => 1);

# This gets defined in testlocks-b.sty.ltxml, input from testlocks-a.sty
DefMacro('\fooE', "testlocks FOO-E", locked => 1);

1;
16 changes: 16 additions & 0 deletions t/daemon/testlocks.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
\documentclass{article}
%\usepackage{testlocks}
% Ignored re-definition
\def\fooB{Locak FOO-B}
\begin{document}
testlocks FOO-A:\fooA

testlocks FOO-B:\fooB

testlocks FOO-C:\fooC

testlocks-b FOO-D:\fooD

testlocks FOO-E:\fooE

\end{document}

0 comments on commit fcfe73d

Please sign in to comment.