]> git.lyx.org Git - lyx.git/blobdiff - src/Cursor.cpp
Fix crash on OS-switch of dark/light mode (#12786)
[lyx.git] / src / Cursor.cpp
index 0d68c8680787e7306799d2a02bf73655a6def302..7afefcf372e3b7cd26558c69af60f8dd28ecf55d 100644 (file)
@@ -27,7 +27,6 @@
 #include "LyXAction.h"
 #include "LyXRC.h"
 #include "Paragraph.h"
-#include "ParIterator.h"
 #include "Row.h"
 #include "texstream.h"
 #include "Text.h"
 
 #include "support/debug.h"
 #include "support/docstream.h"
-#include "support/ExceptionMessage.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 
+#include "insets/InsetLayout.h"
 #include "insets/InsetTabular.h"
-#include "insets/InsetText.h"
 
 #include "mathed/InsetMath.h"
 #include "mathed/InsetMathBrace.h"
@@ -324,6 +322,7 @@ DocIterator CursorData::selectionEnd() const
        if (di.depth() > depth()) {
                di.resize(depth());
                ++di.pos();
+               di.boundary(true);
        }
        return di;
 }
@@ -505,7 +504,7 @@ void CursorData::clearSelection()
 }
 
 
-int CursorData::countInsetsInSelection(InsetCode const & inset_code)
+int CursorData::countInsetsInSelection(InsetCode const & inset_code) const
 {
        if (!selection_)
                return 0;
@@ -531,7 +530,7 @@ int CursorData::countInsetsInSelection(InsetCode const & inset_code)
 }
 
 
-bool CursorData::insetInSelection(InsetCode const & inset_code)
+bool CursorData::insetInSelection(InsetCode const & inset_code) const
 {
        if (!selection_)
                return false;
@@ -640,9 +639,9 @@ void CursorData::recordUndo(UndoKind kind) const
 }
 
 
-void CursorData::recordUndoInset(Inset const * in) const
+void CursorData::recordUndoInset(Inset const * inset) const
 {
-       buffer()->undo().recordUndoInset(*this, in);
+       buffer()->undo().recordUndoInset(*this, inset);
 }
 
 
@@ -672,7 +671,7 @@ void CursorData::recordUndoSelection() const
 }
 
 
-int CursorData::currentMode()
+int CursorData::currentMode() const
 {
        LASSERT(!empty(), return Inset::UNDECIDED_MODE);
        for (int i = depth() - 1; i >= 0; --i) {
@@ -900,19 +899,19 @@ void Cursor::pop()
 }
 
 
-void Cursor::push(Inset & p)
+void Cursor::push(Inset & inset)
 {
-       push_back(CursorSlice(p));
-       p.setBuffer(*buffer());
+       push_back(CursorSlice(inset));
+       inset.setBuffer(*buffer());
 }
 
 
-void Cursor::pushBackward(Inset & p)
+void Cursor::pushBackward(Inset & inset)
 {
        LASSERT(!empty(), return);
        //lyxerr << "Entering inset " << t << " front" << endl;
-       push(p);
-       p.idxFirst(*this);
+       push(inset);
+       inset.idxFirst(*this);
 }
 
 
@@ -1382,19 +1381,19 @@ void Cursor::updateTextTargetOffset()
 }
 
 
-bool Cursor::selHandle(bool sel)
+bool Cursor::selHandle(bool selecting)
 {
        //lyxerr << "Cursor::selHandle" << endl;
        if (mark())
-               sel = true;
-       if (sel == selection())
+               selecting = true;
+       if (selecting == selection())
                return false;
 
-       if (!sel)
+       if (!selecting)
                cap::saveSelection(*this);
 
        resetAnchor();
-       selection(sel);
+       selection(selecting);
        return true;
 }
 
@@ -1435,10 +1434,7 @@ bool Cursor::atFirstOrLastRow(bool up)
 ///////////////////////////////////////////////////////////////////
 
 #include "mathed/InsetMathChar.h"
-#include "mathed/InsetMathGrid.h"
-#include "mathed/InsetMathScript.h"
 #include "mathed/InsetMathUnknown.h"
-#include "mathed/MathFactory.h"
 #include "mathed/MathStream.h"
 #include "mathed/MathSupport.h"
 
@@ -1599,9 +1595,20 @@ bool Cursor::backspace(bool const force)
                        // [|], can not delete from inside
                        return false;
                } else {
-                       if (inMathed())
-                               pullArg();
-                       else
+                       if (inMathed()) {
+                               switch (inset().asInsetMath()->getType()) {
+                               case hullEqnArray:
+                               case hullAlign:
+                               case hullFlAlign: {
+                                       FuncRequest cmd(LFUN_CHAR_BACKWARD);
+                                       this->dispatch(cmd);
+                                       break;
+                               }
+                               default:
+                                       pullArg();
+                                       break;
+                               }
+                       } else
                                popBackward();
                        return true;
                }
@@ -1706,8 +1713,11 @@ void Cursor::handleNest(MathAtom const & a)
 {
        idx_type const idx = a.nucleus()->asNestInset()->firstIdx();
        //lyxerr << "Cursor::handleNest: " << idx << endl;
+       InsetMath const * im = selectionBegin().inset().asInsetMath();
+       Parse::flags const f = im && im->currentMode() != InsetMath::MATH_MODE
+               ? Parse::TEXTMODE : Parse::NORMAL;
        MathAtom t = a;
-       asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(idx));
+       asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(idx), f);
        insert(t);
        editInsertedInset();
 }
@@ -1884,7 +1894,7 @@ void Cursor::normalize()
                       << " in atom: '";
                odocstringstream os;
                otexrowstream ots(os);
-               WriteStream wi(ots, false, true, WriteStream::wsDefault);
+               TeXMathStream wi(ots, false, true, TeXMathStream::wsDefault);
                inset().asInsetMath()->write(wi);
                lyxerr << to_utf8(os.str()) << endl;
                pos() = lastpos();
@@ -2031,20 +2041,39 @@ bool Cursor::mathForward(bool word)
                                        posForward();
                                while (pos() < lastpos() && mc == nextMath().mathClass());
                } else if (openable(nextAtom())) {
+                       InsetMathScript const * n = nextMath().asScriptInset();
+                       bool to_brace_deco = n && !n->nuc().empty()
+                               && n->nuc().back()->lyxCode() == MATH_DECORATION_CODE
+                               && n->nuc().back()->mathClass() == MC_OP;
                        // single step: try to enter the next inset
                        pushBackward(nextMath());
                        inset().idxFirst(*this);
+                       // Make sure the cursor moves directly to an
+                       // \overbrace or \underbrace inset (bug 2264)
+                       if (to_brace_deco) {
+                               pushBackward(nextMath());
+                               inset().idxFirst(*this);
+                       }
                } else
                        posForward();
                return true;
        }
        if (inset().idxForward(*this))
                return true;
+       InsetMath const * m = inset().asInsetMath();
+       bool from_brace_deco = m
+               && m->lyxCode() == MATH_DECORATION_CODE
+               && m->mathClass() == MC_OP;
        // try to pop forwards --- but don't pop out of math! leave that to
        // the FINISH lfuns
        int s = depth() - 2;
-       if (s >= 0 && operator[](s).inset().asInsetMath())
-               return popForward();
+       if (s >= 0 && operator[](s).inset().asInsetMath() && popForward()) {
+               // Make sure the cursor moves directly to an
+               // \overbrace or \underbrace inset (bug 2264)
+               bool to_script = inset().asInsetMath()
+                       && inset().asInsetMath()->asScriptInset();
+               return from_brace_deco && to_script ? mathForward(word) : true;
+       }
        return false;
 }
 
@@ -2066,21 +2095,41 @@ bool Cursor::mathBackward(bool word)
                                while (pos() > 0 && mc == prevMath().mathClass());
                        }
                } else if (openable(prevAtom())) {
+                       InsetMathScript const * p = prevMath().asScriptInset();
+                       bool to_brace_deco = p && !p->nuc().empty()
+                               && p->nuc().back()->lyxCode() == MATH_DECORATION_CODE
+                               && p->nuc().back()->mathClass() == MC_OP;
                        // single step: try to enter the preceding inset
                        posBackward();
                        push(nextMath());
                        inset().idxLast(*this);
+                       // Make sure the cursor moves directly to an
+                       // \overbrace or \underbrace inset (bug 2264)
+                       if (to_brace_deco) {
+                               posBackward();
+                               push(nextMath());
+                               inset().idxLast(*this);
+                       }
                } else
                        posBackward();
                return true;
        }
        if (inset().idxBackward(*this))
                return true;
+       InsetMath const * m = inset().asInsetMath();
+       bool from_brace_deco = m
+               && m->lyxCode() == MATH_DECORATION_CODE
+               && m->mathClass() == MC_OP;
        // try to pop backwards --- but don't pop out of math! leave that to
        // the FINISH lfuns
        int s = depth() - 2;
-       if (s >= 0 && operator[](s).inset().asInsetMath())
-               return popBackward();
+       if (s >= 0 && operator[](s).inset().asInsetMath() && popBackward()) {
+               // Make sure the cursor moves directly to an
+               // \overbrace or \underbrace inset (bug 2264)
+               bool to_script = inset().asInsetMath()
+                       && inset().asInsetMath()->asScriptInset();
+               return from_brace_deco && to_script ? mathBackward(word) : true;
+       }
        return false;
 }
 
@@ -2405,7 +2454,9 @@ bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
 
 void Cursor::setLanguageFromInput()
 {
-       if (!lyxrc.respect_os_kbd_language)
+       if (!lyxrc.respect_os_kbd_language
+           || !inTexted()
+           || paragraph().isPassThru())
                return;
        string const & code = theApp()->inputLanguageCode();
        Language const * lang = languages.getFromCode(code, buffer()->getLanguages());
@@ -2463,11 +2514,20 @@ void Cursor::setCurrentFont()
                real_current_font.setLanguage(lang);
                real_current_font.fontInfo().setNumber(FONT_OFF);
        }
+
+       // No language in pass thru situations
+       if (cs.paragraph().isPassThru()) {
+               current_font.setLanguage(latex_language);
+               real_current_font.setLanguage(latex_language);
+       }
 }
 
 
 void Cursor::checkBufferStructure()
 {
+       if (buffer()->isInternal())
+               return;
+
        Buffer const * master = buffer()->masterBuffer();
        master->tocBackend().updateItem(*this);
        if (master != buffer() && !master->hasGuiDelegate())