[LyX/master] Add AllowedInInsets and AllowedInLayouts InsetLayout tags

Juergen Spitzmueller spitz at lyx.org
Tue Jul 25 10:32:47 UTC 2023


commit 0b463bfe0fd4b3cf46e3c5876444dc0517cff26e
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Tue Jul 25 13:37:07 2023 +0200

    Add AllowedInInsets and AllowedInLayouts InsetLayout tags
    
    This allows to restrict the use of flex insets to specific contexts.
    
    Note that this is currently very basis. It only considers immediately
    containing insets/layouts and can be surpassed via cut and paste.
    
    The use case that made me finally implement this are macros in covington
    that make sense only in specific environments (but also consider \inst
    in some article classes)
---
 lib/doc/Customization.lyx    |  137 +++++++++++++++++++++++++++++++++++++++++-
 lib/doc/de/Customization.lyx |  109 +++++++++++++++++++++++++++++++++-
 lib/scripts/layout2layout.py |   11 +++-
 src/Paragraph.cpp            |   26 ++++++++
 src/Paragraph.h              |    5 ++
 src/Text.cpp                 |    2 +
 src/TextClass.cpp            |    2 +-
 src/insets/InsetLayout.cpp   |   29 +++++++++
 src/insets/InsetLayout.h     |    8 +++
 9 files changed, 325 insertions(+), 4 deletions(-)

diff --git a/lib/doc/Customization.lyx b/lib/doc/Customization.lyx
index 411c74f..3ac10a3 100644
--- a/lib/doc/Customization.lyx
+++ b/lib/doc/Customization.lyx
@@ -1,5 +1,5 @@
 #LyX 2.4 created this file. For more info see https://www.lyx.org/
-\lyxformat 616
+\lyxformat 617
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -20943,6 +20943,135 @@ string
  This is only implemented for Flex insets.
  Default:
  disabled.
+\change_inserted -712698321 1690284265
+
+\end_layout
+
+\begin_layout Description
+
+\change_inserted -712698321 1690284544
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284277
+AllowedInInsets
+\end_layout
+
+\end_inset
+
+ Includes a comma-separated list of insets to which this inset can be inserted.
+ Must be ended by 
+\begin_inset Quotes eld
+\end_inset
+
+
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284318
+EndAllowedInInsets
+\end_layout
+
+\end_inset
+
+
+\begin_inset Quotes erd
+\end_inset
+
+.
+ If you you also want the insertion to be allowed in specific arguments of the target insets,
+ append the argument name after @ (e.
+\begin_inset space \thinspace{}
+\end_inset
+
+g.,
+ 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284445
+My_Inset at post:1
+\change_unchanged
+
+\end_layout
+
+\end_inset
+
+).
+ Note that this currently only supports immediately containing insets (no deeper nesting).
+ See also 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284329
+AllowedInLayouts
+\end_layout
+
+\end_inset
+
+.
+\end_layout
+
+\begin_layout Description
+
+\change_inserted -712698321 1690284561
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284338
+AllowedInLayouts
+\end_layout
+
+\end_inset
+
+ Includes a comma-separated list of layouts within which this inset can be inserted.
+ Must be ended by 
+\begin_inset Quotes eld
+\end_inset
+
+
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284353
+EndAllowedInLayouts
+\end_layout
+
+\end_inset
+
+
+\begin_inset Quotes erd
+\end_inset
+
+.
+ Note that this currently only supports immediately containing layouts (no deeper nesting).
+ See also 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+
+\change_inserted -712698321 1690284358
+AllowedInInsets
+\end_layout
+
+\end_inset
+
+.
+\change_unchanged
+
 \end_layout
 
 \begin_layout Description
@@ -21848,6 +21977,8 @@ status collapsed
 
 \change_inserted 47243155 1689528630
 InheritFont
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -21860,6 +21991,8 @@ status collapsed
 
 \change_inserted 47243155 1689528607
 0
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -21878,6 +22011,8 @@ status collapsed
 
 \emph on
 1
+\change_unchanged
+
 \end_layout
 
 \end_inset
diff --git a/lib/doc/de/Customization.lyx b/lib/doc/de/Customization.lyx
index 39e2121..748e29e 100644
--- a/lib/doc/de/Customization.lyx
+++ b/lib/doc/de/Customization.lyx
@@ -1,5 +1,5 @@
 #LyX 2.4 created this file. For more info see https://www.lyx.org/
-\lyxformat 615
+\lyxformat 617
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -17289,6 +17289,113 @@ IsTocCaption
 status collapsed
 
 \begin_layout Plain Layout
+AllowedInInsets
+\end_layout
+
+\end_inset
+
+ enthält eine kommaseparierte Liste von Einfügungen,
+ in die diese Einfügung platziert werden kann.
+ Muss mittels 
+\begin_inset Quotes eld
+\end_inset
+
+
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+EndAllowedInInsets
+\end_layout
+
+\end_inset
+
+
+\begin_inset Quotes erd
+\end_inset
+
+ beendet werden.
+ Falls Sie die Einfügung (auch) in bestimmten Argumenten der entsprechenden Einfügung erlauben möchten,
+ hängen Sie den Namen des Arguments nach @ an (bspw.
+ 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+Meine_Einfuegung at post:1
+\end_layout
+
+\end_inset
+
+).
+ Beachten Sie,
+ dass im Moment nur Einfügungen berücksichtigt werden,
+ die eine Hierarchiestufe höher stehen (keine tiefere Verschachtelung).
+ Siehe auch 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+AllowedInLayouts
+\end_layout
+
+\end_inset
+
+.
+\end_layout
+
+\begin_layout Description
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+AllowedInLayouts
+\end_layout
+
+\end_inset
+
+ enthält eine kommaseparierte Liste von Stilen,
+ in die diese Einfügung platziert werden kann.
+ Muss mittels 
+\begin_inset Quotes eld
+\end_inset
+
+
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+EndAllowedInLayouts
+\end_layout
+
+\end_inset
+
+
+\begin_inset Quotes erd
+\end_inset
+
+ beendet werden.
+ Beachten Sie,
+ dass im Moment nur Stile berücksichtigt werden,
+ die die Einfügung unmittelbar enthalten (keine tiefere Verschachtelung).
+ Siehe auch 
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
+AllowedInInsets
+\end_layout
+
+\end_inset
+
+.
+\end_layout
+
+\begin_layout Description
+\begin_inset Flex Code
+status collapsed
+
+\begin_layout Plain Layout
 Argument
 \end_layout
 
diff --git a/lib/scripts/layout2layout.py b/lib/scripts/layout2layout.py
index 54f7059..5f4a504 100644
--- a/lib/scripts/layout2layout.py
+++ b/lib/scripts/layout2layout.py
@@ -11,7 +11,7 @@
 # This script will update a .layout file to current format
 
 # The latest layout format is also defined in src/TextClass.cpp
-currentFormat = 101
+currentFormat = 102
 
 
 # Incremented to format 4, 6 April 2007, lasgouttes
@@ -340,6 +340,10 @@ currentFormat = 101
 # Incremented to format 101, 22 July 2023 by lasgouttes
 # add InsetLayout tag InheritFont
 
+# Incremented to format 102, 25 July 2023 by spitz
+# add InsetLayout tags AllowedInInsets, EndAllowedInInsets,
+# AllowedInLayouts, EndAllowedInLayouts
+
 # Do not forget to document format change in Customization
 # Manual (section "Declaring a new text class").
 
@@ -587,6 +591,11 @@ def convert(lines, end_format):
                 i += 1
             continue
 
+        if format == 101:
+            # nothing to do.
+            i += 1
+            continue
+
         if format == 100:
             # InheritFont has been introduced and defaults to true. Some insets had
             # an hardcoded inheritFont') method returning true. We removed them, so
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index d7fa35e..9e7fe30 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -25,6 +25,7 @@
 #include "BufferEncodings.h"
 #include "Changes.h"
 #include "Counters.h"
+#include "Cursor.h"
 #include "InsetList.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
@@ -2338,6 +2339,31 @@ bool Paragraph::parbreakIsNewline() const
 }
 
 
+bool Paragraph::allowedInContext(Cursor const & cur, InsetLayout const & il) const
+{
+	set<docstring> const & allowed_insets = il.allowedInInsets();
+	set<docstring> const & allowed_layouts = il.allowedInLayouts();
+
+	if (allowed_insets.find(inInset().getLayout().name()) != allowed_insets.end())
+		return true;
+
+	if (allowed_layouts.find(d->layout_->name()) != allowed_layouts.end())
+		return true;
+
+	if (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())
+				return true;
+		}
+	}
+
+	return (allowed_insets.empty() && allowed_layouts.empty());
+}
+
+
 bool Paragraph::isPartOfTextSequence() const
 {
 	for (pos_type i = 0; i < size(); ++i) {
diff --git a/src/Paragraph.h b/src/Paragraph.h
index ca88dd7..c8f1373 100644
--- a/src/Paragraph.h
+++ b/src/Paragraph.h
@@ -22,6 +22,8 @@
 #include "support/strfwd.h"
 #include "support/types.h"
 
+#include "insets/InsetLayout.h"
+
 #include <set>
 #include <vector>
 
@@ -31,6 +33,7 @@ class AuthorList;
 class Buffer;
 class BufferParams;
 class Change;
+class Cursor;
 class DocIterator;
 class docstring_list;
 class DocumentClass;
@@ -241,6 +244,8 @@ public:
 	///
 	bool parbreakIsNewline() const;
 	///
+	bool allowedInContext(Cursor const & cur, InsetLayout const & il) const;
+	///
 	bool isPartOfTextSequence() const;
 	///
 	pos_type size() const;
diff --git a/src/Text.cpp b/src/Text.cpp
index 9850f55..9dff2b3 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -6544,6 +6544,8 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
 			s = from_ascii("Flex:") + s;
 		if (!cur.buffer()->params().documentClass().hasInsetLayout(s))
 			enable = false;
+		else if (!cur.paragraph().allowedInContext(cur, cur.buffer()->params().documentClass().insetLayout(s)))
+			enable = false;
 		else {
 			InsetLyXType ilt =
 				cur.buffer()->params().documentClass().insetLayout(s).lyxtype();
diff --git a/src/TextClass.cpp b/src/TextClass.cpp
index 4dd9ae0..5854bcd 100644
--- a/src/TextClass.cpp
+++ b/src/TextClass.cpp
@@ -59,7 +59,7 @@ namespace lyx {
 // You should also run the development/tools/updatelayouts.py script,
 // to update the format of all of our layout files.
 //
-int const LAYOUT_FORMAT = 101; // lasgouttes: add InheritFont tag
+int const LAYOUT_FORMAT = 102; // spitz: add AllowedInInsets and AllowedInLayouts tag
 
 
 // Layout format for the current lyx file format. Controls which format is
diff --git a/src/insets/InsetLayout.cpp b/src/insets/InsetLayout.cpp
index 3efe318..91287f4 100644
--- a/src/insets/InsetLayout.cpp
+++ b/src/insets/InsetLayout.cpp
@@ -64,6 +64,8 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
 {
 	enum {
 		IL_ADDTOTOC,
+		IL_ALLOWED_IN_INSET,
+		IL_ALLOWED_IN_LAYOUT,
 		IL_ARGUMENT,
 		IL_BABELPREAMBLE,
 		IL_BGCOLOR,
@@ -149,6 +151,8 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
 
 	LexerKeyword elementTags[] = {
 		{ "addtotoc", IL_ADDTOTOC },
+		{ "allowedininsets", IL_ALLOWED_IN_INSET },
+		{ "allowedinlayouts", IL_ALLOWED_IN_LAYOUT },
 		{ "argument", IL_ARGUMENT },
 		{ "babelpreamble", IL_BABELPREAMBLE },
 		{ "bgcolor", IL_BGCOLOR },
@@ -622,6 +626,31 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
 		case IL_EDITEXTERNAL:
 			lex >> edit_external_;
 			break;
+		case IL_ALLOWED_IN_INSET: {
+			docstring allowed = lex.getLongString(from_ascii("EndAllowedInInsets"));
+			allowed = subst(allowed, from_ascii("\n"), docstring());
+			allowed = subst(allowed, from_ascii("\t"), docstring());
+			allowed = subst(allowed, from_ascii("\""), docstring());
+			allowed = subst(allowed, '_', ' ');
+			vector<docstring> const allowances =
+				getVectorFromString(allowed, from_ascii(","), false, true);
+			allowed_in_insets_.clear();
+			allowed_in_insets_.insert(allowances.begin(), allowances.end());
+			break;
+		}
+
+		case IL_ALLOWED_IN_LAYOUT: {
+			docstring allowed = lex.getLongString(from_ascii("EndAllowedInLayouts"));
+			allowed = subst(allowed, from_ascii("\n"), docstring());
+			allowed = subst(allowed, from_ascii("\t"), docstring());
+			allowed = subst(allowed, from_ascii("\""), docstring());
+			allowed = subst(allowed, '_', ' ');
+			vector<docstring> const allowances =
+				getVectorFromString(allowed, from_ascii(","), false, true);
+			allowed_in_layouts_.clear();
+			allowed_in_layouts_.insert(allowances.begin(), allowances.end());
+			break;
+		}
 		case IL_END:
 			getout = true;
 			break;
diff --git a/src/insets/InsetLayout.h b/src/insets/InsetLayout.h
index 2342543..4325ff8 100644
--- a/src/insets/InsetLayout.h
+++ b/src/insets/InsetLayout.h
@@ -252,6 +252,10 @@ public:
 	bool isTocCaption() const { return is_toc_caption_; }
 	///
 	bool editExternally () const { return edit_external_; }
+	///
+	std::set<docstring> const & allowedInInsets() const { return allowed_in_insets_; }
+	///
+	std::set<docstring> const & allowedInLayouts() const { return allowed_in_layouts_; }
 private:
 	///
 	void makeDefaultCSS() const;
@@ -435,6 +439,10 @@ private:
 	bool is_toc_caption_ = false;
 	///
 	bool edit_external_ = false;
+	/// Insets that can hold insets with this InsetLayout
+	std::set<docstring> allowed_in_insets_;
+	/// Layouts that can hold insets with this InsetLayout
+	std::set<docstring> allowed_in_layouts_;
 };
 
 ///


More information about the lyx-cvs mailing list