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 "MathMacro.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"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lassert.h"
39 #include "support/lyxalgo.h"
48 MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
49 : base_type(from, to), minasc_(0), mindes_(0), slevel_(0),
50 sshift_(0), kerning_(0), buffer_(buf)
54 MathAtom & MathData::operator[](pos_type pos)
56 LBUFERR(pos < size());
57 return base_type::operator[](pos);
61 MathAtom const & MathData::operator[](pos_type pos) const
63 LBUFERR(pos < size());
64 return base_type::operator[](pos);
68 void MathData::insert(size_type pos, MathAtom const & t)
70 LBUFERR(pos <= size());
71 base_type::insert(begin() + pos, t);
75 void MathData::insert(size_type pos, MathData const & ar)
77 LBUFERR(pos <= size());
78 base_type::insert(begin() + pos, ar.begin(), ar.end());
82 void MathData::append(MathData const & ar)
88 void MathData::erase(size_type pos)
95 void MathData::erase(iterator pos1, iterator pos2)
97 base_type::erase(pos1, pos2);
101 void MathData::erase(iterator pos)
103 base_type::erase(pos);
107 void MathData::erase(size_type pos1, size_type pos2)
109 base_type::erase(begin() + pos1, begin() + pos2);
113 void MathData::dump2() const
117 for (const_iterator it = begin(); it != end(); ++it)
119 lyxerr << to_utf8(os.str());
123 void MathData::dump() const
127 for (const_iterator it = begin(); it != end(); ++it)
128 ns << '<' << *it << '>';
129 lyxerr << to_utf8(os.str());
133 void MathData::validate(LaTeXFeatures & features) const
135 for (const_iterator it = begin(); it != end(); ++it)
136 (*it)->validate(features);
140 bool MathData::match(MathData const & ar) const
142 return size() == ar.size() && matchpart(ar, 0);
146 bool MathData::matchpart(MathData const & ar, pos_type pos) const
148 if (size() < ar.size() + pos)
150 const_iterator it = begin() + pos;
151 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
152 if (asString(*it) != asString(*jt))
158 void MathData::replace(ReplaceData & rep)
160 for (size_type i = 0; i < size(); ++i) {
161 if (find1(rep.from, i)) {
163 lyxerr << "match found!" << endl;
164 erase(i, i + rep.from.size());
169 // FIXME: temporarily disabled
170 // for (const_iterator it = begin(); it != end(); ++it)
171 // it->nucleus()->replace(rep);
175 bool MathData::find1(MathData const & ar, size_type pos) const
177 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
178 for (size_type i = 0, n = ar.size(); i < n; ++i)
179 if (asString(operator[](pos + i)) != asString(ar[i]))
185 MathData::size_type MathData::find(MathData const & ar) const
187 for (int i = 0, last = size() - ar.size(); i < last; ++i)
194 MathData::size_type MathData::find_last(MathData const & ar) const
196 for (int i = size() - ar.size(); i >= 0; --i)
203 bool MathData::contains(MathData const & ar) const
205 if (find(ar) != size())
207 for (const_iterator it = begin(); it != end(); ++it)
208 if ((*it)->contains(ar))
214 void MathData::touch() const
219 bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
221 bool has_contents = false;
222 BufferView * bv = mi.base.bv;
223 MathData * ar = const_cast<MathData*>(this);
224 ar->updateMacros(&bv->cursor(), mi.macrocontext,
227 // FIXME: for completion, try to insert the relevant data in the
228 // mathrow (like is done for text rows). We could add a pair of
229 // InsetMathColor inset, but these come with extra spacing of
231 DocIterator const & inlineCompletionPos = bv->inlineCompletionPos();
232 bool const has_completion = inlineCompletionPos.inMathed()
233 && &inlineCompletionPos.cell() == this;
234 size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
236 for (size_t i = 0 ; i < size() ; ++i) {
237 has_contents |= (*this)[i]->addToMathRow(mrow, mi);
238 if (i + 1 == compl_pos) {
239 mrow.back().compl_text = bv->inlineCompletion();
240 mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
250 bool isInside(DocIterator const & it, MathData const & ar,
251 pos_type p1, pos_type p2)
253 for (size_t i = 0; i != it.depth(); ++i) {
254 CursorSlice const & sl = it[i];
255 if (sl.inset().inMathed() && &sl.cell() == &ar)
256 return p1 <= sl.pos() && sl.pos() < p2;
265 void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
267 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
268 dim = fm.dimension('I');
269 int xascent = fm.dimension('x').ascent();
270 if (xascent >= dim.asc)
271 xascent = (2 * dim.asc) / 3;
273 mindes_ = (3 * xascent) / 4;
274 slevel_ = (4 * xascent) / 5;
275 sshift_ = xascent / 4;
277 MathRow mrow(mi, this);
278 mrow_cache_[mi.base.bv] = mrow;
279 mrow.metrics(mi, dim);
280 kerning_ = mrow.kerning(mi.base.bv);
282 // Cache the dimension.
283 mi.base.bv->coordCache().arrays().add(this, dim);
287 void MathData::draw(PainterInfo & pi, int const x, int const y) const
289 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
290 BufferView & bv = *pi.base.bv;
293 Dimension const & dim = bv.coordCache().getArrays().dim(this);
295 // don't draw outside the workarea
296 if (y + dim.descent() <= 0
297 || y - dim.ascent() >= bv.workHeight()
298 || x + dim.width() <= 0
299 || x >= bv. workWidth())
302 MathRow const & mrow = mrow_cache_[pi.base.bv];
307 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
311 for (const_iterator it = begin(); it != end(); ++it) {
312 (*it)->metricsT(mi, d);
318 void MathData::drawT(TextPainter & pain, int x, int y) const
320 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
322 // FIXME: Abdel 16/10/2006
323 // This drawT() method is never used, this is dead code.
325 for (const_iterator it = begin(), et = end(); it != et; ++it) {
326 (*it)->drawT(pain, x, y);
327 //x += (*it)->width_;
333 void MathData::updateBuffer(ParIterator const & it, UpdateType utype)
336 for (size_t i = 0, n = size(); i != n; ++i) {
337 MathAtom & at = operator[](i);
338 at.nucleus()->updateBuffer(it, utype);
343 void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
346 // If we are editing a macro, we cannot update it immediately,
347 // otherwise wrong undo steps will be recorded (bug 6208).
348 InsetMath const * inmath = cur ? cur->inset().asInsetMath() : 0;
349 MathMacro const * inmacro = inmath ? inmath->asMacro() : 0;
350 docstring const edited_name = inmacro ? inmacro->name() : docstring();
352 // go over the array and look for macros
353 for (size_t i = 0; i < size(); ++i) {
354 MathMacro * macroInset = operator[](i).nucleus()->asMacro();
355 if (!macroInset || macroInset->macroName().empty()
356 || macroInset->macroName()[0] == '^'
357 || macroInset->macroName()[0] == '_'
358 || (macroInset->name() == edited_name
359 && macroInset->displayMode() ==
360 MathMacro::DISPLAY_UNFOLDED))
364 macroInset->updateMacro(mc);
365 size_t macroNumArgs = 0;
366 size_t macroOptionals = 0;
367 MacroData const * macro = macroInset->macro();
369 macroNumArgs = macro->numargs();
370 macroOptionals = macro->optionals();
373 // store old and compute new display mode
374 MathMacro::DisplayMode newDisplayMode;
375 MathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
376 newDisplayMode = macroInset->computeDisplayMode();
378 // arity changed or other reason to detach?
379 if (oldDisplayMode == MathMacro::DISPLAY_NORMAL
380 && (macroInset->arity() != macroNumArgs
381 || macroInset->optionals() != macroOptionals
382 || newDisplayMode == MathMacro::DISPLAY_UNFOLDED))
383 detachMacroParameters(cur, i);
385 // the macro could have been copied while resizing this
386 macroInset = operator[](i).nucleus()->asMacro();
389 if (newDisplayMode != MathMacro::DISPLAY_UNFOLDED
390 && oldDisplayMode == MathMacro::DISPLAY_UNFOLDED) {
391 // put cursor in front of macro
393 int macroSlice = cur->find(macroInset);
394 if (macroSlice != -1)
395 cur->cutOff(macroSlice - 1);
399 // update the display mode
400 size_t appetite = macroInset->appetite();
401 macroInset->setDisplayMode(newDisplayMode);
404 if (newDisplayMode == MathMacro::DISPLAY_NORMAL
405 && (macroInset->arity() != macroNumArgs
406 || macroInset->optionals() != macroOptionals)) {
407 // is it a virgin macro which was never attached to parameters?
408 bool fromInitToNormalMode
409 = (oldDisplayMode == MathMacro::DISPLAY_INIT
410 || oldDisplayMode == MathMacro::DISPLAY_INTERACTIVE_INIT)
411 && newDisplayMode == MathMacro::DISPLAY_NORMAL;
413 // if the macro was entered interactively (i.e. not by paste or during
414 // loading), it should not be greedy, but the cursor should
415 // automatically jump into the macro when behind
416 bool interactive = (oldDisplayMode == MathMacro::DISPLAY_INTERACTIVE_INIT);
419 attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
420 fromInitToNormalMode, interactive, appetite);
423 cur->updateInsets(&cur->bottom().inset());
426 // Give macro the chance to adapt to new situation.
427 // The macroInset could be invalid now because it was put into a script
428 // inset and therefore "deep" copied. So get it again from the MathData.
429 InsetMath * inset = operator[](i).nucleus();
430 if (inset->asScriptInset())
431 inset = inset->asScriptInset()->nuc()[0].nucleus();
432 LASSERT(inset->asMacro(), continue);
433 inset->asMacro()->updateRepresentation(cur, mc, utype);
438 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
440 MathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
441 // We store this now, because the inset pointer will be invalidated in the scond loop below
442 size_t const optionals = macroInset->optionals();
444 // detach all arguments
445 vector<MathData> detachedArgs;
446 if (macroPos + 1 == size())
447 // strip arguments if we are at the MathData end
448 macroInset->detachArguments(detachedArgs, true);
450 macroInset->detachArguments(detachedArgs, false);
453 int curMacroSlice = -1;
455 curMacroSlice = cur->find(macroInset);
456 idx_type curMacroIdx = -1;
457 pos_type curMacroPos = -1;
458 vector<CursorSlice> argSlices;
459 if (curMacroSlice != -1) {
460 curMacroPos = (*cur)[curMacroSlice].pos();
461 curMacroIdx = (*cur)[curMacroSlice].idx();
462 cur->cutOff(curMacroSlice, argSlices);
466 // only [] after the last non-empty argument can be dropped later
467 size_t lastNonEmptyOptional = 0;
468 for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
469 if (!detachedArgs[l].empty())
470 lastNonEmptyOptional = l;
473 // optional arguments to be put back?
474 pos_type p = macroPos + 1;
476 // We do not want to use macroInset below, the insert() call in
477 // the loop will invalidate it.
479 for (; j < detachedArgs.size() && j < optionals; ++j) {
480 // another non-empty parameter follows?
481 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
483 // then we can drop empty optional parameters
484 if (detachedArgs[j].empty() && canDropEmptyOptional) {
485 if (curMacroIdx == j)
486 (*cur)[curMacroSlice - 1].pos() = macroPos + 1;
490 // Otherwise we don't drop an empty optional, put it back normally
492 asArray(from_ascii("[]"), optarg);
493 MathData & arg = detachedArgs[j];
495 // look for "]", i.e. put a brace around?
496 InsetMathBrace * brace = 0;
497 for (size_t q = 0; q < arg.size(); ++q) {
498 if (arg[q]->getChar() == ']') {
500 brace = new InsetMathBrace(buffer_);
505 // put arg between []
507 brace->cell(0) = arg;
508 optarg.insert(1, MathAtom(brace));
510 optarg.insert(1, arg);
512 // insert it into the array
517 if (curMacroSlice == -1)
520 // cursor in optional argument of macro?
521 if (curMacroIdx == j) {
523 cur->append(0, curMacroPos);
524 (*cur)[curMacroSlice - 1].pos() = macroPos + 2;
526 (*cur)[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
527 cur->append(argSlices);
528 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
529 // cursor right of macro
530 (*cur)[curMacroSlice - 1].pos() += optarg.size();
533 // put them back into the MathData
534 for (; j < detachedArgs.size(); ++j, ++p) {
535 MathData const & arg = detachedArgs[j];
537 && !arg[0]->asScriptInset()
538 && !(arg[0]->asMacro() && arg[0]->asMacro()->arity() > 0))
541 insert(p, MathAtom(new InsetMathBrace(arg)));
544 if (curMacroSlice == -1)
547 // cursor in j-th argument of macro?
548 if (curMacroIdx == j) {
549 if (operator[](p).nucleus()->asBraceInset()) {
550 (*cur)[curMacroSlice - 1].pos() = p;
551 cur->append(0, curMacroPos);
552 cur->append(argSlices);
554 (*cur)[curMacroSlice - 1].pos() = p; // + macroPos;
555 cur->append(argSlices);
557 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
558 ++(*cur)[curMacroSlice - 1].pos();
562 cur->updateInsets(&cur->bottom().inset());
566 void MathData::attachMacroParameters(Cursor * cur,
567 const size_type macroPos, const size_type macroNumArgs,
568 const int macroOptionals, const bool fromInitToNormalMode,
569 const bool interactiveInit, const size_t appetite)
571 MathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
573 // start at atom behind the macro again, maybe with some new arguments
574 // from the detach phase above, to add them back into the macro inset
575 size_t p = macroPos + 1;
576 vector<MathData> detachedArgs;
577 MathAtom scriptToPutAround;
579 // find cursor slice again of this MathData
582 thisSlice = cur->find(*this);
585 thisPos = (*cur)[thisSlice].pos();
587 // find arguments behind the macro
588 if (!interactiveInit) {
589 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
590 scriptToPutAround, macroPos, thisPos, thisSlice);
592 collectParameters(cur, macroNumArgs, detachedArgs, p,
593 scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
595 // attach arguments back to macro inset
596 macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
598 // found tail script? E.g. \foo{a}b^x
599 if (scriptToPutAround.nucleus()) {
600 InsetMathScript * scriptInset =
601 scriptToPutAround.nucleus()->asScriptInset();
602 // In the math parser we remove empty braces in the base
603 // of a script inset, but we have to restore them here.
604 if (scriptInset->nuc().empty()) {
606 scriptInset->nuc().push_back(
607 MathAtom(new InsetMathBrace(ar)));
609 // put macro into a script inset
610 scriptInset->nuc()[0] = operator[](macroPos);
611 operator[](macroPos) = scriptToPutAround;
613 // go into the script inset nucleus
614 if (cur && thisPos == int(macroPos))
617 // get pointer to "deep" copied macro inset
618 scriptInset = operator[](macroPos).nucleus()->asScriptInset();
619 macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
622 // remove them from the MathData
623 erase(macroPos + 1, p);
625 // cursor outside this MathData?
629 // fix cursor if right of p
630 if (thisPos >= int(p))
631 (*cur)[thisSlice].pos() -= p - (macroPos + 1);
633 // was the macro inset just inserted interactively and was now folded
634 // and the cursor is just behind?
635 if ((*cur)[thisSlice].pos() == int(macroPos + 1)
637 && fromInitToNormalMode
638 && macroInset->arity() > 0
639 && thisSlice + 1 == int(cur->depth())) {
640 // then enter it if the cursor was just behind
641 (*cur)[thisSlice].pos() = macroPos;
642 cur->push_back(CursorSlice(*macroInset));
643 macroInset->idxFirst(*cur);
648 void MathData::collectOptionalParameters(Cursor * cur,
649 const size_type numOptionalParams, vector<MathData> & params,
650 size_t & pos, MathAtom & scriptToPutAround,
651 const pos_type macroPos, const int thisPos, const int thisSlice)
653 Buffer * buf = cur ? cur->buffer() : 0;
654 // insert optional arguments?
655 while (params.size() < numOptionalParams
657 && !scriptToPutAround.nucleus()) {
658 // is a [] block following which could be an optional parameter?
659 if (operator[](pos)->getChar() != '[')
662 // found possible optional argument, look for "]"
663 size_t right = pos + 1;
664 for (; right < size(); ++right) {
665 MathAtom & cell = operator[](right);
667 if (cell->getChar() == ']')
671 // maybe "]" with a script around?
672 InsetMathScript * script = cell.nucleus()->asScriptInset();
675 if (script->nuc().size() != 1)
677 if (script->nuc()[0]->getChar() == ']') {
678 // script will be put around the macro later
679 scriptToPutAround = cell;
685 if (right >= size()) {
686 // no ] found, so it's not an optional argument
690 // add everything between [ and ] as optional argument
691 MathData optarg(buf, begin() + pos + 1, begin() + right);
695 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
697 params.push_back(optarg[0]->asBraceInset()->cell(0));
699 params.push_back(optarg);
701 // place cursor in optional argument of macro
703 && thisPos >= int(pos) && thisPos <= int(right)) {
704 int paramPos = max(0, thisPos - int(pos) - 1);
705 vector<CursorSlice> x;
706 cur->cutOff(thisSlice, x);
707 (*cur)[thisSlice].pos() = macroPos;
709 paramPos = x[0].pos();
712 cur->append(0, paramPos);
718 // fill up empty optional parameters
719 while (params.size() < numOptionalParams)
720 params.push_back(MathData());
724 void MathData::collectParameters(Cursor * cur,
725 const size_type numParams, vector<MathData> & params,
726 size_t & pos, MathAtom & scriptToPutAround,
727 const pos_type macroPos, const int thisPos, const int thisSlice,
728 const size_t appetite)
730 size_t startSize = params.size();
732 // insert normal arguments
733 while (params.size() < numParams
734 && params.size() - startSize < appetite
736 && !scriptToPutAround.nucleus()) {
737 MathAtom & cell = operator[](pos);
740 vector<CursorSlice> argSlices;
742 if (thisSlice != -1 && thisPos == int(pos))
743 cur->cutOff(thisSlice, argSlices);
745 // which kind of parameter is it? In {}? With index x^n?
746 InsetMathBrace const * brace = cell->asBraceInset();
748 // found brace, convert into argument
749 params.push_back(brace->cell(0));
751 // cursor inside of the brace or just in front of?
752 if (thisPos == int(pos) && !argSlices.empty()) {
753 argPos = argSlices[0].pos();
754 argSlices.erase(argSlices.begin());
756 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
757 // last inset with scripts without braces
758 // -> they belong to the macro, not the argument
759 InsetMathScript * script = cell.nucleus()->asScriptInset();
760 if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
761 // nucleus in brace? Unpack!
762 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
764 params.push_back(script->nuc());
766 // script will be put around below
767 scriptToPutAround = cell;
769 // this should only happen after loading, so make cursor handling simple
770 if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
776 // the simplest case: plain inset
778 array.insert(0, cell);
779 params.push_back(array);
782 // put cursor in argument again
783 if (thisSlice != - 1 && thisPos == int(pos)) {
784 cur->append(params.size() - 1, argPos);
785 cur->append(argSlices);
786 (*cur)[thisSlice].pos() = macroPos;
794 int MathData::pos2x(BufferView const * bv, size_type pos) const
797 size_type target = min(pos, size());
798 CoordCache::Insets const & coords = bv->coordCache().getInsets();
799 for (size_type i = 0; i < target; ++i) {
800 const_iterator it = begin() + i;
801 //lyxerr << "char: " << (*it)->getChar()
802 // << "width: " << (*it)->width() << endl;
803 x += coords.dim((*it).nucleus()).wid;
809 MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
811 const_iterator it = begin();
814 CoordCache::Insets const & coords = bv->coordCache().getInsets();
815 // find first position after targetx
816 for (; currx < targetx && it != end(); ++it) {
818 currx += coords.dim((*it).nucleus()).wid;
822 * If we are not at the beginning of the array, go to the left
823 * of the inset if one of the following two condition holds:
824 * - the current inset is editable (so that the cursor tip is
825 * deeper than us): in this case, we want all intermediate
826 * cursor slices to be before insets;
827 * - the mouse is closer to the left side of the inset than to
829 * See bug 1918 for details.
831 if (it != begin() && currx >= targetx
832 && ((*prev(it, 1))->asNestInset()
833 || abs(lastx - targetx) < abs(currx - targetx))) {
841 int MathData::dist(BufferView const & bv, int x, int y) const
843 return bv.coordCache().getArrays().squareDistance(this, x, y);
847 void MathData::setXY(BufferView & bv, int x, int y) const
849 //lyxerr << "setting position cache for MathData " << this << endl;
850 bv.coordCache().arrays().add(this, x, y);
854 Dimension const & MathData::dimension(BufferView const & bv) const
856 return bv.coordCache().getArrays().dim(this);
860 int MathData::xm(BufferView const & bv) const
862 Geometry const & g = bv.coordCache().getArrays().geometry(this);
864 return g.pos.x_ + g.dim.wid / 2;
868 int MathData::ym(BufferView const & bv) const
870 Geometry const & g = bv.coordCache().getArrays().geometry(this);
872 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
876 int MathData::xo(BufferView const & bv) const
878 return bv.coordCache().getArrays().x(this);
882 int MathData::yo(BufferView const & bv) const
884 return bv.coordCache().getArrays().y(this);
888 ostream & operator<<(ostream & os, MathData const & ar)
890 odocstringstream oss;
891 NormalStream ns(oss);
893 return os << to_utf8(oss.str());
897 odocstream & operator<<(odocstream & os, MathData const & ar)