Jean-Marc Lasgouttes wrote:

> * http://www.lyx.org/trac/changeset/14474
> 
>   The code in 1.4 is very different. I'd rather let you port it.

In fact the code in 1.4 has only two differences to 1.5 that show multiple
times:

1) A LyXView/BufferView reorganization
2) 1.5 does not have BufferView::newFile anymore.

Fortunately 1) has nothing but simple search/replace to do with the patch,
and we can do 2) also for 1.4 because newFile is not used anymore after the
patch.

The attached patch works for me. OK to go in?


Georg

Log:
Fix bug 2404
        * src/lyx_cb.C
        (newFile): Only set the new buffer if it is valid

        * src/BufferView_pimpl.C
        (BufferView::Pimpl::loadLyXFile): Test whether newFile succeeded

        * src/lyxfunc.C
        (LyXFunc::menuNew): Only set the new buffer if it is valid
        (LyXFunc::open): ditto

        * src/importer.C
        (Importer::Import): ditto

        * src/lyx_main.C
        (LyX::exec2): Only use the new buffer if newFile succeeded

        * src/buffer_funcs.C
        (newFile): discard the buffer and return 0 if the template is invalid

        * src/BufferView_pimpl.[Ch]
        (BufferView::Pimpl::newFile): remove, not used anymore

        * src/BufferView.C
        (BufferView::newFile): remove, not used anymore
Index: src/lyx_cb.C
===================================================================
--- src/lyx_cb.C	(Revision 14614)
+++ src/lyx_cb.C	(Arbeitskopie)
@@ -19,6 +19,7 @@
 #include "buffer.h"
 #include "bufferlist.h"
 #include "BufferView.h"
+#include "buffer_funcs.h"
 #include "cursor.h"
 #include "debug.h"
 #include "gettext.h"
@@ -351,7 +352,9 @@ void NewFile(BufferView * bv, string con
 			    << "\nName is " << name
 			    << "\nTemplate is " << tmpname << endl;
 
-	bv->newFile(name, tmpname);
+	Buffer * const b = newFile(name, tmpname);
+	if (b)
+		bv->setBuffer(b);
 }
 
 
Index: src/BufferView_pimpl.C
===================================================================
--- src/BufferView_pimpl.C	(Revision 14614)
+++ src/BufferView_pimpl.C	(Arbeitskopie)
@@ -232,13 +232,6 @@ void BufferView::Pimpl::disconnectBuffer
 }
 
 
-void BufferView::Pimpl::newFile(string const & filename, string const & tname,
-	bool isNamed)
-{
-	setBuffer(::newFile(filename, tname, isNamed));
-}
-
-
 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
 {
 	// Get absolute path of file and add ".lyx"
@@ -285,9 +278,11 @@ bool BufferView::Pimpl::loadLyXFile(stri
 		int const ret = Alert::prompt(_("Create new document?"),
 			 text, 0, 1, _("&Create"), _("Cancel"));
 
-		if (ret == 0)
-			b = ::newFile(s, string(), true);
-		else
+		if (ret == 0) {
+			b = newFile(s, string(), true);
+			if (!b)
+				return false;
+		} else
 			return false;
 	}
 
Index: src/BufferView_pimpl.h
===================================================================
--- src/BufferView_pimpl.h	(Revision 14614)
+++ src/BufferView_pimpl.h	(Arbeitskopie)
@@ -64,8 +64,6 @@ public:
 	///
 	void update(Update::flags flags = Update::Force);
 	///
-	void newFile(std::string const &, std::string const &, bool);
-	///
 	bool loadLyXFile(std::string const &, bool);
 	///
 	void workAreaResize();
Index: src/lyxfunc.C
===================================================================
--- src/lyxfunc.C	(Revision 14614)
+++ src/lyxfunc.C	(Arbeitskopie)
@@ -1724,7 +1724,9 @@ void LyXFunc::menuNew(string const & nam
 		templname = result.second;
 	}
 
-	view()->newFile(filename, templname, !name.empty());
+	Buffer * const b = newFile(filename, templname, !name.empty());
+	if (b)
+		view()->setBuffer(b);
 }
 
 
@@ -1778,8 +1780,10 @@ void LyXFunc::open(string const & fname)
 
 	// if the file doesn't exist, let the user create one
 	if (!fs::exists(filename)) {
-		// the user specifically chose this name. Believe them.
-		view()->newFile(filename, "", true);
+		// the user specifically chose this name. Believe him.
+		Buffer * const b = newFile(filename, string(), true);
+		if (b)
+			view()->setBuffer(b);
 		return;
 	}
 
Index: src/importer.C
===================================================================
--- src/importer.C	(Revision 14614)
+++ src/importer.C	(Arbeitskopie)
@@ -23,6 +23,7 @@
 #include "frontends/Alert.h"
 #include "gettext.h"
 #include "BufferView.h"
+#include "buffer_funcs.h"
 
 using lyx::support::bformat;
 using lyx::support::ChangeExtension;
@@ -71,7 +72,11 @@ bool Importer::Import(LyXView * lv, stri
 	if (loader_format == "lyx") {
 		lv->view()->loadLyXFile(lyxfile);
 	} else {
-		lv->view()->newFile(lyxfile, string(), true);
+		Buffer * const b = newFile(lyxfile, string(), true);
+		if (b)
+			lv->view()->setBuffer(b);
+		else
+			return false;
 		bool as_paragraphs = loader_format == "textparagraph";
 		string filename2 = (loader_format == format) ? filename
 			: ChangeExtension(filename,
Index: src/BufferView.C
===================================================================
--- src/BufferView.C	(Revision 14614)
+++ src/BufferView.C	(Arbeitskopie)
@@ -107,12 +107,6 @@ void BufferView::setBuffer(Buffer * b)
 }
 
 
-void BufferView::newFile(string const & fn, string const & tn, bool named)
-{
-	pimpl_->newFile(fn, tn, named);
-}
-
-
 bool BufferView::loadLyXFile(string const & fn, bool tl)
 {
 	return pimpl_->loadLyXFile(fn, tl);
Index: src/BufferView.h
===================================================================
--- src/BufferView.h	(Revision 14614)
+++ src/BufferView.h	(Arbeitskopie)
@@ -92,9 +92,6 @@ public:
 
 	/// reload the contained buffer
 	void reload();
-	/// create a new buffer based on template
-	void newFile(std::string const & fname, std::string const & tname,
-		     bool named = true);
 	/// load a buffer into the view
 	bool loadLyXFile(std::string const & name, bool tolastfiles = true);
 
Index: src/lyx_main.C
===================================================================
--- src/lyx_main.C	(Revision 14614)
+++ src/lyx_main.C	(Arbeitskopie)
@@ -272,7 +272,9 @@ void LyX::exec2(int & argc, char * argv[
 			// the filename if necessary
 			string s = FileSearch(string(), *it, "lyx");
 			if (s.empty()) {
-				last_loaded = newFile(*it, string(), true);
+				Buffer * const b = newFile(*it, string(), true);
+				if (b)
+					last_loaded = b;
 			} else {
 				Buffer * buf = bufferlist.newBuffer(s, false);
 				buf->error.connect(boost::bind(&LyX::printError, this, _1));
Index: src/buffer_funcs.C
===================================================================
--- src/buffer_funcs.C	(Revision 14614)
+++ src/buffer_funcs.C	(Arbeitskopie)
@@ -189,7 +189,8 @@ Buffer * newFile(string const & filename
 			string const file = MakeDisplayPath(tname, 50);
 			string const text  = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
 			Alert::error(_("Could not read template"), text);
-			// no template, start with empty buffer
+			bufferlist.release(b);
+			return 0;
 		}
 	}
 
Index: status.14x
===================================================================
--- status.14x	(Revision 14615)
+++ status.14x	(Arbeitskopie)
@@ -56,6 +56,9 @@ What's new
 
 - Fix the C-x C-b binding in emacs mode (bug 2747).
 
+- Fix a crash when a non-template file is selected in the
+  "New from Template" dialog (bug 2404)
+
 * Build/installation:
 
 - Fix 'check' make target for systems which do not have /bin/bash (bug 2524).

Reply via email to