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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
% \iffalse meta-comment
% !TeX spellcheck = en-US
%
% File: codesection.sty
% Version: 2014/06/27 v0.1
% Author: Matthias Pospiech
% Email: <matthias@pospiech.eu>
%
% Copyright (C) 2014 by Matthias Pospiech <matthias@pospiech.eu>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) 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.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Matthias Pospiech.
%
% This work consists of the files codesection.dtx and codesection.ins
% and the derived filebase codesection.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{codesection.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{codesection}
%<*package>
[2014/06/27 v0.1 disableable code sections]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{hypdoc}
\RequirePackage[%
loadHyperref=true,
createIndexEntries=false,
applyLayout=true]
{doctools} %
%
\usepackage{lmodern}
\usepackage{codesection}[2014/06/27]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{codesection.dtx}
\PrintChanges
\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
% Digits \0\1\2\3\4\5\6\7\8\9
% Exclamation \! Double quote \" Hash (number) \#
% Dollar \$ Percent \% Ampersand \&
% Acute accent \' Left paren \( Right paren \)
% Asterisk \* Plus \+ Comma \,
% Minus \- Point \. Solidus \/
% Colon \: Semicolon \; Less than \<
% Equals \= Greater than \> Question mark \?
% Commercial at \@ Left bracket \[ Backslash \\
% Right bracket \] Circumflex \^ Underscore \_
% Grave accent \` Left brace \{ Vertical bar \|
% Right brace \} Tilde \~}
%
%
% \changes{v0.1}{2014/06/27}{Converted to DTX file}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{codesection.dtx}
% \title{The \textsf{codesection} package}
% \author{Matthias Pospiech \\ \url{matthias@pospiech.eu}}
% \date{\fileversion~from \filedate}
%
% \maketitle
%
% \section{Introduction}
% This packages provides an environment to switch a section of code
% on or off. The code can be placed anywhere in the file and is
% not limited to the document or the preamble.
% The motivation for this package was to have commands which allow to preselect if
% sections of code in a preamble of a template are executed or not.
%
% \section{Origin of the code}
% The code is based on the \texttt{verbatim.sty} package and was
% originally modified by Ulrich Diez to match the pure comment
% functionality. Further modifications are contributed by
% Matthias Pospiech.
% During the development some discussion about the best approach took place
% on de.comp.text.tex%
% \footnote{\url{http://groups.google.com/group/de.comp.text.tex/%
% browse_thread/thread/2c18f0c221ab167f/}}, which resulted in the current code.
%
% \section{Usage}
% The idea of the following commands is to define a collection of code, here
% notated as a \emph{section}, which can be executed as it would be without the
% commands or which is not executed at all. To use that section it must be defined
% with \emph{true} (execute code) or \emph{false} (skip code).
%
% \DescribeMacro{\DefineCodeSection} \oarg{true/false}\marg{name}
% \AfterLastParam
% Defines a code section with a \emph{name}. The default is \emph{true},
% thus the code will be executed.
%
% \DescribeMacro{\SetCodeSection} \marg{name}\marg{true/false}
% \AfterLastParam
% is like \cs{DefineCodeSection}, but with both arguments mandatory.
%
% \DescribeMacro{\BeginCodeSection} \marg{name} \AfterLastParam
% starts the code section with the given name and
%
% \DescribeMacro{\EndCodeSection} \marg{name} \AfterLastParam
% ends the code section with the given name. Note that both commands need to
% be paired and not to be nested with other code sections.
%
% \cs{BeginCodeSection} and \cs{EndCodeSection} \AfterLastParam
% mimic an environment. It would be preferable to define them as an
% environment, but that opens a group in \TeX{}, which has many disadvantages.
% For example this would make it impossible to load packages. Therefore this
% package defines paired commands and consequently, has no such limitations.
%
% %
% \section{Example}
% In the following code the first section is going to be executed and the second
% and the third are completely skipped.
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=lstDemoStyleLaTeXCode]
\DefineCodeSection[true]{ExecuteMe}
\DefineCodeSection[false]{SkipMe}
%
\BeginCodeSection{ExecuteMe}
This sentence has ...
\EndCodeSection{ExecuteMe}
%
\BeginCodeSection{SkipMe}
no end.
\EndCodeSection{SkipMe}
%
\SetCodeSection{ExecuteMe}{false}
%
\BeginCodeSection{ExecuteMe}
a different ending.
\EndCodeSection{ExecuteMe}
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \DefineCodeSection[true]{ExecuteMe}
% \DefineCodeSection[false]{SkipMe}
% \BeginCodeSection{ExecuteMe}
% This sentence has ...
% \EndCodeSection{ExecuteMe}
% %
% \BeginCodeSection{SkipMe}
% no end.
% \EndCodeSection{SkipMe}
% %
% \SetCodeSection{ExecuteMe}{false}
% %
% \BeginCodeSection{ExecuteMe}
% a different ending.
% \EndCodeSection{ExecuteMe}%
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*codesection.sty>
% \fi
%
%
% \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{codesection}
[2014/06/27 v0.1 disableable code sections]
\RequirePackage{etoolbox}
% \end{macrocode}
%
% \begin{macro}{\DefineCodeSection}
% \begin{macrocode}
%%----------------------------------------------------------------------
%% provide new if (\ifCodeSection<name>)
%% with definition \CodeSection<name><true>
%%
\newcommand{\DefineCodeSection}[2][true]{%
\expandafter\newif\csname ifCodeSection#2\endcsname
\csname CodeSection#2#1\endcsname
}%
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\SetCodeSection}
% \begin{macrocode}
%%----------------------------------------------------------------------
%% Alternative to \DefineCodeSection
\newcommand{\SetCodeSection}[2]{%
\DefineCodeSection[#2]{#1}
}%
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\BeginCodeSection}
% \begin{macrocode}
%%----------------------------------------------------------------------
\newcommand\BeginCodeSection[1]{%
\ifcsdef{ifCodeSection#1}{}{%
\PackageError{codesection}{Section #1 is unknown\MessageBreak}{}%
}
\csname ifCodeSection#1\endcsname
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
{% comment all code inside template section
\@bsphack
%% open new group
\begingroup
%% save current template section name
\def\@currtemplate{#1}%
\let\do\@makeother\dospecials
\catcode`\^^M\active
%% enter main loop
\codeSection@
}%
{% execute all code inside template section
%% = do nothing except trimming spaces
\@bsphack\@esphack%
}%
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\EndCodeSection}
% \begin{macrocode}
%%----------------------------------------------------------------------
%% The macros \@bsphack and \@esphack are internal to LaTeX;
%% they ensure that an entity like a marginal note or
%% label definition does not introduce any extra space
%% into a paragraph, independently of whether or not
%% it is attached to a word.
\newcommand\EndCodeSection[1]{\@bsphack\@esphack}
% \end{macrocode}
% \end{macro}
%
% Modified code from \texttt{verbatim.sty}. This code is not very well documented,
% because I do not understand it well enough.
% \begin{macrocode}
%%----------------------------------------------------------------------
%% usage ???
\@ifundefined{vrb@catcodes}{%
\def\vrb@catcodes{%
\catcode`\!12\catcode`\[12\catcode`\]12%
}%
}{}%
%%----------------------------------------------------------------------
\begingroup
\vrb@catcodes
\lccode`\!=`\\
\lccode`\[=`\{
\lccode`\]=`\}
\catcode`\~=\active
\lccode`\~=`\^^M
\lccode`\C=`\C
\lowercase{%
%%----------------------------------------------------------------------
\def\codeSection@#1{%
\endgroup
%% ----
\def\codeSection@##1~{\codeSection@@##1!#1\@nil}%
%% ----
\def\codeSection@@##1!#1{\futurelet\next\codeSection@@@}%
%% ----
\def\codeSection@@@##1\@nil{%
\ifx\next\@nil
\let\next\codeSection@
\else
\def\@tempa####1!#1\@nil{####1}%
\def\next{\expandafter\codeSection@test\@tempa##1\@nil~}%
\fi
\next
}%
%% ----
\def\codeSection@test##1{%
\let\next\codeSection@test
\if\noexpand##1\noexpand~\let\next\codeSection@
\else \if\noexpand##1
\else \if\noexpand##1\noexpand[\let\@tempc\@empty
\let\next\codeSection@testend
\else \def\next{\codeSection@##1}%
\fi\fi\fi
\next
}%
%% ----
\def\codeSection@testend##1{%
\if\noexpand##1\noexpand~\let\next\codeSection@
\else \if\noexpand##1\noexpand]\let\next\codeSection@@testend
\else\if\noexpand##1\noexpand!\def\next{\codeSection@!}%
\else \expandafter\def\expandafter\@tempc\expandafter
{\@tempc##1}%
\fi\fi\fi
\next
}%
%% ---- test if end statement belongs to current section
%% saved in \@currtemplate
\def\codeSection@@testend{%
\ifx\@tempc\@currtemplate
%% end group and call rescan
\edef\next{\noexpand\endgroup\noexpand\@esphack
\noexpand\codeSection@rescan{\@currtemplate}}%
\else
%% start loop gain
\let\next\codeSection@
\fi
\next
}%
%% ---- does what ???
\def\codeSection@rescan##1##2~{%
\if\noexpand~\noexpand##2~%
\else
\@warning{%
Characters dropped after
`\string\EndCodeSection{##1}'%
}%
\fi
}%
}%
%%----------------------------------------------------------------------
} % lowercase
\codeSection@{EndCodeSection}%
%%----------------------------------------------------------------------
% \end{macrocode}
%
%
% \iffalse
%</codesection.sty>
% \fi
%
% \Finale
\endinput
|