]> git.lyx.org Git - lyx.git/blobdiff - src/Text3.cpp
* Text3.cpp (doDispatch): fix the behaviour of word-delete-forward,
[lyx.git] / src / Text3.cpp
index 37baf50626985622d87755d5b9a688c45a4c0505..1b9618654a85f9fdc2e46449c196e463d0d5e59a 100644 (file)
@@ -79,7 +79,6 @@ using cap::cutSelection;
 using cap::pasteFromStack;
 using cap::pasteClipboard;
 using cap::replaceSelection;
-using cap::saveSelection;
 
 using support::isStrUnsignedInt;
 using support::token;
@@ -99,7 +98,6 @@ namespace {
        Font freefont(Font::ALL_IGNORE);
        bool toggleall = false;
 
-
        void toggleAndShow(Cursor & cur, Text * text,
                Font const & font, bool toggleall = true)
        {
@@ -108,13 +106,10 @@ namespace {
                if (font.language() != ignore_language ||
                                font.number() != Font::IGNORE) {
                        Paragraph & par = cur.paragraph();
-                       text->bidi.computeTables(par, cur.buffer(), cur.textRow());
-                       if (cur.boundary() !=
-                                       text->bidi.isBoundary(cur.buffer(), par,
-                                                       cur.pos(),
-                                                       text->real_current_font))
+                       if (cur.boundary() != text->isRTLBoundary(cur.buffer(), par,
+                                               cur.pos(), text->real_current_font))
                                text->setCursor(cur, cur.pit(), cur.pos(),
-                                               false, !cur.boundary());
+                                               false, !cur.boundary());
                }
        }
 
@@ -123,8 +118,6 @@ namespace {
        {
                if (selecting || cur.mark())
                        cur.setSelection();
-               saveSelection(cur);
-               cur.bv().switchKeyMap();
        }
 
 
@@ -244,6 +237,7 @@ namespace {
 
 void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
 {
+       recordUndo(cur);
        cap::replaceSelection(cur);
        cur.insert(new InsetSpecialChar(kind));
        cur.posRight();
@@ -298,6 +292,56 @@ bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
 }
 
 
+bool Text::isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const
+{
+       if (!lyxrc.rtl_support && !sl.text())
+               return false;
+
+       int correction = 0;
+       if (boundary && sl.pos() > 0)
+               correction = -1;
+               
+       Paragraph const & par = getPar(sl.pit());
+       return getFont(buffer, par, sl.pos() + correction).isVisibleRightToLeft();
+}
+
+
+bool Text::isRTLBoundary(Buffer const & buffer, Paragraph const & par,
+                         pos_type pos) const
+{
+       if (!lyxrc.rtl_support)
+               return false;
+
+       // no RTL boundary at line start
+       if (pos == 0)
+               return false;
+
+       bool left = getFont(buffer, par, pos - 1).isVisibleRightToLeft();
+       bool right;
+       if (pos == par.size())
+               right = par.isRightToLeftPar(buffer.params());
+       else
+               right = getFont(buffer, par, pos).isVisibleRightToLeft();
+       return left != right;
+}
+
+
+bool Text::isRTLBoundary(Buffer const & buffer, Paragraph const & par,
+                         pos_type pos, Font const & font) const
+{
+       if (!lyxrc.rtl_support)
+               return false;
+
+       bool left = font.isVisibleRightToLeft();
+       bool right;
+       if (pos == par.size())
+               right = par.isRightToLeftPar(buffer.params());
+       else
+               right = getFont(buffer, par, pos).isVisibleRightToLeft();
+       return left != right;
+}
+
+
 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 {
        LYXERR(Debug::ACTION) << "Text::dispatch: cmd: " << cmd << endl;
@@ -333,7 +377,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                std::swap(pars_[pit], pars_[pit + 1]);
 
                ParIterator begin(cur);
-               // begin.pos() (== cur.pos()) may point beyond the end of the 
+               // begin.pos() (== cur.pos()) may point beyond the end of the
                // paragraph referenced by begin. This would cause a crash
                // in updateLabels()
                begin.pos() = 0;
@@ -352,7 +396,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                std::swap(pars_[pit], pars_[pit - 1]);
 
                ParIterator end = ParIterator(cur);
-               // end.pos() (== cur.pos()) may point beyond the end of the 
+               // end.pos() (== cur.pos()) may point beyond the end of the
                // paragraph referenced by end. This would cause a crash
                // in boost::next()
                end.pos() = 0;
@@ -391,20 +435,26 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_WORD_DELETE_FORWARD:
-               cur.clearSelection();
-               deleteWordForward(cur);
+               if (cur.selection()) {
+                       cutSelection(cur, true, false);
+               } else
+                       deleteWordForward(cur);
                finishChange(cur, false);
                break;
 
        case LFUN_WORD_DELETE_BACKWARD:
-               cur.clearSelection();
-               deleteWordBackward(cur);
+               if (cur.selection()) {
+                       cutSelection(cur, true, false);
+               } else
+                       deleteWordBackward(cur);
                finishChange(cur, false);
                break;
 
        case LFUN_LINE_DELETE:
-               cur.clearSelection();
-               deleteLineForward(cur);
+               if (cur.selection()) {
+                       cutSelection(cur, true, false);
+               } else
+                       deleteLineForward(cur);
                finishChange(cur, false);
                break;
 
@@ -443,8 +493,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
                }
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_CHAR_BACKWARD:
@@ -461,83 +509,64 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
                }
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
-       case LFUN_UP:
        case LFUN_UP_SELECT:
-               //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
-               needsUpdate |= cur.selHandle(cmd.action == LFUN_UP_SELECT);
-
-               needsUpdate |= cursorUp(cur);
-
-               if (!needsUpdate && oldTopSlice == cur.top()
-                         && cur.boundary() == oldBoundary) {
-                       cur.undispatched();
-                       cmd = FuncRequest(LFUN_FINISHED_UP);
-               }
-               if (cur.selection())
-                       saveSelection(cur);
-               break;
-
-       case LFUN_DOWN:
        case LFUN_DOWN_SELECT:
-               //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
-               needsUpdate |= cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
-               needsUpdate |= cursorDown(cur);
-
-               if (!needsUpdate && oldTopSlice == cur.top() &&
-                   cur.boundary() == oldBoundary)
-               {
+       case LFUN_UP:
+       case LFUN_DOWN: {
+               // stop/start the selection
+               bool select = cmd.action == LFUN_DOWN_SELECT ||
+                       cmd.action == LFUN_UP_SELECT;
+               cur.selHandle(select);
+               
+               // move cursor up/down
+               bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
+               bool const successful = cur.upDownInText(up, needsUpdate);
+               if (successful) {
+                       // notify insets which were left and get their update flags 
+                       notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
+                       cur.fixIfBroken();
+                       
+                       // redraw if you leave mathed (for the decorations)
+                       needsUpdate |= cur.beforeDispatchCursor().inMathed();
+               } else
                        cur.undispatched();
-                       cmd = FuncRequest(LFUN_FINISHED_DOWN);
-               }
-               if (cur.selection())
-                       saveSelection(cur);
+               
                break;
+       }
 
        case LFUN_PARAGRAPH_UP:
        case LFUN_PARAGRAPH_UP_SELECT:
                needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
                needsUpdate |= cursorUpParagraph(cur);
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_PARAGRAPH_DOWN:
        case LFUN_PARAGRAPH_DOWN_SELECT:
                needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
                needsUpdate |= cursorDownParagraph(cur);
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_SCREEN_UP:
        case LFUN_SCREEN_UP_SELECT:
                needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_UP_SELECT);
-               if (cur.pit() == 0 && cur.textRow().pos() == 0) {
+               if (cur.pit() == 0 && cur.textRow().pos() == 0)
                        cur.undispatched();
-                       cmd = FuncRequest(LFUN_FINISHED_UP);
-               } else {
+               else {
                        cursorPrevious(cur);
                }
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_SCREEN_DOWN:
        case LFUN_SCREEN_DOWN_SELECT:
                needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_DOWN_SELECT);
                if (cur.pit() == cur.lastpit()
-                         && cur.textRow().endpos() == cur.lastpos()) {
+                         && cur.textRow().endpos() == cur.lastpos())
                        cur.undispatched();
-                       cmd = FuncRequest(LFUN_FINISHED_DOWN);
-               } else {
+               else {
                        cursorNext(cur);
                }
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_LINE_BEGIN:
@@ -550,8 +579,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_LINE_END_SELECT:
                needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
                needsUpdate |= cursorEnd(cur);
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_WORD_FORWARD:
@@ -561,8 +588,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        needsUpdate |= cursorLeftOneWord(cur);
                else
                        needsUpdate |= cursorRightOneWord(cur);
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_WORD_BACKWARD:
@@ -572,8 +597,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        needsUpdate |= cursorRightOneWord(cur);
                else
                        needsUpdate |= cursorLeftOneWord(cur);
-               if (cur.selection())
-                       saveSelection(cur);
                break;
 
        case LFUN_WORD_SELECT: {
@@ -643,7 +666,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        cutSelection(cur, true, false);
                        singleParUpdate = false;
                }
-               bv->switchKeyMap();
                break;
 
        case LFUN_DELETE_BACKWARD_SKIP:
@@ -664,14 +686,12 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                cap::replaceSelection(cur);
                breakParagraph(cur, 0);
                cur.resetAnchor();
-               bv->switchKeyMap();
                break;
 
        case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
                cap::replaceSelection(cur);
                breakParagraph(cur, 1);
                cur.resetAnchor();
-               bv->switchKeyMap();
                break;
 
        case LFUN_BREAK_PARAGRAPH_SKIP: {
@@ -683,7 +703,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                else
                        breakParagraph(cur, 0);
                cur.resetAnchor();
-               bv->switchKeyMap();
                break;
        }
 
@@ -733,7 +752,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        // FIXME (Abdel 01/02/2006):
                        // What follows would be a partial fix for bug 2154:
                        //   http://bugzilla.lyx.org/show_bug.cgi?id=2154
-                       // This automatically put the label inset _after_ a 
+                       // This automatically put the label inset _after_ a
                        // numbered section. It should be possible to extend the mechanism
                        // to any kind of LateX environement.
                        // The correct way to fix that bug would be at LateX generation.
@@ -809,7 +828,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_CHARS_TRANSPOSE:
-               charsTranspose(cur);
+               charsTranspose(cur);
                break;
 
        case LFUN_PASTE:
@@ -826,7 +845,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                }
                bv->buffer()->errors("Paste");
                cur.clearSelection(); // bug 393
-               bv->switchKeyMap();
                finishUndo();
                break;
 
@@ -913,11 +931,9 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                }
 
                if (change_layout) {
-                       current_layout = layout;
                        setLayout(cur, layout);
                        // inform the GUI that the layout has changed.
                        bv->layoutChanged(layout);
-                       bv->switchKeyMap();
                }
                break;
        }
@@ -925,13 +941,13 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_CLIPBOARD_PASTE:
                cur.clearSelection();
                pasteClipboard(cur, bv->buffer()->errorList("Paste"),
-                              cmd.argument() == "paragraph");
+                              cmd.argument() == "paragraph");
                bv->buffer()->errors("Paste");
                break;
 
        case LFUN_PRIMARY_SELECTION_PASTE:
                pasteString(cur, theSelection().get(),
-                           cmd.argument() == "paragraph");
+                           cmd.argument() == "paragraph");
                break;
 
        case LFUN_UNICODE_INSERT: {
@@ -940,7 +956,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                docstring hexstring = cmd.argument();
                if (lyx::support::isHex(hexstring)) {
                        char_type c = lyx::support::hexToInt(hexstring);
-                       if (c > 32 && c < 0x10ffff) {
+                       if (c >= 32 && c < 0x10ffff) {
                                lyxerr << "Inserting c: " << c << endl;
                                docstring s = docstring(1, c);
                                lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
@@ -948,7 +964,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                }
                break;
        }
-               
+
        case LFUN_QUOTE_INSERT: {
                Paragraph & par = cur.paragraph();
                pos_type pos = cur.pos();
@@ -1001,7 +1017,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        cursorEnd(cur);
                        cur.setSelection();
                        bv->cursor() = cur;
-                       saveSelection(cur);
                }
                break;
 
@@ -1031,9 +1046,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        paste_internally = true;
                }
 
-               // we have to update after dePM triggered
-               bool update = bv->mouseSetCursor(cur);
-
                // Insert primary selection with middle mouse
                // if there is a local selection in the current buffer,
                // insert this
@@ -1042,12 +1054,17 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                                cap::pasteSelection(cur, bv->buffer()->errorList("Paste"));
                                bv->buffer()->errors("Paste");
                                cur.clearSelection(); // bug 393
-                               bv->switchKeyMap();
+                               bv->buffer()->markDirty();
                                finishUndo();
-                       } else
+                       } else {
+                               bv->mouseSetCursor(cur);
                                lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
+                       }
                }
 
+               // we have to update after dePM triggered
+               bool update = bv->mouseSetCursor(cur);
+
                if (!update && cmd.button() == mouse_button::button1) {
                        needsUpdate = false;
                        cur.noUpdate();
@@ -1070,7 +1087,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        int const y = std::max(0, std::min(wh - 1, cmd.y));
 
                        setCursorFromCoordinates(cur, cmd.x, y);
-                       cur.x_target() = cmd.x;
+                       cur.setTargetX(cmd.x);
                        if (cmd.y >= wh)
                                lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
                        else if (cmd.y < 0)
@@ -1111,13 +1128,11 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                                // but bvcur is current mouse position
                                Cursor & bvcur = cur.bv().cursor();
                                bvcur.selection() = true;
-                               saveSelection(bvcur);
                        }
                        needsUpdate = false;
                        cur.noUpdate();
                }
 
-               bv->switchKeyMap();
                break;
        }
 
@@ -1131,9 +1146,17 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                // "auto_region_delete", which defaults to
                // true (on).
 
-               if (lyxrc.auto_region_delete)
-                       if (cur.selection())
-                               cutSelection(cur, false, false);
+               if (lyxrc.auto_region_delete && cur.selection()) {
+                       cutSelection(cur, false, false);
+                       // When change tracking is set to off, the metrics update
+                       // mechanism correctly detects if a full update is needed or not.
+                       // This detection fails when a selection spans multiple rows and
+                       // change tracking is enabled because the paragraph metrics stays
+                       // the same. In this case, we force the full update:
+                       // (see http://bugzilla.lyx.org/show_bug.cgi?id=3992)
+                       if (cur.buffer().params().trackChanges)
+                               cur.updateFlags(Update::Force);
+               }
 
                cur.clearSelection();
                Font const old_font = real_current_font;
@@ -1141,8 +1164,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                docstring::const_iterator cit = cmd.argument().begin();
                docstring::const_iterator end = cmd.argument().end();
                for (; cit != end; ++cit)
-                       bv->getIntl().getTransManager().
-                               translateAndInsert(*cit, this, cur);
+                       bv->translateAndInsert(*cit, this, cur);
 
                cur.resetAnchor();
                moveCursor(cur, false);
@@ -1243,7 +1265,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        pars.push_back(Paragraph());
                        pars.back().setInsetOwner(pars[0].inInset());
                        pars.back().layout(tclass.defaultLayout());
-                       
+
                }
 
                // reposition the cursor to the caption
@@ -1335,7 +1357,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_MATH_DELIM:
        case LFUN_MATH_BIGDELIM: {
                cur.insert(new InsetMathHull(hullSimple));
-               cur.dispatch(FuncRequest(LFUN_CHAR_FORWARD));
+               checkAndActivateInset(cur, true);
+               BOOST_ASSERT(cur.inMathed());
                cur.dispatch(cmd);
                break;
        }
@@ -1409,7 +1432,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                Font font(Font::ALL_IGNORE);
                font.setLanguage(lang);
                toggleAndShow(cur, this, font);
-               bv->switchKeyMap();
                break;
        }
 
@@ -1444,16 +1466,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        ++cur.pos();
                break;
 
-       case LFUN_FINISHED_UP:
-               LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_UP:\n" << cur << endl;
-               cursorUp(cur);
-               break;
-
-       case LFUN_FINISHED_DOWN:
-               LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
-               cursorDown(cur);
-               break;
-
        case LFUN_LAYOUT_PARAGRAPH: {
                string data;
                params2string(cur.paragraph(), data);
@@ -1494,8 +1506,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                theLyXFunc().handleKeyFunc(cmd.action);
                if (!cmd.argument().empty())
                        // FIXME: Are all these characters encoded in one byte in utf8?
-                       bv->getIntl().getTransManager()
-                               .translateAndInsert(cmd.argument()[0], this, cur);
+                       bv->translateAndInsert(cmd.argument()[0], this, cur);
                break;
 
        case LFUN_FLOAT_LIST: {
@@ -1568,7 +1579,6 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_ESCAPE:
                if (cur.selection()) {
                        cur.selection() = false;
-                       saveSelection(cur);
                } else {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
@@ -1600,7 +1610,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        // update flag treatment.
        if (singleParUpdate) {
                // Inserting characters does not change par height
-               ParagraphMetrics const & pms 
+               ParagraphMetrics const & pms
                        = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
                if (pms.dim().height()
                    == olddim.height()) {
@@ -1622,7 +1632,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                //
                //if (cur.result().update() != Update::FitCursor)
                //      cur.noUpdate();
-               // 
+               //
                // But some LFUNs do not set Update::FitCursor when needed, so we
                // do it for all. This is not very harmfull as FitCursor will provoke
                // a full redraw only if needed but still, a proper review of all LFUN
@@ -1949,6 +1959,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_MATH_MATRIX:
        case LFUN_MATH_DELIM:
        case LFUN_MATH_BIGDELIM:
+       case LFUN_MATH_INSERT:
        case LFUN_MATH_SUBSCRIPT:
        case LFUN_MATH_SUPERSCRIPT:
        case LFUN_FONT_DEFAULT: