]> git.lyx.org Git - features.git/commitdiff
visual mode for bidi cursor movement --- in math
authorDov Feldstern <dov@lyx.org>
Sun, 10 Feb 2008 19:57:00 +0000 (19:57 +0000)
committerDov Feldstern <dov@lyx.org>
Sun, 10 Feb 2008 19:57:00 +0000 (19:57 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22930 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/InsetMathHull.cpp
src/mathed/InsetMathNest.cpp
src/mathed/InsetMathNest.h

index 5e20b7fe5ec44ba95fa54313b0cce1fb3ceb1938..c8bce467a14c5699bd2b536bcf789339a69001b8 100644 (file)
@@ -1027,6 +1027,8 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_FINISHED_BACKWARD:
        case LFUN_FINISHED_FORWARD:
+       case LFUN_FINISHED_RIGHT:
+       case LFUN_FINISHED_LEFT:
                //lyxerr << "action: " << cmd.action << endl;
                InsetMathGrid::doDispatch(cur, cmd);
                notifyCursorLeaves(cur);
@@ -1166,6 +1168,8 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
        switch (cmd.action) {
        case LFUN_FINISHED_BACKWARD:
        case LFUN_FINISHED_FORWARD:
+       case LFUN_FINISHED_RIGHT:
+       case LFUN_FINISHED_LEFT:
        case LFUN_UP:
        case LFUN_DOWN:
        case LFUN_NEW_LINE:
index 12bc1952793aeb5dc223a8f327c116aee0eb582e..d8f6cd1dbb7cf3ac9a785825288802eb9d4b0aba 100644 (file)
@@ -43,6 +43,7 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
+#include "LyXRC.h"
 #include "support/gettext.h"
 #include "Text.h"
 #include "OutputParams.h"
@@ -490,77 +491,79 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                lfunMouseRelease(cur, cmd);
                break;
 
+       case LFUN_FINISHED_LEFT: // in math, left is backwards
        case LFUN_FINISHED_BACKWARD:
                cur.bv().cursor() = cur;
                break;
 
+       case LFUN_FINISHED_RIGHT: // in math, right is forward
        case LFUN_FINISHED_FORWARD:
                ++cur.pos();
                cur.bv().cursor() = cur;
                break;
 
+       case LFUN_CHAR_RIGHT:
+       case LFUN_CHAR_LEFT:
+       case LFUN_CHAR_BACKWARD:
        case LFUN_CHAR_FORWARD:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_CHAR_FORWARD_SELECT:
-               cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
-               cur.autocorrect() = false;
-               cur.clearTargetX();
-               cur.macroModeClose();
-               if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
-                       cur.pushBackward(*cur.nextAtom().nucleus());
-                       cur.inset().idxFirst(cur);
-               } else if (cur.posForward() || idxForward(cur)
-                       || cur.popForward() || cur.selection())
-                       ;
+       case LFUN_CHAR_RIGHT_SELECT:
+       case LFUN_CHAR_LEFT_SELECT:
+       case LFUN_CHAR_BACKWARD_SELECT:
+       case LFUN_CHAR_FORWARD_SELECT: {
+               // are we in a selection?
+               bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT 
+                                          || cmd.action == LFUN_CHAR_LEFT_SELECT
+                                          || cmd.action == LFUN_CHAR_BACKWARD_SELECT
+                                          || cmd.action == LFUN_CHAR_FORWARD_SELECT);
+               // are we moving forward or backwards? 
+               // If the command was RIGHT or LEFT, then whether we're moving forward
+               // or backwards depends on the cursor movement mode (logical or visual):
+               //  * in visual mode, since math is always LTR, right -> forward, 
+               //    left -> backwards
+               //  * in logical mode, the mapping is determined by the
+               //    reverseDirectionNeeded() function
+               
+               bool forward;
+               kb_action finish_lfun;
+
+               if (cmd.action == LFUN_CHAR_FORWARD 
+                               || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
+                       forward = true;
+                       finish_lfun = LFUN_FINISHED_FORWARD;
+               }
+               else if (cmd.action == LFUN_CHAR_BACKWARD
+                               || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
+                       forward = false;
+                       finish_lfun = LFUN_FINISHED_BACKWARD;
+               }
                else {
-                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
-                       cur.undispatched();
+                       bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
+                                                 || cmd.action == LFUN_CHAR_RIGHT);
+                       if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
+                               forward = right;
+                       else 
+                               forward = !right;
+
+                       if (right)
+                               finish_lfun = LFUN_FINISHED_RIGHT;
+                       else
+                               finish_lfun = LFUN_FINISHED_LEFT;
                }
-               break;
-
-       case LFUN_CHAR_BACKWARD:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_CHAR_BACKWARD_SELECT:
-               cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
+               // Now that we know exactly what we want to do, let's do it!
+               cur.selHandle(select);
                cur.autocorrect() = false;
                cur.clearTargetX();
                cur.macroModeClose();
-               if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
-                       cur.posBackward();
-                       cur.push(*cur.nextAtom().nucleus());
-                       cur.inset().idxLast(cur);
-               } else if (cur.posBackward() || idxBackward(cur)
-                       || cur.popBackward() || cur.selection())
-                       ;
-               else {
-                       cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
+               // try moving forward or backwards as necessary...
+               if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
+                       // ... and if movement failed, then finish forward or backwards
+                       // as necessary
+                       cmd = FuncRequest(finish_lfun);
                        cur.undispatched();
                }
                break;
-
-       case LFUN_CHAR_RIGHT:
-       case LFUN_CHAR_RIGHT_SELECT:
-               //FIXME: for visual cursor, really move right
-               if (reverseDirectionNeeded(cur))
-                       cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
-                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
-               else 
-                       cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
-                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
-               doDispatch(cur, cmd);
-               break;
-
-       case LFUN_CHAR_LEFT:
-       case LFUN_CHAR_LEFT_SELECT:
-               //FIXME: for visual cursor, really move left
-               if (reverseDirectionNeeded(cur))
-                       cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ? 
-                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
-               else 
-                       cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ? 
-                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
-               doDispatch(cur, cmd);
-               break;
+       }
 
        case LFUN_DOWN:
        case LFUN_UP:
@@ -1584,4 +1587,41 @@ bool InsetMathNest::script(Cursor & cur, bool up,
 }
 
 
+bool InsetMathNest::cursorMathForward(Cursor & cur)
+{
+       if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
+               cur.pushBackward(*cur.nextAtom().nucleus());
+               cur.inset().idxFirst(cur);
+               return true;
+       } 
+       if (cur.posForward() || idxForward(cur) || cur.selection())
+               return true;
+       // try to pop forwards --- but don't pop out of math! leave that to
+       // the FINISH lfuns
+       int s = cur.depth() - 2;
+       if (s >= 0 && cur[s].inset().asInsetMath())
+               return cur.popForward();
+       return false;
+}
+
+
+bool InsetMathNest::cursorMathBackward(Cursor & cur)
+{
+       if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
+               cur.posBackward();
+               cur.push(*cur.nextAtom().nucleus());
+               cur.inset().idxLast(cur);
+               return true;
+       } 
+       if (cur.posBackward() || idxBackward(cur) || cur.selection())
+               return true;
+       // try to pop backwards --- but don't pop out of math! leave that to 
+       // the FINISH lfuns
+       int s = cur.depth() - 2;
+       if (s >= 0 && cur[s].inset().asInsetMath())
+               return cur.popBackward();
+       return false;
+}
+
+
 } // namespace lyx
index 408d13a05b5c70623dab04abd1ee0d12790a2f91..a3419fa73f62d77cdf6fdbc9a649cdb3cb9b3430 100644 (file)
@@ -153,6 +153,10 @@ private:
        /// Find a macro to fold or unfold, starting at searchCur and searchCur.nextInset() pointing to a macro
        /// afterwards if found
        bool findMacroToFoldUnfold(Cursor & searchCur, bool fold) const;
+       /// move cursor forward
+       bool cursorMathForward(Cursor & cur);
+       /// move cursor backwards
+       bool cursorMathBackward(Cursor & cur);
 
 protected:
        /// we store the cells in a vector