#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"
#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;
int xo;
int yo;
Inset const * inset = &it.inset();
- map<Inset const *, Geometry> const & data =
- c.bv().coordCache().getInsets().getData();
- map<Inset const *, Geometry>::const_iterator I = data.find(inset);
+ CoordCache const & cache = c.bv().coordCache();
// FIXME: in the case where the inset is not in the cache, this
// means that no part of it is visible on screen. In this case
// we don't do elaborate search and we just return the forwarded
// DocIterator at its beginning.
- if (I == data.end()) {
+ if (!cache.getInsets().has(inset)) {
it.top().pos() = 0;
return it;
}
- Point o = I->second.pos;
+ Point const o = cache.getInsets().xy(inset);
inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
// Convert to absolute
xo += o.x_;
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)) {
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) {
// 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;
}
+void Cursor::saveBeforeDispatchPosXY()
+{
+ getPos(beforeDispatchPosX_, beforeDispatchPosY_);
+}
+
+
void Cursor::dispatch(FuncRequest const & cmd0)
{
LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
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;
}
// 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())
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
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().resetInlineCompletionPos();
+ }
+ old.endUndoGroup();
+ }
}
-DispatchResult Cursor::result() const
+DispatchResult const & Cursor::result() const
{
return disp_;
}
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_;
}
void Cursor::resetAnchor()
{
anchor_ = *this;
+ checkNewWordPosition();
}
}
+void Cursor::markNewWordPosition()
+{
+ if (lyxrc.spellcheck_continuously && inTexted() && new_word_.empty()) {
+ FontSpan nw = locateWord(WHOLE_WORD);
+ if (nw.size() == 1) {
+ LYXERR(Debug::DEBUG, "start new word: "
+ << " par: " << pit()
+ << " pos: " << nw.first);
+ new_word_ = *this;
+ }
+ }
+}
+
+
+void Cursor::clearNewWordPosition()
+{
+ if (!new_word_.empty()) {
+ LYXERR(Debug::DEBUG, "clear new word: "
+ << " par: " << pit()
+ << " pos: " << pos());
+ new_word_.resize(0);
+ }
+}
+
+
+void Cursor::checkNewWordPosition()
+{
+ if (!lyxrc.spellcheck_continuously || new_word_.empty())
+ return ;
+ if (!inTexted())
+ clearNewWordPosition();
+ else {
+ // forget the position of the current new word if
+ // 1) the paragraph changes or
+ // 2) the count of nested insets changes or
+ // 3) the cursor pos is out of paragraph bound
+ if (pit() != new_word_.pit() ||
+ depth() != new_word_.depth() ||
+ new_word_.pos() > new_word_.lastpos()) {
+ clearNewWordPosition();
+ } else if (new_word_.fixIfBroken())
+ // 4) or the remembered position was "broken"
+ clearNewWordPosition();
+ else {
+ FontSpan nw = locateWord(WHOLE_WORD);
+ if (nw.size()) {
+ FontSpan ow = new_word_.locateWord(WHOLE_WORD);
+ if (nw.intersect(ow).empty())
+ clearNewWordPosition();
+ else
+ LYXERR(Debug::DEBUG, "new word: "
+ << " par: " << pit()
+ << " pos: " << nw.first << ".." << nw.last);
+ } else {
+ clearNewWordPosition();
+ }
+ }
+ }
+}
+
+
bool Cursor::posBackward()
{
if (pos() == 0)
}
-CursorSlice Cursor::anchor() const
+CursorSlice Cursor::normalAnchor() const
{
if (!selection())
return top();
}
+DocIterator & Cursor::realAnchor()
+{
+ return anchor_;
+}
+
+
CursorSlice Cursor::selBegin() const
{
if (!selection())
return top();
- return anchor() < top() ? anchor() : top();
+ return normalAnchor() < top() ? normalAnchor() : top();
}
{
if (!selection())
return top();
- return anchor() > top() ? anchor() : top();
+ return normalAnchor() > top() ? normalAnchor() : top();
}
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;
}
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());
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);
}
namespace lyx {
-//#define FILEDEBUG 1
-
-
bool Cursor::isInside(Inset const * p) const
{
for (size_t i = 0; i != depth(); ++i)
++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));
}
{
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, bool enter)
+int Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
{
MathData ar(buffer());
asArray(t, ar, f);
niceInsert(ar[0]);
else
insert(ar);
+ return ar.size();
}
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();
+ }
}
}
cap::eraseSelection(*this);
cell().insert(pos(), ar);
pos() += ar.size();
- // FIXME audit setBuffer/updateLabels calls
+ // FIXME audit setBuffer calls
inset().setBuffer(bv_->buffer());
}
// 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();
{
if (!inMathed())
return false;
- if (pos() == 0)
+ if (pos() == 0 || cell().empty())
return false;
InsetMathUnknown const * p = prevAtom()->asUnknownInset();
return p && !p->final();
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
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)
++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 {
updateNeeded |= bv().checkDepm(*this, old);
}
+ if (updateNeeded)
+ forceBufferUpdate();
updateTextTargetOffset();
return true;
}
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
// 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
}
-void Cursor::undispatched()
+void Cursor::undispatched() const
{
disp_.dispatched(false);
}
-void Cursor::dispatched()
+void Cursor::dispatched() const
{
disp_.dispatched(true);
}
-void Cursor::updateFlags(Update::flags f)
+void Cursor::screenUpdateFlags(Update::flags f) const
+{
+ disp_.screenUpdate(f);
+}
+
+
+void Cursor::forceBufferUpdate() const
+{
+ disp_.forceBufferUpdate();
+}
+
+
+void Cursor::clearBufferUpdate() const
+{
+ disp_.clearBufferUpdate();
+}
+
+
+bool Cursor::needBufferUpdate() const
{
- disp_.update(f);
+ return disp_.needBufferUpdate();
}
-void Cursor::noUpdate()
+void Cursor::noScreenUpdate() const
{
- disp_.update(Update::None);
+ disp_.screenUpdate(Update::None);
}
bool const broken_anchor = anchor_.fixIfBroken();
if (broken_cursor || broken_anchor) {
+ clearNewWordPosition();
clearSelection();
return true;
}
}
-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);
}