[LyX/master] Factor out the list of macro definitions for InsetPreview.

Thibaut Cuvelier tcuvelier at lyx.org
Wed Oct 13 17:04:19 UTC 2021


commit 46b81018007c23a2959c16360cb0ab257cf21be8
Author: Thibaut Cuvelier <tcuvelier at lyx.org>
Date:   Fri Oct 8 03:14:18 2021 +0200

    Factor out the list of macro definitions for InsetPreview.
    
    This will be soon reused in InsetText to generate images for DocBook.
---
 src/DocIterator.h           |    4 ++--
 src/insets/InsetPreview.cpp |   41 +++++++++++++++++++++++++----------------
 src/insets/InsetPreview.h   |    5 +++++
 3 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/src/DocIterator.h b/src/DocIterator.h
index 5e6ae6c..cfb31f4 100644
--- a/src/DocIterator.h
+++ b/src/DocIterator.h
@@ -28,8 +28,8 @@ class MathAtom;
 class Paragraph;
 class Text;
 
-DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
-DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
+DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = nullptr);
+DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = nullptr);
 
 
 class DocIterator
diff --git a/src/insets/InsetPreview.cpp b/src/insets/InsetPreview.cpp
index d1a0343..e08fdcd 100644
--- a/src/insets/InsetPreview.cpp
+++ b/src/insets/InsetPreview.cpp
@@ -79,36 +79,45 @@ void InsetPreview::addPreview(DocIterator const & inset_pos,
 }
 
 
-void InsetPreview::preparePreview(DocIterator const & pos) const
+MacroNameSet gatherMacroDefinitions(const Buffer* buffer, const Inset * inset)
 {
-	odocstringstream str;
-	otexstream os(str);
-	OutputParams runparams(&pos.buffer()->params().encoding());
-	latex(os, runparams);
-
-	// collect macros at this position
+	// Collect macros for this inset.
+	// Not done yet: this function returns a list of macro *definitions*.
 	MacroNameSet macros;
-	pos.buffer()->listMacroNames(macros);
+	buffer->listMacroNames(macros);
 
-	// look for math insets and collect definitions for the used macros
+	// Look for math insets and collect definitions for the used macros.
 	MacroNameSet defs;
-	DocIterator dit = doc_iterator_begin(pos.buffer(), this);
-	DocIterator const dend = doc_iterator_end(pos.buffer(), this);
+	DocIterator const dbeg = doc_iterator_begin(buffer, inset);
+	DocIterator dit = dbeg;
+	DocIterator const dend = doc_iterator_end(buffer, inset);
 	if (!dit.nextInset())
 		dit.forwardInset();
+
 	for (; dit != dend; dit.forwardInset()) {
 		InsetMath * im = dit.nextInset()->asInsetMath();
 		InsetMathHull * hull = im ? im->asHullInset() : nullptr;
 		if (!hull)
 			continue;
 		for (idx_type idx = 0; idx < hull->nargs(); ++idx)
-			hull->usedMacros(hull->cell(idx), pos, macros, defs);
+			hull->usedMacros(hull->cell(idx), dbeg, macros, defs);
 	}
-	MacroNameSet::iterator it = defs.begin();
-	MacroNameSet::iterator end = defs.end();
+
+	return defs;
+}
+
+
+void InsetPreview::preparePreview(DocIterator const & pos) const
+{
+	odocstringstream str;
+	otexstream os(str);
+	OutputParams runparams(&pos.buffer()->params().encoding());
+	latex(os, runparams);
+
+	MacroNameSet defs = gatherMacroDefinitions(pos.buffer(), this);
 	docstring macro_preamble;
-	for (; it != end; ++it)
-		macro_preamble.append(*it);
+	for (const auto& def : defs)
+		macro_preamble.append(def);
 
 	docstring const snippet = macro_preamble + str.str();
 	preview_->addPreview(snippet, *pos.buffer());
diff --git a/src/insets/InsetPreview.h b/src/insets/InsetPreview.h
index f36ea56..55353e7 100644
--- a/src/insets/InsetPreview.h
+++ b/src/insets/InsetPreview.h
@@ -20,6 +20,7 @@
 namespace lyx {
 
 class Dimension;
+class MacroNameSet;
 class RenderPreview;
 
 namespace graphics {
@@ -87,6 +88,10 @@ protected:
 };
 
 
+/// gathers the list of macro definitions used in the given inset
+MacroNameSet gatherMacroDefinitions(const Buffer* buffer, const Inset * inset);
+
+
 } // namespace lyx
 
 


More information about the lyx-cvs mailing list