]> git.lyx.org Git - features.git/commitdiff
Implement environment-split before and previous
authorJuergen Spitzmueller <spitz@lyx.org>
Mon, 1 Jan 2018 11:34:39 +0000 (12:34 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Mon, 1 Jan 2018 11:34:39 +0000 (12:34 +0100)
Possibility to add an environment before the current one and to append
and environment from outside the nesting scope.

This commit includes all fixes that went in during testing these
functions in master. It also moves the menu items from Edit to Insert.

lib/bind/de/menus.bind
lib/bind/menus.bind
lib/ui/stdmenus.inc
src/LyXAction.cpp
src/Text3.cpp
src/frontends/qt4/Menus.cpp

index fb72535c1d2f761e6bd5d4305bc1a75fe1b9d30b..e9dc3731848ae8593000c824ec171ae3b23780cd 100644 (file)
@@ -113,7 +113,7 @@ Format 4
 \bind "M-a Down"               "outline-down"
 
 
-\bind "M-a Return"             "environment-split"
+\bind "M-a Return"             "command-alternatives environment-split ; environment-split previous"
 \bind "M-a S-Return"           "environment-split outer"
 
 # Obsolete Tastenbelegung: in die persönliche *.bind Datei kopieren und
index 458d9c9bcbac1a2f2e1a3a8c829a7c3d13d3bc3c..a27fab842d56bfd89944045421982f9d55d1f495 100644 (file)
@@ -126,7 +126,7 @@ Format 4
 \bind "M-p Up"                 "outline-up"
 \bind "M-p Down"               "outline-down"
 
-\bind "M-p Return"             "environment-split"
+\bind "M-p Return"             "command-alternatives environment-split ; environment-split previous"
 \bind "M-p S-Return"           "environment-split outer"
 
 
index c3a0c841a37d1c5a45f67be5528d54706af68840..1b79adb31b27367312451a0e1ed70ee8009c0be0 100644 (file)
@@ -134,7 +134,6 @@ Menuset
 # obvious what the context is for the others)
                OptItem "Increase List Depth|I" "depth-increment"
                OptItem "Decrease List Depth|D" "depth-decrement"
-               EnvironmentSeparators
                OptItem "Dissolve Inset" "inset-dissolve"
                OptItem "TeX Code Settings...|C" "inset-settings ert"
 # 'a' shortcut to match Insert entry, shouldn't clash with Table Settings
@@ -393,6 +392,7 @@ Menuset
                Item "Hyperlink...|k" "href-insert"
                Item "Footnote|F" "footnote-insert"
                Item "Marginal Note|M" "marginalnote-insert"
+               EnvironmentSeparators
                Arguments
                Item "TeX Code" "ert-insert"
                Item "Program Listing[[Menu]]" "listing-insert"
index e0d4e473d7ebb8f5fba653cfedb022af349270a9..7cd103b1c40866493ac2bf06dd99ba8faeb7073e 100644 (file)
@@ -1513,9 +1513,13 @@ void LyXAction::init()
 /*!
  * \var lyx::FuncCode lyx::LFUN_ENVIRONMENT_SPLIT
  * \li Action: Splits the current environment with a Separator.
- * \li Syntax: environment-split [outer]
+ * \li Syntax: environment-split [before|outer|previous]
  * \li Params: outer: If this is given, LyX will split the outermost environment in
- *                    the current nesting hierarchy.
+                      the current nesting hierarchy.\n
+               previous: If this is given, LyX will split the environment in the previous
+                      paragraph (is there is one).\n
+               before: If this is given, the new environment will be appended rather than
+                      prepended.
  * \li Origin: spitz, 23 Dec 2012
  * \endvar
  */
index af244002d130665b0f2d057df6cd153066a4d89c..9c4c7f9f7c36d9af51cd91a62f8a5b0769a5b86e 100644 (file)
@@ -1491,36 +1491,74 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_ENVIRONMENT_SPLIT: {
                bool const outer = cmd.argument() == "outer";
+               bool const previous = cmd.argument() == "previous";
+               bool const before = cmd.argument() == "before";
+               bool const normal = cmd.argument().empty();
                Paragraph const & para = cur.paragraph();
-               docstring layout = para.layout().name();
+               docstring layout;
+               if (para.layout().isEnvironment())
+                       layout = para.layout().name();
                depth_type split_depth = cur.paragraph().params().depth();
-               if (outer) {
-                       // check if we have an environment in our nesting hierarchy
+               depth_type nextpar_depth = 0;
+               if (outer || previous) {
+                       // check if we have an environment in our scope
                        pit_type pit = cur.pit();
                        Paragraph cpar = pars_[pit];
                        while (true) {
-                               if (pit == 0 || cpar.params().depth() == 0)
+                               if (pit == 0)
                                        break;
                                --pit;
                                cpar = pars_[pit];
+                               if (layout.empty() && previous
+                                   && cpar.layout().isEnvironment()
+                                   && cpar.params().depth() <= split_depth)
+                                       layout = cpar.layout().name();
                                if (cpar.params().depth() < split_depth
                                    && cpar.layout().isEnvironment()) {
-                                               layout = cpar.layout().name();
+                                               if (!previous)
+                                                       layout = cpar.layout().name();
                                                split_depth = cpar.params().depth();
                                }
+                               if (cpar.params().depth() == 0)
+                                       break;
                        }
                }
-               if (cur.pos() > 0)
+               if ((outer || normal) && cur.pit() < cur.lastpit()) {
+                       // save nesting of following paragraph
+                       Paragraph cpar = pars_[cur.pit() + 1];
+                       nextpar_depth = cpar.params().depth();
+               }
+               if (before)
+                       cur.top().setPitPos(cur.pit(), 0);
+               if (before || cur.pos() > 0)
                        lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
+               else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
+                       lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
                if (outer) {
                        while (cur.paragraph().params().depth() > split_depth)
                                lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
                }
                DocumentClass const & tc = bv->buffer().params().documentClass();
-               lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
+               lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
+                                         + from_ascii("\" ignoreautonests")));
                lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
-               lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
+               if (before) {
+                       cur.backwardPos();
+                       lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
+                       while (cur.paragraph().params().depth() < split_depth)
+                               lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
+               }
+               else
+                       lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
                lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
+               if ((outer || normal) && nextpar_depth > 0) {
+                       // restore nesting of following paragraph
+                       DocIterator scur = cur;
+                       cur.forwardPar();
+                       while (cur.paragraph().params().depth() < nextpar_depth)
+                               lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
+                       cur.setCursor(scur);
+               }
 
                break;
        }
@@ -3180,6 +3218,19 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
                        enable = res;
                        break;
                }
+               else if (cmd.argument() == "previous") {
+                       // look if we have an environment in the previous par
+                       pit_type pit = cur.pit();
+                       Paragraph cpar = pars_[pit];
+                       if (pit > 0) {
+                               --pit;
+                               cpar = pars_[pit];
+                               enable = cpar.layout().isEnvironment();
+                               break;
+                       }
+                       enable = false;
+                       break;
+               }
                else if (cur.paragraph().layout().isEnvironment()) {
                        enable = true;
                        break;
index c97d23d44c28ef05bbd19170a001b6b596b84ea0..ecae50d5815166819b6a4a38e0a5fcf6dd88049f 100644 (file)
@@ -1856,33 +1856,56 @@ void MenuDefinition::expandEnvironmentSeparators(BufferView const * bv)
                return;
 
        pit_type pit = bv->cursor().selBegin().pit();
+       pos_type pos = bv->cursor().selBegin().pos();
        Paragraph const & par = text->getPar(pit);
        docstring const curlayout = par.layout().name();
        docstring outerlayout;
+       docstring prevlayout;
        depth_type current_depth = par.params().depth();
-       // check if we have an environment in our nesting hierarchy
+       // check if we have an environment in our scope
        Paragraph cpar = par;
        while (true) {
-               if (pit == 0 || cpar.params().depth() == 0)
+               if (pit == 0)
                        break;
                --pit;
                cpar = text->getPar(pit);
+               if (cpar.layout().isEnvironment() && prevlayout.empty()
+                   && cpar.params().depth() <= current_depth)
+                               prevlayout = cpar.layout().name();
                if (cpar.params().depth() < current_depth
                    && cpar.layout().isEnvironment()) {
                                outerlayout = cpar.layout().name();
                                current_depth = cpar.params().depth();
                }
+               if (cpar.params().depth() == 0)
+                       break;
        }
        if (par.layout().isEnvironment()) {
-               docstring const label =
-                       bformat(_("Start New Environment (%1$s)"),
+               docstring label = bformat(_("Separated %1$s Above"),
                                translateIfPossible(curlayout));
                add(MenuItem(MenuItem::Command, toqstr(label),
-                            FuncRequest(LFUN_ENVIRONMENT_SPLIT)));
+                            FuncRequest(LFUN_ENVIRONMENT_SPLIT,
+                                        from_ascii("before"))));
+               if (!par.layout().keepempty || pos > 0 || !text->isFirstInSequence(pit)) {
+                       label = bformat(_("Separated %1$s Below"),
+                                       translateIfPossible(curlayout));
+                       add(MenuItem(MenuItem::Command, toqstr(label),
+                                    FuncRequest(LFUN_ENVIRONMENT_SPLIT)));
+               }
        }
-       if (!outerlayout.empty()) {
+       else if (!prevlayout.empty()) {
                docstring const label =
-                       bformat(_("Start New Parent Environment (%1$s)"),
+                       bformat(_("Separated %1$s Below"),
+                               translateIfPossible(prevlayout));
+               add(MenuItem(MenuItem::Command, toqstr(label),
+                            FuncRequest(LFUN_ENVIRONMENT_SPLIT,
+                                        from_ascii("previous"))));
+       }
+       if (!outerlayout.empty()) {
+               docstring const label = (outerlayout == curlayout) ?
+                       bformat(_("Separated Outer %1$s Below"),
+                               translateIfPossible(outerlayout)) :
+                       bformat(_("Separated %1$s Below"),
                                translateIfPossible(outerlayout));
                add(MenuItem(MenuItem::Command, toqstr(label),
                             FuncRequest(LFUN_ENVIRONMENT_SPLIT,