Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active December 27, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/7319188 to your computer and use it in GitHub Desktop.
Save zr-tex8r/7319188 to your computer and use it in GitHub Desktop.
LaTeX: Some additional string functions for LISP on TeX
% bxlot-string.sty
%% helpers
\long\def\bxlot@gobble#1{}
\long\def\bxlot@car#1#2{#1}
\long\def\bxlot@cdr#1#2{#2}
\def\bxlot@end{\bxlot@end@}
\long\def\bxlot@skip#1\bxlot@end{}
% \bxlot@sp is an implicit space token
\edef\bxlot@next{\let\noexpand\bxlot@sp=\space\space}
\bxlot@next
%% \bxlot@cond\if...\fi{<true>}{<false>}
\bxlot@gobble\if\bxlot@gobble\if
\def\bxlot@cond#1\fi{%
#1\expandafter\bxlot@car\else\expandafter\bxlot@cdr\fi
}
%% \bxlot@assign@as\@tlabel@XXX\CS
% Assigns to \CS the value held in \bxlot@result.
\def\bxlot@assign@as#1#2{%
\expandafter\gdef\expandafter#2\expandafter{%
\expandafter#1\expandafter{\bxlot@result}}%
}
%% internal funcs 'bx@strcar'/'bx@strcdr'
\addassoc\@globalenv\bx@strcar{\@tlabel@func{\@bxstr@strcar}}
\def\@bxstr@strcar#1#2#3{%
% type check omitted, because it's internal
\ifx\bxlot@end#3\bxlot@end \let\bxlot@result\empty
\else \bxlot@strcar@a#3\bxlot@end
\fi
\bxlot@assign@as\@tlabel@string#1%
}
\addassoc\@globalenv\bx@strcdr{\@tlabel@func{\@bxstr@strcdr}}
\def\@bxstr@strcdr#1#2#3{%
% type check omitted, because it's internal
\ifx\bxlot@end#3\bxlot@end \let\bxlot@result\empty
\else \bxlot@strcdr@a#3\bxlot@end
\fi
\bxlot@assign@as\@tlabel@string#1%
}
\def\bxlot@strcar@a{%
\futurelet\bxlot@tok\bxlot@strcar@b
}
\def\bxlot@strcar@b{%
\bxlot@cond\ifcat\noexpand\bxlot@tok\bxlot@sp\fi{%
\def\bxlot@result{ }%
\bxlot@skip
}{\bxlot@cond\ifcat\noexpand\bxlot@tok\bgroup\fi{%
\bxlot@strcar@c
}{%else
\bxlot@strcar@d
}}%
}
\def\bxlot@strcar@c#1{%
\def\bxlot@result{{#1}}% brace-enclosed
\bxlot@skip
}
\def\bxlot@strcar@d#1{%
\def\bxlot@result{#1}%
\bxlot@skip
}
\def\bxlot@strcdr@a{%
\futurelet\bxlot@tok\bxlot@strcdr@b
}
\def\bxlot@strcdr@b{%
\bxlot@cond\ifcat\noexpand\bxlot@tok\bxlot@sp\fi{%
\bxlot@strcdr@c
}{%else
\bxlot@strcdr@d
}%
}
\expandafter\def\expandafter\bxlot@strcdr@c\space{%
\bxlot@strcdr@d\space
}
\def\bxlot@strcdr@d#1\bxlot@end{%
\expandafter\def\expandafter\bxlot@result
\expandafter{\bxlot@gobble#1}%
}
%% function 'explode'
\addassoc\@globalenv\bx@explode{\@tlabel@func{\@bxstr@explode}}
\def\@bxstr@explode#1#2#3{% simply checks the type
\ifx#2\@tlabel@string
\gdef#1{\@tlabel@string{#3}}% identity
\else
\errmessage{LISP on TeX [function --- explode]:%
The argument of explode must be a string.}%
\fi
}
\lispinterp{%
(\define \explode (\lambda (\s)
(\bx@explode@ (\bx@explode \s))))
(\define \bx@explode@ (\lambda (\s)
(\lispif (\= \s '') ()
(\cons (\bx@strcar \s) (\bx@explode@ (\bx@strcdr \s))))))
}
%% function 'explode'
\addassoc\@globalenv\groupedQ{\@tlabel@func{\@bxstr@groupedQ}}
\def\@bxstr@groupedQ#1#2#3{%
\ifx#2\@tlabel@string
\bxlot@groupedQ@a#3\bxlot@end
\bxlot@assign@as\@tlabel@bool#1%
\else
\errmessage{LISP on TeX [function --- groupedQ]:%
The argument of groupedQ must be a string.}%
\fi
}
\def\bxlot@groupedQ@a{%
\futurelet\bxlot@tok\bxlot@groupedQ@b
}
\def\bxlot@groupedQ@b{%
\def\bxlot@result{f}%
\bxlot@cond\ifcat\noexpand\bxlot@tok\bgroup\fi{%
\bxlot@groupedQ@c
}{%else
\bxlot@skip
}%
}
\def\bxlot@groupedQ@c#1{%
\futurelet\bxlot@tok\bxlot@groupedQ@d
}
\def\bxlot@groupedQ@d{%
\bxlot@cond\ifx\bxlot@tok\bxlot@end\fi{%
\def\bxlot@result{t}%
}{}
\bxlot@skip
}
%% EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment