summaryrefslogtreecommitdiff
path: root/tex/context/base/file-ini.mkvi
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2011-07-13 23:40:25 +0300
committerMarius <mariausol@gmail.com>2011-07-13 23:40:25 +0300
commit3dd416f677074c27a248e3433695a6fe8c13ef69 (patch)
tree20c4a573a64f2079e2e3d3fe93b004af3caf7b2f /tex/context/base/file-ini.mkvi
parent1ea50dab7f30289214b661f2cbcf53e97e6af0b6 (diff)
downloadcontext-3dd416f677074c27a248e3433695a6fe8c13ef69.tar.gz
beta 2011.07.13 20:14
Diffstat (limited to 'tex/context/base/file-ini.mkvi')
-rw-r--r--tex/context/base/file-ini.mkvi229
1 files changed, 229 insertions, 0 deletions
diff --git a/tex/context/base/file-ini.mkvi b/tex/context/base/file-ini.mkvi
new file mode 100644
index 000000000..b8ee9e2d4
--- /dev/null
+++ b/tex/context/base/file-ini.mkvi
@@ -0,0 +1,229 @@
+%D \module
+%D [ file=file-ini, % was supp-fil,
+%D version=20110701, % 1995.10.10,
+%D title=\CONTEXT\ File Macros,
+%D subtitle=Helpers,
+%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.
+
+%D \TEX\ operates on files, so one wouldn't wonder that there
+%D is a separate module for file commands. In \CONTEXT\ files
+%D are used for several purposes:
+%D
+%D \startitemize[packed]
+%D \item general textual input
+%D \item logging status information
+%D \item saving registers, lists and references
+%D \item buffering defered textual input
+%D \stopitemize
+%D
+%D When dealing with files we can load them as a whole, using
+%D the \type{\input} primitive or load them on a line||by||line
+%D basis, using \type{\read}. Writing is always done line by
+%D line, using \type{\write}.
+
+\writestatus{loading}{ConTeXt File Macros / Helpers}
+
+\registerctxluafile{file-ini}{1.001}
+
+\unprotect
+
+%D \macros
+%D {scratchread, scratchwrite}
+%D
+%D We define a scratch file for reading. Keep in mind that
+%D the number of files is limited to~16, so use this one when
+%D possible. We also define a scratch output file.
+
+\ifx\undefined\scratchread \newread \scratchread \fi
+\ifx\undefined\scratchwrite \newwrite\scratchwrite \fi
+
+%D Seldom needed:
+
+\def\openinputfile #handle#name{\immediate\openin #handle={#name}\relax}
+\def\openoutputfile #handle#name{\immediate\openout#handle={#name}\relax}
+
+\def\closeinputfile #handle{\immediate\closein #handle\relax}
+\def\closeoutputfile#handle{\immediate\closeout#handle\relax}
+
+%D \macros
+%D {writeln}
+%D
+%D This saves a few tokens:
+
+\def\writeln#handle{\write#handle{}}
+
+%D \macros
+%D {pushendofline,popendofline}
+%D
+%D When we are loading files in the middle of the typesetting
+%D process, for instance when we load references, we have to be
+%D sure that the reading process does not generate so called
+%D 'spurious spaces'. This can be prevented by assigning the
+%D line ending character the \CATCODE\ comment. This is
+%D accomplished by
+%D
+%D \starttyping
+%D \pushendofline
+%D ... reading ...
+%D \popendofline
+%D \stoptyping
+
+\newcount\endoflinelevel
+
+\def\pushendofline
+ {\advance\endoflinelevel\plusone
+ \expandafter\chardef\csname :eol:\number\endoflinelevel\endcsname\catcode\endoflineasciicode
+ \catcode\endoflineasciicode\commentcatcode\relax}
+
+\def\popendofline
+ {\catcode\endoflineasciicode\csname :eol:\number\endoflinelevel\endcsname
+ \advance\endoflinelevel\minusone}
+
+\def\restoreendofline
+ {\catcode\endoflineasciicode\endoflinecatcode}
+
+%D \macros
+%D {startreadingfile,stopreadingfile}
+%D
+%D A low level capsule:
+
+\newcount\readingfilelevel
+\newtoks \everystartreadingfile
+\newtoks \everystopreadingfile
+
+\unexpanded\def\startreadingfile% beter een every en \setnormalcatcodes
+ {\global\advance\readingfilelevel\plusone
+ \the\everystartreadingfile
+ \pushcatcodetable % saveguard
+ \setcatcodetable\ctxcatcodes
+ \ctxlua{regimes.push()}}% temporarily this way
+
+\unexpanded\def\stopreadingfile
+ {\popcatcodetable % saveguard
+ \ctxlua{regimes.pop()}% temporarily this way
+ \the\everystopreadingfile
+ \global\advance\readingfilelevel\minusone}
+
+% %D \macros
+% %D {unlinkfile}
+% %D
+% %D Sometimes we want to make sure a file is deleted, so here
+% %D is a macro that does the job. It's named after the \PERL\
+% %D one.
+%
+% \def\unlinkfile#name{\ctxlua{os.remove([[#name]])}} % obsolete
+
+%D \macros
+%D {input, normalinput}
+%D
+%D Sometimes we run into troubles when \type {\input} wants to get
+%D expanded, e.g. in a \type {\write} (which happens in the metafun
+%D manual when we permit long MP lines). So, instead of fixing that,
+%D we go for a redefinition of \type {\input}. Of course it's better
+%D to use \type {\readfile} or \type {\processfile}.
+
+\unexpanded\def\input{\normalinput}
+
+\def\inputgivenfile#name{\normalinput{#name}}
+
+%D \macros
+%D {doiffileelse}
+%D
+%D The next alternative only looks if a file is present. No
+%D loading is done. This one obeys the standard \TEX\ lookup.
+%D
+%D \starttyping
+%D \doiffileelse {filename} {found} {not found}
+%D \stoptyping
+
+\def\doiffileexistselse#name{\ctxcommand{doiffileexistelse([[#name]])}}
+
+\def\doiffileelse {\doiffileexistselse}
+\def\doiffile #name{\doiffileexistselse{#name}\firstofoneargument\gobbleoneargument}
+\def\doifnotfile #name{\doiffileexistselse{#name}\gobbleoneargument\firstofoneargument}
+
+%D \macros
+%D {doifparentfileelse}
+%D
+%D \starttyping
+%D \doifparentfileelse{filename}{yes}{no}
+%D \stoptyping
+
+\ifx\outputfilename\undefined \def\outputfilename{\jobname} \fi
+
+\def\doifparentfileelse#name{\ctxcommand{doifparentfileelse([[#name]])}}
+
+%D \macros
+%D {splitfilename}
+%D
+%D \startbuffer
+%D \def\showfilesplit
+%D {\bgroup \tttf
+%D \hbox{(full: \splitofffull)}\space
+%D \hbox{(path: \splitoffpath)}\space
+%D \hbox{(base: \splitoffbase)}\space
+%D \hbox{(name: \splitoffname)}\space
+%D \hbox{(type: \splitofftype)}\space
+%D \egroup}
+%D
+%D \splitfilename{c:/aa/bb/cc/dd.ee.ff} \showfilesplit \endgraf
+%D \splitfilename{c:/aa/bb/cc/dd.ee} \showfilesplit \endgraf
+%D \splitfilename{c:/aa/bb/cc/dd} \showfilesplit \endgraf
+%D
+%D \splitfilename{dd.ee.ff} \showfilesplit \endgraf
+%D \splitfilename{dd.ee} \showfilesplit \endgraf
+%D \splitfilename{dd} \showfilesplit \endgraf
+%D \stopbuffer
+%D
+%D \start \typebuffer \getbuffer \stop
+
+\newconstant\kindoffile % 0=normal 1=full path spec (or http) / set at the lua end
+
+\def\splitoffroot{.} \newconstant\splitoffkind
+
+\let\splitofffull\empty
+\let\splitoffpath\empty
+\let\splitoffbase\empty
+\let\splitoffname\empty
+\let\splitofftype\empty
+
+\def\splitfilename#name{\ctxcommand{splitfilename([[#name]])}}
+
+%D \macros
+%D {doonlyonce, doinputonce, doendinputonce}
+%D
+%D Especially macropackages need only be loaded once.
+%D Repetitive loading not only costs time, relocating registers
+%D often leads to abortion of the processing because \TEX's
+%D capacity is limited. One can prevent multiple execution and
+%D loading by using one of both:
+%D
+%D \starttyping
+%D \doonlyonce{actions}
+%D \doinputonce{filename}
+%D \doendinputonce{filename}
+%D \stoptyping
+%D
+%D This command obeys the standard method for locating files.
+
+\long\def\doonlyonce#whatever%
+ {\doifundefinedelse{@@@#whatever@@@}
+ {\letgvalue{@@@#whatever@@@}\empty
+ \firstofoneargument}
+ {\gobbleoneargument}}
+
+\def\doinputonce#name%
+ {\doonlyonce{#name}{\doiffileelse{#name}{\inputgivenfile{#name}}\donothing}}
+
+\def\doendinputonce#name%
+ {\doifdefined{@@@#name@@@}\endinput}
+
+\def\forgetdoingonce#whatever%
+ {\global\letbeundefined{@@@#whatever@@@}}
+
+\protect \endinput