#include "FuncCode.h"
#include "FuncRequest.h"
#include "Language.h"
-#include "LyXFunc.h" // only for setMessage()
+#include "LyXAction.h"
#include "LyXRC.h"
#include "Paragraph.h"
#include "ParIterator.h"
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)
{}
}
+bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
+{
+ Cursor cur = *this;
+
+ // Try to fix cursor in case it is broken.
+ cur.fixIfBroken();
+
+ // Is this a function that acts on inset at point?
+ Inset * inset = cur.nextInset();
+ if (lyxaction.funcHasFlag(cmd.action, LyXAction::AtPoint)
+ && inset && inset->getStatus(cur, cmd, status))
+ return true;
+
+ // This is, of course, a mess. Better create a new doc iterator and use
+ // this in Inset::getStatus. This might require an additional
+ // BufferView * arg, though (which should be avoided)
+ //Cursor safe = *this;
+ bool res = false;
+ for ( ; cur.depth(); cur.pop()) {
+ //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
+ LASSERT(cur.idx() <= cur.lastidx(), /**/);
+ LASSERT(cur.pit() <= cur.lastpit(), /**/);
+ LASSERT(cur.pos() <= cur.lastpos(), /**/);
+
+ // The inset's getStatus() will return 'true' if it made
+ // a definitive decision on whether it want to handle the
+ // request or not. The result of this decision is put into
+ // the 'status' parameter.
+ if (cur.inset().getStatus(cur, cmd, status)) {
+ res = true;
+ break;
+ }
+ }
+ return res;
+}
+
+
+void Cursor::saveBeforeDispatchPosXY()
+{
+ getPos(beforeDispatchPosX_, beforeDispatchPosY_);
+}
+
+
void Cursor::dispatch(FuncRequest const & cmd0)
{
LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
buffer()->undo().beginUndoGroup();
+ // Is this a function that acts on inset at point?
+ if (lyxaction.funcHasFlag(cmd.action, LyXAction::AtPoint)
+ && nextInset()) {
+ result().dispatched(true);
+ result().update(Update::FitCursor | Update::Force);
+ FuncRequest tmpcmd = cmd;
+ LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
+ << cmd0 << endl << *this);
+ nextInset()->dispatch(*this, tmpcmd);
+ if (result().dispatched()) {
+ buffer()->undo().endUndoGroup();
+ return;
+ }
+ }
+
// store some values to be used inside of the handlers
beforeDispatchCursor_ = *this;
for (; depth(); pop(), boundary(false)) {
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);
+ }
}
void Cursor::clearSelection()
{
setSelection(false);
+ setWordSelection(false);
setMark(false);
resetAnchor();
}
}
-void Cursor::niceInsert(docstring const & t, Parse::flags f)
+void Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
{
- MathData ar;
+ MathData ar(buffer());
asArray(t, ar, f);
- if (ar.size() == 1)
+ if (ar.size() == 1 && (enter || selection()))
niceInsert(ar[0]);
else
insert(ar);
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
// push the clone, not the original
pushBackward(*nextInset());
// We may not use niceInsert here (recursion)
- MathData ar;
+ MathData ar(buffer());
asArray(safe, ar);
insert(ar);
}
cap::eraseSelection(*this);
cell().insert(pos(), ar);
pos() += ar.size();
+ // FIXME audit setBuffer/updateLabels calls
+ inset().setBuffer(bv_->buffer());
}
return false;
InsetMathUnknown * p = activeMacro();
p->finalize();
- MathData selection;
+ MathData selection(buffer());
asArray(p->selection(), selection);
docstring const s = p->name();
--pos();
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)
int x;
int y;
getPos(x, y);
- int oy = theLyXFunc().cursorBeforeDispatchY();
+ int oy = beforeDispatchPosY_;
if ((!up && y <= oy) ||
(up && y >= oy))
operator=(old);
int x;
int y;
getPos(x, y);
- int oy = theLyXFunc().cursorBeforeDispatchY();
+ int oy = beforeDispatchPosY_;
if ((!up && y <= oy) ||
(up && y >= oy))
operator=(old);
//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;
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
}
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);
}
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);
}
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;
}
// 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;
}