]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathNest.cpp
listerrors.lyx : Update a link.
[lyx.git] / src / mathed / InsetMathNest.cpp
index 21b16a7258f9d8b640a2ca9a329e59a42e24b993..78097f555445236a72d5b1958714b00bf9f9a174 100644 (file)
@@ -371,6 +371,7 @@ MathData InsetMathNest::glue() const
 
 void InsetMathNest::write(WriteStream & os) const
 {
+       MathEnsurer ensurer(os, currentMode() == MATH_MODE);
        ModeSpecifier specifier(os, currentMode(), lockedMode());
        docstring const latex_name = name();
        os << '\\' << latex_name;
@@ -394,17 +395,27 @@ void InsetMathNest::normalize(NormalStream & os) const
 }
 
 
-int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
+void InsetMathNest::latex(otexstream & os, OutputParams const & runparams) const
 {
-       WriteStream wi(os, runparams.moving_arg, true,
+       WriteStream wi(os.os(), runparams.moving_arg, true,
                       runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault,
                       runparams.encoding);
+       wi.canBreakLine(os.canBreakLine());
        write(wi);
-       return wi.line();
+       os.canBreakLine(wi.canBreakLine());
+
+       int lf = wi.line();
+       if (lf > 0 && runparams.lastid != -1) {
+               --lf;
+               os.texrow().newline();
+               os.texrow().start(runparams.lastid, runparams.lastpos);
+       }
+       os.texrow().newlines(lf);
 }
 
 
 bool InsetMathNest::setMouseHover(BufferView const * bv, bool mouse_hover)
+       const
 {
        mouse_hover_[bv] = mouse_hover;
        return true;
@@ -574,8 +585,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                cur.niceInsert(topaste, parseflg, false);
                cur.clearSelection(); // bug 393
-               // FIXME audit setBuffer/updateBuffer calls
-               cur.buffer()->updateBuffer();
+               cur.forceBufferUpdate();
                cur.finishUndo();
                break;
        }
@@ -587,8 +597,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                // Prevent stale position >= size crash
                // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
                cur.normalize();
-               // FIXME audit setBuffer/updateBuffer calls
-               cur.buffer()->updateBuffer();
+               cur.forceBufferUpdate();
                break;
 
        case LFUN_COPY:
@@ -623,7 +632,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_CHAR_LEFT:
        case LFUN_CHAR_BACKWARD:
        case LFUN_CHAR_FORWARD:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_CHAR_RIGHT_SELECT:
        case LFUN_CHAR_LEFT_SELECT:
        case LFUN_CHAR_BACKWARD_SELECT:
@@ -683,7 +692,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_DOWN:
        case LFUN_UP:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_DOWN_SELECT:
        case LFUN_UP_SELECT: {
                // close active macro
@@ -726,7 +735,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_PARAGRAPH_UP:
        case LFUN_PARAGRAPH_DOWN:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_PARAGRAPH_UP_SELECT:
        case LFUN_PARAGRAPH_DOWN_SELECT:
                break;
@@ -734,7 +743,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_LINE_BEGIN:
        case LFUN_WORD_BACKWARD:
        case LFUN_WORD_LEFT:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_LINE_BEGIN_SELECT:
        case LFUN_WORD_BACKWARD_SELECT:
        case LFUN_WORD_LEFT_SELECT:
@@ -759,7 +768,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_WORD_FORWARD:
        case LFUN_WORD_RIGHT:
        case LFUN_LINE_END:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_WORD_FORWARD_SELECT:
        case LFUN_WORD_RIGHT_SELECT:
        case LFUN_LINE_END_SELECT:
@@ -783,12 +792,12 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_CELL_FORWARD:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
                cur.inset().idxNext(cur);
                break;
 
        case LFUN_CELL_BACKWARD:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
                cur.inset().idxPrev(cur);
                break;
 
@@ -998,8 +1007,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.posBackward();
                cur.pushBackward(*cur.nextInset());
                cur.niceInsert(save_selection);
-               // FIXME audit setBuffer/updateBuffer calls
-               cur.buffer()->updateBuffer();
+               cur.forceBufferUpdate();
 #else
                if (currentMode() == Inset::TEXT_MODE) {
                        cur.recordUndoSelection();
@@ -1014,10 +1022,13 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_REGEXP_MODE: {
-               InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
-               if (i && i->getType() == hullRegexp) {
-                       cur.message(_("Already in regular expression mode"));
-                       break;
+               InsetMath * im = cur.inset().asInsetMath();
+               if (im) {
+                       InsetMathHull * i = im->asHullInset();          
+                       if (i && i->getType() == hullRegexp) {
+                               cur.message(_("Already in regular expression mode"));
+                               break;
+                       }
                }
                cur.macroModeClose();
                docstring const save_selection = grabAndEraseSelection(cur);
@@ -1109,20 +1120,48 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                        if (have_l)
                                cur.insert(MathAtom(new InsetMathBig(lname,
                                                                ldelim)));
-                       cur.niceInsert(selection);
-                       if (have_r)
+                       // first insert the right delimiter and then go back
+                       // and re-insert the selection (bug 7088)
+                       if (have_r) {
                                cur.insert(MathAtom(new InsetMathBig(rname,
                                                                rdelim)));
+                               cur.posBackward();
+                       }
+                       cur.niceInsert(selection);
                }
                // Don't call cur.undispatched() if we did nothing, this would
                // lead to infinite recursion via Text::dispatch().
                break;
        }
 
-       case LFUN_SPACE_INSERT:
+       case LFUN_SPACE_INSERT: {
                cur.recordUndoSelection();
-               cur.insert(MathAtom(new InsetMathSpace));
+               string const name = cmd.getArg(0);
+               if (name == "normal")
+                       cur.insert(MathAtom(new InsetMathSpace(" ", "")));
+               else if (name == "protected")
+                       cur.insert(MathAtom(new InsetMathSpace("~", "")));
+               else if (name == "thin" || name == "med" || name == "thick")
+                       cur.insert(MathAtom(new InsetMathSpace(name + "space", "")));
+               else if (name == "hfill*")
+                       cur.insert(MathAtom(new InsetMathSpace("hspace*{\\fill}", "")));
+               else if (name == "quad" || name == "qquad" ||
+                        name == "enspace" || name == "enskip" ||
+                        name == "negthinspace" || name == "negmedspace" ||
+                        name == "negthickspace" || name == "hfill")
+                       cur.insert(MathAtom(new InsetMathSpace(name, "")));
+               else if (name == "hspace" || name == "hspace*") {
+                       string const len = cmd.getArg(1);
+                       if (len.empty() || !isValidLength(len)) {
+                               lyxerr << "LyX function 'space-insert " << name << "' "
+                                         "needs a valid length argument." << endl;
+                               break;
+                       }
+                       cur.insert(MathAtom(new InsetMathSpace(name, len)));
+               } else
+                       cur.insert(MathAtom(new InsetMathSpace));
                break;
+       }
 
        case LFUN_MATH_SPACE:
                cur.recordUndoSelection();
@@ -1193,14 +1232,30 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                                cur.niceInsert(cmd.argument());
                }
                break;
+       }
+
+       case LFUN_UNICODE_INSERT: {
+               if (cmd.argument().empty())
+                       break;
+               docstring hexstring = cmd.argument();
+               if (isHex(hexstring)) {
+                       char_type c = hexToInt(hexstring);
+                       if (c >= 32 && c < 0x10ffff) {
+                               docstring s = docstring(1, c);
+                               FuncCode code = currentMode() == MATH_MODE ?
+                                       LFUN_MATH_INSERT : LFUN_SELF_INSERT;
+                               lyx::dispatch(FuncRequest(code, s));
+                       }
                }
+               break;
+       }
 
        case LFUN_DIALOG_SHOW_NEW_INSET: {
                docstring const & name = cmd.argument();
                string data;
                if (name == "ref") {
                        InsetMathRef tmp(buffer_, name);
-                       data = tmp.createDialogStr(to_utf8(name));
+                       data = tmp.createDialogStr();
                        cur.bv().showDialog(to_utf8(name), data);
                } else if (name == "mathspace") {
                        cur.bv().showDialog(to_utf8(name));
@@ -1213,8 +1268,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
                        cur.recordUndoSelection();
                        cur.insert(ar);
-                       // FIXME audit setBuffer/updateBuffer calls
-                       cur.buffer()->updateBuffer();
+                       cur.forceBufferUpdate();                        
                } else
                        cur.undispatched();
                break;
@@ -1336,6 +1390,12 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                break;
        }
 
+       case LFUN_MATH_MODE:
+               // forbid "math-mode on" in math mode to prevent irritating
+               // behaviour of menu entries (bug 6709)
+               flag.setEnabled(currentMode() == TEXT_MODE || arg != "on");
+               break;
+
        case LFUN_MATH_INSERT:
                flag.setEnabled(currentMode() != TEXT_MODE);
                break;
@@ -1354,6 +1414,14 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                break;
        }
 
+       case LFUN_DIALOG_SHOW_NEW_INSET: {
+               docstring const & name = cmd.argument();
+               if (name == "space")
+                       flag.setEnabled(false);
+               break;
+       }
+
+
        case LFUN_MATH_DELIM:
        case LFUN_MATH_BIGDELIM:
                // Don't do this with multi-cell selections
@@ -1369,10 +1437,18 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
        }
 
        case LFUN_SPECIALCHAR_INSERT:
+       case LFUN_SCRIPT_INSERT:
                // FIXME: These would probably make sense in math-text mode
                flag.setEnabled(false);
                break;
 
+       case LFUN_SPACE_INSERT: {
+               docstring const & name = cmd.argument();
+               if (name == "visible")
+                       flag.setEnabled(false);
+               break;
+       }
+
        case LFUN_INSET_DISSOLVE:
                flag.setEnabled(!asHullInset());
                break;
@@ -1428,6 +1504,15 @@ void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
 {
        //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
        BufferView & bv = cur.bv();
+       if (cmd.button() == mouse_button::button3) {
+               // Don't do anything if we right-click a
+               // selection, a context menu will popup.
+               if (bv.cursor().selection() && cur >= bv.cursor().selectionBegin()
+                     && cur < bv.cursor().selectionEnd()) {
+                       cur.noScreenUpdate();
+                       return;
+               }
+       }
        bool do_selection = cmd.button() == mouse_button::button1
                && cmd.argument() == "region-select";
        bv.mouseSetCursor(cur, do_selection);
@@ -1441,8 +1526,8 @@ void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
                // Update::FitCursor: adjust the screen to the cursor
                //                    position if needed
                // cur.result().update(): don't overwrite previously set flags.
-               cur.updateFlags(Update::Decoration | Update::FitCursor
-                               | cur.result().update());
+               cur.screenUpdateFlags(Update::Decoration | Update::FitCursor
+                               | cur.result().screenUpdate());
        } else if (cmd.button() == mouse_button::button2) {
                if (cap::selection()) {
                        // See comment in Text::dispatch why we do this
@@ -1480,7 +1565,7 @@ void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
 
        if (cmd.button() == mouse_button::button1) {
                if (!cur.selection())
-                       cur.noUpdate();
+                       cur.noScreenUpdate();
                else {
                        Cursor & bvcur = cur.bv().cursor();
                        bvcur.setSelection(true);
@@ -1622,7 +1707,10 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                bool reduced = cap::reduceSelectionToOneCell(cur);
                if (reduced || !cur.selection()) {
                        docstring const safe = cap::grabAndEraseSelection(cur);
-                       cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
+                       if (!cur.inRegexped())
+                               cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
+                       else
+                               cur.niceInsert(createInsetMath("backslash", buf));
                }
                return true;
        }
@@ -1647,7 +1735,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                                // visual box corners that define the inset. If we know for
                                // sure that we stay within the same cell we can optimize for
                                // that using:
-                               //cur.updateFlags(Update::SinglePar | Update::FitCursor);
+                               //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
                        }
                        return true;
                }
@@ -1657,7 +1745,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                        // visual box corners that define the inset. If we know for
                        // sure that we stay within the same cell we can optimize for
                        // that using:
-                       //cur.updateFlags(Update::SinglePar | Update::FitCursor);
+                       //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
                        return true;
                }
 
@@ -1666,7 +1754,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                        // visual box corners that define the inset. If we know for
                        // sure that we stay within the same cell we can optimize for
                        // that using:
-                       //cur.updateFlags(Update::FitCursor);
+                       //cur.screenUpdateFlags(Update::FitCursor);
                        return true;
                }
 
@@ -1675,7 +1763,29 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
        }
 
        // These should be treated differently when not in text mode:
-       if (currentMode() != InsetMath::TEXT_MODE) {
+       if (cur.inRegexped()) {
+               switch (c) {
+               case '\\':
+                       cur.niceInsert(createInsetMath("backslash", buf));
+                       break;
+               case '^':
+                       cur.niceInsert(createInsetMath("mathcircumflex", buf));
+                       break;
+               case '{':
+               case '}':
+               case '#':
+               case '%':
+               case '_':
+                       cur.niceInsert(createInsetMath(docstring(1, c), buf));
+                       break;
+               case '~':
+                       cur.niceInsert(createInsetMath("sim", buf));
+                       break;
+               default:
+                       cur.insert(c);
+               }
+               return true;
+       } else if (currentMode() != InsetMath::TEXT_MODE) {
                if (c == '_') {
                        script(cur, false, save_selection);
                        return true;
@@ -1982,6 +2092,7 @@ MathCompletionList::MathCompletionList(Cursor const & cur)
        globals.push_back(from_ascii("\\cases"));
        globals.push_back(from_ascii("\\substack"));
        globals.push_back(from_ascii("\\xymatrix"));
+       globals.push_back(from_ascii("\\Diagram"));
        globals.push_back(from_ascii("\\subarray"));
        globals.push_back(from_ascii("\\array"));
        globals.push_back(from_ascii("\\sqrt"));