]> git.lyx.org Git - lyx.git/blobdiff - src/Cursor.cpp
partial fix for bug 6986: Use the right tex2lyx (at least if tex2lyx is installed)
[lyx.git] / src / Cursor.cpp
index 185d8669506f765711728866265333f0ec3f762e..e08793c393f2ed8c88c2ffb615bfbd95094189d3 100644 (file)
@@ -26,6 +26,7 @@
 #include "FuncCode.h"
 #include "FuncRequest.h"
 #include "Language.h"
+#include "Layout.h"
 #include "LyXAction.h"
 #include "LyXRC.h"
 #include "Paragraph.h"
 #include "mathed/MathData.h"
 #include "mathed/MathMacro.h"
 
-#include <boost/bind.hpp>
+#include "support/bind.h"
 
 #include <sstream>
 #include <limits>
 #include <map>
+#include <algorithm>
 
 using namespace std;
 
@@ -152,7 +154,7 @@ bool bruteFind(Cursor & cursor,
        else
                ++et.pit();
 
-       double best_dist = numeric_limits<double>::max();;
+       double best_dist = numeric_limits<double>::max();
        DocIterator best_cursor = et;
 
        for ( ; it != et; it.forwardPos(true)) {
@@ -214,8 +216,10 @@ bool bruteFind3(Cursor & cur, int x, int y, bool up)
        for ( ; it != et; it.forwardPos()) {
                // avoid invalid nesting when selecting
                if (bv.cursorStatus(it) == CUR_INSIDE
-                               && (!cur.selection() || positionable(it, cur.anchor_))) {
-                       Point p = bv.getPos(it, false);
+                               && (!cur.selection() || positionable(it, cur.realAnchor()))) {
+                       // If this function is ever used again, check whether this
+                       // is the same as "bv.getPos(it, false)" with boundary = false.
+                       Point p = bv.getPos(it);
                        int xo = p.x_;
                        int yo = p.y_;
                        if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
@@ -329,14 +333,16 @@ void Cursor::dispatch(FuncRequest const & cmd0)
        fixIfBroken();
        FuncRequest cmd = cmd0;
        Cursor safe = *this;
+       Cursor old = *this;
+       disp_ = DispatchResult();
 
        buffer()->undo().beginUndoGroup();
-       
+
        // Is this a function that acts on inset at point?
        if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
            && nextInset()) {
                disp_.dispatched(true);
-               disp_.update(Update::FitCursor | Update::Force);
+               disp_.screenUpdate(Update::FitCursor | Update::Force);
                FuncRequest tmpcmd = cmd;
                LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
                        << cmd0 << endl << *this);
@@ -359,7 +365,7 @@ void Cursor::dispatch(FuncRequest const & cmd0)
                // The common case is 'LFUN handled, need update', so make the
                // LFUN handler's life easier by assuming this as default value.
                // The handler can reset the update and val flags if necessary.
-               disp_.update(Update::FitCursor | Update::Force);
+               disp_.screenUpdate(Update::FitCursor | Update::Force);
                disp_.dispatched(true);
                inset().dispatch(*this, cmd);
                if (disp_.dispatched())
@@ -380,7 +386,7 @@ void Cursor::dispatch(FuncRequest const & cmd0)
                        safe.pos() = safe.lastpos();
                }
                operator=(safe);
-               disp_.update(Update::None);
+               disp_.screenUpdate(Update::None);
                disp_.dispatched(false);
        } else {
                // restore the previous one because nested Cursor::dispatch calls
@@ -388,6 +394,18 @@ void Cursor::dispatch(FuncRequest const & cmd0)
                beforeDispatchCursor_ = safe.beforeDispatchCursor_;
        }
        buffer()->undo().endUndoGroup();
+
+       // notify insets we just left
+       if (*this != old) {
+               old.beginUndoGroup();
+               old.fixIfBroken();
+               bool badcursor = notifyCursorLeavesOrEnters(old, *this);
+               if (badcursor) {
+                       fixIfBroken();
+                       bv().fixInlineCompletionPos();
+               }
+               old.endUndoGroup();
+       }
 }
 
 
@@ -467,7 +485,7 @@ int Cursor::currentMode()
 
 void Cursor::getPos(int & x, int & y) const
 {
-       Point p = bv().getPos(*this, boundary());
+       Point p = bv().getPos(*this);
        x = p.x_;
        y = p.y_;
 }
@@ -994,6 +1012,12 @@ CursorSlice Cursor::normalAnchor() const
 }
 
 
+DocIterator & Cursor::realAnchor()
+{
+       return anchor_;
+}
+
+
 CursorSlice Cursor::selBegin() const
 {
        if (!selection())
@@ -1267,13 +1291,14 @@ void Cursor::plainInsert(MathAtom const & t)
        ++pos();
        inset().setBuffer(bv_->buffer());
        inset().initView();
+       forceBufferUpdate();
 }
 
 
 void Cursor::insert(docstring const & str)
 {
        for_each(str.begin(), str.end(),
-                boost::bind(static_cast<void(Cursor::*)(char_type)>
+                bind(static_cast<void(Cursor::*)(char_type)>
                             (&Cursor::insert), this, _1));
 }
 
@@ -1304,11 +1329,13 @@ void Cursor::insert(Inset * inset0)
 {
        LASSERT(inset0, /**/);
        if (inMathed())
-               insert(MathAtom(inset0));
+               insert(MathAtom(inset0->asInsetMath()));
        else {
                text()->insertInset(*this, inset0);
                inset0->setBuffer(bv_->buffer());
                inset0->initView();
+               if (inset0->isLabeled())
+                       forceBufferUpdate();
        }
 }
 
@@ -1339,6 +1366,15 @@ void Cursor::niceInsert(MathAtom const & t)
                MathData ar(buffer());
                asArray(safe, ar);
                insert(ar);
+       } else if (t->asMacro() && !safe.empty()) {
+               MathData ar(buffer());
+               asArray(safe, ar);
+               docstring const name = t->asMacro()->name();
+               MacroData const * data = buffer()->getMacro(name);
+               if (data && data->numargs() - data->optionals() > 0) {
+                       plainInsert(MathAtom(new InsetMathBrace(ar)));
+                       posBackward();
+               }
        }
 }
 
@@ -1350,7 +1386,7 @@ void Cursor::insert(MathData const & ar)
                cap::eraseSelection(*this);
        cell().insert(pos(), ar);
        pos() += ar.size();
-       // FIXME audit setBuffer/updateBuffer calls
+       // FIXME audit setBuffer calls
        inset().setBuffer(bv_->buffer());
 }
 
@@ -1501,7 +1537,7 @@ bool Cursor::macroModeClose()
 
        // trigger updates of macros, at least, if no full
        // updates take place anyway
-       updateFlags(Update::Force);
+       screenUpdateFlags(Update::Force);
 
        docstring const name = s.substr(1);
        InsetMathNest * const in = inset().asInsetMath()->asNestInset();
@@ -1874,13 +1910,15 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
 
                        updateNeeded |= bv().checkDepm(dummy, *this);
                        updateTextTargetOffset();
+                       if (updateNeeded)
+                               forceBufferUpdate();
                }
                return false;
        }
 
        // with and without selection are handled differently
        if (!selection()) {
-               int yo = bv().getPos(*this, boundary()).y_;
+               int yo = bv().getPos(*this).y_;
                Cursor old = *this;
                // To next/previous row
                if (up)
@@ -1898,7 +1936,7 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
                        ++dummy.pos();
                if (bv().checkDepm(dummy, old)) {
                        updateNeeded = true;
-                       // Make sure that cur gets back whatever happened to dummy(Lgb) 
+                       // Make sure that cur gets back whatever happened to dummy (Lgb) 
                        operator=(dummy);
                }
        } else {
@@ -1943,6 +1981,8 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
                updateNeeded |= bv().checkDepm(*this, old);
        }
 
+       if (updateNeeded)
+               forceBufferUpdate();
        updateTextTargetOffset();
        return true;
 }      
@@ -1999,15 +2039,18 @@ void Cursor::errorMessage(docstring const & msg) const
 }
 
 
-static docstring parbreak(InsetCode code)
+namespace {
+docstring parbreak(Cursor const * cur)
 {
        odocstringstream os;
        os << '\n';
-       // only add blank line if we're not in an ERT or Listings inset
-       if (code != ERT_CODE && code != LISTINGS_CODE)
+       // only add blank line if we're not in a ParbreakIsNewline situation
+       if (!cur->inset().getLayout().parbreakIsNewline() 
+           && !cur->paragraph().layout().parbreak_is_newline)
                os << '\n';
        return os.str();
 }
+}
 
 
 docstring Cursor::selectionAsString(bool with_label) const
@@ -2043,13 +2086,13 @@ docstring Cursor::selectionAsString(bool with_label) const
        // First paragraph in selection
        docstring result = pars[startpit].
                asString(startpos, pars[startpit].size(), label)
-               + parbreak(inset().lyxCode());
+               + parbreak(this);
 
        // The paragraphs in between (if any)
        for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
                Paragraph const & par = pars[pit];
                result += par.asString(0, par.size(), label)
-                       + parbreak(inset().lyxCode());
+                       + parbreak(this);
        }
 
        // Last paragraph in selection
@@ -2104,15 +2147,33 @@ void Cursor::dispatched()
 }
 
 
-void Cursor::updateFlags(Update::flags f)
+void Cursor::screenUpdateFlags(Update::flags f)
+{
+       disp_.screenUpdate(f);
+}
+
+
+void Cursor::forceBufferUpdate()
+{
+       disp_.forceBufferUpdate();
+}
+
+
+void Cursor::clearBufferUpdate()
+{
+       disp_.clearBufferUpdate();
+}
+
+
+bool Cursor::needBufferUpdate() const
 {
-       disp_.update(f);
+       return disp_.needBufferUpdate();
 }
 
 
-void Cursor::noUpdate()
+void Cursor::noScreenUpdate()
 {
-       disp_.update(Update::None);
+       disp_.screenUpdate(Update::None);
 }
 
 
@@ -2310,9 +2371,9 @@ void Cursor::recordUndo(UndoKind kind) const
 }
 
 
-void Cursor::recordUndoInset(UndoKind kind) const
+void Cursor::recordUndoInset(UndoKind kind, Inset const * inset) const
 {
-       buffer()->undo().recordUndoInset(*this, kind);
+       buffer()->undo().recordUndoInset(*this, kind, inset);
 }