ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Accessing inserts from Lua in LuaMetaTeX
@ 2022-08-15  6:18 Max Chernoff via ntg-context
  2022-08-15 14:24 ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 3+ messages in thread
From: Max Chernoff via ntg-context @ 2022-08-15  6:18 UTC (permalink / raw)
  To: ntg-context; +Cc: Max Chernoff

Hi all,

I'm trying to manipulate some inserts from Lua in LuaMetaTeX, and I'm
having some problems that I'm not having with LuaTeX. 

First, how do I get an insert's class/type from the "insert" nodes on
the page? With LuaTeX, the insert's class/type is the same as the
subtype of the "ins" nodes, but the subtype of the "insert" nodes is
always zero in LuaMetaTeX, so I'm not sure how to get the class/type.

Second, how do I get the head of the insert box from Lua? With LuaTeX,
from TeX you would use "\box<class>" and from Lua you can use
"tex.box[<class>]". With LuaMetaTeX, you use "\insertbox<class>" from
TeX, but I'm not sure how to get at the insert box from Lua.

The context for both of these is that my module lua-widow-control
sometimes moves a line containing footnotes to the next page, so I need
to do some surgery to make sure that the "footnote marks" stay with the
"footnote text". This is tricky but doable with LuaTeX, but I can't seem
to get it to work with the new inserts in LuaMetaTeX, hence the above
questions.

Thanks,
-- Max
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Accessing inserts from Lua in LuaMetaTeX
  2022-08-15  6:18 Accessing inserts from Lua in LuaMetaTeX Max Chernoff via ntg-context
@ 2022-08-15 14:24 ` Hans Hagen via ntg-context
  0 siblings, 0 replies; 3+ messages in thread
From: Hans Hagen via ntg-context @ 2022-08-15 14:24 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen, Max Chernoff

On 8/15/2022 8:18 AM, Max Chernoff via ntg-context wrote:
> Hi all,
> 
> I'm trying to manipulate some inserts from Lua in LuaMetaTeX, and I'm
> having some problems that I'm not having with LuaTeX.
> 
> First, how do I get an insert's class/type from the "insert" nodes on
> the page? With LuaTeX, the insert's class/type is the same as the
> subtype of the "ins" nodes, but the subtype of the "insert" nodes is
> always zero in LuaMetaTeX, so I'm not sure how to get the class/type.

Indeed, we don't use these boxes and registers because we run in 
\insertmode=2. This gives us more state info (i kept the old method 
around for now).

> Second, how do I get the head of the insert box from Lua? With LuaTeX,
> from TeX you would use "\box<class>" and from Lua you can use
> "tex.box[<class>]". With LuaMetaTeX, you use "\insertbox<class>" from
> TeX, but I'm not sure how to get at the insert box from Lua.

tex.getinsertdistance
tex.getinsertmultiplier
tex.getinsertlimit
tex.getinsertheight
tex.getinsertdepth
tex.getinsertwidth
tex.getinsertcontent
tex.setinsertdistance
tex.setinsertmultiplier
tex.setinsertlimit
tex.setinsertcontent

> The context for both of these is that my module lua-widow-control
> sometimes moves a line containing footnotes to the next page, so I need
> to do some surgery to make sure that the "footnote marks" stay with the
> "footnote text". This is tricky but doable with LuaTeX, but I can't seem
> to get it to work with the new inserts in LuaMetaTeX, hence the above
> questions.
The insert (etc) handler is more clever and flexible. So, for instance 
we can bubble up inserts. I think you do a preroll so in that case you 
can set the migrations related parameter to not bubble up.

\starttext

\holdingmigrations\numexpr1+2+4\relax

\setbox0\hbox{test \footnote{test} test}

\setbox2\vbox{\unhcopy0}

\holdingmigrations0

\setbox4\vbox{\unhcopy0}

\unhbox0

\unvbox2

\unvbox4

\stoptext

But I have no clue how all that will work out with (lua) code that 
manipulates these things. In list nodes you can consult the pre and post 
fields to what got migrated.

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Accessing inserts from Lua in LuaMetaTeX
@ 2022-08-16  1:56 Max Chernoff via ntg-context
  0 siblings, 0 replies; 3+ messages in thread
From: Max Chernoff via ntg-context @ 2022-08-16  1:56 UTC (permalink / raw)
  To: ntg-context; +Cc: Max Chernoff


Hi Hans,

> > First, how do I get an insert's class/type from the "insert" nodes on
> > the page? With LuaTeX, the insert's class/type is the same as the
> > subtype of the "ins" nodes, but the subtype of the "insert" nodes is
> > always zero in LuaMetaTeX, so I'm not sure how to get the class/type.

After playing around, I found that this is stored in "<insert
node>.index". I'm able to write to this value just fine, but whenever I
read it I get an error.

This minimal example:

   \starttext
       \setbox999=\vbox{\hrule width 10pt height 10pt}
   
       \startluacode
           local insert = node.new("insert")
           local rule = tex.box[999].list
           insert.list = rule
           insert.index = 4
   
           node.write(insert)
       \stopluacode
   \stoptext
   
works perfectly, but this example:

   \startluacode
       local insert = node.new "insert"
       insert.index = 4
       print(insert.index)
   \stopluacode
   
gives this error message:

   In \insertmode 2 you can't use zero as index.
   
I suspect that this is a bug, but I may just be doing something wrong.

> Indeed, we don't use these boxes and registers because we run in 
> \insertmode=2. This gives us more state info (i kept the old method 
> around for now).

Inserts always felt a little hacky in TeX, so I think that the new
methods are a huge improvement.

> > Second, how do I get the head of the insert box from Lua? With LuaTeX,
> > from TeX you would use "\box<class>" and from Lua you can use
> > "tex.box[<class>]". With LuaMetaTeX, you use "\insertbox<class>" from
> > TeX, but I'm not sure how to get at the insert box from Lua.
> 
> tex.getinsertdistance
> tex.getinsertmultiplier
> tex.getinsertlimit
> tex.getinsertheight
> tex.getinsertdepth
> tex.getinsertwidth
> tex.getinsertcontent
> tex.setinsertdistance
> tex.setinsertmultiplier
> tex.setinsertlimit
> tex.setinsertcontent

Perfect! "tex.getinsertcontent" does exactly what I need.

Really minor, but I would expect that

   tex.insertcontent[4] = <some node>
   <some node> = tex.insertcontent[4]
   
would be exist and be equivalent to

   <some node> = tex.getinsertcontent(4)
   tex.setinsertcontent(4, <some node>)

> The insert (etc) handler is more clever and flexible. So, for instance
> we can bubble up inserts. I think you do a preroll so in that case you
> can set the migrations related parameter to not bubble up.
> 
> \starttext
> 
> \holdingmigrations\numexpr1+2+4\relax
> 
> \setbox0\hbox{test \footnote{test} test}
> 
> \setbox2\vbox{\unhcopy0}
> 
> \holdingmigrations0
> 
> \setbox4\vbox{\unhcopy0}
> 
> \unhbox0
> 
> \unvbox2
> 
> \unvbox4
> 
> \stoptext
> 
> But I have no clue how all that will work out with (lua) code that 
> manipulates these things. In list nodes you can consult the pre and post 
> fields to what got migrated.

I don't think that that will work in this case. I'm doing most of the
work in "pre_output_filter", which is after all of the insert nodes have
been removed from the page and put into their respective boxes. And I
can't postpone setting any inserts until then because TeX needs to know
their height for when it breaks the page. I don't really like how I have
to move the footnotes, but I can't find any better way of doing things.

If you're curious, most of the insert code is in these two functions:

   
   https://github.com/gucci-on-fleek/lua-widow-control/blob/2bca90/source/lua-widow-control.lua#L612-L669
   https://github.com/gucci-on-fleek/lua-widow-control/blob/2bca90/source/lua-widow-control.lua#L843-L924

Thanks,
-- Max

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-16  1:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15  6:18 Accessing inserts from Lua in LuaMetaTeX Max Chernoff via ntg-context
2022-08-15 14:24 ` Hans Hagen via ntg-context
2022-08-16  1:56 Max Chernoff via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).