X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FBuffer.cpp;h=1ccb263014651595f417bb099d9ea820c8a4b522;hb=bea9d2f3f774d62aa48fe83ebbefdd0e7d5f85b9;hp=4be7e2119701c6a456ee574300d5aa3ffb18c39d;hpb=f9999039ac7278a719513fdd15c34f12846a1189;p=lyx.git diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 4be7e21197..1ccb263014 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -46,18 +46,18 @@ #include "LyX.h" #include "LyXRC.h" #include "LyXVC.h" -#include "output_docbook.h" #include "output.h" #include "output_latex.h" -#include "output_xhtml.h" +#include "output_docbook.h" #include "output_plaintext.h" +#include "output_xhtml.h" #include "Paragraph.h" #include "ParagraphParameters.h" #include "ParIterator.h" #include "PDFOptions.h" #include "Session.h" #include "SpellChecker.h" -#include "sgml.h" +#include "xml.h" #include "texstream.h" #include "TexRow.h" #include "Text.h" @@ -111,8 +111,6 @@ #include "support/textutils.h" #include "support/types.h" -#include "support/bind.h" - #include #include #include @@ -297,6 +295,8 @@ public: /// CloneList_ptr clone_list_; + /// + std::list include_list_; private: /// So we can force access via the accessors. mutable Buffer const * parent_buffer; @@ -664,7 +664,6 @@ void Buffer::cloneWithChildren(BufferMap & bufmap, CloneList_ptr clones) const buffer_clone->setChild(dit, child_clone); } buffer_clone->d->macro_lock = false; - return; } @@ -1093,6 +1092,10 @@ bool Buffer::readDocument(Lexer & lex) d->old_position = params().origin; else d->old_position = filePath(); + + if (!parent()) + clearIncludeList(); + bool const res = text().read(lex, errorList, d->inset); d->old_position.clear(); @@ -1353,7 +1356,7 @@ Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn, LYXERR(Debug::INFO, "Running '" << command_str << '\''); cmd_ret const ret = runCommand(command_str); - if (ret.first != 0) { + if (!ret.valid) { if (from_format < LYX_FORMAT) { Alert::error(_("Conversion script failed"), bformat(_("%1$s is from an older version" @@ -1545,9 +1548,8 @@ bool Buffer::save() const // time stamp is invalidated by copying/moving saveCheckSum(); markClean(); - if (d->file_format != LYX_FORMAT) - // the file associated with this buffer is now in the current format - d->file_format = LYX_FORMAT; + // the file associated with this buffer is now in the current format + d->file_format = LYX_FORMAT; return true; } // else we saved the file, but failed to move it to the right location. @@ -1901,9 +1903,9 @@ Buffer::ExportStatus Buffer::writeLaTeXSource(otexstream & os, docstring uncodable_glyphs; Encoding const * const enc = runparams.encoding; if (enc) { - for (size_t n = 0; n < inputpath.size(); ++n) { - if (!enc->encodable(inputpath[n])) { - docstring const glyph(1, inputpath[n]); + for (char_type n : inputpath) { + if (!enc->encodable(n)) { + docstring const glyph(1, n); LYXERR0("Uncodable character '" << glyph << "' in input path!"); @@ -2108,7 +2110,7 @@ Buffer::ExportStatus Buffer::makeDocBookFile(FileName const & fname, updateMacroInstances(OutputUpdate); ExportStatus const retval = - writeDocBookSource(ofs, fname.absFileName(), runparams, output); + writeDocBookSource(ofs, runparams, output); if (retval == ExportKilled) return ExportKilled; @@ -2119,17 +2121,17 @@ Buffer::ExportStatus Buffer::makeDocBookFile(FileName const & fname, } -Buffer::ExportStatus Buffer::writeDocBookSource(odocstream & os, string const & fname, +Buffer::ExportStatus Buffer::writeDocBookSource(odocstream & os, OutputParams const & runparams, OutputWhat output) const { LaTeXFeatures features(*this, params(), runparams); validate(features); + d->bibinfo_.makeCitationLabels(*this); d->texrow.reset(); DocumentClass const & tclass = params().documentClass(); - string const & top_element = tclass.latexname(); bool const output_preamble = output == FullSource || output == OnlyPreamble; @@ -2137,68 +2139,38 @@ Buffer::ExportStatus Buffer::writeDocBookSource(odocstream & os, string const & output == FullSource || output == OnlyBody; if (output_preamble) { - if (runparams.flavor == OutputParams::XML) - os << "\n"; - - // FIXME UNICODE - os << "\n"; - preamble += "\n"; - preamble += "\n"; - preamble += "\n"; - } + // XML preamble, no doctype needed. + // Not using XMLStream for this, as the root tag would be in the tag stack and make troubles with the error + // detection mechanisms (these are called before the end tag is output, and thus interact with the canary + // parsep in output_docbook.cpp). + os << "\n" + << "\n"; - string const name = runparams.nice - ? changeExtension(absFileName(), ".sgml") : fname; - preamble += features.getIncludedFiles(name); - preamble += features.getLyXSGMLEntities(); + // Directly output the root tag, based on the current type of document. + string languageCode = params().language->code(); + string params = "xml:lang=\"" + languageCode + '"' + + " xmlns=\"http://docbook.org/ns/docbook\"" + + " xmlns:xlink=\"http://www.w3.org/1999/xlink\"" + + " xmlns:m=\"http://www.w3.org/1998/Math/MathML\"" + + " xmlns:xi=\"http://www.w3.org/2001/XInclude\"" + + " version=\"5.2\""; - if (!preamble.empty()) { - os << "\n [ " << preamble << " ]"; - } - os << ">\n\n"; + os << "<" << from_ascii(tclass.docbookroot()) << " " << from_ascii(params) << ">\n"; } if (output_body) { - string top = top_element; - top += " lang=\""; - if (runparams.flavor == OutputParams::XML) - top += params().language->code(); - else - top += params().language->code().substr(0, 2); - top += '"'; - - if (!params().options.empty()) { - top += ' '; - top += params().options; - } - - os << "\n"; - - params().documentClass().counters().reset(); + // Start to output the document. + XMLStream xs(os); + docbookParagraphs(text(), *this, xs, runparams); + } - sgml::openTag(os, top); - os << '\n'; - try { - docbookParagraphs(text(), *this, os, runparams); - } - catch (ConversionException const &) { return ExportKilled; } - sgml::closeTag(os, top_element); + if (output_preamble) { + // Close the root element. No need for a line break, as free text is never allowed + // in a root element, it must always be wrapped in some container. + os << ""; } + return ExportSuccess; } @@ -2255,7 +2227,7 @@ Buffer::ExportStatus Buffer::writeLyXHTMLSource(odocstream & os, os << "" << (doctitle.empty() ? from_ascii("LyX Document") : - html::htmlize(doctitle, XHTMLStream::ESCAPE_ALL)) + xml::escapeString(doctitle, XMLStream::ESCAPE_ALL)) << "\n"; docstring styles = features.getTClassHTMLPreamble(); @@ -2321,7 +2293,7 @@ Buffer::ExportStatus Buffer::writeLyXHTMLSource(odocstream & os, bool const output_body_tag = (output != IncludedFile); if (output_body_tag) os << "\n"; - XHTMLStream xs(os); + XMLStream xs(os); if (output != IncludedFile) // if we're an included file, the counters are in the master. params().documentClass().counters().reset(); @@ -2398,6 +2370,9 @@ void Buffer::validate(LaTeXFeatures & features) const if (!features.runparams().is_child) params().validate(features); + if (!parent()) + clearIncludeList(); + for (Paragraph const & p : paragraphs()) p.validate(features); @@ -2610,6 +2585,9 @@ void Buffer::reloadBibInfoCache(bool const force) const void Buffer::collectBibKeys(FileNameList & checkedFiles) const { + if (!parent()) + clearIncludeList(); + for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { it->collectBibKeys(it, checkedFiles); if (it->lyxCode() == BIBITEM_CODE) { @@ -3005,7 +2983,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) // get buffer of external file InsetInclude const & ins = static_cast(*it); - Buffer * child = ins.getChildBuffer(); + Buffer * child = ins.loadIfNeeded(); if (!child) continue; child->dispatch(func, dr); @@ -3083,9 +3061,10 @@ void Buffer::changeLanguage(Language const * from, Language const * to) LASSERT(from, return); LASSERT(to, return); - for_each(par_iterator_begin(), - par_iterator_end(), - bind(&Paragraph::changeLanguage, _1, params(), from, to)); + ParIterator it = par_iterator_begin(); + ParIterator eit = par_iterator_end(); + for (; it != eit; ++it) + it->changeLanguage(params(), from, to); } @@ -3472,8 +3451,37 @@ bool Buffer::isReadonly() const void Buffer::setParent(Buffer const * buffer) { - // Avoids recursive include. - d->setParent(buffer == this ? nullptr : buffer); + // We need to do some work here to avoid recursive parent structures. + // This is the easy case. + if (buffer == this) { + LYXERR0("Ignoring attempt to set self as parent in\n" << fileName()); + return; + } + // Now we check parents going upward, to make sure that IF we set the + // parent as requested, we would not generate a recursive include. + set sb; + Buffer const * b = buffer; + bool found_recursion = false; + while (b) { + if (sb.find(b) != sb.end()) { + found_recursion = true; + break; + } + sb.insert(b); + b = b->parent(); + } + + if (found_recursion) { + LYXERR0("Ignoring attempt to set parent of\n" << + fileName() << + "\nto " << + buffer->fileName() << + "\nbecause that would create a recursive inclusion."); + return; + } + + // We should be safe now. + d->setParent(buffer); updateMacros(); } @@ -3494,8 +3502,6 @@ ListOfBuffers Buffer::allRelatives() const Buffer const * Buffer::masterBuffer() const { - // FIXME Should be make sure we are not in some kind - // of recursive include? A -> B -> A will crash this. Buffer const * const pbuf = d->parent(); if (!pbuf) return this; @@ -3766,7 +3772,7 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope) InsetInclude const & incinset = static_cast(*insit.inset); macro_lock = true; - Buffer * child = incinset.getChildBuffer(); + Buffer * child = incinset.loadIfNeeded(); macro_lock = false; if (!child) continue; @@ -3861,7 +3867,7 @@ void Buffer::getUsedBranches(std::list & result, bool const from_mast // get buffer of external file InsetInclude const & ins = static_cast(*it); - Buffer * child = ins.getChildBuffer(); + Buffer * child = ins.loadIfNeeded(); if (!child) continue; child->getUsedBranches(result, true); @@ -4003,10 +4009,7 @@ InsetLabel const * Buffer::insetLabel(docstring const & label, bool Buffer::activeLabel(docstring const & label) const { - if (!insetLabel(label, true)) - return false; - - return true; + return insetLabel(label, true) != nullptr; } @@ -4085,7 +4088,7 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, par.write(ods, params(), dt); os << from_utf8(ods.str()); } else if (runparams.flavor == OutputParams::HTML) { - XHTMLStream xs(os); + XMLStream xs(os); setMathFlavor(runparams); xhtmlParagraphs(text(), *this, xs, runparams); } else if (runparams.flavor == OutputParams::TEXT) { @@ -4094,8 +4097,9 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, // Probably should have some routine with a signature like them. writePlaintextParagraph(*this, text().paragraphs()[par_begin], os, runparams, dummy); - } else if (params().isDocBook()) { - docbookParagraphs(text(), *this, os, runparams); + } else if (runparams.flavor == OutputParams::DOCBOOK5) { + XMLStream xs{os}; + docbookParagraphs(text(), *this, xs, runparams); } else { // If we are previewing a paragraph, even if this is the // child of some other buffer, let's cut the link here, @@ -4147,8 +4151,8 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, os << "% "<< _("Plain text does not have a preamble."); } else writePlaintextFile(*this, os, runparams); - } else if (params().isDocBook()) { - writeDocBookSource(os, absFileName(), runparams, output); + } else if (runparams.flavor == OutputParams::DOCBOOK5) { + writeDocBookSource(os, runparams, output); } else { // latex or literate otexstream ots(os); @@ -4404,7 +4408,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir // Only show this alert if this is an export to a non-temporary // file (not for previewing). docstring s = bformat(_("No information for exporting the format %1$s."), - theFormats().prettyName(format)); + translateIfPossible(theFormats().prettyName(format))); if (format == "pdf4") s += "\n" + bformat(_("Hint: use non-TeX fonts or set input encoding " @@ -4457,9 +4461,10 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir return ExportKilled; } else if (backend_format == "lyx") writeFile(FileName(filename)); - // Docbook backend - else if (params().isDocBook()) { - runparams.nice = !put_in_tempdir; + // DocBook backend + else if (backend_format == "docbook5") { + runparams.flavor = OutputParams::DOCBOOK5; + runparams.nice = false; if (makeDocBookFile(FileName(filename), runparams) == ExportKilled) return ExportKilled; } @@ -4601,13 +4606,13 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir } else { message(bformat(_("Document exported as %1$s " "to file `%2$s'"), - theFormats().prettyName(format), + translateIfPossible(theFormats().prettyName(format)), makeDisplayPath(result_file))); } } else { // This must be a dummy converter like fax (bug 1888) message(bformat(_("Document exported as %1$s"), - theFormats().prettyName(format))); + translateIfPossible(theFormats().prettyName(format)))); } return success ? ExportSuccess : ExportConverterError; @@ -4948,6 +4953,8 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const // do the real work ParIterator parit = cbuf.par_iterator_begin(); + if (scope == UpdateMaster) + clearIncludeList(); updateBuffer(parit, utype); // If this document has siblings, then update the TocBackend later. The @@ -4990,6 +4997,7 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const } d->cite_labels_valid_ = true; /// FIXME: Perf + clearIncludeList(); cbuf.tocBackend().update(true, utype); if (scope == UpdateMaster) cbuf.structureChanged(); @@ -5220,6 +5228,7 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const void Buffer::updateBuffer(ParIterator & parit, UpdateType utype, bool const deleted) const { + pushIncludedBuffer(this); // LASSERT: Is it safe to continue here, or should we just return? LASSERT(parit.pit() == 0, /**/); @@ -5227,11 +5236,6 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype, bool const dele // to resolve macros in it. parit.text()->setMacrocontextPosition(parit); - // Reset bibitem counter in master (#8499) - Buffer const * const master = masterBuffer(); - if (master == this && !d->ignore_parent) - master->params().documentClass().counters().reset(from_ascii("bibitem")); - depth_type maxdepth = 0; pit_type const lastpit = parit.lastpit(); bool changed = false; @@ -5275,6 +5279,7 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype, bool const dele // set change indicator for the inset (or the cell that the iterator // points to, if applicable). parit.text()->inset().isChanged(changed); + popIncludedBuffer(); } @@ -5351,8 +5356,11 @@ void Buffer::Impl::updateStatistics(DocIterator & from, DocIterator & to, bool s ++word_count_; inword = true; } - if (ins && ins->isLetter()) - ++char_count_; + if (ins && ins->isLetter()) { + odocstringstream os; + ins->toString(os); + char_count_ += os.str().length(); + } else if (ins && ins->isSpace()) ++blank_count_; else { @@ -5591,4 +5599,55 @@ void Buffer::clearExternalModification() const } +void Buffer::pushIncludedBuffer(Buffer const * buf) const +{ + masterBuffer()->d->include_list_.push_back(buf); + if (lyxerr.debugging(Debug::FILES)) { + LYXERR0("Pushed. Stack now:"); + if (masterBuffer()->d->include_list_.empty()) + LYXERR0("EMPTY!"); + else + for (auto const & b : masterBuffer()->d->include_list_) + LYXERR0(b->fileName()); + } +} + + +void Buffer::popIncludedBuffer() const +{ + masterBuffer()->d->include_list_.pop_back(); + if (lyxerr.debugging(Debug::FILES)) { + LYXERR0("Popped. Stack now:"); + if (masterBuffer()->d->include_list_.empty()) + LYXERR0("EMPTY!"); + else + for (auto const & b : masterBuffer()->d->include_list_) + LYXERR0(b->fileName()); + } +} + + +bool Buffer::isBufferIncluded(Buffer const * buf) const +{ + if (!buf) + return false; + if (lyxerr.debugging(Debug::FILES)) { + LYXERR0("Checking for " << buf->fileName() << ". Stack now:"); + if (masterBuffer()->d->include_list_.empty()) + LYXERR0("EMPTY!"); + else + for (auto const & b : masterBuffer()->d->include_list_) + LYXERR0(b->fileName()); + } + list const & blist = masterBuffer()->d->include_list_; + return find(blist.begin(), blist.end(), buf) != blist.end(); +} + + +void Buffer::clearIncludeList() const +{ + LYXERR(Debug::FILES, "Clearing include list for " << fileName()); + d->include_list_.clear(); +} + } // namespace lyx