@@ -198,28 +198,42 @@ DocumentThread::Prefs::Prefs(bool use_defaults)
198198 {
199199#ifndef EMSCRIPTEN
200200 config_path=std::string (Glib::get_user_config_dir ()) + " /cadabra2.conf" ;
201- if (!use_defaults) {
202- std::ifstream f (config_path);
203- if (f)
204- f >> data;
205- else {
206- data = nlohmann::json::object ();
207-
208- // Backwards compatibility, check to see if cadabra.conf exists
209- // and if so take the is_registered variable from there
210- std::ifstream old_f (std::string (Glib::get_user_config_dir ()) + " /cadabra.conf" );
211- if (old_f) {
212- std::string line;
213- while (old_f.good ()) {
214- std::getline (old_f, line);
215- if (line.find (" registered=true" ) != std::string::npos) {
216- data[" is_registered" ] = true ;
217- break ;
201+ try {
202+
203+ if (!use_defaults) {
204+ std::ifstream f (config_path);
205+ if (f) {
206+ try {
207+ f >> data;
208+ }
209+ catch (nlohmann::json::exception& ex) {
210+ std::cerr << " Config file " << config_path << " is not JSON; ignoring." << std::endl;
211+ data = nlohmann::json::object ();
212+ }
213+ }
214+ else {
215+ data = nlohmann::json::object ();
216+
217+ // Backwards compatibility, check to see if cadabra.conf exists
218+ // and if so take the is_registered variable from there
219+ std::ifstream old_f (std::string (Glib::get_user_config_dir ()) + " /cadabra.conf" );
220+ if (old_f) {
221+ std::string line;
222+ while (old_f.good ()) {
223+ std::getline (old_f, line);
224+ if (line.find (" registered=true" ) != std::string::npos) {
225+ data[" is_registered" ] = true ;
226+ break ;
227+ }
218228 }
219229 }
220230 }
221231 }
222232 }
233+ catch (std::exception& ex) {
234+ data = nlohmann::json::object ();
235+ }
236+
223237 font_step = data.value (" font_step" , 0 );
224238 highlight = data.value (" highlight" , false );
225239 is_registered = data.value (" is_registered" , false );
@@ -231,8 +245,13 @@ DocumentThread::Prefs::Prefs(bool use_defaults)
231245
232246 if (git_path==" " )
233247 git_path=" /usr/bin/git" ;
248+
234249 // Get the colours for syntax highlighting.
235- auto python_colours = data[" colours" ][" python" ];
250+ if (data.count (" colours" )==0 )
251+ data[" colours" ]={ {" python" , nlohmann::json::object () }, {" latex" , nlohmann::json::object () } };
252+
253+ const auto & python_colours = data[" colours" ][" python" ];
254+
236255 colours[" python" ][" keyword" ] = python_colours.value (" keyword" , " RoyalBlue" );
237256 colours[" python" ][" operator" ] = python_colours.value (" operator" , " SlateGray" );
238257 colours[" python" ][" brace" ] = python_colours.value (" brace" , " SlateGray" );
@@ -245,7 +264,7 @@ DocumentThread::Prefs::Prefs(bool use_defaults)
245264 colours[" python" ][" decorator" ] = python_colours.value (" decorator" , " DarkViolet" );
246265 colours[" python" ][" class" ] = python_colours.value (" class" , " MediumOrchid" );
247266
248- auto latex_colours = data[" colours" ][" latex" ];
267+ const auto & latex_colours = data[" colours" ][" latex" ];
249268 colours[" latex" ][" command" ] = latex_colours.value (" command" , " rgb(52,101,164)" );
250269 colours[" latex" ][" parameter" ] = latex_colours.value (" brace" , " rgb(245,121,0)" );
251270 colours[" latex" ][" comment" ] = latex_colours.value (" comment" , " Silver" );
0 commit comments