]> git.lyx.org Git - lyx.git/blobdiff - src/Text3.cpp
Allow dissolution of insets inside mathed
[lyx.git] / src / Text3.cpp
index 1c0ceacfbbdf7c4f65c1916a79d231c6cd21ea39..3b9ea74184e125577b580447b8ed048750baeb98 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "Text.h"
 
+#include "Bidi.h"
 #include "BranchList.h"
 #include "FloatList.h"
 #include "FuncStatus.h"
@@ -24,7 +25,6 @@
 #include "buffer_funcs.h"
 #include "BufferParams.h"
 #include "BufferView.h"
-#include "bufferview_funcs.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "debug.h"
@@ -35,6 +35,7 @@
 #include "gettext.h"
 #include "Intl.h"
 #include "Language.h"
+#include "Layout.h"
 #include "LyXAction.h"
 #include "LyXFunc.h"
 #include "Lexer.h"
@@ -85,116 +86,92 @@ using cap::replaceSelection;
 using support::isStrUnsignedInt;
 using support::token;
 
-namespace frontend {
-extern docstring current_layout;
-}
-
-namespace {
-
-       // globals...
-       Font freefont(Font::ALL_IGNORE);
-       bool toggleall = false;
+// globals...
+static Font freefont(Font::ALL_IGNORE);
+static bool toggleall = false;
 
-       void toggleAndShow(Cursor & cur, Text * text,
-               Font const & font, bool toggleall = true)
-       {
-               text->toggleFree(cur, font, toggleall);
-
-               if (font.language() != ignore_language ||
-                               font.number() != Font::IGNORE) {
-                       TextMetrics const & tm = cur.bv().textMetrics(text);
-                       if (cur.boundary() != tm.isRTLBoundary(cur.pit(),
-                                               cur.pos(), cur.real_current_font))
-                               text->setCursor(cur, cur.pit(), cur.pos(),
-                                               false, !cur.boundary());
-               }
+static void toggleAndShow(Cursor & cur, Text * text,
+       Font const & font, bool toggleall = true)
+{
+       text->toggleFree(cur, font, toggleall);
+
+       if (font.language() != ignore_language ||
+                       font.number() != Font::IGNORE) {
+               TextMetrics const & tm = cur.bv().textMetrics(text);
+               if (cur.boundary() != tm.isRTLBoundary(cur.pit(),
+                                                                                                               cur.pos(), cur.real_current_font))
+                       text->setCursor(cur, cur.pit(), cur.pos(),
+                                                                                       false, !cur.boundary());
        }
+}
 
 
-       void moveCursor(Cursor & cur, bool selecting)
-       {
-               if (selecting || cur.mark())
-                       cur.setSelection();
-       }
+static void moveCursor(Cursor & cur, bool selecting)
+{
+       if (selecting || cur.mark())
+               cur.setSelection();
+}
 
 
-       void finishChange(Cursor & cur, bool selecting)
-       {
-               finishUndo();
-               moveCursor(cur, selecting);
-       }
+static void finishChange(Cursor & cur, bool selecting)
+{
+       finishUndo();
+       moveCursor(cur, selecting);
+}
 
 
-       void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
-       {
-               recordUndo(cur);
-               docstring sel = cur.selectionAsString(false);
+static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
+{
+       recordUndo(cur);
+       docstring sel = cur.selectionAsString(false);
 
-               // It may happen that sel is empty but there is a selection
-               replaceSelection(cur);
+       // It may happen that sel is empty but there is a selection
+       replaceSelection(cur);
 
-               if (sel.empty()) {
+       if (sel.empty()) {
 #ifdef ENABLE_ASSERTIONS
-                       const int old_pos = cur.pos();
+               const int old_pos = cur.pos();
 #endif
-                       cur.insert(new InsetMathHull(hullSimple));
-                       BOOST_ASSERT(old_pos == cur.pos());
-                       cur.nextInset()->edit(cur, true);
-                       // don't do that also for LFUN_MATH_MODE
-                       // unless you want end up with always changing
-                       // to mathrm when opening an inlined inset --
-                       // I really hate "LyXfunc overloading"...
-                       if (display)
-                               cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
-                       // Avoid an unnecessary undo step if cmd.argument
-                       // is empty
-                       if (!cmd.argument().empty())
-                               cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
-                                                        cmd.argument()));
+               cur.insert(new InsetMathHull(hullSimple));
+               BOOST_ASSERT(old_pos == cur.pos());
+               cur.nextInset()->edit(cur, true);
+               // don't do that also for LFUN_MATH_MODE
+               // unless you want end up with always changing
+               // to mathrm when opening an inlined inset --
+               // I really hate "LyXfunc overloading"...
+               if (display)
+                       cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
+               // Avoid an unnecessary undo step if cmd.argument
+               // is empty
+               if (!cmd.argument().empty())
+                       cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
+                                                cmd.argument()));
+       } else {
+               // create a macro if we see "\\newcommand"
+               // somewhere, and an ordinary formula
+               // otherwise
+               if (sel.find(from_ascii("\\newcommand")) == string::npos
+                               && sel.find(from_ascii("\\def")) == string::npos)
+               {
+                       InsetMathHull * formula = new InsetMathHull;
+                       istringstream is(to_utf8(sel));
+                       Lexer lex(0, 0);
+                       lex.setStream(is);
+                       formula->read(cur.buffer(), lex);
+                       if (formula->getType() == hullNone)
+                               // Don't create pseudo formulas if
+                               // delimiters are left out
+                               formula->mutate(hullSimple);
+                       cur.insert(formula);
                } else {
-                       // create a macro if we see "\\newcommand"
-                       // somewhere, and an ordinary formula
-                       // otherwise
-                       if (sel.find(from_ascii("\\newcommand")) == string::npos
-                           && sel.find(from_ascii("\\def")) == string::npos)
-                       {
-                               InsetMathHull * formula = new InsetMathHull;
-                               istringstream is(to_utf8(sel));
-                               Lexer lex(0, 0);
-                               lex.setStream(is);
-                               formula->read(cur.buffer(), lex);
-                               if (formula->getType() == hullNone)
-                                       // Don't create pseudo formulas if
-                                       // delimiters are left out
-                                       formula->mutate(hullSimple);
-                               cur.insert(formula);
-                       } else {
-                               cur.insert(new MathMacroTemplate(sel));
-                       }
+                       cur.insert(new MathMacroTemplate(sel));
                }
-               cur.message(from_utf8(N_("Math editor mode")));
        }
-
-} // namespace anon
-
-
-
-namespace bv_funcs {
-
-string const freefont2string()
-{
-       string data;
-       if (font2string(freefont, toggleall, data))
-               return data;
-       return string();
+       cur.message(from_utf8(N_("Math editor mode")));
 }
 
-}
-
-
-namespace {
 
-void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
+static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
 {
        recordUndo(cur);
        cap::replaceSelection(cur);
@@ -203,7 +180,7 @@ void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
 }
 
 
-bool doInsertInset(Cursor & cur, Text * text,
+static bool doInsertInset(Cursor & cur, Text * text,
        FuncRequest const & cmd, bool edit, bool pastesel)
 {
        Inset * inset = createInset(&cur.bv(), cmd);
@@ -234,7 +211,10 @@ bool doInsertInset(Cursor & cur, Text * text,
 }
 
 
-} // anon namespace
+string const freefont2string()
+{
+       return freefont.toString(toggleall);
+}
 
 
 void Text::number(Cursor & cur)
@@ -247,7 +227,7 @@ void Text::number(Cursor & cur)
 
 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
 {
-       return par.isRightToLeftPar(buffer.params());
+       return par.isRTL(buffer.params());
 }
 
 
@@ -440,9 +420,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                needsUpdate |= cursorDownParagraph(cur);
                break;
 
-       case LFUN_SCREEN_UP:
        case LFUN_SCREEN_UP_SELECT:
-               needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_UP_SELECT);
+               needsUpdate |= cur.selHandle(true);
                if (cur.pit() == 0 && cur.textRow().pos() == 0)
                        cur.undispatched();
                else {
@@ -450,9 +429,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                }
                break;
 
-       case LFUN_SCREEN_DOWN:
        case LFUN_SCREEN_DOWN_SELECT:
-               needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_DOWN_SELECT);
+               needsUpdate |= cur.selHandle(true);
                if (cur.pit() == cur.lastpit()
                          && cur.textRow().endpos() == cur.lastpos())
                        cur.undispatched();
@@ -574,13 +552,13 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_BREAK_PARAGRAPH:
                cap::replaceSelection(cur);
-               breakParagraph(cur, 0);
+               breakParagraph(cur, false);
                cur.resetAnchor();
                break;
 
        case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
                cap::replaceSelection(cur);
-               breakParagraph(cur, 1);
+               breakParagraph(cur, true);
                cur.resetAnchor();
                break;
 
@@ -591,7 +569,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                if (cur.pos() == 0)
                        cur.paragraph().params().labelWidthString(docstring());
                else
-                       breakParagraph(cur, 0);
+                       breakParagraph(cur, false);
                cur.resetAnchor();
                break;
        }
@@ -790,6 +768,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                docstring layout = cmd.argument();
                LYXERR(Debug::INFO) << "LFUN_LAYOUT: (arg) " << to_utf8(layout) << endl;
 
+               docstring const old_layout = cur.paragraph().layout()->name();
+
                // Derive layout number from given argument (string)
                // and current buffer's textclass (number)
                TextClass const & tclass = bv->buffer().params().getTextClass();
@@ -810,7 +790,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        break;
                }
 
-               bool change_layout = (frontend::current_layout != layout);
+               bool change_layout = (old_layout != layout);
 
                if (!change_layout && cur.selection() &&
                        cur.selBegin().pit() != cur.selEnd().pit())
@@ -818,7 +798,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        pit_type spit = cur.selBegin().pit();
                        pit_type epit = cur.selEnd().pit() + 1;
                        while (spit != epit) {
-                               if (pars_[spit].layout()->name() != frontend::current_layout) {
+                               if (pars_[spit].layout()->name() != old_layout) {
                                        change_layout = true;
                                        break;
                                }
@@ -826,11 +806,9 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        }
                }
 
-               if (change_layout) {
+               if (change_layout)
                        setLayout(cur, layout);
-                       // inform the GUI that the layout has changed.
-                       bv->layoutChanged(layout);
-               }
+
                break;
        }
 
@@ -1126,7 +1104,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                updateLabels(bv->buffer());
                break;
        case LFUN_NOTE_INSERT:
-       case LFUN_CHARSTYLE_INSERT:
+       case LFUN_FLEX_INSERT:
        case LFUN_BOX_INSERT:
        case LFUN_BRANCH_INSERT:
        case LFUN_BIBITEM_INSERT:
@@ -1202,12 +1180,10 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                recordUndo(cur);
                cur.clearSelection();
                insertInset(cur, inset);
-               inset->edit(cur, true);
                // Show the dialog for the nomenclature entry, since the
                // description entry still needs to be filled in.
                if (cmd.action == LFUN_NOMENCL_INSERT)
-                       InsetCommandMailer("nomenclature",
-                               *reinterpret_cast<InsetCommand *>(inset)).showDialog(&cur.bv());
+                       inset->edit(cur, true);
                cur.posRight();
                break;
        }
@@ -1306,7 +1282,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_FONT_CODE: {
+       case LFUN_FONT_TYPEWRITER: {
                Font font(Font::ALL_IGNORE);
                font.setFamily(Font::TYPEWRITER_FAMILY); // no good
                toggleAndShow(cur, this, font);
@@ -1367,7 +1343,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_FONT_FREE_UPDATE: {
                Font font;
                bool toggle;
-               if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) {
+               if (font.fromString(to_utf8(cmd.argument()), toggle)) {
                        freefont = font;
                        toggleall = toggle;
                        toggleAndShow(cur, this, freefont, toggleall);
@@ -1378,14 +1354,18 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_FINISHED_LEFT:
                LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
-               if (reverseDirectionNeeded(cur))
+               if (reverseDirectionNeeded(cur)) {
                        ++cur.pos();
+                       cur.setCurrentFont();
+               }
                break;
 
        case LFUN_FINISHED_RIGHT:
                LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
-               if (!reverseDirectionNeeded(cur))
+               if (!reverseDirectionNeeded(cur)) {
                        ++cur.pos();
+                       cur.setCurrentFont();
+               }
                break;
 
        case LFUN_LAYOUT_PARAGRAPH: {
@@ -1680,11 +1660,16 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_NOTE_INSERT:
                code = Inset::NOTE_CODE;
                break;
-       case LFUN_CHARSTYLE_INSERT:
-               code = Inset::CHARSTYLE_CODE;
-               if (cur.buffer().params().getTextClass().insetlayouts().empty())
+       case LFUN_FLEX_INSERT: {
+               code = Inset::FLEX_CODE;
+               string s = cmd.getArg(0);
+               InsetLayout il =  cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
+               if (il.lyxtype != "charstyle" &&
+                   il.lyxtype != "custom" &&
+                   il.lyxtype != "element")
                        enable = false;
                break;
+               }
        case LFUN_BOX_INSERT:
                code = Inset::BOX_CODE;
                break;
@@ -1769,7 +1754,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
                flag.setOnOff(font.family() == Font::ROMAN_FAMILY);
                return true;
 
-       case LFUN_FONT_CODE:
+       case LFUN_FONT_TYPEWRITER:
                flag.setOnOff(font.family() == Font::TYPEWRITER_FAMILY);
                return true;
 
@@ -1850,8 +1835,6 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_WORD_SELECT:
        case LFUN_PARAGRAPH_UP:
        case LFUN_PARAGRAPH_DOWN:
-       case LFUN_SCREEN_UP:
-       case LFUN_SCREEN_DOWN:
        case LFUN_LINE_BEGIN:
        case LFUN_LINE_END:
        case LFUN_BREAK_LINE: