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 "InsetMathMacro.h"
19 #include "InsetMathScript.h"
20 #include "MacroTable.h"
22 #include "MathStream.h"
23 #include "MathSupport.h"
24 #include "MetricsInfo.h"
25 #include "ReplaceData.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
31 #include "Dimension.h"
33 #include "mathed/InsetMathUnknown.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
41 #include "support/lassert.h"
50 MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
51 : base_type(from, to), buffer_(buf)
55 void MathData::setBuffer(Buffer & b)
58 for (MathAtom & at : *this)
59 at.nucleus()->setBuffer(b);
63 MathAtom & MathData::operator[](pos_type pos)
65 LBUFERR(pos < size());
66 return base_type::operator[](pos);
70 MathAtom const & MathData::operator[](pos_type pos) const
72 LBUFERR(pos < size());
73 return base_type::operator[](pos);
77 void MathData::insert(size_type pos, MathAtom const & t)
79 LBUFERR(pos <= size());
80 base_type::insert(begin() + pos, t);
84 void MathData::insert(size_type pos, MathData const & ar)
86 LBUFERR(pos <= size());
87 base_type::insert(begin() + pos, ar.begin(), ar.end());
91 void MathData::append(MathData const & ar)
97 void MathData::erase(size_type pos)
104 void MathData::erase(iterator pos1, iterator pos2)
106 base_type::erase(pos1, pos2);
110 void MathData::erase(iterator pos)
112 base_type::erase(pos);
116 void MathData::erase(size_type pos1, size_type pos2)
118 base_type::erase(begin() + pos1, begin() + pos2);
122 void MathData::dump2() const
126 for (const_iterator it = begin(); it != end(); ++it)
128 lyxerr << to_utf8(os.str());
132 void MathData::dump() const
136 for (const_iterator it = begin(); it != end(); ++it)
137 ns << '<' << *it << '>';
138 lyxerr << to_utf8(os.str());
142 void MathData::validate(LaTeXFeatures & features) const
144 for (const_iterator it = begin(); it != end(); ++it)
145 (*it)->validate(features);
149 bool MathData::match(MathData const & ar) const
151 return size() == ar.size() && matchpart(ar, 0);
155 bool MathData::matchpart(MathData const & ar, pos_type pos) const
157 if (size() < ar.size() + pos)
159 const_iterator it = begin() + pos;
160 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
161 if (asString(*it) != asString(*jt))
167 void MathData::replace(ReplaceData & rep)
169 for (size_type i = 0; i < size(); ++i) {
170 if (find1(rep.from, i)) {
172 lyxerr << "match found!" << endl;
173 erase(i, i + rep.from.size());
178 // FIXME: temporarily disabled
179 // for (const_iterator it = begin(); it != end(); ++it)
180 // it->nucleus()->replace(rep);
184 bool MathData::find1(MathData const & ar, size_type pos) const
186 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
187 for (size_type i = 0, n = ar.size(); i < n; ++i)
188 if (asString(operator[](pos + i)) != asString(ar[i]))
194 MathData::size_type MathData::find(MathData const & ar) const
196 for (int i = 0, last = size() - ar.size(); i < last; ++i)
203 MathData::size_type MathData::find_last(MathData const & ar) const
205 for (int i = size() - ar.size(); i >= 0; --i)
212 bool MathData::contains(MathData const & ar) const
214 if (find(ar) != size())
216 for (const_iterator it = begin(); it != end(); ++it)
217 if ((*it)->contains(ar))
223 bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
225 bool has_contents = false;
226 BufferView * bv = mi.base.bv;
227 display_style_ = mi.base.font.style() == DISPLAY_STYLE;
228 MathData * ar = const_cast<MathData*>(this);
229 ar->updateMacros(&bv->cursor(), mi.macrocontext,
230 InternalUpdate, mi.base.macro_nesting);
232 pos_type bspos = -1, espos = -1;
233 Cursor const & cur = bv->cursor();
234 InsetMath const * inset = cur.inset().asInsetMath();
235 if (cur.selection() && inset) {
236 CursorSlice const s1 = cur.selBegin();
237 CursorSlice const s2 = cur.selEnd();
238 // Detect inner selection in this math data.
239 if (s1.idx() == s2.idx() && &inset->cell(s1.idx()) == this) {
245 // FIXME: for completion, try to insert the relevant data in the
246 // mathrow (like is done for text rows). We could add a pair of
247 // InsetMathColor inset, but these come with extra spacing of
249 DocIterator const & inlineCompletionPos = bv->inlineCompletionPos();
250 bool const has_completion = inlineCompletionPos.inMathed()
251 && &inlineCompletionPos.cell() == this;
252 size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
254 for (size_t i = 0 ; i < size() ; ++i) {
256 mrow.push_back(MathRow::Element(mi, MathRow::BEGIN_SEL));
257 has_contents |= (*this)[i]->addToMathRow(mrow, mi);
258 if (i + 1 == compl_pos) {
259 mrow.back().compl_text = bv->inlineCompletion();
260 mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
263 mrow.push_back(MathRow::Element(mi, MathRow::END_SEL));
272 bool isInside(DocIterator const & it, MathData const & ar,
273 pos_type p1, pos_type p2)
275 for (size_t i = 0; i != it.depth(); ++i) {
276 CursorSlice const & sl = it[i];
277 if (sl.inset().inMathed() && &sl.cell() == &ar)
278 return p1 <= sl.pos() && sl.pos() < p2;
287 void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const
289 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
290 BufferView * bv = mi.base.bv;
291 int const Iascent = fm.dimension('I').ascent();
292 int xascent = fm.xHeight();
293 if (xascent >= Iascent)
294 xascent = (2 * Iascent) / 3;
296 mindes_ = (3 * xascent) / 4;
297 slevel_ = (4 * xascent) / 5;
298 sshift_ = xascent / 4;
300 MathRow mrow(mi, this);
301 mrow.metrics(mi, dim);
303 // Set a minimal ascent/descent for the cell
305 // FIXME: this is the minimal ascent seen empirically, check
306 // what the TeXbook says.
307 dim.asc = max(dim.asc, fm.xHeight());
309 dim.asc = max(dim.asc, fm.maxAscent());
310 dim.des = max(dim.des, fm.maxDescent());
313 // This is one of the the few points where the drawing font is known,
314 // so that we can set the caret vertical dimensions.
315 mrow.caret_dim.asc = min(dim.asc, fm.maxAscent());
316 mrow.caret_dim.des = min(dim.des, fm.maxDescent());
317 mrow.caret_dim.wid = max(fm.lineWidth(), 1);
319 /// do the same for math cells linearized in the row
320 MathRow caret_row = MathRow(mrow.caret_dim);
321 for (auto const & e : mrow)
322 if (e.type == MathRow::BEGIN && e.ar)
323 bv->setMathRow(e.ar, caret_row);
325 // Cache row and dimension.
326 bv->setMathRow(this, mrow);
327 bv->coordCache().arrays().add(this, dim);
331 void MathData::drawSelection(PainterInfo & pi, int const x, int const y) const
333 BufferView const * bv = pi.base.bv;
334 Cursor const & cur = bv->cursor();
335 InsetMath const * inset = cur.inset().asInsetMath();
336 if (!cur.selection() || !inset || inset->nargs() == 0)
339 CursorSlice const s1 = cur.selBegin();
340 CursorSlice const s2 = cur.selEnd();
341 MathData const & c1 = inset->cell(s1.idx());
343 if (s1.idx() == s2.idx() && &c1 == this) {
344 // selection inside cell
345 Dimension const dim = bv->coordCache().getArrays().dim(&c1);
346 int const beg = c1.pos2x(bv, s1.pos());
347 int const end = c1.pos2x(bv, s2.pos());
348 pi.pain.fillRectangle(x + beg, y - dim.ascent(),
349 end - beg, dim.height(), Color_selection);
351 for (idx_type i = 0; i < inset->nargs(); ++i) {
352 MathData const & c = inset->cell(i);
353 if (&c == this && inset->idxBetween(i, s1.idx(), s2.idx())) {
354 // The whole cell is selected
355 Dimension const dim = bv->coordCache().getArrays().dim(&c);
356 pi.pain.fillRectangle(x, y - dim.ascent(),
357 dim.width(), dim.height(),
365 void MathData::draw(PainterInfo & pi, int const x, int const y) const
367 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
368 setXY(*pi.base.bv, x, y);
370 drawSelection(pi, x, y);
371 MathRow const & mrow = pi.base.bv->mathRow(this);
376 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
380 for (const_iterator it = begin(); it != end(); ++it) {
381 (*it)->metricsT(mi, d);
387 void MathData::drawT(TextPainter & pain, int x, int y) const
389 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
391 // FIXME: Abdel 16/10/2006
392 // This drawT() method is never used, this is dead code.
394 for (auto const & it : *this) {
395 it->drawT(pain, x, y);
402 int MathData::kerning(BufferView const * bv) const
404 return bv->mathRow(this).kerning(bv);
408 void MathData::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
411 for (size_t i = 0, n = size(); i != n; ++i) {
412 MathAtom & at = operator[](i);
413 at.nucleus()->updateBuffer(it, utype, deleted);
418 void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
419 UpdateType utype, int nesting)
421 // If we are editing a macro, we cannot update it immediately,
422 // otherwise wrong undo steps will be recorded (bug 6208).
423 InsetMath const * inmath = cur ? cur->inset().asInsetMath() : 0;
424 InsetMathMacro const * inmacro = inmath ? inmath->asMacro() : 0;
425 docstring const edited_name = inmacro ? inmacro->name() : docstring();
427 // go over the array and look for macros
428 for (size_t i = 0; i < size(); ++i) {
429 InsetMathMacro * macroInset = operator[](i).nucleus()->asMacro();
430 if (!macroInset || macroInset->macroName().empty()
431 || macroInset->macroName()[0] == '^'
432 || macroInset->macroName()[0] == '_'
433 || (macroInset->name() == edited_name
434 && macroInset->displayMode() ==
435 InsetMathMacro::DISPLAY_UNFOLDED))
439 macroInset->updateMacro(mc);
440 size_t macroNumArgs = 0;
441 size_t macroOptionals = 0;
442 MacroData const * macro = macroInset->macro();
444 macroNumArgs = macro->numargs();
445 macroOptionals = macro->optionals();
448 // store old and compute new display mode
449 InsetMathMacro::DisplayMode newDisplayMode;
450 InsetMathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
451 newDisplayMode = macroInset->computeDisplayMode();
453 // arity changed or other reason to detach?
454 if (oldDisplayMode == InsetMathMacro::DISPLAY_NORMAL
455 && (macroInset->arity() != macroNumArgs
456 || macroInset->optionals() != macroOptionals
457 || newDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED))
458 detachMacroParameters(cur, i);
460 // the macro could have been copied while resizing this
461 macroInset = operator[](i).nucleus()->asMacro();
464 if (newDisplayMode != InsetMathMacro::DISPLAY_UNFOLDED
465 && oldDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED) {
466 // put cursor in front of macro
468 int macroSlice = cur->find(macroInset);
469 if (macroSlice != -1)
470 cur->cutOff(macroSlice - 1);
474 // update the display mode
475 size_t appetite = macroInset->appetite();
476 macroInset->setDisplayMode(newDisplayMode);
479 if (newDisplayMode == InsetMathMacro::DISPLAY_NORMAL
480 && (macroInset->arity() != macroNumArgs
481 || macroInset->optionals() != macroOptionals)) {
482 // is it a virgin macro which was never attached to parameters?
483 bool fromInitToNormalMode
484 = (oldDisplayMode == InsetMathMacro::DISPLAY_INIT
485 || oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT)
486 && newDisplayMode == InsetMathMacro::DISPLAY_NORMAL;
488 // if the macro was entered interactively (i.e. not by paste or during
489 // loading), it should not be greedy, but the cursor should
490 // automatically jump into the macro when behind
491 bool interactive = (oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT);
494 attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
495 fromInitToNormalMode, interactive, appetite);
498 cur->updateInsets(&cur->bottom().inset());
501 // Give macro the chance to adapt to new situation.
502 // The macroInset could be invalid now because it was put into a script
503 // inset and therefore "deep" copied. So get it again from the MathData.
504 InsetMath * inset = operator[](i).nucleus();
505 if (inset->asScriptInset())
506 inset = inset->asScriptInset()->nuc()[0].nucleus();
507 LASSERT(inset->asMacro(), continue);
508 inset->asMacro()->updateRepresentation(cur, mc, utype, nesting + 1);
513 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
515 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
516 // We store this now, because the inset pointer will be invalidated in the scond loop below
517 size_t const optionals = macroInset->optionals();
519 // detach all arguments
520 vector<MathData> detachedArgs;
521 if (macroPos + 1 == size())
522 // strip arguments if we are at the MathData end
523 macroInset->detachArguments(detachedArgs, true);
525 macroInset->detachArguments(detachedArgs, false);
528 int curMacroSlice = -1;
530 curMacroSlice = cur->find(macroInset);
531 idx_type curMacroIdx = -1;
532 pos_type curMacroPos = -1;
533 vector<CursorSlice> argSlices;
534 if (curMacroSlice != -1) {
535 curMacroPos = (*cur)[curMacroSlice].pos();
536 curMacroIdx = (*cur)[curMacroSlice].idx();
537 cur->cutOff(curMacroSlice, argSlices);
541 // only [] after the last non-empty argument can be dropped later
542 size_t lastNonEmptyOptional = 0;
543 for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
544 if (!detachedArgs[l].empty())
545 lastNonEmptyOptional = l;
548 // optional arguments to be put back?
549 pos_type p = macroPos + 1;
551 // We do not want to use macroInset below, the insert() call in
552 // the loop will invalidate it.
554 for (; j < detachedArgs.size() && j < optionals; ++j) {
555 // another non-empty parameter follows?
556 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
558 // then we can drop empty optional parameters
559 if (detachedArgs[j].empty() && canDropEmptyOptional) {
560 if (curMacroIdx == j)
561 (*cur)[curMacroSlice - 1].pos() = macroPos + 1;
565 // Otherwise we don't drop an empty optional, put it back normally
567 asArray(from_ascii("[]"), optarg);
568 MathData & arg = detachedArgs[j];
570 // look for "]", i.e. put a brace around?
571 InsetMathBrace * brace = 0;
572 for (size_t q = 0; q < arg.size(); ++q) {
573 if (arg[q]->getChar() == ']') {
575 brace = new InsetMathBrace(buffer_);
580 // put arg between []
582 brace->cell(0) = arg;
583 optarg.insert(1, MathAtom(brace));
585 optarg.insert(1, arg);
587 // insert it into the array
592 if (curMacroSlice == -1)
595 // cursor in optional argument of macro?
596 if (curMacroIdx == j) {
598 cur->append(0, curMacroPos);
599 (*cur)[curMacroSlice - 1].pos() = macroPos + 2;
601 (*cur)[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
602 cur->append(argSlices);
603 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
604 // cursor right of macro
605 (*cur)[curMacroSlice - 1].pos() += optarg.size();
608 // put them back into the MathData
609 for (; j < detachedArgs.size(); ++j, ++p) {
610 MathData const & arg = detachedArgs[j];
612 && !arg[0]->asScriptInset()
613 && !(arg[0]->asMacro() && arg[0]->asMacro()->arity() > 0))
616 insert(p, MathAtom(new InsetMathBrace(arg)));
619 if (curMacroSlice == -1)
622 // cursor in j-th argument of macro?
623 if (curMacroIdx == j) {
624 if (operator[](p).nucleus()->asBraceInset()) {
625 (*cur)[curMacroSlice - 1].pos() = p;
626 cur->append(0, curMacroPos);
627 cur->append(argSlices);
629 (*cur)[curMacroSlice - 1].pos() = p; // + macroPos;
630 cur->append(argSlices);
632 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
633 ++(*cur)[curMacroSlice - 1].pos();
637 cur->updateInsets(&cur->bottom().inset());
641 void MathData::attachMacroParameters(Cursor * cur,
642 const size_type macroPos, const size_type macroNumArgs,
643 const int macroOptionals, const bool fromInitToNormalMode,
644 const bool interactiveInit, const size_t appetite)
646 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
648 // start at atom behind the macro again, maybe with some new arguments
649 // from the detach phase above, to add them back into the macro inset
650 size_t p = macroPos + 1;
651 vector<MathData> detachedArgs;
652 MathAtom scriptToPutAround;
654 // find cursor slice again of this MathData
657 thisSlice = cur->find(*this);
660 thisPos = (*cur)[thisSlice].pos();
662 // find arguments behind the macro
663 if (!interactiveInit) {
664 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
665 scriptToPutAround, macroPos, thisPos, thisSlice);
667 collectParameters(cur, macroNumArgs, detachedArgs, p,
668 scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
670 // attach arguments back to macro inset
671 macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
673 // found tail script? E.g. \foo{a}b^x
674 if (scriptToPutAround.nucleus()) {
675 InsetMathScript * scriptInset =
676 scriptToPutAround.nucleus()->asScriptInset();
677 // In the math parser we remove empty braces in the base
678 // of a script inset, but we have to restore them here.
679 if (scriptInset->nuc().empty()) {
681 scriptInset->nuc().push_back(
682 MathAtom(new InsetMathBrace(ar)));
684 // put macro into a script inset
685 scriptInset->nuc()[0] = operator[](macroPos);
686 operator[](macroPos) = scriptToPutAround;
688 // go into the script inset nucleus
689 if (cur && thisPos == int(macroPos))
692 // get pointer to "deep" copied macro inset
693 scriptInset = operator[](macroPos).nucleus()->asScriptInset();
694 macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
697 // remove them from the MathData
698 erase(macroPos + 1, p);
700 // cursor outside this MathData?
704 // fix cursor if right of p
705 if (thisPos >= int(p))
706 (*cur)[thisSlice].pos() -= p - (macroPos + 1);
708 // was the macro inset just inserted interactively and was now folded
709 // and the cursor is just behind?
710 if ((*cur)[thisSlice].pos() == int(macroPos + 1)
712 && fromInitToNormalMode
713 && macroInset->arity() > 0
714 && thisSlice + 1 == int(cur->depth())) {
715 // then enter it if the cursor was just behind
716 (*cur)[thisSlice].pos() = macroPos;
717 cur->push_back(CursorSlice(*macroInset));
718 macroInset->idxFirst(*cur);
723 void MathData::collectOptionalParameters(Cursor * cur,
724 const size_type numOptionalParams, vector<MathData> & params,
725 size_t & pos, MathAtom & scriptToPutAround,
726 const pos_type macroPos, const int thisPos, const int thisSlice)
728 Buffer * buf = cur ? cur->buffer() : 0;
729 // insert optional arguments?
730 while (params.size() < numOptionalParams
732 && !scriptToPutAround.nucleus()) {
733 // is a [] block following which could be an optional parameter?
734 if (operator[](pos)->getChar() != '[')
737 // found possible optional argument, look for pairing "]"
739 size_t right = pos + 1;
740 for (; right < size(); ++right) {
741 MathAtom & cell = operator[](right);
743 if (cell->getChar() == '[')
745 else if (cell->getChar() == ']' && --count == 0)
749 // maybe "]" with a script around?
750 InsetMathScript * script = cell.nucleus()->asScriptInset();
753 if (script->nuc().size() != 1)
755 if (script->nuc()[0]->getChar() == ']') {
756 // script will be put around the macro later
757 scriptToPutAround = cell;
763 if (right >= size()) {
764 // no ] found, so it's not an optional argument
768 // add everything between [ and ] as optional argument
769 MathData optarg(buf, begin() + pos + 1, begin() + right);
773 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
775 params.push_back(optarg[0]->asBraceInset()->cell(0));
777 params.push_back(optarg);
779 // place cursor in optional argument of macro
780 // Note: The two expressions on the first line are equivalent
781 // (see caller), but making this explicit pleases coverity.
782 if (cur && thisSlice != -1
783 && thisPos >= int(pos) && thisPos <= int(right)) {
784 int paramPos = max(0, thisPos - int(pos) - 1);
785 vector<CursorSlice> x;
786 cur->cutOff(thisSlice, x);
787 (*cur)[thisSlice].pos() = macroPos;
789 paramPos = x[0].pos();
792 cur->append(0, paramPos);
798 // fill up empty optional parameters
799 while (params.size() < numOptionalParams)
800 params.push_back(MathData());
804 void MathData::collectParameters(Cursor * cur,
805 const size_type numParams, vector<MathData> & params,
806 size_t & pos, MathAtom & scriptToPutAround,
807 const pos_type macroPos, const int thisPos, const int thisSlice,
808 const size_t appetite)
810 size_t startSize = params.size();
812 // insert normal arguments
813 while (params.size() < numParams
814 && params.size() - startSize < appetite
816 && !scriptToPutAround.nucleus()) {
817 MathAtom & cell = operator[](pos);
820 vector<CursorSlice> argSlices;
822 // Note: The two expressions on the first line are equivalent
823 // (see caller), but making this explicit pleases coverity.
824 if (cur && thisSlice != -1
825 && thisPos == int(pos))
826 cur->cutOff(thisSlice, argSlices);
828 // which kind of parameter is it? In {}? With index x^n?
829 InsetMathBrace const * brace = cell->asBraceInset();
831 // found brace, convert into argument
832 params.push_back(brace->cell(0));
834 // cursor inside of the brace or just in front of?
835 if (thisPos == int(pos) && !argSlices.empty()) {
836 argPos = argSlices[0].pos();
837 argSlices.erase(argSlices.begin());
839 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
840 // last inset with scripts without braces
841 // -> they belong to the macro, not the argument
842 InsetMathScript * script = cell.nucleus()->asScriptInset();
843 if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
844 // nucleus in brace? Unpack!
845 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
847 params.push_back(script->nuc());
849 // script will be put around below
850 scriptToPutAround = cell;
852 // this should only happen after loading, so make cursor handling simple
853 if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
859 // the simplest case: plain inset
861 array.insert(0, cell);
862 params.push_back(array);
865 // put cursor in argument again
866 // Note: The first two expressions on the first line are
867 // equivalent (see caller), but making this explicit pleases
869 if (cur && thisSlice != -1 && thisPos == int(pos)) {
870 cur->append(params.size() - 1, argPos);
871 cur->append(argSlices);
872 (*cur)[thisSlice].pos() = macroPos;
880 int MathData::pos2x(BufferView const * bv, size_type pos) const
883 size_type target = min(pos, size());
884 CoordCache::Insets const & coords = bv->coordCache().getInsets();
885 for (size_type i = 0; i < target; ++i) {
886 const_iterator it = begin() + i;
887 //lyxerr << "char: " << (*it)->getChar()
888 // << "width: " << (*it)->width() << endl;
889 x += coords.dim((*it).nucleus()).wid;
895 MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
897 const_iterator it = begin();
900 CoordCache::Insets const & coords = bv->coordCache().getInsets();
901 // find first position after targetx
902 for (; currx < targetx && it != end(); ++it) {
904 currx += coords.dim((*it).nucleus()).wid;
908 * If we are not at the beginning of the array, go to the left
909 * of the inset if one of the following two condition holds:
910 * - the current inset is editable (so that the cursor tip is
911 * deeper than us): in this case, we want all intermediate
912 * cursor slices to be before insets;
913 * - the mouse is closer to the left side of the inset than to
915 * See bug 1918 for details.
917 if (it != begin() && currx >= targetx
918 && ((*prev(it, 1))->asNestInset()
919 || abs(lastx - targetx) < abs(currx - targetx))) {
927 int MathData::dist(BufferView const & bv, int x, int y) const
929 return bv.coordCache().getArrays().squareDistance(this, x, y);
933 void MathData::setXY(BufferView & bv, int x, int y) const
935 //lyxerr << "setting position cache for MathData " << this << endl;
936 bv.coordCache().arrays().add(this, x, y);
940 Dimension const & MathData::dimension(BufferView const & bv) const
942 return bv.coordCache().getArrays().dim(this);
946 int MathData::xm(BufferView const & bv) const
948 Geometry const & g = bv.coordCache().getArrays().geometry(this);
950 return g.pos.x_ + g.dim.wid / 2;
954 int MathData::ym(BufferView const & bv) const
956 Geometry const & g = bv.coordCache().getArrays().geometry(this);
958 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
962 int MathData::xo(BufferView const & bv) const
964 return bv.coordCache().getArrays().x(this);
968 int MathData::yo(BufferView const & bv) const
970 return bv.coordCache().getArrays().y(this);
974 MathClass MathData::mathClass() const
976 MathClass res = MC_UNKNOWN;
977 for (MathAtom const & at : *this) {
978 MathClass mc = at->mathClass();
979 if (res == MC_UNKNOWN)
981 else if (mc != MC_UNKNOWN && res != mc)
984 return res == MC_UNKNOWN ? MC_ORD : res;
988 MathClass MathData::firstMathClass() const
990 for (MathAtom const & at : *this) {
991 MathClass mc = at->mathClass();
992 if (mc != MC_UNKNOWN)
999 MathClass MathData::lastMathClass() const
1001 MathClass res = MC_ORD;
1002 for (MathAtom const & at : *this) {
1003 MathClass mc = at->mathClass();
1004 if (mc != MC_UNKNOWN)
1011 ostream & operator<<(ostream & os, MathData const & ar)
1013 odocstringstream oss;
1014 NormalStream ns(oss);
1016 return os << to_utf8(oss.str());
1020 odocstream & operator<<(odocstream & os, MathData const & ar)
1022 NormalStream ns(os);