Hi David,

Here is a better example. Instead of directlua, we register a function and call that one. The funny expandafters make sure we call them out-of-order. It works ok in luatex-plain. In fact, \directlua itself could be reimplemented as function but it is slightly slower then so we keep it as core primitive.

(Btw, some day soon I'm going to see if some backend primitives can be replaced by directlua + tokenscanner calls). The smaller the core and the more decoupled the backend is from the scanner, the better.)

\directlua{
    gadgets = gadgets or { } % reserved namespace

    gadgets.functions = { }
    local registered  = { }

    function gadgets.functions.reserve()
        local numb = newtoken.scan_int()
        local name = newtoken.scan_string()
        local okay = string.gsub(name,"[\string\\ ]","")
        registered[okay] = numb
texio.write_nl("reserving lua function '"..okay.."' with number "..numb)
    end

    function gadgets.functions.register(name,f)
        local okay = string.gsub(name,"[\string\\ ]","")
        local numb = registered[okay]
        if numb then
texio.write_nl("registering lua function '"..okay.."' with number "..numb)
            lua.get_functions_table()[numb] = f
        else
            texio.write_nl("lua function '"..okay.."' is not reserved")
        end
    end

}

\newcount\lastallocatedluafunction

\def\newluafunction#1%
  {\ifdefined#1\else
     \global\advance\lastallocatedluafunction 1
     \global\chardef#1\lastallocatedluafunction
     \directlua{gadgets.functions.reserve()}#1{\detokenize{#1}}%
   \fi}

\newluafunction\UcharcatLuaOne
\newluafunction\UcharcatLuaTwo

\directlua {

    local cct = nil
    local chr = nil

    gadgets.functions.register("UcharcatLuaOne",function()
        chr = newtoken.scan_int()
        cct = tex.getcatcode(chr)
        tex.setcatcode(chr,newtoken.scan_int())
        tex.sprint(unicode.utf8.char(chr))
    end)

    gadgets.functions.register("UcharcatLuaTwo",function()
        tex.setcatcode(chr,cct)
    end)

}

\def\Ucharcat
  {\expandafter\expandafter\expandafter\luafunction
   \expandafter\expandafter\expandafter\UcharcatLuaTwo
   \luafunction\UcharcatLuaOne}

A:\the\catcode65:\Ucharcat 65 11:A:\the\catcode65\par
A:\the\catcode65:\Ucharcat 65  5:A:\the\catcode65\par
A:\the\catcode65:\Ucharcat 65 11:A:\the\catcode65\par

Hans

-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
    tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                             | www.pragma-pod.nl
-----------------------------------------------------------------

Reply via email to