summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lstaddons/lstautogobble.dtx
blob: f9d62cf8d3fda9d352f3e8860b4f49d6e3bf9ea6 (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
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
% \iffalse meta-comment
%<=*COPYRIGHT>
%% Copyright (C) 2011-2012 by Martin Scharrer <martin@scharrer-online.de>
%% -----------------------------------------------------------------------
%% 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 Martin Scharrer.
%%
%% This work consists of the files lstautogobble.dtx and lstaddons.ins
%% and the derived filebase lstautogobble.sty.
%%
%<=/COPYRIGHT>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{lstautogobble.dtx}[%
%<=*DATE>
    2012/05/03
%<=/DATE>
%<=*VERSION>
    v0.1
%<=/VERSION>
    DTX file for 'lstautogobble']
\documentclass{ydoc}
\GetFileInfo{lstautogobble.dtx}
\usepackage{lstautogobble}[\filedate]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.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}{2012/05/03}{Initial release.}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \GetFileInfo{lstautogobble.dtx}
% \author{Martin Scharrer}
% \email{martin@scharrer-online.de}
% \ifdefined\repository
%   \repository{https://bitbucket.org/martin_scharrer/lstautogobble}
% \fi
%
% \maketitle
%
% \begin{abstract}\noindent
% This add-on package to \pkg{listings} provides a boolean \Key{autogobble}
% setting which will automatically set the \Key{gobble} setting to indention
% of the first line.
%
% This package was created as response to the question
% \href{http://tex.stackexchange.com/q/19953/2975}{``How to automatically skip leading white spaces in listings?''}
% on \href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}.
% \end{abstract}
%
% \section{Introduction}
% The \pkg{listings} package has a setting \Key{gobble}'='<number> which allows to remove a certain number
% of characters from the beginning of every line in the listing. This can be used to indent the listing
% in the source code without affecting the printed result. However, this forces the user to set a suitable
% value manually. An incorrect value will lead either to an indented listing or to missing leading characters.
%
% A solution for this is to automatically detect the used indention of the listing and that the \Key{gobble} setting
% to this value. This functionality is provided by this package.
% For this it reads and scans the first listing line and reinserts it again afterwards.
%
%
% \section{Usage}
% After loading \pkg{lstautogobble} the following new \pkg{listings} setting
% is available:
%
% \DescribeKey{autogobble}'=true|false'
% This boolean setting switches the autogobble feature on or off.
% If no value is used the default is `|true|'. The initial setting is `|false|'.
% One enabled the first line of any \pkg{lstlisting} is scanned and the amount
% of spaces or tabulators is used to set the \Key{gobble} setting.
% If \Key{gobble} is set manually it will not be overwritten and \Key{autogobble}'=true'
% is ignored.
%
% \section{Examples / Tests}
% The following code is intended as examples and also for testing the package.
% Here the \Key{autogobble} feature is globally enabled.
%
% \lstdefinestyle{exampleresult}{basicstyle=\ttfamily,columns=fullflexible,autogobble=true}%
%
% \begin{example}
%   \caption{Only environment (with \Key{autogobble} enabled globally).}
%   \begin{examplecode}
%   \begin{lstlisting}
%     test
%     it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{With options (must be skipped and reinserted).}
%   \begin{examplecode}
%   \begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
%       test
%       it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{Manual gobble option (override). Intentionally set to an incorrect value.}
%   \begin{examplecode}
%   \begin{lstlisting}[gobble=7]
%         test
%         it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{Locally turned-off \Key{autogobble}.}
%   \begin{examplecode}
%   \begin{lstlisting}[autogobble=false]
%       test
%       it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{With some material on the same line as \texttt{\string\begin} (dropped by listings. The warning message got preserved).}
%   \begin{examplecode}
%   \begin{lstlisting} some text at the first line
%       test
%       it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{As before, but with optional argument.}
%   \begin{examplecode}
%   \begin{lstlisting}[] some text at the first line
%       test
%       it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{Different indention levels.}
%   \begin{examplecode}
%   \begin{lstlisting}
%       test
%       it
%   \end{lstlisting}
%   \end{examplecode}
%   \begin{examplecode}
%       \begin{lstlisting}
%           test
%           it
%       \end{lstlisting}
%   \end{examplecode}
%   \begin{examplecode}
%       \begin{lstlisting}
%   test
%   it
%       \end{lstlisting}
%   \end{examplecode}
%   \begin{examplecode}
%           \begin{lstlisting}
%                           test
%                           it
%           \end{lstlisting}
%   \end{examplecode}
%   \begin{examplecode}
%   \begin{lstlisting}
%                   test
%                   it
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{Some real C Code.}
%   \begin{examplecode}
%   \begin{lstlisting}[autogobble]
%      #include <stdio.h>
%      int main(){
%        printf("tex.stackexchange.com: the coolest community ever!\n");
%      }
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \StopEventually{}
% \clearpage
% \section{Implementation}
%
% \iffalse
%<*lstautogobble.sty>
% \fi
%    \begin{macrocode}
%<!COPYRIGHT>
\ProvidesPackage{lstautogobble}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
    2099/01/01 develop
%</DRIVER>
    Implements 'autogobble' option for 'listings']
%    \end{macrocode}
%    \begin{macrocode}

% This is an add-on to the `listings` package
\RequirePackage{listings}

% Counter for leading spaces
\newcount\lstag@spacecount

% Some macros for comparison:
\def\lstag@activespace{\lst@ProcessSpace}%  Definition of an active space
\def\lstag@tabulator{\lst@ProcessTabulator}%  Definition of an tabulator

\begingroup
\catcode`\^^M=\active%
\gdef\lstag@activenl{^^M}%  Active CR (ASCII 13) character which is used as line break
\endgroup


% Define `autogobble` option as boolean (by default off)
\lst@Key{autogobble}{false}[t]{\lstKV@SetIf{#1}\lst@ifautogobble}

% `ungobble` option
\lst@Key{ungobble}{0}{\def\lst@ungobble{#1}}

% Insert required code at environment init
\lst@AddToHook{Init}{\lst@autogobble}

% Autogobble init macro.
% If the option is active and `gobble` is not set, init vars and overwrite the process macro with own definition.
\def\lst@autogobble{%
    \lst@ifautogobble
        \ifnum\lst@gobble>0\else
            \def\lst@gobble{\lstag@gobble}%
            \def\lstag@gobble{0}%
            \lstag@spacecount\z@
            \def\lstag@spaceaccu{}%
            \let\lstag@restofline\empty
            \let\lstag@origlstenv@Process\lstenv@Process
            \let\lstenv@Process\lstag@countleadingspaces
        \fi
    \fi
}

% Checks if the next following character (read as argument) is a line break (as it is supposed to be)
% Otherwise there is some text direct after the `\begin{<env>}[<options>]` which is dropped by `listings`.
\def\lstag@countleadingspaces#1{%
    \expandafter\ifx\lstag@activenl#1\relax
        \expandafter\lstag@countleadingspaces@
    \else
        \def\lstag@restofline{Dummy replacement of text after begin of listing to trigger original warning message}%
        \expandafter\lstag@countleadingspaces
    \fi
}

% After the new line is found this macro counts the spaces and tabulators
\def\lstag@countleadingspaces@#1{%
    \ifx\lstag@activespace#1\relax
        \advance\lstag@spacecount by \@ne
        % Accumulate spaces (i.e. their definitions) for later re-insertion:
        \expandafter\def\expandafter\lstag@spaceaccu\expandafter{\lstag@spaceaccu\lst@ProcessSpace}%
        \let\next\lstag@countleadingspaces@
    \else% Character wasn't a space
    \ifx\lstag@tabulator#1\relax
        \advance\lstag@spacecount by \lst@tabsize\relax
        % Accumulate spaces (i.e. their definitions) for later re-insertion:
        \@tempcnta=\lst@tabsize\relax
        \loop
        \ifnum\@tempcnta>\z@
            \expandafter\def\expandafter\lstag@spaceaccu\expandafter{\lstag@spaceaccu\lst@ProcessSpace}%
            \advance\@tempcnta\m@ne
        \repeat
        \let\next\lstag@countleadingspaces@
    \else% Character wasn't a tabulator either
        % Set gobble option (indirect):
        \xdef\lstag@gobble{\the\numexpr\lstag@spacecount-\lst@ungobble\relax}%
        % Restore original definition of process macro:
        \global\let\lstenv@Process\lstag@origlstenv@Process
        % Re-insert all collected material or appropriate replacement material:
        \edef\next{\noexpand\lstenv@Process\lstag@restofline\expandafter\noexpand\lstag@activenl\expandafter\unexpanded\expandafter{\lstag@spaceaccu}\noexpand#1}%
    \fi\fi
    \next
}
%    \end{macrocode}
% \iffalse
%</lstautogobble.sty>
% \fi
%
% \Finale
\endinput