]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Capitalize labels of floats, etc. Fixes #11993.
[lyx.git] / src / Buffer.cpp
index 1ccb263014651595f417bb099d9ea820c8a4b522..3a6ae648b25885b5009da363c47659c56098b58e 100644 (file)
@@ -14,7 +14,6 @@
 #include "Buffer.h"
 
 #include "Author.h"
-#include "LayoutFile.h"
 #include "BiblioInfo.h"
 #include "BranchList.h"
 #include "buffer_funcs.h"
 #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"
 #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<uint32_t>(e.failed_char) << dec;
                if (getParFromID(e.par_id).paragraph().layout().pass_thru) {
                        docstring msg = bformat(_("Uncodable character '%1$s'"
                                                  " (code point %2$s)"),
@@ -2136,7 +2128,7 @@ Buffer::ExportStatus Buffer::writeDocBookSource(odocstream & os,
        bool const output_preamble =
                output == FullSource || output == OnlyPreamble;
        bool const output_body =
-         output == FullSource || output == OnlyBody;
+         output == FullSource || output == OnlyBody || output == IncludedFile;
 
        if (output_preamble) {
                // XML preamble, no doctype needed.
@@ -2328,7 +2320,7 @@ int Buffer::runChktex()
 
        // Generate the LaTeX file if neccessary
        OutputParams runparams(&params().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<Buffer *>(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<InsetBranch &>(*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<Buffer *>(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<docstring> & 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<InsetBranch &>(*it);
+       for (Inset const & it : inset()) {
+               if (it.lyxCode() == BRANCH_CODE) {
+                       InsetBranch const & br = static_cast<InsetBranch const &>(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<docstring> & 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<InsetInclude const &>(*it);
+                               static_cast<InsetInclude const &>(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;
@@ -4081,23 +4071,23 @@ unique_ptr<TexRow> Buffer::getSourceCode(odocstream & os, string const & format,
                           << "\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 {
@@ -4135,7 +4125,7 @@ unique_ptr<TexRow> Buffer::getSourceCode(odocstream & os, string const & format,
                else if (output == OnlyBody)
                        os << _("Preview body");
                os << "\n\n";
-               if (runparams.flavor == OutputParams::LYX) {
+               if (runparams.flavor == Flavor::LyX) {
                        ostringstream ods;
                        if (output == FullSource)
                                write(ods);
@@ -4144,14 +4134,14 @@ unique_ptr<TexRow> 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) {
+               } else if (runparams.flavor == Flavor::Text) {
                        if (output == OnlyPreamble) {
                                os << "% "<< _("Plain text does not have a preamble.");
                        } 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 +4376,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<string> backs = params().backends();
@@ -4430,13 +4420,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 +4437,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 +4445,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 +4453,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 +5152,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);