From 3c90e56662f78f217093c118319abbe1c2f99b84 Mon Sep 17 00:00:00 2001
From: Yvan Volochine <yvan.volochine@gmail.com>
Date: Tue, 15 Mar 2011 01:59:15 +0100
Subject: [PATCH] testing writing recentfiles to pdsettings

---
 src/s_file.c            |   25 ++++++++++++++++++++++++-
 src/s_inter.c           |    2 ++
 src/s_path.c            |   12 ++++++++++++
 src/s_stuff.h           |    1 +
 tcl/pd_menus.tcl        |    2 ++
 tcl/wheredoesthisgo.tcl |    2 ++
 6 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/src/s_file.c b/src/s_file.c
index 841fb6a..d034520 100644
--- a/src/s_file.c
+++ b/src/s_file.c
@@ -405,6 +405,17 @@ void sys_loadpreferences( void)
             break;
         sys_externlist = namelist_append_files(sys_externlist, prefbuf);
     }
+
+        /* recent files */
+    //for (i = 0; i<total_recentfiles; i++)
+    for (i = 0; i<10; i++)
+    {
+	sprintf(keybuf, "recent%d", i+1);
+	if (!sys_getpreference(keybuf, prefbuf, MAXPDSTRING))
+	  break;
+	recentfiles_list = namelist_append_files(recentfiles_list, prefbuf);
+    }
+
     if (sys_getpreference("defeatrt", prefbuf, MAXPDSTRING))
         sscanf(prefbuf, "%d", &sys_defeatrt);
     if (sys_getpreference("flags", prefbuf, MAXPDSTRING))
@@ -519,10 +530,22 @@ void glob_savepreferences(t_pd *dummy)
     }
     sprintf(buf1, "%d", i);
     sys_putpreference("nloadlib", buf1);
+
+    /* recent files */
+    //for (i = 0; i<total_recentfiles; i++)
+    for (i = 0; 1; i++)
+    {
+	char *pathelem = namelist_get(recentfiles_list, i);
+	if (!pathelem)
+	    break;
+	sprintf(buf1, "recent%d", i+1);
+	sys_putpreference(buf1, pathelem);
+    }
+
     sprintf(buf1, "%d", sys_defeatrt);
     sys_putpreference("defeatrt", buf1);
     sys_putpreference("flags", 
         (sys_flags ? sys_flags->s_name : ""));
     sys_donesavepreferences();
-    
+
 }
diff --git a/src/s_inter.c b/src/s_inter.c
index 60f61d4..850c850 100644
--- a/src/s_inter.c
+++ b/src/s_inter.c
@@ -106,6 +106,7 @@ extern int sys_addhist(int phase);
 void sys_set_searchpath(void);
 void sys_set_extrapath(void);
 void sys_set_startup(void);
+void sys_set_recentfiles_list(void);
 
 /* ----------- functions for timing, signals, priorities, etc  --------- */
 
@@ -1251,6 +1252,7 @@ int sys_startgui(const char *libdir)
          sys_set_searchpath();     /* tell GUI about path and startup flags */
          sys_set_extrapath();
          sys_set_startup();
+	 sys_set_recentfiles_list();
                             /* ... and about font, medio APIS, etc */
          sys_vgui("pdtk_pd_startup %d %d %d {%s} %s %s {%s} %s\n",
                   PD_MAJOR_VERSION, PD_MINOR_VERSION, 
diff --git a/src/s_path.c b/src/s_path.c
index 020db14..6026180 100644
--- a/src/s_path.c
+++ b/src/s_path.c
@@ -45,6 +45,7 @@ t_namelist *sys_externlist;
 t_namelist *sys_searchpath;
 t_namelist *sys_staticpath;
 t_namelist *sys_helppath;
+t_namelist *recentfiles_list;
 
     /* change '/' characters to the system's native file separator */
 void sys_bashfilename(const char *from, char *to)
@@ -621,6 +622,17 @@ void sys_set_searchpath( void)
     sys_gui("set ::sys_searchpath $::tmp_path\n");
 }
 
+void sys_set_recentfiles_list( void)
+{
+    int i;
+    t_namelist *nl;
+
+    sys_gui("set ::tmp_path {}\n");
+    for (nl = recentfiles_list, i = 0; nl; nl = nl->nl_next, i++)
+      sys_vgui("lappend ::tmp_path {%s}\n", nl->nl_string);
+    sys_gui("set ::recentfiles_list $::tmp_path\n");
+}
+
     /* send the hard-coded search path to pd-gui */
 void sys_set_extrapath( void)
 {
diff --git a/src/s_stuff.h b/src/s_stuff.h
index b37b161..a1e91f6 100644
--- a/src/s_stuff.h
+++ b/src/s_stuff.h
@@ -24,6 +24,7 @@ extern int sys_usestdpath;
 extern t_namelist *sys_externlist;
 extern t_namelist *sys_searchpath;
 extern t_namelist *sys_helppath;
+extern t_namelist *recentfiles_list;
 int sys_open_absolute(const char *name, const char* ext,
     char *dirresult, char **nameresult, unsigned int size, int bin, int *fdp);
 int sys_trytoopenone(const char *dir, const char *name, const char* ext,
diff --git a/tcl/pd_menus.tcl b/tcl/pd_menus.tcl
index fc617df..6fa619d 100644
--- a/tcl/pd_menus.tcl
+++ b/tcl/pd_menus.tcl
@@ -525,6 +525,8 @@ proc ::pd_menus::build_file_menu_x11 {mymenu} {
     $mymenu add command -label [_ "Close"]      -accelerator "$accelerator+W"
     $mymenu add command -label [_ "Quit"]       -accelerator "$accelerator+Q" \
         -command {pdsend "pd verifyquit"}
+    # insert recent files
+    ::pd_menus::update_recentfiles_on_menu $mymenu
 }
 
 # the "Edit", "Put", and "Find" menus do not have cross-platform differences
diff --git a/tcl/wheredoesthisgo.tcl b/tcl/wheredoesthisgo.tcl
index 1e9e034..265a396 100644
--- a/tcl/wheredoesthisgo.tcl
+++ b/tcl/wheredoesthisgo.tcl
@@ -15,6 +15,8 @@ proc open_file {filename} {
         lappend ::recentfiles_list $filename
         set ::recentfiles_list [lrange $::recentfiles_list 0 $::total_recentfiles]
         ::pd_menus::update_recentfiles_menu
+	# save preferences after updating recentfiles for testing # FIXME
+	pdsend {pd save-preferences}
     } {
         ::pdwindow::post [format [_ "Ignoring '%s': doesn't look like a Pd-file"] $filename]
     }
-- 
1.7.4.1

