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)
This commit is contained in:
Juergen Spitzmueller 2023-07-25 13:37:07 +02:00
parent bc95ff1ace
commit 0b463bfe0f
9 changed files with 325 additions and 4 deletions

View 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

View 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

View 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

View 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) {

View 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;

View 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();

View 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

View 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;

View 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_;
};
///