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 "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_);
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");
BOOST_ASSERT(cursor_.inTexted());
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:

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>
* buffer_funcs.C (bufferErrors): prevent endless loop (bug 2121)
@ -16,7 +35,8 @@
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>

View File

@ -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 & 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();

View File

@ -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.

View File

@ -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
// we are reading in a brand new document
BOOST_ASSERT(paragraphs().empty());
Buffer tmpbuf("", false);
tmpbuf.readHeader(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;
}
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;
}

View File

@ -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;