diff options
Diffstat (limited to 'Master/texmf-dist/tex/optex/base/more-macros.opm')
-rw-r--r-- | Master/texmf-dist/tex/optex/base/more-macros.opm | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/Master/texmf-dist/tex/optex/base/more-macros.opm b/Master/texmf-dist/tex/optex/base/more-macros.opm index 7e1d1e2b94a..ef6c6faf784 100644 --- a/Master/texmf-dist/tex/optex/base/more-macros.opm +++ b/Master/texmf-dist/tex/optex/base/more-macros.opm @@ -1,6 +1,6 @@ %% This is part of the OpTeX project, see http://petr.olsak.net/optex -\_codedecl \eoldef {OpTeX useful macros <2024-02-10>} % preloaded in format +\_codedecl \eoldef {OpTeX useful macros <2024-10-31>} % preloaded in format \_doc ----------------------------- We define \`\opinput` `{<file name>}` macro which @@ -78,7 +78,7 @@ \_public \normalcatcodes ; \_doc ----------------------------- - The \`\load` `[<filename-list>]` loads files specfied in + The \`\load` `[<filename-list>]` loads files specified in comma separated `<filename-list>`. The first space (after comma) is ignored using the trick `#1#2,`: first parameter is unseparated. The `\load` macro saves information about loaded files by setting @@ -205,39 +205,47 @@ `\_replacestringsA#1<textA>{...}` and runs `\_replacestringsA<foo-body>?<textA>!<textA>`. So, `#1` includes the first part of <foo-body> before first <textA>. - It is saved to \`\_tmptoks` and `\_replacestringsB` is run in a loop. - It finishes processing or appends the next part to `\_tmptoks` separated by - <textB> and continues loop. The final part of the macro removes the last `?` - from resulting `\_tmptoks` and defines a new version of the `\foo`. - - The \^`\replstring` macro is not expandable, but you can create your - expandable macro, for example: - \begtt - \def\replAB#1{\immediateassigned{\def\tmp{#1}\replstring\tmp{A}{B}}\the\_tmptoks} - \replAB {text A \and A} % expands to "text B \and B" - \endtt - - There exists another limitation of the \^`\replstring` macro, see - \ulink[http://petr.olsak.net/optex/optex-tricks.html\#xreplstring]{\OpTeX/ trick 0136}. - The expandable \^`\xreplstring` macro is defined by Lua code here. - And \ulink[http://petr.olsak.net/optex/optex-tricks.html\#replmacro]{\OpTeX/ trick 0137} - defines \^`\replmacro` which enables more general modifications of macros by regular expressions. + It is saved to \`\_repltoks` and `\_replacestringsB` is run with `\_replacestingsC` in a loop. + It finishes processing (if final `!` is scanned) or appends the next part to `\_repltoks` separated by + <textB> and continues loop. The final part of the \^`\replstring` macro removes the last `?` + from resulting `\_repltoks` and defines a new version of the `\foo`. \_cod ----------------------------- -\_newtoks\_tmptoks +\_newtoks\_repltoks \_catcode`!=3 \_catcode`?=3 -\_def\_replstring #1#2#3{% \replstring #1{stringA}{stringB} - \_long\_def\_replacestringsA##1#2{\_tmptoks{##1}\_replacestringsB}% - \_long\_def\_replacestringsB##1#2{\_ifx!##1\_relax \_else \_toksapp\_tmptoks{#3##1}% - \_ea\_replacestringsB\_fi}% - \_ea\_replacestringsA #1?#2!#2% - \_long\_def\_replacestringsA##1?{\_tmptoks{##1}\_edef#1{\_the\_tmptoks}}% - \_ea\_replacestringsA \_the\_tmptoks} +\_long\_def\_replstring #1#2#3{% \replstring #1{stringA}{stringB} + \_long\_def\_replacestringsA##1#2{\_repltoks\_ea{##1}\_replacestringsB\_empty}% + \_long\_def\_replacestringsB##1#2{\_ea\_replacestringsC\_ea{##1}}% + \_long\_def\_replacestringsC##1{\_ifx!##1\_empty\_else + \_toksapp\_repltoks{#3##1}\_ea\_replacestringsB\_ea\_empty\_fi}% + \_ea\_replacestringsA \_ea\_empty#1?#2!#2% + \_ea\_replacestringsD \_ea\_empty\_the\_repltoks#1} +\_long\_def\_replacestringsD #1?#2{\_repltoks\_ea{#1}\_edef#2{\_the\_repltoks}}% \_normalcatcodes \_public \replstring ; \_doc ----------------------------- + You can find three special tricks in the \^`\replstring` code: + \begitems + * Adding and removing `?` character: without this the + `\def\b{xxxA}\replstring\b{AA}{BB}` will cause an error. + * Using `\_toksapp` instead `\addto`: without this you cannot have $`#`_6$ + in the string. Moreover, it significantly + reduces the calculation time. Unfortunately, it does \^`\replstring` unexpandable. + * Using `\_empty` before scanning separated parameters and removing it + after scanning by \^`\ea`: without this \TeX/ + removes braces from `<textA>{xxx}<textA>`. + \enditems + + The \^`\replstring` macro isn't expandable. But you can use + \ulink[http://petr.olsak.net/optex/optex-tricks.html\#xreplstring]{\OpTeX/ trick 0136}. + The expandable \^`\xreplstring` macro with more features is defined by Lua code here. + And \ulink[http://petr.olsak.net/optex/optex-tricks.html\#replmacro]{\OpTeX/ trick 0137} + defines \^`\replmacro` which enables more general modifications of macros by regular expressions. + \_cod ----------------------------- + + \_doc ----------------------------- The \`\catcode` primitive is redefined here. Why? There is very common cases like \code{\\catcode`}`<something>` or `\catcode"<number>` but these characters @@ -408,6 +416,8 @@ \_endcode % ------------------------------------- +2024-10-31 \_tmptoks replaced by \_repltoks in \replstring +2024-09-09 \replstring: uses \toksapp (better speed) and \empty (keeps braces) 2024-02-10 \optdef reimplemented, it can create expandable macros now. 2024-01-18 \bracedparam reimplemented. 2023-12-04 \nnum introduced |