]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.cpp
#include cosmetics
[lyx.git] / src / mathed / MathData.cpp
1 /**
2  * \file MathData.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Stefan Schimanski
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "MathData.h"
15
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"
25
26 #include "Buffer.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
29 #include "Cursor.h"
30 #include "debug.h"
31
32 #include "support/docstream.h"
33
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 #include <boost/assert.hpp>
38 #include <boost/next_prior.hpp>
39
40
41 namespace lyx {
42
43 using std::abs;
44 using std::endl;
45 using std::min;
46 using std::ostringstream;
47 using std::string;
48 using std::vector;
49
50
51 MathData::MathData(const_iterator from, const_iterator to)
52         : base_type(from, to)
53 {}
54
55
56 MathAtom & MathData::operator[](pos_type pos)
57 {
58         BOOST_ASSERT(pos < size());
59         return base_type::operator[](pos);
60 }
61
62
63 MathAtom const & MathData::operator[](pos_type pos) const
64 {
65         BOOST_ASSERT(pos < size());
66         return base_type::operator[](pos);
67 }
68
69
70 void MathData::insert(size_type pos, MathAtom const & t)
71 {
72         base_type::insert(begin() + pos, t);
73 }
74
75
76 void MathData::insert(size_type pos, MathData const & ar)
77 {
78         BOOST_ASSERT(pos <= size());
79         base_type::insert(begin() + pos, ar.begin(), ar.end());
80 }
81
82
83 void MathData::append(MathData const & ar)
84 {
85         insert(size(), ar);
86 }
87
88
89 void MathData::erase(size_type pos)
90 {
91         if (pos < size())
92                 erase(pos, pos + 1);
93 }
94
95
96 void MathData::erase(iterator pos1, iterator pos2)
97 {
98         base_type::erase(pos1, pos2);
99 }
100
101
102 void MathData::erase(iterator pos)
103 {
104         base_type::erase(pos);
105 }
106
107
108 void MathData::erase(size_type pos1, size_type pos2)
109 {
110         base_type::erase(begin() + pos1, begin() + pos2);
111 }
112
113
114 void MathData::dump2() const
115 {
116         odocstringstream os;
117         NormalStream ns(os);
118         for (const_iterator it = begin(); it != end(); ++it)
119                 ns << *it << ' ';
120         lyxerr << to_utf8(os.str());
121 }
122
123
124 void MathData::dump() const
125 {
126         odocstringstream os;
127         NormalStream ns(os);
128         for (const_iterator it = begin(); it != end(); ++it)
129                 ns << '<' << *it << '>';
130         lyxerr << to_utf8(os.str());
131 }
132
133
134 void MathData::validate(LaTeXFeatures & features) const
135 {
136         for (const_iterator it = begin(); it != end(); ++it)
137                 (*it)->validate(features);
138 }
139
140
141 bool MathData::match(MathData const & ar) const
142 {
143         return size() == ar.size() && matchpart(ar, 0);
144 }
145
146
147 bool MathData::matchpart(MathData const & ar, pos_type pos) const
148 {
149         if (size() < ar.size() + pos)
150                 return false;
151         const_iterator it = begin() + pos;
152         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
153                 if (asString(*it) != asString(*jt))
154                         return false;
155         return true;
156 }
157
158
159 void MathData::replace(ReplaceData & rep)
160 {
161         for (size_type i = 0; i < size(); ++i) {
162                 if (find1(rep.from, i)) {
163                         // match found
164                         lyxerr << "match found!" << endl;
165                         erase(i, i + rep.from.size());
166                         insert(i, rep.to);
167                 }
168         }
169
170         // FIXME: temporarily disabled
171         // for (const_iterator it = begin(); it != end(); ++it)
172         //      it->nucleus()->replace(rep);
173 }
174
175
176 bool MathData::find1(MathData const & ar, size_type pos) const
177 {
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]))
181                         return false;
182         return true;
183 }
184
185
186 MathData::size_type MathData::find(MathData const & ar) const
187 {
188         for (int i = 0, last = size() - ar.size(); i < last; ++i)
189                 if (find1(ar, i))
190                         return i;
191         return size();
192 }
193
194
195 MathData::size_type MathData::find_last(MathData const & ar) const
196 {
197         for (int i = size() - ar.size(); i >= 0; --i)
198                 if (find1(ar, i))
199                         return i;
200         return size();
201 }
202
203
204 bool MathData::contains(MathData const & ar) const
205 {
206         if (find(ar) != size())
207                 return true;
208         for (const_iterator it = begin(); it != end(); ++it)
209                 if ((*it)->contains(ar))
210                         return true;
211         return false;
212 }
213
214
215 void MathData::touch() const
216 {
217 }
218
219
220 namespace {
221
222 bool isInside(DocIterator const & it, MathData const & ar,
223         pos_type p1, pos_type p2)
224 {
225         for (size_t i = 0; i != it.depth(); ++i) {
226                 CursorSlice const & sl = it[i];
227                 if (sl.inset().inMathed() && &sl.cell() == &ar)
228                         return p1 <= sl.pos() && sl.pos() < p2;
229         }
230         return false;
231 }
232
233 }
234
235
236
237 void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
238 {
239         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
240         dim = fm.dimension('I');
241         int xascent = fm.dimension('x').ascent();
242         if (xascent >= dim.asc)
243                 xascent = (2 * dim.asc) / 3;
244         minasc_ = xascent;
245         mindes_ = (3 * xascent) / 4;
246         slevel_ = (4 * xascent) / 5;
247         sshift_ = xascent / 4;
248         kerning_ = 0;
249
250         if (empty()) {
251                 // Cache the dimension.
252                 mi.base.bv->coordCache().arrays().add(this, dim);
253                 return;
254         }
255
256         const_cast<MathData*>(this)->updateMacros(mi);
257
258         dim.asc = 0;
259         dim.wid = 0;
260         Dimension d;
261         atom_dims_.clear();
262         for (size_t i = 0, n = size(); i != n; ++i) {
263                 MathAtom const & at = operator[](i);
264                 at->metrics(mi, d);
265                 atom_dims_.push_back(d);
266                 dim += d;
267                 if (i == n - 1)
268                         kerning_ = at->kerning();
269         }
270         // Cache the dimension.
271         mi.base.bv->coordCache().arrays().add(this, dim);
272 }
273
274
275 void MathData::draw(PainterInfo & pi, int x, int y) const
276 {
277         //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
278         BufferView & bv  = *pi.base.bv;
279         setXY(bv, x, y);
280
281         Dimension const & dim = bv.coordCache().getArrays().dim(this);
282
283         if (empty()) {
284                 pi.pain.rectangle(x, y - dim.ascent(), dim.width(), dim.height(), Color_mathline);
285                 return;
286         }
287
288         // don't draw outside the workarea
289         if (y + dim.descent() <= 0
290                 || y - dim.ascent() >= bv.workHeight()
291                 || x + dim.width() <= 0
292                 || x >= bv. workWidth())
293                 return;
294
295         for (size_t i = 0, n = size(); i != n; ++i) {
296                 MathAtom const & at = operator[](i);
297                 bv.coordCache().insets().add(at.nucleus(), x, y);
298                 at->drawSelection(pi, x, y);
299                 at->draw(pi, x, y);
300                 x += atom_dims_[i].wid;
301         }
302 }
303
304
305 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
306 {
307         dim.clear();
308         Dimension d;
309         for (const_iterator it = begin(); it != end(); ++it) {
310                 (*it)->metricsT(mi, d);
311                 dim += d;
312         }
313 }
314
315
316 void MathData::drawT(TextPainter & pain, int x, int y) const
317 {
318         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
319
320         // FIXME: Abdel 16/10/2006
321         // This drawT() method is never used, this is dead code.
322
323         for (const_iterator it = begin(), et = end(); it != et; ++it) {
324                 (*it)->drawT(pain, x, y);
325                 //x += (*it)->width_;
326                 x += 2;
327         }
328 }
329
330
331 void MathData::updateMacros(MetricsInfo & mi) 
332 {
333         Cursor & cur = mi.base.bv->cursor();
334
335         // go over the array and look for macros
336         for (size_t i = 0; i < size(); ++i) {
337                 MathMacro * macroInset = operator[](i).nucleus()->asMacro();
338                 if (!macroInset)
339                         continue;
340                 
341                 // get macro
342                 macroInset->updateMacro(mi);
343                 size_t macroNumArgs = 0;
344                 size_t macroOptionals = 0;
345                 MacroData const * macro = macroInset->macro();
346                 if (macro) {
347                         macroNumArgs = macro->numargs();
348                         macroOptionals = macro->optionals();
349                 }
350
351                 // store old and compute new display mode
352                 MathMacro::DisplayMode newDisplayMode;
353                 MathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
354                 newDisplayMode = macroInset->computeDisplayMode(mi);
355
356                 // arity changed or other reason to detach?
357                 if (oldDisplayMode == MathMacro::DISPLAY_NORMAL
358                                 && (macroInset->arity() != macroNumArgs
359                                                 || macroInset->optionals() != macroOptionals
360                                                 || newDisplayMode == MathMacro::DISPLAY_UNFOLDED)) {
361                         detachMacroParameters(cur, i);
362                 }
363
364                 // the macro could have been copied while resizing this
365                 macroInset = operator[](i).nucleus()->asMacro();
366
367                 // Cursor in \label?
368                 if (newDisplayMode != MathMacro::DISPLAY_UNFOLDED 
369                                 && oldDisplayMode == MathMacro::DISPLAY_UNFOLDED) {
370                         // put cursor in front of macro
371                         int macroSlice = cur.find(macroInset);
372                         if (macroSlice != -1)
373                                 cur.cutOff(macroSlice - 1);
374                 }
375
376                 // update the display mode
377                 macroInset->setDisplayMode(newDisplayMode);
378
379                 // arity changed?
380                 if (newDisplayMode == MathMacro::DISPLAY_NORMAL 
381                                 && (macroInset->arity() != macroNumArgs
382                                                 || macroInset->optionals() != macroOptionals)) {
383                         // is it a virgin macro which was never attached to parameters?
384                         bool fromInitToNormalMode
385                         = (oldDisplayMode == MathMacro::DISPLAY_INIT 
386                                  || oldDisplayMode == MathMacro::DISPLAY_NONGREEDY_INIT)
387                                 && newDisplayMode == MathMacro::DISPLAY_NORMAL;
388                         bool greedy = (oldDisplayMode != MathMacro::DISPLAY_NONGREEDY_INIT);
389                         
390                         // attach parameters
391                         attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
392                                 fromInitToNormalMode, greedy);
393                         
394                         // FIXME: proper anchor handling, this removes the selection
395                         cur.updateInsets(&cur.bottom().inset());
396                         cur.clearSelection();   
397                 }
398
399                 // give macro the chance to adapt to new situation
400                 InsetMath * inset = operator[](i).nucleus();
401                 if (inset->asScriptInset())
402                         inset = inset->asScriptInset()->nuc()[0].nucleus();
403                 BOOST_ASSERT(inset->asMacro());
404                 inset->asMacro()->updateRepresentation(mi);
405         }
406 }
407
408
409 void MathData::detachMacroParameters(Cursor & cur, const size_type macroPos)
410 {
411         MathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
412         
413         // detach all arguments
414         std::vector<MathData> detachedArgs;
415         if (macroPos + 1 == size())
416                                 // strip arguments if we are at the MathData end
417                                 macroInset->detachArguments(detachedArgs, true);
418         else
419                                 macroInset->detachArguments(detachedArgs, false);
420         
421         // find cursor slice
422         int curMacroSlice = cur.find(macroInset);
423         idx_type curMacroIdx = -1;
424         pos_type curMacroPos = -1;
425         std::vector<CursorSlice> argSlices;
426         if (curMacroSlice != -1) {
427                                 curMacroPos = cur[curMacroSlice].pos();
428                                 curMacroIdx = cur[curMacroSlice].idx();
429                                 cur.cutOff(curMacroSlice, argSlices);
430                                 cur.pop_back();
431         }
432         
433         // only [] after the last non-empty argument can be dropped later 
434         size_t lastNonEmptyOptional = 0;
435         for (size_t l = 0; l < detachedArgs.size() && l < macroInset->optionals(); ++l) {
436                                 if (!detachedArgs[l].empty())
437                                         lastNonEmptyOptional = l;
438         }
439         
440         // optional arguments to be put back?
441         pos_type p = macroPos + 1;
442         size_t j = 0;
443         for (; j < detachedArgs.size() && j < macroInset->optionals(); ++j) {
444                 // another non-empty parameter follows?
445                 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
446                 
447                 // then we can drop empty optional parameters
448                 if (detachedArgs[j].empty() && canDropEmptyOptional) {
449                         if (curMacroIdx == j)
450                                 cur[curMacroSlice - 1].pos() = macroPos + 1;
451                         continue;
452                 }
453                 
454                 // Otherwise we don't drop an empty optional, put it back normally
455                 MathData optarg;
456                 asArray(from_ascii("[]"), optarg);
457                 MathData & arg = detachedArgs[j];
458                 
459                 // look for "]", i.e. put a brace around?
460                 InsetMathBrace * brace = 0;
461                 for (size_t q = 0; q < arg.size(); ++q) {
462                         if (arg[q]->getChar() == ']') {
463                                 // put brace
464                                 brace = new InsetMathBrace();
465                                 break;
466                         }
467                 }
468                 
469                 // put arg between []
470                 if (brace) {
471                         brace->cell(0) = arg;
472                         optarg.insert(1, MathAtom(brace));
473                 } else
474                         optarg.insert(1, arg);
475                 
476                 // insert it into the array
477                 insert(p, optarg);
478                 p += optarg.size();
479                 
480                 // cursor in optional argument of macro?
481                 if (curMacroIdx == j) {
482                         if (brace) {
483                                 cur.append(0, curMacroPos);
484                                 cur[curMacroSlice - 1].pos() = macroPos + 2;
485                         } else
486                                 cur[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
487                         cur.append(argSlices);
488                 } else if (cur[curMacroSlice - 1].pos() >= int(p))
489                         // cursor right of macro
490                         cur[curMacroSlice - 1].pos() += optarg.size();
491         }
492         
493         // put them back into the MathData
494         for (; j < detachedArgs.size(); ++j) {                          
495                 MathData const & arg = detachedArgs[j];
496                 if (arg.size() == 1 && !arg[0]->asScriptInset()) // && arg[0]->asCharInset())
497                         insert(p, arg[0]);
498                 else
499                         insert(p, MathAtom(new InsetMathBrace(arg)));
500                 
501                 // cursor in j-th argument of macro?
502                 if (curMacroIdx == j) {
503                         if (operator[](p).nucleus()->asBraceInset()) {
504                                 cur[curMacroSlice - 1].pos() = p;
505                                 cur.append(0, curMacroPos);
506                                 cur.append(argSlices);
507                         } else {
508                                 cur[curMacroSlice - 1].pos() = p; // + macroPos;
509                                 cur.append(argSlices);
510                         }
511                 } else if (cur[curMacroSlice - 1].pos() >= int(p))
512                         ++cur[curMacroSlice - 1].pos();
513                 
514                 ++p;
515         }
516         
517         // FIXME: proper anchor handling, this removes the selection
518         cur.clearSelection();
519         cur.updateInsets(&cur.bottom().inset());
520 }
521
522
523 void MathData::attachMacroParameters(Cursor & cur, 
524         const size_type macroPos, const size_type macroNumArgs,
525         const int macroOptionals, const bool fromInitToNormalMode,
526         const bool greedy)
527 {
528         MathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
529
530         // start at atom behind the macro again, maybe with some new arguments from above
531         // to add them back into the macro inset
532         size_t p = macroPos + 1;
533         std::vector<MathData> detachedArgs;
534         MathAtom scriptToPutAround;
535         
536         // find cursor slice again
537         int thisSlice = cur.find(*this);
538         int thisPos = -1;
539         if (thisSlice != -1)
540                 thisPos = cur[thisSlice].pos();
541         
542         // find arguments behind the macro
543         if (greedy) {
544                 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
545                         macroPos, thisPos, thisSlice);
546                 collectParameters(cur, macroNumArgs, detachedArgs, p,
547                         scriptToPutAround, 
548                         macroPos, thisPos, thisSlice);
549         }
550                 
551         // attach arguments back to macro inset
552         macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
553         
554         // found tail script? E.g. \foo{a}b^x
555         if (scriptToPutAround.nucleus()) {
556                 // put macro into a script inset
557                 scriptToPutAround.nucleus()->asScriptInset()->nuc()[0] 
558                 = operator[](macroPos);
559                 operator[](macroPos) = scriptToPutAround;
560                 
561                 if (thisPos == int(macroPos))
562                         cur.append(0, 0);
563         }
564         
565         // remove them from the MathData
566         erase(begin() + macroPos + 1, begin() + p);
567         
568         // fix cursor if right of p
569         if (thisPos >= int(p))
570                 cur[thisSlice].pos() -= p - (macroPos + 1);
571         
572         // was the macro inset just inserted and was now folded?
573         if (cur[thisSlice].pos() == int(macroPos + 1)
574                         && fromInitToNormalMode
575                         && macroInset->arity() > 0
576                         && thisSlice + 1 == int(cur.depth())) {
577                 // then enter it if the cursor was just behind
578                 cur[thisSlice].pos() = macroPos;
579                 cur.push_back(CursorSlice(*macroInset));
580                 macroInset->idxFirst(cur);
581         }
582 }
583
584
585 void MathData::collectOptionalParameters(Cursor & cur, 
586         const size_type numOptionalParams, std::vector<MathData> & params, 
587         size_t & pos, const pos_type macroPos, const int thisPos, const int thisSlice)
588 {
589         // insert optional arguments?
590         while (params.size() < numOptionalParams && pos < size()) {
591                 // is a [] block following which could be an optional parameter?
592                 if (operator[](pos)->getChar() != '[')
593                         break;
594                                 
595                 // found possible optional argument, look for "]"
596                 size_t right = pos + 1;
597                 for (; right < size(); ++right) {
598                         if (operator[](right)->getChar() == ']')
599                                 // found right end
600                                 break;
601                 }
602                 
603                 // found?
604                 if (right >= size()) {
605                         // no ] found, so it's not an optional argument
606                         break;
607                 }
608                 
609                 // add everything between [ and ] as optional argument
610                 MathData optarg(begin() + pos + 1, begin() + right);
611                 
612                 // a brace?
613                 bool brace = false;
614                 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
615                         brace = true;
616                         params.push_back(optarg[0]->asBraceInset()->cell(0));
617                 } else
618                         params.push_back(optarg);
619                 
620                 // place cursor in optional argument of macro
621                 if (thisPos >= int(pos) && thisPos <= int(right)) {
622                         int paramPos = std::max(0, thisPos - int(pos) - 1);
623                         std::vector<CursorSlice> x;
624                         cur.cutOff(thisSlice, x);
625                         cur[thisSlice].pos() = macroPos;
626                         if (brace) {
627                                 paramPos = x[0].pos();
628                                 x.erase(x.begin());
629                         }
630                         cur.append(0, paramPos);
631                         cur.append(x);
632                 }
633                 pos = right + 1;
634         }
635
636         // fill up empty optional parameters
637         while (params.size() < numOptionalParams) {
638                 params.push_back(MathData());
639         }
640 }
641
642
643 void MathData::collectParameters(Cursor & cur, 
644         const size_type numParams, std::vector<MathData> & params, 
645         size_t & pos, MathAtom & scriptToPutAround,
646         const pos_type macroPos, const int thisPos, const int thisSlice) 
647 {
648         // insert normal arguments
649         for (; params.size() < numParams && pos < size();) {
650                 MathAtom & cell = operator[](pos);
651                 
652                 // fix cursor
653                 std::vector<CursorSlice> argSlices;
654                 int argPos = 0;
655                 if (thisPos == int(pos)) {
656                         cur.cutOff(thisSlice, argSlices);
657                 }
658                 
659                 // which kind of parameter is it? In {}? With index x^n?
660                 InsetMathBrace const * brace = cell->asBraceInset();
661                 if (brace) {
662                         // found brace, convert into argument
663                         params.push_back(brace->cell(0));
664                         
665                         // cursor inside of the brace or just in front of?
666                         if (thisPos == int(pos) && !argSlices.empty()) {
667                                 argPos = argSlices[0].pos();
668                                 argSlices.erase(argSlices.begin());
669                         }
670                 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
671                         // last inset with scripts without braces
672                         // -> they belong to the macro, not the argument
673                         InsetMathScript * script = cell.nucleus()->asScriptInset();
674                         if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
675                                 // nucleus in brace? Unpack!
676                                 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
677                         else
678                                 params.push_back(script->nuc());
679                         
680                         // script will be put around below
681                         scriptToPutAround = cell;
682                         
683                         // this should only happen after loading, so make cursor handling simple
684                         if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
685                                 argSlices.clear();
686                                 cur.append(0, 0);
687                         }
688                 } else {
689                         // the simplest case: plain inset
690                         MathData array;
691                         array.insert(0, cell);
692                         params.push_back(array);
693                 }
694                 
695                 // put cursor in argument again
696                 if (thisPos == int(pos)) {
697                         cur.append(params.size() - 1, argPos);
698                         cur.append(argSlices);
699                         cur[thisSlice].pos() = macroPos;
700                 }
701                 
702                 ++pos;
703         }       
704 }
705
706
707 int MathData::pos2x(size_type pos) const
708 {
709         return pos2x(pos, 0);
710 }
711
712
713 int MathData::pos2x(size_type pos, int glue) const
714 {
715         int x = 0;
716         size_type target = min(pos, size());
717         for (size_type i = 0; i < target; ++i) {
718                 const_iterator it = begin() + i;
719                 if ((*it)->getChar() == ' ')
720                         x += glue;
721                 //lyxerr << "char: " << (*it)->getChar()
722                 //      << "width: " << (*it)->width() << std::endl;
723                 x += atom_dims_[i].wid;
724         }
725         return x;
726 }
727
728
729 MathData::size_type MathData::x2pos(int targetx) const
730 {
731         return x2pos(targetx, 0);
732 }
733
734
735 MathData::size_type MathData::x2pos(int targetx, int glue) const
736 {
737         const_iterator it = begin();
738         int lastx = 0;
739         int currx = 0;
740         // find first position after targetx
741         for (; currx < targetx && it < end(); ++it) {
742                 lastx = currx;
743                 if ((*it)->getChar() == ' ')
744                         currx += glue;
745                 currx += atom_dims_[it - begin()].wid;
746         }
747
748         /**
749          * If we are not at the beginning of the array, go to the left
750          * of the inset if one of the following two condition holds:
751          * - the current inset is editable (so that the cursor tip is
752          *   deeper than us): in this case, we want all intermediate
753          *   cursor slices to be before insets;
754          * - the mouse is closer to the left side of the inset than to
755          *   the right one.
756          * See bug 1918 for details.
757          **/
758         if (it != begin() && currx >= targetx
759             && ((*boost::prior(it))->asNestInset()
760                 || abs(lastx - targetx) < abs(currx - targetx))) {
761                 --it;
762         }
763
764         return it - begin();
765 }
766
767
768 int MathData::dist(BufferView const & bv, int x, int y) const
769 {
770         return bv.coordCache().getArrays().squareDistance(this, x, y);
771 }
772
773
774 void MathData::setXY(BufferView & bv, int x, int y) const
775 {
776         //lyxerr << "setting position cache for MathData " << this << std::endl;
777         bv.coordCache().arrays().add(this, x, y);
778 }
779
780
781 Dimension const & MathData::dimension(BufferView const & bv) const
782 {
783         return bv.coordCache().getArrays().dim(this);
784 }
785
786
787 int MathData::xm(BufferView const & bv) const
788 {
789         Geometry const & g = bv.coordCache().getArrays().geometry(this);
790
791         return g.pos.x_ + g.dim.wid / 2;
792 }
793
794
795 int MathData::ym(BufferView const & bv) const
796 {
797         Geometry const & g = bv.coordCache().getArrays().geometry(this);
798
799         return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
800 }
801
802
803 int MathData::xo(BufferView const & bv) const
804 {
805         return bv.coordCache().getArrays().x(this);
806 }
807
808
809 int MathData::yo(BufferView const & bv) const
810 {
811         return bv.coordCache().getArrays().y(this);
812 }
813
814
815 std::ostream & operator<<(std::ostream & os, MathData const & ar)
816 {
817         odocstringstream oss;
818         NormalStream ns(oss);
819         ns << ar;
820         return os << to_utf8(oss.str());
821 }
822
823
824 odocstream & operator<<(odocstream & os, MathData const & ar)
825 {
826         NormalStream ns(os);
827         ns << ar;
828         return os;
829 }
830
831
832 } // namespace lyx