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)
57 void MathData::setContentsBuffer()
60 for (MathAtom & at : *this)
61 at.nucleus()->setBuffer(*buffer_);
65 void MathData::setBuffer(Buffer & b)
72 MathAtom & MathData::operator[](pos_type pos)
74 LBUFERR(pos < size());
75 return base_type::operator[](pos);
79 MathAtom const & MathData::operator[](pos_type pos) const
81 LBUFERR(pos < size());
82 return base_type::operator[](pos);
86 void MathData::insert(size_type pos, MathAtom const & t)
88 LBUFERR(pos <= size());
89 base_type::insert(begin() + pos, t);
91 operator[](pos)->setBuffer(*buffer_);
95 void MathData::insert(size_type pos, MathData const & ar)
97 LBUFERR(pos <= size());
98 base_type::insert(begin() + pos, ar.begin(), ar.end());
100 for (size_type i = 0 ; i < ar.size() ; ++i)
101 operator[](pos + i)->setBuffer(*buffer_);
105 void MathData::push_back(MathAtom const & t)
107 base_type::push_back(t);
109 back()->setBuffer(*buffer_);
113 void MathData::append(MathData const & ar)
119 void MathData::erase(size_type pos)
126 void MathData::erase(iterator pos1, iterator pos2)
128 base_type::erase(pos1, pos2);
132 void MathData::erase(iterator pos)
134 base_type::erase(pos);
138 void MathData::erase(size_type pos1, size_type pos2)
140 base_type::erase(begin() + pos1, begin() + pos2);
144 void MathData::dump2() const
148 for (const_iterator it = begin(); it != end(); ++it)
150 lyxerr << to_utf8(os.str());
154 void MathData::dump() const
158 for (const_iterator it = begin(); it != end(); ++it)
159 ns << '<' << *it << '>';
160 lyxerr << to_utf8(os.str());
164 void MathData::validate(LaTeXFeatures & features) const
166 for (const_iterator it = begin(); it != end(); ++it)
167 (*it)->validate(features);
171 bool MathData::match(MathData const & ar) const
173 return size() == ar.size() && matchpart(ar, 0);
177 bool MathData::matchpart(MathData const & ar, pos_type pos) const
179 if (size() < ar.size() + pos)
181 const_iterator it = begin() + pos;
182 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
183 if (asString(*it) != asString(*jt))
189 void MathData::replace(ReplaceData & rep)
191 for (size_type i = 0; i < size(); ++i) {
192 if (find1(rep.from, i)) {
194 lyxerr << "match found!" << endl;
195 erase(i, i + rep.from.size());
200 // FIXME: temporarily disabled
201 // for (const_iterator it = begin(); it != end(); ++it)
202 // it->nucleus()->replace(rep);
206 bool MathData::find1(MathData const & ar, size_type pos) const
208 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
209 for (size_type i = 0, n = ar.size(); i < n; ++i)
210 if (asString(operator[](pos + i)) != asString(ar[i]))
216 MathData::size_type MathData::find(MathData const & ar) const
218 for (int i = 0, last = size() - ar.size(); i < last; ++i)
225 MathData::size_type MathData::find_last(MathData const & ar) const
227 for (int i = size() - ar.size(); i >= 0; --i)
234 bool MathData::contains(MathData const & ar) const
236 if (find(ar) != size())
238 for (const_iterator it = begin(); it != end(); ++it)
239 if ((*it)->contains(ar))
245 bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
247 bool has_contents = false;
248 BufferView * bv = mi.base.bv;
249 display_style_ = mi.base.font.style() == DISPLAY_STYLE;
250 MathData * ar = const_cast<MathData*>(this);
251 ar->updateMacros(&bv->cursor(), mi.macrocontext,
252 InternalUpdate, mi.base.macro_nesting);
254 pos_type bspos = -1, espos = -1;
255 Cursor const & cur = bv->cursor();
256 InsetMath const * inset = cur.inset().asInsetMath();
257 if (cur.selection() && inset) {
258 CursorSlice const s1 = cur.selBegin();
259 CursorSlice const s2 = cur.selEnd();
260 // Detect inner selection in this math data.
261 if (s1.idx() == s2.idx() && &inset->cell(s1.idx()) == this) {
264 } else if (s1.idx() != s2.idx()) {
265 // search for this math data and check whether it is selected
266 for (idx_type idx = 0; idx < inset->nargs(); ++idx) {
267 MathData const & c = inset->cell(idx);
268 if (&c == this && inset->idxBetween(idx, s1.idx(), s2.idx())) {
269 // whole cell is selected
272 // no need to continue searchning
279 // FIXME: for completion, try to insert the relevant data in the
280 // mathrow (like is done for text rows). We could add a pair of
281 // InsetMathColor inset, but these come with extra spacing of
283 DocIterator const & inlineCompletionPos = bv->inlineCompletionPos();
284 bool const has_completion = inlineCompletionPos.inMathed()
285 && &inlineCompletionPos.cell() == this;
286 size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
288 for (size_t i = 0 ; i < size() ; ++i) {
290 mrow.push_back(MathRow::Element(mi, MathRow::BEGIN_SEL));
291 has_contents |= (*this)[i]->addToMathRow(mrow, mi);
292 if (i + 1 == compl_pos) {
293 mrow.back().compl_text = bv->inlineCompletion();
294 mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
297 mrow.push_back(MathRow::Element(mi, MathRow::END_SEL));
306 bool isInside(DocIterator const & it, MathData const & ar,
307 pos_type p1, pos_type p2)
309 for (size_t i = 0; i != it.depth(); ++i) {
310 CursorSlice const & sl = it[i];
311 if (sl.inset().inMathed() && &sl.cell() == &ar)
312 return p1 <= sl.pos() && sl.pos() < p2;
321 void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const
323 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
324 BufferView * bv = mi.base.bv;
325 int const Iascent = fm.dimension('I').ascent();
326 int xascent = fm.xHeight();
327 if (xascent >= Iascent)
328 xascent = (2 * Iascent) / 3;
330 mindes_ = (3 * xascent) / 4;
331 slevel_ = (4 * xascent) / 5;
332 sshift_ = xascent / 4;
334 MathRow mrow(mi, this);
335 mrow.metrics(mi, dim);
337 // Set a minimal ascent/descent for the cell
339 // FIXME: this is the minimal ascent seen empirically, check
340 // what the TeXbook says.
341 dim.asc = max(dim.asc, fm.xHeight());
343 dim.asc = max(dim.asc, fm.maxAscent());
344 dim.des = max(dim.des, fm.maxDescent());
347 // This is one of the the few points where the drawing font is known,
348 // so that we can set the caret vertical dimensions.
349 mrow.caret_dim.asc = min(dim.asc, fm.maxAscent());
350 mrow.caret_dim.des = min(dim.des, fm.maxDescent());
351 mrow.caret_dim.wid = max(fm.lineWidth(), 1);
353 /// do the same for math cells linearized in the row
354 MathRow caret_row = MathRow(mrow.caret_dim);
355 for (auto const & e : mrow)
356 if (e.type == MathRow::BEGIN && e.ar)
357 bv->setMathRow(e.ar, caret_row);
359 // Cache row and dimension.
360 bv->setMathRow(this, mrow);
361 bv->coordCache().arrays().add(this, dim);
365 void MathData::drawSelection(PainterInfo & pi, int const x, int const y) const
367 BufferView const * bv = pi.base.bv;
368 Cursor const & cur = bv->cursor();
369 InsetMath const * inset = cur.inset().asInsetMath();
370 if (!cur.selection() || !inset || inset->nargs() == 0)
373 CursorSlice const s1 = cur.selBegin();
374 CursorSlice const s2 = cur.selEnd();
375 MathData const & c1 = inset->cell(s1.idx());
377 if (s1.idx() == s2.idx() && &c1 == this) {
378 // selection inside cell
379 Dimension const dim = bv->coordCache().getArrays().dim(&c1);
380 int const beg = c1.pos2x(bv, s1.pos());
381 int const end = c1.pos2x(bv, s2.pos());
382 pi.pain.fillRectangle(x + beg, y - dim.ascent(),
383 end - beg, dim.height(), Color_selection);
385 for (idx_type i = 0; i < inset->nargs(); ++i) {
386 MathData const & c = inset->cell(i);
387 if (&c == this && inset->idxBetween(i, s1.idx(), s2.idx())) {
388 // The whole cell is selected
389 Dimension const dim = bv->coordCache().getArrays().dim(&c);
390 pi.pain.fillRectangle(x, y - dim.ascent(),
391 dim.width(), dim.height(),
399 void MathData::draw(PainterInfo & pi, int const x, int const y) const
401 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
402 setXY(*pi.base.bv, x, y);
404 drawSelection(pi, x, y);
405 MathRow const & mrow = pi.base.bv->mathRow(this);
410 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
414 for (const_iterator it = begin(); it != end(); ++it) {
415 (*it)->metricsT(mi, d);
421 void MathData::drawT(TextPainter & pain, int x, int y) const
423 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
425 // FIXME: Abdel 16/10/2006
426 // This drawT() method is never used, this is dead code.
428 for (auto const & it : *this) {
429 it->drawT(pain, x, y);
436 int MathData::kerning(BufferView const * bv) const
438 return bv->mathRow(this).kerning(bv);
442 void MathData::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
445 for (size_t i = 0, n = size(); i != n; ++i) {
446 MathAtom & at = operator[](i);
447 at.nucleus()->updateBuffer(it, utype, deleted);
452 void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
453 UpdateType utype, int nesting)
455 // If we are editing a macro, we cannot update it immediately,
456 // otherwise wrong undo steps will be recorded (bug 6208).
457 InsetMath const * inmath = cur ? cur->inset().asInsetMath() : 0;
458 InsetMathMacro const * inmacro = inmath ? inmath->asMacro() : 0;
459 docstring const edited_name = inmacro ? inmacro->name() : docstring();
461 // go over the array and look for macros
462 for (size_t i = 0; i < size(); ++i) {
463 InsetMathMacro * macroInset = operator[](i).nucleus()->asMacro();
464 if (!macroInset || macroInset->macroName().empty()
465 || macroInset->macroName()[0] == '^'
466 || macroInset->macroName()[0] == '_'
467 || (macroInset->name() == edited_name
468 && macroInset->displayMode() ==
469 InsetMathMacro::DISPLAY_UNFOLDED))
473 macroInset->updateMacro(mc);
474 size_t macroNumArgs = 0;
475 size_t macroOptionals = 0;
476 MacroData const * macro = macroInset->macro();
478 macroNumArgs = macro->numargs();
479 macroOptionals = macro->optionals();
482 // store old and compute new display mode
483 InsetMathMacro::DisplayMode newDisplayMode;
484 InsetMathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
485 newDisplayMode = macroInset->computeDisplayMode();
487 // arity changed or other reason to detach?
488 if (oldDisplayMode == InsetMathMacro::DISPLAY_NORMAL
489 && (macroInset->arity() != macroNumArgs
490 || macroInset->optionals() != macroOptionals
491 || newDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED))
492 detachMacroParameters(cur, i);
494 // the macro could have been copied while resizing this
495 macroInset = operator[](i).nucleus()->asMacro();
498 if (newDisplayMode != InsetMathMacro::DISPLAY_UNFOLDED
499 && oldDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED) {
500 // put cursor in front of macro
502 int macroSlice = cur->find(macroInset);
503 if (macroSlice != -1)
504 cur->cutOff(macroSlice - 1);
508 // update the display mode
509 size_t appetite = macroInset->appetite();
510 macroInset->setDisplayMode(newDisplayMode);
513 if (newDisplayMode == InsetMathMacro::DISPLAY_NORMAL
514 && (macroInset->arity() != macroNumArgs
515 || macroInset->optionals() != macroOptionals)) {
516 // is it a virgin macro which was never attached to parameters?
517 bool fromInitToNormalMode
518 = (oldDisplayMode == InsetMathMacro::DISPLAY_INIT
519 || oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT)
520 && newDisplayMode == InsetMathMacro::DISPLAY_NORMAL;
522 // if the macro was entered interactively (i.e. not by paste or during
523 // loading), it should not be greedy, but the cursor should
524 // automatically jump into the macro when behind
525 bool interactive = (oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT);
528 attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
529 fromInitToNormalMode, interactive, appetite);
532 cur->updateInsets(&cur->bottom().inset());
535 // Give macro the chance to adapt to new situation.
536 // The macroInset could be invalid now because it was put into a script
537 // inset and therefore "deep" copied. So get it again from the MathData.
538 InsetMath * inset = operator[](i).nucleus();
539 if (inset->asScriptInset())
540 inset = inset->asScriptInset()->nuc()[0].nucleus();
541 LASSERT(inset->asMacro(), continue);
542 inset->asMacro()->updateRepresentation(cur, mc, utype, nesting + 1);
547 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
549 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
550 // We store this now, because the inset pointer will be invalidated in the scond loop below
551 size_t const optionals = macroInset->optionals();
553 // detach all arguments
554 vector<MathData> detachedArgs;
555 if (macroPos + 1 == size())
556 // strip arguments if we are at the MathData end
557 macroInset->detachArguments(detachedArgs, true);
559 macroInset->detachArguments(detachedArgs, false);
562 int curMacroSlice = -1;
564 curMacroSlice = cur->find(macroInset);
565 idx_type curMacroIdx = -1;
566 pos_type curMacroPos = -1;
567 vector<CursorSlice> argSlices;
568 if (curMacroSlice != -1) {
569 curMacroPos = (*cur)[curMacroSlice].pos();
570 curMacroIdx = (*cur)[curMacroSlice].idx();
571 cur->cutOff(curMacroSlice, argSlices);
575 // only [] after the last non-empty argument can be dropped later
576 size_t lastNonEmptyOptional = 0;
577 for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
578 if (!detachedArgs[l].empty())
579 lastNonEmptyOptional = l;
582 // optional arguments to be put back?
583 pos_type p = macroPos + 1;
585 // We do not want to use macroInset below, the insert() call in
586 // the loop will invalidate it.
588 for (; j < detachedArgs.size() && j < optionals; ++j) {
589 // another non-empty parameter follows?
590 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
592 // then we can drop empty optional parameters
593 if (detachedArgs[j].empty() && canDropEmptyOptional) {
594 if (curMacroIdx == j)
595 (*cur)[curMacroSlice - 1].pos() = macroPos + 1;
599 // Otherwise we don't drop an empty optional, put it back normally
600 MathData optarg(buffer_);
601 asArray(from_ascii("[]"), optarg);
602 MathData & arg = detachedArgs[j];
604 // look for "]", i.e. put a brace around?
605 InsetMathBrace * brace = 0;
606 for (size_t q = 0; q < arg.size(); ++q) {
607 if (arg[q]->getChar() == ']') {
609 brace = new InsetMathBrace(buffer_);
614 // put arg between []
616 brace->cell(0) = arg;
617 optarg.insert(1, MathAtom(brace));
619 optarg.insert(1, arg);
621 // insert it into the array
626 if (curMacroSlice == -1)
629 // cursor in optional argument of macro?
630 if (curMacroIdx == j) {
632 cur->append(0, curMacroPos);
633 (*cur)[curMacroSlice - 1].pos() = macroPos + 2;
635 (*cur)[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
636 cur->append(argSlices);
637 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
638 // cursor right of macro
639 (*cur)[curMacroSlice - 1].pos() += optarg.size();
642 // put them back into the MathData
643 for (; j < detachedArgs.size(); ++j, ++p) {
644 MathData const & arg = detachedArgs[j];
646 && !arg[0]->asScriptInset()
647 && !(arg[0]->asMacro() && arg[0]->asMacro()->arity() > 0))
650 insert(p, MathAtom(new InsetMathBrace(buffer_, arg)));
653 if (curMacroSlice == -1)
656 // cursor in j-th argument of macro?
657 if (curMacroIdx == j) {
658 if (operator[](p).nucleus()->asBraceInset()) {
659 (*cur)[curMacroSlice - 1].pos() = p;
660 cur->append(0, curMacroPos);
661 cur->append(argSlices);
663 (*cur)[curMacroSlice - 1].pos() = p; // + macroPos;
664 cur->append(argSlices);
666 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
667 ++(*cur)[curMacroSlice - 1].pos();
671 cur->updateInsets(&cur->bottom().inset());
675 void MathData::attachMacroParameters(Cursor * cur,
676 const size_type macroPos, const size_type macroNumArgs,
677 const int macroOptionals, const bool fromInitToNormalMode,
678 const bool interactiveInit, const size_t appetite)
680 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
682 // start at atom behind the macro again, maybe with some new arguments
683 // from the detach phase above, to add them back into the macro inset
684 size_t p = macroPos + 1;
685 vector<MathData> detachedArgs;
686 MathAtom scriptToPutAround;
688 // find cursor slice again of this MathData
691 thisSlice = cur->find(*this);
694 thisPos = (*cur)[thisSlice].pos();
696 // find arguments behind the macro
697 if (!interactiveInit) {
698 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
699 scriptToPutAround, macroPos, thisPos, thisSlice);
701 collectParameters(cur, macroNumArgs, detachedArgs, p,
702 scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
704 // attach arguments back to macro inset
705 macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
707 // found tail script? E.g. \foo{a}b^x
708 if (scriptToPutAround.nucleus()) {
709 InsetMathScript * scriptInset =
710 scriptToPutAround.nucleus()->asScriptInset();
711 // In the math parser we remove empty braces in the base
712 // of a script inset, but we have to restore them here.
713 if (scriptInset->nuc().empty()) {
714 MathData ar(buffer_);
715 scriptInset->nuc().push_back(
716 MathAtom(new InsetMathBrace(buffer_, ar)));
718 // put macro into a script inset
719 scriptInset->nuc()[0] = operator[](macroPos);
720 operator[](macroPos) = scriptToPutAround;
722 // go into the script inset nucleus
723 if (cur && thisPos == int(macroPos))
726 // get pointer to "deep" copied macro inset
727 scriptInset = operator[](macroPos).nucleus()->asScriptInset();
728 macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
731 // remove them from the MathData
732 erase(macroPos + 1, p);
734 // cursor outside this MathData?
738 // fix cursor if right of p
739 if (thisPos >= int(p))
740 (*cur)[thisSlice].pos() -= p - (macroPos + 1);
742 // was the macro inset just inserted interactively and was now folded
743 // and the cursor is just behind?
744 if ((*cur)[thisSlice].pos() == int(macroPos + 1)
746 && fromInitToNormalMode
747 && macroInset->arity() > 0
748 && thisSlice + 1 == int(cur->depth())) {
749 // then enter it if the cursor was just behind
750 (*cur)[thisSlice].pos() = macroPos;
751 cur->push_back(CursorSlice(*macroInset));
752 macroInset->idxFirst(*cur);
757 void MathData::collectOptionalParameters(Cursor * cur,
758 const size_type numOptionalParams, vector<MathData> & params,
759 size_t & pos, MathAtom & scriptToPutAround,
760 const pos_type macroPos, const int thisPos, const int thisSlice)
762 Buffer * buf = cur ? cur->buffer() : 0;
763 // insert optional arguments?
764 while (params.size() < numOptionalParams
766 && !scriptToPutAround.nucleus()) {
767 // is a [] block following which could be an optional parameter?
768 if (operator[](pos)->getChar() != '[')
771 // found possible optional argument, look for pairing "]"
773 size_t right = pos + 1;
774 for (; right < size(); ++right) {
775 MathAtom & cell = operator[](right);
777 if (cell->getChar() == '[')
779 else if (cell->getChar() == ']' && --count == 0)
783 // maybe "]" with a script around?
784 InsetMathScript * script = cell.nucleus()->asScriptInset();
787 if (script->nuc().size() != 1)
789 if (script->nuc()[0]->getChar() == ']') {
790 // script will be put around the macro later
791 scriptToPutAround = cell;
797 if (right >= size()) {
798 // no ] found, so it's not an optional argument
802 // add everything between [ and ] as optional argument
803 MathData optarg(buf, begin() + pos + 1, begin() + right);
807 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
809 params.push_back(optarg[0]->asBraceInset()->cell(0));
811 params.push_back(optarg);
813 // place cursor in optional argument of macro
814 // Note: The two expressions on the first line are equivalent
815 // (see caller), but making this explicit pleases coverity.
816 if (cur && thisSlice != -1
817 && thisPos >= int(pos) && thisPos <= int(right)) {
818 int paramPos = max(0, thisPos - int(pos) - 1);
819 vector<CursorSlice> x;
820 cur->cutOff(thisSlice, x);
821 (*cur)[thisSlice].pos() = macroPos;
823 paramPos = x[0].pos();
826 cur->append(0, paramPos);
832 // fill up empty optional parameters
833 while (params.size() < numOptionalParams)
834 params.push_back(MathData(buffer_));
838 void MathData::collectParameters(Cursor * cur,
839 const size_type numParams, vector<MathData> & params,
840 size_t & pos, MathAtom & scriptToPutAround,
841 const pos_type macroPos, const int thisPos, const int thisSlice,
842 const size_t appetite)
844 size_t startSize = params.size();
846 // insert normal arguments
847 while (params.size() < numParams
848 && params.size() - startSize < appetite
850 && !scriptToPutAround.nucleus()) {
851 MathAtom & cell = operator[](pos);
854 vector<CursorSlice> argSlices;
856 // Note: The two expressions on the first line are equivalent
857 // (see caller), but making this explicit pleases coverity.
858 if (cur && thisSlice != -1
859 && thisPos == int(pos))
860 cur->cutOff(thisSlice, argSlices);
862 // which kind of parameter is it? In {}? With index x^n?
863 InsetMathBrace const * brace = cell->asBraceInset();
865 // found brace, convert into argument
866 params.push_back(brace->cell(0));
868 // cursor inside of the brace or just in front of?
869 if (thisPos == int(pos) && !argSlices.empty()) {
870 argPos = argSlices[0].pos();
871 argSlices.erase(argSlices.begin());
873 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
874 // last inset with scripts without braces
875 // -> they belong to the macro, not the argument
876 InsetMathScript * script = cell.nucleus()->asScriptInset();
877 if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
878 // nucleus in brace? Unpack!
879 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
881 params.push_back(script->nuc());
883 // script will be put around below
884 scriptToPutAround = cell;
886 // this should only happen after loading, so make cursor handling simple
887 if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
893 // the simplest case: plain inset
894 MathData array(buffer_);
895 array.insert(0, cell);
896 params.push_back(array);
899 // put cursor in argument again
900 // Note: The first two expressions on the first line are
901 // equivalent (see caller), but making this explicit pleases
903 if (cur && thisSlice != -1 && thisPos == int(pos)) {
904 cur->append(params.size() - 1, argPos);
905 cur->append(argSlices);
906 (*cur)[thisSlice].pos() = macroPos;
914 int MathData::pos2x(BufferView const * bv, size_type pos) const
917 size_type target = min(pos, size());
918 CoordCache::Insets const & coords = bv->coordCache().getInsets();
919 for (size_type i = 0; i < target; ++i) {
920 const_iterator it = begin() + i;
921 //lyxerr << "char: " << (*it)->getChar()
922 // << "width: " << (*it)->width() << endl;
923 x += coords.dim((*it).nucleus()).wid;
929 MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
931 const_iterator it = begin();
934 CoordCache::Insets const & coords = bv->coordCache().getInsets();
935 // find first position after targetx
936 for (; currx < targetx && it != end(); ++it) {
938 currx += coords.dim((*it).nucleus()).wid;
942 * If we are not at the beginning of the array, go to the left
943 * of the inset if one of the following two condition holds:
944 * - the current inset is editable (so that the cursor tip is
945 * deeper than us): in this case, we want all intermediate
946 * cursor slices to be before insets;
947 * - the mouse is closer to the left side of the inset than to
949 * See bug 1918 for details.
951 if (it != begin() && currx >= targetx
952 && ((*prev(it, 1))->asNestInset()
953 || abs(lastx - targetx) < abs(currx - targetx))) {
961 int MathData::dist(BufferView const & bv, int x, int y) const
963 return bv.coordCache().getArrays().squareDistance(this, x, y);
967 void MathData::setXY(BufferView & bv, int x, int y) const
969 //lyxerr << "setting position cache for MathData " << this << endl;
970 bv.coordCache().arrays().add(this, x, y);
974 Dimension const & MathData::dimension(BufferView const & bv) const
976 return bv.coordCache().getArrays().dim(this);
980 int MathData::xm(BufferView const & bv) const
982 Geometry const & g = bv.coordCache().getArrays().geometry(this);
984 return g.pos.x_ + g.dim.wid / 2;
988 int MathData::ym(BufferView const & bv) const
990 Geometry const & g = bv.coordCache().getArrays().geometry(this);
992 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
996 int MathData::xo(BufferView const & bv) const
998 return bv.coordCache().getArrays().x(this);
1002 int MathData::yo(BufferView const & bv) const
1004 return bv.coordCache().getArrays().y(this);
1008 MathClass MathData::mathClass() const
1010 MathClass res = MC_UNKNOWN;
1011 for (MathAtom const & at : *this) {
1012 MathClass mc = at->mathClass();
1013 if (res == MC_UNKNOWN)
1015 else if (mc != MC_UNKNOWN && res != mc)
1018 return res == MC_UNKNOWN ? MC_ORD : res;
1022 MathClass MathData::firstMathClass() const
1024 for (MathAtom const & at : *this) {
1025 MathClass mc = at->mathClass();
1026 if (mc != MC_UNKNOWN)
1033 MathClass MathData::lastMathClass() const
1035 MathClass res = MC_ORD;
1036 for (MathAtom const & at : *this) {
1037 MathClass mc = at->mathClass();
1038 if (mc != MC_UNKNOWN)
1045 ostream & operator<<(ostream & os, MathData const & ar)
1047 odocstringstream oss;
1048 NormalStream ns(oss);
1050 return os << to_utf8(oss.str());
1054 odocstream & operator<<(odocstream & os, MathData const & ar)
1056 NormalStream ns(os);