OSDN Git Service

Merge branch 'kitagawa_test' of git.sourceforge.jp:/gitroot/luatex-ja/luatexja into...
[luatex-ja/luatexja.git] / src / patches / lltjp-listings.sty
index 1edb37e..1234e3f 100644 (file)
@@ -3,7 +3,7 @@
 %
 
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{lltjp-listings}[2015/09/08 Patch to listings for LuaTeX-ja]
+\ProvidesPackage{lltjp-listings}[2015/11/05 Patch to listings for LuaTeX-ja]
 \RequirePackage{listings,luatexbase-cctb}
 
 %%%%%%%%%%%%%%%% Japanese support
   \SetCatcodeRange{"FE00}{"FE0F}{13}%     Variation Selector
 }
 
-% redefine \lstinline@ and \lst@InlineG
-% because \lstinline!あ...! causes ``Runaway argument?'' Error
-\def\lstinline@#1{%
-    \lst@Init\relax
-    \ltj@lst@check@inline{\lst@InlineM#1}{\lst@InlineJ#1}}
-\def\lst@InlineG{%
-    \lst@Init\relax
-    \ltj@lst@check@inline{\lst@InlineM\}}%
-                         {\let\lst@arg\@empty \lst@InlineGJ}}
-
-\def\ltj@lst@check@inline#1#2#3{%
-    \begingroup \lccode`\~=`#3\lowercase{\endgroup
-    \ifx~}#3%
-        \def\lst@next{#1}%
-    \else\ifnum`#3>127\relax
-        \def\lst@next{#1}%
-    \else
-        \def\lst@next{#2}%
-    \fi\fi\lst@next #3}
+% redefine \lstinline and its inner commands to support Japanese characters
+\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}
+
 
 % We redefine \lst@BeginDropInput, since now we have
 % two additional `process macros'.
 \lst@AddToHook{Init}{%
   \luatexcatcodetable\CatcodeTableLTJlistings\ltj@@listing@jpcmd
   \lccode`\~="FFFFF\lowercase{\def~{\ltj@lst@ProcessJALetter}}%"
-  \directlua{luatexbase.add_to_callback('process_input_buffer',
-    function(buf)
-      local ret = ''
-      for i = 1, utf.len(buf) do
-        local c = utf.sub(buf, i, i)
-        local cu = utf.byte(c)
-        if cu >= 0x80 and tex.getcatcode(cu) \string~= 13 then
-          ret = ret .. utf.char(1048575) % U+FFFFF
+  \directlua{%
+    luatexja.patch_listings = 1
+    luatexbase.add_to_callback('process_input_buffer',
+      function(buf)
+        local ret = ''
+        for i = 1, utf.len(buf) do
+          local c = utf.sub(buf, i, i)
+          local cu = utf.byte(c)
+          if cu >= 0x80 and tex.getcatcode(cu) \string~= 13 then
+            ret = ret .. utf.char(1048575) % U+FFFFF
+          end
+          ret = ret .. c
         end
-        ret = ret .. c
-      end
-      return ret
-    end, 'ltj.listings_unicode', 1)}%
+        return ret
+      end, 'ltj.listings_unicode', 1)}%
   \if@ltj@lst@double
     \let\ltj@lst@hss@normal=\ltj@lst@hss@double
   \else
     \let\ltj@lst@hss=\ltj@lst@hss@normal
   \fi
 }
+
+\def\ltj@lst@MakeActive#1{%
+    \let\lst@temp\@empty \ltj@lst@MakeActive@#1\relax}
+\begingroup
+\catcode`\^^A=12
+\catcode`\^^@=\active
+\lccode`\$="FFFFF \catcode`\$=13 %"
+\lowercase{%
+\gdef\ltj@lst@MakeActive@#1{\let\lst@next\relax%
+    \ifx#1\relax
+    \else\let\lst@next\ltj@lst@MakeActive@
+    \ifnum`#1>127 
+      \lccode`\^^A=`#1 
+      \lowercase{\lst@lAddTo\lst@temp{$^^A}}%
+    \else
+      \lccode`\^^@=`#1 
+      \lowercase{\lst@lAddTo\lst@temp{^^@}}%
+    \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
+
 \lst@AddToHook{ExitVars}{%
-  \directlua{luatexbase.remove_from_callback('process_input_buffer', 
-    'ltj.listings_unicode')}%
-}
+  \directlua{%
+    if luatexja.patch_listings then
+      luatexja.patch_listings=nil
+      luatexbase.remove_from_callback('process_input_buffer', 
+        'ltj.listings_unicode')%
+    end}}
 
 
 % 白線対策
+\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}%
     \@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
-    \edef\lst@framelr{\lineskiplimit-\the\@tempdima\copy\lst@framebox}%
+    \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
 \def\lstlistingname{ソースコード}
 \def\lstlistlistingname{ソースコード目次}
 %    \end{修正事項}
-\endinput
\ No newline at end of file
+\endinput