summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/cprotect/cprotect.sty
blob: d340995fb557606fb8f481c20773d5de462f6c99 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%%
%% This is file `cprotect.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% cprotect.dtx  (with options: `package')
%% ----------------------------------------------------------------
%% cprotect --- verbatim in macro arguments
%% E-mail: bruno@le-floch.fr
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cprotect}[2010/12/30 v1.0 (Bruno Le Floch)]
\RequirePackage{ifthen}
\RequirePackage{suffix}
\newwrite\CPT@WriteOut
\newcounter{CPT@WriteCount}
\newcommand{\CPT@Write}[1]{%
  \stepcounter{CPT@WriteCount}%
  \immediate\openout\CPT@WriteOut=\jobname-%
                              \arabic{CPT@WriteCount}.cpt%
  \newlinechar`\^^M%
  \immediate\write\CPT@WriteOut{#1}%
  \immediate\closeout\CPT@WriteOut%
  \aftergroup\CPT@setLastFileName%
}
\newcommand{\CPT@setLastFileName}{%
  \def\CPT@lastFileName{\jobname-\arabic{CPT@WriteCount}.cpt}}
\newcommand{\CPT@input@last}{%
  \expandafter\protect\expandafter\input
                              \expandafter{\CPT@lastFileName}}
\newcommand{\makeallletters}{%
  \count0=0\relax %
  \loop %
  \catcode\count0=11\relax %
  \advance\count0 by 1\relax %
  \ifnum\count0<256 %
  \repeat %
}
\newcommand\ReadVerbatimUntil[2][]{%
  \def\CPT@commandatend{#2}%
  \begingroup #1%
  \makeallletters%
  \CPT@setup}
\WithSuffix\newcommand\ReadVerbatimUntil*[2][]{%
  \def\CPT@commandatend{#2}%
  \begingroup #1%
  \makeallletters%
  \CPT@starsetup}
\newcommand{\CPT@def}[2]{\expandafter\def\expandafter#1%
  \expandafter##\expandafter1#2}
\newcommand{\CPT@setup}[1]{%
  \def\CPT@delimiter{#1}%
  \CPT@def\CPT@readPreText\CPT@delimiter{%
       \def\CPT@preText{##1}\CPT@readPostText}%
  \CPT@def\CPT@readPostText\CPT@delimiter{%
       \def\CPT@postText{##1}\CPT@readBegin}%
  \CPT@def\CPT@readBegin\CPT@delimiter{%
       \def\CPT@begin{##1}\CPT@readEnd}%
  \CPT@def\CPT@readEnd\CPT@delimiter{%
       \def\CPT@end{##1}\CPT@readContent}%
  \CPT@readPreText%
}
\newcommand{\CPT@starsetup}[1]{\CPT@setup#1#1#1}
\def\CPT@qend{\CPT@qend}
\newcounter{CPT@numB}
\newcommand{\CPT@store}[1]{\edef\CPT@storage{\CPT@storage#1}}
\newcommand{\CPT@readContent}{%
  \CPT@def\CPT@gobbleOneB\CPT@begin##2{%
    \ifx\CPT@qend##2\CPT@store{##1}\addtocounter{CPT@numB}{-1}%
    \else\CPT@store{##1\CPT@begin}\stepcounter{CPT@numB}%
    \expandafter\CPT@gobbleOneB\expandafter##2\fi}%
  %
  \CPT@def\CPT@gobbleUntilE\CPT@end{%
    \edef\CPT@tempi{##1\CPT@begin}%
    \expandafter\CPT@gobbleOneB\CPT@tempi\CPT@qend%
    \ifthenelse{\value{CPT@numB}<0}{%
      \CPT@store{\CPT@postText}%
      \CPT@Write{\CPT@storage}\endgroup%
      \CPT@commandatend%
    }{%
      \CPT@store{\CPT@end}\CPT@gobbleUntilE%
    }%
  }%
  \setcounter{CPT@numB}{0}%
  \def\CPT@storage{\CPT@preText}%
  \CPT@gobbleUntilE%
}
\newcommand{\cprotect}[1]{\def\CPT@cs{#1}%
  \afterassignment\CPT@A\let\CPT@next}
\begingroup
  \catcode`\{=11 \catcode`\}=11 \catcode`\+=11
  \catcode`\(=1 \catcode`\)=2
  \gdef\CPT@A(%
    \ifx\CPT@next\bgroup%
    \expandafter\CPT@n{%
    \else%
    \expandafter\CPT@N%
    \fi)
  \gdef\CPT@n{(\ReadVerbatimUntil*(%
         \CPT@cs(\CPT@input@last))+{+}+)%
  \gdef\CPT@N(\expandafter\CPT@n{\CPT@next })%
\endgroup
\newcommand{\cMakeRobust}[1]{%
  \def\CPT@cs@name{\expandafter\@gobble\string#1}%
  \expandafter\let\csname CPT@old@\CPT@cs@name\endcsname #1%
  \expandafter\def\csname\CPT@cs@name\endcsname{%
    \expandafter\cprotect\csname CPT@old@\CPT@cs@name\endcsname}%
}
\newcommand{\CPTbegin}[1]{%
  \def\CPT@commandatend{\begin{#1}\CPT@input@last\end{#1}}%
  \begingroup%
  \CPT@env@setup{#1}%
  \makeallletters%
  \CPT@readContent%
}
\begingroup\catcode`\/=0
/begingroup/catcode`/\=11
  /catcode`/{=11 /catcode`/}=11 /catcode`/-=11
  /catcode`/(=1 /catcode`/)=2
  /gdef/CPT@env@setup#1(%
    /def/CPT@preText(\relax )%
    /def/CPT@postText(\relax )%
    /def/CPT@begin(\begin{#1})%
    /def/CPT@end(\end{#1})%
    )
/endgroup
\endgroup
\def\cprotEnv\begin{\CPTbegin}
%% 
%% Copyright (C) 2010 by Bruno Le Floch <bruno@le-floch.fr>
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License (LPPL), either
%% version 1.3c of this license or (at your option) any later
%% version.  The latest version of this license is in the file:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status) by
%% Bruno Le Floch.
%% 
%% This work consists of the file  cprotect.dtx
%% and the derived files           cprotect.ins,
%%                                 cprotect.pdf and
%%                                 cprotect.sty.
%% 
%%
%% End of file `cprotect.sty'.