]> git.lyx.org Git - features.git/commitdiff
Improve AllowedOccurrences
authorJuergen Spitzmueller <spitz@lyx.org>
Wed, 26 Jul 2023 07:53:25 +0000 (09:53 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Wed, 26 Jul 2023 07:53:25 +0000 (09:53 +0200)
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
lib/doc/de/Customization.lyx
lib/layouts/linguistics.module
lib/scripts/layout2layout.py
src/Paragraph.cpp
src/insets/InsetLayout.cpp
src/insets/InsetLayout.h

index 04d9033ae6fffa33dbc743fe58818e5c0b83cea6..060a19756ae116c01560ea05f606450430504a79 100644 (file)
@@ -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
index aa5aad8f9460894cd131eacc998b58de674cb639..9ae0e7401660be5fa69b6d48fca8c598f5b66fdd 100644 (file)
@@ -17439,6 +17439,79 @@ AllowedInLayouts
 \begin_inset Flex Code
 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
index 43ec54fff04d45cb2e78e84ad6872fdcc5af7e04..bd2528c931cc1f3beedfabf485c69619b1bf36b3 100644 (file)
@@ -158,6 +158,7 @@ InsetLayout Flex:Example_Preamble
            Numbered_Example_(multiline), Numbered_Examples_(consecutive)
        EndAllowedInLayouts
        AllowedOccurrences   1
+       AllowedOccurrencesPerItem  true
 End
 
 InsetLayout Flex:Subexample_Preamble
index cb6023c96fcb4e0b88a694c1861a942e3201a3ca..c77b05e100c13152f2b0e4d660f5c389474eeb32 100644 (file)
@@ -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").
index 22e18435d655c4da60f3d685fae8e5d0faafb249..d4658faf845d7e49fb498fe4432d1c8ed7da37f5 100644 (file)
@@ -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());
index de217a07cc074fa977756822af1e61762b262e89..fc1a53c7ff8269d85ff0191c40091519125c85b3 100644 (file)
@@ -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;
index bf5a01688338175c9370e1ef8b4ac5afd7022464..6700100252a24b3996938b5d5359c1ad4f407b18 100644 (file)
@@ -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;
 };
 
 ///