summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/file-res.mkvi
blob: 17f4cf44c5c2b9b42a4ce2b2f7702b352707d102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%D \module
%D   [       file=file-mod, % was supp-fil,
%D        version=20110701, % 1995.10.10,
%D          title=\CONTEXT\ File Macros,
%D       subtitle=Resolvers,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt File Macros / Resolvers}

\unprotect

\registerctxluafile{file-res}{1.001}

%D \macros
%D   {readfile,ReadFile}
%D
%D One cannot be sure if a file exists. When no file can be
%D found, the \type{\input} primitive gives an error message
%D and switches to interactive mode. The macro \type{\readfile}
%D takes care of non||existing files. This macro has two faces.
%D
%D \starttyping
%D \ReadFile {filename}
%D \readfile {filename} {before loading} {not found}
%D \stoptyping
%D
%D Many \TEX\ implementations have laid out some strategy for
%D locating files. This can lead to unexpected results,
%D especially when one loads files that are not found in the
%D current directory. Let's give an example of this. In
%D \CONTEXT\ illustrations can be defined in an external file.
%D The resizing macro first looks if an illustration is defined
%D in the local definitions file. When no such file is found,
%D it searches for a global file and when this file is not
%D found either, the illustration itself is scanned for
%D dimensions. One can imagine what happens if an adapted,
%D localy stored illustration, is scaled according to
%D dimensions stored somewhere else.
%D
%D When some \TEX\ implementation starts looking for a file, it
%D normally first looks in the current directory. When no file
%D is found, \TEX\ starts searching on the path where format
%D and|/|or style files are stored. Depending on the implementation
%D this can considerably slow down processing speed.
%D
%D In \CONTEXT, we support a project||wise ordening of files.
%D In such an approach it seems feasible to store common files
%D in a lower directory. When for instance searching for a
%D general layout file, we therefore have to backtrack.
%D
%D These three considerations have lead to a more advanced
%D approach for loading files.
%D
%D We first present an earlier implementation of
%D \type{\readfile}. This command backtracks parent
%D directories, upto a predefined level. Users can change this
%D level (on the commandline using a directive); we default to~3.
%D
%D We use \type{\normalinput} instead of \type{\input}
%D because we want to be able to redefine the original
%D \type{\input} when needed, for instance when loading third
%D party libraries.

\let\readfilename\empty

\def\syst_files_read_file#protocol#path#name% #true #false
  {\edef\readfilename{\clf_getreadfilename{#protocol}{#path}{#name}}%
   \ifx\readfilename\empty
     \expandafter\secondoftwoarguments
   \else
     \expandafter\syst_files_read_file_indeed
   \fi}

\def\syst_files_read_file_indeed#true#false%
  {#true%
   \relax
   \normalinput{\readfilename}%
   \relax}

%D \macros
%D   {readjobfile,readlocfile,readsysfile,
%D    readfixfile,readsetfile}
%D
%D This implementation honnors the third situation, but we
%D still can get unwanted files loaded and/or can get involved
%D in extensive searching.
%D
%D Due to different needs, we decided to offer four alternative
%D loading commands. With \type{\readjobfile} we load a local
%D file and do no backtracking, while \type{\readlocfile}
%D backtracks~\number\maxreadlevel\ directories, including the current
%D one.
%D
%D System files can be anywhere and therefore
%D \type{\readsysfile} is not bound to the current directory
%D and obeys the \TEX\ implementation.
%D
%D Of the last two, \type{\readfixfile} searches on the
%D directory specified and backtracks too, while
%D \type{\readsetfile} does only search on the specified path.
%D
%D The most liberal is \type {\readfile}.

\unexpanded\def\readjobfile     #name{\syst_files_read_file{job}    {.}{#name}} % current path, no backtracking
\unexpanded\def\readlocfile     #name{\syst_files_read_file{loc}    {.}{#name}} % current path, backtracking
\unexpanded\def\readsysfile     #name{\syst_files_read_file{sys}    {.}{#name}} % current path, obeys tex search
\unexpanded\def\readfixfile#path#name{\syst_files_read_file{fix}{#path}{#name}} % specified path, backtracking
\unexpanded\def\readsetfile#path#name{\syst_files_read_file{set}{#path}{#name}} % specified path, no backtracking
\unexpanded\def\readfile        #name{\syst_files_read_file{any}    {.}{#name}}
\unexpanded\def\ReadFile        #name{\syst_files_read_file{any}    {.}{#name}\donothing\donothing}

%D So now we've got ourselves five file loading commands:
%D
%D \starttyping
%D \readfile                {filename} {before loading} {not found}
%D
%D \readjobfile             {filename} {before loading} {not found}
%D \readlocfile             {filename} {before loading} {not found}
%D \readfixfile             {filename} {before loading} {not found}
%D \readsysfile {directory} {filename} {before loading} {not found}
%D \stoptyping

\unexpanded\def\readtexfile#name#true#false%
  {\pushcatcodetable
   \catcodetable\ctxcatcodes
   \readfile{#name}{#true}{#false}%
   \popcatcodetable}

\unexpanded\def\readxmlfile#name#true#false%
  {\pushcatcodetable
   \catcodetable\xmlcatcodes
   \readfile{#name}{#true}{#false}%
   \popcatcodetable}

%D \macros
%D   {doiflocfileelse,locfilename}
%D
%D \starttyping
%D \doiflocfileelse {filename} {before loading} {not found}
%D \stoptyping

\unexpanded\def\doifelselocfile#name{\clf_doifelselocfile{#name}}
           \def\locfilename    #name{\clf_locfilename    {#name}}

\let\doiflocfileelse\doifelselocfile

\protect \endinput