Debian Codegraph @

texmf-dist/tex/optex/base/ref-file.opm

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \openref {File for references <2021-07-19>} % preloaded in format
4
5 \_doc --------------------------
6 The \`\_inputref` macro is executed in `\everyjob`. It reads the
7 `\jobname.ref` file, if it exists. After the file is read then it is removed
8 and opened for writing.
9 \_cod --------------------------
10
11 \_newwrite\_reffile
12
13 \_def\_inputref {%
14 \_isfile{\_jobname.ref}\_iftrue
15 \_input {\_jobname.ref}%
16 \_edef\_prevrefhash{\_mdfive{\_jobname.ref}}%
17 \_gfnotenum=0 \_lfnotenum=0 \_mnotenum=0
18 \_openref
19 \_fi
20 }
21
22 \_doc --------------------------
23 \`\_mdfive``{<file>}` expands to the MD5 hash of a given file.
24 We use it to do consistency checking of the `.ref` file. First, we read the MD5 hash
25 of `.ref` file from previous \TeX/ run before it is removed and opened for
26 writing again in the \^`\_inputref` macro. The hash is saved to \`\_prevrefhash`.
27 Second, we read the MD5 hash in the \^`\_byehook` macro again and if
28 these hashes differ, warning that \"ref file has changed" is
29 printed. Try running `optex op-demo` twice to see the effect.
30 \_cod --------------------------
31
32 \_def\_mdfive#1{\_directlua{optex.mdfive("#1")}}
33 \_def\_prevrefhash{}
34
35 \_doc --------------------------
36 If the `.ref` file does not exist, then it is not created by default. This means that if you
37 process a document without any forward references then no `\jobname.ref`
38 file is created (it would be unusable). The \^`\_wref` macro is a dummy in
39 that case.
40 \_cod --------------------------
41
42 \_def\_wrefrelax#1#2{}
43 \_let\_wref=\_wrefrelax
44
45 \_doc ---------------------
46 If a macro needs to create and use the `.ref` file, then such macro must
47 first use \`\openref`. It creates the file and redefines
48 \`\_wref` `\<macro>{<data>}` so that it
49 saves the line `\<macro><data>` to the `.ref` file using the asynchronous
50 `\write` primitive. Finally, `\_openref` destroys itself, because we
51 don't need to open the file again.\nl
52 `\_wref``<csname>{<params>}` in fact does `\write\_reffile{\string<csname><params>}`
53 and similarly \`\_ewref``<csname>{<params>}` does
54 `\write\_reffile{\string<csname><expanded-params>}`.
55 \_cod ---------------------
56
57 \_def\_openref {%
58 \_immediate\_openout\_reffile="\_jobname.ref"\_relax
59 \_gdef\_wref ##1##2{\_write\_reffile{\_bslash\_csstring##1##2}}%
60 \_immediate\_write\_reffile {\_pcent\_pcent\_space OpTeX <\_optexversion> - REF file}%
61 \_immediate\_wref \Xrefversion{{\_REFversion}}%
62 \_ifx\_refdecldata\_empty \_else \_refdeclwrite \_fi
63 \_gdef\_openref{}%
64 }
65 \_def\_ewref #1#2{\_edef\_ewrefA{#2}\_ea\_wref\_ea#1\_ea{\_ewrefA}}
66 \_def\openref{\_openref}
67
68 \_doc ----------------------
69 We are using the convention that the macros used in `.ref` file are named
70 `\_X<foo>`.
71 We don't want to read `.ref` files from old, incompatible versions of
72 \OpTeX/ (and OPmac). This is ensured by using a version number and the
73 \`\Xrefversion` macro at the beginning of the `.ref` file:
74 \begtt \catcode`\<=13
75 \Xrefversion{<version>}
76 \endtt
77 The macro checks the version compatibility.
78 Because OPmac does not understand `\_Xrefversion` we use
79 `\Xrefversion` (with a different number of `<version>` than OPmac) here.
80 The result: OPmac skips `.ref` files produced by \OpTeX/ and vice
81 versa.
82 \_cod ----------------------
83
84 \_def\_REFversion{6} % current version of .ref files in OpTeX
85 \_def\_Xrefversion#1{\_ifnum #1=\_REFversion\_relax \_else \_endinput \_fi}
86 \_public \Xrefversion ; % we want to ignore .ref files generated by OPmac
87
88 \_doc -----------------------
89 You cannot define your own `.ref` macros before `.ref` file is read
90 because it is read in `\everyjob`. But you can define such macros by using
91 \`\refdecl``{<definitions of your ref macros>}`.
92 This command writes `<definitions of your ref macros>` to the
93 `.ref` file. Then the next lines written to the `.ref` file can include
94 your macros.
95 An example from CTUstyle2:
96 \begtt
97 \refdecl{%
98 \def\totlist{} \def\toflist{}^^J
99 \def\Xtab#1#2#3{\addto\totlist{\totline{#1}{#2}{#3}}}^^J
100 \def\Xfig#1#2#3{\addto\toflist{\tofline{#1}{#2}{#3}}}
101 }
102 \endtt
103 We must read `<definitions of your ref macros>` while `#` has the catcode 12,
104 because we don't want to duplicate each `#` in the `.ref` file.\nl
105 `\refdecl` appends its data to the \`\_refdecldata` macro. It is
106 pushed to the `.ref` file immediately only if the file is opened
107 already. Otherwise we are waiting to `\openref` because we don't want
108 to open the `.ref` file if it is unnecessary.
109 \_cod \_fin -----------------
110
111 \_def\_refdecldata{}
112 \_def\_refdecl{\_bgroup \_catcode`\#=12 \_catcode`\\=12 \_catcode`\ =12 \_refdeclA}
113 \_def\_refdeclA#1{\_egroup
114 \_ifx\_refdecldata\_empty\_else \_global\_addto\_refdecldata{^^J}\_fi
115 \_global\_addto\_refdecldata{#1}%
116 \_ifx\_openref\_empty \_refdeclwrite \_fi
117 }
118 \_def\_refdeclwrite{%
119 \_immediate\_write\_reffile{\_pcent\_space \_string\refdecl:^^J\_detokenize\_ea{\_refdecldata}}%
120 \_gdef\_refdecldata{}%
121 }
122 \_public \refdecl ;
123
124 \_endcode % ================================================
125
126 A so called `.ref` (`\jobname.ref`) file is used to store data that will be
127 needed in the next \TeX/ run (information about references, TOC lines, etc.).
128 If it exists it is read by `\everyjob`, when processing of the document starts,
129 but it is not created at all if the document doesn't need any forward
130 references. Here are the typical contents of a `.ref` file:
131
132 \begtt \catcode`\<=13
133 \Xrefversion{<ref-version>}
134 \_Xpage{<gpageno>}{<pageno>}
135 \_Xtoc{<level>}{<type>}{<text>}{}<title>
136 \_Xlabel{<label>}{<text>}
137 \_Xlabel{<label>}{<text>}
138 ...
139 \_Xpage{<gpageno>}{<pageno>}
140 \_Xlabel{<label>}{<text>}
141 ...
142 \endtt
143
144 \begitems
145 * \^`\_Xpage` corresponds to the beginning of a page. <gpageno> is an internal
146 page number, globally numbered from one. `<pageno>` is the page number
147 (`\the\pageno`) used in pagination (they may differ).
148
149 * \^`\_Xtoc` corresponds to a chapter, section or subsection title on a page.
150 `<title>` is the title of the chapter (`<level>`=1, `<type>`=`chap`), section
151 (`<level>`=2, `<type>`=`sec`) or subsection (`<level>`=3, `<type>`=`secc`).
152
153 * \^`\_Xlabel` corresponds to a labelled object on a page. `<label>` is the
154 label provided by the user in \^`\label[<label>]`, while `<text>` is the text
155 which should be used for the reference (section or table number, for example
156 `2.3.14`).
157 \enditems
158
159
160 \_endinput
161
162 History:
163 2021-07-19 \openrefA merged to \openref
164 2021-07-18 ref file consistency checking added
165 2021-04-13 \Xrefversion incremented (6), new format of \_Xbib
166 2021-02-05 \_ewref introduced
167 2020-02-14 released