X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FBuffer.cpp;h=445503993eae7ad536bd105b1649645d89c93ed8;hb=d9082639080b9de993742bd352f92e5183058cf5;hp=53ff15b4f6cad6674fa1c215ea16a95ffb74ac09;hpb=d1b9af3b1dede776f92910378f538f881a68d28e;p=lyx.git diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 53ff15b4f6..445503993e 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -14,7 +14,6 @@ #include "Buffer.h" #include "Author.h" -#include "LayoutFile.h" #include "BiblioInfo.h" #include "BranchList.h" #include "buffer_funcs.h" @@ -67,12 +66,9 @@ #include "VCBackend.h" #include "version.h" #include "WordLangTuple.h" -#include "WordList.h" -#include "insets/InsetBibtex.h" #include "insets/InsetBranch.h" #include "insets/InsetInclude.h" -#include "insets/InsetTabular.h" #include "insets/InsetText.h" #include "mathed/InsetMathHull.h" @@ -80,7 +76,6 @@ #include "mathed/InsetMathMacroTemplate.h" #include "mathed/MathSupport.h" -#include "graphics/GraphicsCache.h" #include "graphics/PreviewLoader.h" #include "frontends/Application.h" @@ -97,11 +92,9 @@ #include "support/FileName.h" #include "support/FileNameList.h" #include "support/filetools.h" -#include "support/ForkedCalls.h" #include "support/gettext.h" #include "support/gzstream.h" #include "support/lstrings.h" -#include "support/lyxalgo.h" #include "support/mutex.h" #include "support/os.h" #include "support/Package.h" @@ -1189,9 +1182,8 @@ bool Buffer::readString(string const & s) Buffer::ReadStatus Buffer::readFile(FileName const & fn) { - FileName fname(fn); Lexer lex; - if (!lex.setFile(fname)) { + if (!lex.setFile(fn)) { Alert::error(_("File Not Found"), bformat(_("Unable to open file `%1$s'."), from_utf8(fn.absFileName()))); @@ -1282,9 +1274,9 @@ void Buffer::updatePreviews() const if (!ploader) return; - InsetIterator it = inset_iterator_begin(*d->inset); - InsetIterator const end = inset_iterator_end(*d->inset); - for (; it != end; ++it) + InsetIterator it = begin(*d->inset); + InsetIterator const itend = end(*d->inset); + for (; it != itend; ++it) it->addPreview(it, *ploader); ploader->startLoading(); @@ -1616,7 +1608,7 @@ bool Buffer::writeFile(FileName const & fname) const } -docstring Buffer::emergencyWrite() +docstring Buffer::emergencyWrite() const { // No need to save if the buffer has not changed. if (isClean()) @@ -1779,7 +1771,7 @@ Buffer::ExportStatus Buffer::makeLaTeXFile(FileName const & fname, catch (EncodingException const & e) { docstring const failed(1, e.failed_char); ostringstream oss; - oss << "0x" << hex << e.failed_char << dec; + oss << "0x" << hex << static_cast(e.failed_char) << dec; if (getParFromID(e.par_id).paragraph().layout().pass_thru) { docstring msg = bformat(_("Uncodable character '%1$s'" " (code point %2$s)"), @@ -2145,7 +2137,7 @@ Buffer::ExportStatus Buffer::writeDocBookSource(odocstream & os, // parsep in output_docbook.cpp). os << "\n" << "\n"; + << "\n See https://www.lyx.org/ for more information -->\n"; // Directly output the root tag, based on the current type of document. string languageCode = params().language->code(); @@ -2328,7 +2320,7 @@ int Buffer::runChktex() // Generate the LaTeX file if neccessary OutputParams runparams(¶ms().encoding()); - runparams.flavor = OutputParams::LATEX; + runparams.flavor = Flavor::LaTeX; runparams.nice = false; runparams.linelen = lyxrc.plaintext_linelen; ExportStatus const retval = @@ -2588,7 +2580,7 @@ void Buffer::collectBibKeys(FileNameList & checkedFiles) const if (!parent()) clearIncludeList(); - for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { + for (InsetIterator it = begin(inset()); it; ++it) { it->collectBibKeys(it, checkedFiles); if (it->lyxCode() == BIBITEM_CODE) { if (parent() != nullptr) @@ -2600,18 +2592,16 @@ void Buffer::collectBibKeys(FileNameList & checkedFiles) const } -void Buffer::addBiblioInfo(BiblioInfo const & bin) const +void Buffer::addBiblioInfo(BiblioInfo const & bi_in) const { - // We add the biblio info to the master buffer, - // if there is one, but also to every single buffer, - // in case a child is compiled alone. - BiblioInfo & bi = d->bibinfo_; - bi.mergeBiblioInfo(bin); + // We add the biblio info to the parent buffer, + // if there is one, but also to this buffer, in case + // it is compiled alone. + BiblioInfo & our_bi = d->bibinfo_; + our_bi.mergeBiblioInfo(bi_in); - if (parent() != nullptr) { - BiblioInfo & masterbi = parent()->d->bibinfo_; - masterbi.mergeBiblioInfo(bin); - } + if (parent()) + parent()->addBiblioInfo(bi_in); } @@ -2708,7 +2698,7 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag) break; case LFUN_BUFFER_EXPORT: { - docstring const arg = cmd.argument(); + docstring const & arg = cmd.argument(); if (arg == "custom") { enable = true; break; @@ -2737,7 +2727,7 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag) || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE); BranchList const & branchList = master ? masterBuffer()->params().branchlist() : params().branchlist(); - docstring const branchName = cmd.argument(); + docstring const & branchName = cmd.argument(); flag.setEnabled(!branchName.empty() && branchList.find(branchName)); break; } @@ -2899,7 +2889,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) Buffer * buf = master ? const_cast(masterBuffer()) : this; - docstring const branch_name = func.argument(); + docstring const & branch_name = func.argument(); // the case without a branch name is handled elsewhere if (branch_name.empty()) { dispatched = false; @@ -2927,7 +2917,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) } case LFUN_BRANCH_ADD: { - docstring branchnames = func.argument(); + docstring const & branchnames = func.argument(); if (branchnames.empty()) { dispatched = false; break; @@ -2966,10 +2956,10 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) docstring const oldname = from_utf8(func.getArg(0)); docstring const newname = from_utf8(func.getArg(1)); - InsetIterator it = inset_iterator_begin(inset()); - InsetIterator const end = inset_iterator_end(inset()); + InsetIterator it = begin(inset()); + InsetIterator const itend = end(inset()); bool success = false; - for (; it != end; ++it) { + for (; it != itend; ++it) { if (it->lyxCode() == BRANCH_CODE) { InsetBranch & ins = static_cast(*it); if (ins.branch() == oldname) { @@ -3146,6 +3136,11 @@ ParConstIterator Buffer::par_iterator_end() const return ParConstIterator(doc_iterator_end(this)); } +bool Buffer::empty() const +{ + return paragraphs().size() == 1 && paragraphs().front().empty(); +} + Language const * Buffer::language() const { @@ -3532,19 +3527,19 @@ bool Buffer::hasChildren() const } -void Buffer::collectChildren(ListOfBuffers & clist, bool grand_children) const +void Buffer::collectChildren(ListOfBuffers & children, bool grand_children) const { // loop over children for (auto const & p : d->children_positions) { Buffer * child = const_cast(p.first); // No duplicates - ListOfBuffers::const_iterator bit = find(clist.begin(), clist.end(), child); - if (bit != clist.end()) + ListOfBuffers::const_iterator bit = find(children.begin(), children.end(), child); + if (bit != children.end()) continue; - clist.push_back(child); + children.push_back(child); if (grand_children) // there might be grandchildren - child->collectChildren(clist, true); + child->collectChildren(children, true); } } @@ -3733,7 +3728,6 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope) while (it.pit() <= lastpit) { Paragraph & par = it.paragraph(); - // FIXME Can this be done with the new-style iterators? // iterate over the insets of the current paragraph for (auto const & insit : par.insetList()) { it.pos() = insit.pos; @@ -3779,9 +3773,7 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope) // register its position, but only when it is // included first in the buffer - if (children_positions.find(child) == - children_positions.end()) - children_positions[child] = it; + children_positions.insert({child, it}); // register child with its scope position_to_children[it] = Impl::ScopeBuffer(scope, child); @@ -3851,11 +3843,9 @@ void Buffer::updateMacros() const void Buffer::getUsedBranches(std::list & result, bool const from_master) const { - InsetIterator it = inset_iterator_begin(inset()); - InsetIterator const end = inset_iterator_end(inset()); - for (; it != end; ++it) { - if (it->lyxCode() == BRANCH_CODE) { - InsetBranch & br = static_cast(*it); + for (Inset const & it : inset()) { + if (it.lyxCode() == BRANCH_CODE) { + InsetBranch const & br = static_cast(it); docstring const name = br.branch(); if (!from_master && !params().branchlist().find(name)) result.push_back(name); @@ -3863,10 +3853,10 @@ void Buffer::getUsedBranches(std::list & result, bool const from_mast result.push_back(name); continue; } - if (it->lyxCode() == INCLUDE_CODE) { + if (it.lyxCode() == INCLUDE_CODE) { // get buffer of external file InsetInclude const & ins = - static_cast(*it); + static_cast(it); Buffer * child = ins.loadIfNeeded(); if (!child) continue; @@ -3892,9 +3882,9 @@ void Buffer::updateMacroInstances(UpdateType utype) const continue; // update macro in all cells of the InsetMathNest - DocIterator::idx_type n = minset->nargs(); + idx_type n = minset->nargs(); MacroContext mc = MacroContext(this, it); - for (DocIterator::idx_type i = 0; i < n; ++i) { + for (idx_type i = 0; i < n; ++i) { MathData & data = minset->cell(i); data.updateMacros(nullptr, mc, utype, 0); } @@ -4037,7 +4027,7 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to) string const paramName = "key"; UndoGroupHelper ugh(this); - InsetIterator it = inset_iterator_begin(inset()); + InsetIterator it = begin(inset()); for (; it; ++it) { if (it->lyxCode() != CITE_CODE) continue; @@ -4066,38 +4056,49 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, // Some macros rely on font encoding runparams.main_fontenc = params().main_font_encoding(); + // Use the right wrapping for the comment at the beginning of the generated + // snippet, so that it is either valid LaTeX or valid XML (including HTML and DocBook). + docstring comment_start = from_ascii("% "); + docstring comment_end = from_ascii(""); + if (runparams.flavor == Flavor::Html || runparams.flavor == Flavor::DocBook5) { + comment_start = from_ascii(""); + } + if (output == CurrentParagraph) { runparams.par_begin = par_begin; runparams.par_end = par_end; if (par_begin + 1 == par_end) { - os << "% " + os << comment_start << bformat(_("Preview source code for paragraph %1$d"), par_begin) + << comment_end << "\n\n"; } else { - os << "% " + os << comment_start << bformat(_("Preview source code from paragraph %1$s to %2$s"), convert(par_begin), convert(par_end - 1)) + << comment_end << "\n\n"; } // output paragraphs - if (runparams.flavor == OutputParams::LYX) { + if (runparams.flavor == Flavor::LyX) { Paragraph const & par = text().paragraphs()[par_begin]; ostringstream ods; depth_type dt = par.getDepth(); par.write(ods, params(), dt); os << from_utf8(ods.str()); - } else if (runparams.flavor == OutputParams::HTML) { + } else if (runparams.flavor == Flavor::Html) { XMLStream xs(os); setMathFlavor(runparams); xhtmlParagraphs(text(), *this, xs, runparams); - } else if (runparams.flavor == OutputParams::TEXT) { + } else if (runparams.flavor == Flavor::Text) { bool dummy = false; // FIXME Handles only one paragraph, unlike the others. // Probably should have some routine with a signature like them. writePlaintextParagraph(*this, text().paragraphs()[par_begin], os, runparams, dummy); - } else if (runparams.flavor == OutputParams::DOCBOOK5) { + } else if (runparams.flavor == Flavor::DocBook5) { XMLStream xs{os}; docbookParagraphs(text(), *this, xs, runparams); } else { @@ -4127,15 +4128,16 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, d->ignore_parent = false; } } else { - os << "% "; + os << comment_start; if (output == FullSource) os << _("Preview source code"); else if (output == OnlyPreamble) os << _("Preview preamble"); else if (output == OnlyBody) os << _("Preview body"); + os << comment_end; os << "\n\n"; - if (runparams.flavor == OutputParams::LYX) { + if (runparams.flavor == Flavor::LyX) { ostringstream ods; if (output == FullSource) write(ods); @@ -4144,14 +4146,14 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, else if (output == OnlyBody) text().write(ods); os << from_utf8(ods.str()); - } else if (runparams.flavor == OutputParams::HTML) { + } else if (runparams.flavor == Flavor::Html) { writeLyXHTMLSource(os, runparams, output); - } else if (runparams.flavor == OutputParams::TEXT) { - if (output == OnlyPreamble) { + } else if (runparams.flavor == Flavor::Text) { + if (output == OnlyPreamble) os << "% "<< _("Plain text does not have a preamble."); - } else + else writePlaintextFile(*this, os, runparams); - } else if (runparams.flavor == OutputParams::DOCBOOK5) { + } else if (runparams.flavor == Flavor::DocBook5) { writeDocBookSource(os, runparams, output); } else { // latex or literate @@ -4386,7 +4388,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir } MarkAsExporting exporting(this); string backend_format; - runparams.flavor = OutputParams::LATEX; + runparams.flavor = Flavor::LaTeX; runparams.linelen = lyxrc.plaintext_linelen; runparams.includeall = includeall; vector backs = params().backends(); @@ -4430,13 +4432,13 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir LYXERR(Debug::FILES, "backend_format=" << backend_format); // FIXME: Don't hardcode format names here, but use a flag if (backend_format == "pdflatex") - runparams.flavor = OutputParams::PDFLATEX; + runparams.flavor = Flavor::PdfLaTeX; else if (backend_format == "luatex") - runparams.flavor = OutputParams::LUATEX; + runparams.flavor = Flavor::LuaTeX; else if (backend_format == "dviluatex") - runparams.flavor = OutputParams::DVILUATEX; + runparams.flavor = Flavor::DviLuaTeX; else if (backend_format == "xetex") - runparams.flavor = OutputParams::XETEX; + runparams.flavor = Flavor::XeTeX; } string filename = latexName(false); @@ -4447,7 +4449,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir // Plain text backend if (backend_format == "text") { - runparams.flavor = OutputParams::TEXT; + runparams.flavor = Flavor::Text; try { writePlaintextFile(*this, FileName(filename), runparams); } @@ -4455,7 +4457,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir } // HTML backend else if (backend_format == "xhtml") { - runparams.flavor = OutputParams::HTML; + runparams.flavor = Flavor::Html; setMathFlavor(runparams); if (makeLyXHTMLFile(FileName(filename), runparams) == ExportKilled) return ExportKilled; @@ -4463,7 +4465,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir writeFile(FileName(filename)); // DocBook backend else if (backend_format == "docbook5") { - runparams.flavor = OutputParams::DOCBOOK5; + runparams.flavor = Flavor::DocBook5; runparams.nice = false; if (makeDocBookFile(FileName(filename), runparams) == ExportKilled) return ExportKilled; @@ -5162,9 +5164,9 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const } if (needEnumCounterReset(it)) { - // Increase the master counter? - if (layout.stepmastercounter) - counters.stepMaster(enumcounter, utype); + // Increase the parent counter? + if (layout.stepparentcounter) + counters.stepParent(enumcounter, utype); // Maybe we have to reset the enumeration counter. if (!layout.resumecounter) counters.reset(enumcounter);