%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \uv {Miscenaleous <2024-11-21>} % preloaded in format

    \_doc ----------------------------
    \`\useOpTeX` and \`\useoptex` are declared as `\relax`.
    \_cod ----------------------------

\_let \useOpTeX = \_relax   \_let \useoptex = \_relax

    \_doc ----------------------------
    The \`\lastpage` and \`\totalpages` get the information from the
    \^`\_currpage`. The \^`\_Xpage` from `.ref` file sets the \^`\_currpage`.
    \_cod ----------------------------

\_def\_totalpages {\_openref\_ea\_ignoresecond\_currpage}
\_def\_lastpage   {\_openref\_ea\_usesecond\_currpage}
\_def\_currpage {{0}{?}}
\_public \lastpage \totalpages ;

    \_doc ----------------------------
    We need \`\uv`, \`\clqq`, \`\crqq`, \`\flqq`, \`\frqq`, \`\uslang`, \`\ehyph`
    \`\chyph`, \`\shyph`, for backward compatibility with \csplain.
    Codes are set according to Unicode because we are using Czech only in Unicode
    when \LuaTeX/ is used.
    \_cod ----------------------------


% for compatibility with csplain:

\_chardef\clqq=8222  \_chardef\crqq=8220
\_chardef\flqq=171   \_chardef\frqq=187
\_chardef\promile=8240

\_def\uv#1{\clqq#1\crqq}

\_let\uslang=\enlang  \_let\ehyph=\enlang
\_let\chyph=\cslang   \_let\shyph=\sklang
\_let\csUnicode=\csPatt \_let\czUnicode=\csPatt \_let\skUnicode=\skPatt

    \_doc ----------------------------
    The \`\letfont` was used in \csplain/ instead of `\fontlet`.
    \_cod ----------------------------

\_let \letfont = \_fontlet

    \_doc ----------------------------
    Non-breaking space in Unicode.
    \_cod ----------------------------

\let ^^a0=~

    \_doc ----------------------------
    Old macro packages need these funny control sequences.
    We don't use them in new macros.
    \_cod ----------------------------

\_catcode`\@=11
\_let\z@=\_zo  \_let\z@skip=\_zoskip
\_newdimen\p@ \p@=1pt
\_toksdef\toks@=0
\_let\voidb@x=\_voidbox
\_chardef\@ne=1 \_chardef\tw@=2 \_chardef\thr@@=3 \_chardef\sixt@@n=16
\_mathchardef\@m=1000 \_mathchardef\@M=10000 \_mathchardef\@MM=20000
\_countdef\m@ne=22 \m@ne=-1
\_chardef\@cclv=255 \_mathchardef\@cclvi=256
\_skipdef\skip@=0
\_dimendef\dimen@=0  \_dimendef\dimen@i=1
\_dimendef\dimen@ii=2
\_countdef\count@=255
\_def\m@th{\_mathsurround\z@}
\_def\o@lign{\_lineskiplimit\z@ \_oalign}
\_def\n@space{\_nulldelimiterspace\z@ \m@th}
\_newdimen\p@renwd \p@renwd=8.75pt
\_def\alloc@#1#2#3#4#5{\_allocator#5{\_csstring#2}#3}
\_catcode`\@=12

    \_doc ----------------------------
    We don't want to read `opmac.tex` unless `\input opmac` is specified.
    \_cod ----------------------------

\_def\OPmacversion{OpTeX}

    \_doc ----------------------------
    We allow empty lines in math formulae. It is more comfortable.
    \_cod ----------------------------

\_suppressmathparerror = 1

    \_doc ----------------------------
    Lorem ipsum can be printed by \`\lipsum``[<range>]` or \`\lorem``[<range>]`,
    for example `\lipsum[3]` or `\lipsum[112-121]`, max=150.

    First usage of `\lipsum` reads the \LaTeX/ file `lipsum.ltd.tex`
    by \`\_lipsumload`
    and prints the selected paragraph(s). Next usages of `\lipsum`
    prints the selected paragraph(s) from memory.
	`\lipsum` is fully expandable.

    `\lipsum` adds `\_par` after each printed paragraph. If you don't need
    such `\_par` here, use \`\lipsumtext``[<number>]` or `\lipsum[<number>.]`
    (i.e.\ dot after the parameter). The first case prints the paragraph
    <number> without the final `\_par` and the second case prints only
    first sentence from the paragraph <number> using \`\_lipsumdot`.
    \_cod ----------------------------

\_newbox\_nonebox
\_def\_lipsumtext[#1]{\_lipsumload\_cs{_lip:#1}}
\_def\_lipsum[#1]{\_lipsumA #1.]{#1}}
\_def\_lipsumA #1.#2]#3{\_ifx^#2^\_lipsumB #1\_empty-\_empty\_fin \_else \_lipsumdot[#1].\_fi}
\_def\_lipsumB #1-#2\_empty#3\_fin{%
   \_fornum #1..\_ifx^#2^#1\_else#2\_fi \_do {\_lipsumtext[##1]\_par}}
\_def\_lipsumfile{lipsum.ltd.tex}
\_def\_lipsumload{\_beglocalcontrol
   {\_setbox\_nonebox=\_vpack{\_tmpnum=0 % vertical mode during \input lipsum.ltd.tex
      \_def\ProvidesFile##1[##2]{}%
      \_def\SetLipsumLanguage##1{}%
      \_def\NewLipsumPar{\_incr\_tmpnum \_sxdef{_lip:\_the\_tmpnum}}%
      \_opinput\_lipsumfile
      \_glet\_lipsumload=\_empty
   }}%
   \_endlocalcontrol}
\_def\_lipsumdot[#1]{\_lipsumload \_ea\_ea\_ea \_lipsumdotA \_csname _lip:#1\_endcsname.\_fin}
\_def\_lipsumdotA #1.#2\_fin {#1}

\_public \lipsum \lipsumtext ;
\_let \lorem=\lipsum

   \_doc -----------------------------
   Selected macros from \ulink[https://petr.olsak.net/optex/optex-tricks.html]{\OpTeX/ tricks}
   are registered using \`\_regtrick``<cs-name>`. The <cs-name> is defined
   as \^`\loadtrick` `<cs-name> <cs-name>`. When a user runs such a registered
   <cs-name> then \`\loadtrick` `<cs-name>` reads the appropriate code from the
   file `optex-tricks.opm` and the <cs-name> is redefined. Finally, <cs-name>  is run again.\nl
   The `optex-tricks.opm` file includes blocks started by \^`\_trick` followed
   by the declared <cs-names> followed by semicolon followed by the code
   with declarations of <cs-names> itself. The next \`\_trick` does
   `\endpinput` of the file. The file is read inside temporary `\vbox` with `\globaldefs=1`
   because it can be read inside horizontal mode and/or inside a group.
   The `optextrick` name space is used during reading the code from
   the file. Only registered control sequences are re-defined directly
   in user name space.\nl
   You can load a code chunk by \^`\loadtrick` `<cs-name>`. This
   command doesn't run the `<cs-name>`, only loads the appropriate code. It
   should be usable if you want to load the code before the first usage of
   the `<cs-name>`.
   \_cod -----------------------------

\_def\_regtrick#1{\_ifx#1\_undefined\_def#1{\_loadtrickD#1}\_else\_badtrick\_fi}
\_def\_loadtrickD#1{\_loadtrick#1#1}
\_def\_loadtrick#1{\_beglocalcontrol
   \_resetnamespace{optextrick}\_setctable\_optexcatcodes
   \_savecatcodetable\_tmpcatcodes \_catcodetable\_tmpcatcodes
   \_long\_def\_loadtrickA ##1\_trick##2#1##3;{##1}%
   \_wlog{Loading trick macros for \_string#1}%
   \_setbox\_nonebox=\_vpack{\_let\:=\_trickprefix \_def\_trickseq{#1}%
      \_globaldefs=1 \_ea\_loadtrickA \_input {optex-tricks.opm}}%
   \_restorectable \_endnamespace
   \_endlocalcontrol
}
\_def\_trick #1;{\_endinput}
\_public \loadtrick ;

\_xargs \_regtrick \begfile \createfile \beglua \begLUA \logginglua
   \sethours \setminutes \setseconds \setweekday \showpglists \shownodes \runsystem
   \directoutput \algol \algolkw \algolcap \algolnum \makeLOA \setaca \scaleto \scaletof
   \ttlineref \lref \easylist \keepstyle \fcread \shadedframe \roundframe \cancel \ignoreinspic
   \keystroke \colortab \crx \crtop \crbot \crmid \longtable \vcent \vbot \tnote
   \tabnodes \tablebefore \framedblocks \twoblocks \pstart \settabs \import \incrpp \ispageodd
   \iniseccc \seccc \makeLOF \makeLOT \captionF \captionT \correctvsize \pgforeground
   \onlyifnew \thedimen \rebox \leftfill \rightfill \lrfill \directchar \ereplstring
   \xreplstring \replmacro \tdnum \clipbox \ismatch \isinmacro \mathinexpr \xparshape
   \csvread \csvdecl \dbcreate \exectree \showtree \jsonread \jsoncnv ;
\_sdef{_item:m}{\_loadtrick{\style m}\_cs{_item:m}}

   \_doc
   The `\:` is used as a prefix before `\def` etc. in the file `optex-tricks.opm`
   if the declared control sequence is from user namespace. It checks if a user
   have defined it before the trick is loaded and prints warning in this case.
   The `\:` is set to \`\_trickprefix` in the group before trick loading.
   If a trick includes only one autoloaded control sequence then it
   needs not to be prefixed by `\:` because if a user redefines it then
   the usage of it doesn't run autoloading.
   \_cod

\_def\_trickprefix #1#2{\_ifx#2\_undefined \_else \_def\_tmp{\_loadtrickD#2}\_ifx\_tmp#2\_else
   \_opwarning{trick by \_ea\_string\_trickseq\_space loaded: \_string#2 redefined}\_fi\_fi #1#2}

   \_doc -----------------------------
   Lua\TeX/ version 1.14 and newer provides `\partokenname` which allows to
   specify something different than `\par` at empty lines. We set `\_par`
   (see below) in \OpTeX/ version 1.04+ and newer. Some macros were rewritten
   due to this change. And we copy old versions of these changed macros here
   in order to allow to use older Lua\TeX/ versions where `\partokenname` is
   not provided.\nl
   Note that your macros where a parameter is separated by the empty line
   must be changed too. Use `\def\macro #1\_par{...}` instead
   `\def\macro #1\par{...}`.
   \_cod -----------------------------

\_ifx\_partokenname\_undefined % LuaTeX 1.13 or older:

   \_def\_incaption {\_bgroup
      \_ifcsname _\_tmpa num\_endcsname \_ea\_incr \_csname _\_tmpa num\_endcsname
      \_else \_opwarning{Unknown caption /\_tmpa}\_fi
      \_edef\_thecapnum {\_csname _the\_tmpa num\_endcsname}%
      \_edef\_thecaptitle{\_mtext{\_tmpa}}%
      \_ea\_the \_csname _everycaption\_tmpa\_endcsname
      \_def\_par{\_nbpar\_egroup}\_let\par=\_par
      \_cs{_printcaption\_tmpa}%
   }
   \_def\_boxlines{%
      \_def\_boxlinesE{\_ifhmode\_egroup\_empty\_fi}%
      \_def\_nl{\_boxlinesE}%
      \_bgroup \_lccode`\~=`\^^M\_lowercase{\_egroup\_let~}\_boxlinesE
      \_everypar{\_setbox0=\_lastbox\_endgraf
         \_hbox\_bgroup \_catcode`\^^M=13 \_let\par=\_nl \_aftergroup\_boxlinesC}%
   }
   \_def\_letter{
      \_def\_address{\_vtop\_bgroup\_boxlines \_parskip=0pt \_let\par=\_egroup}
      \_def\_subject{{\_bf \_mtext{subj}: }}
      \_public \address \subject ;
      \_typosize[11/14]
      \_vsize=\_dimexpr \_topskip + 49\_baselineskip \_relax % added 2020-03-28
      \_parindent=0pt
      \_parskip=\_medskipamount
      \_nopagenumbers
   }
   \_def\_printverbline#1{\_putttpenalty \_indent \_printverblinenum \_kern\_ttshift #1\par}
   \_public \boxlines \letter ;

\_else % LuaTeX 1.14 or newer:

   \_doc -----------------------------
   We set `\partokenneame` to `\_par` in order to keep the name `\par` in the
   public namespace for end users. I.e.\ a user can say `\def\par{paragraph}` for example
   without crash of processing the document. Se section~\ref[namespaces] for
   more details about the name space concept.\nl
   Moreover, we set `\partokencontext` to one in order to the `\_par` token
   is inserted not only at empty lines, but also at the end of `\vbox`, `\vtop` and
   `\vcenter` if horizontal mode is opened here. This differs from default
   \TeX/ behavior where horizontal mode is closed in these cases without
   inserting par token.\nl
   We set \`\_partokenset` to defined value 1 in order to the macro programmer can
   easily check these settings in \OpTeX/ format by
   `\ifx\_partokenset\undefined ... \else ...\fi`.
   \_cod -----------------------------

   \_partokenname\_par
   \_partokencontext=1
   \_let\_partokenset=1
\_fi

\_endcode

2024-11-21 \_trickprefix introduced
2024-09-09 for old LuaTeX 1.13: \_def\_bemulti removed
2024-06-27 \_loadtrickD introduced in order to save format size.
2024-03-11 \_lipsumload+\_loadtrick: \vpack used instead \vbox.
2024-03-04 \_beglocalcontrol used in \_lipsumload.
2024-02-18 \_beglocalcontrol used in \_loadtrik.
2023-12-23 \_nonebox instead box 0 introduced.
2023-10-17 \_loadtrick introduced.
2022-05-04 \lipsum[<num>]. changed to \lipsum[<num>.] to keep macro expandable.
2022-04-21 syntax \lipsum[<num>]. introduced.
2022-02-16 declaration of @-names moved here from plain-at.opm.
2021-08-02 \SetLipsumLanguage added, fix for new lipusm version
2021-01-04 \lipsumtext introduced
2020-05-22 \lipsum uses \fornum (expandable after first usage)
2020-04-02 released
