X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FBuffer.cpp;h=97a6cfbd31afe1e771034326ef982b37fff7a7fb;hb=294e4884ee29585d311177406cd31499e6d81877;hp=9b6727fd46c54af1751b59b5af1ff5069c5fc1fb;hpb=a9d9fdc9bfbb23303d3988e9973239568b82125c;p=lyx.git diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 9b6727fd46..97a6cfbd31 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; @@ -1093,6 +1093,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(); @@ -1858,6 +1862,7 @@ Buffer::ExportStatus Buffer::writeLaTeXSource(otexstream & os, // This is only set once per document (in master) if (!runparams.is_child) { runparams.use_polyglossia = features.usePolyglossia(); + runparams.use_hyperref = features.isRequired("hyperref"); runparams.use_CJK = features.mustProvide("CJK"); } LYXERR(Debug::LATEX, " Buffer validation done."); @@ -2107,7 +2112,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; @@ -2118,85 +2123,56 @@ 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; bool const output_body = output == FullSource || output == OnlyBody; - if (output_preamble) { - if (runparams.flavor == OutputParams::XML) - os << "\n"; - - // FIXME UNICODE - os << "\n"; - preamble += "\n"; - preamble += "\n"; - preamble += "\n"; - } + if (output_preamble) { + // 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(); - sgml::openTag(os, top); - os << '\n'; - try { - docbookParagraphs(text(), *this, os, runparams); - } - catch (ConversionException const &) { return ExportKilled; } - sgml::closeTag(os, top_element); + // Start to output the document. + docbookParagraphs(text(), *this, xs, runparams); + } + + if (output_preamble) { + // Close the root element. + os << "\n"; } return ExportSuccess; } @@ -2254,17 +2230,13 @@ 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(); if (!styles.empty()) os << "\n\n" << styles << '\n'; - styles = features.getPreambleSnippets().str; - if (!styles.empty()) - os << "\n\n" << styles << '\n'; - // we will collect CSS information in a stream, and then output it // either here, as part of the header, or else in a separate file. odocstringstream css; @@ -2324,7 +2296,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(); @@ -2401,6 +2373,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); @@ -2613,6 +2588,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) { @@ -3008,7 +2986,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); @@ -3086,9 +3064,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); } @@ -3177,7 +3156,7 @@ Language const * Buffer::language() const } -docstring const Buffer::B_(string const & l10n) const +docstring Buffer::B_(string const & l10n) const { return params().B_(l10n); } @@ -3408,7 +3387,7 @@ vector> const Buffer::prepareBibFilePaths(OutputParams c enc = params().bibFileEncoding(utf8input); bool recorded = false; - for (pair pe : res) { + for (auto const & pe : res) { if (pe.first == path) { recorded = true; break; @@ -3475,8 +3454,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(); } @@ -3497,8 +3505,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; @@ -3769,7 +3775,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; @@ -3864,7 +3870,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); @@ -3996,7 +4002,7 @@ void Buffer::setInsetLabel(docstring const & label, InsetLabel const * il, InsetLabel const * Buffer::insetLabel(docstring const & label, bool const active) const { - for (auto & rc : masterBuffer()->d->label_cache_) { + for (auto const & rc : masterBuffer()->d->label_cache_) { if (rc.label == label && (rc.active || !active)) return rc.inset; } @@ -4088,7 +4094,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) { @@ -4097,8 +4103,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, @@ -4112,6 +4119,7 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, LaTeXFeatures features(*this, params(), runparams); validate(features); runparams.use_polyglossia = features.usePolyglossia(); + runparams.use_hyperref = features.isRequired("hyperref"); // latex or literate otexstream ots(os); // output above @@ -4149,8 +4157,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); @@ -4235,91 +4243,6 @@ void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui) } - -namespace { - -class AutoSaveBuffer : public ForkedProcess { -public: - /// - AutoSaveBuffer(Buffer const & buffer, FileName const & fname) - : buffer_(buffer), fname_(fname) {} - /// - virtual shared_ptr clone() const - { - return make_shared(*this); - } - /// - int start() - { - command_ = to_utf8(bformat(_("Auto-saving %1$s"), - from_utf8(fname_.absFileName()))); - return run(DontWait); - } -private: - /// - virtual int generateChild(); - /// - Buffer const & buffer_; - FileName fname_; -}; - - -int AutoSaveBuffer::generateChild() -{ -#if defined(__APPLE__) - /* FIXME fork() is not usable for autosave on Mac OS X 10.6 (snow leopard) - * We should use something else like threads. - * - * Since I do not know how to determine at run time what is the OS X - * version, I just disable forking altogether for now (JMarc) - */ - pid_t const pid = -1; -#else - // tmp_ret will be located (usually) in /tmp - // will that be a problem? - // Note that this calls ForkedCalls::fork(), so it's - // ok cross-platform. - pid_t const pid = fork(); - // If you want to debug the autosave - // you should set pid to -1, and comment out the fork. - if (pid != 0 && pid != -1) - return pid; -#endif - - // pid = -1 signifies that lyx was unable - // to fork. But we will do the save - // anyway. - bool failed = false; - TempFile tempfile("lyxautoXXXXXX.lyx"); - tempfile.setAutoRemove(false); - FileName const tmp_ret = tempfile.name(); - if (!tmp_ret.empty()) { - if (!buffer_.writeFile(tmp_ret)) - failed = true; - else if (!tmp_ret.moveTo(fname_)) - failed = true; - } else - failed = true; - - if (failed) { - // failed to write/rename tmp_ret so try writing direct - if (!buffer_.writeFile(fname_)) { - // It is dangerous to do this in the child, - // but safe in the parent, so... - if (pid == -1) // emit message signal. - buffer_.message(_("Autosave failed!")); - } - } - - if (pid == 0) // we are the child so... - _exit(0); - - return pid; -} - -} // namespace - - FileName Buffer::getEmergencyFileName() const { return FileName(d->filename.absFileName() + ".emergency"); @@ -4416,7 +4339,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir string & result_file) const { bool const update_unincluded = - params().maintain_unincluded_children + params().maintain_unincluded_children != BufferParams::CM_None && !params().getIncludedChildren().empty(); // (1) export with all included children (omit \includeonly) @@ -4544,8 +4467,9 @@ 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()) { + // DocBook backend + else if (backend_format == "docbook5") { + runparams.flavor = OutputParams::DOCBOOK5; runparams.nice = !put_in_tempdir; if (makeDocBookFile(FileName(filename), runparams) == ExportKilled) return ExportKilled; @@ -4587,8 +4511,9 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir string const ext = theFormats().extension(format); FileName const tmp_result_file(changeExtension(filename, ext)); Converters::RetVal const retval = - converters.convert(this, FileName(filename), tmp_result_file, - FileName(absFileName()), backend_format, format, error_list); + converters.convert(this, FileName(filename), tmp_result_file, + FileName(absFileName()), backend_format, format, + error_list, Converters::none, includeall); if (retval == Converters::KILLED) return ExportCancel; bool success = (retval == Converters::SUCCESS); @@ -4703,7 +4628,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir Buffer::ExportStatus Buffer::preview(string const & format) const { bool const update_unincluded = - params().maintain_unincluded_children + params().maintain_unincluded_children != BufferParams::CM_None && !params().getIncludedChildren().empty(); return preview(format, update_unincluded); } @@ -5034,6 +4959,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 @@ -5076,6 +5003,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(); @@ -5304,8 +5232,9 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const } -void Buffer::updateBuffer(ParIterator & parit, 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, /**/); @@ -5313,11 +5242,6 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const // 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; @@ -5350,7 +5274,7 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const // now the insets for (auto const & insit : parit->insetList()) { parit.pos() = insit.pos; - insit.inset->updateBuffer(parit, utype); + insit.inset->updateBuffer(parit, utype, deleted || parit->isDeleted(insit.pos)); changed |= insit.inset->isChanged(); } @@ -5361,6 +5285,7 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const // set change indicator for the inset (or the cell that the iterator // points to, if applicable). parit.text()->inset().isChanged(changed); + popIncludedBuffer(); } @@ -5437,8 +5362,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 { @@ -5677,4 +5605,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