From b17163c6ef0c5d84456874f67d198e844e4d774f Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 23 Feb 2012 15:50:05 +0100 Subject: finally, Context-style macros; support for inheritance --- tex/context/third/enigma/enigma.lua | 50 +++++++++++++++++++++++++---- tex/context/third/enigma/t-enigma.mkvi | 58 +++++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 18 deletions(-) diff --git a/tex/context/third/enigma/enigma.lua b/tex/context/third/enigma/enigma.lua index 7eec8bc..ad23c2b 100644 --- a/tex/context/third/enigma/enigma.lua +++ b/tex/context/third/enigma/enigma.lua @@ -92,6 +92,23 @@ local utf8len = unicode.utf8.len local utf8sub = unicode.utf8.sub local utfcharacters = string.utfcharacters +local tablecopy +if format_is_context_p then + tablecopy = table.copy +else + tablecopy = function (t) -- ignores tables as keys + local result = { } + for k, v in next, t do + if type(v) == table then + result[k] = tablecopy(v) + else + result[k] = v + end + end + return result + end +end + local glyph_node = nodeid"glyph" local glue_node = nodeid"glue" @@ -1189,10 +1206,11 @@ on \dots\ many thanks to Khaled Hosny, who posted an answer to \stopparagraph --ichd]]-- -enigma.new_callback = function (machine, name) +local new_callback = function (machine, name) enigma.machines [name] = machine local format_is_context_p = format_is_context_p local cbk = function (a, _, c) + print(name, "<<<<<<<<<<<<<<<<<<<") local head = format_is_context_p and c or a for n in nodetraverse(head) do --print(node, node.id) @@ -1218,7 +1236,6 @@ enigma.new_callback = function (machine, name) noderemove(head, n) end end - --if context then return head, true end return head end if format_is_context_p then @@ -1229,20 +1246,41 @@ enigma.new_callback = function (machine, name) processor = cbk, } nodestasksappendaction("processors", - "characters", + --"characters", + -- this one is “for users” (cf. node-tsk.lua) + "before", "thirddata.enigma.callbacks." .. name) + nodes.tasks.disableaction("processors", + "thirddata.enigma.callbacks." .. name) else enigma.callbacks[name] = cbk end end ---local teststring = [[B I II III 01 01 01]] -enigma.new_machine = function (args, name) +local configurations = { } +local save_raw_args = function (conf, name) + local current = configurations[name] or { } + for k, v in next, conf do + current[k] = v + end + configurations[name] = current +end +local retrieve_raw_args = function (name) + local cn = configurations[name] + return cn and tablecopy(cn) or { } +end +enigma.save_raw_args = save_raw_args +enigma.retrieve_raw_args = retrieve_raw_args + +local new_machine = function (args, name) local machine = new(args.day_key, args.rotor_setting) machine.name = name verbose_level = args.verbose return machine -end --stub +end + +enigma.new_machine = new_machine +enigma.new_callback = new_callback ------------------------------------------------------------------------ diff --git a/tex/context/third/enigma/t-enigma.mkvi b/tex/context/third/enigma/t-enigma.mkvi index 7c01144..173bb07 100644 --- a/tex/context/third/enigma/t-enigma.mkvi +++ b/tex/context/third/enigma/t-enigma.mkvi @@ -49,12 +49,13 @@ thirddata = thirddata or { } %D on the internal mechanisms but implement our own macro generator. %D \stopparagraph -\def\do_define_enigma#id{% +\def\do_do_define_enigma#id{% + \edef\enigmaid{#id}% \expandafter\gdef\csname start\enigmaid\endcsname{% + \endgraf \bgroup \ctxlua{ if thirddata.enigma.machines["#id"] then - local callback = thirddata.enigma.callbacks["#id"], nodes.tasks.enableaction("processors", "thirddata.enigma.callbacks.#id") else @@ -64,6 +65,8 @@ thirddata = thirddata or { } }% % \expandafter\gdef\csname stop\enigmaid\endcsname{% + \endgraf%% We need to force a paragraph here for the callback to be + %% applied. \ctxlua{ nodes.tasks.disableaction("processors", "thirddata.enigma.callbacks.#id") @@ -73,28 +76,49 @@ thirddata = thirddata or { } }% } -\def\defineenigma#id{% - \begingroup - \edef\enigmaid{#id}% - \expandafter\do_define_enigma\expandafter{\enigmaid}% +\def\inherit_enigma#to#from{% + \ctxlua{% + local enigma = thirddata.enigma + local current_args = enigma.retrieve_raw_args(\!!bs#from\!!es) + enigma.new_callback( + enigma.new_machine(current_args, \!!bs#to\!!es), \!!bs#to\!!es + ) + }% + \do_do_define_enigma{#to}% +} + +\def\do_define_enigma[#id][#secondid]{% + \ifsecondargument %% Copy an existing machine and callback. + \inherit_enigma{#id}{#secondid}% + \else %% Create a new machine. + \iffirstargument + \do_do_define_enigma{#id}% + \else + \donothing + \fi + \fi \endgroup% } +\def\defineenigma{% + \begingroup + \dodoubleempty\do_define_enigma% +} + %D \stopdocsection %D \startdocsection[title=Setup] -\def\do_setup_enigma#args{% +\def\do_do_setup_enigma#args{% \ctxlua{ local enigma = thirddata.enigma local current_args = enigma.parse_args([====[\detokenize{#args}]====]) + enigma.save_raw_args(current_args, [====[\currentenigmaid]====]) enigma.new_callback( enigma.new_machine(current_args, [====[\currentenigmaid]====]), [====[\currentenigmaid]====]) }% - \popcatcodetable - \egroup% } %D \startparagraph @@ -102,12 +126,22 @@ thirddata = thirddata or { } %D All the logic is at the Lua end, not much to see here … %D \stopparagraph -\def\setupenigma#id{% - \bgroup +\def\do_setup_enigma[#id][#args]{% + \ifsecondargument \edef\currentenigmaid{#id} \pushcatcodetable \catcodetable \txtcatcodes - \do_setup_enigma% + \do_do_setup_enigma{#args}% + \else + \donothing + \fi + \popcatcodetable + \egroup% +} + +\def\setupenigma{% + \bgroup + \dodoubleempty\do_setup_enigma% } %D \stopdocsection -- cgit v1.2.3