From 090acac281c4da09866a956d885c67a21cda134a Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@monsoon.(none)>
Date: Sat, 22 Oct 2011 07:05:23 -0400
Subject: [PATCH] Added universal copy and paste allowing importing of patches
 by copying and pasting the text directly into the patch. Still need to decide
 whether auto-resizing of the window is also desirable (there are both pros
 and cons).

---
 src/g_editor.c | 27 ++++++++++++++++++++++++++-
 src/m_pd.h     |  2 +-
 src/pd.tk      | 29 ++++++++++++++++++++++++-----
 3 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/src/g_editor.c b/src/g_editor.c
index 16abed5a4..38a4cbf28 100644
--- a/src/g_editor.c
+++ b/src/g_editor.c
@@ -48,6 +48,7 @@ static int outlet_issignal = 0;
 static int inlet_issignal = 0;
 static int last_inlet_filter = 0;
 static int last_outlet_filter = 0;
+static int copyfromexternalbuffer = 0;
 struct _outlet
 {
     t_object *o_owner;
@@ -3274,10 +3275,32 @@ static t_binbuf *canvas_docopy(t_canvas *x)
     return (b);
 }
 
+static void canvas_copyfromexternalbuffer(t_canvas *x, t_symbol *s, int ac, t_atom *av)
+{
+	if (!x->gl_editor)
+		return;
+	if (ac == 0) {
+		//fprintf(stderr,"init\n");
+		copyfromexternalbuffer = 1;
+		binbuf_free(copy_binbuf);
+		copy_binbuf = binbuf_new();
+	} else {
+		if (av[0].a_type == A_SYMBOL && strcmp(av[0].a_w.w_symbol->s_name, "#N")) {
+			//fprintf(stderr,"fill %d\n", ac);
+			binbuf_add(copy_binbuf, ac, av);
+			binbuf_addsemi(copy_binbuf);
+		} else {
+			//probably should resize window size here...
+			//fprintf(stderr,"ignoring canvas\n");
+		}
+	}
+}
+
 static void canvas_copy(t_canvas *x)
 {
     if (!x->gl_editor || !x->gl_editor->e_selection)
         return;
+	copyfromexternalbuffer = 0;
     binbuf_free(copy_binbuf);
 	//fprintf(stderr, "canvas_copy\n");
     copy_binbuf = canvas_docopy(x);
@@ -3558,7 +3581,7 @@ static void canvas_dopaste(t_canvas *x, t_binbuf *b)
 		//reset canvas_undo_already_set_move
 		canvas_undo_already_set_move = 0;
 	}
-	else if (canvas_undo_name && !strcmp(canvas_undo_name, "paste") ) {
+	else if (canvas_undo_name && !strcmp(canvas_undo_name, "paste") && !copyfromexternalbuffer) {
 		canvas_paste_atmouse(x);
 		//fprintf(stderr,"doing a paste\n");
 	}
@@ -4069,6 +4092,8 @@ void g_editor_setup(void)
         gensym("donecanvasdialog"), A_GIMME, A_NULL);
     class_addmethod(canvas_class, (t_method)glist_arraydialog,
         gensym("arraydialog"), A_GIMME, A_NULL);
+    class_addmethod(canvas_class, (t_method)canvas_copyfromexternalbuffer,
+        gensym("copyfromexternalbuffer"), A_GIMME, A_NULL);
 
 /* -------------- connect method used in reading files ------------------ */
     class_addmethod(canvas_class, (t_method)canvas_connect,
diff --git a/src/m_pd.h b/src/m_pd.h
index 81e440765..b3fadf248 100644
--- a/src/m_pd.h
+++ b/src/m_pd.h
@@ -11,7 +11,7 @@ extern "C" {
 #define PD_MAJOR_VERSION 0
 #define PD_MINOR_VERSION 42
 #define PD_BUGFIX_VERSION 5
-#define PD_TEST_VERSION "extended-l2ork-20110920"
+#define PD_TEST_VERSION "extended-l2ork-20111021"
 
 /* old name for "MSW" flag -- we have to take it for the sake of many old
 "nmakefiles" for externs, which will define NT and not MSW */
diff --git a/src/pd.tk b/src/pd.tk
index 38ade0c3c..c59da3fcb 100644
--- a/src/pd.tk
+++ b/src/pd.tk
@@ -1598,7 +1598,7 @@ proc menu_copy {name} {
 	if {![string match .gfxstub* $name] && ![string match .printout* $name] && ![string match *entry $name]} {
 	    pd [concat $name copy \;]
 		set copytexttocanvas 0
-		# clipboard clear
+		clipboard clear
 	} else {
 		# pdtk_post "this_is_text\n"
 		set copytexttocanvas 1
@@ -1612,12 +1612,12 @@ proc menu_paste {name} {
 	# pdtk_post "pasting..."
 	if {![string match .gfxstub* $name] && ![string match .printout* $name] && ![string match *entry $name]} {
 		# puts stderr "this is not an entry $copytexttocanvas\n"
-		if {$copytexttocanvas} {
-			pdtk_pastetext
-		} else {
+		# if {$copytexttocanvas} {
+		#	pdtk_pastetext
+		#} else {
 			pd [concat $name paste \;]
 			pdtk_canvas_getscroll $name.c
-		}
+		#}
 	} else {
 		# puts stderr "pasting text..."
 		pdtk_pastetext
@@ -1763,6 +1763,25 @@ proc menu_fixeditmenu {name} {
 	global global_selection
 	global global_clipboard
 
+	# investigate if we have already something copied from an external source
+	set existing_clipboard 0
+	catch {set existing_clipboard [clipboard get]}
+	if {$existing_clipboard != 0} {
+		set data [split $existing_clipboard "\n"]
+		set first 0
+		foreach line $data { 
+			if { !$first } {
+				pd [concat $name copyfromexternalbuffer\;]
+				pd [concat $name copyfromexternalbuffer $line\;]
+				set first 1
+			} else {
+				pd [concat $name copyfromexternalbuffer $line\;]
+			}
+		}
+		set global_clipboard 1
+		pdtk_canvas_update_edit_menu $name 1
+	}
+
     #    puts stderr [concat menu_fixeditmenu $name $pd_undocanvas $pd_undoaction]
     if {$name == $pd_undocanvas && $pd_undoaction != "no"} {
         $name.m.edit entryconfigure "Undo*" -state normal \
-- 
GitLab