From 890310eb11aadcc4e6654b1eb9fd0c76037d038c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Albert=20Gr=C3=A4f?= <aggraef@gmail.com>
Date: Thu, 27 Jul 2017 09:23:25 +0200
Subject: [PATCH] Save the defaults on OSX if the user prefs don't exist yet,
 in order to avoid losing them later when saving the recent files list (fixes
 #339).

---
 pd/src/s_file.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/pd/src/s_file.c b/pd/src/s_file.c
index 32034d04b..b695f11e0 100644
--- a/pd/src/s_file.c
+++ b/pd/src/s_file.c
@@ -268,6 +268,8 @@ static char *sys_prefbuf;
 // prefs is *much* faster now than with the previous method which invoked
 // 'defaults read' on each individual key.
 
+static int save_prefs_later = 0;
+
 static void sys_initloadpreferences(void)
 {
     char cmdbuf[MAXPDSTRING], *buf;
@@ -308,6 +310,8 @@ static void sys_initloadpreferences(void)
       // Read from the package defaults and write to the user prefs.
       prefs = default_prefs;
       strncpy(current_prefs, user_prefs, FILENAME_MAX);
+      // AG: Remember to save the prefs later after we loaded them (see below).
+      save_prefs_later = 1;
     }
     // This looks complicated, but is rather straightforward. The individual
     // stages of the pipe are:
@@ -405,6 +409,14 @@ static void sys_doneloadpreferences( void)
     if (sys_prefbuf)
         free(sys_prefbuf);
     sys_prefbuf = NULL;
+    if (save_prefs_later) {
+      // AG: We need to save the default prefs to the user prefs at this point
+      // in order to avoid losing them, in case the recent file list is written
+      // without first saving the defaults (fixes #339).
+      extern void glob_savepreferences(t_pd *dummy);
+      glob_savepreferences(NULL);
+      save_prefs_later = 0;
+    }
 }
 
 // AG: We use a similar approach here to import the data into the defaults
-- 
GitLab