3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Stefan Schimanski
9 * Full author contact details are available in file CREDITS.
16 #include "InsetMathBrace.h"
17 #include "InsetMathFont.h"
18 #include "InsetMathScript.h"
19 #include "MacroTable.h"
20 #include "InsetMathMacro.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
23 #include "MetricsInfo.h"
24 #include "ReplaceData.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "mathed/InsetMathUnknown.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lassert.h"
40 #include "support/lyxalgo.h"
49 MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
50 : base_type(from, to), minasc_(0), mindes_(0), slevel_(0),
51 sshift_(0), kerning_(0), buffer_(buf)
55 MathAtom & MathData::operator[](pos_type pos)
57 LBUFERR(pos < size());
58 return base_type::operator[](pos);
62 MathAtom const & MathData::operator[](pos_type pos) const
64 LBUFERR(pos < size());
65 return base_type::operator[](pos);
69 void MathData::insert(size_type pos, MathAtom const & t)
71 LBUFERR(pos <= size());
72 base_type::insert(begin() + pos, t);
76 void MathData::insert(size_type pos, MathData const & ar)
78 LBUFERR(pos <= size());
79 base_type::insert(begin() + pos, ar.begin(), ar.end());
83 void MathData::append(MathData const & ar)
89 void MathData::erase(size_type pos)
96 void MathData::erase(iterator pos1, iterator pos2)
98 base_type::erase(pos1, pos2);
102 void MathData::erase(iterator pos)
104 base_type::erase(pos);
108 void MathData::erase(size_type pos1, size_type pos2)
110 base_type::erase(begin() + pos1, begin() + pos2);
114 void MathData::dump2() const
118 for (const_iterator it = begin(); it != end(); ++it)
120 lyxerr << to_utf8(os.str());
124 void MathData::dump() const
128 for (const_iterator it = begin(); it != end(); ++it)
129 ns << '<' << *it << '>';
130 lyxerr << to_utf8(os.str());
134 void MathData::validate(LaTeXFeatures & features) const
136 for (const_iterator it = begin(); it != end(); ++it)
137 (*it)->validate(features);
141 bool MathData::match(MathData const & ar) const
143 return size() == ar.size() && matchpart(ar, 0);
147 bool MathData::matchpart(MathData const & ar, pos_type pos) const
149 if (size() < ar.size() + pos)
151 const_iterator it = begin() + pos;
152 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
153 if (asString(*it) != asString(*jt))
159 void MathData::replace(ReplaceData & rep)
161 for (size_type i = 0; i < size(); ++i) {
162 if (find1(rep.from, i)) {
164 lyxerr << "match found!" << endl;
165 erase(i, i + rep.from.size());
170 // FIXME: temporarily disabled
171 // for (const_iterator it = begin(); it != end(); ++it)
172 // it->nucleus()->replace(rep);
176 bool MathData::find1(MathData const & ar, size_type pos) const
178 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
179 for (size_type i = 0, n = ar.size(); i < n; ++i)
180 if (asString(operator[](pos + i)) != asString(ar[i]))
186 MathData::size_type MathData::find(MathData const & ar) const
188 for (int i = 0, last = size() - ar.size(); i < last; ++i)
195 MathData::size_type MathData::find_last(MathData const & ar) const
197 for (int i = size() - ar.size(); i >= 0; --i)
204 bool MathData::contains(MathData const & ar) const
206 if (find(ar) != size())
208 for (const_iterator it = begin(); it != end(); ++it)
209 if ((*it)->contains(ar))
215 bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
217 bool has_contents = false;
218 BufferView * bv = mi.base.bv;
219 MathData * ar = const_cast<MathData*>(this);
220 ar->updateMacros(&bv->cursor(), mi.macrocontext,
221 InternalUpdate, mi.base.macro_nesting);
224 // FIXME: for completion, try to insert the relevant data in the
225 // mathrow (like is done for text rows). We could add a pair of
226 // InsetMathColor inset, but these come with extra spacing of
228 DocIterator const & inlineCompletionPos = bv->inlineCompletionPos();
229 bool const has_completion = inlineCompletionPos.inMathed()
230 && &inlineCompletionPos.cell() == this;
231 size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
233 for (size_t i = 0 ; i < size() ; ++i) {
234 has_contents |= (*this)[i]->addToMathRow(mrow, mi);
235 if (i + 1 == compl_pos) {
236 mrow.back().compl_text = bv->inlineCompletion();
237 mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
247 bool isInside(DocIterator const & it, MathData const & ar,
248 pos_type p1, pos_type p2)
250 for (size_t i = 0; i != it.depth(); ++i) {
251 CursorSlice const & sl = it[i];
252 if (sl.inset().inMathed() && &sl.cell() == &ar)
253 return p1 <= sl.pos() && sl.pos() < p2;
262 void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
264 // This is the only point where the drawing font is known.
265 // We set cursor current font so that the blinking caret height
266 // adapts to math font.
267 Cursor & cur = mi.base.bv->cursor();
268 if (cur.inMathed() && &cur.cell() == this) {
269 cur.current_font.fontInfo() = mi.base.font;
270 cur.real_current_font.fontInfo() = mi.base.font;
273 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
274 dim = fm.dimension('I');
275 int xascent = fm.dimension('x').ascent();
276 if (xascent >= dim.asc)
277 xascent = (2 * dim.asc) / 3;
279 mindes_ = (3 * xascent) / 4;
280 slevel_ = (4 * xascent) / 5;
281 sshift_ = xascent / 4;
283 MathRow mrow(mi, this);
284 mrow.metrics(mi, dim);
285 mrow_cache_[mi.base.bv] = mrow;
286 kerning_ = mrow.kerning(mi.base.bv);
288 // Cache the dimension.
289 mi.base.bv->coordCache().arrays().add(this, dim);
293 void MathData::drawSelection(PainterInfo & pi, int const x, int const y) const
295 BufferView const * bv = pi.base.bv;
296 Cursor const & cur = bv->cursor();
297 InsetMath const * inset = cur.inset().asInsetMath();
298 if (!cur.selection() || !inset || inset->nargs() == 0)
301 CursorSlice const s1 = cur.selBegin();
302 CursorSlice const s2 = cur.selEnd();
303 MathData const & c1 = inset->cell(s1.idx());
305 if (s1.idx() == s2.idx() && &c1 == this) {
306 // selection indide cell
307 Dimension const dim = bv->coordCache().getArrays().dim(&c1);
308 int const beg = c1.pos2x(bv, s1.pos());
309 int const end = c1.pos2x(bv, s2.pos());
310 pi.pain.fillRectangle(x + beg, y - dim.ascent(),
311 end - beg, dim.height(), Color_selection);
313 for (idx_type i = 0; i < inset->nargs(); ++i) {
314 MathData const & c = inset->cell(i);
315 if (&c == this && inset->idxBetween(i, s1.idx(), s2.idx())) {
316 // The whole cell is selected
317 Dimension const dim = bv->coordCache().getArrays().dim(&c);
318 pi.pain.fillRectangle(x, y - dim.ascent(),
319 dim.width(), dim.height(),
327 void MathData::draw(PainterInfo & pi, int const x, int const y) const
329 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
330 setXY(*pi.base.bv, x, y);
332 drawSelection(pi, x, y);
333 MathRow const & mrow = mrow_cache_[pi.base.bv];
338 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
342 for (const_iterator it = begin(); it != end(); ++it) {
343 (*it)->metricsT(mi, d);
349 void MathData::drawT(TextPainter & pain, int x, int y) const
351 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
353 // FIXME: Abdel 16/10/2006
354 // This drawT() method is never used, this is dead code.
356 for (const_iterator it = begin(), et = end(); it != et; ++it) {
357 (*it)->drawT(pain, x, y);
358 //x += (*it)->width_;
364 void MathData::updateBuffer(ParIterator const & it, UpdateType utype)
367 for (size_t i = 0, n = size(); i != n; ++i) {
368 MathAtom & at = operator[](i);
369 at.nucleus()->updateBuffer(it, utype);
374 void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
375 UpdateType utype, int nesting)
377 // If we are editing a macro, we cannot update it immediately,
378 // otherwise wrong undo steps will be recorded (bug 6208).
379 InsetMath const * inmath = cur ? cur->inset().asInsetMath() : 0;
380 InsetMathMacro const * inmacro = inmath ? inmath->asMacro() : 0;
381 docstring const edited_name = inmacro ? inmacro->name() : docstring();
383 // go over the array and look for macros
384 for (size_t i = 0; i < size(); ++i) {
385 InsetMathMacro * macroInset = operator[](i).nucleus()->asMacro();
386 if (!macroInset || macroInset->macroName().empty()
387 || macroInset->macroName()[0] == '^'
388 || macroInset->macroName()[0] == '_'
389 || (macroInset->name() == edited_name
390 && macroInset->displayMode() ==
391 InsetMathMacro::DISPLAY_UNFOLDED))
395 macroInset->updateMacro(mc);
396 size_t macroNumArgs = 0;
397 size_t macroOptionals = 0;
398 MacroData const * macro = macroInset->macro();
400 macroNumArgs = macro->numargs();
401 macroOptionals = macro->optionals();
404 // store old and compute new display mode
405 InsetMathMacro::DisplayMode newDisplayMode;
406 InsetMathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
407 newDisplayMode = macroInset->computeDisplayMode();
409 // arity changed or other reason to detach?
410 if (oldDisplayMode == InsetMathMacro::DISPLAY_NORMAL
411 && (macroInset->arity() != macroNumArgs
412 || macroInset->optionals() != macroOptionals
413 || newDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED))
414 detachMacroParameters(cur, i);
416 // the macro could have been copied while resizing this
417 macroInset = operator[](i).nucleus()->asMacro();
420 if (newDisplayMode != InsetMathMacro::DISPLAY_UNFOLDED
421 && oldDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED) {
422 // put cursor in front of macro
424 int macroSlice = cur->find(macroInset);
425 if (macroSlice != -1)
426 cur->cutOff(macroSlice - 1);
430 // update the display mode
431 size_t appetite = macroInset->appetite();
432 macroInset->setDisplayMode(newDisplayMode);
435 if (newDisplayMode == InsetMathMacro::DISPLAY_NORMAL
436 && (macroInset->arity() != macroNumArgs
437 || macroInset->optionals() != macroOptionals)) {
438 // is it a virgin macro which was never attached to parameters?
439 bool fromInitToNormalMode
440 = (oldDisplayMode == InsetMathMacro::DISPLAY_INIT
441 || oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT)
442 && newDisplayMode == InsetMathMacro::DISPLAY_NORMAL;
444 // if the macro was entered interactively (i.e. not by paste or during
445 // loading), it should not be greedy, but the cursor should
446 // automatically jump into the macro when behind
447 bool interactive = (oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT);
450 attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
451 fromInitToNormalMode, interactive, appetite);
454 cur->updateInsets(&cur->bottom().inset());
457 // Give macro the chance to adapt to new situation.
458 // The macroInset could be invalid now because it was put into a script
459 // inset and therefore "deep" copied. So get it again from the MathData.
460 InsetMath * inset = operator[](i).nucleus();
461 if (inset->asScriptInset())
462 inset = inset->asScriptInset()->nuc()[0].nucleus();
463 LASSERT(inset->asMacro(), continue);
464 inset->asMacro()->updateRepresentation(cur, mc, utype, nesting + 1);
469 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
471 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
472 // We store this now, because the inset pointer will be invalidated in the scond loop below
473 size_t const optionals = macroInset->optionals();
475 // detach all arguments
476 vector<MathData> detachedArgs;
477 if (macroPos + 1 == size())
478 // strip arguments if we are at the MathData end
479 macroInset->detachArguments(detachedArgs, true);
481 macroInset->detachArguments(detachedArgs, false);
484 int curMacroSlice = -1;
486 curMacroSlice = cur->find(macroInset);
487 idx_type curMacroIdx = -1;
488 pos_type curMacroPos = -1;
489 vector<CursorSlice> argSlices;
490 if (curMacroSlice != -1) {
491 curMacroPos = (*cur)[curMacroSlice].pos();
492 curMacroIdx = (*cur)[curMacroSlice].idx();
493 cur->cutOff(curMacroSlice, argSlices);
497 // only [] after the last non-empty argument can be dropped later
498 size_t lastNonEmptyOptional = 0;
499 for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
500 if (!detachedArgs[l].empty())
501 lastNonEmptyOptional = l;
504 // optional arguments to be put back?
505 pos_type p = macroPos + 1;
507 // We do not want to use macroInset below, the insert() call in
508 // the loop will invalidate it.
510 for (; j < detachedArgs.size() && j < optionals; ++j) {
511 // another non-empty parameter follows?
512 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
514 // then we can drop empty optional parameters
515 if (detachedArgs[j].empty() && canDropEmptyOptional) {
516 if (curMacroIdx == j)
517 (*cur)[curMacroSlice - 1].pos() = macroPos + 1;
521 // Otherwise we don't drop an empty optional, put it back normally
523 asArray(from_ascii("[]"), optarg);
524 MathData & arg = detachedArgs[j];
526 // look for "]", i.e. put a brace around?
527 InsetMathBrace * brace = 0;
528 for (size_t q = 0; q < arg.size(); ++q) {
529 if (arg[q]->getChar() == ']') {
531 brace = new InsetMathBrace(buffer_);
536 // put arg between []
538 brace->cell(0) = arg;
539 optarg.insert(1, MathAtom(brace));
541 optarg.insert(1, arg);
543 // insert it into the array
548 if (curMacroSlice == -1)
551 // cursor in optional argument of macro?
552 if (curMacroIdx == j) {
554 cur->append(0, curMacroPos);
555 (*cur)[curMacroSlice - 1].pos() = macroPos + 2;
557 (*cur)[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
558 cur->append(argSlices);
559 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
560 // cursor right of macro
561 (*cur)[curMacroSlice - 1].pos() += optarg.size();
564 // put them back into the MathData
565 for (; j < detachedArgs.size(); ++j, ++p) {
566 MathData const & arg = detachedArgs[j];
568 && !arg[0]->asScriptInset()
569 && !(arg[0]->asMacro() && arg[0]->asMacro()->arity() > 0))
572 insert(p, MathAtom(new InsetMathBrace(arg)));
575 if (curMacroSlice == -1)
578 // cursor in j-th argument of macro?
579 if (curMacroIdx == j) {
580 if (operator[](p).nucleus()->asBraceInset()) {
581 (*cur)[curMacroSlice - 1].pos() = p;
582 cur->append(0, curMacroPos);
583 cur->append(argSlices);
585 (*cur)[curMacroSlice - 1].pos() = p; // + macroPos;
586 cur->append(argSlices);
588 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
589 ++(*cur)[curMacroSlice - 1].pos();
593 cur->updateInsets(&cur->bottom().inset());
597 void MathData::attachMacroParameters(Cursor * cur,
598 const size_type macroPos, const size_type macroNumArgs,
599 const int macroOptionals, const bool fromInitToNormalMode,
600 const bool interactiveInit, const size_t appetite)
602 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
604 // start at atom behind the macro again, maybe with some new arguments
605 // from the detach phase above, to add them back into the macro inset
606 size_t p = macroPos + 1;
607 vector<MathData> detachedArgs;
608 MathAtom scriptToPutAround;
610 // find cursor slice again of this MathData
613 thisSlice = cur->find(*this);
616 thisPos = (*cur)[thisSlice].pos();
618 // find arguments behind the macro
619 if (!interactiveInit) {
620 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
621 scriptToPutAround, macroPos, thisPos, thisSlice);
623 collectParameters(cur, macroNumArgs, detachedArgs, p,
624 scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
626 // attach arguments back to macro inset
627 macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
629 // found tail script? E.g. \foo{a}b^x
630 if (scriptToPutAround.nucleus()) {
631 InsetMathScript * scriptInset =
632 scriptToPutAround.nucleus()->asScriptInset();
633 // In the math parser we remove empty braces in the base
634 // of a script inset, but we have to restore them here.
635 if (scriptInset->nuc().empty()) {
637 scriptInset->nuc().push_back(
638 MathAtom(new InsetMathBrace(ar)));
640 // put macro into a script inset
641 scriptInset->nuc()[0] = operator[](macroPos);
642 operator[](macroPos) = scriptToPutAround;
644 // go into the script inset nucleus
645 if (cur && thisPos == int(macroPos))
648 // get pointer to "deep" copied macro inset
649 scriptInset = operator[](macroPos).nucleus()->asScriptInset();
650 macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
653 // remove them from the MathData
654 erase(macroPos + 1, p);
656 // cursor outside this MathData?
660 // fix cursor if right of p
661 if (thisPos >= int(p))
662 (*cur)[thisSlice].pos() -= p - (macroPos + 1);
664 // was the macro inset just inserted interactively and was now folded
665 // and the cursor is just behind?
666 if ((*cur)[thisSlice].pos() == int(macroPos + 1)
668 && fromInitToNormalMode
669 && macroInset->arity() > 0
670 && thisSlice + 1 == int(cur->depth())) {
671 // then enter it if the cursor was just behind
672 (*cur)[thisSlice].pos() = macroPos;
673 cur->push_back(CursorSlice(*macroInset));
674 macroInset->idxFirst(*cur);
679 void MathData::collectOptionalParameters(Cursor * cur,
680 const size_type numOptionalParams, vector<MathData> & params,
681 size_t & pos, MathAtom & scriptToPutAround,
682 const pos_type macroPos, const int thisPos, const int thisSlice)
684 Buffer * buf = cur ? cur->buffer() : 0;
685 // insert optional arguments?
686 while (params.size() < numOptionalParams
688 && !scriptToPutAround.nucleus()) {
689 // is a [] block following which could be an optional parameter?
690 if (operator[](pos)->getChar() != '[')
693 // found possible optional argument, look for pairing "]"
695 size_t right = pos + 1;
696 for (; right < size(); ++right) {
697 MathAtom & cell = operator[](right);
699 if (cell->getChar() == '[')
701 else if (cell->getChar() == ']' && --count == 0)
705 // maybe "]" with a script around?
706 InsetMathScript * script = cell.nucleus()->asScriptInset();
709 if (script->nuc().size() != 1)
711 if (script->nuc()[0]->getChar() == ']') {
712 // script will be put around the macro later
713 scriptToPutAround = cell;
719 if (right >= size()) {
720 // no ] found, so it's not an optional argument
724 // add everything between [ and ] as optional argument
725 MathData optarg(buf, begin() + pos + 1, begin() + right);
729 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
731 params.push_back(optarg[0]->asBraceInset()->cell(0));
733 params.push_back(optarg);
735 // place cursor in optional argument of macro
736 // Note: The two expressions on the first line are equivalent
737 // (see caller), but making this explicit pleases coverity.
738 if (cur && thisSlice != -1
739 && thisPos >= int(pos) && thisPos <= int(right)) {
740 int paramPos = max(0, thisPos - int(pos) - 1);
741 vector<CursorSlice> x;
742 cur->cutOff(thisSlice, x);
743 (*cur)[thisSlice].pos() = macroPos;
745 paramPos = x[0].pos();
748 cur->append(0, paramPos);
754 // fill up empty optional parameters
755 while (params.size() < numOptionalParams)
756 params.push_back(MathData());
760 void MathData::collectParameters(Cursor * cur,
761 const size_type numParams, vector<MathData> & params,
762 size_t & pos, MathAtom & scriptToPutAround,
763 const pos_type macroPos, const int thisPos, const int thisSlice,
764 const size_t appetite)
766 size_t startSize = params.size();
768 // insert normal arguments
769 while (params.size() < numParams
770 && params.size() - startSize < appetite
772 && !scriptToPutAround.nucleus()) {
773 MathAtom & cell = operator[](pos);
776 vector<CursorSlice> argSlices;
778 // Note: The two expressions on the first line are equivalent
779 // (see caller), but making this explicit pleases coverity.
780 if (cur && thisSlice != -1
781 && thisPos == int(pos))
782 cur->cutOff(thisSlice, argSlices);
784 // which kind of parameter is it? In {}? With index x^n?
785 InsetMathBrace const * brace = cell->asBraceInset();
787 // found brace, convert into argument
788 params.push_back(brace->cell(0));
790 // cursor inside of the brace or just in front of?
791 if (thisPos == int(pos) && !argSlices.empty()) {
792 argPos = argSlices[0].pos();
793 argSlices.erase(argSlices.begin());
795 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
796 // last inset with scripts without braces
797 // -> they belong to the macro, not the argument
798 InsetMathScript * script = cell.nucleus()->asScriptInset();
799 if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
800 // nucleus in brace? Unpack!
801 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
803 params.push_back(script->nuc());
805 // script will be put around below
806 scriptToPutAround = cell;
808 // this should only happen after loading, so make cursor handling simple
809 if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
815 // the simplest case: plain inset
817 array.insert(0, cell);
818 params.push_back(array);
821 // put cursor in argument again
822 // Note: The first two expressions on the first line are
823 // equivalent (see caller), but making this explicit pleases
825 if (cur && thisSlice != -1 && thisPos == int(pos)) {
826 cur->append(params.size() - 1, argPos);
827 cur->append(argSlices);
828 (*cur)[thisSlice].pos() = macroPos;
836 int MathData::pos2x(BufferView const * bv, size_type pos) const
839 size_type target = min(pos, size());
840 CoordCache::Insets const & coords = bv->coordCache().getInsets();
841 for (size_type i = 0; i < target; ++i) {
842 const_iterator it = begin() + i;
843 //lyxerr << "char: " << (*it)->getChar()
844 // << "width: " << (*it)->width() << endl;
845 x += coords.dim((*it).nucleus()).wid;
851 MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
853 const_iterator it = begin();
856 CoordCache::Insets const & coords = bv->coordCache().getInsets();
857 // find first position after targetx
858 for (; currx < targetx && it != end(); ++it) {
860 currx += coords.dim((*it).nucleus()).wid;
864 * If we are not at the beginning of the array, go to the left
865 * of the inset if one of the following two condition holds:
866 * - the current inset is editable (so that the cursor tip is
867 * deeper than us): in this case, we want all intermediate
868 * cursor slices to be before insets;
869 * - the mouse is closer to the left side of the inset than to
871 * See bug 1918 for details.
873 if (it != begin() && currx >= targetx
874 && ((*prev(it, 1))->asNestInset()
875 || abs(lastx - targetx) < abs(currx - targetx))) {
883 int MathData::dist(BufferView const & bv, int x, int y) const
885 return bv.coordCache().getArrays().squareDistance(this, x, y);
889 void MathData::setXY(BufferView & bv, int x, int y) const
891 //lyxerr << "setting position cache for MathData " << this << endl;
892 bv.coordCache().arrays().add(this, x, y);
896 Dimension const & MathData::dimension(BufferView const & bv) const
898 return bv.coordCache().getArrays().dim(this);
902 int MathData::xm(BufferView const & bv) const
904 Geometry const & g = bv.coordCache().getArrays().geometry(this);
906 return g.pos.x_ + g.dim.wid / 2;
910 int MathData::ym(BufferView const & bv) const
912 Geometry const & g = bv.coordCache().getArrays().geometry(this);
914 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
918 int MathData::xo(BufferView const & bv) const
920 return bv.coordCache().getArrays().x(this);
924 int MathData::yo(BufferView const & bv) const
926 return bv.coordCache().getArrays().y(this);
930 MathClass MathData::mathClass() const
932 MathClass res = MC_UNKNOWN;
933 for (MathAtom const & at : *this) {
934 MathClass mc = at->mathClass();
935 if (res == MC_UNKNOWN)
937 else if (mc != MC_UNKNOWN && res != mc)
940 return res == MC_UNKNOWN ? MC_ORD : res;
944 ostream & operator<<(ostream & os, MathData const & ar)
946 odocstringstream oss;
947 NormalStream ns(oss);
949 return os << to_utf8(oss.str());
953 odocstream & operator<<(odocstream & os, MathData const & ar)