summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ltxtools/ltxtools-environ.sty
blob: 978b8143a3e1de53aa08e39e8b600bc98875cb4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
%                                                                          %
% This is file 'ltxtools-environ.sty', version 0.0.1, December 2011.       %
%                                                                          %
% This package and accompanying files may be distributed and/or            %
% modified under the conditions of the LaTeX Project Public License,       %
% either version 1.3 of this license or any later version. The latest      %
% version of this license is in http://www.latex-project.org/lppl.txt      %
% and version 1.3 or later is part of all distributions of LaTeX           %
% version 2005/12/01 or later.                                             %
%                                                                          %
% The LPPL maintenance status of this software is 'author-maintained'.     %
%                                                                          %
% This software is provided 'as it is', without warranty of any kind,      %
% either expressed or implied, including, but not limited to, the          %
% implied warranties of merchantability and fitness for a particular       %
% purpose.                                                                 %
%                                                                          %
% Copyright (c) 2011 Ahmed Musa (amusa22@gmail.com).                       %
%                                                                          %
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%

\@ifpackageloaded{catoptions}{}{\RequirePackage{catoptions}[2011/12/12]}
\@ifpackageloaded{ltxtools}{}{\RequirePackage{ltxtools}[2011/12/12]}
\UseNormalCatcodes
\StyleFilePurpose{Collect and execute environment body (AM)}
\StyleFileRCSInfo
$Id: ltxtools-environ.sty,v 0.0.1 2011/12/12 09:00:00 Ahmed Musa Exp $
\ProvidesPackage{ltxtools-environ}[\StyleFileInfo]
\NeedsTeXFormat{LaTeX2e}[2011/06/27]

% \newenviron, \renewenviron {<name>}{<start>}<body>{<end>}.
% Collect environment body in \envbody:
\new@def*\AlwaysTrimEnvEntries{\global\LTS@alwaystrimtrue}
\new@def*\trimenvspaces{%
  \ifboolTF{LTS@alwaystrim}\cpttrimspaces\unexpanded
}
\new@def*\everyeoenv#1{\ltsfiltermergetsv\LTS@everyeoenv{#1}\nofilter}
\everyeoenv{\@ignoretrue}
\robust@def*\newenviron{\cpt@starorlong\LTS@newenviron}
\robust@def*\LTS@newenviron#1{%
  \edef\LTS@tempa{\trimenvspaces{#1}}%
  \cptexpandarg\cpt@testopt
    {\LTS@n@wenviron{\expandcsonce\LTS@tempa}}0%
}
\robust@def*\LTS@n@wenviron#1[#2]{%
  \cpt@ifbrack
    {\LTS@n@wenv@ron#1[#2]}
    {\LTS@n@wenv@r@n{#1}{[#2]}}%
}
\robust@def*\LTS@n@wenv@ron#1[#2][#3]{\LTS@n@wenv@r@n{#1}{[#2][{#3}]}}
\robust@def*\renewenviron{\cpt@starorlong\LTS@renewenviron}
\robust@def*\LTS@renewenviron#1{%
  \edef\LTS@tempa{\trimenvspaces{#1}}%
  \ifcsndefTF\LTS@tempa{}
    {\@latex@error{Environment #1 is undefined}\@ehc}%
  \letcsntocs\LTS@tempa\relax
  \letcsntocs{end\LTS@tempa}\relax
  \expandafter\LTS@newenviron\expandafter{\LTS@tempa}%
}
\robust@def\LTS@n@wenv@r@n#1#2#3#4{%
  \ifcsndefTF{#1}{}{\letcsntocsn{#1}{end#1}}%
  \aftercsname\new@command{#1}#2{%
    \edef\LTS@beforebody{\trimenvspaces{#3}}%
    \LTS@collectbody
  }%
  \l@ngrel@x\csn@edef{end#1}{%
    \begingroup
    \defpass\noexpand\x{\endgroup\trimenvspaces{#4}}%
  }%
}
\robust@def*\LTS@collectbody{%
  \begingroup
  \toks@{}%
  \everyeof{\end{EOF}\relax}%
  \LTS@coll@ctbody
}
\robust@def\LTS@coll@ctbody#1\end#2{%
  \toks1{%
    \cptexpanded{%
      \toks@{\the\toks@\trimenvspaces{#1}%
      \noexpand\end{\expandcsonce\LTS@tempa}}%
    }%
    \LTS@coll@ctbody
  }%
  \edef\LTS@tempa{\trimenvspaces{#2}}%
  \ifxTF\LTS@tempa\@currenvir{%
    \edef\LTS@tempb{\LTS@pushbegin#1\begin\end\cpt@relax}%
    \if@csemptyTF\LTS@tempb{%
      \cptexpandsecond\endgroup{%
        \edef\noexpand\envbody{\noexpand\unexpanded{%
          \the\toks@\ifblankTF{#1}{}{\trimenvspaces{#1}}%
        }}%
        \unexpanded{%
          \LTS@beforebody\relax\LTS@everyeoenv
          \ifboolTF{LTS@alwaystrim}\@ignoretrue\relax
        }%
        \noexpand\end{\LTS@tempa}%
      }%
    }{%
      \the\toks1
    }%
  }{%
    \oifstrcmpTF{\LTS@tempa}{document}{%
      \expandafter\endgroup\expandafter
        \@checkend\expandafter{\LTS@tempa}%
    }{%
      \oifstrcmpTF{\LTS@tempa}{EOF}{%
        \expandafter\endgroup\expandafter
          \@checkend\expandafter{\LTS@tempa}%
      }{%
        \the\toks1
      }%
    }%
  }%
}
\new@def\LTS@pushbegin#1\begin#2{%
  \expandafter\ifxTF\cpt@car#2x\car@nil\end{%
    \@gobble
  }{%
    \expandafter\swapbracefirst\expandafter
    {\romannumeral-`\q\cptoxdetok\LTS@tempa}
    {\expandafter\ifstrcmpTF\expandafter
    {\detokenize\expandafter{\romannumeral-`\q
      \trimenvspaces{#2}}}}{%
      x\cptgobbletorelax
    }{%
      \LTS@pushbegin
    }%
  }%
}
\XDeclareBooleanOption{alwaystrim}[true](LTS@){}{}
\XDeclareOption*{\LTS@warn{Unknown option '\CurrentOption' ignored}}
\XExecuteOptions{alwaystrim}
\XProcessOptions*\relax

\endinput