From b9ea02d6f07cd47186bf78aab36706da1b15ec53 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Wed, 15 May 2013 02:54:25 -0400
Subject: [PATCH] Added support for multiline comments (with some help from
 Taylor O'Connor and Jean-Philippe Ouellet).

---
 pd/src/g_rtext.c |  9 ++++++++-
 pd/src/g_text.c  | 37 +++++++++++++++++++++++++++++++++----
 pd/src/pd.tk     |  8 +++++---
 3 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/pd/src/g_rtext.c b/pd/src/g_rtext.c
index f406175f0..0de03b6ff 100644
--- a/pd/src/g_rtext.c
+++ b/pd/src/g_rtext.c
@@ -233,9 +233,15 @@ static void rtext_senditup(t_rtext *x, int action, int *widthp, int *heightp,
 			int maxindex_b = u8_offset(x->x_buf + inindex_b, maxindex_c);
 		    int eatchar = 1;
 			int foundit_b  = firstone(x->x_buf + inindex_b, '\n', maxindex_b);
+
+			//following deals with \v replacement for \n in multiline comments
+			int foundit_bv  = firstone(x->x_buf + inindex_b, '\v', maxindex_b);
+			//fprintf(stderr,"%d %d <%s>\n", foundit_b, foundit_bv, x->x_buf);
+			if ((foundit_bv < foundit_b && foundit_bv != -1) || (foundit_b == -1 && foundit_bv != -1)) foundit_b = foundit_bv;
+
 			int foundit_c;
 			if (foundit_b < 0)
-		    {
+		    { 
 		        if (inchars_c > widthlimit_c)
 		        {
 					foundit_b = lastone(x->x_buf + inindex_b, ' ', maxindex_b);
@@ -486,6 +492,7 @@ void rtext_select(t_rtext *x, int state)
 
 void rtext_activate(t_rtext *x, int state)
 {
+    //fprintf(stderr, "rtext_activate\n");
     int w = 0, h = 0, indx;
     t_glist *glist = x->x_glist;
     t_canvas *canvas = glist_getcanvas(glist);
diff --git a/pd/src/g_text.c b/pd/src/g_text.c
index c9f512ba1..9f7cd736f 100644
--- a/pd/src/g_text.c
+++ b/pd/src/g_text.c
@@ -60,7 +60,9 @@ void glist_text(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
     {
         x->te_xpix = atom_getfloatarg(0, argc, argv);
         x->te_ypix = atom_getfloatarg(1, argc, argv);
-        if (argc > 2) binbuf_restore(x->te_binbuf, argc-2, argv+2);
+        if (argc > 2) {
+            binbuf_restore(x->te_binbuf, argc-2, argv+2);
+        }
         else
         {
             SETSYMBOL(&at, gensym("comment"));
@@ -1573,14 +1575,35 @@ void text_save(t_gobj *z, t_binbuf *b)
             label, symfrom, symto);
         binbuf_addv(b, ";");
     }           
-    else        
+    else    
     {
-		//fprintf(stderr, "text\n");
+        //fprintf(stderr,"comment\n");
+        int natom = binbuf_getnatom(x->te_binbuf);
+        t_atom *a = binbuf_getvec(x->te_binbuf);
+        int i;
+        for (i = 0; i < natom; i++) {
+            t_symbol *s;
+            if(a[i].a_type == A_SYMBOL) {
+                //fprintf(stderr,"%d is a symbol\n", i);
+                s = a[i].a_w.w_symbol;
+                if (s != NULL && s->s_name != NULL) {
+                    //fprintf(stderr,"s != NULL\n");
+                    char *c;
+                    for(c = s->s_name; c != NULL && *c != '\0'; c++) {
+                        if(*c == '\n') {
+                            *c = '\v';
+                            //fprintf(stderr,"n->v\n");
+                        }
+                    }
+                }
+            }
+        }
+
         binbuf_addv(b, "ssii", gensym("#X"), gensym("text"),
             (int)x->te_xpix, (int)x->te_ypix);
         binbuf_addbinbuf(b, x->te_binbuf);
         binbuf_addv(b, ";");
-    }           
+    }
 }
 
     /* this one is for everyone but "gatoms"; it's imposed in m_class.c */
@@ -2024,6 +2047,12 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
                 canvas_updatewindowlist();
     }
     else {
+        char * c;
+        for(c = buf; *c != '\0'; c++) {
+            if(*c == '\n') {
+                *c = '\v';
+            }
+        }
 		binbuf_gettext(x->te_binbuf, &c1, &i1);
 		t_binbuf *b = binbuf_new();
 		binbuf_text(b, buf, bufsize);
diff --git a/pd/src/pd.tk b/pd/src/pd.tk
index 47b4f3c45..df8d4e610 100644
--- a/pd/src/pd.tk
+++ b/pd/src/pd.tk
@@ -3678,7 +3678,7 @@ proc pdtk_canvas_getscroll {name} {
 	set ::update_tick([winfo parent $name]) 1
     pdtk_canvas_checkgeometry [canvastosym $name]
 	#puts stderr "END top-left x:[$name canvasx 0] y:[$name canvasy 0]"
-    #update (bad idea--all kinds of unpredicted problems)
+    #update (bad idea--all kinds of unpredictable problems)
 
 }
 
@@ -6064,8 +6064,9 @@ proc pdtk_text_new {canvasname myname x y text font color} {
 #        36 { set typeface [lindex $pd_fontlist 9] }
 #    }
 	#[encoding convertfrom utf-8 $text]
+	set print_text [string map { \v \n } $text]
     $canvasname create text $x $y -font [get_font_for_size $font] \
-        -tags $myname -text [encoding convertfrom utf-8 $text] -fill $color -anchor nw
+        -tags $myname -text [encoding convertfrom utf-8 $print_text] -fill $color -anchor nw
 	$canvasname addtag text withtag $myname
 	#$canvasname bind $myname <Home> [concat $canvasname icursor $myname 0]
 	#$canvasname bind $myname <End> [concat $canvasname icursor $myname end]
@@ -6080,7 +6081,8 @@ proc pdtk_text_new {canvasname myname x y text font color} {
 
 ################ pdtk_text_set -- change the text ##################
 proc pdtk_text_set {canvasname myname text} {
-    catch {$canvasname itemconfig $myname -text [encoding convertfrom utf-8 $text]}
+    set print_text [string map { \v \n } $text]
+    catch {$canvasname itemconfig $myname -text [encoding convertfrom utf-8 $print_text]}
     #    pd [concat $myname size [$canvasname bbox $myname] \;]
 }
 
-- 
GitLab