[LyX/master] Improve AllowedOccurrences

Juergen Spitzmueller spitz at lyx.org
Wed Jul 26 06:44:12 UTC 2023


commit 7f4c4cd5487735e6fa95331320eadd2b46d5e279
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Wed Jul 26 09:53:25 2023 +0200

    Improve AllowedOccurrences
    
    This does now apply to the whole inset or paragraph group (not to the
    current paragraph only) unless the new boolean AllowedOccurrencesPerItem
    is set and we are in a list-type environment.
    
    Amends 729ab602ca70
---
 lib/doc/Customization.lyx      |   88 +++++++++++++++++++++++++++++++++++++++-
 lib/doc/de/Customization.lyx   |   73 +++++++++++++++++++++++++++++++++
 lib/layouts/linguistics.module |    1 +
 lib/scripts/layout2layout.py   |    3 +-
 src/Paragraph.cpp              |   66 ++++++++++++++++++++++++------
 src/insets/InsetLayout.cpp     |    5 ++
 src/insets/InsetLayout.h       |    4 ++
 7 files changed, 224 insertions(+), 16 deletions(-)

diff --git a/lib/doc/Customization.lyx b/lib/doc/Customization.lyx
index 04d9033..060a197 100644
--- a/lib/doc/Customization.lyx
+++ b/lib/doc/Customization.lyx
@@ -21086,7 +21086,7 @@ AllowedInInsets
 
 \begin_layout Description
 
-\change_inserted -712698321 1690292676
+\change_inserted -712698321 1690357747
 \begin_inset Flex Code
 status collapsed
 
@@ -21094,6 +21094,8 @@ status collapsed
 
 \change_inserted -712698321 1690292501
 AllowedOccurrences
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -21106,6 +21108,8 @@ status collapsed
 
 \change_inserted -712698321 1690292507
 int
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -21118,6 +21122,8 @@ status collapsed
 
 \change_inserted -712698321 1690292530
 AllowedInInsets
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -21130,12 +21136,90 @@ status collapsed
 
 \change_inserted -712698321 1690292536
 AllowedInLayouts
+\change_unchanged
+
 \end_layout
 
 \end_inset
 
  has been defined,
- this can be used to determine how many times the inset can be inserted to a given paragraph.
+ this can be used to determine how many times the inset can be inserted to a given inset or the paragraph (group).
+\end_layout
+
+\begin_layout Description
+
+\change_inserted -712698321 1690357828
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690357753
+AllowedOccurrencesPerItem
+\end_layout
+
+\end_inset
+
+ [
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690357747
+
+\emph on
+0
+\end_layout
+
+\end_inset
+
+,
+\begin_inset space \thinspace{}
+\end_inset
+
+
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690357747
+1
+\end_layout
+
+\end_inset
+
+] If this is set to true,
+ 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690357774
+AllowedOccurrences
+\end_layout
+
+\end_inset
+
+ applies to single paragraphs if we are in a list-type environment (with 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690357825
+
+\backslash
+item
+\change_unchanged
+
+\end_layout
+
+\end_inset
+
+s).
 \change_unchanged
 
 \end_layout
diff --git a/lib/doc/de/Customization.lyx b/lib/doc/de/Customization.lyx
index aa5aad8..9ae0e74 100644
--- a/lib/doc/de/Customization.lyx
+++ b/lib/doc/de/Customization.lyx
@@ -17440,6 +17440,79 @@ AllowedInLayouts
 status collapsed
 
 \begin_layout Plain Layout
+AllowedOccurrencesPerItem
+\end_layout
+
+\end_inset
+
+ [
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\emph on
+0
+\end_layout
+
+\end_inset
+
+,
+\begin_inset space \thinspace{}
+\end_inset
+
+
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+1
+\end_layout
+
+\end_inset
+
+] Wenn dies auf 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+true
+\end_layout
+
+\end_inset
+
+ gesetzt wird,
+ zählen die 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+AllowedOccurrences
+\end_layout
+
+\end_inset
+
+ je Absatz,
+ sofern wir uns in einer listenartigen Umgebung (mit 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\backslash
+item
+\end_layout
+
+\end_inset
+
+s) befinden.
+\end_layout
+
+\begin_layout Description
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
 Argument
 \end_layout
 
diff --git a/lib/layouts/linguistics.module b/lib/layouts/linguistics.module
index 43ec54f..bd2528c 100644
--- a/lib/layouts/linguistics.module
+++ b/lib/layouts/linguistics.module
@@ -158,6 +158,7 @@ InsetLayout Flex:Example_Preamble
 	    Numbered_Example_(multiline), Numbered_Examples_(consecutive)
 	EndAllowedInLayouts
 	AllowedOccurrences   1
+	AllowedOccurrencesPerItem  true
 End
 
 InsetLayout Flex:Subexample_Preamble
diff --git a/lib/scripts/layout2layout.py b/lib/scripts/layout2layout.py
index cb6023c..c77b05e 100644
--- a/lib/scripts/layout2layout.py
+++ b/lib/scripts/layout2layout.py
@@ -342,7 +342,8 @@ currentFormat = 102
 
 # Incremented to format 102, 25 July 2023 by spitz
 # add InsetLayout tags AllowedInInsets, EndAllowedInInsets,
-# AllowedInLayouts, EndAllowedInLayouts, AllowedOccurrences
+# AllowedInLayouts, EndAllowedInLayouts, AllowedOccurrences,
+# AllowedOccurrencesPerItem
 
 # Do not forget to document format change in Customization
 # Manual (section "Declaring a new text class").
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index 22e1843..d4658fa 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -2344,33 +2344,73 @@ bool Paragraph::allowedInContext(Cursor const & cur, InsetLayout const & il) con
 	set<docstring> const & allowed_insets = il.allowedInInsets();
 	set<docstring> const & allowed_layouts = il.allowedInLayouts();
 
-	bool result = false;
-	if (allowed_insets.find(inInset().getLayout().name()) != allowed_insets.end())
-		result = true;
+	bool in_allowed_inset =
+		allowed_insets.find(inInset().getLayout().name()) != allowed_insets.end();
 
-	else if (allowed_layouts.find(d->layout_->name()) != allowed_layouts.end())
-		result = true;
+	bool in_allowed_layout =
+		allowed_layouts.find(d->layout_->name()) != allowed_layouts.end();
 
-	else if (inInset().asInsetArgument()) {
+	if (!in_allowed_inset && inInset().asInsetArgument()) {
 		// check if the argument allows the inset in question
 		if (cur.depth() > 1) {
 			docstring parlayout = cur[cur.depth() - 2].inset().getLayout().name()
 					+ from_ascii("@") + from_ascii(inInset().asInsetArgument()->name());
 			if (allowed_insets.find(parlayout) != allowed_insets.end())
-				result = true;
+				in_allowed_inset = true;
 		}
 	}
 	
-	if (result && il.allowedOccurrences() != -1) {
-		int have_ins = 0;
-		for (auto const & table : insetList())
-			if (table.inset->getLayout().name() == il.name())
-				++have_ins;
+	int have_ins = 0;
+	// check if we exceed the number of allowed insets in this inset
+	if (in_allowed_inset && inInset().asInsetText() && il.allowedOccurrences() != -1) {
+		ParagraphList & pars = cur.text()->paragraphs();
+			for (Paragraph const & par : pars) {
+				for (auto const & table : par.insetList())
+				if (table.inset->getLayout().name() == il.name())
+					++have_ins;
+			}
+		if (have_ins >= il.allowedOccurrences())
+			return false;
+	}
+	
+	have_ins = 0;
+	// check if we exceed the number of allowed insets in the layout group
+	if (in_allowed_layout && il.allowedOccurrences() != -1) {
+		pit_type pit = cur.pit();
+		pit_type lastpit = cur.pit();
+		ParagraphList & pars = cur.text()->paragraphs();
+		// If we are not on a list-type environment or AllowedOccurrencesPerItem
+		// is false, we check the whole paragraph group
+		if (d->layout_->isEnvironment()
+		    && !(il.allowedOccurrencesPerItem()
+			 && (d->layout_->latextype == LATEX_LIST_ENVIRONMENT
+			     || d->layout_->latextype == LATEX_ITEM_ENVIRONMENT))) {
+			lastpit = cur.lastpit();
+			// get the first paragraph in sequence with this layout
+			depth_type const current_depth = params().depth();
+			while (true) {
+				if (pit == 0)
+					break;
+				Paragraph cpar = pars[pit - 1];
+				if (&cpar.layout() == d->layout_
+				    && cpar.params().depth() == current_depth)
+					--pit;
+				else
+					break;
+			}
+		}
+		for (; pit <= lastpit; ++pit) {
+			if (&pars[pit].layout() != d->layout_)
+				break;
+			for (auto const & table : pars[pit].insetList())
+				if (table.inset->getLayout().name() == il.name())
+					++have_ins;
+		}
 		if (have_ins >= il.allowedOccurrences())
 			return false;
 	}
 	
-	if (result)
+	if (in_allowed_layout || in_allowed_inset)
 		return true;
 
 	return (allowed_insets.empty() && allowed_layouts.empty());
diff --git a/src/insets/InsetLayout.cpp b/src/insets/InsetLayout.cpp
index de217a0..fc1a53c 100644
--- a/src/insets/InsetLayout.cpp
+++ b/src/insets/InsetLayout.cpp
@@ -67,6 +67,7 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
 		IL_ALLOWED_IN_INSET,
 		IL_ALLOWED_IN_LAYOUT,
 		IL_ALLOWED_OCCURRENCES,
+		IL_ALLOWED_OCCURRENCES_PER_ITEM,
 		IL_ARGUMENT,
 		IL_BABELPREAMBLE,
 		IL_BGCOLOR,
@@ -155,6 +156,7 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
 		{ "allowedininsets", IL_ALLOWED_IN_INSET },
 		{ "allowedinlayouts", IL_ALLOWED_IN_LAYOUT },
 		{ "allowedoccurrences", IL_ALLOWED_OCCURRENCES },
+		{ "allowedoccurrencesperitem", IL_ALLOWED_OCCURRENCES_PER_ITEM },
 		{ "argument", IL_ARGUMENT },
 		{ "babelpreamble", IL_BABELPREAMBLE },
 		{ "bgcolor", IL_BGCOLOR },
@@ -656,6 +658,9 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
 		case IL_ALLOWED_OCCURRENCES:
 			lex >> allowed_occurrences_;
 			break;
+		case IL_ALLOWED_OCCURRENCES_PER_ITEM:
+			lex >> allowed_occurrences_per_item_;
+			break;
 		case IL_END:
 			getout = true;
 			break;
diff --git a/src/insets/InsetLayout.h b/src/insets/InsetLayout.h
index bf5a016..6700100 100644
--- a/src/insets/InsetLayout.h
+++ b/src/insets/InsetLayout.h
@@ -258,6 +258,8 @@ public:
 	std::set<docstring> const & allowedInLayouts() const { return allowed_in_layouts_; }
 	///
 	int allowedOccurrences() const { return allowed_occurrences_; }
+	///
+	bool allowedOccurrencesPerItem() const { return allowed_occurrences_per_item_; }
 private:
 	///
 	void makeDefaultCSS() const;
@@ -447,6 +449,8 @@ private:
 	std::set<docstring> allowed_in_layouts_;
 	///
 	int allowed_occurrences_ = -1;
+	///
+	bool allowed_occurrences_per_item_ = false;
 };
 
 ///


More information about the lyx-cvs mailing list