>From 0683953c7caaac3a6c2be6224f5e59e24b3288ae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcel=20Kr=C3=BCger?= <tex@2krueger.de>
Date: Wed, 2 May 2018 21:54:42 +0200
Subject: [PATCH 2/2] Add `\scantexmacro`

---
 source/texk/web2c/luatexdir/lua/luatoken.w |  2 ++
 source/texk/web2c/luatexdir/tex/commands.h |  2 ++
 source/texk/web2c/luatexdir/tex/commands.w |  3 +++
 source/texk/web2c/luatexdir/tex/expand.w   | 16 ++++++++++------
 4 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/source/texk/web2c/luatexdir/lua/luatoken.w b/source/texk/web2c/luatexdir/lua/luatoken.w
index bd09c29ab..ec8cd2f1b 100644
--- a/source/texk/web2c/luatexdir/lua/luatoken.w
+++ b/source/texk/web2c/luatexdir/lua/luatoken.w
@@ -166,6 +166,8 @@ command_item command_names[] = {
     {"combinetoks", combine_toks_cmd, NULL},
     {"top_bot_mark", top_bot_mark_cmd, NULL},
     {"call", call_cmd, NULL},
+    {"scantexmacro", scantexmacro_cmd, NULL},
+    {"scanlongtexmacro", long_scantexmacro_cmd, NULL},
     {"long_call", long_call_cmd, NULL},
     {"outer_call", outer_call_cmd, NULL},
     {"long_outer_call", long_outer_call_cmd, NULL},
diff --git a/source/texk/web2c/luatexdir/tex/commands.h b/source/texk/web2c/luatexdir/tex/commands.h
index aabbe7f6b..89b75681e 100644
--- a/source/texk/web2c/luatexdir/tex/commands.h
+++ b/source/texk/web2c/luatexdir/tex/commands.h
@@ -214,6 +214,8 @@ typedef enum {
     the_cmd,                              /* expand an internal quantity ( \.{\\the} or \.{\\unexpanded}, \.{\\detokenize} ) */
     combine_toks_cmd,
     top_bot_mark_cmd,                     /* inserted mark ( \.{\\topmark}, etc.~) */
+    scantexmacro_cmd,
+    long_scantexmacro_cmd,
     call_cmd,                             /* non-long, non-outer control sequence */
     long_call_cmd,                        /* long, non-outer control sequence */
     outer_call_cmd,                       /* non-long, outer control sequence */
diff --git a/source/texk/web2c/luatexdir/tex/commands.w b/source/texk/web2c/luatexdir/tex/commands.w
index f2ef02b8f..5678c6bed 100644
--- a/source/texk/web2c/luatexdir/tex/commands.w
+++ b/source/texk/web2c/luatexdir/tex/commands.w
@@ -847,6 +847,9 @@ void initialize_etex_commands(void)
     primitive_etex("scantokens", input_cmd, 2, 0);
     primitive_luatex("scantextokens", input_cmd, 3, 0);
 
+    primitive_luatex("scantexmacro", scantexmacro_cmd, 0, 0);
+    primitive_luatex("scanlongtexmacro", long_scantexmacro_cmd, 0, 0);
+
     primitive_etex("readline", read_to_cs_cmd, 1, 0);
 
     primitive_etex("unless", expand_after_cmd, 1, 0);
diff --git a/source/texk/web2c/luatexdir/tex/expand.w b/source/texk/web2c/luatexdir/tex/expand.w
index e354358e5..2ff869d2b 100644
--- a/source/texk/web2c/luatexdir/tex/expand.w
+++ b/source/texk/web2c/luatexdir/tex/expand.w
@@ -25,7 +25,7 @@
 @ Only a dozen or so command codes |>max_command| can possibly be returned by
 |get_next|; in increasing order, they are |undefined_cs|, |expand_after|,
 |no_expand|, |input|, |if_test|, |fi_or_else|, |cs_name|, |convert|, |the|,
-|top_bot_mark|, |lua_function|, |call|, |long_call|, |outer_call|, |long_outer_call|, and
+|top_bot_mark|, |lua_function|, |scantexmacro|, |scanlongtexmacro|, |call|, |long_call|, |outer_call|, |long_outer_call|, and
 |end_template|.{\emergencystretch=40pt\par}
 
 Sometimes, recursive calls to the following |expand| routine may
@@ -79,7 +79,7 @@ void expand(void)
     co_backup = cur_order;
     backup_backup = token_link(backup_head);
   RESWITCH:
-    if (cur_cmd < call_cmd) {
+    if (cur_cmd < scantexmacro_cmd) {
         /* Expand a nonmacro */
         if (tracing_commands_par > 1)
             show_cur_cmd_chr();
@@ -436,7 +436,7 @@ void get_x_token(void)
     get_next();
     if (cur_cmd <= max_command_cmd)
         goto DONE;
-    if (cur_cmd >= call_cmd) {
+    if (cur_cmd >= scantexmacro_cmd) {
         if (cur_cmd < end_template_cmd) {
             macro_call();
         } else {
@@ -570,6 +570,12 @@ void macro_call(void)
     halfword save_warning_index = warning_index;        /* |warning_index| upon entry */
     int match_chr = 0;          /* character used in parameter */
     warning_index = cur_cs;
+    if (cur_cmd <= long_scantexmacro_cmd) {
+      ref_count = def_ref;
+      q = scan_toks(true, false);
+      cur_chr = def_ref;
+      def_ref = ref_count;
+    }
     ref_count = cur_chr;
     r = token_link(ref_count);
     if (tracing_macros_par > 0) {
@@ -598,9 +604,7 @@ void macro_call(void)
 
         scanner_status = matching;
         unbalance = 0;
-        long_state = eq_type(cur_cs);
-        if (long_state >= outer_call_cmd)
-            long_state = long_state - 2;
+        long_state = (eq_type(cur_cs) - scantexmacro_cmd) % 2 + call_cmd;
         do {
             set_token_link(temp_token_head, null);
             if ((token_info(r) >= end_match_token)
-- 
2.17.0

