OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/luatex-ja/luatexja
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sat, 14 May 2011 12:09:45 +0000 (21:09 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sat, 14 May 2011 12:09:45 +0000 (21:09 +0900)
13 files changed:
src/luatexja-base.sty [new file with mode: 0644]
src/luatexja-cctbreg.sty [new file with mode: 0644]
src/luatexja-compat-ptex.sty [new file with mode: 0644]
src/luatexja-compat-ptex.tex [deleted file]
src/luatexja-compat.sty [new file with mode: 0644]
src/luatexja-core.sty
src/luatexja-plain.sty [moved from src/luatexja-plain.tex with 60% similarity]
src/luatexja.sty [new file with mode: 0644]
src/luatexja/base.lua [new file with mode: 0644]
src/luatexja/debug.lua [new file with mode: 0644]
src/luatexja/infomute.lua [new file with mode: 0644]
src/luatexja/stack.lua [new file with mode: 0644]
src/luatexja/tangle.lua [new file with mode: 0644]

diff --git a/src/luatexja-base.sty b/src/luatexja-base.sty
new file mode 100644 (file)
index 0000000..4e43ab5
--- /dev/null
@@ -0,0 +1,519 @@
+%
+% luatexja-base.sty
+%
+
+%! これは外から読まれない
+%% Avoid multiple loading.
+\csname luatexjabaseLoaded\endcsname
+\edef\ltj@base@AtEnd{%
+\endlinechar=\the\endlinechar
+\relax}
+\endlinechar=-1 %
+\def\luatexjabaseLoaded{\endinput}
+
+\ifltj@in@latex                 %<*LaTeX>
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja-base}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+%% Quark tokens needed in Lua modules.
+\protected\def\ltj@@q@escape{\ltj@@q@escape@}
+\protected\def\ltj@@q@escapenum{\ltj@@q@escapenum@}
+
+%% Load Lua modules.
+\RequireLuaModule{luatexja.base}
+\RequireLuaModule{luatexja.tangle}
+\RequireLuaModule{luatexja.stack}
+
+%%------------------ Tiny helpers
+
+%% Registers
+\newcount\ltj@tempcnta
+\newcount\ltj@tempcntb
+\newdimen\ltj@tempdima
+
+%% Quarks
+%! ただ expl3 の quark とは異なり展開されるとエラーになる.
+%% \ltj@q@stop
+\protected\def\ltj@q@stop{\ltj@q@stop@}
+%% \ltj@q@nil
+\protected\def\ltj@q@nil{\ltj@q@nil@}
+%% \ltj@q@mark
+\protected\def\ltj@q@mark{\ltj@q@mark@}
+
+%! etoolbox の \letcs, \cslet, \csletcs.
+%% \ltj@letcs
+\protected\def\ltj@letcs#1#2{
+  \expandafter\let\expandafter#1\csname#2\endcsname
+}
+
+%% \ltj@cslet
+\protected\def\ltj@cslet#1#2{
+  \expandafter\let\csname#1\endcsname#2
+}
+
+%% \ltj@csletcs
+\protected\def\ltj@csletcs#1#2{
+  \expandafter\let\csname#1\expandafter\endcsname
+  \csname#2\endcsname
+}
+
+%% \ltj@ifx{<sutff>}{<yes>}{<no>}
+%! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
+%! 脱出が可能であること.
+% Does \ifx<stuff> test in LaTeX style.
+\long\def\ltj@ifx#1{
+  \ifx#1\expandafter\ltx@firstoftwo
+  \else\expandafter\ltx@secondoftwo\fi
+}
+
+%% \ltj@if@empty{<stuff>}{<yes>}{<no>}
+%! <stuff> が空であるか.
+% Checks if <stuff> is empty.
+\long\def\ltj@if@empty#1{
+  \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
+}
+\protected\def\ltj@@q@empty{\ltj@@q@empty@}
+
+%% \ltj@if@blank{<stuff>}{<yes>}{<no>}
+%! <stuff> が空または空白文字からなるか.
+% Checks if <stuff> is either empty or consisting only of spaces.
+\def\ltj@if@blank#1{
+  \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
+   \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
+}
+\long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
+  #4
+}
+
+%% \ltj@burst-`>TEXT
+%! トリックに使う \romannumeral をエイリアスしておく.
+% Gets the head of TEXT expanded repeatedly until an unexpandable
+% token is seen, and if the token is a space then it is gobbled.
+\let\ltj@burst\romannumeral
+
+%%------------------ LaTeX vs plain
+\ifltj@in@latex            %<*LaTeX>
+
+%% \ifltj@in@latex
+%! LaTeX であるか.
+%(Defined in luatexja-core.sty.)
+
+%% \ifltj@in@plain
+%! plain であるか.
+\ltj@csletcs{ifltj@in@plain}{iffalse}
+
+%% \ltj@require@package{<package>}{<date>}
+%! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
+%! \input を使う. <date> は必須だが空でもよい.
+\def\ltj@require@package#1#2{
+  \RequirePackage{#1}[#2]
+}
+
+%% \ltj@print{<message>}
+%! 端末への出力.
+\let\ltj@print\typeout
+
+\else                           %<*!LaTeX>
+
+%% \ifltj@in@plain
+\ifnum\pdf@strcmp{\fmtname}{plain}=0 %
+  \ltj@csletcs{ifltj@in@plain}{iftrue}
+\else
+  \ltj@csletcs{ifltj@in@plain}{iffalse}
+\fi
+
+%% \ltj@require@package{<package>}{<date>}
+\def\ltj@require@package#1#2{
+  \input #1.sty\relax
+}
+
+%% \ltj@print{<message>}
+\def\ltj@print#1{
+  \immediate\write16{#1}
+}
+
+\fi                             %</LaTeX>
+%%------------------ Value-token handling
+
+%% helper stuffs
+
+%% \ltj@gobble@num <number>
+%! 次に続く整数を(2 回展開で)読み捨てる. 
+\def\ltj@gobble@num{
+  \ltj@burst-`>\ltj@@gobble@num
+}
+\def\ltj@@gobble@num{
+  \expandafter\ltj@@gobble@numA\the\parshapeindent
+}
+\begingroup
+  \lccode`8=`p\lccode`9=`t
+\lowercase{\endgroup
+  \def\ltj@@gobble@numA#189{ }
+}
+
+%% \ltj@gobble@glue <glue>
+%! 次に続くグルー値を(2 回展開で)読み捨てる. 
+\def\ltj@gobble@glue{%
+  \ltj@burst-`>\ltj@@gobble@glue
+}
+\def\ltj@@gobble@glue{
+  \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
+}
+\def\ltj@@gobble@glueA#1{ }
+
+%% \ltj@gobble@dimen <dimen>
+%! 次に続く寸法値を(2 回展開で)読み捨てる. 
+\def\ltj@gobble@dimen{%
+  \ltj@burst-`>\ltj@@gobble@dimen
+}
+\def\ltj@@gobble@dimen{
+  \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
+  0pt minus
+}
+\def\ltj@@gobble@dimenA#1{ }
+
+%% \ltj@@scan@brace
+% This is to be followed by a macro with one argument; if the macro is
+% followed by an open-group token (catcode 1), then it receives as the
+% argument a token \bxnt@escape instead of the group initiated by the
+% open-group, which is left untouched.
+\def\ltj@@scan@brace{
+  \directlua{luatexja.base.scan_brace()}
+}
+
+%% \ltj@@scan@number
+\def\ltj@@scan@number{
+  \directlua{luatexja.base.scan_number()}
+}
+
+%% \ltj@grab@num <number>
+%! 次に続く整数を読み取って, それと等しい整数を表すトークン列
+%! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
+%! 展開する.
+%! ただし, 整数は以下の形式のいずれかでなければならない.
+%! - 整数表記(10 進, 8 進, 16 進, 文字)
+%! - chardef トークン
+%! - 内部整数パラメタ, countdef トークン
+%!   (command_name が "assign_int" であるトークン).
+%! (注意: まだ文字表記(`A)に対応していません.)
+\def\ltj@grab@num{
+  \ltj@burst-`>\ltj@@grab@num
+}
+\def\ltj@@grab@num{
+  \ltj@@scan@brace\ltj@@grab@numA
+}
+\def\ltj@@grab@numA#1{
+  \ltj@ifx{#1\ltj@@q@escape}{}{%else
+    \ltj@@scan@number\ltj@@grab@numB#1
+  }
+}
+\def\ltj@@grab@numB#1{
+  \ltj@ifx{#1\ltj@@q@escape}{
+    {0}
+  }{
+    \ltj@@grab@numC
+  }
+}
+\def\ltj@@grab@numC#1\ltj@@q@escapenum{
+  {#1}
+}
+
+%%------------------ Safe passing
+
+% These macros convert a token sequence denoting a TeX value to its
+% suitable notation in Lua, and when the argument is malformed then
+% 'nil' is returned. They are all fully-expandable.
+%! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
+
+\def\ltj@@safe@end{\noexpand\ltj@@safe@end}
+  % behaves same as \relax but is distinct from it
+
+%% \ltj@safe@invalid
+% The value passed to lua instead of malformed value tokens.
+\def\ltj@safe@invalid{(nil)}
+
+%% \ltj@safe@num{<number>}
+%! 整数.
+% For a number (integer).
+\def\ltj@safe@num{
+  \ltj@safe@num@or\ltj@safe@invalid
+}
+\def\ltj@safe@num@or#1#2{
+  \expandafter\expandafter\expandafter\ltj@@safe@numA
+   \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
+}
+\def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
+  \ltj@if@blank{#1}{
+    (\number#2)
+  }{
+    #3
+  }
+}
+
+%% \ltj@safe@dimen{<dimen>}
+%! 寸法値.
+% For a dimension. The result is a scaled-point value.
+\def\ltj@safe@dimen{
+  \ltj@safe@dimen@or\ltj@safe@invalid
+}
+\def\ltj@safe@dimen@or#1#2{
+  \expandafter\expandafter\expandafter\ltj@@safe@dimenA
+   \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
+}
+\def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
+  \ltj@if@blank{#1}{
+    (\number\dimexpr#2\relax)
+  }{
+    #3
+  }
+}
+
+%% \ltj@safe@glue{<glue>}
+%! グルー値.
+% For a glue. The result is a gluespec object.
+\def\ltj@safe@glue{
+  \ltj@safe@glue@or\ltj@safe@invalid
+}
+\def\ltj@safe@glue@or#1#2{
+  \expandafter\expandafter\expandafter\ltj@@safe@glueA
+   \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
+}
+\def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
+  \ltj@if@blank{#1}{
+    (luatexja.base.to_skip("\the\glueexpr#2\relax"))
+  }{
+    #3
+  }
+}
+
+%% \ltj@safe@real{<real>}
+%! 実数. これは十進表記に限る.
+% For a real number given in decimal notation or a macro that
+% expands to such notation.
+\def\ltj@safe@real#1{
+  (tonumber("\luatexluaescapestring{#1}"))
+}
+
+%% \ltj@val@counter{<counter>}
+%! LaTeX カウンタの現在値.
+% For the current value of a LaTeX counter.
+\def\ltj@val@counter#1{
+  (\expandafter\number\csname c@#1\endcsname)
+}
+
+%% \ltj@val@skip{<skip>}
+% For the current value of a skip (or LaTeX-length) parameter.
+%! グルーレジスタ(LaTeX 長さ変数).
+%! 整形式のグルー値にも使える.
+\def\ltj@val@skip#1{
+  (luatexja.base.to_skip("\the\glueexpr#1\relax"))
+}
+%! 整形式の整数 → 数値 : \number#1
+%! 内部寸法 → 数値 : \number#1
+%! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
+
+%% \ltj@safe@str{<text>}
+%! 文字列. (トークン列を非トークン化)
+% Converts a token sequence to Lua string notation.
+\def\ltj@safe@str#1{
+  "\luatexluaescapestring{\detokenize{#1}}"
+}
+
+%% \ltj@luaescape{<text>}
+%! 非トークン化して Lua エスケープ.
+\def\ltj@luaescape#1{
+  \luatexluaescapestring{\detokenize{#1}}
+}
+%% \ltj@luaxescape{<text>}
+%! 非トークン化せずに(展開ありで) Lua エスケープ.
+%! つまり単なる \luatexescape.
+\let\ltj@luaxescape\luatexluaescapestring
+
+%%------------------ Fully-expandable error messaging
+
+%! ixerrtrick パッケージから移植.
+%! 展開限定文脈でも使える \PackageError 等.
+%! Lua 関数版(luatexja/base.lua 参照)もある.
+
+%% \ltj@@error@message@a
+\begingroup
+\def~{ }
+\xdef\ltj@@error@message@a{%
+  Type~~H <return>~~for immediate help%
+}%
+\endgroup
+
+%%<+> \ltj@MessageBreak
+% An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
+% (\MessageBreak cannot be used there.)
+% NB: It is realized as a macro that expands to two instances
+% of the character of code 127. This marker is later detected
+% by the Lua process.
+\begingroup
+\lccode42=127
+\lowercase{
+  \gdef\ltj@MessageBreak{**}
+  \directlua{
+    luatexja.base._error_set_break("**")
+  }
+}
+\endgroup
+
+%%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
+% A variant of \GenericError that can be used in expansion-only
+% situation. The meanings of arguments are the same as the original
+% \GenericError.
+%%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
+% A simpler error messenger available in expansion-only situations.
+% It behaves similarly to:
+%   \errhelp{<msg-help}\errmessage{<msg-main>}
+\begingroup
+% with the same hack as in \GenericError...
+\lccode`\@=`\ %
+\lccode`\~=`\ %
+\lccode`\}=`\ %
+\lccode`\{=`\ %
+\catcode`\ =11\relax%
+\lowercase{%
+\endgroup%
+\def\ltj@GenericError#1#2#3#4{% not protected
+\directlua{%
+luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
+"\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
+"\ltj@luaxescape{#4}")%
+}%
+\    % use csname with four spaces as last expander
+}
+\def\    {% csname with four spaces
+\directlua{%
+luatexja.base._error_show(false)% and many spaces trail!
+                                        %
+                                        %
+}%
+}%
+\def\ltj@PlainError#1#2{%
+\directlua{%
+luatexja.base._error_set_message("  ",%
+"\ltj@luaxescape{#1}",%
+"\ltj@luaxescape{#2}")%
+}%
+\error  % again a weird name is used
+}
+\def\error  {% csname with two trailing spaces
+\directlua{%
+luatexja.base._error_show(true)% and many spaces trail!
+                                        %
+                                        %
+}%
+}%
+}
+
+%% \ltj@@error@on@line
+\def\ltj@@error@on@line{
+  on input line \the \inputlineno
+}
+
+%% \ltj@generic@warn@info
+\def\ltj@@generic@warn@info#1#2#3#4{
+  \begingroup
+    \directlua{
+      luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
+        "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
+    }
+  \endgroup
+}
+
+%% \ltj@GenericWarning{<cont>}{<msg-main>}
+\def\ltj@GenericWarning{
+  \ltj@@generic@warn@info{1}{1}
+}
+%% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
+\def\ltj@GenericWarningNoLine{
+  \ltj@@generic@warn@info{1}{0}
+}
+%% \ltj@GenericInfo{<cont>}{<msg-main>}
+\def\ltj@GenericInfo{
+  \ltj@@generic@warn@info{0}{1}
+}
+%% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
+\def\ltj@GenericInfoNoLine{
+  \ltj@@generic@warn@info{0}{0}
+}
+
+%% \ltj@@space@seq@a
+\begingroup
+\def~{ }
+\xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
+\xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
+\endgroup
+
+%% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
+\def\ltj@PackageError#1#2#3{%
+  \ltj@GenericError{(#1)\ltj@@space@seq@a}%
+   {Package #1 Error: #2}%
+   {See the #1 package documentation for explanation.}%
+   {#3}%
+}
+%% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
+\def\ltj@PackageWarning#1#2{%
+  \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
+   {Package #1 Warning: #2}%
+}
+%% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
+\def\ltj@PackageWarningNoLine#1#2{%
+  \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
+   {Package #1 Warning: #2}%
+}
+%% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
+\def\ltj@PackageInfo#1#2{%
+  \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
+   {Package #1 Info: #2}%
+}
+%% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
+\def\ltj@PackageInfoNoLine#1#2{%
+  \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
+   {Package #1 Info: #2}%
+}
+
+%%------------------ debug logging
+\ifdefined\LuaTeXjaDebugEnabled
+
+%% Load Lua module
+\RequireLuaModule{luatexja.debug}
+
+%% \ltj@debug{<format>}{<arg>,...}
+\def\ltj@debug#1#2{
+  \directlua{
+    luatexja.base.debug(\ltj@safe@str{#1}
+      \ltj@if@blank{#2}{}{, }
+      #2)
+  }
+}
+
+%% \ltj@package@debug{<package>}{<format>}{<arg>,...}
+\def\ltj@package@debug#1#2#3{
+  \directlua{
+    luatexja.base.package_debug(\ltj@safe@str{#1},
+      \ltj@safe@str{#2}
+      \ltj@if@blank{#3}{}{, }
+      #3)
+  }
+}
+
+\else
+
+\def\ltj@debug#1#2{}
+\def\ltj@package@debug#1#2#3{}
+
+\fi
+
+%% \ltj@debug@logger\CS{<package>}
+\def\ltj@debug@logger#1#2{
+  \def#1{\ltj@package@debug{#2}}
+}
+
+%%------------------ all done
+\ltj@base@AtEnd
+\endinput
+%% EOF
diff --git a/src/luatexja-cctbreg.sty b/src/luatexja-cctbreg.sty
new file mode 100644 (file)
index 0000000..390cbb7
--- /dev/null
@@ -0,0 +1,282 @@
+%
+% luatexja-cctbreg.sty
+%
+
+%! luatexbase と luatex パッケージのカテゴリコードテーブルの割当に
+%! 関する衝突を回避する.
+%! この衝突に関して「外部のパッチ」で解決しようとするのは, あまり
+%! 筋が良くなく, できるなら避けたいが, 今のままだと LuaTeX-ja を
+%! LaTeX + fontspec で用いた時に確実に影響が出る. また衝突を残した
+%! ままだとカテゴリコードテーブルが実質的に使えない訳で, これも
+%! 残念なことである. 
+%! 「近いうちに両パッケージの中で解決される」ことを期待しつつ,
+%! 「衝突がない」という前提で開発を行いたいので, 暫定的処置として,
+%! このモジュールを用意する.
+
+%! これは外から読まれない
+%% Avoid multiple loading.
+\csname luatexjacctbregLoaded\endcsname
+\edef\ltj@cctbreg@AtEnd{%
+\endlinechar=\the\endlinechar
+\relax}
+\endlinechar=-1 %
+\def\luatexjacctbregLoaded{\endinput}
+
+\ifltj@in@latex                 %<*LaTeX>
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja-cctbreg}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+% LS = Size of storage reserved for luatex-pkg.
+\chardef\ltj@cctb@lower@stor@size=32768
+
+%-------------------
+
+\newcount\ltj@@cctb@failure
+
+\ltj@@cctb@failure=0
+\def\ltj@@cctb@check@failure{
+  \ifnum\ltj@@cctb@failure>0 %
+    \@PackageWarningNoLine{luatexja-cctbreg}
+      {Cannot apply the patch (\the\ltj@@cctb@failure)}
+    \ltj@cctbreg@AtEnd
+  \expandafter\endinput\fi\relax
+}
+
+\chardef\ltj@@cctb@lower@stor@max=
+  \numexpr\ltj@cctb@lower@stor@size-1\relax
+\chardef\ltj@@cctb@lower@stor@safemax=
+  \numexpr\ltj@cctb@lower@stor@size-8\relax
+
+\chardef\ltj@@tempcctb@a=\ltj@cctb@lower@stor@size
+\chardef\ltj@@tempcctb@b=\numexpr\ltj@cctb@lower@stor@size+1\relax
+\def\ltj@@copy@cctb#1#2{%
+  % Note: The current cctb number is supposed to be zero.
+  \luatexcatcodetable#1\relax
+  \luatexsavecatcodetable#2\relax
+  \luatexcatcodetable0\relax
+}
+
+%-------------------
+
+% The luatexbase-cctb package stores its preset settings to
+% registers 1--6, and new user allocations go to number 7, 8, ....
+
+\ifdefined\lltxb@catcodetable@alloc
+  \ifnum\lltxb@catcodetable@alloc>\ltj@@cctb@lower@stor@safemax
+    % Failure: luatexbase-cctb alloc pointer is too high!
+    \ltj@@cctb@failure=2 %
+  \fi
+\else
+  % Failure: luatexbase-cctb not loaded!
+  \ltj@@cctb@failure=1 %
+\fi
+
+\begingroup
+\endlinechar=13
+\catcode`\_=12
+\gdef\ltj@@cctb@org@def#1{%
+  \ifnum\lltxb@catcodetable@alloc<65535\relax
+    \global\advance\lltxb@catcodetable@alloc\@ne
+    \allocationnumber\lltxb@catcodetable@alloc
+    \global\chardef#1\allocationnumber
+    \luatexinitcatcodetable\allocationnumber
+    \begingroup\escapechar\m@ne
+    \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
+      '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
+    \endgroup
+    \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
+  \else
+    \errmessage{No room for a new \string\luatexcatcodetable}%
+  \fi}
+\endgroup
+
+\unless\ifx\ltj@@cctb@org@def\newluatexcatcodetable
+  % Failure: luatexbase-cctb is not of expected version!
+\bebo
+  \ltj@@cctb@failure=3 %
+\fi
+
+\ifdefined\LuT@AllocAttribute
+  \ifnum\LuT@AllocAttribute>\ltj@@cctb@lower@stor@safemax
+    % Failure: luatex-pkg alloc pointer is too high!
+    \ltj@@cctb@failure=4 %
+  \fi
+\fi
+\ifdefined\CatcodeTableStack
+  \ifnum\CatcodeTableStack>\ltj@@cctb@lower@stor@safemax
+    % Failure: luatex-pkg cctb-stack pointer is too high!
+    \ltj@@cctb@failure=5 %
+  \fi
+\fi
+
+\ltj@@cctb@check@failure
+
+\ltx@firstofone{
+  \newluatexcatcodetable\cctb@seven
+  \catcode12345=14 %
+  \luatexsavecatcodetable\cctb@seven
+  \catcode12345=12 %
+}
+
+%% Save the register 7 (to a temporary register)
+\unless\ifnum\lltxb@catcodetable@alloc<7 %
+  \ltj@@copy@cctb{7}{\ltj@@tempcctb@a}
+\fi
+
+%-------------------
+
+% The luatex package stores its preset settings to registers 1, 3, 5, 7
+% and new user allocations go to odd number 9, 11, 13, etc.; registers
+% with even number are reserved for the "category code table stack".
+
+% I relocate all the future allocation by luatexbase-cctb to the "higher"
+% area (number >= \ltj@cctb@lower@stor@size), which is simply done by
+% moving the allocation pointer. And all the preset settings of
+% luatexbase-cctb (those of luatex-pkg is a subset) are prepared again.
+% The colliding user allocation cannot be helped, but in the fortunate
+% case where luatex-pkg is not yet loaded or its allocating function
+% is never invoked, the only endangered user allocation is register 7
+% of luatexbase-cctb, which is rescued by simply reassining the table,
+% since the all presets are migrated to the higher area.
+
+\unless\ifdefined\LuT@AllocAttribute
+  \ifltj@in@latex
+    \RequirePackage{luatex}[2010/03/01]
+  \else
+    \input luatex.sty\relax
+  \fi
+\fi
+
+\edef\ltj@@tempa{\meaning\LuT@AllocAttribute}
+\edef\ltj@@tempb{\meaning\lltxb@catcodetable@alloc}
+\ifx\ltj@@tempa\ltj@@tempb
+  % Failure: luatexbase-cctb and luatex-pkg seem to be synchronized!
+  \ltj@@cctb@failure=6 %
+\fi
+\ltj@@cctb@check@failure
+
+\unless\ifnum 7=
+ \ifdefined\CatcodeTableLaTeX \CatcodeTableLaTeX
+ \else 0 \fi
+  % Failure: luatex-pkg is not of expected version!!
+  \ltj@@cctb@failure=100 %
+\fi
+
+\def\ltj@@cctb@org@def#1{
+  \ifnum\LuT@AllocCatcodeTable<1114110 %
+    \global\advance\LuT@AllocCatcodeTable by\tw@
+    \allocationnumber=\LuT@AllocCatcodeTable
+    \global\chardef#1=\allocationnumber
+    \wlog{
+      \string#1=\string\catcodetable\the\allocationnumber
+    }
+  \else
+    \errmessage{No room for a new \string\catcodetable}
+  \fi
+}
+\unless\ifx\ltj@@cctb@org@def\newcatcodetable
+  % Failure: luatex-pkg is not of expected version!!
+  \ltj@@cctb@failure=100 %
+\fi
+
+\def\ltj@@cctb@org@def{%
+  \ifnum\CatcodeTableStack<268435454 %
+    \global\advance\CatcodeTableStack by\tw@
+  \else
+    \@PackageError{luatex}{%
+      Catcode table stack overflow%
+    }\@ehd
+  \fi
+}
+\unless\ifx\ltj@@cctb@org@def\IncCatcodeTableStack
+  % Failure: luatex-pkg is not of expected version!!
+  \ltj@@cctb@failure=100 %
+\fi
+\ltj@@cctb@check@failure
+
+%% Restore the register 7 which is overwritten by luatex-pkg.
+\unless\ifnum\lltxb@catcodetable@alloc<7 %
+  \ltj@@copy@cctb{\ltj@@tempcctb@a}{7}
+\fi
+
+%% Move the pointer of luatexbase-cctb to higher area.
+\lltxb@catcodetable@alloc=\ltj@cctb@lower@stor@size
+
+%% Restrict the allocation by luatex-pkg to lower area.
+\chardef\ltj@@cctb@lower@stor@check=
+ \numexpr\ltj@cctb@lower@stor@size-2\relax
+\def\newcatcodetable#1{
+  \ifnum\LuT@AllocCatcodeTable<\ltj@@cctb@lower@stor@check
+    \global\advance\LuT@AllocCatcodeTable by\tw@
+    \allocationnumber=\LuT@AllocCatcodeTable
+    \global\chardef#1=\allocationnumber
+    \wlog{
+      \string#1=\string\catcodetable\the\allocationnumber
+    }
+  \else
+    \errmessage{No room for a new \string\catcodetable}
+  \fi
+}
+\def\IncCatcodeTableStack{%
+  \ifnum\CatcodeTableStack<\ltj@@cctb@lower@stor@check
+    \global\advance\CatcodeTableStack by\tw@
+  \else
+    \@PackageError{luatex}{%
+      Catcode table stack overflow%
+    }\@ehd
+  \fi
+}
+
+%------------------- Re-create the presets
+
+\newluatexcatcodetable\CatcodeTableIniTeX
+\newluatexcatcodetable\CatcodeTableString
+\setluatexcatcodetable\CatcodeTableString{%
+  \luatexcatcodetable\CatcodeTableIniTeX
+  \catcode0   12 % nul
+  \catcode13  12 % carriage return
+  \catcode37  12 % percent
+  \setcatcoderange{65}{90}{12}% A-Z
+  \setcatcoderange{97}{122}{12}% a-z
+  \catcode92  12 % backslash
+  \catcode127 12 }
+\newluatexcatcodetable\CatcodeTableOther
+\setluatexcatcodetable\CatcodeTableOther{%
+  \luatexcatcodetable\CatcodeTableString
+  \catcode32 12 }
+\newluatexcatcodetable\CatcodeTableLaTeX
+\setluatexcatcodetable\CatcodeTableLaTeX{%
+  \luatexcatcodetable\CatcodeTableIniTeX
+  \setcatcoderange{0}{31}{15}%
+  \catcode9   10 % tab
+  \catcode12  13 % form feed
+  \catcode13  5  % carriage return
+  \catcode35  6  % hash
+  \catcode36  3  % dollar
+  \catcode38  4  % ampersand
+  \catcode94  7  % circumflex
+  \catcode95  8  % underscore
+  \catcode123 1  % brace left
+  \catcode125 2  % brace right
+  \catcode126 13 % tilde
+  \catcode127 15 }
+\newluatexcatcodetable\CatcodeTableLaTeXAtLetter
+\setluatexcatcodetable\CatcodeTableLaTeXAtLetter{%
+  \luatexcatcodetable\CatcodeTableLaTeX
+  \catcode64=11 }
+\newluatexcatcodetable\CatcodeTableExpl
+\setluatexcatcodetable\CatcodeTableExpl{%
+  \luatexcatcodetable\CatcodeTableLaTeX
+  \catcode126 10 % tilde is a space char
+  \catcode32  9  % space is ignored
+  \catcode9   9  % tab also ignored
+  \catcode95  11 % underscore letter
+  \catcode58  11 % colon letter
+  }
+\luatexbase@directlua{luatexbase.catcodetable_do_shortcuts()}
+
+%%------------------ all done
+\ltj@cctbreg@AtEnd
+\endinput
+%% EOF
diff --git a/src/luatexja-compat-ptex.sty b/src/luatexja-compat-ptex.sty
new file mode 100644 (file)
index 0000000..c0eb7d2
--- /dev/null
@@ -0,0 +1,67 @@
+%
+% luatexja-compat-ptex.sty
+%
+
+%! これは外から読まれない
+%% Avoid multiple loading.
+\csname luatexjacompatptexLoaded\endcsname
+\edef\ltj@compat@ptex@AtEnd{%
+\catcode64=\the\catcode64%
+\endlinechar=\the\endlinechar
+\relax}
+\catcode64=11 %<@>
+\endlinechar=-1 %
+\def\luatexjacompatptexLoaded{\endinput}
+
+%% Check if LaTeX is used.
+\begingroup\expandafter\expandafter\expandafter\endgroup
+\expandafter\ifx\csname RequirePackage\endcsname\relax %<*!LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iffalse\endcsname
+\else                           %<*LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iftrue\endcsname
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja-compat-ptex}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+%% Load prerequisite packages.
+\endlinechar=13 %
+\ifltj@in@latex                 %<*LaTeX>
+  \RequirePackage{infwarerr}  [2010/04/08] % v1.3
+  \RequirePackage{ltxcmds}    [2010/04/26] % v1.7
+  \RequirePackage{xkeyval}    [2008/08/13] % v2.6a
+\else                           %<*!LaTeX>
+  \input infwarerr.sty
+  \input ltxcmds.sty
+  \input xkeyval
+\fi                             %</LaTeX>
+\endlinechar=-1 %
+
+\ifx\luatexjacompatLoaded\@undefined
+  \@PackageError{luatexja}
+   {This file must be loaded through 'luatexja-compat'}
+   {\InfWarErr@ehc}
+\fi
+
+%%------------------
+
+\endlinechar=13
+%! この中に master の luatexja-compat-ptex.tex の内容を挿入する
+%! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
+\def\getprebreakpenalty#1{\prebreakpenalty#1 }
+\def\getpostbreakpenalty#1{\postbreakpenalty#1 }
+\def\setprebreakpenalty#1#2{\prebreakpenalty#1=#2 }
+\def\setpostbreakpenalty#1#2{\postbreakpenalty#1=#2 }
+\def\getinhibitxspcode#1{\inhibitxspcode#1 }
+\def\setinhibitxspcode#1#2{\inhibitxspcode#1=#2 }
+\def\getxspcode#1{\xspcode#1 }
+\def\setxspcode#1#2{\xspcode#1=#2 }
+
+%\endinput
+%! ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
+
+%%------------------ all done
+\ltj@compat@ptex@AtEnd
+\endinput
+%% EOF
diff --git a/src/luatexja-compat-ptex.tex b/src/luatexja-compat-ptex.tex
deleted file mode 100644 (file)
index 8f7a96e..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-%%% luatexja-compat-ptex.tex:
-
-
-\def\getprebreakpenalty#1{\prebreakpenalty#1 }
-\def\getpostbreakpenalty#1{\postbreakpenalty#1 }
-\def\setprebreakpenalty#1#2{\prebreakpenalty#1=#2 }
-\def\setpostbreakpenalty#1#2{\postbreakpenalty#1=#2 }
-\def\getinhibitxspcode#1{\inhibitxspcode#1 }
-\def\setinhibitxspcode#1#2{\inhibitxspcode#1=#2 }
-\def\getxspcode#1{\xspcode#1 }
-\def\setxspcode#1#2{\xspcode#1=#2 }
-
-\endinput
\ No newline at end of file
diff --git a/src/luatexja-compat.sty b/src/luatexja-compat.sty
new file mode 100644 (file)
index 0000000..69a8622
--- /dev/null
@@ -0,0 +1,110 @@
+%
+% luatexja-compat.sty
+%
+
+%! 外から読まれるので仰々しくなってます
+%% Avoid multiple loading.
+\csname\if11luatexjacompatLoaded\fi\endcsname
+\edef\x{%
+\catcode32=\the\catcode32%
+\catcode33=\the\catcode33%
+\catcode35=\the\catcode35%
+\catcode40=\the\catcode40%
+\catcode41=\the\catcode41%
+\catcode42=\the\catcode42%
+\catcode43=\the\catcode43%
+\catcode44=\the\catcode44%
+\catcode45=\the\catcode45%
+\catcode46=\the\catcode46%
+\catcode58=\the\catcode58%
+\catcode59=\the\catcode59%
+\catcode60=\the\catcode60%
+\catcode61=\the\catcode61%
+\catcode62=\the\catcode62%
+\catcode64=\the\catcode64%
+\catcode91=\the\catcode91%
+\catcode93=\the\catcode93%
+\catcode94=\the\catcode94%
+\catcode95=\the\catcode95%
+\catcode126=\the\catcode126%
+\endlinechar=\the\endlinechar
+\relax}
+\catcode32=10 %< >
+\catcode33=12 %<">
+\catcode35=6  %<#>
+\catcode40=12 %<(>
+\catcode41=12 %<)>
+\catcode42=12 %<*>
+\catcode43=12 %<+>
+\catcode44=12 %<,>
+\catcode45=12 %<->
+\catcode46=12 %<.>
+\catcode58=12 %<:>
+\catcode59=12 %<;>
+\catcode60=12 %<<>
+\catcode61=12 %<=>
+\catcode62=12 %<>>
+\catcode64=11 %<@>
+\catcode91=12 %<[>
+\catcode93=12 %<]>
+\catcode94=7  %<^>
+\catcode95=8  %<_>
+\catcode126=13 %<~>
+\endlinechar=-1 %
+%! なお, 上のcatcode設定はcoreとcompatで同一にしておく.
+\edef\ltj@compat@AtEnd{\x
+  \noexpand\let\noexpand\ltj@compat@AtEnd\relax}
+\def\luatexjacompatLoaded{\endinput}
+
+%% Check if LaTeX is used.
+\begingroup\expandafter\expandafter\expandafter\endgroup
+\expandafter\ifx\csname RequirePackage\endcsname\relax %<*!LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iffalse\endcsname
+\else                           %<*LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iftrue\endcsname
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja-compat}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+%% If the engine is pTeX, then load the module for it and exit.
+\edef\ltj@@tempa{\string\kansuji}
+\edef\ltj@@tempb{\meaning\kansuji}
+\ifx\ltj@@tempa\ltj@@tempb      %<*pTeX>
+  \let\ltj@@compat@pTeX=t
+  \ifltj@in@latex                 %<*LaTeX>
+    \RequirePackage{luatexja-compat-ptex}[2011/04/01]
+  \else                           %<*!LaTeX>
+    \input luatexja-compat-ptex.sty %
+  \fi                             %</LaTeX>
+  \ltj@compat@AtEnd
+\expandafter\endinput\fi\relax  %</pTeX>
+
+% Load core module if not yet.
+\ifx\luatexjacoreLoaded\@undefined
+  \ifltj@in@latex                 %<*LaTeX>
+    \RequirePackage{luatexja-core}[2011/04/01]
+  \else                           %<*!LaTeX>
+    \input luatexja-core.sty %
+  \fi                             %</LaTeX>
+\fi
+
+%% When environment check fails, exit now.
+\ifx\LuaTeXjaAvailable\@undefined
+  \ltj@compat@AtEnd
+\expandafter\endinput\fi\relax
+
+%% Load Lua modules.
+\RequireLuaModule{luatexja.infomute}
+
+%% Silence info messages of luatexbase-mcb
+\directlua{ luatexja.infomute.add_mute("luatexbase-mcb") }
+
+%%------------------
+%! これ以降に LuaTeX 用の compat の内容を入れる.
+
+%%------------------ all done
+\ltj@compat@AtEnd
+\endinput
+%% EOF
index ac8724c..c03c1f9 100644 (file)
+%
+% luatexja-core.sty
+%
+
+%! 外から読まれるので仰々しくなってます
+%% Avoid multiple loading.
+\csname\if11luatexjacoreLoaded\fi\endcsname
+\edef\x{%
+\catcode32=\the\catcode32%
+\catcode33=\the\catcode33%
+\catcode35=\the\catcode35%
+\catcode40=\the\catcode40%
+\catcode41=\the\catcode41%
+\catcode42=\the\catcode42%
+\catcode43=\the\catcode43%
+\catcode44=\the\catcode44%
+\catcode45=\the\catcode45%
+\catcode46=\the\catcode46%
+\catcode58=\the\catcode58%
+\catcode59=\the\catcode59%
+\catcode60=\the\catcode60%
+\catcode61=\the\catcode61%
+\catcode62=\the\catcode62%
+\catcode64=\the\catcode64%
+\catcode91=\the\catcode91%
+\catcode93=\the\catcode93%
+\catcode94=\the\catcode94%
+\catcode95=\the\catcode95%
+\catcode126=\the\catcode126%
+\endlinechar=\the\endlinechar
+\relax}
+\catcode32=10 %< >
+\catcode33=12 %<">
+\catcode35=6  %<#>
+\catcode40=12 %<(>
+\catcode41=12 %<)>
+\catcode42=12 %<*>
+\catcode43=12 %<+>
+\catcode44=12 %<,>
+\catcode45=12 %<->
+\catcode46=12 %<.>
+\catcode58=12 %<:>
+\catcode59=12 %<;>
+\catcode60=12 %<<>
+\catcode61=12 %<=>
+\catcode62=12 %<>>
+\catcode64=11 %<@>
+\catcode91=12 %<[>
+\catcode93=12 %<]>
+\catcode94=7  %<^>
+\catcode95=8  %<_>
+\catcode126=13 %<~>
+\endlinechar=-1 %
+%! なお, 上のcatcode設定はcoreとcompatで同一にしておく.
+\edef\ltj@core@AtEnd{\x
+  \noexpand\let\noexpand\ltj@core@AtEnd\relax}
+\def\luatexjacoreLoaded{\endinput}
+
+%% Check if LaTeX is used.
+\begingroup\expandafter\expandafter\expandafter\endgroup
+\expandafter\ifx\csname RequirePackage\endcsname\relax %<*!LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iffalse\endcsname
+\else                           %<*LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iftrue\endcsname
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja-core}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+%% Load prerequisite packages.
+%! 参考: W32TeX の LuaTeX 
+%!     2010/11/27 v0.65.0 r3988 (LuaTeX-ja の最低要件)
+%!     2010/07/10 v0.60.2 r3735 (TL2010 とほぼ同じ)
+\endlinechar=13 %
+%! 外部パッケージは普通の \endlinechar で読まないと危険.
+\ifltj@in@latex                 %<*LaTeX>
+  \RequirePackage{luaotfload} [2010/11/11] % v1.21
+  \RequirePackage{infwarerr}  [2010/04/08] % v1.3
+  \RequirePackage{ltxcmds}    [2010/04/26] % v1.7
+  \RequirePackage{pdftexcmds} [2010/04/01] % v0.9
+  \RequirePackage{xkeyval}    [2008/08/13] % v2.6a
+\else                           %<*!LaTeX>
+  \input luaotfload.sty
+  \input infwarerr.sty
+  \input ltxcmds.sty
+  \input pdftexcmds.sty
+  \input xkeyval
+\fi                             %</LaTeX>
+\endlinechar=-1 %
+
+%% Check if LuaTeX is used
+\ltx@LocalExpandAfter\ifx\csname directlua\endcsname\relax
+  % Loading is silently aborted, since the notice of the wrong
+  % engine is aldready shown in otfluaload.
+  \ltj@core@AtEnd
+\expandafter\endinput\fi\relax
+
+%% Check LuaTeX version.
+\ifnum\luatexversion<65
+  \@PackageError{luatexja-core}
+    {This LuaTeX engine is too old for this package;\MessageBreak
+     at least v0.65 is needed}
+    {Package loading is aborted now.\MessageBreak}
+\expandafter\endinput\fi\relax
+
+%! なお、luatexbase-compat の説明に従うと、LuaTeX 拡張プリミティブは
+%! plain/LaTeX の両方で \luatex 付の名前(\directlua はそのまま)
+%! で使えると考えてよい。
+
+%% \LuaTeXjaAvailable
+%! 環境検査にパスした時に定義される.
+\let\LuaTeXjaAvailable=t %
+
+%%------------------
+
+%% Load all sub-packages.
+\ifltj@in@latex                 %<*LaTeX>
+  \RequirePackage{luatexja-cctbreg}[2011/04/01]
+  \RequirePackage{luatexja-base}[2011/04/01]
+\else                           %<*!LaTeX>
+  \input luatexja-cctbreg.sty %
+  \input luatexja-base.sty %
+\fi                             %</LaTeX>
+
+%%------------------
+\endlinechar=13
+%! この中に master の luatexja-core.sty の内容を挿入する
+%! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 % LuaTeX-ja core TeX source
 
-\catcode`\@=11
+%\catcode`\@=11
 \catcode"FFFFF=14 %"
 
-\input luaotfload.sty
-\input ltxcmds.sty
-\input xkeyval.tex
-\input infwarerr.sty
-\ifdefined\ltj@loaded\endinput\fi
+%\input luaotfload.sty
+%\input ltxcmds.sty
+%\input xkeyval.tex
+%\input infwarerr.sty
+%\ifdefined\ltj@loaded\endinput\fi
 \def\ltj@loaded{hoge}
 
 \newcount\ltj@tempcnta
 
 
 
-\def\ltj@temp{plain}
-\ifx\fmtname\ltj@temp
-  \message{plain format: loading luatexja-plain.tex}
-\fi
-\input luatexja-plain.tex
-\catcode`\@=12
+%\def\ltj@temp{plain}
+%\ifx\fmtname\ltj@temp
+%  \message{plain format: loading luatexja-plain.tex}
+%\fi
+%\input luatexja-plain.tex
+%\catcode`\@=12
+%\endinput
+%! ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
+
+%%------------------ Load module for the format
+
+\ifltj@in@latex                 %<*LaTeX>
+  \RequirePackage{luatexja-latex}[2011/04/01]
+\else\ifltj@in@plain            %<*plain>
+  \input luatexja-plain.sty %
+\fi\fi                          %</LaTeX|plain>
+
+%%------------------ all done
+\ltj@core@AtEnd
 \endinput
+%% EOF
similarity index 60%
rename from src/luatexja-plain.tex
rename to src/luatexja-plain.sty
index 2c70ab0..1539647 100644 (file)
@@ -1,3 +1,26 @@
+%
+% luatexja-plain.sty
+%
+
+%! これは外から読まれない
+%% Avoid multiple loading.
+\csname luatexjaplainLoaded\endcsname
+\edef\ltj@plain@AtEnd{%
+\endlinechar=\the\endlinechar
+\relax}
+\endlinechar=-1 %
+\def\luatexjaplainLoaded{\endinput}
+
+\ifltj@in@latex                 %<*LaTeX>
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja-plain}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+%%------------------
+
+\endlinechar=13
+%! この中に master の luatexja-plain.tex の内容を挿入する
+%! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 % LuaTeX-ja macros against plain TeX
 
 \jfont\tenmin=psft:Ryumin-Light:jfm=ujis at 13.5\jQ
   \ltjsetparameter{kcatcode={\ltj@tempcnta,1}}%
   \advance\ltj@tempcnta by1
 \repeat
-\endinput
\ No newline at end of file
+%\endinput
+%! ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
+
+%%------------------ all done
+\ltj@plain@AtEnd
+\endinput
+%% EOF
diff --git a/src/luatexja.sty b/src/luatexja.sty
new file mode 100644 (file)
index 0000000..1ff2a5b
--- /dev/null
@@ -0,0 +1,53 @@
+%
+% luatexja.sty
+%
+
+%! 外から読まれるので多少仰々しくなってます
+%% Avoid multiple loading.
+\csname\if11luatexjaLoaded\fi\endcsname
+\edef\x{%
+\catcode32=\the\catcode32%
+\catcode45=\the\catcode45%
+\catcode46=\the\catcode46%
+\catcode64=\the\catcode64%
+\catcode91=\the\catcode91%
+\catcode93=\the\catcode93%
+\endlinechar=\the\endlinechar
+\relax}
+\catcode32=10 %< >
+\catcode45=12 %<->
+\catcode46=12 %<.>
+\catcode64=11 %<@>
+\catcode91=12 %<[>
+\catcode93=12 %<]>
+\endlinechar=13 %
+%! あまり恩恵がないので \endlinechar=-1 はなし
+\edef\ltj@AtEnd{\x
+  \noexpand\let\noexpand\ltj@AtEnd\relax}
+\def\luatexjaLoaded{\endinput}
+
+%% Check if LaTeX is used.
+\begingroup\expandafter\expandafter\expandafter\endgroup
+\expandafter\ifx\csname RequirePackage\endcsname\relax %<*!LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iffalse\endcsname
+\else                           %<*LaTeX>
+  \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
+    \csname iftrue\endcsname
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{luatexja}[2011/04/01 v0.1]
+\fi                             %</LaTeX>
+
+%% Load all sub-packages.
+\ifltj@in@latex                 %<*LaTeX>
+  \RequirePackage{luatexja-core}[2011/04/01]
+  \RequirePackage{luatexja-compat}[2011/04/01]
+\else                           %<*!LaTeX>
+  \input luatexja-core.sty %
+  \input luatexja-compat.sty %
+\fi                             %</LaTeX>
+
+%% all done
+\ltj@AtEnd
+\endinput
+%% EOF
diff --git a/src/luatexja/base.lua b/src/luatexja/base.lua
new file mode 100644 (file)
index 0000000..8379619
--- /dev/null
@@ -0,0 +1,389 @@
+--
+-- luatexja/base.lua
+--
+luatexbase.provides_module({
+  name = 'luatexja.base',
+  date = '2011/04/01',
+  version = '0.1',
+  description = '',
+})
+module('luatexja.base', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+local ltb = luatexbase
+local tostring = tostring
+local node, table, tex, token = node, table, tex, token
+
+-------------------- TeX stream I/O
+do
+
+--! ixbase.print() と同じ
+  --- Extension to tex.print(). Each argument string may contain
+  -- newline characters, in which case the string is output (to
+  -- TeX input stream) as multiple lines.
+  -- @param ... (string) string to output 
+  function mprint(...)
+    local arg = {...}
+    local lines = {}
+    if type(arg[1]) == "number" then
+      table.insert(lines, arg[1])
+      table.remove(arg, 1)
+    end
+    for _, cnk in ipairs(arg) do
+      local ls = cnk:explode("\n")
+      if ls[#ls] == "" then
+        table.remove(ls, #ls)
+      end
+      for _, l in ipairs(ls) do
+        table.insert(lines, l)
+      end
+    end
+    return tex.print(unpack(lines))
+  end
+
+end
+-------------------- Handling of TeX values
+do
+
+  local glue_spec_id = node.id("glue_spec")
+
+  local function copy_skip(s1, s2)
+    if not s1 then
+      s1 = node.new(glue_spec_id)
+    end
+    s1.width = s2.width or 0
+    s1.stretch = s2.stretch or 0
+    s1.stretch_order = s2.stretch_order or 0
+    s1.shrink = s2.shrink or 0
+    s1.shrink_order = s2.shrink_order or 0
+    return s1
+  end
+
+--! ixbase.to_dimen() と同じ
+  function to_dimen(val)
+    if val == nil then
+      return 0
+    elseif type(val) == "number" then
+      return val
+    else
+      return tex.sp(tostring(val))
+    end
+  end
+
+  local function parse_dimen(val)
+    val = tostring(val):lower()
+    local r, fil = val:match("([-.%d]+)fi(l*)")
+    if r then
+      val, fil = r.."pt", fil:len() + 1
+    else
+      fil = 0
+    end
+    return tex.sp(val), fil
+  end
+
+--! ixbase.to_skip() と同じ
+  function to_skip(val)
+    if type(val) == "userdata" then
+      return val
+    end
+    local res = node.new(glue_spec_id)
+    if val == nil then
+      res.width = 0
+    elseif type(val) == "number" then
+      res.width = val
+    elseif type(val) == "table" then
+      copy_skip(res, val)
+    else
+      local t = tostring(val):lower():explode()
+      local w, p, m = t[1], t[3], t[5]
+      if t[2] == "minus" then
+        p, m = nil, t[3]
+      end
+      res.width = tex.sp(t[1])
+      if t[3] then
+        res.stretch, res.stretch_order = parse_dimen(t[3])
+      end
+      if t[5] then
+        res.shrink, res.shrink_order = parse_dimen(t[5])
+      end
+    end
+    return res
+  end
+
+  function dump_skip(s)
+    print(("%s+%s<%s>-%s<%s>"):format(
+      s.width or 0, s.stretch or 0, s.stretch_order or 0,
+      s.shrink or 0, s.shrink_order or 0))
+  end
+
+end
+-------------------- Virtual table for LaTeX counters
+do
+
+--! ixbase.counter と同じ
+  counter = {}
+  local mt_counter = {}
+  setmetatable(counter, mt_counter)
+
+  function mt_counter.__index(tbl, key)
+    return tex.count['c@'..key]
+  end
+  function mt_counter.__newindex(tbl, key, val)
+    tex.count['c@'..key] = val
+  end
+
+--! ixbase.length は tex.skip と全く同じなので不要.
+
+end
+-------------------- Fully-expandable error messaging
+do
+--! LaTeX 形式のエラーメッセージ(\PackageError 等)を
+--! Lua 関数の呼び出しで行う.
+
+  local LF = "\\n"
+  local err_break = ""
+  local err_main = ""
+  local err_help = ""
+
+  local function message_cont(str, c)
+    return str:gsub(err_break, LF .. c)
+  end
+  local function into_lines(str)
+    return str:gsub(err_break, LF):explode(LF)
+  end
+
+  function _error_set_break(str)
+    err_break = str
+  end
+
+  function _error_set_message(msgcont, main, help)
+    err_main = message_cont(main, msgcont)
+    err_help = into_lines(help)
+  end
+
+  function _error_show(escchar)
+    local escapechar = tex.escapechar
+    local newlinechar = tex.newlinechar
+    local errorcontextlines = tex.errorcontextlines
+    if not escchar then tex.escapechar = -1 end
+    tex.newlinechar = 10
+    tex.errorcontextlines = -1
+    tex.error(err_main, err_help)
+    tex.escapechar = escapechar
+    tex.newlinechar = newlinechar
+    tex.errorcontextlines = errorcontextlines
+  end
+
+  local message_a = "Type  H <return>  for immediate help"
+
+  function generic_error(msgcont, main, ref, help)
+    local mainref = main..".\n\n"..ref.."\n"..message_a
+    _error_set_message(msgcont, mainref, help)
+    _error_show(true)
+  end
+
+  function _generic_warn_info(msgcont, main, warn, line)
+    local mainc = message_cont(main, msgcont)
+    local br = warn and "\n" or ""
+    local out = warn and "term and log" or "log"
+    local on_line = line and (" on input line "..tex.inputlineno) or ""
+    local newlinechar = tex.newlinechar
+    tex.newlinechar = -1
+    texio.write_nl(out, br..main..on_line.."."..br)
+    tex.newlinechar = newlinechar
+  end
+
+  function generic_warning(msgcont, main)
+    _generic_warn_info(msgcont, main, true, true)
+  end
+  function generic_warning_no_line(msgcont, main)
+    _generic_warn_info(msgcont, main, true, false)
+  end
+  function generic_info(msgcont, main)
+    _generic_warn_info(msgcont, main, false, true)
+  end
+  function generic_info_no_line(msgcont, main)
+    _generic_warn_info(msgcont, main, false, false)
+  end
+
+  function package_error(pkgname, main, help)
+    generic_error("("..pkgname.."                ",
+      "Package "..pkgname.." Error: "..main,
+      "See the "..pkgname.." package documentation for explanation.",
+      help)
+  end
+  function package_warning(pkgname, main)
+    generic_warning("("..pkgname.."                ",
+      "Package "..pkgname.." Warning: "..main)
+  end
+  function package_warning_no_line(pkgname, main)
+    generic_warning_no_line("("..pkgname.."                ",
+      "Package "..pkgname.." Warning: "..main)
+  end
+  function package_info(pkgname, main)
+    generic_info("("..pkgname.."             ",
+      "Package "..pkgname.." Info: "..main)
+  end
+  function package_info_no_line(pkgname, main)
+    generic_info_no_line("("..pkgname.."             ",
+      "Package "..pkgname.." Info: "..main)
+  end
+
+end
+-------------------- Number handling in TeX source
+do
+
+  local tok_escape = token.create("ltj@@q@escape")
+  local tok_num = token.create("ltj@@q@escapenum")
+  local c_id_assign_int = token.command_id("assign_int")
+  local c_id_char_given = token.command_id("char_given")
+
+  local function error_scan()
+    _M.package_error("luatexja",
+      "Missing number of a permitted form, treated as zero",
+      "A number should have been here; I inserted '0'.")
+  end
+
+  local function get_expd_next()
+    local next = token.get_next()
+    while token.is_expandable(next) do
+      token.expand(next)
+      next = token.get_next()
+    end
+    return next
+  end
+
+  local function grab_decimal(next, res)
+    table.insert(res, next)
+    while true do
+      next = get_expd_next()
+      if not (next[1] == 12 and 0x30 <= next[2] and next[2] <= 0x39) then
+        break
+      end
+      table.insert(res, next)
+    end
+    if next[1] == 10 then next = nil end
+    return true, next
+  end
+
+  local function grab_hexa(next, res)
+    local ok = false
+    table.insert(res, next)
+    while true do
+      next = get_expd_next()
+      if not ((next[1] == 12 and (0x30 <= next[2] and next[2] <= 0x39)) or
+              ((next[1] == 12 or next[1] == 11) and
+               (0x41 <= next[2] and next[2] <= 0x46))) then
+        break
+      end
+      ok = true
+      table.insert(res, next)
+    end
+    if next[1] == 10 then next = nil end
+    return ok, next
+  end
+
+  local function grab_octal(next, res)
+    local ok = false
+    table.insert(res, next)
+    while true do
+      next = get_expd_next()
+      if not (next[1] == 12 and (0x30 <= next[2] and next[2] <= 0x37)) then
+        break
+      end
+      ok = true
+      table.insert(res, next)
+    end
+    if next[1] == 10 then next = nil end
+    return ok, next
+  end
+
+  local function grab_charnum(next, res)
+    table.insert(res, next)
+    next = token.get_next()
+    table.insert(res, next)
+    next = get_expd_next()
+    if next[1] == 10 then next = nil end
+    return true, next
+  end
+
+  local function scan_with(delay, scanner)
+    local function proc()
+      if delay ~= 0 then
+        if delay > 0 then delay = delay - 1 end
+        return token.get_next()
+      else
+        local cont, back = scanner()
+        if not cont then
+          ltb.remove_from_callback("token_filter", "ltj@grab@num")
+        end
+        return back
+      end
+    end
+    ltb.add_to_callback("token_filter", proc, "ltj@grab@num", 1)
+  end
+
+  function scan_brace()
+    scan_with(1, function()
+      local next = token.get_next()
+      if next[1] == 1 then
+        return false, { tok_escape, next }
+      elseif next[1] == 10 then
+        return true, { next }
+      else
+        return false, { next }
+      end
+    end)
+  end
+
+  function scan_number()
+    scan_with(1, function()
+      local next = get_expd_next()
+      local res, ok = { tok_num }, false
+      while true do
+        if next[1] == 12 and (next[2] == 0x2B or next[2] == 0x2D) then
+          table.insert(res, next)
+        elseif next[1] ~= 10 then
+          break
+        end
+        next = get_expd_next()
+      end
+      if next[1] == 12 and 0x30 <= next[2] and next[2] <= 0x39 then
+        ok, next = grab_decimal(next, res)
+      elseif next[1] == 12 and next[2] == 0x22 then
+        ok, next = grab_hexa(next, res)
+      elseif next[1] == 12 and next[2] == 0x27 then
+        ok, next = grab_octal(next, res)
+      elseif next[1] == 12 and next[2] == 0x60 then
+        ok, next = grab_charnum(next, res)
+      elseif next[1] == c_id_assign_int or next[1] == c_id_char_given then
+        table.insert(res, next)
+        ok, next = true, nil
+      end
+      if ok then
+         table.insert(res, tok_num)
+      else
+         error_scan()
+         res = { tok_escape }
+      end
+       if next then table.insert(res, next) end
+       return false, res
+    end)
+  end
+
+end
+-------------------- mock of debug logger
+
+if not _M.debug or _M.debug == _G.debug then
+  local function no_op() end
+  debug = no_op
+  package_debug = no_op
+  show_term = no_op
+  show_log = no_op
+  function debug_logger()
+    return no_op
+  end
+end
+
+-------------------- all done
+-- EOF
diff --git a/src/luatexja/debug.lua b/src/luatexja/debug.lua
new file mode 100644 (file)
index 0000000..673e06b
--- /dev/null
@@ -0,0 +1,143 @@
+--
+-- luatexja/debug.lua
+--
+luatexbase.provides_module({
+  name = 'luatexja.debug',
+  date = '2011/04/01',
+  version = '0.1',
+  description = '',
+})
+module('luatexja.debug', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+local table = table
+
+-------------------- pretty-print
+
+local function get_serialize_param()
+  return table.serialize_functions,
+         table.serialize_compact,
+         table.serialize_inline
+end
+local function set_serialize_param(s_f, s_c, s_i)
+  table.serialize_functions = s_f
+  table.serialize_compact = s_c
+  table.serialize_inline = s_i
+end
+
+local function normal_serialize(t)
+  local s_f, s_c, s_i = get_serialize_param()
+  set_serialize_param(true, true, true)
+  local ret = table.serialize(t, false, false, true)
+  set_serialize_param(s_f, s_c, s_i)
+  return ret
+end
+
+function table_tosource(t)
+  if not next(t) then return "{}" end
+  local res_n = "\127"..normal_serialize({t}).."\127"
+  local s, e, cap = res_n:find("\127{\n ({ .* }),\n}\127")
+  if s == 1 and e == res_n:len() then return cap
+  else return normal_serialize(t)
+  end
+end
+
+function function_tosource(f)
+  local res = normal_serialize({f})
+  return res:sub(4, res:len() - 3)
+end
+
+--! 値 v をそれを表すソース文字列に変換する.
+--! lualibs の table.serialize() の処理を利用している.
+function tosource(v)
+  local tv = type(v)
+  if tv == "function" then return function_tosource(v)
+  elseif tv == "table" then return table_tosource(v)
+  elseif tv == "string" then return string.format('%q', v)
+  else return tostring(v)
+  end
+end
+
+local function coerce(f, v)
+  if f == "q" then return "s", tosource(v)
+  elseif f == "s" then return f, tostring(v)
+  else return f, tonumber(v) or 0
+  end
+end
+
+local function do_pformat(fmt, ...)
+  fmt = fmt:gsub("``", "\127"):gsub("`", "%%"):gsub("\127", "`")
+  local i, na, a = 0, {}, {...}
+  local function proc(p, f)
+    i = i + 1; f, na[i] = coerce(f, a[i])
+    return p..f
+  end
+  fmt = fmt:gsub("(%%[-+#]?[%d%.]*)([a-zA-Z])", proc)
+  return fmt:format(unpack(na))
+end
+
+--! string.format() の拡張版. 以下の点が異なる.
+--!  - %q は全ての型について tosource() に変換
+--!  - <%> の代わりに <`> も使える (TeX での使用のため)
+--!  - %d, %s 等でキャストを行う
+function pformat(fmt, ...)
+  if type(fmt) == "string" then
+    return do_pformat(fmt, ...)
+  else 
+    return tosource(fmt)
+  end
+end
+
+-------------------- debug logging
+
+local debug_show_term = true
+local debug_show_log = true
+--! デバッグログを端末に出力するか
+function show_term(v)
+  debug_show_term = v
+end
+--! デバッグログをログファイルに出力するか
+function show_log(v)
+  debug_show_log = v
+end
+
+local function write_debug_log(s)
+  local target
+  if debug_show_term and debug_show_log then
+    texio.write_nl("term and log", s)
+  elseif debug_show_term and not debug_show_log then
+    texio.write_nl("term", s)
+  elseif not debug_show_term and debug_show_log then
+    texio.write_nl("log", s)
+  end
+end
+
+--! デバッグログ出力. 引数は pformat() と同じ.
+function debug(...)
+  if debug_show_term or debug_show_log then
+    write_debug_log("%DEBUG:"..pformat(...))
+  end
+end
+
+--! デバッグログ出力, パッケージ名付き.
+function package_debug(pkg, ...)
+  if debug_show_term or debug_show_log then
+    write_debug_log("%DEBUG("..pkg.."):"..pformat(...))
+  end
+end
+
+--! パッケージ名付きデバッグログ出力器を得る.
+function debug_logger(pkg)
+  return function(...) package_debug(pkg, ...) end
+end
+
+if luatexja.base then
+  luatexja.base.debug = debug
+  luatexja.base.package_debug = package_debug
+  luatexja.base.debug_logger = debug_logger
+  luatexja.base.show_term = show_term
+  luatexja.base.show_log = show_log
+end
+
+-------------------- all done
+-- EOF
diff --git a/src/luatexja/infomute.lua b/src/luatexja/infomute.lua
new file mode 100644 (file)
index 0000000..ec5593a
--- /dev/null
@@ -0,0 +1,59 @@
+--
+-- luatexja/infomute.lua
+--
+luatexbase.provides_module({
+  name = 'luatexja.infomute',
+  date = '2011/04/01',
+  version = '0.1',
+  description = '',
+})
+module('luatexja.infomute', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+--------------------
+--! luatexbase.module_info() で特定のモジュールの情報出力だけ
+--! 抑止したい. 
+
+local org_texio = texio
+local patch_applied = false
+local info_mute = {}
+
+local function pick_module_name(line)
+  local mod
+  if line:sub(1, 7) == "Module " then
+     local s, e = line:find(" ", 8, true)
+     if s then mod = line:sub(8, s - 1) end
+  elseif line:sub(1, 1) == "(" then
+     local s, e = line:find(")", 2, true)
+     if s then mod = line:sub(2, s - 1) end
+  end
+  return mod
+end
+
+local function patched_write_nl(line, ...)
+  local mod = pick_module_name(line)
+  if not (mod and info_mute[mod]) then
+    org_texio.write_nl(line, ...)
+  end
+end
+
+local new_texio = setmetatable({ write_nl = patched_write_nl },
+  { __index = org_texio })
+local org_fenv = getfenv(luatexbase.module_info)
+
+local function apply_patch()
+  setfenv(luatexbase.module_info,
+    setmetatable({ texio = new_texio }, { __index = org_fenv }))
+  patch_applied = true
+end
+
+!-- モジュール mod の情報出力を抑止する.
+function add_mute(mod)
+  info_mute[mod] = true
+  if not patch_applied then
+    apply_patch()
+  end
+end
+
+-------------------- all done
+-- EOF
diff --git a/src/luatexja/stack.lua b/src/luatexja/stack.lua
new file mode 100644 (file)
index 0000000..564957f
--- /dev/null
@@ -0,0 +1,14 @@
+--
+-- luatexja/stack.lua
+--
+luatexbase.provides_module({
+  name = 'luatexja.stack',
+  date = '2011/04/01',
+  version = '0.1',
+  description = 'LuaTeX-ja stack system',
+})
+module('luatexja.stack', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+
+-- EOF
diff --git a/src/luatexja/tangle.lua b/src/luatexja/tangle.lua
new file mode 100644 (file)
index 0000000..7914c93
--- /dev/null
@@ -0,0 +1,77 @@
+--
+-- luatexja/tangle.lua
+--
+luatexbase.provides_module({
+  name = 'luatexja.tangle',
+  date = '2011/04/01',
+  version = '0.1',
+  description = '',
+})
+module('luatexja.tangle', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+--! ixbase0 からの移植
+
+local _DONE, _TEX, _STOP = 0, 1, 2
+local _current_co, _interrupted
+local _resume, _check
+
+local resume_code =
+  "\\directlua{".._NAME..".resume()}\\relax"
+
+function execute(func, ...)
+  if _current_co then
+    err("tangle is going now")
+  end
+  local args = { ... }
+  local co = coroutine.create(function()
+    return _DONE, { func(unpack(args)) }
+  end)
+  _current_co = co
+  _interrupted = false
+  return _check(coroutine.resume(co, ...))
+end
+
+function resume()
+  return _resume(false)
+end
+
+function interrupt()
+  return _resume(true)
+end
+
+function run_tex()
+  coroutine.yield(_TEX, {})
+end
+
+function suspend(...)
+  local intr = coroutine.yield(_STOP, { ... })
+  if intr then
+    _interrupted = true
+    error("*INTR*") -- this error is caught later
+  end
+end
+
+function _resume(intr)
+  if not _current_co then
+    err("tangle is not going")
+  end
+  local co = _current_co
+  return _check(coroutine.resume(co, intr))
+end
+
+function _check(costat, tstat, extra)
+  if not costat then  -- error in coroutine
+    _current_co = nil
+    if _interrupted then return end
+    err(tstat)
+  elseif tstat == _DONE then
+    _current_co = nil
+  elseif tstat == _TEX then
+    tex.print(resume_code)
+  end
+  return unpack(extra)
+end
+
+-- EOF
+