]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.cpp
8bba8909e93f883c014f39700c9eceb87442cbe5
[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 up cursor
569         if (thisSlice != -1) {
570                 // fix cursor if right of p
571                 if (thisPos >= int(p))
572                         cur[thisSlice].pos() -= p - (macroPos + 1);
573         
574                 // was the macro inset just inserted and was now folded?
575                 if (cur[thisSlice].pos() == int(macroPos + 1)
576                                 && fromInitToNormalMode
577                                 && macroInset->arity() > 0
578                                 && thisSlice + 1 == int(cur.depth())) {
579                         // then enter it if the cursor was just behind
580                         cur[thisSlice].pos() = macroPos;
581                         cur.push_back(CursorSlice(*macroInset));
582                         macroInset->idxFirst(cur);
583                 }
584         }
585 }
586
587
588 void MathData::collectOptionalParameters(Cursor & cur, 
589         const size_type numOptionalParams, std::vector<MathData> & params, 
590         size_t & pos, const pos_type macroPos, const int thisPos, const int thisSlice)
591 {
592         // insert optional arguments?
593         while (params.size() < numOptionalParams && pos < size()) {
594                 // is a [] block following which could be an optional parameter?
595                 if (operator[](pos)->getChar() != '[')
596                         break;
597                                 
598                 // found possible optional argument, look for "]"
599                 size_t right = pos + 1;
600                 for (; right < size(); ++right) {
601                         if (operator[](right)->getChar() == ']')
602                                 // found right end
603                                 break;
604                 }
605                 
606                 // found?
607                 if (right >= size()) {
608                         // no ] found, so it's not an optional argument
609                         break;
610                 }
611                 
612                 // add everything between [ and ] as optional argument
613                 MathData optarg(begin() + pos + 1, begin() + right);
614                 
615                 // a brace?
616                 bool brace = false;
617                 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
618                         brace = true;
619                         params.push_back(optarg[0]->asBraceInset()->cell(0));
620                 } else
621                         params.push_back(optarg);
622                 
623                 // place cursor in optional argument of macro
624                 if (thisSlice != -1
625                                 && thisPos >= int(pos) && thisPos <= int(right)) {
626                         int paramPos = std::max(0, thisPos - int(pos) - 1);
627                         std::vector<CursorSlice> x;
628                         cur.cutOff(thisSlice, x);
629                         cur[thisSlice].pos() = macroPos;
630                         if (brace) {
631                                 paramPos = x[0].pos();
632                                 x.erase(x.begin());
633                         }
634                         cur.append(0, paramPos);
635                         cur.append(x);
636                 }
637                 pos = right + 1;
638         }
639
640         // fill up empty optional parameters
641         while (params.size() < numOptionalParams) {
642                 params.push_back(MathData());
643         }
644 }
645
646
647 void MathData::collectParameters(Cursor & cur, 
648         const size_type numParams, std::vector<MathData> & params, 
649         size_t & pos, MathAtom & scriptToPutAround,
650         const pos_type macroPos, const int thisPos, const int thisSlice) 
651 {
652         // insert normal arguments
653         for (; params.size() < numParams && pos < size();) {
654                 MathAtom & cell = operator[](pos);
655                 
656                 // fix cursor
657                 std::vector<CursorSlice> argSlices;
658                 int argPos = 0;
659                 if (thisSlice != -1 && thisPos == int(pos)) {
660                         cur.cutOff(thisSlice, argSlices);
661                 }
662                 
663                 // which kind of parameter is it? In {}? With index x^n?
664                 InsetMathBrace const * brace = cell->asBraceInset();
665                 if (brace) {
666                         // found brace, convert into argument
667                         params.push_back(brace->cell(0));
668                         
669                         // cursor inside of the brace or just in front of?
670                         if (thisPos == int(pos) && !argSlices.empty()) {
671                                 argPos = argSlices[0].pos();
672                                 argSlices.erase(argSlices.begin());
673                         }
674                 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
675                         // last inset with scripts without braces
676                         // -> they belong to the macro, not the argument
677                         InsetMathScript * script = cell.nucleus()->asScriptInset();
678                         if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
679                                 // nucleus in brace? Unpack!
680                                 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
681                         else
682                                 params.push_back(script->nuc());
683                         
684                         // script will be put around below
685                         scriptToPutAround = cell;
686                         
687                         // this should only happen after loading, so make cursor handling simple
688                         if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
689                                 argSlices.clear();
690                                 cur.append(0, 0);
691                         }
692                 } else {
693                         // the simplest case: plain inset
694                         MathData array;
695                         array.insert(0, cell);
696                         params.push_back(array);
697                 }
698                 
699                 // put cursor in argument again
700                 if (thisSlice != - 1 && thisPos == int(pos)) {
701                         cur.append(params.size() - 1, argPos);
702                         cur.append(argSlices);
703                         cur[thisSlice].pos() = macroPos;
704                 }
705                 
706                 ++pos;
707         }       
708 }
709
710
711 int MathData::pos2x(size_type pos) const
712 {
713         return pos2x(pos, 0);
714 }
715
716
717 int MathData::pos2x(size_type pos, int glue) const
718 {
719         int x = 0;
720         size_type target = min(pos, size());
721         for (size_type i = 0; i < target; ++i) {
722                 const_iterator it = begin() + i;
723                 if ((*it)->getChar() == ' ')
724                         x += glue;
725                 //lyxerr << "char: " << (*it)->getChar()
726                 //      << "width: " << (*it)->width() << std::endl;
727                 x += atom_dims_[i].wid;
728         }
729         return x;
730 }
731
732
733 MathData::size_type MathData::x2pos(int targetx) const
734 {
735         return x2pos(targetx, 0);
736 }
737
738
739 MathData::size_type MathData::x2pos(int targetx, int glue) const
740 {
741         const_iterator it = begin();
742         int lastx = 0;
743         int currx = 0;
744         // find first position after targetx
745         for (; currx < targetx && it < end(); ++it) {
746                 lastx = currx;
747                 if ((*it)->getChar() == ' ')
748                         currx += glue;
749                 currx += atom_dims_[it - begin()].wid;
750         }
751
752         /**
753          * If we are not at the beginning of the array, go to the left
754          * of the inset if one of the following two condition holds:
755          * - the current inset is editable (so that the cursor tip is
756          *   deeper than us): in this case, we want all intermediate
757          *   cursor slices to be before insets;
758          * - the mouse is closer to the left side of the inset than to
759          *   the right one.
760          * See bug 1918 for details.
761          **/
762         if (it != begin() && currx >= targetx
763             && ((*boost::prior(it))->asNestInset()
764                 || abs(lastx - targetx) < abs(currx - targetx))) {
765                 --it;
766         }
767
768         return it - begin();
769 }
770
771
772 int MathData::dist(BufferView const & bv, int x, int y) const
773 {
774         return bv.coordCache().getArrays().squareDistance(this, x, y);
775 }
776
777
778 void MathData::setXY(BufferView & bv, int x, int y) const
779 {
780         //lyxerr << "setting position cache for MathData " << this << std::endl;
781         bv.coordCache().arrays().add(this, x, y);
782 }
783
784
785 Dimension const & MathData::dimension(BufferView const & bv) const
786 {
787         return bv.coordCache().getArrays().dim(this);
788 }
789
790
791 int MathData::xm(BufferView const & bv) const
792 {
793         Geometry const & g = bv.coordCache().getArrays().geometry(this);
794
795         return g.pos.x_ + g.dim.wid / 2;
796 }
797
798
799 int MathData::ym(BufferView const & bv) const
800 {
801         Geometry const & g = bv.coordCache().getArrays().geometry(this);
802
803         return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
804 }
805
806
807 int MathData::xo(BufferView const & bv) const
808 {
809         return bv.coordCache().getArrays().x(this);
810 }
811
812
813 int MathData::yo(BufferView const & bv) const
814 {
815         return bv.coordCache().getArrays().y(this);
816 }
817
818
819 std::ostream & operator<<(std::ostream & os, MathData const & ar)
820 {
821         odocstringstream oss;
822         NormalStream ns(oss);
823         ns << ar;
824         return os << to_utf8(oss.str());
825 }
826
827
828 odocstream & operator<<(odocstream & os, MathData const & ar)
829 {
830         NormalStream ns(os);
831         ns << ar;
832         return os;
833 }
834
835
836 } // namespace lyx