From 33bf971197079ec6dff97014016b91c4c8bc45fa Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Wed, 14 Dec 2016 09:32:32 +0100
Subject: [PATCH] Port pd-l2ork upstream commits from Dec 7, 2015 improving the
 handling of $ substitutions in GUI elements. Also fixes #195.

---
 pd/nw/dialog_canvas.html |  9 +++++++--
 pd/nw/dialog_gatom.html  | 31 ++++++++++++++++++++++++-------
 pd/nw/dialog_iemgui.html | 33 +++++++++++++++++++++++++++------
 pd/src/g_all_guis.c      |  4 ++--
 pd/src/g_array.c         | 17 ++++++++++-------
 5 files changed, 70 insertions(+), 24 deletions(-)

diff --git a/pd/nw/dialog_canvas.html b/pd/nw/dialog_canvas.html
index 37103c303..d2ecdf607 100644
--- a/pd/nw/dialog_canvas.html
+++ b/pd/nw/dialog_canvas.html
@@ -567,9 +567,14 @@ function apply() {
     for (i = 0; i < pd_garray_attrs.length; i++) {
         attrs = pd_garray_attrs[i];
         name = get_array_value("array_name", attrs);
-        if (name.slice(0, 1) === "$") {
-            name = "#" + name.slice(1);
+        var arr = name.split("");
+        for (var i = 0; i < arr.length; i++) {
+            if (arr[i] === "$" && i+1 < arr.length &&
+                arr[i+1] >= "0" && arr[i+1] <= "9") {
+                arr[i] = "#";
+            }
         }
+        name = arr.join("");
         pdgui.pdsend(
             get_array_value("array_gfxstub", attrs),
             "arraydialog",
diff --git a/pd/nw/dialog_gatom.html b/pd/nw/dialog_gatom.html
index 4674bda38..d7732a315 100644
--- a/pd/nw/dialog_gatom.html
+++ b/pd/nw/dialog_gatom.html
@@ -177,15 +177,16 @@ function strip_problem_chars(arg) {
 }
 
 function gatom_escape(str) {
-    var arr, i, ret;
+    var ret;
     if (str.length === 0) {
         ret = "-";
     } else if (str.slice(0,1) === "-") {
         ret = "-" + str;
     } else {
-        arr = str.split("");
-        for (i = 0; i < arr.length; i++) {
-            if (arr[i] === "$") {
+        var arr = str.split("");
+        for (var i = 0; i < arr.length; i++) {
+            if (arr[i] === "$" && i+1 < arr.length &&
+                arr[i+1] >= "0" && arr[i+1] <= "9") {
                 arr[i] = "#";
             }
         }
@@ -194,6 +195,22 @@ function gatom_escape(str) {
     return strip_problem_chars(ret);
 }
 
+function gatom_unescape(str) {
+    if (str === "-") {
+        str = "";
+    } else {
+        var arr = str.split("");
+        for (var i = 0; i < arr.length; i++) {
+            if (arr[i] === "#" && i+1 < arr.length &&
+                arr[i+1] >= "0" && arr[i+1] <= "9") {
+                arr[i] = "$";
+            }
+        }
+        str = arr.join("");
+    }
+    return str;
+}
+
 function update_attr(elem) {
     new_attrs[elem.name] = elem.value;
 }
@@ -306,11 +323,11 @@ function populate_form(attributes) {
     get_elem("draglo").value = attributes.draglo;
     get_elem("draghi").value = attributes.draghi;
     label = attributes.label;
-    get_elem("label").value = label === "-" ? "" : label;
+    get_elem("label").value = gatom_unescape(label);
     snd = attributes.send_symbol;
-    get_elem("send_symbol").value = snd === "-" ? "" : snd;
+    get_elem("send_symbol").value = gatom_unescape(snd);
     rcv = attributes.receive_symbol;
-    get_elem("receive_symbol").value = rcv === "-" ? "" : rcv;
+    get_elem("receive_symbol").value = gatom_unescape(rcv);
 
     labelpos = attributes.labelpos;
     radios = document.getElementsByName("labelpos");
diff --git a/pd/nw/dialog_iemgui.html b/pd/nw/dialog_iemgui.html
index 7825ffa8f..84e1cae08 100644
--- a/pd/nw/dialog_iemgui.html
+++ b/pd/nw/dialog_iemgui.html
@@ -371,16 +371,33 @@ function update_attr(elem) {
 }
 
 //Clean up strings to send as symbol arguments to Pd
-function pd_symbol_carwash(s) {
+function iemgui_escape(s) {
     s = !s ? "empty" : s;
-    if (s.charAt(0) === "$") {
-        s = "#" + s.slice(1);
+    var arr = s.split("");
+    for (var i = 0; i < arr.length; i++) {
+        if (arr[i] === "$" && i+1 < arr.length &&
+            arr[i+1] >= "0" && arr[i+1] <= "9") {
+            arr[i] = "#";
+        }
     }
+    s = arr.join("");
     s = substitute_space(s);
     s = strip_problem_chars(s);
     return s;
 }
 
+function iemgui_unescape(s) {
+    var arr = s.split("");
+    for (var i = 0; i < arr.length; i++) {
+        if (arr[i] === "#" && i+1 < arr.length &&
+            arr[i+1] >= "0" && arr[i+1] <= "9") {
+            arr[i] = "$";
+        }
+    }
+    s = arr.join("");
+    return s;
+}
+
 function send_params(attrs, create_undo_point) {
     /* Not sure what these are...
         iemgui_clip_dim $id
@@ -394,9 +411,9 @@ function send_params(attrs, create_undo_point) {
     var send_symbol = attrs.send_symbol,
         receive_symbol = attrs.receive_symbol,
         label =  attrs["label"];
-    send_symbol = pd_symbol_carwash(send_symbol);
-    receive_symbol = pd_symbol_carwash(receive_symbol);
-    label = pd_symbol_carwash(label);
+    send_symbol = iemgui_escape(send_symbol);
+    receive_symbol = iemgui_escape(receive_symbol);
+    label = iemgui_escape(label);
 
     var label_x_offset =  attrs.x_offset;
     var label_y_offset =  attrs.y_offset;
@@ -605,6 +622,10 @@ function populate_form(attr_object) {
                     elem[0].checked = +attr_object[attr];
                 } else if (elem[0].type === "select-one") {
                     elem[0].selectedIndex = +attr_object[attr];
+                } else if (attr === "send_symbol" ||
+			   attr === "receive_symbol" ||
+			   attr === "label") {
+                    elem[0].value = iemgui_unescape(attr_object[attr]);
                 } else {
                     elem[0].value = attr_object[attr];
                 }
diff --git a/pd/src/g_all_guis.c b/pd/src/g_all_guis.c
index 80640fc7b..e97014b93 100644
--- a/pd/src/g_all_guis.c
+++ b/pd/src/g_all_guis.c
@@ -53,7 +53,7 @@ t_symbol *iemgui_dollar2raute(t_symbol *s)
         return (s);
     for (s1 = s->s_name, s2 = buf; ; s1++, s2++)
     {
-        if (*s1 == '$')
+        if (*s1 == '$' && *s1 && isdigit(s1[1]))
             *s2 = '#';
         else if (!(*s2 = *s1))
             break;
@@ -68,7 +68,7 @@ t_symbol *iemgui_raute2dollar(t_symbol *s)
         return (s);
     for (s1 = s->s_name, s2 = buf; ; s1++, s2++)
     {
-        if (*s1 == '#')
+        if (*s1 == '#' && *s1 && isdigit(s1[1]))
             *s2 = '$';
         else if (!(*s2 = *s1))
             break;
diff --git a/pd/src/g_array.c b/pd/src/g_array.c
index 2706f685e..91fb0e902 100644
--- a/pd/src/g_array.c
+++ b/pd/src/g_array.c
@@ -8,6 +8,7 @@
 #include "m_pd.h"
 #include "g_canvas.h"
 #include <math.h>
+#include <ctype.h>
 
 extern int glob_lmclick;
 
@@ -24,15 +25,17 @@ two, but how? */
 
 t_symbol *sharptodollar(t_symbol *s)
 {
-    if (*s->s_name == '#')
+    char buf[MAXPDSTRING], *s1, *s2;
+    if (strlen(s->s_name) >= MAXPDSTRING)
+        return (s);
+    for (s1 = s->s_name, s2 = buf; ; s1++, s2++)
     {
-        char buf[MAXPDSTRING];
-        strncpy(buf, s->s_name, MAXPDSTRING);
-        buf[MAXPDSTRING-1] = 0;
-        buf[0] = '$';
-        return (gensym(buf));
+        if (*s1 == '#' && *s1 && isdigit(s1[1]))
+            *s2 = '$';
+        else if (!(*s2 = *s1))
+            break;
     }
-    else return (s);
+    return (gensym(buf));
 }
 
 /* --------- "pure" arrays with scalars for elements. --------------- */
-- 
GitLab