file-res.mklx /size: 5937 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=file-mod, % was supp-fil,
3%D        version=20110701, % 1995.10.10,
4%D          title=\CONTEXT\ File Macros,
5%D       subtitle=Resolvers,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D We could move some more to the lua end (implementors).
15
16\writestatus{loading}{ConTeXt File Macros / Resolvers}
17
18\unprotect
19
20\registerctxluafile{file-res}{}
21
22%D \macros
23%D   {readfile,ReadFile}
24%D
25%D One cannot be sure if a file exists. When no file can be found, the \type
26%D {\input} primitive gives an error message and switches to interactive mode. The
27%D macro \type {\readfile} takes care of non||existing files. This macro has two
28%D faces.
29%D
30%D \starttyping
31%D \ReadFile {filename}
32%D \readfile {filename} {before loading} {not found}
33%D \stoptyping
34%D
35%D Many \TEX\ implementations have laid out some strategy for locating files. This
36%D can lead to unexpected results, especially when one loads files that are not
37%D found in the current directory. Let's give an example of this. In \CONTEXT\
38%D illustrations can be defined in an external file. The resizing macro first looks
39%D if an illustration is defined in the local definitions file. When no such file is
40%D found, it searches for a global file and when this file is not found either, the
41%D illustration itself is scanned for dimensions. One can imagine what happens if an
42%D adapted, localy stored illustration, is scaled according to dimensions stored
43%D somewhere else.
44%D
45%D When some \TEX\ implementation starts looking for a file, it normally first looks
46%D in the current directory. When no file is found, \TEX\ starts searching on the
47%D path where format and|/|or style files are stored. Depending on the
48%D implementation this can considerably slow down processing speed.
49%D
50%D In \CONTEXT, we support a project||wise ordening of files. In such an approach it
51%D seems feasible to store common files in a lower directory. When for instance
52%D searching for a general layout file, we therefore have to backtrack.
53%D
54%D These three considerations have lead to a more advanced approach for loading
55%D files.
56%D
57%D We first present an earlier implementation of \type {\readfile}. This command
58%D backtracks parent directories, upto a predefined level. Users can change this
59%D level (on the commandline using a directive); we default to~3.
60%D
61%D We use \type {\normalinput} instead of \type {\input} because we want to be able
62%D to redefine the original \type {\input} when needed, for instance when loading
63%D third party libraries.
64
65\mutable\lettonothing\readfilename
66
67\def\syst_files_read_file#protocol#path#name% #true #false
68  {\edef\readfilename{\clf_getreadfilename{#protocol}{#path}{#name}}%
69   \ifempty\readfilename
70     \expandafter\secondoftwoarguments
71   \else
72     \expandafter\syst_files_read_file_indeed
73   \fi}
74
75\def\syst_files_read_file_indeed#true#false%
76  {#true%
77   \relax
78   \normalinput{\readfilename}%
79   \relax}
80
81%D \macros
82%D   {readjobfile,readlocfile,readsysfile,
83%D    readfixfile,readsetfile}
84%D
85%D This implementation honnors the third situation, but we still can get unwanted
86%D files loaded and/or can get involved in extensive searching.
87%D
88%D Due to different needs, we decided to offer four alternative loading commands.
89%D With \type {\readjobfile} we load a local file and do no backtracking, while
90%D \type {\readlocfile} backtracks~\number \maxreadlevel\ directories, including the
91%D current one.
92%D
93%D System files can be anywhere and therefore \type {\readsysfile} is not bound to
94%D the current directory and obeys the \TEX\ implementation.
95%D
96%D Of the last two, \type {\readfixfile} searches on the directory specified and
97%D backtracks too, while \type {\readsetfile} does only search on the specified
98%D path.
99%D
100%D The most liberal is \type {\readfile}.
101
102\permanent\protected\def\readjobfile     #name{\syst_files_read_file{job}    {.}{#name}} % current path, no backtracking
103\permanent\protected\def\readlocfile     #name{\syst_files_read_file{loc}    {.}{#name}} % current path, backtracking
104\permanent\protected\def\readsysfile     #name{\syst_files_read_file{sys}    {.}{#name}} % current path, obeys tex search
105\permanent\protected\def\readfixfile#path#name{\syst_files_read_file{fix}{#path}{#name}} % specified path, backtracking
106\permanent\protected\def\readsetfile#path#name{\syst_files_read_file{set}{#path}{#name}} % specified path, no backtracking
107\permanent\protected\def\readfile        #name{\syst_files_read_file{any}    {.}{#name}}
108\permanent\protected\def\ReadFile        #name{\syst_files_read_file{any}    {.}{#name}\donothing\donothing}
109
110%D So now we've got ourselves five file loading commands:
111%D
112%D \starttyping
113%D \readfile                {filename} {before loading} {not found}
114%D
115%D \readjobfile             {filename} {before loading} {not found}
116%D \readlocfile             {filename} {before loading} {not found}
117%D \readfixfile             {filename} {before loading} {not found}
118%D \readsysfile {directory} {filename} {before loading} {not found}
119%D \stoptyping
120
121\permanent\protected\def\readtexfile#name#true#false%
122  {\pushcatcodetable
123   \catcodetable\ctxcatcodes
124   \readfile{#name}{#true}{#false}%
125   \popcatcodetable}
126
127% \permanent\protected\def\readxmlfile#name#true#false%
128%   {\pushcatcodetable
129%    \catcodetable\xmlcatcodes
130%    \readfile{#name}{#true}{#false}%
131%    \popcatcodetable}
132
133%D \macros
134%D   {doiflocfileelse,locfilename}
135%D
136%D \starttyping
137%D \doiflocfileelse {filename} {before loading} {not found}
138%D \stoptyping
139
140\permanent\protected\def\doifelselocfile#name{\clf_doifelselocfile{#name}}
141\permanent          \def\locfilename    #name{\clf_locfilename    {#name}}
142
143\aliased\let\doiflocfileelse\doifelselocfile
144
145\protect \endinput
146