Josef-Friedrich / nodetree

LuaTeX package to visualize node lists in a tree view.
https://www.ctan.org/pkg/nodetree
LaTeX Project Public License v1.3c
15 stars 2 forks source link

Please add 'pre' and 'post' hooks #16

Closed lemzwerg closed 1 year ago

lemzwerg commented 1 year ago

Right now, if I select, say, the hyph hook, 'nodetree' completely replaces any existing hooks with its own. This makes it inconvenient to actually watch what already installed hooks (or LuaTeX's default actions) actually do.

I thus suggest a new feature to improve this: If I add a prefix pre- or post- to a hook name, nodetree's hook gets installed as the very first or the very last, respectively. For example, adding both pre-hyph and post-hyph would allow me closely watch what LuaTeX's default hyphenation handler is doing.

Josef-Friedrich commented 1 year ago

If the luatexbase module is available, nodetree uses luatexbase to register its callbacks. Using luatexbase it is possible to register multiple callbacks.

In plain LuaTex you can load luatexbase using \input ltluatex.tex

https://github.com/Josef-Friedrich/nodetree/blob/d613470202cfc32b8a6cd9bc747b2ffa48af8e48/nodetree.lua#L1551-L1560

lemzwerg commented 1 year ago

Yes, I could do this on the lua side. However, it would be nice to make 'nodetree' do something like the following pseudo-code if I call \NodetreeRegisterCallback.

<get original hook>
if <pre>
  new_hook = function, which calls nodetree hook then original hook
else if <post>
  new_hook = function, which calls original hook then nodetree hook
else
  new_hook = nodetree hook
lemzwerg commented 1 year ago

By the way, the newest version of luatexbase.add_to_callback (as distributed with the core LaTeX stuff) allows to install stuff 'before' and 'after' hooks...

Josef-Friedrich commented 1 year ago

... the newest version of luatexbase.add_to_callback allows to install stuff 'before' and 'after' hooks...

Are you sure? As I understand it, you can only change the order of the registered user functions.

http://mirrors.ctan.org/macros/latex/base/ltluatex.pdf

luatexbase.declare_callback_rule(⟨name⟩, ⟨first⟩, ⟨relation⟩, ⟨second ⟩): relation = before | after

Josef-Friedrich commented 1 year ago
<get original hook>
if <pre>
  new_hook = function, which calls nodetree hook then original hook
else if <post>
  new_hook = function, which calls original hook then nodetree hook
else
  new_hook = nodetree hook

thanks for your thoughts in pseudo code form. I don't know how to get the original hook? Is the original hyphenate algorithm not written in C? If your registered hyphenate hook returns false, then the internal hyphenate algorithm is disabled.

From the LuaTeX manual:

No return values. This callback has to insert discretionary nodes in the node list it receives. Setting this callback to false will prevent the internal discretionary insertion pass.

lemzwerg commented 1 year ago

I'm a LuaTeX newbie, so I'm not sure at all and just poke in the dark. Perhaps I got this wrong. It probably makes sense to discuss this on the LuaTeX mailing list...

Josef-Friedrich commented 1 year ago

I have drawn a diagram that visualizes in what order the callbacks are called: Callbacks graph

lemzwerg commented 1 year ago

This looks great, but I don't understand it at all. Please add a legend and some more documentation.

Josef-Friedrich commented 1 year ago

I tried to visualize in which order the callbacks are executed. The ligaturing hook is always called after the hyphenate hook. But before the hyphenate hook come many different hooks.

lemzwerg commented 1 year ago

OK. However, some steps need more explanation IMHO.

lemzwerg commented 1 year ago

The wanted functionality is now available by using leading and trailing colons to the callback name (commit ac67fe792bb37b51c21f7629e7f4f2fa4933b91b and some follow-up fixes until a52c7560a6efdd3e6abbb79867c3e2ba4670d8a9).