From c9fcf97bc045a9d020980c33b6c7c1ee5aedef99 Mon Sep 17 00:00:00 2001
From: Norbert Preining <norbert@preining.info>
Date: Mon, 20 Sep 2021 03:02:23 +0000
Subject: CTAN sync 202109200302

---
 macros/luatex/generic/luakeys/luakeys-debug.tex | 35 ++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

(limited to 'macros/luatex/generic/luakeys/luakeys-debug.tex')

diff --git a/macros/luatex/generic/luakeys/luakeys-debug.tex b/macros/luatex/generic/luakeys/luakeys-debug.tex
index 499b1305f6..b63e67e050 100644
--- a/macros/luatex/generic/luakeys/luakeys-debug.tex
+++ b/macros/luatex/generic/luakeys/luakeys-debug.tex
@@ -20,14 +20,43 @@
   luakeys = require('luakeys')
 }
 
-\def\luakeysdebug#1{
+% https://tex.stackexchange.com/a/418401/42311
+\catcode`\@=11
+\long\def\LuaKeysIfNextChar#1#2#3{%
+  \let\@tmpa=#1%
+  \def\@tmpb{#2}%
+  \def\@tmpc{#3}%
+  \futurelet\@future\LuaKeysIfNextChar@i%
+}%
+\def\LuaKeysIfNextChar@i{%
+  \ifx\@tmpa\@future%
+    \expandafter\@tmpb
+  \else
+    \expandafter\@tmpc
+  \fi
+}%
+\def\luakeysdebug@parse@options#1{
+  \directlua{
+    luakeys.save('debug_options', luakeys.parse('#1'))
+  }
+}%
+\def\luakeysdebug@output#1{
   {
     \tt
     \parindent=0pt
     \directlua{
-      local result = luakeys.parse('#1')
+      local result = luakeys.parse('\luaescapestring{\unexpanded{#1}}', luakeys.get('debug_options'))
       tex.print(luakeys.stringify(result, true))
       luakeys.print(result)
     }
   }
-}
+}%
+\def\luakeysdebug@oarg[#1]#2{%
+  \luakeysdebug@parse@options{#1}%
+  \luakeysdebug@output{#2}%
+}%
+\def\luakeysdebug@marg#1{%
+  \luakeysdebug@output{#1}%
+}%
+\def\luakeysdebug{\LuaKeysIfNextChar[{\luakeysdebug@oarg}{\luakeysdebug@marg}}%
+\catcode`\@=12
-- 
cgit v1.2.3

