diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 265b8a399ff41b80c072f912311a11a4efbfce06..d04d05f37a7134d949ca962ad61e9f41fa7013e0 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -92,17 +92,6 @@ function string_to_array_of_chunks(msg) {
     return out_array;
 }
 
-// Should probably be in pdgui.js
-function encode_for_dialog(s) {
-    s = s.replace(/\+/g, "++");
-    s = s.replace(/\s/g, "+_");
-    s = s.replace(/\$/g, "+d");
-    s = s.replace(/;/g, "+s");
-    s = s.replace(/,/g, "+c");
-    s = "+" + s;
-    return s;
-}
-
 // Super-simplistic guess at whether the string from the clipboard
 // starts with Pd code. This is just meant as a convenience so that
 // stuff in the copy buffer that obviously isn't Pd code doesn't get
@@ -371,7 +360,7 @@ var canvas_events = (function() {
                         pdgui.pdsend(name, "findagain");
                     } else {
                         pdgui.pdsend(name, "find",
-                        encode_for_dialog(t),
+                        pdgui.encode_for_dialog(t),
                         match_words_state ? "1" : "0");
                     }
                 }
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index dfc010884d484835885b5fba5e3d907ba21da5b8..7792ebe2f7472b99e7bafbeeea8738361b319325 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -459,6 +459,21 @@ var font_fixed_metrics = [
 
 // Utility Functions
 
+// This is used to escape spaces and other special delimiters in FUDI
+// arguments for dialogs. (The reverse function is sys_decodedialog() in the C
+// code.)
+function encode_for_dialog(s) {
+    s = s.replace(/\+/g, "++");
+    s = s.replace(/\s/g, "+_");
+    s = s.replace(/\$/g, "+d");
+    s = s.replace(/;/g, "+s");
+    s = s.replace(/,/g, "+c");
+    s = "+" + s;
+    return s;
+}
+
+exports.encode_for_dialog = encode_for_dialog;
+
 // originally used to enquote a string to send it to a tcl function
 function enquote (x) {
     var foo = x.replace(/,/g, "");