From 729ab602ca7081b034ff346dc80f0ecb4a55efe8 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Tue, 25 Jul 2023 15:49:53 +0200 Subject: [PATCH] To finish up #6854, also add AllowedOccurrences Again very basic for now (this only considers the current paragraph, not the whole inset or a serious of grouped layouts --- lib/doc/Customization.lyx | 68 +++++++++++++++++++++++++++++++++- lib/doc/de/Customization.lyx | 44 ++++++++++++++++++++++ lib/layouts/acmart.layout | 6 +++ lib/layouts/linguistics.module | 1 + lib/scripts/layout2layout.py | 2 +- src/Paragraph.cpp | 23 +++++++++--- src/TextClass.cpp | 2 +- src/insets/InsetLayout.cpp | 5 +++ src/insets/InsetLayout.h | 4 ++ 9 files changed, 147 insertions(+), 8 deletions(-) diff --git a/lib/doc/Customization.lyx b/lib/doc/Customization.lyx index 3ac10a3968..04d9033ae6 100644 --- a/lib/doc/Customization.lyx +++ b/lib/doc/Customization.lyx @@ -20957,6 +20957,8 @@ status collapsed \change_inserted -712698321 1690284277 AllowedInInsets +\change_unchanged + \end_layout \end_inset @@ -20974,6 +20976,8 @@ status collapsed \change_inserted -712698321 1690284318 EndAllowedInInsets +\change_unchanged + \end_layout \end_inset @@ -21013,6 +21017,8 @@ status collapsed \change_inserted -712698321 1690284329 AllowedInLayouts +\change_unchanged + \end_layout \end_inset @@ -21022,7 +21028,7 @@ AllowedInLayouts \begin_layout Description -\change_inserted -712698321 1690284561 +\change_inserted -712698321 1690292495 \begin_inset Flex Code status collapsed @@ -21030,6 +21036,8 @@ status collapsed \change_inserted -712698321 1690284338 AllowedInLayouts +\change_unchanged + \end_layout \end_inset @@ -21047,6 +21055,8 @@ status collapsed \change_inserted -712698321 1690284353 EndAllowedInLayouts +\change_unchanged + \end_layout \end_inset @@ -21065,11 +21075,67 @@ status collapsed \change_inserted -712698321 1690284358 AllowedInInsets +\change_unchanged + \end_layout \end_inset . +\end_layout + +\begin_layout Description + +\change_inserted -712698321 1690292676 +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout + +\change_inserted -712698321 1690292501 +AllowedOccurrences +\end_layout + +\end_inset + + [ +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout + +\change_inserted -712698321 1690292507 +int +\end_layout + +\end_inset + +] If +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout + +\change_inserted -712698321 1690292530 +AllowedInInsets +\end_layout + +\end_inset + + or +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout + +\change_inserted -712698321 1690292536 +AllowedInLayouts +\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. \change_unchanged \end_layout diff --git a/lib/doc/de/Customization.lyx b/lib/doc/de/Customization.lyx index 748e29e62a..aa5aad8f94 100644 --- a/lib/doc/de/Customization.lyx +++ b/lib/doc/de/Customization.lyx @@ -17395,6 +17395,50 @@ AllowedInInsets \begin_inset Flex Code status collapsed +\begin_layout Plain Layout +AllowedOccurrences +\end_layout + +\end_inset + + [ +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout +int +\end_layout + +\end_inset + +] Falls +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout +AllowedInInsets +\end_layout + +\end_inset + + oder +\begin_inset Flex Code +status collapsed + +\begin_layout Plain Layout +AllowedInLayouts +\end_layout + +\end_inset + + definiert wurde, + kann hiermit eine maximale Anzahl von Einfügungen pro Absatz festgelegt werden. +\end_layout + +\begin_layout Description +\begin_inset Flex Code +status collapsed + \begin_layout Plain Layout Argument \end_layout diff --git a/lib/layouts/acmart.layout b/lib/layouts/acmart.layout index 547fdf9df0..5b6c76ea1f 100644 --- a/lib/layouts/acmart.layout +++ b/lib/layouts/acmart.layout @@ -250,6 +250,7 @@ InsetLayout Flex:Department AllowedInLayouts Affiliation EndAllowedInLayouts + AllowedOccurrences 1 End InsetLayout Flex:Street_Address @@ -261,6 +262,7 @@ InsetLayout Flex:Street_Address AllowedInLayouts Affiliation EndAllowedInLayouts + AllowedOccurrences 1 End InsetLayout Flex:City @@ -272,6 +274,7 @@ InsetLayout Flex:City AllowedInLayouts Affiliation EndAllowedInLayouts + AllowedOccurrences 1 End InsetLayout Flex:Country @@ -283,6 +286,7 @@ InsetLayout Flex:Country AllowedInLayouts Affiliation EndAllowedInLayouts + AllowedOccurrences 1 End InsetLayout Flex:State @@ -294,6 +298,7 @@ InsetLayout Flex:State AllowedInLayouts Affiliation EndAllowedInLayouts + AllowedOccurrences 1 End InsetLayout Flex:Postal_Code @@ -305,6 +310,7 @@ InsetLayout Flex:Postal_Code AllowedInLayouts Affiliation EndAllowedInLayouts + AllowedOccurrences 1 End Style TitleNote diff --git a/lib/layouts/linguistics.module b/lib/layouts/linguistics.module index eafea96595..43ec54fff0 100644 --- a/lib/layouts/linguistics.module +++ b/lib/layouts/linguistics.module @@ -157,6 +157,7 @@ InsetLayout Flex:Example_Preamble AllowedInLayouts Numbered_Example_(multiline), Numbered_Examples_(consecutive) EndAllowedInLayouts + AllowedOccurrences 1 End InsetLayout Flex:Subexample_Preamble diff --git a/lib/scripts/layout2layout.py b/lib/scripts/layout2layout.py index 5f4a504706..cb6023c96f 100644 --- a/lib/scripts/layout2layout.py +++ b/lib/scripts/layout2layout.py @@ -342,7 +342,7 @@ currentFormat = 102 # Incremented to format 102, 25 July 2023 by spitz # add InsetLayout tags AllowedInInsets, EndAllowedInInsets, -# AllowedInLayouts, EndAllowedInLayouts +# AllowedInLayouts, EndAllowedInLayouts, AllowedOccurrences # 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 9e7fe30163..22e18435d6 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -2344,21 +2344,34 @@ bool Paragraph::allowedInContext(Cursor const & cur, InsetLayout const & il) con set const & allowed_insets = il.allowedInInsets(); set const & allowed_layouts = il.allowedInLayouts(); + bool result = false; if (allowed_insets.find(inInset().getLayout().name()) != allowed_insets.end()) - return true; + result = true; - if (allowed_layouts.find(d->layout_->name()) != allowed_layouts.end()) - return true; + else if (allowed_layouts.find(d->layout_->name()) != allowed_layouts.end()) + result = true; - if (inInset().asInsetArgument()) { + else 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; + result = true; } } + + if (result && il.allowedOccurrences() != -1) { + int have_ins = 0; + for (auto const & table : insetList()) + if (table.inset->getLayout().name() == il.name()) + ++have_ins; + if (have_ins >= il.allowedOccurrences()) + return false; + } + + if (result) + return true; return (allowed_insets.empty() && allowed_layouts.empty()); } diff --git a/src/TextClass.cpp b/src/TextClass.cpp index 5854bcdc27..d99004f7aa 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 = 102; // spitz: add AllowedInInsets and AllowedInLayouts tag +int const LAYOUT_FORMAT = 102; // spitz: add tags AllowedInInsets, AllowedInLayouts, AllowedOccurrences // 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 91287f41ae..de217a07cc 100644 --- a/src/insets/InsetLayout.cpp +++ b/src/insets/InsetLayout.cpp @@ -66,6 +66,7 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass, IL_ADDTOTOC, IL_ALLOWED_IN_INSET, IL_ALLOWED_IN_LAYOUT, + IL_ALLOWED_OCCURRENCES, IL_ARGUMENT, IL_BABELPREAMBLE, IL_BGCOLOR, @@ -153,6 +154,7 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass, { "addtotoc", IL_ADDTOTOC }, { "allowedininsets", IL_ALLOWED_IN_INSET }, { "allowedinlayouts", IL_ALLOWED_IN_LAYOUT }, + { "allowedoccurrences", IL_ALLOWED_OCCURRENCES }, { "argument", IL_ARGUMENT }, { "babelpreamble", IL_BABELPREAMBLE }, { "bgcolor", IL_BGCOLOR }, @@ -651,6 +653,9 @@ bool InsetLayout::read(Lexer & lex, TextClass const & tclass, allowed_in_layouts_.insert(allowances.begin(), allowances.end()); break; } + case IL_ALLOWED_OCCURRENCES: + lex >> allowed_occurrences_; + break; case IL_END: getout = true; break; diff --git a/src/insets/InsetLayout.h b/src/insets/InsetLayout.h index 4325ff82ac..bf5a016883 100644 --- a/src/insets/InsetLayout.h +++ b/src/insets/InsetLayout.h @@ -256,6 +256,8 @@ public: std::set const & allowedInInsets() const { return allowed_in_insets_; } /// std::set const & allowedInLayouts() const { return allowed_in_layouts_; } + /// + int allowedOccurrences() const { return allowed_occurrences_; } private: /// void makeDefaultCSS() const; @@ -443,6 +445,8 @@ private: std::set allowed_in_insets_; /// Layouts that can hold insets with this InsetLayout std::set allowed_in_layouts_; + /// + int allowed_occurrences_ = -1; }; ///