]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
Add AllowedInInsets and AllowedInLayouts InsetLayout tags
[lyx.git] / src / Text.cpp
index d8cd365093e53313f700b65370b94123f4b5ad7e..9dff2b37ab837f6fd5a3e0a67380237f97792a08 100644 (file)
@@ -716,9 +716,8 @@ double Text::spacing(Paragraph const & par) const
 /**
  * This breaks a paragraph at the specified position.
  * The new paragraph will:
- * - Decrease depth by one (or change layout to default layout) when
- *    keep_layout == false
- * - keep current depth and layout when keep_layout == true
+ * - change layout to default layout when keep_layout == false
+ * - keep layout when keep_layout == true
  */
 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
                    bool keep_layout)
@@ -733,21 +732,13 @@ static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
 
        // remember to set the inset_owner
        tmp->setInsetOwner(&par.inInset());
-       // without doing that we get a crash when typing <Return> at the
-       // end of a paragraph
-       tmp->setPlainOrDefaultLayout(bparams.documentClass());
+       tmp->params().depth(par.params().depth());
 
        if (keep_layout) {
                tmp->setLayout(par.layout());
                tmp->setLabelWidthString(par.params().labelWidthString());
-               tmp->params().depth(par.params().depth());
-       } else if (par.params().depth() > 0) {
-               Paragraph const & hook = pars[text.outerHook(par_offset)];
-               tmp->setLayout(hook.layout());
-               // not sure the line below is useful
-               tmp->setLabelWidthString(par.params().labelWidthString());
-               tmp->params().depth(hook.params().depth());
-       }
+       } else
+               tmp->setPlainOrDefaultLayout(bparams.documentClass());
 
        bool const isempty = (par.allowEmpty() && par.empty());
 
@@ -2917,7 +2908,8 @@ void Text::setParagraphs(Cursor const & cur, docstring const & arg, bool merge)
        Layout priorlayout;
        Cursor c(cur.bv());
        c.setCursor(cur.selectionBegin());
-       for ( ; c <= cur.selectionEnd() ; ++c.pit()) {
+       pit_type const last_pit = cur.selectionEnd().pit();
+       for ( ; c.pit() <= last_pit ; ++c.pit()) {
                Paragraph & par = c.paragraph();
                ParagraphParameters params = par.params();
                params.read(argument, merge);
@@ -2943,7 +2935,8 @@ void Text::setParagraphs(Cursor const & cur, ParagraphParameters const & p)
        Layout priorlayout;
        Cursor c(cur.bv());
        c.setCursor(cur.selectionBegin());
-       for ( ; c < cur.selectionEnd() ; ++c.pit()) {
+       pit_type const last_pit = cur.selectionEnd().pit();
+       for ( ; c.pit() <= last_pit ; ++c.pit()) {
                Paragraph & par = c.paragraph();
                // Changes to label width string apply to all paragraphs
                // with same layout in a sequence.
@@ -3746,11 +3739,12 @@ void insertSeparator(Cursor const & cur, depth_type const depth)
 }
 
 
-void outline(OutlineOp mode, Cursor & cur, Text * text)
+void outline(OutlineOp mode, Cursor & cur, bool local)
 {
        Buffer & buf = *cur.buffer();
+       Text & text = *cur.text();
        pit_type & pit = cur.pit();
-       ParagraphList & pars = buf.text().paragraphs();
+       ParagraphList & pars = text.paragraphs();
        ParagraphList::iterator const bgn = pars.begin();
        // The first paragraph of the area to be copied:
        ParagraphList::iterator start = pars.iterator_at(pit);
@@ -3759,18 +3753,20 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
        ParagraphList::iterator const end = pars.end();
        depth_type const current_depth = cur.paragraph().params().depth();
 
-       int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
+       int const thistoclevel = text.getTocLevel(distance(bgn, start));
        int toclevel;
 
        // Move out (down) from this section header
        if (finish != end)
                ++finish;
 
-       // Seek the one (on same level) below
-       for (; finish != end; ++finish) {
-               toclevel = buf.text().getTocLevel(distance(bgn, finish));
-               if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
-                       break;
+       if (!local || (mode != OutlineIn && mode != OutlineOut)) {
+               // Seek the one (on same level) below
+               for (; finish != end; ++finish) {
+                       toclevel = text.getTocLevel(distance(bgn, finish));
+                       if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
+                               break;
+               }
        }
 
        switch (mode) {
@@ -3785,7 +3781,7 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
                        // Search previous same-level header above
                        do {
                                --dest;
-                               toclevel = buf.text().getTocLevel(distance(bgn, dest));
+                               toclevel = text.getTocLevel(distance(bgn, dest));
                        } while(dest != bgn
                                && (toclevel == Layout::NOT_IN_TOC
                                    || toclevel > thistoclevel));
@@ -3819,7 +3815,7 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
                                // Get the parent paragraph (outer in nested context)
                                pit_type const parent =
                                        before->params().depth() > current_depth
-                                               ? text->depthHook(distance(bgn, before), current_depth)
+                                               ? text.depthHook(distance(bgn, before), current_depth)
                                                : distance(bgn, before);
                                // If a environment with same layout preceeds the moved one in the new
                                // position, and there is no separator yet, insert one.
@@ -3845,7 +3841,7 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
                        ParagraphList::iterator dest = next(finish, 1);
                        // Go further down to find header to insert in front of:
                        for (; dest != end; ++dest) {
-                               toclevel = buf.text().getTocLevel(distance(bgn, dest));
+                               toclevel = text.getTocLevel(distance(bgn, dest));
                                if (toclevel != Layout::NOT_IN_TOC
                                      && toclevel <= thistoclevel)
                                        break;
@@ -3861,7 +3857,7 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
                        // Get the parent paragraph (outer in nested context)
                        pit_type const parent =
                                before->params().depth() > current_depth
-                                       ? text->depthHook(distance(bgn, before), current_depth)
+                                       ? text.depthHook(distance(bgn, before), current_depth)
                                        : distance(bgn, before);
                        // If a environment with same layout preceeds the moved one in the new
                        // position, and there is no separator yet, insert one.
@@ -3902,7 +3898,7 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
                        ParagraphList::iterator cstart = start;
                        bool strucchange = false;
                        for (; cstart != finish; ++cstart) {
-                               toclevel = buf.text().getTocLevel(distance(bgn, cstart));
+                               toclevel = text.getTocLevel(distance(bgn, cstart));
                                if (toclevel == Layout::NOT_IN_TOC)
                                        continue;
 
@@ -3938,7 +3934,7 @@ void outline(OutlineOp mode, Cursor & cur, Text * text)
                        pit_type const len = distance(start, finish);
                        buf.undo().recordUndo(cur, pit, pit + len - 1);
                        for (; start != finish; ++start) {
-                               toclevel = buf.text().getTocLevel(distance(bgn, start));
+                               toclevel = text.getTocLevel(distance(bgn, start));
                                if (toclevel == Layout::NOT_IN_TOC)
                                        continue;
 
@@ -6247,7 +6243,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_OUTLINE_UP: {
                pos_type const opos = cur.pos();
-               outline(OutlineUp, cur, this);
+               outline(OutlineUp, cur, false);
                setCursor(cur, cur.pit(), opos);
                cur.forceBufferUpdate();
                needsUpdate = true;
@@ -6256,7 +6252,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_OUTLINE_DOWN: {
                pos_type const opos = cur.pos();
-               outline(OutlineDown, cur, this);
+               outline(OutlineDown, cur, false);
                setCursor(cur, cur.pit(), opos);
                cur.forceBufferUpdate();
                needsUpdate = true;
@@ -6264,13 +6260,13 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_OUTLINE_IN:
-               outline(OutlineIn, cur, this);
+               outline(OutlineIn, cur, cmd.getArg(0) == "local");
                cur.forceBufferUpdate();
                needsUpdate = true;
                break;
 
        case LFUN_OUTLINE_OUT:
-               outline(OutlineOut, cur, this);
+               outline(OutlineOut, cur, cmd.getArg(0) == "local");
                cur.forceBufferUpdate();
                needsUpdate = true;
                break;
@@ -6548,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();
@@ -6879,9 +6877,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_OUTLINE_DOWN:
        case LFUN_OUTLINE_IN:
        case LFUN_OUTLINE_OUT:
-               // FIXME: LyX is not ready for outlining within inset.
-               enable = isMainText()
-                       && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
+               enable = cur.text()->getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
                break;
 
        case LFUN_NEWLINE_INSERT: