X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FBuffer.cpp;h=d68ef97c7a48c9eccce5515598ecdc983665eb51;hb=0011f47885e070cf36e92d3a2f11f29ce1d4fdf4;hp=78aaf190701e76ea13583563aa4187964d6189a9;hpb=a01cb111a07717a337c18860216359cdbe22af3c;p=lyx.git diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 78aaf19070..d68ef97c7a 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -14,7 +14,7 @@ #include "Buffer.h" #include "Author.h" -#include "BaseClassList.h" +#include "LayoutFile.h" #include "BiblioInfo.h" #include "BranchList.h" #include "buffer_funcs.h" @@ -51,7 +51,6 @@ #include "ParagraphParameters.h" #include "ParIterator.h" #include "PDFOptions.h" -#include "Session.h" #include "sgml.h" #include "TexRow.h" #include "TexStream.h" @@ -117,7 +116,7 @@ namespace os = support::os; namespace { -int const LYX_FORMAT = 317; // Uwe Stöhr: float placement support for wrap floats +int const LYX_FORMAT = 321; typedef map DepClean; typedef map > RefCache; @@ -174,6 +173,8 @@ public: typedef pair ScopeMacro; typedef map PositionScopeMacroMap; typedef map NamePositionScopeMacroMap; + /// map from the macro name to the position map, + /// which maps the macro definition position to the scope and the MacroData. NamePositionScopeMacroMap macros; bool macro_lock; @@ -181,7 +182,9 @@ public: typedef map BufferPositionMap; typedef pair ScopeBuffer; typedef map PositionScopeBufferMap; + /// position of children buffers in this buffer BufferPositionMap children_positions; + /// map from children inclusion positions to their scope and their buffer PositionScopeBufferMap position_to_children; /// Container for all sort of Buffer dependant errors. @@ -498,7 +501,8 @@ int Buffer::readHeader(Lexer & lex) LYXERR(Debug::PARSER, "Handling document header token: `" << token << '\''); - string unknown = params().readToken(lex, token, d->filename.onlyPath()); + string unknown = params().readToken(lex, token, d->filename.onlyPath(), + d->temppath); if (!unknown.empty()) { if (unknown[0] != '\\' && token == "\\textclass") { Alert::warning(_("Unknown document class"), @@ -571,6 +575,22 @@ bool Buffer::readDocument(Lexer & lex) // read main text bool const res = text().read(*this, lex, errorList, &(d->inset)); + // Enable embeded files, which will set temp path and move + // inconsistent inzip files if needed. + try { + embeddedFiles().validate(*this); + embeddedFiles().enable(params().embedded, *this, false); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + Alert::warning(_("Failed to read embedded files"), + _("Due to most likely a bug, LyX failed to locate all embedded " + "file. If you unzip the LyX file, you should be able to see and " + "open content.lyx which is your main text. You may also be able " + "to recover some embedded files. Please report this bug to the " + "lyx-devel mailing list.")); + return false; + } + updateMacros(); updateMacroInstances(); return res; @@ -905,18 +925,18 @@ bool Buffer::writeFile(FileName const & fname) const } if (!retval) { - message(str + _(" could not write file!.")); + message(str + _(" could not write file!")); return false; } removeAutosaveFile(d->filename.absFilename()); if (params().embedded) { - message(str + _(" writing embedded files!.")); + message(str + _(" writing embedded files.")); // if embedding is enabled, write file.lyx and all the embedded files // to the zip file fname. if (!d->embedded_files.writeFile(fname, *this)) { - message(str + _(" could not write embedded files!.")); + message(str + _(" could not write embedded files!")); return false; } } @@ -1352,33 +1372,28 @@ void Buffer::validate(LaTeXFeatures & features) const void Buffer::getLabelList(vector & list) const { - /// if this is a child document and the parent is already loaded - /// Use the parent's list instead [ale990407] - Buffer const * tmp = masterBuffer(); - if (!tmp) { - lyxerr << "masterBuffer() failed!" << endl; - BOOST_ASSERT(tmp); - } - if (tmp != this) { - tmp->getLabelList(list); + // If this is a child document, use the parent's list instead. + if (d->parent_buffer) { + d->parent_buffer->getLabelList(list); return; } - updateMacros(); - - for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) - it.nextInset()->getLabelList(list); + list.clear(); + Toc & toc = d->toc_backend.toc("label"); + TocIterator toc_it = toc.begin(); + TocIterator end = toc.end(); + for (; toc_it != end; ++toc_it) { + if (toc_it->depth() == 0) + list.push_back(toc_it->str()); + } } void Buffer::updateBibfilesCache() const { - // if this is a child document and the parent is already loaded - // update the parent's cache instead - Buffer const * tmp = masterBuffer(); - BOOST_ASSERT(tmp); - if (tmp != this) { - tmp->updateBibfilesCache(); + // If this is a child document, use the parent's cache instead. + if (d->parent_buffer) { + d->parent_buffer->updateBibfilesCache(); return; } @@ -1407,12 +1422,9 @@ void Buffer::updateBibfilesCache() const EmbeddedFileList const & Buffer::getBibfilesCache() const { - // if this is a child document and the parent is already loaded - // use the parent's cache instead - Buffer const * tmp = masterBuffer(); - BOOST_ASSERT(tmp); - if (tmp != this) - return tmp->getBibfilesCache(); + // If this is a child document, use the parent's cache instead. + if (d->parent_buffer) + return d->parent_buffer->getBibfilesCache(); // We update the cache when first used instead of at loading time. if (d->bibfilesCache_.empty()) @@ -1777,8 +1789,8 @@ MacroData const * Buffer::getMacro(docstring const & name, // If there is a master buffer, query that if (d->parent_buffer) { d->macro_lock = true; - MacroData const * macro - = d->parent_buffer->getMacro(name, *this, false); + MacroData const * macro = d->parent_buffer->getMacro( + name, *this, false); d->macro_lock = false; if (macro) return macro; @@ -1804,11 +1816,11 @@ MacroData const * Buffer::getMacro(docstring const & name, bool global) const } -MacroData const * Buffer::getMacro(docstring const & name, Buffer const & child, bool global) const +MacroData const * Buffer::getMacro(docstring const & name, + Buffer const & child, bool global) const { // look where the child buffer is included first - Impl::BufferPositionMap::iterator it - = d->children_positions.find(&child); + Impl::BufferPositionMap::iterator it = d->children_positions.find(&child); if (it == d->children_positions.end()) return 0; @@ -1817,28 +1829,14 @@ MacroData const * Buffer::getMacro(docstring const & name, Buffer const & child, } -void Buffer::updateEnvironmentMacros(DocIterator & it, - pit_type lastpit, - DocIterator & scope) const +void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const { - Paragraph & par = it.paragraph(); - depth_type depth - = par.params().depth(); - Length const & leftIndent - = par.params().leftIndent(); + pit_type lastpit = it.lastpit(); // look for macros in each paragraph while (it.pit() <= lastpit) { Paragraph & par = it.paragraph(); - // increased depth? - if ((par.params().depth() > depth - || par.params().leftIndent() != leftIndent) - && par.layout().isEnvironment()) { - updateBlockMacros(it, scope); - continue; - } - // iterate over the insets of the current paragraph InsetList const & insets = par.insetList(); InsetList::const_iterator iit = insets.begin(); @@ -1859,7 +1857,7 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, // collect macros in inset it.push_back(CursorSlice(*iit->inset)); - updateInsetMacros(it, newScope ? insetScope : scope); + updateMacros(it, newScope ? insetScope : scope); it.pop_back(); continue; } @@ -1868,7 +1866,7 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, if (iit->inset->lyxCode() == INCLUDE_CODE) { // get buffer of external file InsetCommand const & inset - = static_cast(*iit->inset); + = static_cast(*iit->inset); InsetCommandParams const & ip = inset.params(); d->macro_lock = true; Buffer * child = loadIfNeeded(*this, ip); @@ -1879,12 +1877,11 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, // register its position, but only when it is // included first in the buffer if (d->children_positions.find(child) - == d->children_positions.end()) + == d->children_positions.end()) d->children_positions[child] = it; // register child with its scope d->position_to_children[it] = Impl::ScopeBuffer(scope, child); - continue; } @@ -1906,8 +1903,8 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, continue; // register macro - d->macros[macroTemplate.name()][it] - = Impl::ScopeMacro(scope, MacroData(*this, it)); + d->macros[macroTemplate.name()][it] = + Impl::ScopeMacro(scope, MacroData(*this, it)); } // next paragraph @@ -1917,48 +1914,6 @@ void Buffer::updateEnvironmentMacros(DocIterator & it, } -void Buffer::updateBlockMacros(DocIterator & it, DocIterator & scope) const -{ - Paragraph & par = it.paragraph(); - - // set scope for macros in this paragraph: - // * either the "old" outer scope - // * or the scope ending after the environment - if (par.layout().isEnvironment()) { - // find end of environment block, - DocIterator envEnd = it; - pit_type n = it.lastpit() + 1; - depth_type depth = par.params().depth(); - Length const & length = par.params().leftIndent(); - // looping through the paragraph, basically until - // the layout changes or the depth gets smaller. - // (the logic of output_latex.cpp's TeXEnvironment) - do { - envEnd.pit()++; - if (envEnd.pit() == n) - break; - } while (par.layout() == envEnd.paragraph().layout() - || depth < envEnd.paragraph().params().depth() - || length != envEnd.paragraph().params().leftIndent()); - - // collect macros from environment block - updateEnvironmentMacros(it, envEnd.pit() - 1, envEnd); - } else { - // collect macros from paragraph - updateEnvironmentMacros(it, it.pit(), scope); - } -} - - -void Buffer::updateInsetMacros(DocIterator & it, DocIterator & scope) const -{ - // look for macros in each paragraph - pit_type n = it.lastpit() + 1; - while (it.pit() < n) - updateBlockMacros(it, scope); -} - - void Buffer::updateMacros() const { if (d->macro_lock) @@ -1978,7 +1933,7 @@ void Buffer::updateMacros() const DocIterator it = par_iterator_begin(); DocIterator outerScope = it; outerScope.pit() = outerScope.lastpit() + 2; - updateInsetMacros(it, outerScope); + updateMacros(it, outerScope); } @@ -2017,18 +1972,14 @@ void Buffer::listMacroNames(MacroNameSet & macros) const d->macro_lock = true; // loop over macro names - Impl::NamePositionScopeMacroMap::iterator nameIt - = d->macros.begin(); - Impl::NamePositionScopeMacroMap::iterator nameEnd - = d->macros.end(); + Impl::NamePositionScopeMacroMap::iterator nameIt = d->macros.begin(); + Impl::NamePositionScopeMacroMap::iterator nameEnd = d->macros.end(); for (; nameIt != nameEnd; ++nameIt) macros.insert(nameIt->first); // loop over children - Impl::BufferPositionMap::iterator it - = d->children_positions.begin(); - Impl::BufferPositionMap::iterator end - = d->children_positions.end(); + Impl::BufferPositionMap::iterator it = d->children_positions.begin(); + Impl::BufferPositionMap::iterator end = d->children_positions.end(); for (; it != end; ++it) it->first->listMacroNames(macros); @@ -2073,7 +2024,8 @@ Buffer::References & Buffer::references(docstring const & label) static InsetLabel const * dummy_il = 0; static References const dummy_refs; - it = d->ref_cache_.insert(make_pair(label, make_pair(dummy_il, dummy_refs))).first; + it = d->ref_cache_.insert( + make_pair(label, make_pair(dummy_il, dummy_refs))).first; return it->second.second; } @@ -2107,25 +2059,19 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to, InsetCode code) { //FIXME: This does not work for child documents yet. - BOOST_ASSERT(code == CITE_CODE || code == REF_CODE); + BOOST_ASSERT(code == CITE_CODE); // Check if the label 'from' appears more than once vector labels; - string paramName; - if (code == CITE_CODE) { - BiblioInfo keys; - keys.fillWithBibKeys(this); - BiblioInfo::const_iterator bit = keys.begin(); - BiblioInfo::const_iterator bend = keys.end(); + BiblioInfo keys; + keys.fillWithBibKeys(this); + BiblioInfo::const_iterator bit = keys.begin(); + BiblioInfo::const_iterator bend = keys.end(); - for (; bit != bend; ++bit) - // FIXME UNICODE - labels.push_back(bit->first); - paramName = "key"; - } else { - getLabelList(labels); - paramName = "reference"; - } + for (; bit != bend; ++bit) + // FIXME UNICODE + labels.push_back(bit->first); + paramName = "key"; if (count(labels.begin(), labels.end(), from) > 1) return; @@ -2158,31 +2104,30 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin, d->texrow.newline(); if (isLatex()) writeLaTeXSource(os, filePath(), runparams, true, true); - else { + else writeDocBookSource(os, absFileName(), runparams, false); - } } else { runparams.par_begin = par_begin; runparams.par_end = par_end; - if (par_begin + 1 == par_end) + if (par_begin + 1 == par_end) { os << "% " << bformat(_("Preview source code for paragraph %1$d"), par_begin) << "\n\n"; - else + } else { os << "% " << bformat(_("Preview source code from paragraph %1$s to %2$s"), convert(par_begin), convert(par_end - 1)) << "\n\n"; + } d->texrow.newline(); d->texrow.newline(); // output paragraphs - if (isLatex()) { + if (isLatex()) latexParagraphs(*this, text(), os, d->texrow, runparams); - } else { + else // DocBook docbookParagraphs(paragraphs(), *this, os, runparams); - } } }