X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCursor.cpp;h=e08793c393f2ed8c88c2ffb615bfbd95094189d3;hb=225c1dbe55635a97b183909ea7194b8d92051378;hp=2a6d4e6420688226b4b3fe8b8f6644570c0ab230;hpb=3f3bedd0d9832ebd8c70ea0144387eadaaf5ab6f;p=lyx.git diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 2a6d4e6420..e08793c393 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -26,8 +26,8 @@ #include "FuncCode.h" #include "FuncRequest.h" #include "Language.h" +#include "Layout.h" #include "LyXAction.h" -#include "LyXFunc.h" // only for setMessage() #include "LyXRC.h" #include "Paragraph.h" #include "ParIterator.h" @@ -50,11 +50,12 @@ #include "mathed/MathData.h" #include "mathed/MathMacro.h" -#include +#include "support/bind.h" #include #include #include +#include using namespace std; @@ -153,7 +154,7 @@ bool bruteFind(Cursor & cursor, else ++et.pit(); - double best_dist = numeric_limits::max();; + double best_dist = numeric_limits::max(); DocIterator best_cursor = et; for ( ; it != et; it.forwardPos(true)) { @@ -215,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) { @@ -254,8 +257,8 @@ bool bruteFind3(Cursor & cur, int x, int y, bool up) Cursor::Cursor(BufferView & bv) : DocIterator(&bv.buffer()), bv_(&bv), anchor_(), x_target_(-1), textTargetOffset_(0), - selection_(false), mark_(false), logicalpos_(false), - current_font(inherit_font) + selection_(false), mark_(false), word_selection_(false), + logicalpos_(false), current_font(inherit_font) {} @@ -287,7 +290,7 @@ bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const // Is this a function that acts on inset at point? Inset * inset = cur.nextInset(); - if (lyxaction.funcHasFlag(cmd.action, LyXAction::AtPoint) + if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint) && inset && inset->getStatus(cur, cmd, status)) return true; @@ -315,6 +318,12 @@ bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const } +void Cursor::saveBeforeDispatchPosXY() +{ + getPos(beforeDispatchPosX_, beforeDispatchPosY_); +} + + void Cursor::dispatch(FuncRequest const & cmd0) { LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this); @@ -324,20 +333,24 @@ 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) + if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint) && nextInset()) { - result().dispatched(true); - result().update(Update::FitCursor | Update::Force); + disp_.dispatched(true); + disp_.screenUpdate(Update::FitCursor | Update::Force); FuncRequest tmpcmd = cmd; LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: " << cmd0 << endl << *this); nextInset()->dispatch(*this, tmpcmd); - if (result().dispatched()) + if (disp_.dispatched()) { + buffer()->undo().endUndoGroup(); return; + } } // store some values to be used inside of the handlers @@ -352,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()) @@ -373,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 @@ -381,10 +394,22 @@ 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(); + } } -DispatchResult Cursor::result() const +DispatchResult const & Cursor::result() const { return disp_; } @@ -460,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_; } @@ -483,8 +508,14 @@ void Cursor::resetAnchor() void Cursor::setCursorToAnchor() { - if (selection()) - setCursor(anchor_); + if (selection()) { + DocIterator normal = anchor_; + while (depth() < normal.depth()) + normal.pop_back(); + if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1]) + ++normal.pos(); + setCursor(normal); + } } @@ -967,7 +998,7 @@ void Cursor::posVisToRowExtremity(bool left) } -CursorSlice Cursor::anchor() const +CursorSlice Cursor::normalAnchor() const { if (!selection()) return top(); @@ -981,11 +1012,17 @@ CursorSlice Cursor::anchor() const } +DocIterator & Cursor::realAnchor() +{ + return anchor_; +} + + CursorSlice Cursor::selBegin() const { if (!selection()) return top(); - return anchor() < top() ? anchor() : top(); + return normalAnchor() < top() ? normalAnchor() : top(); } @@ -993,7 +1030,7 @@ CursorSlice Cursor::selEnd() const { if (!selection()) return top(); - return anchor() > top() ? anchor() : top(); + return normalAnchor() > top() ? normalAnchor() : top(); } @@ -1005,10 +1042,10 @@ DocIterator Cursor::selectionBegin() const DocIterator di; // FIXME: This is a work-around for the problem that // CursorSlice doesn't keep track of the boundary. - if (anchor() == top()) + if (normalAnchor() == top()) di = anchor_.boundary() > boundary() ? anchor_ : *this; else - di = anchor() < top() ? anchor_ : *this; + di = normalAnchor() < top() ? anchor_ : *this; di.resize(depth()); return di; } @@ -1022,10 +1059,10 @@ DocIterator Cursor::selectionEnd() const DocIterator di; // FIXME: This is a work-around for the problem that // CursorSlice doesn't keep track of the boundary. - if (anchor() == top()) + if (normalAnchor() == top()) di = anchor_.boundary() < boundary() ? anchor_ : *this; else - di = anchor() > top() ? anchor_ : *this; + di = normalAnchor() > top() ? anchor_ : *this; if (di.depth() > depth()) { di.resize(depth()); @@ -1040,9 +1077,9 @@ void Cursor::setSelection() setSelection(true); // A selection with no contents is not a selection // FIXME: doesnt look ok - if (idx() == anchor().idx() && - pit() == anchor().pit() && - pos() == anchor().pos()) + if (idx() == normalAnchor().idx() && + pit() == normalAnchor().pit() && + pos() == normalAnchor().pos()) setSelection(false); } @@ -1059,6 +1096,7 @@ void Cursor::setSelection(DocIterator const & where, int n) void Cursor::clearSelection() { setSelection(false); + setWordSelection(false); setMark(false); resetAnchor(); } @@ -1253,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 + bind(static_cast (&Cursor::insert), this, _1)); } @@ -1290,20 +1329,22 @@ 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(); } } -void Cursor::niceInsert(docstring const & t, Parse::flags f) +void Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter) { MathData ar(buffer()); asArray(t, ar, f); - if (ar.size() == 1) + if (ar.size() == 1 && (enter || selection())) niceInsert(ar[0]); else insert(ar); @@ -1315,7 +1356,7 @@ void Cursor::niceInsert(MathAtom const & t) macroModeClose(); docstring const safe = cap::grabAndEraseSelection(*this); plainInsert(t); - // enter the new inset and move the contents of the selection if possible + // If possible, enter the new inset and move the contents of the selection if (t->isActive()) { posBackward(); // be careful here: don't use 'pushBackward(t)' as this we need to @@ -1325,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(); + } } } @@ -1336,6 +1386,8 @@ void Cursor::insert(MathData const & ar) cap::eraseSelection(*this); cell().insert(pos(), ar); pos() += ar.size(); + // FIXME audit setBuffer calls + inset().setBuffer(bv_->buffer()); } @@ -1485,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(); @@ -1651,8 +1703,8 @@ bool Cursor::upDownInMath(bool up) int xo = 0; int yo = 0; getPos(xo, yo); - xo = theLyXFunc().cursorBeforeDispatchX(); - + xo = beforeDispatchPosX_; + // check if we had something else in mind, if not, this is the future // target if (x_target_ == -1) @@ -1701,7 +1753,7 @@ bool Cursor::upDownInMath(bool up) int x; int y; getPos(x, y); - int oy = theLyXFunc().cursorBeforeDispatchY(); + int oy = beforeDispatchPosY_; if ((!up && y <= oy) || (up && y >= oy)) operator=(old); @@ -1722,7 +1774,7 @@ bool Cursor::upDownInMath(bool up) int x; int y; getPos(x, y); - int oy = theLyXFunc().cursorBeforeDispatchY(); + int oy = beforeDispatchPosY_; if ((!up && y <= oy) || (up && y >= oy)) operator=(old); @@ -1746,7 +1798,7 @@ bool Cursor::upDownInMath(bool up) //lyxerr << "updown: popBackward succeeded" << endl; int xnew; int ynew; - int yold = theLyXFunc().cursorBeforeDispatchY(); + int yold = beforeDispatchPosY_; getPos(xnew, ynew); if (up ? ynew < yold : ynew > yold) return true; @@ -1788,7 +1840,7 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded) int xo = 0; int yo = 0; getPos(xo, yo); - xo = theLyXFunc().cursorBeforeDispatchX(); + xo = beforeDispatchPosX_; // update the targetX - this is here before the "return false" // to set a new target which can be used by InsetTexts above @@ -1858,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) @@ -1882,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 { @@ -1914,11 +1968,21 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded) } top().pos() = min(tm.x2pos(pit(), next_row, xo), top().lastpos()); - boundary(tm.x2pos(pit(), next_row, xo) - == tm.x2pos(pit(), next_row, tm.width())); + int const xpos = tm.x2pos(pit(), next_row, xo); + bool const at_end_row = xpos == tm.x2pos(pit(), next_row, tm.width()); + bool const at_beg_row = xpos == tm.x2pos(pit(), next_row, 0); + + if (at_end_row && at_beg_row) + // make sure the cursor ends up on this row + boundary(false); + else + boundary(at_end_row); + updateNeeded |= bv().checkDepm(*this, old); } + if (updateNeeded) + forceBufferUpdate(); updateTextTargetOffset(); return true; } @@ -1964,25 +2028,29 @@ void Cursor::handleFont(string const & font) void Cursor::message(docstring const & msg) const { - theLyXFunc().setMessage(msg); + disp_.setMessage(msg); } void Cursor::errorMessage(docstring const & msg) const { - theLyXFunc().setErrorMessage(msg); + disp_.setMessage(msg); + disp_.setError(true); } -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 @@ -2018,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 @@ -2079,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_.update(f); + disp_.forceBufferUpdate(); } -void Cursor::noUpdate() +void Cursor::clearBufferUpdate() { - disp_.update(Update::None); + disp_.clearBufferUpdate(); +} + + +bool Cursor::needBufferUpdate() const +{ + return disp_.needBufferUpdate(); +} + + +void Cursor::noScreenUpdate() +{ + disp_.screenUpdate(Update::None); } @@ -2127,9 +2213,12 @@ Font Cursor::getFont() const bool Cursor::fixIfBroken() { - if (DocIterator::fixIfBroken()) { - clearSelection(); - return true; + bool const broken_cursor = DocIterator::fixIfBroken(); + bool const broken_anchor = anchor_.fixIfBroken(); + + if (broken_cursor || broken_anchor) { + clearSelection(); + return true; } return false; } @@ -2155,9 +2244,9 @@ bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur) // notify everything on top of the common part in old cursor, // but stop if the inset claims the cursor to be invalid now for (size_type j = i; j < old.depth(); ++j) { - Cursor insetPos = old; - insetPos.cutOff(j); - if (old[j].inset().notifyCursorLeaves(insetPos, cur)) + Cursor inset_pos = old; + inset_pos.cutOff(j); + if (old[j].inset().notifyCursorLeaves(inset_pos, cur)) return true; } @@ -2282,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); }