为什么在处理“process_input_buffer”时“tex.sprint”会被延迟?

为什么在处理“process_input_buffer”时“tex.sprint”会被延迟?
%!TEX program=lualatex
\documentclass{article}

\begin{document}
\def\specialRead{\directlua{
    luatexbase.add_to_callback('process_input_buffer', function (line) 
        if line:find('^\\offSpecialRead') then return end
        tex.sprint("line: " .. line .. "$")
        return 'Sanitized line'
    end, 'special read')
}}%
\def\offSpecialRead{\directlua{
    luatexbase.remove_from_callback('process_input_buffer', 'special read')
}}%
\specialRead
a
b
\offSpecialRead c
d

\edef\e{\directlua{tex.sprint('')}f}
\verb|\e: [|\meaning\e\verb|]|
\end{document}

前一个tex.sprints 被推迟到tex.sprint的定义中的另一个 s \e

编译输出

答案1

我读完源代码后就明白了。

tex.sprint将字符串存储到列表中

#define  write_spindle spindles[spindle_index]
#define  read_spindle  spindles[(spindle_index-1)]
...
static int luac_store(lua_State * L, int i, int partial, int cattable)
{
    ...
    luacstrings++;
    rn = (rope *) xmalloc(sizeof(rope));
    rn->text = st;
    rn->tsize = (unsigned) tsize;
    rn->tok = tok;
    rn->nod = nod;
    rn->next = NULL;
    rn->partial = partial;
    rn->cattable = cattable;
    /* add */
    if (write_spindle.head == NULL) {
        write_spindle.head = rn;
    } else {
        write_spindle.tail->next = rn;
    }
    write_spindle.tail = rn;
    write_spindle.complete = 0;
    return 1;
}

主轴由 输入read_spindle,并且只有当 时才变为write_spindle。并且由 调用read_spindleluacstring_start()spindle_size++luacstring_start()lua_string_start(),它在主循环中执行完一段lua代码之后调用,每次调用之前都会检查luacstrings > 0

void lua_string_start(void)
{
    ...
    luacstring_start(iindex);
}

在这些lua代码之前该变量luacstrings被重置为0,并且仅在中被改变luac_store

与显式 不同\directlua{...tex.print(...)...},回调函数在主循环之外调用,因此lua_string_start()直到另一个显式 才会被调用tex.print

相关内容