summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/plain/enigma/enigma.tex
blob: d18b72ffa1835f1008c25f8faa650742eb8eb15b (plain)
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
\newif\ifenigmaisrunningplain
\ifcsname ver@enigma.sty\endcsname\else
  \enigmaisrunningplaintrue
  \input{luatexbase.sty}
  \catcode`\@=11
% \else latex
\fi
\catcode`\_=11 % There’s no reason why this shouldn’t be the case.
\catcode`\!=11
%D Nice tool from luat-ini.mkiv. This really helps with those annoying
%D string separators of Lua’s that clutter the source.
% this permits \typefile{self} otherwise nested b/e sep problems
\def\luastringsep{===}
\edef\!!bs{[\luastringsep[}
\edef\!!es{]\luastringsep]}
%D \startdocsection[title=Prerequisites]
%D \startparagraph
%D Package loading and the namespacing issue are commented on in
%D \identifier{enigma.lua}.
%D \stopparagraph
\directlua{
  packagedata = packagedata or { }
  dofile(kpse.find_file\!!bs enigma.lua\!!es)
}

%D \startparagraph
%D First, create somthing like \CONTEXT’s asciimode. We found
%D \texmacro{newluatexcatcodetable} in \identifier{luacode.sty} and it
%D seems to get the job done.
%D \stopparagraph
\newluatexcatcodetable \enigmasetupcatcodes
\bgroup
  \def\escapecatcode      {0}
  \def\begingroupcatcode  {1}
  \def\endgroupcatcode    {2}
  \def\spacecatcode      {10}
  \def\lettercatcode     {11}
  \setluatexcatcodetable\enigmasetupcatcodes {
      \catcode`\^^I = \spacecatcode % tab
      \catcode`\    = \spacecatcode
      \catcode`\{   = \begingroupcatcode
      \catcode`\}   = \endgroupcatcode
      \catcode`\^^L = \lettercatcode    % form feed
      \catcode`\^^M = \lettercatcode    % eol
  }
\egroup
%D \stopdocsection

%D \startdocsection[title=Setups]
%D \startparagraph
%D Once the proper catcodes are in place, the setup macro
%D \texmacro{do_setup_enigma} doesn’t to anything besides passing stuff
%D through to Lua.
%D \stopparagraph
\def\do_setup_enigma#1{%
    \directlua{
      local enigma = packagedata.enigma
      local current_args = enigma.parse_args(\!!bs\detokenize{#1}\!!es)
      enigma.save_raw_args(current_args, \!!bs\current_enigma_id\!!es)
      enigma.new_callback(
        enigma.new_machine(\!!bs\current_enigma_id\!!es),
        \!!bs\current_enigma_id\!!es)
    }%
  \egroup%
}

%D The module setup \texmacro{setupenigma} expects key=value, notation.
%D All the logic is at the Lua end, not much to see here …
\def\setupenigma#1{%
  \bgroup
    \edef\current_enigma_id{#1}
    \luatexcatcodetable \enigmasetupcatcodes
    \do_setup_enigma%
}
%D \stopdocsection

%D \startdocsection[title=Encoding Macros]
%D \startparagraph
%D The environment of \texmacro{begin<enigmaid>} and
%D \texmacro{end<enigmaid>} toggles Enigma encoding.
%D (Regarding environment delimiters we adhere to Knuth’s
%D practice of prefixing with \type{begin}/\type{end}.)
%D \stopparagraph

\def\e!start{begin} %{start}
\def \e!stop{end}   %{stop}
\edef\c!pre_linebreak_filter{pre_linebreak_filter}
\def\do_define_enigma#1{%
  \@EA\gdef\csname \e!start\current_enigma_id\endcsname{%
    \endgraf
    \bgroup%
    \directlua{%
      if packagedata.enigma                         and
         packagedata.enigma.machines[ \!!bs#1\!!es] then
        luatexbase.add_to_callback(
          \!!bs\c!pre_linebreak_filter\!!es,
          packagedata.enigma.callbacks[ \!!bs#1\!!es],
          \!!bs#1\!!es)
      else
        print\!!bs ENIGMA: No machine of that name: #1!\!!es
        os.exit()
      end
    }%
  }%
  \@EA\gdef\csname \e!stop\current_enigma_id\endcsname{%
    \endgraf
    \directlua{
      luatexbase.remove_from_callback(
        \!!bs\c!pre_linebreak_filter\!!es,
        \!!bs#1\!!es)
      packagedata.enigma.machines[ \!!bs#1\!!es]:processed_chars()
    }%
    \egroup%
  }%
}

\def\defineenigma#1{%
  \begingroup
  \let\@EA\expandafter
  \edef\current_enigma_id{#1}%
  \@EA\do_define_enigma\@EA{\current_enigma_id}%
  \endgroup%
}

%D \stopdocsection

\catcode`\_=8  % \popcatcodes
\catcode`\!=12 % reserved according to source2e
\ifenigmaisrunningplain\catcode`\@=12\fi
% vim:ft=plaintex:sw=2:ts=2:expandtab:tw=71