| 1 |
% \iffalse meta-comment |
| 2 |
%<=*COPYRIGHT> |
| 3 |
%% Copyright (c) 2011-2022 by Martin Scharrer <[email protected]> |
| 4 |
%% ----------------------------------------------------------------------- |
| 5 |
%% This work may be distributed and/or modified under the |
| 6 |
%% conditions of the LaTeX Project Public License, either version 1.3 |
| 7 |
%% of this license or (at your option) any later version. |
| 8 |
%% The latest version of this license is in |
| 9 |
%% http://www.latex-project.org/lppl.txt |
| 10 |
%% and version 1.3 or later is part of all distributions of LaTeX |
| 11 |
%% version 2005/12/01 or later. |
| 12 |
%% |
| 13 |
%% This work has the LPPL maintenance status `maintained'. |
| 14 |
%% |
| 15 |
%% The Current Maintainer of this work is Martin Scharrer. |
| 16 |
%% |
| 17 |
%% This work consists of the files lstautogobble.dtx and lstaddons.ins |
| 18 |
%% and the derived filebase lstautogobble.sty. |
| 19 |
%% |
| 20 |
%<=/COPYRIGHT> |
| 21 |
% \fi |
| 22 |
% |
| 23 |
% \iffalse |
| 24 |
%<*driver> |
| 25 |
\ProvidesFile{lstautogobble.dtx}[% |
| 26 |
%<=*DATE> |
| 27 |
2012/05/03 |
| 28 |
%<=/DATE> |
| 29 |
%<=*VERSION> |
| 30 |
v0.1 |
| 31 |
%<=/VERSION> |
| 32 |
DTX file for 'lstautogobble'] |
| 33 |
\documentclass{ydoc} |
| 34 |
\GetFileInfo{lstautogobble.dtx} |
| 35 |
\usepackage{lstautogobble}[\filedate] |
| 36 |
\EnableCrossrefs |
| 37 |
\CodelineIndex |
| 38 |
\RecordChanges |
| 39 |
\begin{document} |
| 40 |
\DocInput{\jobname.dtx} |
| 41 |
\PrintChanges |
| 42 |
\PrintIndex |
| 43 |
\end{document} |
| 44 |
%</driver> |
| 45 |
% \fi |
| 46 |
% |
| 47 |
% \CheckSum{0} |
| 48 |
% |
| 49 |
% \CharacterTable |
| 50 |
% {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 |
| 51 |
% 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 |
| 52 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 53 |
% Exclamation \! Double quote \" Hash (number) \# |
| 54 |
% Dollar \$ Percent \% Ampersand \& |
| 55 |
% Acute accent \' Left paren \( Right paren \) |
| 56 |
% Asterisk \* Plus \+ Comma \, |
| 57 |
% Minus \- Point \. Solidus \/ |
| 58 |
% Colon \: Semicolon \; Less than \< |
| 59 |
% Equals \= Greater than \> Question mark \? |
| 60 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 61 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 62 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 63 |
% Right brace \} Tilde \~} |
| 64 |
% |
| 65 |
% |
| 66 |
% \changes{v0.1}{2012/05/03}{Initial release.} |
| 67 |
% |
| 68 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 69 |
% |
| 70 |
% \GetFileInfo{lstautogobble.dtx} |
| 71 |
% \author{Martin Scharrer} |
| 72 |
% \email{[email protected]} |
| 73 |
% \ifdefined\repository |
| 74 |
% \repository{https://github.com/MartinScharrer/lstaddons/} |
| 75 |
% \fi |
| 76 |
% |
| 77 |
% \maketitle |
| 78 |
% |
| 79 |
% \begin{abstract}\noindent |
| 80 |
% This add-on package to \pkg{listings} provides a boolean \Key{autogobble} |
| 81 |
% setting which will automatically set the \Key{gobble} setting to indention |
| 82 |
% of the first line. |
| 83 |
% |
| 84 |
% This package was created as response to the question |
| 85 |
% \href{http://tex.stackexchange.com/q/19953/2975}{``How to automatically skip leading white spaces in listings?''} |
| 86 |
% on \href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}. |
| 87 |
% \end{abstract} |
| 88 |
% |
| 89 |
% \section{Introduction} |
| 90 |
% The \pkg{listings} package has a setting \Key{gobble}'='<number> which allows to remove a certain number |
| 91 |
% of characters from the beginning of every line in the listing. This can be used to indent the listing |
| 92 |
% in the source code without affecting the printed result. However, this forces the user to set a suitable |
| 93 |
% value manually. An incorrect value will lead either to an indented listing or to missing leading characters. |
| 94 |
% |
| 95 |
% A solution for this is to automatically detect the used indention of the listing and that the \Key{gobble} setting |
| 96 |
% to this value. This functionality is provided by this package. |
| 97 |
% For this it reads and scans the first listing line and reinserts it again afterwards. |
| 98 |
% |
| 99 |
% |
| 100 |
% \section{Usage} |
| 101 |
% After loading \pkg{lstautogobble} the following new \pkg{listings} setting |
| 102 |
% is available: |
| 103 |
% |
| 104 |
% \DescribeKey{autogobble}'=true|false' |
| 105 |
% This boolean setting switches the autogobble feature on or off. |
| 106 |
% If no value is used the default is `|true|'. The initial setting is `|false|'. |
| 107 |
% One enabled the first line of any \pkg{lstlisting} is scanned and the amount |
| 108 |
% of spaces or tabulators is used to set the \Key{gobble} setting. |
| 109 |
% If \Key{gobble} is set manually it will not be overwritten and \Key{autogobble}'=true' |
| 110 |
% is ignored. |
| 111 |
% |
| 112 |
% \section{Examples / Tests} |
| 113 |
% The following code is intended as examples and also for testing the package. |
| 114 |
% Here the \Key{autogobble} feature is globally enabled. |
| 115 |
% |
| 116 |
% \lstdefinestyle{exampleresult}{basicstyle=\ttfamily,columns=fullflexible,autogobble=true}% |
| 117 |
% |
| 118 |
% \begin{example} |
| 119 |
% \caption{Only environment (with \Key{autogobble} enabled globally).} |
| 120 |
% \begin{examplecode} |
| 121 |
% \begin{lstlisting} |
| 122 |
% test |
| 123 |
% it |
| 124 |
% \end{lstlisting} |
| 125 |
% \end{examplecode} |
| 126 |
% \end{example} |
| 127 |
% |
| 128 |
% \begin{example} |
| 129 |
% \caption{With options (must be skipped and reinserted).} |
| 130 |
% \begin{examplecode} |
| 131 |
% \begin{lstlisting}[basicstyle=\ttfamily\scriptsize] |
| 132 |
% test |
| 133 |
% it |
| 134 |
% \end{lstlisting} |
| 135 |
% \end{examplecode} |
| 136 |
% \end{example} |
| 137 |
% |
| 138 |
% \begin{example} |
| 139 |
% \caption{Manual gobble option (override). Intentionally set to an incorrect value.} |
| 140 |
% \begin{examplecode} |
| 141 |
% \begin{lstlisting}[gobble=7] |
| 142 |
% test |
| 143 |
% it |
| 144 |
% \end{lstlisting} |
| 145 |
% \end{examplecode} |
| 146 |
% \end{example} |
| 147 |
% |
| 148 |
% \begin{example} |
| 149 |
% \caption{Locally turned-off \Key{autogobble}.} |
| 150 |
% \begin{examplecode} |
| 151 |
% \begin{lstlisting}[autogobble=false] |
| 152 |
% test |
| 153 |
% it |
| 154 |
% \end{lstlisting} |
| 155 |
% \end{examplecode} |
| 156 |
% \end{example} |
| 157 |
% |
| 158 |
% \begin{example} |
| 159 |
% \caption{With some material on the same line as \texttt{\string\begin} (dropped by listings. The warning message got preserved).} |
| 160 |
% \begin{examplecode} |
| 161 |
% \begin{lstlisting} some text at the first line |
| 162 |
% test |
| 163 |
% it |
| 164 |
% \end{lstlisting} |
| 165 |
% \end{examplecode} |
| 166 |
% \end{example} |
| 167 |
% |
| 168 |
% \begin{example} |
| 169 |
% \caption{As before, but with optional argument.} |
| 170 |
% \begin{examplecode} |
| 171 |
% \begin{lstlisting}[] some text at the first line |
| 172 |
% test |
| 173 |
% it |
| 174 |
% \end{lstlisting} |
| 175 |
% \end{examplecode} |
| 176 |
% \end{example} |
| 177 |
% |
| 178 |
% \begin{example} |
| 179 |
% \caption{Different indention levels.} |
| 180 |
% \begin{examplecode} |
| 181 |
% \begin{lstlisting} |
| 182 |
% test |
| 183 |
% it |
| 184 |
% \end{lstlisting} |
| 185 |
% \end{examplecode} |
| 186 |
% \begin{examplecode} |
| 187 |
% \begin{lstlisting} |
| 188 |
% test |
| 189 |
% it |
| 190 |
% \end{lstlisting} |
| 191 |
% \end{examplecode} |
| 192 |
% \begin{examplecode} |
| 193 |
% \begin{lstlisting} |
| 194 |
% test |
| 195 |
% it |
| 196 |
% \end{lstlisting} |
| 197 |
% \end{examplecode} |
| 198 |
% \begin{examplecode} |
| 199 |
% \begin{lstlisting} |
| 200 |
% test |
| 201 |
% it |
| 202 |
% \end{lstlisting} |
| 203 |
% \end{examplecode} |
| 204 |
% \begin{examplecode} |
| 205 |
% \begin{lstlisting} |
| 206 |
% test |
| 207 |
% it |
| 208 |
% \end{lstlisting} |
| 209 |
% \end{examplecode} |
| 210 |
% \end{example} |
| 211 |
% |
| 212 |
% \begin{example} |
| 213 |
% \caption{Some real C Code.} |
| 214 |
% \begin{examplecode} |
| 215 |
% \begin{lstlisting}[autogobble] |
| 216 |
% #include <stdio.h> |
| 217 |
% int main(){ |
| 218 |
% printf("tex.stackexchange.com: the coolest community ever!\n"); |
| 219 |
% } |
| 220 |
% \end{lstlisting} |
| 221 |
% \end{examplecode} |
| 222 |
% \end{example} |
| 223 |
% |
| 224 |
% \StopEventually{} |
| 225 |
% \clearpage |
| 226 |
% \section{Implementation} |
| 227 |
% |
| 228 |
% \iffalse |
| 229 |
%<*lstautogobble.sty> |
| 230 |
% \fi |
| 231 |
% \begin{macrocode} |
| 232 |
%<!COPYRIGHT> |
| 233 |
\ProvidesPackage{lstautogobble}[% |
| 234 |
%<!DATE> |
| 235 |
%<!VERSION> |
| 236 |
%<*DRIVER> |
| 237 |
2099/01/01 develop |
| 238 |
%</DRIVER> |
| 239 |
Implements 'autogobble' option for 'listings'] |
| 240 |
% \end{macrocode} |
| 241 |
% \begin{macrocode} |
| 242 |
|
| 243 |
% This is an add-on to the `listings` package |
| 244 |
\RequirePackage{listings} |
| 245 |
|
| 246 |
% Counter for leading spaces |
| 247 |
\newcount\lstag@spacecount |
| 248 |
|
| 249 |
% Some macros for comparison: |
| 250 |
\def\lstag@activespace{\lst@ProcessSpace}% Definition of an active space |
| 251 |
\def\lstag@tabulator{\lst@ProcessTabulator}% Definition of an tabulator |
| 252 |
|
| 253 |
\begingroup |
| 254 |
\catcode`\^^M=\active% |
| 255 |
\gdef\lstag@activenl{^^M}% Active CR (ASCII 13) character which is used as line break |
| 256 |
\endgroup |
| 257 |
|
| 258 |
|
| 259 |
% Define `autogobble` option as boolean (by default off) |
| 260 |
\lst@Key{autogobble}{false}[t]{\lstKV@SetIf{#1}\lst@ifautogobble} |
| 261 |
|
| 262 |
% `ungobble` option |
| 263 |
\lst@Key{ungobble}{0}{\def\lst@ungobble{#1}} |
| 264 |
|
| 265 |
% Insert required code at environment init |
| 266 |
\lst@AddToHook{Init}{\lst@autogobble} |
| 267 |
|
| 268 |
% Autogobble init macro. |
| 269 |
% If the option is active and `gobble` is not set, init vars and overwrite the process macro with own definition. |
| 270 |
\def\lst@autogobble{% |
| 271 |
\lst@ifautogobble |
| 272 |
\ifnum\lst@gobble>0\else |
| 273 |
\def\lst@gobble{\lstag@gobble}% |
| 274 |
\def\lstag@gobble{0}% |
| 275 |
\lstag@spacecount\z@ |
| 276 |
\def\lstag@spaceaccu{}% |
| 277 |
\let\lstag@restofline\empty |
| 278 |
\let\lstag@origlstenv@Process\lstenv@Process |
| 279 |
\let\lstenv@Process\lstag@countleadingspaces |
| 280 |
\fi |
| 281 |
\fi |
| 282 |
} |
| 283 |
|
| 284 |
% Checks if the next following character (read as argument) is a line break (as it is supposed to be) |
| 285 |
% Otherwise there is some text direct after the `\begin{<env>}[<options>]` which is dropped by `listings`. |
| 286 |
\def\lstag@countleadingspaces#1{% |
| 287 |
\expandafter\ifx\lstag@activenl#1\relax |
| 288 |
\expandafter\lstag@countleadingspaces@ |
| 289 |
\else |
| 290 |
\def\lstag@restofline{Dummy replacement of text after begin of listing to trigger original warning message}% |
| 291 |
\expandafter\lstag@countleadingspaces |
| 292 |
\fi |
| 293 |
} |
| 294 |
|
| 295 |
% After the new line is found this macro counts the spaces and tabulators |
| 296 |
\def\lstag@countleadingspaces@#1{% |
| 297 |
\ifx\lstag@activespace#1\relax |
| 298 |
\advance\lstag@spacecount by \@ne |
| 299 |
% Accumulate spaces (i.e. their definitions) for later re-insertion: |
| 300 |
\expandafter\def\expandafter\lstag@spaceaccu\expandafter{\lstag@spaceaccu\lst@ProcessSpace}% |
| 301 |
\let\next\lstag@countleadingspaces@ |
| 302 |
\else% Character wasn't a space |
| 303 |
\ifx\lstag@tabulator#1\relax |
| 304 |
\advance\lstag@spacecount by \lst@tabsize\relax |
| 305 |
% Accumulate spaces (i.e. their definitions) for later re-insertion: |
| 306 |
\@tempcnta=\lst@tabsize\relax |
| 307 |
\loop |
| 308 |
\ifnum\@tempcnta>\z@ |
| 309 |
\expandafter\def\expandafter\lstag@spaceaccu\expandafter{\lstag@spaceaccu\lst@ProcessSpace}% |
| 310 |
\advance\@tempcnta\m@ne |
| 311 |
\repeat |
| 312 |
\let\next\lstag@countleadingspaces@ |
| 313 |
\else% Character wasn't a tabulator either |
| 314 |
% Set gobble option (indirect): |
| 315 |
\xdef\lstag@gobble{\the\numexpr\lstag@spacecount-\lst@ungobble\relax}% |
| 316 |
% Restore original definition of process macro: |
| 317 |
\global\let\lstenv@Process\lstag@origlstenv@Process |
| 318 |
% Re-insert all collected material or appropriate replacement material: |
| 319 |
\edef\next{\noexpand\lstenv@Process\lstag@restofline\expandafter\noexpand\lstag@activenl\expandafter\unexpanded\expandafter{\lstag@spaceaccu}\noexpand#1}% |
| 320 |
\fi\fi |
| 321 |
\next |
| 322 |
} |
| 323 |
% \end{macrocode} |
| 324 |
% \iffalse |
| 325 |
%</lstautogobble.sty> |
| 326 |
% \fi |
| 327 |
% |
| 328 |
% \Finale |
| 329 |
\endinput |