From 4842112b77270a6d9b81c1ad59298bbb7aba2329 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Tue, 29 Nov 2005 15:08:35 +0000 Subject: [PATCH] fix bug 2096: LyX file inserting is broken git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10631 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView_pimpl.C | 26 ++++++++++++++--------- src/ChangeLog | 22 ++++++++++++++++++- src/CutAndPaste.C | 44 +++++++++++++++++++++----------------- src/CutAndPaste.h | 5 +++++ src/buffer.C | 48 ++++++++++++++++++------------------------ src/buffer.h | 6 +----- 6 files changed, 88 insertions(+), 63 deletions(-) diff --git a/src/BufferView_pimpl.C b/src/BufferView_pimpl.C index 419015a796..87bc17b337 100644 --- a/src/BufferView_pimpl.C +++ b/src/BufferView_pimpl.C @@ -25,6 +25,7 @@ #include "bufferparams.h" #include "coordcache.h" #include "cursor.h" +#include "CutAndPaste.h" #include "debug.h" #include "dispatchresult.h" #include "factory.h" @@ -831,6 +832,7 @@ void BufferView::Pimpl::stuffClipboard(string const & content) const void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm) { + BOOST_ASSERT(cursor_.inTexted()); string filename = filenm; if (filename.empty()) { @@ -875,18 +877,19 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm) 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 res; + Buffer buf("", false); + buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1)); + if (::loadLyXFile(&buf, MakeAbsPath(filename))) { + lyx::cap::pasteParagraphList(cursor_, buf.paragraphs(), + buf.params().textclass); + res = _("Document %1$s inserted."); + } else + res = _("Could not insert document %1$s"); - string const fname = MakeAbsPath(filename); - bool const res = buffer_->readFile(fname, cursor_.pit()); + owner_->message(bformat(res, disp_fn)); + bv_->showErrorList(_("Document insertion")); resizeCurrentBuffer(); - - string s = res ? _("Document %1$s inserted.") - : _("Could not insert document %1$s"); - owner_->message(bformat(s, disp_fn)); } @@ -1022,6 +1025,9 @@ FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd) case LFUN_FILE_INSERT: case LFUN_FILE_INSERT_ASCII_PARA: case LFUN_FILE_INSERT_ASCII: + // FIXME: Actually, these LFUNS should be moved to LyXText + flag.enabled(cursor_.inTexted()); + break; case LFUN_FONT_STATE: case LFUN_INSERT_LABEL: case LFUN_BOOKMARK_SAVE: diff --git a/src/ChangeLog b/src/ChangeLog index 22ab20f2da..26f367fe6b 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,22 @@ +2005-11-29 Jean-Marc Lasgouttes + + Fix bug 2096. + + * BufferView_pimpl.C (getStatus): it is only possible to insert a + file in a text inset. + + * 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 + cap::pasteParagraphList to insert a file. + 2005-11-29 Georg Baum * buffer_funcs.C (bufferErrors): prevent endless loop (bug 2121) @@ -16,7 +35,8 @@ 2005-11-25 Jürgen Spitzmüller - * 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 diff --git a/src/CutAndPaste.C b/src/CutAndPaste.C index b74d018dcc..ce12c42960 100644 --- a/src/CutAndPaste.C +++ b/src/CutAndPaste.C @@ -105,18 +105,19 @@ bool checkPastePossible(int index) pair -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 & buf, size_t sel_index) } -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); @@ -623,22 +620,18 @@ void pasteSelection(LCursor & cur, size_t sel_index) pit_type endpit; PitPosPair ppp; - ErrorList el; boost::tie(ppp, endpit) = 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")); - + updateCounters(cur.buffer()); cur.clearSelection(); text->setCursor(cur, ppp.first, ppp.second); - cur.setSelection(); - updateCounters(cur.buffer()); } // mathed is handled in MathNestInset/MathGridInset @@ -646,6 +639,19 @@ void pasteSelection(LCursor & cur, size_t sel_index) } +void pasteSelection(LCursor & cur, size_t sel_index) +{ + // this does not make sense, if there is nothing to paste + if (!checkPastePossible(sel_index)) + return; + + pasteParagraphList(cur, theCuts[sel_index].first, + theCuts[sel_index].second); + cur.bv().showErrorList(_("Paste")); + cur.setSelection(); +} + + void setSelectionRange(LCursor & cur, pos_type length) { LyXText * text = cur.text(); diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h index 8332205fc7..ff9ec902b3 100644 --- a/src/CutAndPaste.h +++ b/src/CutAndPaste.h @@ -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. diff --git a/src/buffer.C b/src/buffer.C index b2cf912c18..429907f18a 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -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()); + + 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; } - return text().read(*this, lex); + 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 & filename) // 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 & filename) } -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 value) } -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, string const & filename, pit_type const pit) 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; } diff --git a/src/buffer.h b/src/buffer.h index bf2c690f42..ba3ec7c40c 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -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; -- 2.39.2