summaryrefslogtreecommitdiff
path: root/tex/plain/enigma/enigma.tex
blob: 2b4fb8e2e27c3edaf8a8c994654f14cf618a7a6a (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
\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{start<enigmaid>} and
%D \texmacro{stop<enigmaid>} allow enabling of Enigma encoding in
%D different parts of the document.
%D \stopparagraph

\edef\c!pre_linebreak_filter{pre_linebreak_filter}
\def\do_define_enigma#1{%
  \@EA\gdef\csname 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 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=tex:sw=2:ts=2:expandtab:tw=71