[LyX features/cleanup/updateMacros4] Move updateMacros code to Paragraph class.

Richard Kimberly Heck rikiheck at lyx.org
Thu Nov 12 23:48:21 UTC 2020


The branch, cleanup/updateMacros4, has been created.
        at  e63184eb41cee93129502394263661d4b0363ac7 (commit)

- Log -----------------------------------------------------------------

commit e63184eb41cee93129502394263661d4b0363ac7
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 18:51:23 2020 -0500

    Move updateMacros code to Paragraph class.
    
    My dream is that this will eventually allow us to run updateMacros
    only on a Paragraph when, say, a character is entered.

diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index c668f3b..8a97209 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -4553,6 +4553,21 @@ void Paragraph::updateWords()
 }
 
 
+void Paragraph::updateMacros(DocIterator const & us, DocIterator const & scope)
+{
+	// We expect a DocIterator pointing at the beginning of this paragraph.
+	LBUFERR(&us.paragraph() == this && us.pos() == 0);
+	DocIterator it = us;
+	// iterate over the insets of the current paragraph
+	for (auto const & insit : insetList()) {
+		it.pos() = insit.pos;
+		it.push_back(CursorSlice(*insit.inset));
+		insit.inset->updateMacros(it, scope);
+		it.pop_back();
+	}
+}
+
+
 void Paragraph::Private::appendSkipPosition(SkipPositions & skips, pos_type const pos) const
 {
 	SkipPositionsIterator begin = skips.begin();
diff --git a/src/Paragraph.h b/src/Paragraph.h
index 4812684..24bdbc8 100644
--- a/src/Paragraph.h
+++ b/src/Paragraph.h
@@ -476,6 +476,8 @@ public:
 		word_location const loc, bool const ignore_deleted = false) const;
 	///
 	void updateWords();
+	///
+	void updateMacros(DocIterator const & us, DocIterator const & scope);
 
 	/// Spellcheck word at position \p from and fill in found misspelled word
 	/// and \p suggestions if \p do_suggestion is true.
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index 7af5cf2..942ef77 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -922,23 +922,10 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 	}
 
 	DocIterator it = us;
-	DocIterator ourscope = scope;
-	if (!producesOutput()) {
-		ourscope = us;
-		++ourscope.pos();
-	}
-
 	pit_type const lastpit = it.lastpit();
 	// look for macros in each paragraph
 	while (it.pit() <= lastpit) {
-		Paragraph & par = it.paragraph();
-		// iterate over the insets of the current paragraph
-		for (auto const & insit : par.insetList()) {
-			it.pos() = insit.pos;
-			it.push_back(CursorSlice(*insit.inset));
-			insit.inset->updateMacros(it, ourscope);
-			it.pop_back();
-		}
+		it.paragraph().updateMacros(it, ourscope);
 		// next paragraph
 		it.pit()++;
 		it.pos() = 0;

commit f85a259237630c219d056bad202dd5106766764d
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 18:07:35 2020 -0500

    Now just use the Inset::updateMacros method.
    
    I did all this step by step so (a) I could check the output along
    the way and (b) so it will be easier to isolate problems if those
    should arise.

diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index 34397ad..7af5cf2 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -932,48 +932,13 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 	// look for macros in each paragraph
 	while (it.pit() <= lastpit) {
 		Paragraph & par = it.paragraph();
-
 		// iterate over the insets of the current paragraph
 		for (auto const & insit : par.insetList()) {
 			it.pos() = insit.pos;
-
-			if (InsetText * itext = insit.inset->asInsetText()) {
-				// collect macros in inset
-				it.push_back(CursorSlice(*insit.inset));
-				itext->updateMacros(it, ourscope);
-				it.pop_back();
-				continue;
-			}
-
-			if (InsetTabular * itext = insit.inset->asInsetTabular()) {
-				it.push_back(CursorSlice(*insit.inset));
-				itext->updateMacros(it, ourscope);
-				it.pop_back();
-				continue;
-			}
-
-			// is it an external file?
-			if (insit.inset->lyxCode() == INCLUDE_CODE) {
-				insit.inset->updateMacros(it, ourscope);
-				continue;
-			}
-
-			InsetMath * im = insit.inset->asInsetMath();
-			if (im)  {
-				InsetMathHull * hull = im->asHullInset();
-				if (hull)
-					hull->updateMacros(it, ourscope);
-			}
-
-			if (insit.inset->lyxCode() != MATHMACRO_CODE)
-				continue;
-
-			// get macro data
-			InsetMathMacroTemplate * macroTemplate =
-				insit.inset->asInsetMath()->asMacroTemplate();
-			macroTemplate->updateMacros(it, ourscope);
+			it.push_back(CursorSlice(*insit.inset));
+			insit.inset->updateMacros(it, ourscope);
+			it.pop_back();
 		}
-
 		// next paragraph
 		it.pit()++;
 		it.pos() = 0;

commit 0cbdec8e97afb4af4798b773254321b8ecd0695d
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 18:03:36 2020 -0500

    Move updateMacros code into InsetMathMacroTemplate.

diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index d25f967..34397ad 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -969,21 +969,9 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 				continue;
 
 			// get macro data
-			InsetMathMacroTemplate & macroTemplate =
-				*insit.inset->asInsetMath()->asMacroTemplate();
-			MacroContext mc(&buffer(), it);
-			macroTemplate.updateToContext(mc);
-
-			// valid?
-			bool valid = macroTemplate.validMacro();
-			// FIXME: Should be fixNameAndCheckIfValid() in fact,
-			// then the BufferView's cursor will be invalid in
-			// some cases which leads to crashes.
-			if (!valid)
-				continue;
-
-			// register macro
-			buffer().registerMacro(macroTemplate.name(), it, ourscope);
+			InsetMathMacroTemplate * macroTemplate =
+				insit.inset->asInsetMath()->asMacroTemplate();
+			macroTemplate->updateMacros(it, ourscope);
 		}
 
 		// next paragraph
diff --git a/src/mathed/InsetMathMacroTemplate.cpp b/src/mathed/InsetMathMacroTemplate.cpp
index e5c7db9..a445b4d 100644
--- a/src/mathed/InsetMathMacroTemplate.cpp
+++ b/src/mathed/InsetMathMacroTemplate.cpp
@@ -460,6 +460,25 @@ docstring InsetMathMacroTemplate::name() const
 }
 
 
+void InsetMathMacroTemplate::updateMacros(DocIterator const & us, DocIterator const & scope)
+{
+	// This matches the previous code, but I am not sure why it
+	// should be like this.
+	DocIterator it = us;
+	it.pop_back();
+	MacroContext mc(&buffer(), it);
+	updateToContext(mc);
+	// FIXME: Should be fixNameAndCheckIfValid() in fact,
+	// then the BufferView's cursor will be invalid in
+	// some cases which leads to crashes.
+	if (!validMacro())
+		return;
+
+	// register macro
+	buffer().registerMacro(name(), it, scope);
+}
+
+
 void InsetMathMacroTemplate::updateToContext(MacroContext const & mc)
 {
 	redefinition_ = mc.get(name()) != nullptr;
diff --git a/src/mathed/InsetMathMacroTemplate.h b/src/mathed/InsetMathMacroTemplate.h
index 0940896..7c9a680 100644
--- a/src/mathed/InsetMathMacroTemplate.h
+++ b/src/mathed/InsetMathMacroTemplate.h
@@ -87,6 +87,8 @@ public:
 
 	/// decide whether its a redefinition
 	void updateToContext(MacroContext const & mc);
+	///
+	void updateMacros(DocIterator const & us, DocIterator const & scope) override;
 
 	///
 	void draw(PainterInfo & pi, int x, int y) const override;

commit f9f22e7be975850628efb8730b46c27412d43063
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 17:48:51 2020 -0500

    Move updateMacros code to InsetInclude.

diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index 75a41c3..eff6fb5 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -1400,6 +1400,16 @@ void InsetInclude::updateCommand()
 }
 
 
+void InsetInclude::updateMacros(DocIterator const & us, DocIterator const & scope)
+{
+	buffer().setMacroLock();
+	loadIfNeeded();
+	buffer().clearMacroLock();
+	if (child_buffer_)
+		buffer().registerChild(child_buffer_, us, scope);
+}
+
+
 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
 {
 	file_exist_ = includedFileExist();
diff --git a/src/insets/InsetInclude.h b/src/insets/InsetInclude.h
index a254254..046f481 100644
--- a/src/insets/InsetInclude.h
+++ b/src/insets/InsetInclude.h
@@ -106,6 +106,8 @@ public:
 	///
 	void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false) override;
 	///
+	void updateMacros(DocIterator const & us, DocIterator const & scope) override;
+	///
 	std::string contextMenuName() const override;
 	//@}
 
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index 1964785..d25f967 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -954,15 +954,7 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 
 			// is it an external file?
 			if (insit.inset->lyxCode() == INCLUDE_CODE) {
-				// get buffer of external file
-				InsetInclude const & incinset =
-					static_cast<InsetInclude const &>(*insit.inset);
-				buffer().setMacroLock();
-				Buffer * child = incinset.loadIfNeeded();
-				buffer().clearMacroLock();
-				if (!child)
-					continue;
-				buffer().registerChild(child, it, ourscope);
+				insit.inset->updateMacros(it, ourscope);
 				continue;
 			}
 

commit 5ea5c5dc09ccc7e9e2dc60741dea444cefd7272d
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 17:43:57 2020 -0500

    Move code to InsetMathHull::updateMacros.

diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index bad23a4..1964785 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -970,7 +970,7 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 			if (im)  {
 				InsetMathHull * hull = im->asHullInset();
 				if (hull)
-					hull->recordLocation(it);
+					hull->updateMacros(it, ourscope);
 			}
 
 			if (insit.inset->lyxCode() != MATHMACRO_CODE)
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 43f4958..5464115 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -238,6 +238,13 @@ namespace {
 } // namespace
 
 
+// FIXME This may or may not be the right place to do this.
+void InsetMathHull::updateMacros(DocIterator const & us, DocIterator const &)
+{
+	recordLocation(us);
+}
+
+
 void InsetMathHull::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
 {
 	if (!buffer_) {
diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h
index 0b085a1..ef77a75 100644
--- a/src/mathed/InsetMathHull.h
+++ b/src/mathed/InsetMathHull.h
@@ -50,6 +50,8 @@ public:
 	void setBuffer(Buffer &) override;
 	///
 	void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false) override;
+	/// This must be called by any subclass that overrides it!
+	void updateMacros(DocIterator const & us, DocIterator const & scope) override;
 	///
 	void addToToc(DocIterator const & di, bool output_active,
 				  UpdateType utype, TocBackend & backend) const override;

commit 668ad347a0b199792c8c35e2c0cc421d1634db1e
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 17:20:48 2020 -0500

    Move updateMacros code into InsetTabular

diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp
index e6c9892..75886f5 100644
--- a/src/insets/InsetTabular.cpp
+++ b/src/insets/InsetTabular.cpp
@@ -4813,6 +4813,19 @@ void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
 }
 
 
+void InsetTabular::updateMacros(DocIterator const & us, DocIterator const & scope)
+{
+	DocIterator pos = us;
+	size_t const numcells = pos.nargs();
+	for (size_t c = 0; c < numcells; ++c, pos.top().forwardIdx()) {
+		shared_ptr<InsetTableCell> cp = cell(c);
+		// this test should be unnecessary, right?
+		if (cp)
+			cp->updateMacros(pos, scope);
+	}
+}
+
+
 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
 {
 	// In a longtable, tell captions what the current float is
diff --git a/src/insets/InsetTabular.h b/src/insets/InsetTabular.h
index 2ee107a..c27fec0 100644
--- a/src/insets/InsetTabular.h
+++ b/src/insets/InsetTabular.h
@@ -1054,6 +1054,8 @@ public:
 	/// Update the counters of this inset and of its contents
 	void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false) override;
 	///
+	void updateMacros(DocIterator const & us, DocIterator const & scope) override;
+	///
 	void addToToc(DocIterator const & di, bool output_active,
 				  UpdateType utype, TocBackend & backend) const override;
 
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index eda1303..bad23a4 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -62,6 +62,7 @@
 #include "mathed/InsetMathHull.h"
 #include "mathed/MacroTable.h"
 #include "mathed/InsetMathMacroTemplate.h"
+#include "insets/InsetTabular.h"
 
 #include "support/convert.h"
 #include "support/debug.h"
@@ -944,14 +945,10 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 				continue;
 			}
 
-			if (insit.inset->asInsetTabular()) {
-				CursorSlice slice(*insit.inset);
-				size_t const numcells = slice.nargs();
-				for (; slice.idx() < numcells; slice.forwardIdx()) {
-					it.push_back(slice);
-					updateMacros(it, ourscope);
-					it.pop_back();
-				}
+			if (InsetTabular * itext = insit.inset->asInsetTabular()) {
+				it.push_back(CursorSlice(*insit.inset));
+				itext->updateMacros(it, ourscope);
+				it.pop_back();
 				continue;
 			}
 

commit 5fbe354bfa7f6d91ce3364592b0d95e1b46fd2fe
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 16:33:28 2020 -0500

    Move some processing from the loop into the main InsetText routine.

diff --git a/src/insets/Inset.h b/src/insets/Inset.h
index c6fbfdb..7885046 100644
--- a/src/insets/Inset.h
+++ b/src/insets/Inset.h
@@ -571,6 +571,9 @@ public:
 	/// The boolean indicates whether we are preparing for output, e.g.,
 	/// of XHTML.
 	virtual void updateBuffer(ParIterator const &, UpdateType, bool const) {}
+	/// Note that \param us is expected to be a DocIterator pointing at this inset.
+	/// We will assert otherwise.
+	virtual void updateMacros(DocIterator const & /* us */, DocIterator const & /* scope */) {}
 
 	/// Updates the inset's dialog
 	virtual Buffer const * updateFrontend() const;
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index 9c32d49..eda1303 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -921,8 +921,13 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 	}
 
 	DocIterator it = us;
-	pit_type const lastpit = it.lastpit();
+	DocIterator ourscope = scope;
+	if (!producesOutput()) {
+		ourscope = us;
+		++ourscope.pos();
+	}
 
+	pit_type const lastpit = it.lastpit();
 	// look for macros in each paragraph
 	while (it.pit() <= lastpit) {
 		Paragraph & par = it.paragraph();
@@ -931,20 +936,10 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 		for (auto const & insit : par.insetList()) {
 			it.pos() = insit.pos;
 
-			if (InsetText const * itext = insit.inset->asInsetText()) {
+			if (InsetText * itext = insit.inset->asInsetText()) {
 				// collect macros in inset
 				it.push_back(CursorSlice(*insit.inset));
-				if (itext->producesOutput()) {
-					// the simple case
-					updateMacros(it, scope);
-				} else {
-					// We don't want macros declared in notes, comments, etc,
-					// to affect anything outside them.
-					// New scope which ends just behind the inset
-					DocIterator new_scope = it;
-					++new_scope.pos();
-					updateMacros(it, new_scope);
-				}
+				itext->updateMacros(it, ourscope);
 				it.pop_back();
 				continue;
 			}
@@ -954,7 +949,7 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 				size_t const numcells = slice.nargs();
 				for (; slice.idx() < numcells; slice.forwardIdx()) {
 					it.push_back(slice);
-					updateMacros(it, scope);
+					updateMacros(it, ourscope);
 					it.pop_back();
 				}
 				continue;
@@ -970,7 +965,7 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 				buffer().clearMacroLock();
 				if (!child)
 					continue;
-				buffer().registerChild(child, it, scope);
+				buffer().registerChild(child, it, ourscope);
 				continue;
 			}
 
@@ -999,7 +994,7 @@ void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
 				continue;
 
 			// register macro
-			buffer().registerMacro(macroTemplate.name(), it, scope);
+			buffer().registerMacro(macroTemplate.name(), it, ourscope);
 		}
 
 		// next paragraph
diff --git a/src/insets/InsetText.h b/src/insets/InsetText.h
index 9fdfb6f..079d0f4 100644
--- a/src/insets/InsetText.h
+++ b/src/insets/InsetText.h
@@ -173,7 +173,7 @@ public:
 	/// Update the counters of this inset and of its contents
 	void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false) override;
 	///
-	void updateMacros(DocIterator const & us, DocIterator const & scope);
+	void updateMacros(DocIterator const & us, DocIterator const & scope) override;
 	///
 	void setMacrocontextPositionRecursive(DocIterator const & pos);
 	///

commit 57a225a6cb682b90fdcaa89d3eb4682ada246c31
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 19:10:24 2020 -0500

    Send all updateMacro stuff through the insets.

diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index c0a133f..f4b513c 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -3760,98 +3760,7 @@ MacroData const * Buffer::getMacro(docstring const & name,
 
 void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
 {
-	pit_type const lastpit = it.lastpit();
-
-	// look for macros in each paragraph
-	while (it.pit() <= lastpit) {
-		Paragraph & par = it.paragraph();
-
-		// iterate over the insets of the current paragraph
-		for (auto const & insit : par.insetList()) {
-			it.pos() = insit.pos;
-
-			if (InsetText const * itext = insit.inset->asInsetText()) {
-				// collect macros in inset
-				it.push_back(CursorSlice(*insit.inset));
-				if (itext->producesOutput()) {
-					// the simple case
-					updateMacros(it, scope);
-				} else {
-					// We don't want macros declared in notes, comments, etc, 
-					// to affect anything outside them.
-					// New scope which ends just behind the inset
-					DocIterator new_scope = it;
-					++new_scope.pos();
-					updateMacros(it, new_scope);
-				}
-				it.pop_back();
-				continue;
-			}
-
-			if (insit.inset->asInsetTabular()) {
-				CursorSlice slice(*insit.inset);
-				size_t const numcells = slice.nargs();
-				for (; slice.idx() < numcells; slice.forwardIdx()) {
-					it.push_back(slice);
-					updateMacros(it, scope);
-					it.pop_back();
-				}
-				continue;
-			}
-
-			// is it an external file?
-			if (insit.inset->lyxCode() == INCLUDE_CODE) {
-				// get buffer of external file
-				InsetInclude const & incinset =
-					static_cast<InsetInclude const &>(*insit.inset);
-				macro_lock = true;
-				Buffer * child = incinset.loadIfNeeded();
-				macro_lock = false;
-				if (!child)
-					continue;
-
-				// register its position, but only when it is
-				// included first in the buffer
-				children_positions.insert({child, it});
-
-				// register child with its scope
-				position_to_children[it] = Impl::ScopeBuffer(scope, child);
-				continue;
-			}
-
-			InsetMath * im = insit.inset->asInsetMath();
-			if (doing_export && im)  {
-				InsetMathHull * hull = im->asHullInset();
-				if (hull)
-					hull->recordLocation(it);
-			}
-
-			if (insit.inset->lyxCode() != MATHMACRO_CODE)
-				continue;
-
-			// get macro data
-			InsetMathMacroTemplate & macroTemplate =
-				*insit.inset->asInsetMath()->asMacroTemplate();
-			MacroContext mc(owner_, it);
-			macroTemplate.updateToContext(mc);
-
-			// valid?
-			bool valid = macroTemplate.validMacro();
-			// FIXME: Should be fixNameAndCheckIfValid() in fact,
-			// then the BufferView's cursor will be invalid in
-			// some cases which leads to crashes.
-			if (!valid)
-				continue;
-
-			// register macro
-			macro_table.addMacroDefinition(macroTemplate.name(), it, scope, 
-				MacroData(const_cast<Buffer *>(owner_), it));
-		}
-
-		// next paragraph
-		it.pit()++;
-		it.pos() = 0;
-	}
+	owner_->inset().updateMacros(it, scope);
 }
 
 

commit c30179a44992b7b65ad9af41771fefa9641955e7
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 16:16:15 2020 -0500

    Start moving updateMacros code into the insets.

diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 859f9ce..c0a133f 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -3855,6 +3855,37 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
 }
 
 
+void Buffer::setMacroLock() const
+{
+	d->macro_lock = true;
+}
+
+
+void Buffer::clearMacroLock() const
+{
+	d->macro_lock = false;
+}
+
+
+void Buffer::registerChild(Buffer * child,
+		DocIterator const & pos, DocIterator const & scope) const
+{
+	// register its position, but only when it is
+	// included first in the buffer
+	d->children_positions.insert({child, pos});
+	// register child with its scope
+	d->position_to_children[pos] = Impl::ScopeBuffer(scope, child);
+}
+
+
+void Buffer::registerMacro(docstring const & name, DocIterator const & pos,
+		DocIterator const & scope)
+{
+	d->macro_table.addMacroDefinition(name, pos, scope,
+		MacroData(const_cast<Buffer *>(this), pos));
+}
+
+
 void Buffer::updateMacros() const
 {
 	if (d->macro_lock)
diff --git a/src/Buffer.h b/src/Buffer.h
index c8946d6..267bf94 100644
--- a/src/Buffer.h
+++ b/src/Buffer.h
@@ -597,6 +597,10 @@ public:
 	//
 	// Macro handling
 	//
+	///
+	void setMacroLock() const;
+	///
+	void clearMacroLock() const;
 	/// Collect macro definitions in paragraphs
 	void updateMacros() const;
 	/// Iterate through the whole buffer and try to resolve macros
@@ -614,10 +618,18 @@ public:
 	/// Return macro defined before the inclusion of the child
 	MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
 
+	///
+	void registerMacro(docstring const & name, DocIterator const & pos,
+			DocIterator const & scope);
+
 	/// Collect user macro names at loading time
 	typedef std::set<docstring> UserMacroSet;
 	mutable UserMacroSet usermacros;
 
+	///
+	void registerChild(Buffer * child,
+			DocIterator const & pos, DocIterator const & scope) const;
+
 	/// Replace the inset contents for insets which InsetCode is equal
 	/// to the passed \p inset_code. Handles undo.
 	void changeRefsIfUnique(docstring const & from, docstring const & to);
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index 35c1d3b..9c32d49 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -56,6 +56,13 @@
 #include "frontends/alert.h"
 #include "frontends/Painter.h"
 
+// These are only temporarily here, until the relevant code
+// is moved into these insets
+#include "insets/InsetInclude.h"
+#include "mathed/InsetMathHull.h"
+#include "mathed/MacroTable.h"
+#include "mathed/InsetMathMacroTemplate.h"
+
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/gettext.h"
@@ -902,6 +909,106 @@ void InsetText::updateBuffer(ParIterator const & it, UpdateType utype, bool cons
 }
 
 
+void InsetText::updateMacros(DocIterator const & us, DocIterator const & scope)
+{
+	DocIterator ourscope = scope;
+	// If we do not produce output, then we do not want our macros to
+	// affect anything outside this inset. So we set the scope to the
+	// position right after the inset.
+	if (!producesOutput()) {
+		ourscope = us;
+		++ourscope.pos();
+	}
+
+	DocIterator it = us;
+	pit_type const lastpit = it.lastpit();
+
+	// look for macros in each paragraph
+	while (it.pit() <= lastpit) {
+		Paragraph & par = it.paragraph();
+
+		// iterate over the insets of the current paragraph
+		for (auto const & insit : par.insetList()) {
+			it.pos() = insit.pos;
+
+			if (InsetText const * itext = insit.inset->asInsetText()) {
+				// collect macros in inset
+				it.push_back(CursorSlice(*insit.inset));
+				if (itext->producesOutput()) {
+					// the simple case
+					updateMacros(it, scope);
+				} else {
+					// We don't want macros declared in notes, comments, etc,
+					// to affect anything outside them.
+					// New scope which ends just behind the inset
+					DocIterator new_scope = it;
+					++new_scope.pos();
+					updateMacros(it, new_scope);
+				}
+				it.pop_back();
+				continue;
+			}
+
+			if (insit.inset->asInsetTabular()) {
+				CursorSlice slice(*insit.inset);
+				size_t const numcells = slice.nargs();
+				for (; slice.idx() < numcells; slice.forwardIdx()) {
+					it.push_back(slice);
+					updateMacros(it, scope);
+					it.pop_back();
+				}
+				continue;
+			}
+
+			// is it an external file?
+			if (insit.inset->lyxCode() == INCLUDE_CODE) {
+				// get buffer of external file
+				InsetInclude const & incinset =
+					static_cast<InsetInclude const &>(*insit.inset);
+				buffer().setMacroLock();
+				Buffer * child = incinset.loadIfNeeded();
+				buffer().clearMacroLock();
+				if (!child)
+					continue;
+				buffer().registerChild(child, it, scope);
+				continue;
+			}
+
+			InsetMath * im = insit.inset->asInsetMath();
+			if (im)  {
+				InsetMathHull * hull = im->asHullInset();
+				if (hull)
+					hull->recordLocation(it);
+			}
+
+			if (insit.inset->lyxCode() != MATHMACRO_CODE)
+				continue;
+
+			// get macro data
+			InsetMathMacroTemplate & macroTemplate =
+				*insit.inset->asInsetMath()->asMacroTemplate();
+			MacroContext mc(&buffer(), it);
+			macroTemplate.updateToContext(mc);
+
+			// valid?
+			bool valid = macroTemplate.validMacro();
+			// FIXME: Should be fixNameAndCheckIfValid() in fact,
+			// then the BufferView's cursor will be invalid in
+			// some cases which leads to crashes.
+			if (!valid)
+				continue;
+
+			// register macro
+			buffer().registerMacro(macroTemplate.name(), it, scope);
+		}
+
+		// next paragraph
+		it.pit()++;
+		it.pos() = 0;
+	}
+}
+
+
 void InsetText::toString(odocstream & os) const
 {
 	os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
diff --git a/src/insets/InsetText.h b/src/insets/InsetText.h
index cefd44c..9fdfb6f 100644
--- a/src/insets/InsetText.h
+++ b/src/insets/InsetText.h
@@ -173,6 +173,8 @@ public:
 	/// Update the counters of this inset and of its contents
 	void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false) override;
 	///
+	void updateMacros(DocIterator const & us, DocIterator const & scope);
+	///
 	void setMacrocontextPositionRecursive(DocIterator const & pos);
 	///
 	void toString(odocstream &) const override;

commit bc816df45a2367c632e8646ba6e37f06bf0ba0d3
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 18:05:40 2020 -0500

    nullptr

diff --git a/src/mathed/InsetMathMacroTemplate.cpp b/src/mathed/InsetMathMacroTemplate.cpp
index 1152936..e5c7db9 100644
--- a/src/mathed/InsetMathMacroTemplate.cpp
+++ b/src/mathed/InsetMathMacroTemplate.cpp
@@ -462,7 +462,7 @@ docstring InsetMathMacroTemplate::name() const
 
 void InsetMathMacroTemplate::updateToContext(MacroContext const & mc)
 {
-	redefinition_ = mc.get(name()) != 0;
+	redefinition_ = mc.get(name()) != nullptr;
 }
 
 
@@ -545,7 +545,7 @@ void InsetMathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
 	Changer dummy2 = mi.base.font.changeStyle(TEXT_STYLE);
 
 	// valid macro?
-	MacroData const * macro = 0;
+	MacroData const * macro = nullptr;
 	if (validName())
 		macro = mi.macrocontext.get(name());
 
@@ -668,7 +668,7 @@ void InsetMathMacroTemplate::shiftArguments(size_t from, int by)
 int InsetMathMacroTemplate::maxArgumentInDefinition() const
 {
 	// We don't have a buffer when pasting from the clipboard (bug 6014).
-	Buffer const * macro_buffer = isBufferLoaded() ? &buffer() : 0;
+	Buffer const * macro_buffer = isBufferLoaded() ? &buffer() : nullptr;
 	int maxArg = 0;
 	DocIterator it = doc_iterator_begin(macro_buffer, this);
 	it.idx() = defIdx();

commit 1ef9f19cd1355d09347b20384c2b4f330b8a5192
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 16:30:03 2020 -0500

    nullptr

diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index ec223fa..35c1d3b 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -1088,7 +1088,7 @@ bool InsetText::showCompletionCursor() const
 
 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
 {
-	return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
+	return completionSupported(cur) ? text_.createCompletionList(cur) : nullptr;
 }
 
 

commit b8f86edcd05fce9f12f8b9aafb9985f1fdfd800a
Author: Richard Kimberly Heck <rikiheck at lyx.org>
Date:   Thu Nov 12 16:19:46 2020 -0500

    Temporary script for testing math export.

diff --git a/runtest.sh b/runtest.sh
new file mode 100644
index 0000000..45ac23e
--- /dev/null
+++ b/runtest.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+/cvs/lyx/lyx-devel/build/bin/lyx2.4 -E pdflatex /cvs/lyx/lyx-devel/Math-Cleanup.tex lib/doc/Math.lyx
+if diff -q Math-Master.tex Math-Cleanup.tex; then
+    echo "No differences in LaTeX output!";
+else
+    echo "Differences in LaTeX output! Enter to see them....";
+    pause
+    diff -u Math-Master.tex Math-Cleanup.tex
+fi

-----------------------------------------------------------------------


hooks/post-receive
-- 
Repository for new features


More information about the lyx-cvs mailing list