5 \NeedsTeXFormat{LaTeX2e}
6 \ProvidesPackage{lltjp-listings}[2015/01/07 Patch to listings for LuaTeX-ja]
7 \RequirePackage{listings,luatexbase-cctb}
9 %%%%%%%%%%%%%%%% Japanese support
10 %% whether letter-space in a fixed mode box is doubled or not
11 \newif\if@ltj@lst@double
12 \lst@Key{doubleletterspace}f[t]{\lstKV@SetIf{#1}\if@ltj@lst@double}
15 \newif\if@ltj@lst@vsraw
17 \lst@Key{vsraw}f[t]{\lstKV@SetIf{#1}\if@ltj@lst@vsraw}
18 \lst@Key{vscmd}\relax{\def\ltj@lst@vscmd{#1}}
21 \def\ltjlistingsvsstdcmd#1{\@tempdima=\f@size pt%
22 \smash{\raisebox{.35\@tempdima}{\tt%
23 \fboxsep=.1\@tempdima\fbox{\fontsize{.5\@tempdima}{\z@}\selectfont
24 \oalign{\hss VS\hss\crcr\hss#1\hss\crcr}}}}}
25 \def\ltj@lst@vscmd{\ltjlistingsvsstdcmd}
27 % override \lst@FillFixed@
28 % \ltj@hst@hss is \ltj@lst@hss@ivs or \ltj@lst@hss@normal,
29 % according to vsraw = true or not.
30 \def\lst@FillFixed@#1{%
31 \ifx\@empty#1\else\ltj@lst@hss#1\expandafter\lst@FillFixed@\fi}
34 local cat_str = luatexbase.catcodetables['string']
35 local cat_lp = luatexbase.catcodetables['latex-package']
36 local ubyte = unicode.utf8.byte
37 local spccmd = string.char(0x5C) .. 'ltj@lst@hss@normal'
39 function luatexja.lst.althss(t)
41 if not (t>=0xE0100 and t<0xE01F0) then
42 tex.sprint(cat_lp, spccmd)
46 \def\ltj@lst@hss@ivs#1{%
47 \directlua{luatexja.lst.althss('\luatexluaescapestring{#1}')}#1%
49 \def\ltj@lst@hss@double{\lst@hss\lst@hss}
52 \newif\if@ltj@lst@kanji
53 \lst@AddToHook{InitVars}{\@ltj@lst@kanjifalse}
55 \def\lst@AppendLetter{%
56 \ltj@lst@setletterflag\lst@Append}
57 \def\lst@AppendOther{%
58 \lst@ifletter\lst@Output\lst@letterfalse\fi\@ltj@lst@kanjifalse
59 \futurelet\lst@lastother\lst@Append}
61 \def\ltj@lst@setletterflag{%
63 \if@ltj@lst@kanji\lst@Output\@ltj@lst@kanjifalse\fi
65 \lst@lettertrue\if@ltj@lst@kanji\@ltj@lst@kanjifalse\else\lst@OutputOther\fi
68 \def\ltj@lst@setkanjiflag{%
72 \if@ltj@lst@kanji\else\lst@OutputOther\fi\lst@lettertrue
73 \fi\@ltj@lst@kanjitrue}
75 \def\ltj@lst@setopenflag{%
77 \lst@letterfalse\lst@Output
79 \if@ltj@lst@kanji\else\lst@OutputOther\fi
80 \fi\@ltj@lst@kanjitrue}
82 \def\ltj@lst@setcloseflag{%
83 \lst@ifletter\else\lst@lettertrue\fi\@ltj@lst@kanjitrue}
85 % Processing Japanese characters
86 \def\ltj@lst@ProcessJALetter#1{%
88 \ifnum\ltjgetparameter{jacharrange}{\ltjgetparameter{chartorange}{`#1}}=0
89 \ifnum\ltjgetparameter{postbreakpenalty}{`#1}>0
90 \ltj@lst@setopenflag % 開き括弧類
92 \ifnum\ltjgetparameter{prebreakpenalty}{`#1}>0
93 \ltj@lst@setcloseflag % 閉じ括弧類,句読点
95 \ltj@lst@setkanjiflag % 通常の和文文字
97 \advance\lst@length\@ne % 和文文字は通常の2倍の幅
99 \ltj@lst@setletterflag
104 \def\ltj@lst@ProcessJALetterHalf#1{%
106 \ifnum\ltjgetparameter{jacharrange}{\ltjgetparameter{chartorange}{`#1}}=0
107 \ifnum\ltjgetparameter{postbreakpenalty}{`#1}>0
108 \ltj@lst@setopenflag % 開き括弧類
110 \ifnum\ltjgetparameter{prebreakpenalty}{`#1}>0
111 \ltj@lst@setcloseflag % 閉じ括弧類
113 \ltj@lst@setkanjiflag % 通常の和文文字
117 \ltj@lst@setletterflag
122 \def\ltj@lst@ProcessIVS#1{%
125 \lst@Append#1\advance\lst@length\m@ne
127 \lst@TrackNewLines\lst@OutputLostSpace \lst@PrintToken
128 \setbox\@tempboxa\hbox to 2\lst@width{\hss
129 \expandafter\expandafter\expandafter\ltj@lst@vscmd
130 \expandafter{\the\numexpr`#1-"E00EF\relax}%"
132 \lst@CalcLostSpaceAndOutput\lst@whitespacefalse
137 \def\ltj@lst@ProcessVS#1{%
140 \lst@Append#1\advance\lst@length\m@ne
142 \lst@TrackNewLines\lst@OutputLostSpace \lst@PrintToken
143 \setbox\@tempboxa\hbox to 2\lst@width{\hss
144 \expandafter\expandafter\expandafter\ltj@lst@vscmd
145 \expandafter{\the\numexpr`#1-"FDFF\relax}%"
147 \lst@CalcLostSpaceAndOutput\lst@whitespacefalse
152 % 半角カナ,異体字セレクタはアクティブ化
153 \def\ltj@@listing@jpsetN#1#2#3{% for (not large) range
154 \@tempcnta=\numexpr#1-1\relax\@tempcntb=\numexpr 1+#2\relax
155 \loop \global\advance\@tempcnta\@ne\ifnum\@tempcnta<\@tempcntb\relax
156 \ltj@@listing@jpsetN@{\@tempcnta}{#3}\repeat
159 \def\ltj@@listing@jpsetN@#1#2{{%
160 \@tempcnta=#1 \lccode`\~=\@tempcnta \lccode`\/=\@tempcnta
161 \lowercase{\gdef\@temp{\gdef~{#2/}}}}%
162 \@temptokena\expandafter\expandafter\expandafter\expandafter%
163 \expandafter\expandafter\expandafter{\expandafter\expandafter%
164 \expandafter\the\expandafter\@temptokena\@temp}%
167 \ltj@@listing@jpsetN{65377}{65439}{\ltj@lst@ProcessJALetterHalf}
168 \ltj@@listing@jpsetN{65024}{65039}{\ltj@lst@ProcessVS}
169 \ltj@@listing@jpsetN{917760}{917999}{\ltj@lst@ProcessIVS}
170 \xdef\ltj@@listing@jpcmd{\the\@temptokena}\@temptokena{}
173 \newluatexcatcodetable\CatcodeTableLTJlistings
174 \setluatexcatcodetable\CatcodeTableLTJlistings{%
175 \luatexcatcodetable\CatcodeTableLaTeXAtLetter
177 \SetCatcodeRange{"FF61}{"FF9F}{13}% 半角カナ
178 \SetCatcodeRange{"E0100}{"E01EF}{13}% 漢字用異体字セレクタ
179 \SetCatcodeRange{"FE00}{"FE0F}{13}% Variation Selector
182 % redefine \lstinline@ and \lst@InlineG
183 % because \lstinline!あ...! causes ``Runaway argument?'' Error
186 \ltj@lst@check@inline{\lst@InlineM#1}{\lst@InlineJ#1}}
189 \ltj@lst@check@inline{\lst@InlineM\}}%
190 {\let\lst@arg\@empty \lst@InlineGJ}}
192 \def\ltj@lst@check@inline#1#2#3{%
193 \begingroup \lccode`\~=`#3\lowercase{\endgroup
196 \else\ifnum`#3>127\relax
202 % We redefine \lst@BeginDropInput, since now we have
203 % two additional `process macros'.
204 \def\lst@BeginDropInput#1{%
207 \let\lst@OutputBox\@gobble
208 \let\lst@ifdropinput\iftrue
209 \let\lst@ProcessLetter\@gobble
210 \let\lst@ProcessDigit\@gobble
211 \let\lst@ProcessOther\@gobble
212 \let\lst@ProcessSpace\@empty
213 \let\lst@ProcessTabulator\@empty
214 \let\lst@ProcessFormFeed\@empty
215 \let\ltj@lst@ProcessJALetter\@gobble % added
216 \let\ltj@lst@ProcessJALetterHalf\@gobble % added
221 \lst@AddToHook{Init}{%
222 \luatexcatcodetable\CatcodeTableLTJlistings\ltj@@listing@jpcmd
223 \lccode`\~="FFFFF\lowercase{\def~{\ltj@lst@ProcessJALetter}}%"
224 \directlua{luatexbase.add_to_callback('process_input_buffer',
227 for i = 1, utf.len(buf) do
228 local c = utf.sub(buf, i, i)
229 local cu = utf.byte(c)
230 if cu >= 0x80 and tex.getcatcode(cu) \string~= 13 then
231 ret = ret .. utf.char(1048575) % U+FFFFF
236 end, 'ltj.listings_unicode', 1)}%
238 \let\ltj@lst@hss@normal=\ltj@lst@hss@double
240 \let\ltj@lst@hss@normal=\lst@hss
243 \let\ltj@lst@hss=\ltj@lst@hss@ivs
245 \let\ltj@lst@hss=\ltj@lst@hss@normal
248 \lst@AddToHook{ExitVars}{%
249 \directlua{luatexbase.remove_from_callback('process_input_buffer',
250 'ltj.listings_unicode')}%
255 \def\lst@OutputToken{%
256 \lst@TrackNewLines \lst@OutputLostSpace
258 \lst@gobbledwhitespacefalse
262 {\lst@thestyle{\lst@FontAdjust
263 \setbox\@tempboxa\lst@hbox
266 \expandafter\lst@FillOutputBox\the\lst@token\@empty
268 \ht\@tempboxa=0pt\dp\@tempboxa=0pt
269 \lst@CalcLostSpaceAndOutput}}%
272 %%%%%%%%%%%%%%%% escape to \LaTeX
273 \lstloadaspects{escape}
274 \gdef\lst@Escape#1#2#3#4{%
275 \lst@CArgX #1\relax\lst@CDefX
277 {\lst@ifdropinput\else
278 \lst@TrackNewLines\lst@OutputLostSpace \lst@XPrintToken
280 \lst@EnterMode{\lst@TeXmode}{\lst@modetrue}%
282 \lst@CArg #2\relax\lst@ActiveCDefX
284 {\lst@escapeend #4\lst@LeaveAllModes\lst@ReenterModes}%
285 {\lst@MProcessListing}%
287 \lst@CArg #2\relax\lst@ActiveCDefX
289 {\lst@escapeend #4\lst@LeaveAllModes\lst@ReenterModes
290 \lst@newlines\z@ \lst@whitespacefalse}%
293 #3\catcode"FFFFF=9\lst@escapebegin%"
298 \lstloadaspects{writefile}
299 \begingroup \catcode`\^^I=11
300 \gdef\lst@WFBegin#1#2{%
302 \let\lst@OutputBox#1%
304 \advance\lst@length\@ne
305 \expandafter\lst@token\expandafter{\the\lst@token##1}%
306 \ifx ##1\lst@outputspace \else
309 \lst@lAddTo\lst@PreGotoTabStop{\lst@WFAppend{^^I}}%
310 \lst@lAddTo\lst@ProcessSpace{\lst@WFAppend{ }}%
311 \def\ltj@lst@ProcessIVS##1{\lst@whitespacefalse\lst@Append##1}%
312 \def\ltj@lst@ProcessVS##1{\lst@whitespacefalse\lst@Append##1}%
313 \let\lst@DeInit\lst@WFDeInit
314 \let\lst@MProcessListing\lst@WFMProcessListing
316 \immediate\openout\lst@WF=#2\relax
317 \global\let\lst@WFifopen\iftrue
323 % \begin{修正事項}{1.3} from jlisting.sty
325 \gdef\lst@breakProcessOther#1{\lst@ProcessOther#1}
326 % ソースコード目次における文字と番号の空き
327 \let \l@lstlisting = \l@figure
328 % キャプションとソースコード目次に対する日本語対応
329 \def\lstlistingname{ソースコード}
330 \def\lstlistlistingname{ソースコード目次}