commit 3591aa833f902f1c7a3e060a5614e4ae1d72fcf4
Author: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date:   Mon May 14 23:58:42 2018 +0200

    Implement layout-toggle lfun
    
    This is like `layout', except that the layout is reset to default if
    it was already correct. In the case of a selection, the layout is set
    normally if some layouts in the selection are not at the required
    layout; it is reset only if all the layouts of the selection already
    have the required layout.
    
    Part of ticket #9864.
---
 src/FuncCode.h    |    1 +
 src/LyXAction.cpp |   13 +++++++++++++
 src/Text3.cpp     |   21 ++++++++++++++-------
 3 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/FuncCode.h b/src/FuncCode.h
index f19a3e6..6154cdd 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -479,6 +479,7 @@ enum FuncCode
        LFUN_BUFFER_ANONYMIZE,          // sanda, 20180201
        LFUN_GRAPHICS_UNIFY,            // sanda, 20180207
        LFUN_MASTER_BUFFER_EXPORT,      // rkh, 20180417
+       LFUN_LAYOUT_TOGGLE,             // lasgouttes 20180514
        LFUN_LASTACTION                 // end of the table
 };
 
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 6e7c96a..5a4bab9 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -2333,6 +2333,19 @@ void LyXAction::init()
                { LFUN_LAYOUT, "layout", Noop, Layout },
 
 /*!
+ * \var lyx::FuncCode lyx::LFUN_LAYOUT_TOGGLE
+ * \li Action: Toggles the layout (that is, environment) for the current 
paragraph.
+ * \li Notion: Contrary to `layout', this function resets the current
+ * (or selection) layout to the standard layout it already has the
+ * correct layout. Useful for toolbar icons.
+ * \li Syntax: layout-toggle <LAYOUT> [ignorenests]
+ * \li Params: <LAYOUT>: the layout to toggle\n
+               ignorenests: If specified, nesting advices will be ignored.
+ * \endvar
+ */
+               { LFUN_LAYOUT_TOGGLE, "layout-toggle", Noop, Layout },
+
+/*!
  * \var lyx::FuncCode lyx::LFUN_LAYOUT_MODULES_CLEAR
  * \li Action: Clears the module list.
  * \li Notion: Clears the list of included modules for the current buffer.
diff --git a/src/Text3.cpp b/src/Text3.cpp
index 9d0e8ff..619bbf8 100644
--- a/src/Text3.cpp
+++ b/src/Text3.cpp
@@ -572,16 +572,16 @@ Language const * getLanguage(Cursor const & cur, string 
const & lang)
 }
 
 
-docstring resolveLayout(docstring layout, Cursor const & cur)
+docstring resolveLayout(docstring layout, DocIterator const & dit)
 {
-       Paragraph const & par = cur.paragraph();
+       Paragraph const & par = dit.paragraph();
        docstring const old_layout = par.layout().name();
-       DocumentClass const & tclass = cur.buffer()->params().documentClass();
+       DocumentClass const & tclass = dit.buffer()->params().documentClass();
 
        if (layout.empty())
                layout = tclass.defaultLayoutName();
 
-       if (cur.inset().forcePlainLayout(cur.idx()))
+       if (dit.inset().forcePlainLayout(dit.idx()))
                // in this case only the empty layout is allowed
                layout = tclass.plainLayoutName();
        else if (par.usePlainLayout()) {
@@ -606,7 +606,7 @@ docstring resolveLayout(docstring layout, Cursor const & 
cur)
 }
 
 
-bool isAlreadyLayout(docstring const & layout, Cursor const & cur)
+bool isAlreadyLayout(docstring const & layout, CursorData const & cur)
 {
        ParagraphList const & pars = cur.text()->paragraphs();
 
@@ -1546,7 +1546,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                cur.message(cur.paragraph().layout().name());
                break;
 
-       case LFUN_LAYOUT: {
+       case LFUN_LAYOUT:
+       case LFUN_LAYOUT_TOGGLE: {
                bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
                docstring req_layout = ignoreautonests ? 
from_utf8(cmd.getArg(0)) : cmd.argument();
                LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << 
to_utf8(req_layout));
@@ -1561,6 +1562,11 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                docstring const old_layout = cur.paragraph().layout().name();
                bool change_layout = !isAlreadyLayout(layout, cur);
 
+               if (cmd.action() == LFUN_LAYOUT_TOGGLE && !change_layout) {
+                       change_layout = true;
+                       layout = resolveLayout(docstring(), cur);
+               }
+
                if (change_layout) {
                        setLayout(cur, layout);
                        if (cur.pit() > 0 && !ignoreautonests) {
@@ -3286,7 +3292,8 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & 
cmd,
                }
                break;
 
-       case LFUN_LAYOUT: {
+       case LFUN_LAYOUT:
+       case LFUN_LAYOUT_TOGGLE: {
                bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
                docstring const req_layout = ignoreautonests ? 
from_utf8(cmd.getArg(0)) : cmd.argument();
                docstring const layout = resolveLayout(req_layout, cur);

Reply via email to