From 1edfab8f63f62bcf7ed293af056e23ed89335d2e Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Wed, 18 Sep 2013 17:06:47 -0400
Subject: [PATCH] *refined recent documents logic (limiting it to the requested
 size, added files opened from the command line) *fixed buggy way of focusing
 new windows

---
 pd/src/pd.tk           | 16 +++++++++++-----
 pd/src/pd_guiprefs.tcl |  2 +-
 pd/src/s_main.c        |  5 ++++-
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/pd/src/pd.tk b/pd/src/pd.tk
index 5ac66af7b..891f6537e 100644
--- a/pd/src/pd.tk
+++ b/pd/src/pd.tk
@@ -3300,7 +3300,7 @@ proc pdtk_canvas_new {name width height geometry editable} {
 	bind $name <FocusIn> "menu_fixeditmenu $name"
 	# bind $name <FocusOut> { pdtk_noselect %W }
 	if { $k12_mode == 1 } { pd [concat $name tooltips 1 \;] }
-    after 100 [concat focus $name.c]
+    after idle [concat focus $name.c]
 
 	if { $k12_mode == 1 && $k12_saveas_on_new == 1 } {
 		after 1000 [concat pdtk_k12_saveas_on_new $name]
@@ -4354,7 +4354,6 @@ proc pdtk_canvas_sendkey {name state key iso shift focus serial} {
 
     # autorepeat logic
     if { $iso == "null" } { set iso "" }
-    if { $iso == "spc" } { set iso " " }
 	set autorepeat 1
 	if { $state == 1 } {
 		# press
@@ -4370,8 +4369,11 @@ proc pdtk_canvas_sendkey {name state key iso shift focus serial} {
 	} else {
 		# schedule release
 		set key_serial_release $serial
-		if { $iso == "" } { set iso null }
-		if { $iso == " " } { set iso spc }
+		if {$iso != ""} {
+        	scan $iso %c iso
+    	} else {
+			set iso "null"
+		}
 		after 0 [concat pdtk_canvas_sendkey $name $state $key $iso $shift $focus -1]
 		return
 	}
@@ -4398,7 +4400,11 @@ proc pdtk_canvas_sendkey {name state key iso shift focus serial} {
 		set key 127
     }
     if {$iso != ""} {
-        scan $iso %c key
+    	if {$serial != -1} {
+        	scan $iso %c key
+        } else {
+        	set key $iso
+        }
     }
 
 	if {$key == "Control_L"} {
diff --git a/pd/src/pd_guiprefs.tcl b/pd/src/pd_guiprefs.tcl
index f97d2b45e..6e7955feb 100644
--- a/pd/src/pd_guiprefs.tcl
+++ b/pd/src/pd_guiprefs.tcl
@@ -73,7 +73,7 @@ proc ::pd_guiprefs::update_recentfiles {afile save} {
     set ::recentfiles_list [lreplace $::recentfiles_list $index $index]
     # insert new one in the beginning and crop the list
     set ::recentfiles_list [linsert $::recentfiles_list 0 $afile]
-    set ::recentfiles_list [lrange $::recentfiles_list 0 $::total_recentfiles]
+    set ::recentfiles_list [lrange $::recentfiles_list 0 [expr $::total_recentfiles - 1]]
     #::pd_menus::update_recentfiles_menu .mbar.file $save
     ::pd_guiprefs::write_recentfiles
 }
diff --git a/pd/src/s_main.c b/pd/src/s_main.c
index 4a00c5a91..621614d34 100644
--- a/pd/src/s_main.c
+++ b/pd/src/s_main.c
@@ -160,6 +160,7 @@ int sys_defaultfont;
 
 static void openit(const char *dirname, const char *filename)
 {
+    fprintf(stderr,"openit\n");
     char dirbuf[FILENAME_MAX], *nameptr;
     int fd = open_via_path(dirname, filename, "", dirbuf, &nameptr,
         FILENAME_MAX, 0);
@@ -168,6 +169,8 @@ static void openit(const char *dirname, const char *filename)
         close (fd);
         glob_evalfile(0, gensym(nameptr), gensym(dirbuf));
 		sys_vgui("pdtk_set_current_dir %s\n", filename);
+        //sys_vgui("::pd_menus::update_recentfiles_menu .mbar.file 0\n");
+        sys_vgui("::pd_guiprefs::update_recentfiles %s 1\n", filename);
     }
     else
         error("%s: can't open", filename);
@@ -222,7 +225,7 @@ void glob_initfromgui(void *dummy, t_symbol *s, int argc, t_atom *argv)
     for  (nl = sys_externlist; nl; nl = nl->nl_next)
         if (!sys_load_lib(0, nl->nl_string))
             post("%s: can't load library", nl->nl_string);
-        /* open patches specifies with "-open" args */
+        /* open patches specified with "-open" args */
     for  (nl = sys_openlist; nl; nl = nl->nl_next)
         openit(cwd, nl->nl_string);
     namelist_free(sys_openlist);
-- 
GitLab