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