Merge pull request #102 from sebix/ignoretmp
[ach-master.git] / src / common / lstautounindent.tex
1 %
2 %
3 % https://tex.stackexchange.com/questions/151208/how-to-remove-the-leading-unnecessary-white-spaces-when-using-linerange-option/164610#164610
4
5 \makeatletter
6
7 % -------- ugly details --------
8
9 % custom key
10 \lst@Key{autounindent}f[t]{\lstKV@SetIf{#1}\lst@ifautounindent}
11
12 % --- accumulation of tokens ---
13 \toks@={
14     \lst@DefSaveDef{`\ }\lsts@myspace{\processmyspace}
15     %\lst@DefSaveDef{`\^^I}\lsts@mytab{\processmytab}
16 }
17
18
19 % --- egreg's helper function for accumulating tokens ---
20 \def\add@savedef#1#2{%
21   \begingroup\lccode`?=#1\relax
22   \lowercase{\endgroup
23   \edef\@temp{%
24     \noexpand\lst@DefSaveDef{\number#1}%
25     \expandafter\noexpand\csname lsts@?\endcsname{\noexpand#2% % I had to swap things around to fix a bug here;
26       \expandafter\noexpand\csname lsts@?\endcsname}%          % otherwise, comment highlighting would be broken.
27   }}%
28   \toks@=\expandafter{\the\expandafter\toks@\@temp}%
29 }
30
31 \count@=33
32 \loop
33     \add@savedef\count@\processchar
34   \ifnum\count@<127
35   \advance\count@\@ne
36 \repeat
37
38 % --- to keep track of the state ---
39 \newcount\spacesToGobble
40 \newcount\spacesGobbledSoFar
41 \newif\ifafterindent
42 \newif\ifonBasisLine
43 \newif\ifPostponeCountToNextLine
44
45
46 \lst@AddToHook{Init}
47 {%
48   \lst@ifautounindent%
49     % patch hook macros
50     \let\@ddedToInitVarsBOLhook\@@ddedToInitVarsBOLhook%
51     \let\@ddedToEOLhook\@@ddedToEOLhook%
52     % initialise things
53     \global\onBasisLinetrue%
54     \global\afterindentfalse%
55     \global\spacesToGobble=0%
56     \lst@ifincluderangemarker%
57       \ifnum9999999=\lst@firstline% (i.e. if firstline option not used)
58         \global\PostponeCountToNextLinetrue%
59       \fi
60     \fi
61   \fi
62 }
63
64
65 \lst@AddToHook{SelectCharTable}
66 {%
67     \lst@ifautounindent%
68         \lst@lAddTo\lst@DeveloperSCT{\the\toks@}%
69     \fi
70 }
71
72
73 \lst@AddToHook{InitVarsBOL}{\@ddedToInitVarsBOLhook}
74 \newcommand\@ddedToInitVarsBOLhook{}
75 \newcommand\@@ddedToInitVarsBOLhook
76 {%
77   \global\afterindentfalse%
78 }
79
80 \lst@AddToHook{EOL}{\@ddedToEOLhook}
81 \newcommand\@ddedToEOLhook{}
82 \newcommand\@@ddedToEOLhook
83 {%
84   \ifonBasisLine%
85     \ifPostponeCountToNextLine%
86     \else
87       \global\onBasisLinefalse%
88     \fi
89   \fi
90   \global\spacesGobbledSoFar=0%
91   \global\PostponeCountToNextLinefalse%
92 }
93
94 \lst@AddToHook{DeInit}
95 {%
96   \lst@ifautounindent%
97     % undo patches
98     \def\@ddedToInitVarsBOLhook{}%
99     \def\@ddedToEOLhook{}%
100   \fi
101 }
102
103
104 % --- helper macros ---
105 \newcommand\processchar
106 {%
107   \global\afterindenttrue%
108 }
109
110 \newcommand\processmyspace
111 {%
112   \ifafterindent%
113     \lsts@myspace%
114   \else
115     \ifonBasisLine%
116       \global\advance\spacesToGobble\@ne\relax%
117     \else
118       \ifnum\spacesGobbledSoFar<\spacesToGobble\relax%
119         \global\advance\spacesGobbledSoFar\@ne\relax%
120       \else
121         \lsts@myspace%
122       \fi
123     \fi
124   \fi
125 }
126
127 \makeatother