-
Notifications
You must be signed in to change notification settings - Fork 1
/
plistings.sty
347 lines (320 loc) · 11.3 KB
/
plistings.sty
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
%
% plistings.sty
%
% lltjp-listings.sty ベース,コード未整理
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{plistings}[2019/03/31 v0.12 Japanese support of listings package]
%%%%%%%% Package options
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{listings}}
\ProcessOptions\relax
\RequirePackage{listings,etoolbox}
%%%%%%%% Japanese support
%% whether letter-space in a fixed mode box is doubled or not
\newif\if@ltj@lst@double
\lst@Key{doubleletterspace}f[t]{\lstKV@SetIf{#1}\if@ltj@lst@double}
% override \lst@FillFixed@
\def\lst@FillFixed@#1{%
\ifx\@empty#1\else\ltj@lst@hss#1\expandafter\lst@FillFixed@\fi}
\def\ltj@lst@hss@double{\lst@hss\lst@hss}
% 最下層の処理
\newif\if@ltj@lst@kanji
\lst@AddToHook{InitVars}{\@ltj@lst@kanjifalse}
\def\lst@AppendLetter{%
\ltj@lst@setletterflag\lst@Append}
\def\lst@AppendOther{%
\lst@ifletter\lst@Output\lst@letterfalse\fi\@ltj@lst@kanjifalse
\futurelet\lst@lastother\lst@Append}
\def\ltj@lst@setletterflag{%
\lst@ifletter
\if@ltj@lst@kanji\lst@Output\@ltj@lst@kanjifalse\fi
\else
\lst@lettertrue\if@ltj@lst@kanji\@ltj@lst@kanjifalse\else\lst@OutputOther\fi
\fi}
\def\ltj@lst@setkanjiflag{%
\lst@ifletter
\lst@Output
\else
\if@ltj@lst@kanji\else\lst@OutputOther\fi\lst@lettertrue
\fi\@ltj@lst@kanjitrue}
\def\ltj@lst@setopenflag{%
\lst@ifletter
\lst@letterfalse\lst@Output
\else
\if@ltj@lst@kanji\else\lst@OutputOther\fi
\fi\@ltj@lst@kanjitrue}
\def\ltj@lst@setcloseflag{%
\lst@ifletter\else\lst@lettertrue\fi\@ltj@lst@kanjitrue}
%%%% 和文文字の出力命令.
%%%% 和文文字の前にこれが前置されることになる.
\def\ltj@lst@ProcessJALetter#1{%
\lst@whitespacefalse
\ifnum`#1>255
\ifnum\postbreakpenalty`#1>0
\ltj@lst@setopenflag % 開き括弧類
\else
\ifnum\prebreakpenalty`#1>0
\ltj@lst@setcloseflag % 閉じ括弧類,句読点
\else
\ltj@lst@setkanjiflag % 通常の和文文字
\fi\fi
\advance\lst@length\@ne % 和文文字は通常の2倍の幅
\else
\ltj@lst@setletterflag
\fi
\lst@Append#1}
%%%% \lst@InsideConvert の処理内容変更
%%%% active 文字化に加え,^^@ を和文文字の前に前置
\def\ltj@lst@MakeActive#1{%
\let\lst@temp\@empty \ltj@lst@MakeActive@#1\relax}
\begingroup
\catcode`\^^A=\active
\catcode`\^^@=\active
\lowercase{%
\gdef\ltj@lst@MakeActive@#1{\let\lst@next\relax%
\ifx#1\relax
\else\let\lst@next\ltj@lst@MakeActive@
\ifnum`#1>255
\lst@lAddTo\lst@temp{^^@#1}%
\else
\lccode`\^^A=`#1
\lowercase{\lst@lAddTo\lst@temp{^^A}}%
\fi\fi\lst@next}}
\endgroup
\begingroup \lccode`\~=`\ \relax \lowercase{%
\gdef\lst@InsideConvert@#1 #2{%
\ltj@lst@MakeActive{#1}%
\ifx\@empty#2%
\lst@lExtend\lst@arg{\lst@temp}%
\else
\lst@lExtend\lst@arg{\lst@temp~}%
\expandafter\lst@InsideConvert@
\fi #2}
}\endgroup
%%%%%%%% \lstinline の再定義.
%%%% 引数を全部読み込み,\lst@InsideConvert で変換
\renewcommand\lstinline[1][]{%
\leavevmode\bgroup % \hbox\bgroup --> \bgroup
\def\lst@boxpos{b}%
\lsthk@PreSet\lstset{flexiblecolumns,#1}%
\lsthk@TextStyle
\@ifnextchar\bgroup \ltj@lst@InlineG \ltj@lstinline@}
\def\ltj@lstinline@#1{%
\edef\ltj@lst@temp{\the\catcode`#1}\lst@Init\relax\catcode`#1\ltj@lst@temp
\lst@Def{13}{\lst@DeInit\egroup \global\let\lst@inlinechars\@empty
\PackageError{Listings}{lstinline ended by EOL}\@ehc}%
\lst@InlineJ#1}
\def\ltj@lst@InlineG{%
\lst@Init\relax\edef\ltj@lst@temp{\the\catcode`\}}%
\catcode`\}=2 \catcode`\ =12\relax
\lst@Def{13}{\lst@DeInit\egroup \global\let\lst@inlinechars\@empty
\PackageError{Listings}{lstinline ended by EOL}\@ehc}%
\let\lst@arg\@empty\afterassignment\ltj@lst@InlineG@@\@temptokena}
\def\ltj@lst@InlineG@@{%
\catcode`\}=\ltj@lst@temp
\expandafter\expandafter\expandafter\lst@InsideConvert%
\expandafter{\the\@temptokena}\lst@arg\lst@DeInit\egroup}
%%%%%%%% \lstenv@process の再定義
%%%% 基本的にはインライン時と同様に全トークンを読み込み→\lst@InsideConvert で変換
%%%% その後,変換した中身は \scantokens で読み込み直される
\begingroup \lccode`\~=`\^^M\lowercase{%
\gdef\lstenv@Process#1{%
\ifx~#1%
\lstenv@DroppedWarning \let\lst@next\ltj@lstenv@ProcessM
\else\ifx^^J#1%
\lstenv@DroppedWarning \let\lst@next\lstenv@ProcessJ
\else
\let\lst@dropped#1\let\lst@next\lstenv@Process
\fi \fi
\lst@next}
}\endgroup
\begingroup\lccode`\[=`\{\lccode`\]=`\}\lccode`|=`\\\lowercase{%
\gdef\ltj@lstenv@ProcessM{%
\let\lst@arg\@empty
\edef\lst@temp{|end[\lstenv@name]}%
\expandafter\expandafter\expandafter\lst@InsideConvert%
\expandafter{\lst@temp}%
\@temptokena{%
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter
\lst@SkipToFirst\expandafter\expandafter\expandafter\scantokens\expandafter{\lst@arg}%
}
\expandafter\expandafter\expandafter\toks@\expandafter{\lst@arg}
\expandafter\edef\expandafter\lst@temp\expandafter##\expandafter1\lst@arg
{%
\noexpand\let\noexpand\lst@arg\noexpand\@empty%
\noexpand\lst@InsideConvert{\noexpand##1}
\the\@temptokena
\noexpand\ifnum\noexpand\catcode92=\noexpand\z@
\noexpand\end{\lstenv@name}%
\noexpand\else\the\toks@\noexpand\fi\tracingnone
}%
\lst@temp}
}\endgroup
\def\lst@BeginDropInput#1{%
\lst@EnterMode{#1}%
{\lst@modetrue
\let\lst@OutputBox\@gobble
\let\lst@ifdropinput\iftrue
\let\lst@ProcessLetter\@gobble
\let\lst@ProcessDigit\@gobble
\let\lst@ProcessOther\@gobble
\let\lst@ProcessSpace\@empty
\let\lst@ProcessTabulator\@empty
\let\lst@ProcessFormFeed\@empty
\let\ltj@lst@ProcessJALetter\@gobble % added
}}
%%%% ^^@ を active 文字化 (\ltj@lst@ProcessJALetter)
\begingroup\catcode`\^^@=\active
\lst@AddTo\lst@SelectStdCharTable{\def^^@{\ltj@lst@ProcessJALetter}}
\endgroup
\lst@AddToHook{InitVars}{%
\catcode`\^^@=\active
\if@ltj@lst@double
\let\ltj@lst@hss=\ltj@lst@hss@double
\else
\let\ltj@lst@hss=\lst@hss
\fi
}
%%%% 白線対策のため,\lineskiplimit を負の値にする
\newif\ifltj@lst@frame@top
\newdimen\ltj@lst@frame@lslimit
\gdef\lst@frameInit{%
\ltj@lst@frame@toptrue
\ifx\lst@framelshape\@empty \let\lst@frameL\@empty \fi
\ifx\lst@framershape\@empty \let\lst@frameR\@empty \fi
\def\lst@framevrule{\vrule\@width\lst@framerulewidth\relax}%
\lst@ifframeround
\lst@frameCalcDimA\z@ \@getcirc\@tempdima
\@tempdimb\@tempdima \divide\@tempdimb\tw@
\advance\@tempdimb -\@wholewidth
\edef\lst@frametextsep{\the\@tempdimb}%
\edef\lst@framerulewidth{\the\@wholewidth}%
\lst@frameCalcDimA\@ne \@getcirc\@tempdima
\@tempdimb\@tempdima \divide\@tempdimb\tw@
\advance\@tempdimb -\tw@\@wholewidth
\advance\@tempdimb -\lst@frametextsep
\edef\lst@rulesep{\the\@tempdimb}%
\fi
\lst@frameMakeBoxV\lst@framebox{\ht\strutbox}{\dp\strutbox}%
%%%% ここから
\@tempdima\z@
\ifdim\ht\strutbox<\cht\@tempdima=\dimexpr\cht-\ht\strutbox\relax\fi
\ifdim\dp\strutbox<\cdp\advance\@tempdima=\dimexpr\cdp-\dp\strutbox\relax\fi
\ltj@lst@frame@lslimit=-\@tempdima
\def\lst@framelr{%
\ifltj@lst@frame@top\ltj@lst@frame@topfalse\else\lineskiplimit\ltj@lst@frame@lslimit\fi
\copy\lst@framebox}%
%%%% ここまで
\ifx\lst@frametshape\@empty\else
\lst@frameH T\lst@frametshape
\ifvoid\z@\else
\par\lst@parshape
\@tempdima-\baselineskip \advance\@tempdima\ht\z@
\ifdim\prevdepth<\@cclvi\p@\else
\advance\@tempdima\prevdepth
\fi
\ifdim\@tempdima<\z@
\vskip\@tempdima\vskip\lineskip
\fi
\noindent\box\z@\par
\lineskiplimit\maxdimen \lineskip\z@
\fi
\lst@frameSpreadV\lst@framextopmargin
\fi}
% lstinputlisting
% modified from jlisting.sty
\def\lst@InputListing#1{%
\begingroup
\lsthk@PreSet \gdef\lst@intname{#1}%
\expandafter\lstset\expandafter{\lst@set}%
\lsthk@DisplayStyle
\catcode\active=\active
\lst@Init\relax \let\lst@gobble\z@
\lst@SkipToFirst
\lst@ifprint \def\lst@next{\lst@get@filecontents{#1}}%
\else \let\lst@next\@empty
\fi
\lst@next
\lst@DeInit
\endgroup}
\newread\lst@inputfile
\def\lst@get@filecontents#1{%
\let\lst@filecontents\@empty
\openin\lst@inputfile=#1\relax
\let\@lst@get@filecontents@prevline\relax
\lst@get@filecontents@loop
\closein\lst@inputfile
\lst@filecontents\empty}
\def\lst@get@filecontents@loop{%
\read\lst@inputfile to\lst@temp
\let\lst@arg\@empty\expandafter\expandafter\expandafter\lst@InsideConvert\expandafter{\lst@temp}%
\ifx\@lst@get@filecontents@prevline\relax\else
\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\lst@filecontents
\expandafter\expandafter\expandafter{%
\expandafter\lst@filecontents\@lst@get@filecontents@prevline}%
\fi
\let\@lst@get@filecontents@prevline\lst@arg
\ifeof\lst@inputfile\else
\expandafter\lst@get@filecontents@loop
\fi}
%%%%%%%% escape to \LaTeX
%%%% 一旦中身を全部取得した後で,^^@ ( = \ltj@lst@ProcessJALetter) を
%%%% トークン列から削除,その後 \scantokens で再読み込み
\lstloadaspects{escape}
\gdef\lst@Escape#1#2#3#4{%
\lst@CArgX #1\relax\lst@CDefX
{}%
{\lst@ifdropinput\else
\lst@TrackNewLines\lst@OutputLostSpace \lst@XPrintToken
\lst@InterruptModes
\lst@EnterMode{\lst@TeXmode}{\lst@modetrue}%
\ifx\^^M#2%
\lst@CArg #2\relax\lst@ActiveCDefX
{}%
{\lst@escapeend #4\lst@LeaveAllModes\lst@ReenterModes}%
{\ltj@lst@MProcessListing}%
\else
\lst@CArg #2\relax\lst@ActiveCDefX
{}%
{\lst@escapeend #4\lst@LeaveAllModes\lst@ReenterModes
\lst@newlines\z@ \lst@whitespacefalse}%
{}%
\fi%
\ltj@lst@escape@setup#2%
#3\lst@escapebegin\expandafter\lst@next%
\fi}%
{}}
\gdef\ltj@empty@grp{{}}
\gdef\ltj@lst@escape@setup#1{%
\begingroup\lccode`\~=`#1\lowercase{%
\gdef\lst@next##1~{%
\let\lst@arg\@empty\ltj@lst@remove@jacmd{##1}%
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter
\scantokens\expandafter\expandafter\expandafter{\expandafter\ltj@empty@grp\lst@arg\empty}%
~}%
}\endgroup
}
\begingroup
\catcode`\^^@=12 %
\gdef\ltj@lst@remove@jacmd#1{%
\expandafter\ltj@lst@remove@jacmd@\detokenize{#1}^^@\@nil^^@}
\gdef\ltj@lst@remove@jacmd@#1^^@{%
\ifx#1\@nil\else
\lst@lAddTo\lst@arg{#1}%
\expandafter\ltj@lst@remove@jacmd@
\fi}
\endgroup
%%%%%%%%%%%%%%%% texcl
{\lccode`\~=`\%\lccode`\|=`\^^M \catcode`\|=13\relax
\lowercase{%
\gdef\plst@texcl@comment#1|{}%
\gdef\plst@texcl@setchar{\catcode`\%=\active \let~\plst@texcl@comment}%
}%
}
\lst@AddToHook{AfterBeginComment}{
\ifnum\lst@mode=\lst@TeXLmode
\catcode`\^^M=13\relax \catcode`\^^@=9\relax\plst@texcl@setchar
\fi}
%%%%%%%%%%%%%%%% \lstMakeShortInline
\patchcmd\lstMakeShortInline@{\lst@shortinlinedef#1}{\lst@shortinlinedef~}{}{}
\endinput