summaryrefslogtreecommitdiff
path: root/macros/luatex/optex/op-macros.opm
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/optex/op-macros.opm')
-rw-r--r--macros/luatex/optex/op-macros.opm94
1 files changed, 94 insertions, 0 deletions
diff --git a/macros/luatex/optex/op-macros.opm b/macros/luatex/optex/op-macros.opm
new file mode 100644
index 0000000000..659abfe875
--- /dev/null
+++ b/macros/luatex/optex/op-macros.opm
@@ -0,0 +1,94 @@
+%% This is part of OpTeX project, see http://petr.olsak.net/optex
+
+\_codedecl \eoldef {OpTeX useful macos <2020-02-14>}
+
+\_newcatcodetable \_optexcatcodes
+\_savecatcodetable\_optexcatcodes
+
+\_def\_normalcatcodes {\_catcodetable\_optexcatcodes \_savecatcodetable0 \_catcodetable0 }
+
+\_newcatcodetable \_plaintexcatcodes
+\_public
+ \optexcatcodes \plaintexcatcodes ;
+
+{\_catcode`_=8 \savecatcodetable\plaintexcatcodes}
+
+% \catcodetable stack
+% use \setctable<num> ... \setctable<num> ... \restorectable ... \restorectable
+
+\_newcount\_currctable \_currctable=0
+\_catcodetable0
+
+\_def\_setctable#1{\_edef\_ctablelist{{\_the\_currctable}\_ctablelist}%
+ \_catcodetable#1\_relax \_currctable=#1\_relax
+}
+\_def\_restorectable{\_ea\_restorectableA\_ctablelist\_relax}
+\_def\_restorectableA#1#2\_relax{%
+ \_ifx^#2^\_opwarning
+ {You can't use \_noindent\restorectable without previous \_string\setctable}%
+ \_else \_def\_ctablelist{#2}\_catcodetable#1\_relax \_currctable=#1\_relax \_fi
+}
+\_def\_ctablelist{.}
+
+\_def\_opinput #1{\_setctable\_optexcatcodes \_input "#1" \_restorectable}
+
+
+% \optdef \foo [opt-default] #1#2{...\the\opt...}, usage \foo {}{} or \foo [opt] {}{}
+
+\_def\_optdef#1[#2]{%
+ \_def#1{\_opt={#2}\_isnextchar[{\_cs{_oA:\_string#1}}{\_cs{_oB:\_string#1}}}%
+ \_sdef{_oA:\_string#1}[##1]{\_opt={##1}\_cs{_oB:\string#1\_nospaceafter}}%
+ \_sdef{_oB:\_string#1\_nospaceafter}%
+}
+\_def\_nospaceafter#1{\_ea#1\_romannumeral-`\.}
+
+\_newtoks\_opt
+
+
+% \eoldef \foo #1{thex to end of line is: #1}
+
+\_def\_eoldef #1{\_def #1{\_begingroup \_catcode`\^^M=12 \_eoldefA #1}%
+ \_ea\_def\_csname _\_string #1:M\_endcsname}
+\_catcode`\^^M=12 %
+\_def\_eoldefA #1#2^^M{\_endgroup\_csname _\_string #1:M\_endcsname{#2}}%
+\_normalcatcodes %
+
+\_public \opt \optdef \eoldef ;
+
+\_catcode`!=3 \_catcode`?=3
+\_def\_replstring #1#2#3{% \replstring #1{stringA}{stringB}
+ \_long\_def\_replacestringsA##1#2{\_def #1{##1}\_replacestringsB}%
+ \_long\_def\_replacestringsB##1#2{\_ifx!##1\_relax \_else \_addto #1{#3##1}%
+ \_ea\_replacestringsB\_fi}%
+ \_ea\_replacestringsA #1?#2!#2%
+ \_long\_def\_replacestringsA##1?{\_def #1{##1}}\_ea\_replacestringsA #1}
+\_normalcatcodes
+
+\_def\_remfirstunderscore#1{\_ea\_remfirstunderscoreA#1\_relax#1}
+\_def\_remfirstunderscoreA#1#2\_relax#3{\_if _#1\_def#3{#2}\_fi}
+
+\_ea\_def \_ea\_meaningsepare \_ea#\_ea1\_string m#2:#3->#4^{{#1}{#3}{#4}}
+
+% \keepinputnames ... \input foo ... \lastinputname -> foo
+
+\_def\_keepinputnames{\_directlua{%
+ callback.register("find_read_file",
+ function (id, name)
+ input_file_name = name
+ return name
+ end)
+}}
+\_def\_lastinputname{\_directlua{tex.print(input_file_name)}}
+
+\def\catcode{\_catcode\_string} % more robust in cases \catcode` or \catcode"
+
+\_def\_enquotes{\_chardef\"="201C } % use \"text" for English quotes
+
+\public
+ \setctable \restorectable
+ \normalcatodes \opt \optdef \eoldef \replstring
+ \keepinputnames \lastinputname \enquotes ;
+
+
+\_endcode % -------------------------------------
+