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
This commit is contained in:
Jean-Marc Lasgouttes 2005-11-29 15:08:35 +00:00
parent 238411b596
commit 4842112b77
6 changed files with 87 additions and 62 deletions

View File

@ -25,6 +25,7 @@
#include "bufferparams.h" #include "bufferparams.h"
#include "coordcache.h" #include "coordcache.h"
#include "cursor.h" #include "cursor.h"
#include "CutAndPaste.h"
#include "debug.h" #include "debug.h"
#include "dispatchresult.h" #include "dispatchresult.h"
#include "factory.h" #include "factory.h"
@ -831,6 +832,7 @@ void BufferView::Pimpl::stuffClipboard(string const & content) const
void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm) void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
{ {
BOOST_ASSERT(cursor_.inTexted());
string filename = filenm; string filename = filenm;
if (filename.empty()) { if (filename.empty()) {
@ -875,18 +877,19 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
string const disp_fn = MakeDisplayPath(filename); string const disp_fn = MakeDisplayPath(filename);
owner_->message(bformat(_("Inserting document %1$s..."), disp_fn)); owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
cursor_.clearSelection(); string res;
bv_->getLyXText()->breakParagraph(cursor_); 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");
BOOST_ASSERT(cursor_.inTexted()); owner_->message(bformat(res, disp_fn));
bv_->showErrorList(_("Document insertion"));
string const fname = MakeAbsPath(filename);
bool const res = buffer_->readFile(fname, cursor_.pit());
resizeCurrentBuffer(); 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:
case LFUN_FILE_INSERT_ASCII_PARA: case LFUN_FILE_INSERT_ASCII_PARA:
case LFUN_FILE_INSERT_ASCII: 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_FONT_STATE:
case LFUN_INSERT_LABEL: case LFUN_INSERT_LABEL:
case LFUN_BOOKMARK_SAVE: case LFUN_BOOKMARK_SAVE:

View File

@ -1,3 +1,22 @@
2005-11-29 Jean-Marc Lasgouttes <lasgouttes@lyx.org>
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 <Georg.Baum@post.rwth-aachen.de> 2005-11-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* buffer_funcs.C (bufferErrors): prevent endless loop (bug 2121) * buffer_funcs.C (bufferErrors): prevent endless loop (bug 2121)
@ -16,7 +35,8 @@
2005-11-25 Jürgen Spitzmüller <j.spitzmueller@gmx.de> 2005-11-25 Jürgen Spitzmüller <j.spitzmueller@gmx.de>
* 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 <lasgouttes@lyx.org> 2005-11-24 Jean-Marc Lasgouttes <lasgouttes@lyx.org>

View File

@ -105,18 +105,19 @@ bool checkPastePossible(int index)
pair<PitPosPair, pit_type> pair<PitPosPair, pit_type>
pasteSelectionHelper(Buffer const & buffer, ParagraphList & pars, pasteSelectionHelper(Buffer const & buffer,
pit_type pit, int pos, ParagraphList & pars, pit_type pit, int pos,
textclass_type tc, size_t cut_index, ErrorList & errorlist) ParagraphList const & parlist, textclass_type textclass,
ErrorList & errorlist)
{ {
if (!checkPastePossible(cut_index)) if (parlist.empty())
return make_pair(PitPosPair(pit, pos), pit); return make_pair(PitPosPair(pit, pos), pit);
BOOST_ASSERT (pos <= pars[pit].size()); BOOST_ASSERT (pos <= pars[pit].size());
// Make a copy of the CaP paragraphs. // Make a copy of the CaP paragraphs.
ParagraphList insertion = theCuts[cut_index].first; ParagraphList insertion = parlist;
textclass_type const textclass = theCuts[cut_index].second; textclass_type const tc = buffer.params().textclass;
// Now remove all out of the pars which is NOT allowed in the // Now remove all out of the pars which is NOT allowed in the
// new environment and set also another font if that is required. // 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()) { if (cur.inTexted()) {
LyXText * text = cur.text(); LyXText * text = cur.text();
BOOST_ASSERT(text); BOOST_ASSERT(text);
@ -623,22 +620,18 @@ void pasteSelection(LCursor & cur, size_t sel_index)
pit_type endpit; pit_type endpit;
PitPosPair ppp; PitPosPair ppp;
ErrorList el; ErrorList el;
boost::tie(ppp, endpit) = boost::tie(ppp, endpit) =
pasteSelectionHelper(cur.buffer(), pasteSelectionHelper(cur.buffer(),
text->paragraphs(), text->paragraphs(),
cur.pit(), cur.pos(), cur.pit(), cur.pos(),
cur.buffer().params().textclass, parlist, textclass,
sel_index, el); el);
bufferErrors(cur.buffer(), el); bufferErrors(cur.buffer(), el);
cur.bv().showErrorList(_("Paste")); updateCounters(cur.buffer());
cur.clearSelection(); cur.clearSelection();
text->setCursor(cur, ppp.first, ppp.second); text->setCursor(cur, ppp.first, ppp.second);
cur.setSelection();
updateCounters(cur.buffer());
} }
// mathed is handled in MathNestInset/MathGridInset // 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) void setSelectionRange(LCursor & cur, pos_type length)
{ {
LyXText * text = cur.text(); LyXText * text = cur.text();

View File

@ -56,6 +56,11 @@ void copySelection(LCursor & cur);
/// ///
void pasteSelection(LCursor & cur, size_t sel_index = 0); 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 /** Needed to switch between different classes. This works
* for a list of paragraphs beginning with the specified par. * for a list of paragraphs beginning with the specified par.
* It changes layouts and character styles. * It changes layouts and character styles.

View File

@ -470,7 +470,9 @@ bool Buffer::readDocument(LyXLex & lex)
error(ErrorItem(_("Document header error"), s, -1, 0, 0)); error(ErrorItem(_("Document header error"), s, -1, 0, 0));
} }
if (paragraphs().empty()) { // we are reading in a brand new document
BOOST_ASSERT(paragraphs().empty());
readHeader(lex); readHeader(lex);
if (!params().getLyXTextClass().load()) { if (!params().getLyXTextClass().load()) {
string theclass = params().getLyXTextClass().name(); string theclass = params().getLyXTextClass().name();
@ -479,16 +481,12 @@ bool Buffer::readDocument(LyXLex & lex)
" class %1$s could not be loaded.", theclass)); " class %1$s could not be loaded.", theclass));
params().textclass = 0; 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); bool const res = text().read(*this, lex);
tmpbuf.readHeader(lex); for_each(text().paragraphs().begin(),
} text().paragraphs().end(),
bind(&Paragraph::setInsetOwner, _1, &inset()));
return text().read(*this, lex); return res;
} }
@ -556,7 +554,9 @@ bool Buffer::readFile(string const & filename)
// remove dummy empty par // remove dummy empty par
paragraphs().clear(); 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 // After we have read a file, we must ensure that the buffer
// language is set and used in the gui. // 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 bool Buffer::fully_loaded() const
{ {
return pimpl_->file_fully_loaded; 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()); BOOST_ASSERT(!filename.empty());
@ -668,7 +660,7 @@ bool Buffer::readFile(LyXLex & lex, string const & filename, pit_type const pit)
filename)); filename));
return false; return false;
} else { } else {
bool const ret = readFile(tmpfile, pit); bool const ret = readFile(tmpfile);
// Do stuff with tmpfile name and buffer name here. // Do stuff with tmpfile name and buffer name here.
return ret; return ret;
} }

View File

@ -93,8 +93,6 @@ public:
/// load a new file /// load a new file
bool readFile(std::string const & filename); bool readFile(std::string const & filename);
bool readFile(std::string const & filename, lyx::pit_type pit);
/// read the header, returns number of unknown tokens /// read the header, returns number of unknown tokens
int readHeader(LyXLex & lex); int readHeader(LyXLex & lex);
@ -338,11 +336,9 @@ public:
private: private:
/** Inserts a file into a document /** Inserts a file into a document
\param par if != 0 insert the file.
\return \c false if method fails. \return \c false if method fails.
*/ */
bool readFile(LyXLex &, std::string const & filename, bool readFile(LyXLex &, std::string const & filename);
lyx::pit_type pit);
bool do_writeFile(std::ostream & ofs) const; bool do_writeFile(std::ostream & ofs) const;