>>>>> "Georg" == Georg Baum <[EMAIL PROTECTED]> writes:

Georg> The advantage of my patch is that it uses the existing cut and
Georg> paste code and that it will automatically benefit from future
Georg> changes to that code.

I thought again about it during the week-end and came up with the
following version of the cut-and-paste idea, that I like a lot more.
This is from my version of the idea, so there may be some problems I
overlooked (and that you fixed in your version).

What do you think?

JMarc

Index: src/BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.599
diff -u -p -r1.599 BufferView_pimpl.C
--- src/BufferView_pimpl.C	7 Nov 2005 15:06:41 -0000	1.599
+++ src/BufferView_pimpl.C	28 Nov 2005 11:35:03 -0000
@@ -875,13 +875,9 @@ void BufferView::Pimpl::MenuInsertLyXFil
 	string const disp_fn = MakeDisplayPath(filename);
 	owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
 
-	cursor_.clearSelection();
-	bv_->getLyXText()->breakParagraph(cursor_);
-
-	BOOST_ASSERT(cursor_.inTexted());
-
 	string const fname = MakeAbsPath(filename);
-	bool const res = buffer_->readFile(fname, cursor_.pit());
+	bool const res = insertLyXFile(cursor_, fname);
+	cursor_.clearSelection();
 	resizeCurrentBuffer();
 
 	string s = res ? _("Document %1$s inserted.")
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.2328
diff -u -p -r1.2328 ChangeLog
--- src/ChangeLog	25 Nov 2005 14:40:33 -0000	1.2328
+++ src/ChangeLog	28 Nov 2005 11:35:04 -0000
@@ -1,6 +1,25 @@
+2005-11-28  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
+
+	Fix bug 2096.
+	
+	* buffer.C (readDocument): remove pit argument and code releated
+	to it; set the inset owner correctly (unrelated, but useful).
+	(readFile): get rid of pit argument.
+
+	* CutAndPaste.C (pasteSelectionHelper): use a ParagraphList and a
+	textclass instead of a selection index.
+	(pasteParagraphList): new function.
+	(pasteSelection): make it a wrapper around pasteParagraphList.
+
+	* BufferView_pimpl.C (MenuInsertLyXFile): use insertLyXFile.
+
+	* buffer_funcs.C (insertLyXFile): new method, which uses
+	cap::pasteParagraphList to insert a file.
+
 2005-11-25  Jürgen Spitzmüller  <[EMAIL PROTECTED]>
 
-	* paragraph.C (asString): use new inset->textString method (fix bug 2089)
+	* paragraph.C (asString): use new inset->textString method (fix
+	bug 2089) 
 
 2005-11-24  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
Index: src/CutAndPaste.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.C,v
retrieving revision 1.159
diff -u -p -r1.159 CutAndPaste.C
--- src/CutAndPaste.C	25 Nov 2005 09:27:08 -0000	1.159
+++ src/CutAndPaste.C	28 Nov 2005 11:35:04 -0000
@@ -105,18 +105,19 @@ bool checkPastePossible(int index)
 
 
 pair<PitPosPair, pit_type>
-pasteSelectionHelper(Buffer const & buffer, ParagraphList & pars,
-	pit_type pit, int pos,
-	textclass_type tc, size_t cut_index, ErrorList & errorlist)
+pasteSelectionHelper(Buffer const & buffer, 
+		     ParagraphList & pars, pit_type pit, int pos,
+		     ParagraphList const & parlist, textclass_type textclass, 
+		     ErrorList & errorlist)
 {
-	if (!checkPastePossible(cut_index))
+	if (parlist.empty())
 		return make_pair(PitPosPair(pit, pos), pit);
 
 	BOOST_ASSERT (pos <= pars[pit].size());
 
 	// Make a copy of the CaP paragraphs.
-	ParagraphList insertion = theCuts[cut_index].first;
-	textclass_type const textclass = theCuts[cut_index].second;
+	ParagraphList insertion = parlist;
+	textclass_type const tc = buffer.params().textclass;
 
 	// Now remove all out of the pars which is NOT allowed in the
 	// new environment and set also another font if that is required.
@@ -608,13 +609,9 @@ std::string getSelection(Buffer const & 
 }
 
 
-void pasteSelection(LCursor & cur, size_t sel_index)
+void pasteParagraphList(LCursor & cur, ParagraphList const & parlist, 
+                        textclass_type textclass)
 {
-	// this does not make sense, if there is nothing to paste
-	lyxerr << "#### pasteSelection " << sel_index << endl;
-	if (!checkPastePossible(sel_index))
-		return;
-
 	if (cur.inTexted()) {
 		LyXText * text = cur.text();
 		BOOST_ASSERT(text);
@@ -630,8 +627,8 @@ void pasteSelection(LCursor & cur, size_
 			pasteSelectionHelper(cur.buffer(),
                                              text->paragraphs(),
                                              cur.pit(), cur.pos(),
-                                             cur.buffer().params().textclass,
-                                             sel_index, el);
+                                             parlist, textclass,
+                                             el);
 		bufferErrors(cur.buffer(), el);
 		cur.bv().showErrorList(_("Paste"));
 
@@ -643,6 +640,18 @@ void pasteSelection(LCursor & cur, size_
 
 	// mathed is handled in MathNestInset/MathGridInset
 	BOOST_ASSERT(!cur.inMathed());
+}
+
+
+void pasteSelection(LCursor & cur, size_t sel_index)
+{
+	// this does not make sense, if there is nothing to paste
+	lyxerr[Debug::DEBUG] << "#### pasteSelection " << sel_index << endl;
+	if (!checkPastePossible(sel_index))
+		return;
+
+	pasteParagraphList(cur, theCuts[sel_index].first,
+			   theCuts[sel_index].second);
 }
 
 
Index: src/CutAndPaste.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.h,v
retrieving revision 1.42
diff -u -p -r1.42 CutAndPaste.h
--- src/CutAndPaste.h	25 Nov 2005 09:27:08 -0000	1.42
+++ src/CutAndPaste.h	28 Nov 2005 11:35:04 -0000
@@ -56,6 +56,11 @@ void copySelection(LCursor & cur);
 ///
 void pasteSelection(LCursor & cur, size_t sel_index = 0);
 
+///
+void pasteParagraphList(LCursor & cur, ParagraphList const & parlist, 
+                        textclass_type textclass);
+
+
 /** Needed to switch between different classes. This works
  *  for a list of paragraphs beginning with the specified par.
  *  It changes layouts and character styles.
Index: src/buffer.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.C,v
retrieving revision 1.629
diff -u -p -r1.629 buffer.C
--- src/buffer.C	13 Oct 2005 10:59:37 -0000	1.629
+++ src/buffer.C	28 Nov 2005 11:35:04 -0000
@@ -470,25 +470,23 @@ bool Buffer::readDocument(LyXLex & lex)
 		error(ErrorItem(_("Document header error"), s, -1, 0, 0));
 	}
 
-	if (paragraphs().empty()) {
-		readHeader(lex);
-		if (!params().getLyXTextClass().load()) {
-			string theclass = params().getLyXTextClass().name();
-			Alert::error(_("Can't load document class"), bformat(
-					"Using the default document class, because the "
-					" class %1$s could not be loaded.", theclass));
-			params().textclass = 0;
-		}
-	} else {
-		// We don't want to adopt the parameters from the
-		// document we insert, so read them into a temporary buffer
-		// and then discard it
-
-		Buffer tmpbuf("", false);
-		tmpbuf.readHeader(lex);
-	}
+	// we are reading in a brand new document
+	BOOST_ASSERT(paragraphs().empty());
 
-	return text().read(*this, lex);
+	readHeader(lex);
+	if (!params().getLyXTextClass().load()) {
+		string theclass = params().getLyXTextClass().name();
+		Alert::error(_("Can't load document class"), bformat(
+				     "Using the default document class, because the "
+				     " class %1$s could not be loaded.", theclass));
+		params().textclass = 0;
+	}
+
+	bool const res = text().read(*this, lex);
+	for_each(text().paragraphs().begin(),
+		 text().paragraphs().end(),
+		 bind(&Paragraph::setInsetOwner, _1, &inset()));
+	return res;
 }
 
 
@@ -556,7 +554,9 @@ bool Buffer::readFile(string const & fil
 
 	// remove dummy empty par
 	paragraphs().clear();
-	bool ret = readFile(filename, paragraphs().size());
+	LyXLex lex(0, 0);
+	lex.setFile(filename);
+	bool ret = readFile(lex, filename);
 
 	// After we have read a file, we must ensure that the buffer
 	// language is set and used in the gui.
@@ -567,14 +567,6 @@ bool Buffer::readFile(string const & fil
 }
 
 
-bool Buffer::readFile(string const & filename, pit_type const pit)
-{
-	LyXLex lex(0, 0);
-	lex.setFile(filename);
-	return readFile(lex, filename, pit);
-}
-
-
 bool Buffer::fully_loaded() const
 {
 	return pimpl_->file_fully_loaded;
@@ -587,7 +579,7 @@ void Buffer::fully_loaded(bool const val
 }
 
 
-bool Buffer::readFile(LyXLex & lex, string const & filename, pit_type const pit)
+bool Buffer::readFile(LyXLex & lex, string const & filename)
 {
 	BOOST_ASSERT(!filename.empty());
 
@@ -668,7 +660,7 @@ bool Buffer::readFile(LyXLex & lex, stri
 					      filename));
 			return false;
 		} else {
-			bool const ret = readFile(tmpfile, pit);
+			bool const ret = readFile(tmpfile);
 			// Do stuff with tmpfile name and buffer name here.
 			return ret;
 		}
Index: src/buffer.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.h,v
retrieving revision 1.193
diff -u -p -r1.193 buffer.h
--- src/buffer.h	17 Jul 2005 23:02:58 -0000	1.193
+++ src/buffer.h	28 Nov 2005 11:35:04 -0000
@@ -93,8 +93,6 @@ public:
 	/// load a new file
 	bool readFile(std::string const & filename);
 
-	bool readFile(std::string const & filename, lyx::pit_type pit);
-
 	/// read the header, returns number of unknown tokens
 	int readHeader(LyXLex & lex);
 
@@ -338,11 +336,9 @@ public:
 
 private:
 	/** Inserts a file into a document
-	    \param par if != 0 insert the file.
 	    \return \c false if method fails.
 	*/
-	bool readFile(LyXLex &, std::string const & filename,
-		      lyx::pit_type pit);
+	bool readFile(LyXLex &, std::string const & filename);
 
 	bool do_writeFile(std::ostream & ofs) const;
 
Index: src/buffer_funcs.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer_funcs.C,v
retrieving revision 1.34
diff -u -p -r1.34 buffer_funcs.C
--- src/buffer_funcs.C	9 Sep 2005 14:52:55 -0000	1.34
+++ src/buffer_funcs.C	28 Nov 2005 11:35:05 -0000
@@ -16,8 +16,8 @@
 #include "buffer.h"
 #include "bufferlist.h"
 #include "bufferparams.h"
-#include "dociterator.h"
 #include "counters.h"
+#include "CutAndPaste.h"
 #include "errorlist.h"
 #include "Floating.h"
 #include "FloatList.h"
@@ -167,6 +167,21 @@ bool loadLyXFile(Buffer * b, string cons
 		}
 	}
 	return false;
+}
+
+
+bool insertLyXFile(LCursor & cur, string const & s)
+{
+	BOOST_ASSERT(cur.inTexted());
+
+	Buffer buf("", false);
+
+	if (!loadLyXFile(&buf, s))
+		return false;
+
+	lyx::cap::pasteParagraphList(cur, buf.paragraphs(), 
+				     buf.params().textclass);
+	return true;
 }
 
 
Index: src/buffer_funcs.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer_funcs.h,v
retrieving revision 1.12
diff -u -p -r1.12 buffer_funcs.h
--- src/buffer_funcs.h	9 Jun 2005 09:58:05 -0000	1.12
+++ src/buffer_funcs.h	28 Nov 2005 11:35:05 -0000
@@ -20,6 +20,7 @@
 class Buffer;
 class DocIterator;
 class ErrorList;
+class LCursor;
 class TeXErrors;
 
 /**
@@ -27,6 +28,14 @@ class TeXErrors;
  *  and \return success status.
  */
 bool loadLyXFile(Buffer *, std::string const & filename);
+
+
+/**
+ *  Insert LyX file \c filename at \c cur position
+ *  and \return success status.
+ */
+bool insertLyXFile(LCursor & cur, std::string const & s);
+
 
 /* Make a new file (buffer) with name \c filename based on a template
  * named \c templatename

Reply via email to