]> git.lyx.org Git - lyx.git/commitdiff
Add AllowedInInsets and AllowedInLayouts InsetLayout tags
authorJuergen Spitzmueller <spitz@lyx.org>
Tue, 25 Jul 2023 11:37:07 +0000 (13:37 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Tue, 25 Jul 2023 11:37:07 +0000 (13:37 +0200)
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
lib/doc/de/Customization.lyx
lib/scripts/layout2layout.py
src/Paragraph.cpp
src/Paragraph.h
src/Text.cpp
src/TextClass.cpp
src/insets/InsetLayout.cpp
src/insets/InsetLayout.h

index 411c74fba8dc99484c0a5cc1e38fcd770e0c5601..3ac10a39689e4e765cfdf20a9edb792715bb02e1 100644 (file)
@@ -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@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
index 39e21215de32cf2d31f040a62b792cfde6d69fd1..748e29e62a3cf843fc5cae7fd03644c26db05bd5 100644 (file)
@@ -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
@@ -17288,6 +17288,113 @@ IsTocCaption
 \begin_inset Flex Code
 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@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
index 54f705910b42b45a9611e00d1370d840a37d2192..5f4a50470644f2cca289cec97bbb73f146658536 100644 (file)
@@ -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
index d7fa35ef2ef526dff929cd0fe0ea3b779bbe6145..9e7fe3016328544f0ab1d8b837d864b9a08acfe9 100644 (file)
@@ -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) {
index ca88dd774e9d48e7f0472b8db35972899af5c0d0..c8f1373a976f333791700e108c0b6fa32833fc49 100644 (file)
@@ -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;
index 9850f5513c02428e8760ac946dd7b6c744657740..9dff2b37ab837f6fd5a3e0a67380237f97792a08 100644 (file)
@@ -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();
index 4dd9ae091c653c7558cd37b5dc6efa4a201e9c3f..5854bcdc278fc799dcf40c211c18f7c45f674f34 100644 (file)
@@ -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
index 3efe3189f1876bff6160f3eb5a928f5f269a85cf..91287f41aeaba51ebe164056b4694bf0199ad00f 100644 (file)
@@ -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;
index 2342543396b10c6a9368995c6683fc4b5bd08eeb..4325ff82ac93b301d8943e6c7d6627dae4a45e19 100644 (file)
@@ -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_;
 };
 
 ///