>From caf513855b13c113290507c56fa5e8b495bea1b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcel=20Kr=C3=BCger?= <tex@2krueger.de>
Date: Wed, 2 May 2018 16:29:15 +0200
Subject: [PATCH 1/2] Add `\luafunctiondef`

---
 source/texk/web2c/luatexdir/lua/luatoken.w    |  1 +
 source/texk/web2c/luatexdir/tex/commands.h    |  2 +-
 source/texk/web2c/luatexdir/tex/commands.w    |  3 ++-
 source/texk/web2c/luatexdir/tex/expand.w      | 20 +++++++++++++++++-
 source/texk/web2c/luatexdir/tex/maincontrol.h | 21 ++++++++++---------
 source/texk/web2c/luatexdir/tex/maincontrol.w |  7 +++++++
 source/texk/web2c/luatexdir/tex/primitive.w   |  6 ++++++
 source/texk/web2c/luatexdir/tex/textoken.w    | 16 --------------
 8 files changed, 47 insertions(+), 29 deletions(-)

diff --git a/source/texk/web2c/luatexdir/lua/luatoken.w b/source/texk/web2c/luatexdir/lua/luatoken.w
index c26dc4e36..bd09c29ab 100644
--- a/source/texk/web2c/luatexdir/lua/luatoken.w
+++ b/source/texk/web2c/luatexdir/lua/luatoken.w
@@ -159,6 +159,7 @@ command_item command_names[] = {
     {"fi_or_else", fi_or_else_cmd, NULL},
     {"cs_name", cs_name_cmd, NULL},
     {"convert", convert_cmd, NULL},
+    {"lua_function", lua_function_cmd, NULL},
     {"variable", variable_cmd, NULL},
     {"feedback", feedback_cmd, NULL},
     {"the", the_cmd, NULL},
diff --git a/source/texk/web2c/luatexdir/tex/commands.h b/source/texk/web2c/luatexdir/tex/commands.h
index 006796918..aabbe7f6b 100644
--- a/source/texk/web2c/luatexdir/tex/commands.h
+++ b/source/texk/web2c/luatexdir/tex/commands.h
@@ -208,6 +208,7 @@ typedef enum {
     fi_or_else_cmd,                       /* delimiters for conditionals ( \.{\\else}, etc.~) */
     cs_name_cmd,                          /* make a control sequence from tokens ( \.{\\csname} ) */
     convert_cmd,                          /* convert to text ( \.{\\number}, \.{\\string}, etc.~) */
+    lua_function_cmd,                     /* call a lua function ( \.{\\luafunction} )  */
     variable_cmd,
     feedback_cmd,
     the_cmd,                              /* expand an internal quantity ( \.{\\the} or \.{\\unexpanded}, \.{\\detokenize} ) */
@@ -239,7 +240,6 @@ typedef enum {
 
 typedef enum {
     number_code = 0,            /* command code for \.{\\number} */
-    lua_function_code,          /* command code for \.{\\luafunction} */
     lua_code,                   /* command code for \.{\\directlua} */
     expanded_code,              /* command code for \.{\\expanded} */
     math_style_code,            /* command code for \.{\\mathstyle} */
diff --git a/source/texk/web2c/luatexdir/tex/commands.w b/source/texk/web2c/luatexdir/tex/commands.w
index 3d781dc0e..f2ef02b8f 100644
--- a/source/texk/web2c/luatexdir/tex/commands.w
+++ b/source/texk/web2c/luatexdir/tex/commands.w
@@ -335,7 +335,7 @@ void initialize_commands(void)
     primitive_luatex("uniformdeviate", convert_cmd, uniform_deviate_code, 0);
     primitive_luatex("normaldeviate", convert_cmd, normal_deviate_code, 0);
     primitive_core("directlua", convert_cmd, lua_code, 0);
-    primitive_luatex("luafunction", convert_cmd, lua_function_code, 0);
+    primitive_luatex("luafunction", lua_function_cmd, 0, 0);
     primitive_luatex("luaescapestring", convert_cmd, lua_escape_string_code, 0);
     primitive_luatex("mathstyle", convert_cmd, math_style_code, 0);
     primitive_luatex("expanded", convert_cmd, expanded_code, 0);
@@ -533,6 +533,7 @@ void initialize_commands(void)
     primitive_tex("skipdef", shorthand_def_cmd, skip_def_code, 0);
     primitive_tex("muskipdef", shorthand_def_cmd, mu_skip_def_code, 0);
     primitive_tex("toksdef", shorthand_def_cmd, toks_def_code, 0);
+    primitive_luatex("luafunctiondef", shorthand_def_cmd, lua_function_def_code, 0);
     primitive_tex("catcode", def_char_code_cmd, cat_code_base, cat_code_base);
     primitive_tex("mathcode", def_char_code_cmd, math_code_base, cat_code_base);
     primitive_tex("lccode", def_char_code_cmd, lc_code_base, cat_code_base);
diff --git a/source/texk/web2c/luatexdir/tex/expand.w b/source/texk/web2c/luatexdir/tex/expand.w
index c912863b2..e354358e5 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|, |call|, |long_call|, |outer_call|, |long_outer_call|, and
+|top_bot_mark|, |lua_function|, |call|, |long_call|, |outer_call|, |long_outer_call|, and
 |end_template|.{\emergencystretch=40pt\par}
 
 Sometimes, recursive calls to the following |expand| routine may
@@ -69,6 +69,7 @@ void expand(void)
     int cvl_backup, radix_backup, co_backup;    /* to save |cur_val_level|, etc. */
     halfword backup_backup;     /* to save |link(backup_head)| */
     int save_scanner_status;    /* temporary storage of |scanner_status| */
+    str_number u = 0;           /* temporary string */
     incr(expand_depth_count);
     if (expand_depth_count >= expand_depth)
         overflow("expansion depth", (unsigned) expand_depth);
@@ -229,6 +230,23 @@ void expand(void)
                 manufacture_csname(1);
             }
             break;
+        case lua_function_cmd:
+            if (cur_chr == 0) {
+                scan_int();
+            } else {
+                cur_val = cur_chr;
+            }
+            if (cur_val <= 0) {
+                normal_error("luafunction", "invalid number");
+            } else {
+                u = save_cur_string();
+                luacstrings = 0;
+                luafunctioncall(cur_val);
+                restore_cur_string(u);
+                if (luacstrings > 0)
+                    lua_string_start();
+            }
+            break;
         case convert_cmd:
             conv_toks();        /* this procedure is discussed in Part 27 below */
             break;
diff --git a/source/texk/web2c/luatexdir/tex/maincontrol.h b/source/texk/web2c/luatexdir/tex/maincontrol.h
index 13e3c6ff3..b5c2a9b0b 100644
--- a/source/texk/web2c/luatexdir/tex/maincontrol.h
+++ b/source/texk/web2c/luatexdir/tex/maincontrol.h
@@ -101,16 +101,17 @@ sequence whose |cmd| is |assign_int| or \dots\ or |assign_mu_glue|, and the
 corresponding |chr| is the |eqtb| location of the internal register in question.
 */
 
-#  define char_def_code 0       /* |shorthand_def| for \.{\\chardef} */
-#  define math_char_def_code 1  /* |shorthand_def| for \.{\\mathchardef} */
-#  define xmath_char_def_code 2 /* |shorthand_def| for \.{\\Umathchardef} */
-#  define count_def_code 3      /* |shorthand_def| for \.{\\countdef} */
-#  define attribute_def_code 4  /* |shorthand_def| for \.{\\attributedef} */
-#  define dimen_def_code 5      /* |shorthand_def| for \.{\\dimendef} */
-#  define skip_def_code 6       /* |shorthand_def| for \.{\\skipdef} */
-#  define mu_skip_def_code 7    /* |shorthand_def| for \.{\\muskipdef} */
-#  define toks_def_code 8       /* |shorthand_def| for \.{\\toksdef} */
-#  define umath_char_def_code 9 /* |shorthand_def| for \.{\\Umathcharnumdef} */
+#  define char_def_code 0          /* |shorthand_def| for \.{\\chardef} */
+#  define math_char_def_code 1     /* |shorthand_def| for \.{\\mathchardef} */
+#  define xmath_char_def_code 2    /* |shorthand_def| for \.{\\Umathchardef} */
+#  define count_def_code 3         /* |shorthand_def| for \.{\\countdef} */
+#  define attribute_def_code 4     /* |shorthand_def| for \.{\\attributedef} */
+#  define dimen_def_code 5         /* |shorthand_def| for \.{\\dimendef} */
+#  define skip_def_code 6          /* |shorthand_def| for \.{\\skipdef} */
+#  define mu_skip_def_code 7       /* |shorthand_def| for \.{\\muskipdef} */
+#  define toks_def_code 8          /* |shorthand_def| for \.{\\toksdef} */
+#  define umath_char_def_code 9    /* |shorthand_def| for \.{\\Umathcharnumdef} */
+#  define lua_function_def_code 10 /* |shorthand_def| for \.{\\luafunctiondef} */
 
 extern void adjust_space_factor(void);
 extern void main_control(void);
diff --git a/source/texk/web2c/luatexdir/tex/maincontrol.w b/source/texk/web2c/luatexdir/tex/maincontrol.w
index af260cddc..395af18d7 100644
--- a/source/texk/web2c/luatexdir/tex/maincontrol.w
+++ b/source/texk/web2c/luatexdir/tex/maincontrol.w
@@ -2357,6 +2357,13 @@ void prefixed_command(void)
                 cur_val = (mval.class_value + (8 * mval.family_value)) * (65536 * 32) + mval.character_value;
                 define(p, xmath_given_cmd, cur_val);
                 break;
+            case lua_function_def_code:
+                scan_int();
+                if (cur_val <= 0) {
+                    normal_error("luafunctiondef", "invalid number");
+                }
+                define(p, lua_function_cmd, cur_val);
+                break;
             default:
                 scan_register_num();
                 switch (n) {
diff --git a/source/texk/web2c/luatexdir/tex/primitive.w b/source/texk/web2c/luatexdir/tex/primitive.w
index 8c6592c9e..91768b796 100644
--- a/source/texk/web2c/luatexdir/tex/primitive.w
+++ b/source/texk/web2c/luatexdir/tex/primitive.w
@@ -637,6 +637,12 @@ void print_cmd_chr(quarterword cmd, halfword chr_code)
             print_int(chr_code - scaled_base);
         }
         break;
+    case lua_function_cmd:
+        tprint_esc("luafunction");
+        if (chr_code != 0) {
+            print_int(chr_code);
+        }
+        break;
     case normal_cmd:
         if (chr_code < prim_data[cmd].subids && prim_data[cmd].names[chr_code] != 0) {
             prim_cmd_chr(cmd, chr_code);
diff --git a/source/texk/web2c/luatexdir/tex/textoken.w b/source/texk/web2c/luatexdir/tex/textoken.w
index d83c9df80..232863a99 100644
--- a/source/texk/web2c/luatexdir/tex/textoken.w
+++ b/source/texk/web2c/luatexdir/tex/textoken.w
@@ -2525,21 +2525,6 @@ void conv_toks(void)
             print_int(cur_val);
             pop_selector;
             break;
-        case lua_function_code:
-            scan_int();
-            if (cur_val <= 0) {
-                normal_error("luafunction", "invalid number");
-            } else {
-                u = save_cur_string();
-                luacstrings = 0;
-                luafunctioncall(cur_val);
-                restore_cur_string(u);
-                if (luacstrings > 0)
-                    lua_string_start();
-            }
-            /* no further action */
-            return;
-            break;
         case lua_code:
             u = save_cur_string();
             save_scanner_status = scanner_status;
@@ -2974,7 +2959,6 @@ str_number the_convert_string(halfword c, int i)
         case number_code:
             print_int(i);
             break;
-     /* case lua_function_code: */
      /* case lua_code: */
      /* case expanded_code: */
         case math_style_code:
-- 
2.17.0

