]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
fixes for \xxalignat and old style font changes
[lyx.git] / src / mathed / math_cursor.C
1 /*
2  *  File:        math_cursor.C
3  *  Purpose:     Interaction for mathed
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5  *  Created:     January 1996
6  *  Description: Math interaction for a WYSIWYG math editor.
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta, Math & Lyx project.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 #include <config.h>
19 #include <lyxrc.h>
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "debug.h"
28 #include "LColor.h"
29 #include "frontends/Painter.h"
30 #include "math_cursor.h"
31 #include "formulabase.h"
32 #include "math_autocorrect.h"
33 #include "math_arrayinset.h"
34 #include "math_braceinset.h"
35 #include "math_boxinset.h"
36 #include "math_casesinset.h"
37 #include "math_charinset.h"
38 #include "math_extern.h"
39 #include "math_factory.h"
40 #include "math_fboxinset.h"
41 #include "math_hullinset.h"
42 #include "math_iterator.h"
43 #include "math_macroarg.h"
44 #include "math_macrotemplate.h"
45 #include "math_mathmlstream.h"
46 #include "math_parser.h"
47 #include "math_replace.h"
48 #include "math_scriptinset.h"
49 #include "math_spaceinset.h"
50 #include "math_support.h"
51 #include "math_unknowninset.h"
52
53 #include <algorithm>
54 #include <cctype>
55
56 #define FILEDEBUG 0
57
58 using std::endl;
59 using std::min;
60 using std::max;
61 using std::swap;
62 using std::vector;
63 using std::ostringstream;
64 using std::isalpha;
65
66
67 // matheds own cut buffer
68 MathGridInset theCutBuffer = MathGridInset(1, 1);
69
70
71 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
72         :       formula_(formula), autocorrect_(false), selection_(false)
73 {
74         front ? first() : last();
75         Anchor_ = Cursor_;
76 }
77
78
79 MathCursor::~MathCursor()
80 {
81   // ensure that 'notifyCursorLeave' is called
82   while (popLeft())
83     ;
84 }   
85
86
87 void MathCursor::push(MathAtom & t)
88 {
89         Cursor_.push_back(MathCursorPos(t.nucleus()));
90 }
91
92
93 void MathCursor::pushLeft(MathAtom & t)
94 {
95         //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
96         push(t);
97         t->idxFirst(idx(), pos());
98 }
99
100
101 void MathCursor::pushRight(MathAtom & t)
102 {
103         //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
104         posLeft();
105         push(t);
106         t->idxLast(idx(), pos());
107 }
108
109
110 bool MathCursor::popLeft()
111 {
112         //cerr << "Leaving atom to the left\n";
113         if (depth() <= 1) {
114                 if (depth() == 1)
115                         par()->notifyCursorLeaves();
116                 return false;
117         }
118         par()->notifyCursorLeaves();
119         Cursor_.pop_back();
120         return true;
121 }
122
123
124 bool MathCursor::popRight()
125 {
126         //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
127         if (depth() <= 1) {
128                 if (depth() == 1)
129                         par()->notifyCursorLeaves();
130                 return false;
131         }
132         par()->notifyCursorLeaves();
133         Cursor_.pop_back();
134         posRight();
135         return true;
136 }
137
138
139
140 #if FILEDEBUG
141         void MathCursor::dump(char const * what) const
142         {
143                 lyxerr << "MC: " << what << "\n";
144                 lyxerr << " Cursor: " << depth() << "\n";
145                 for (unsigned i = 0; i < depth(); ++i)
146                         lyxerr << "    i: " << i << " " << Cursor_[i] << "\n";
147                 lyxerr << " Anchor: " << Anchor_.size() << "\n";
148                 for (unsigned i = 0; i < Anchor_.size(); ++i)
149                         lyxerr << "    i: " << i << " " << Anchor_[i] << "\n";
150                 lyxerr  << " sel: " << selection_ << "\n";
151         }
152 #else
153         void MathCursor::dump(char const *) const {}
154 #endif
155
156
157 bool MathCursor::isInside(MathInset const * p) const
158 {
159         for (unsigned i = 0; i < depth(); ++i)
160                 if (Cursor_[i].par_ == p)
161                         return true;
162         return false;
163 }
164
165
166 bool MathCursor::openable(MathAtom const & t, bool sel) const
167 {
168         if (!t->isActive())
169                 return false;
170
171         if (t->lock())
172                 return false;
173
174         if (t->asScriptInset())
175                 return false;
176
177         if (sel) {
178                 // we can't move into anything new during selection
179                 if (depth() == Anchor_.size())
180                         return false;
181                 if (t.nucleus() != Anchor_[depth()].par_)
182                         return false;
183         }
184         return true;
185 }
186
187
188 bool MathCursor::posLeft()
189 {
190         if (pos() == 0)
191                 return false;
192         --pos();
193         return true;
194 }
195
196
197 bool MathCursor::posRight()
198 {
199         if (pos() == size())
200                 return false;
201         ++pos();
202         return true;
203 }
204
205
206 bool MathCursor::left(bool sel)
207 {
208         dump("Left 1");
209         autocorrect_ = false;
210         targetx_ = -1; // "no target"
211         if (inMacroMode()) {
212                 macroModeClose();
213                 return true;
214         }
215         selHandle(sel);
216
217         if (hasPrevAtom() && openable(prevAtom(), sel)) {
218                 pushRight(prevAtom());
219                 return true;
220         }
221
222         return posLeft() || idxLeft() || popLeft() || selection_;
223 }
224
225
226 bool MathCursor::right(bool sel)
227 {
228         dump("Right 1");
229         autocorrect_ = false;
230         targetx_ = -1; // "no target"
231         if (inMacroMode()) {
232                 macroModeClose();
233                 return true;
234         }
235         selHandle(sel);
236
237         if (hasNextAtom() && openable(nextAtom(), sel)) {
238                 pushLeft(nextAtom());
239                 return true;
240         }
241
242         return posRight() || idxRight() || popRight() || selection_;
243 }
244
245
246 void MathCursor::first()
247 {
248         Cursor_.clear();
249         push(formula_->par());
250         par()->idxFirst(idx(), pos());
251 }
252
253
254 void MathCursor::last()
255 {
256         Cursor_.clear();
257         push(formula_->par());
258         par()->idxLast(idx(), pos());
259 }
260
261
262 bool positionable(MathCursor::cursor_type const & cursor,
263                   MathCursor::cursor_type const & anchor)
264 {
265         // avoid deeper nested insets when selecting
266         if (cursor.size() > anchor.size())
267                 return false;
268
269         // anchor might be deeper, should have same path then
270         for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
271                 if (cursor[i].par_ != anchor[i].par_)
272                         return false;
273
274         // position should be ok.
275         return true;
276 }
277
278
279 void MathCursor::setPos(int x, int y)
280 {
281         dump("setPos 1");
282         bool res = bruteFind(x, y,
283                 formula()->xlow(), formula()->xhigh(),
284                 formula()->ylow(), formula()->yhigh());
285         if (!res) {
286                 // this ccan happen on creation of "math-display"
287                 dump("setPos 1.5");
288                 first();
289         }
290         dump("setPos 2");
291 }
292
293
294
295 void MathCursor::home(bool sel)
296 {
297         dump("home 1");
298         autocorrect_ = false;
299         selHandle(sel);
300         macroModeClose();
301         if (!par()->idxHome(idx(), pos()))
302                 popLeft();
303         dump("home 2");
304 }
305
306
307 void MathCursor::end(bool sel)
308 {
309         dump("end 1");
310         autocorrect_ = false;
311         selHandle(sel);
312         macroModeClose();
313         if (!par()->idxEnd(idx(), pos()))
314                 popRight();
315         dump("end 2");
316 }
317
318
319 void MathCursor::plainErase()
320 {
321         array().erase(pos());
322 }
323
324
325 void MathCursor::markInsert()
326 {
327         //lyxerr << "inserting mark\n";
328         array().insert(pos(), MathAtom(new MathCharInset(0)));
329 }
330
331
332 void MathCursor::markErase()
333 {
334         //lyxerr << "deleting mark\n";
335         array().erase(pos());
336 }
337
338
339 void MathCursor::plainInsert(MathAtom const & t)
340 {
341         array().insert(pos(), t);
342         ++pos();
343 }
344
345
346 void MathCursor::insert(string const & str)
347 {
348         //lyxerr << "inserting '" << str << "'\n";
349         selClearOrDel();
350         for (string::const_iterator it = str.begin(); it != str.end(); ++it)
351                 plainInsert(MathAtom(new MathCharInset(*it)));
352 }
353
354
355 void MathCursor::insert(char c)
356 {
357         //lyxerr << "inserting '" << c << "'\n";
358         selClearOrDel();
359         plainInsert(MathAtom(new MathCharInset(c)));
360 }
361
362
363 void MathCursor::insert(MathAtom const & t)
364 {
365         macroModeClose();
366         selClearOrDel();
367         plainInsert(t);
368 }
369
370
371 void MathCursor::niceInsert(MathAtom const & t)
372 {
373         macroModeClose();
374         MathGridInset safe = grabAndEraseSelection();
375         plainInsert(t);
376         // enter the new inset and move the contents of the selection if possible
377         if (t->isActive()) {
378                 posLeft();
379                 pushLeft(nextAtom());
380                 paste(safe);
381         }
382 }
383
384
385 void MathCursor::insert(MathArray const & ar)
386 {
387         macroModeClose();
388         if (selection_)
389                 eraseSelection();
390
391         array().insert(pos(), ar);
392         pos() += ar.size();
393 }
394
395
396 void MathCursor::paste(MathArray const & ar)
397 {
398         Anchor_ = Cursor_;
399         selection_ = true;
400         array().insert(pos(), ar);
401         pos() += ar.size();
402 }
403
404
405 void MathCursor::paste(MathGridInset const & data)
406 {
407         if (data.nargs() == 1) {
408                 // single cell/part of cell
409                 paste(data.cell(0));
410         } else {
411                 // mulitple cells
412                 idx_type idx; // index of upper left cell
413                 MathGridInset * p = enclosingGrid(idx);
414                 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
415                 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
416                 for (row_type row = 0; row < numrows; ++row) {
417                         for (col_type col = 0; col < numcols; ++col) {
418                                 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
419                                 p->cell(i).push_back(data.cell(data.index(row, col)));
420                         }
421                         // append the left over horizontal cells to the last column
422                         idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
423                         for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
424                                 p->cell(i).push_back(data.cell(data.index(row, col)));
425                 }
426                 // append the left over vertical cells to the last _cell_
427                 idx_type i = p->nargs() - 1;
428                 for (row_type row = numrows; row < data.nrows(); ++row)
429                         for (col_type col = 0; col < data.ncols(); ++col)
430                                 p->cell(i).push_back(data.cell(data.index(row, col)));
431         }
432 }
433
434 void MathCursor::backspace()
435 {
436         autocorrect_ = false;
437         if (pos() == 0) {
438                 pullArg(false);
439                 return;
440         }
441
442         if (selection_) {
443                 selDel();
444                 return;
445         }
446
447         MathScriptInset * p = prevAtom()->asScriptInset();
448         if (p) {
449                 p->removeScript(p->hasUp());
450                 // Don't delete if there is anything left
451                 if (p->hasUp() || p->hasDown())
452                         return;
453         }
454
455         --pos();
456         plainErase();
457 }
458
459
460 void MathCursor::erase()
461 {
462         autocorrect_ = false;
463         if (inMacroMode())
464                 return;
465
466         if (selection_) {
467                 selDel();
468                 return;
469         }
470
471         // delete empty cells if possible
472         if (array().empty())
473                 if (par()->idxDelete(idx()))
474                         return;
475
476         // old behaviour when in last position of cell
477         if (pos() == size()) {
478                 par()->idxGlue(idx());
479                 return;
480         }
481
482         MathScriptInset * p = nextAtom()->asScriptInset();
483         if (p) {
484                 p->removeScript(p->hasUp());
485                 // Don't delete if there is anything left
486                 if (p->hasUp() || p->hasDown())
487                         return;
488         }
489
490         plainErase();
491 }
492
493
494 void MathCursor::delLine()
495 {
496         autocorrect_ = false;
497         macroModeClose();
498
499         if (selection_) {
500                 selDel();
501                 return;
502         }
503
504         if (par()->nrows() > 1) {
505                 // grid are the only things with more than one row...
506                 lyx::Assert(par()->asGridInset());
507                 par()->asGridInset()->delRow(hullRow());
508         }
509
510         if (idx() >= par()->nargs())
511                 idx() = par()->nargs() - 1;
512
513         if (pos() > size())
514                 pos() = size();
515 }
516
517
518 bool MathCursor::up(bool sel)
519 {
520         dump("up 1");
521         macroModeClose();
522         selHandle(sel);
523         cursor_type save = Cursor_;
524         if (goUpDown(true))
525                 return true;
526         Cursor_ = save;
527         autocorrect_ = false;
528         return selection_;
529 }
530
531
532 bool MathCursor::down(bool sel)
533 {
534         dump("down 1");
535         macroModeClose();
536         selHandle(sel);
537         cursor_type save = Cursor_;
538         if (goUpDown(false))
539                 return true;
540         Cursor_ = save;
541         autocorrect_ = false;
542         return selection_;
543 }
544
545
546 bool MathCursor::toggleLimits()
547 {
548         if (!hasNextAtom())
549                 return false;
550         MathScriptInset * t = nextAtom()->asScriptInset();
551         if (!t)
552                 return false;
553         int old = t->limits();
554         t->limits(old < 0 ? 1 : -1);
555         return old != t->limits();
556 }
557
558
559 void MathCursor::macroModeClose()
560 {
561         MathUnknownInset * p = inMacroMode();
562         if (!p)
563                 return;
564         p->finalize();
565         string s = p->name();
566         --pos();
567         array().erase(pos());
568         if (s != "\\")
569                 interpret(s);
570 }
571
572
573 string MathCursor::macroName() const
574 {
575         return inMacroMode() ? inMacroMode()->name() : string();
576 }
577
578
579 void MathCursor::selClear()
580 {
581         selection_ = false;
582 }
583
584
585 void MathCursor::selCopy()
586 {
587         dump("selCopy");
588         if (selection_) {
589                 theCutBuffer = grabSelection();
590                 selection_ = false;
591         } else {
592                 theCutBuffer = MathGridInset(1, 1);
593         }
594 }
595
596
597 void MathCursor::selCut()
598 {
599         dump("selCut");
600         theCutBuffer = grabAndEraseSelection();
601 }
602
603
604 void MathCursor::selDel()
605 {
606         dump("selDel");
607         if (selection_) {
608                 eraseSelection();
609                 selection_ = false;
610         }
611 }
612
613
614 void MathCursor::selPaste()
615 {
616         dump("selPaste");
617         selClearOrDel();
618         paste(theCutBuffer);
619         //grabSelection();
620         selection_ = false;
621 }
622
623
624 void MathCursor::selHandle(bool sel)
625 {
626         if (sel == selection_)
627                 return;
628         //clear();
629         Anchor_ = Cursor_;
630         selection_ = sel;
631 }
632
633
634 void MathCursor::selStart()
635 {
636         dump("selStart 1");
637         //clear();
638         Anchor_ = Cursor_;
639         selection_ = true;
640         dump("selStart 2");
641 }
642
643
644 void MathCursor::selClearOrDel()
645 {
646         if (lyxrc.auto_region_delete)
647                 selDel();
648         else
649                 selection_ = false;
650 }
651
652
653 void MathCursor::selGet(MathArray & ar)
654 {
655         dump("selGet");
656         if (selection_)
657                 ar = grabSelection().glue();
658 }
659
660
661
662 void MathCursor::drawSelection(MathPainterInfo & pain) const
663 {
664         if (!selection_)
665                 return;
666
667         MathCursorPos i1;
668         MathCursorPos i2;
669         getSelection(i1, i2);
670
671         if (i1.idx_ == i2.idx_) {
672                 MathXArray & c = i1.xcell();
673                 int x1 = c.xo() + c.pos2x(i1.pos_);
674                 int y1 = c.yo() - c.ascent();
675                 int x2 = c.xo() + c.pos2x(i2.pos_);
676                 int y2 = c.yo() + c.descent();
677                 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
678         } else {
679                 vector<MathInset::idx_type> indices
680                         = i1.par_->idxBetween(i1.idx_, i2.idx_);
681                 for (unsigned i = 0; i < indices.size(); ++i) {
682                         MathXArray & c = i1.xcell(indices[i]);
683                         int x1 = c.xo();
684                         int y1 = c.yo() - c.ascent();
685                         int x2 = c.xo() + c.width();
686                         int y2 = c.yo() + c.descent();
687                         pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
688                 }
689         }
690
691 #if 0
692         // draw anchor if different from selection boundary
693         MathCursorPos anc = Anchor_.back();
694         if (anc != i1 && anc != i2) {
695                 MathXArray & c = anc.xcell();
696                 int x  = c.xo() + c.pos2x(anc.pos_);
697                 int y1 = c.yo() - c.ascent();
698                 int y2 = c.yo() + c.descent();
699                 pain.line(x, y1, x, y2, LColor::math);
700         }
701 #endif
702 }
703
704
705 void MathCursor::handleNest(MathAtom const & at)
706 {
707         at->cell(0) = grabAndEraseSelection().glue();
708         insert(at);
709         pushRight(prevAtom());
710 }
711
712
713 void MathCursor::getPos(int & x, int & y)
714 {
715         par()->getPos(idx(), pos(), x, y);
716 }
717
718
719 MathInset * MathCursor::par() const
720 {
721         return cursor().par_;
722 }
723
724
725 InsetFormulaBase * MathCursor::formula() const
726 {
727         return formula_;
728 }
729
730
731 MathCursor::idx_type MathCursor::idx() const
732 {
733         return cursor().idx_;
734 }
735
736
737 MathCursor::idx_type & MathCursor::idx()
738 {
739         return cursor().idx_;
740 }
741
742
743 MathCursor::pos_type MathCursor::pos() const
744 {
745         return cursor().pos_;
746 }
747
748
749 MathCursor::pos_type & MathCursor::pos()
750 {
751         return cursor().pos_;
752 }
753
754
755 MathUnknownInset * MathCursor::inMacroMode() const
756 {
757         if (!hasPrevAtom())
758                 return 0;
759         MathUnknownInset * p = prevAtom()->asUnknownInset();
760         return (p && !p->final()) ? p : 0;
761 }
762
763
764 bool MathCursor::inMacroArgMode() const
765 {
766         return pos() > 0 && prevAtom()->getChar() == '#';
767 }
768
769
770 bool MathCursor::selection() const
771 {
772         return selection_;
773 }
774
775
776 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
777 {
778         for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
779                 MathGridInset * p = Cursor_[i].par_->asGridInset();
780                 if (p) {
781                         idx = Cursor_[i].idx_;
782                         return p;
783                 }
784         }
785         return 0;
786 }
787
788
789 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
790 {
791         for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
792                 MathHullInset * p = Cursor_[i].par_->asHullInset();
793                 if (p) {
794                         idx = Cursor_[i].idx_;
795                         return p;
796                 }
797         }
798         return 0;
799 }
800
801
802 void MathCursor::popToEnclosingGrid()
803 {
804         while (depth() && !Cursor_.back().par_->asGridInset())
805                 Cursor_.pop_back();
806 }
807
808
809 void MathCursor::popToEnclosingHull()
810 {
811         while (depth() && !Cursor_.back().par_->asHullInset())
812                 Cursor_.pop_back();
813 }
814
815
816 void MathCursor::pullArg(bool goright)
817 {
818         dump("pullarg");
819         MathArray a = array();
820
821         MathScriptInset const * p = par()->asScriptInset();
822         if (p) {
823                 // special handling for scripts
824                 const bool up = p->hasUp();
825                 popLeft();
826                 MathScriptInset * q = nextAtom()->asScriptInset();
827                 if (q)
828                         q->removeScript(up);
829                 ++pos();
830                 array().insert(pos(), a);
831                 return;
832         }
833
834         if (popLeft()) {
835                 plainErase();
836                 array().insert(pos(), a);
837                 if (goright)
838                         pos() += a.size();
839         } else {
840                 formula()->mutateToText();
841         }
842 }
843
844
845 void MathCursor::touch()
846 {
847         cursor_type::const_iterator it = Cursor_.begin();
848         cursor_type::const_iterator et = Cursor_.end();
849         for ( ; it != et; ++it)
850                 it->xcell().touch();
851 }
852
853
854 void MathCursor::normalize()
855 {
856 #if 0
857         // rebreak
858         {
859                 MathIterator it = ibegin(formula()->par().nucleus());
860                 MathIterator et = iend(formula()->par().nucleus());
861                 for (; it != et; ++it)
862                         if (it.par()->asBoxInset())
863                                 it.par()->asBoxInset()->rebreak();
864         }
865 #endif
866
867         if (idx() >= par()->nargs()) {
868                 lyxerr << "this should not really happen - 1: "
869                        << idx() << " " << par()->nargs() << "\n";
870                 dump("error 2");
871         }
872         idx() = min(idx(), par()->nargs() - 1);
873
874         if (pos() > size()) {
875                 lyxerr << "this should not really happen - 2: "
876                         << pos() << " " << size() <<  " in idx: " << idx()
877                         << " in atom: '";
878                 WriteStream wi(lyxerr, false, true);
879                 par()->write(wi);
880                 lyxerr << "\n";
881                 dump("error 4");
882         }
883         pos() = min(pos(), size());
884
885         // remove empty scripts if possible
886         if (1) {
887                 for (pos_type i = 0; i < size(); ++i) {
888                         MathScriptInset * p = array()[i]->asScriptInset();
889                         if (p) {
890                                 p->removeEmptyScripts();
891                                 //if (p->empty())
892                                 //      array().erase(i);
893                         }
894                 }
895         }
896
897         // fix again position
898         pos() = min(pos(), size());
899 }
900
901
902 MathCursor::size_type MathCursor::size() const
903 {
904         return array().size();
905 }
906
907
908 MathCursor::col_type MathCursor::hullCol() const
909 {
910         idx_type idx = 0;
911         MathHullInset * p = enclosingHull(idx);
912         return p->col(idx);
913 }
914
915
916 MathCursor::row_type MathCursor::hullRow() const
917 {
918         idx_type idx = 0;
919         MathHullInset * p = enclosingHull(idx);
920         return p->row(idx);
921 }
922
923
924 bool MathCursor::hasPrevAtom() const
925 {
926         return pos() > 0;
927 }
928
929
930 bool MathCursor::hasNextAtom() const
931 {
932         return pos() < size();
933 }
934
935
936 MathAtom const & MathCursor::prevAtom() const
937 {
938         lyx::Assert(pos() > 0);
939         return array()[pos() - 1];
940 }
941
942
943 MathAtom & MathCursor::prevAtom()
944 {
945         lyx::Assert(pos() > 0);
946         return array()[pos() - 1];
947 }
948
949
950 MathAtom const & MathCursor::nextAtom() const
951 {
952         lyx::Assert(pos() < size());
953         return array()[pos()];
954 }
955
956
957 MathAtom & MathCursor::nextAtom()
958 {
959         lyx::Assert(pos() < size());
960         return array()[pos()];
961 }
962
963
964 MathArray & MathCursor::array() const
965 {
966         static MathArray dummy;
967
968         if (idx() >= par()->nargs()) {
969                 lyxerr << "############  idx_ " << idx() << " not valid\n";
970                 return dummy;
971         }
972
973         if (depth() == 0) {
974                 lyxerr << "############  depth() == 0 not valid\n";
975                 return dummy;
976         }
977
978         return cursor().cell();
979 }
980
981
982 MathXArray & MathCursor::xarray() const
983 {
984         static MathXArray dummy;
985
986         if (depth() == 0) {
987                 lyxerr << "############  depth() == 0 not valid\n";
988                 return dummy;
989         }
990
991         return cursor().xcell();
992 }
993
994
995 void MathCursor::idxNext()
996 {
997         par()->idxNext(idx(), pos());
998 }
999
1000
1001 void MathCursor::idxPrev()
1002 {
1003         par()->idxPrev(idx(), pos());
1004 }
1005
1006
1007 void MathCursor::splitCell()
1008 {
1009         if (idx() + 1 == par()->nargs())
1010                 return;
1011         MathArray ar = array();
1012         ar.erase(0, pos());
1013         array().erase(pos(), size());
1014         ++idx();
1015         pos() = 0;
1016         array().insert(0, ar);
1017 }
1018
1019
1020 void MathCursor::breakLine()
1021 {
1022         // leave inner cells
1023         while (popRight())
1024                 ;
1025
1026         idx_type dummy;
1027         MathHullInset * p = enclosingHull(dummy);
1028         if (!p)
1029                 return;
1030
1031         if (p->getType() == "simple" || p->getType() == "equation") {
1032                 p->mutate("eqnarray");
1033                 idx() = 1;
1034                 pos() = 0;
1035         } else {
1036                 p->addRow(hullRow());
1037
1038                 // split line
1039                 const row_type r = hullRow();
1040                 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1041                         p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1042
1043                 // split cell
1044                 splitCell();
1045                 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1046         }
1047 }
1048
1049
1050 //void MathCursor::readLine(MathArray & ar) const
1051 //{
1052 //      idx_type base = row() * par()->ncols();
1053 //      for (idx_type off = 0; off < par()->ncols(); ++off)
1054 //              ar.push_back(par()->cell(base + off));
1055 //}
1056
1057
1058 char MathCursor::valign() const
1059 {
1060         idx_type idx;
1061         MathGridInset * p = enclosingGrid(idx);
1062         return p ? p->valign() : '\0';
1063 }
1064
1065
1066 char MathCursor::halign() const
1067 {
1068         idx_type idx;
1069         MathGridInset * p = enclosingGrid(idx);
1070         return p ? p->halign(idx % p->ncols()) : '\0';
1071 }
1072
1073
1074 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1075 {
1076         MathCursorPos anc = normalAnchor();
1077         if (anc < cursor()) {
1078                 i1 = anc;
1079                 i2 = cursor();
1080         } else {
1081                 i1 = cursor();
1082                 i2 = anc;
1083         }
1084 }
1085
1086
1087 MathCursorPos & MathCursor::cursor()
1088 {
1089         lyx::Assert(depth());
1090         return Cursor_.back();
1091 }
1092
1093
1094 MathCursorPos const & MathCursor::cursor() const
1095 {
1096         lyx::Assert(depth());
1097         return Cursor_.back();
1098 }
1099
1100
1101 bool MathCursor::goUpDown(bool up)
1102 {
1103         // Be warned: The 'logic' implemented in this function is highly fragile.
1104         // A distance of one pixel or a '<' vs '<=' _really_ matters.
1105         // So fiddle around with it only if you know what you are doing!
1106         int xlow, xhigh, ylow, yhigh;
1107
1108   int xo, yo;
1109         getPos(xo, yo);
1110
1111         // check if we had something else in mind, if not, this is the future goal
1112         if (targetx_ == -1)
1113                 targetx_ = xo;
1114         else
1115                 xo = targetx_;
1116
1117         // try neigbouring script insets
1118         // try left
1119         if (hasPrevAtom()) {
1120                 MathScriptInset * p = prevAtom()->asScriptInset();
1121                 if (p && p->has(up)) {
1122                         --pos();
1123                         push(nextAtom());
1124                         idx() = up; // the superscript has index 1
1125                         pos() = size();
1126                         ///lyxerr << "updown: handled by scriptinset to the left\n";
1127                         return true;
1128                 }
1129         }
1130
1131         // try right
1132         if (hasNextAtom()) {
1133                 MathScriptInset * p = nextAtom()->asScriptInset();
1134                 if (p && p->has(up)) {
1135                         push(nextAtom());
1136                         idx() = up;
1137                         pos() = 0;
1138                         ///lyxerr << "updown: handled by scriptinset to the right\n";
1139                         return true;
1140                 }
1141         }
1142
1143         // try current cell
1144         //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1145         //if (up)
1146         //      yhigh = yo - 4;
1147         //else
1148         //      ylow = yo + 4;
1149         //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1150         //      lyxerr << "updown: handled by brute find in the same cell\n";
1151         //      return true;
1152         //}
1153
1154         // try to find an inset that knows better then we
1155         while (1) {
1156                 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1157                 // ask inset first
1158                 if (par()->idxUpDown(idx(), up)) {
1159                         // we found a cell that thinks it has something "below" us.
1160                         ///lyxerr << "updown: found inset that handles UpDown\n";
1161                         xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1162                         // project (xo,yo) onto proper box
1163                         ///lyxerr << "\n   xo: " << xo << " yo: " << yo
1164                         ///       << "\n   xlow: " << xlow << " ylow: " << ylow
1165                         ///       << "\n   xhigh: " << xhigh << " yhigh: " << yhigh;
1166                         xo = min(max(xo, xlow), xhigh);
1167                         yo = min(max(yo, ylow), yhigh);
1168                         ///lyxerr << "\n   xo2: " << xo << " yo2: " << yo << "\n";
1169                         bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1170                         ///lyxerr << "updown: handled by final brute find\n";
1171                         return true;
1172                 }
1173
1174                 // leave inset
1175                 if (!popLeft()) {
1176                         // no such inset found, just take something "above"
1177                         ///lyxerr << "updown: handled by strange case\n";
1178                         return
1179                                 bruteFind(xo, yo,
1180                                         formula()->xlow(),
1181                                         formula()->xhigh(),
1182                                         up ? formula()->ylow() : yo + 4,
1183                                         up ? yo - 4 : formula()->yhigh()
1184                                 );
1185                 }
1186
1187                 // any improvement so far?
1188                 int xnew, ynew;
1189                 getPos(xnew, ynew);
1190                 if (up ? ynew < yo : ynew > yo)
1191                         return true;
1192         }
1193 }
1194
1195
1196 bool MathCursor::bruteFind
1197         (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1198 {
1199         cursor_type best_cursor;
1200         double best_dist = 1e10;
1201
1202         MathIterator it = ibegin(formula()->par().nucleus());
1203         MathIterator et = iend(formula()->par().nucleus());
1204         while (1) {
1205                 // avoid invalid nesting when selecting
1206                 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1207                         MathCursorPos const & top = it.position();
1208                         int xo = top.xpos();
1209                         int yo = top.ypos();
1210                         if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1211                                 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1212                                 // '<=' in order to take the last possible position
1213                                 // this is important for clicking behind \sum in e.g. '\sum_i a'
1214                                 if (d <= best_dist) {
1215                                         best_dist   = d;
1216                                         best_cursor = it.cursor();
1217                                 }
1218                         }
1219                 }
1220
1221                 if (it == et)
1222                         break;
1223                 ++it;
1224         }
1225
1226         if (best_dist < 1e10)
1227                 Cursor_ = best_cursor;
1228         return best_dist < 1e10;
1229 }
1230
1231
1232 bool MathCursor::idxLineFirst()
1233 {
1234         idx() -= idx() % par()->ncols();
1235         pos() = 0;
1236         return true;
1237 }
1238
1239
1240 bool MathCursor::idxLineLast()
1241 {
1242         idx() -= idx() % par()->ncols();
1243         idx() += par()->ncols() - 1;
1244         pos() = size();
1245         return true;
1246 }
1247
1248 bool MathCursor::idxLeft()
1249 {
1250         return par()->idxLeft(idx(), pos());
1251 }
1252
1253
1254 bool MathCursor::idxRight()
1255 {
1256         return par()->idxRight(idx(), pos());
1257 }
1258
1259
1260 bool MathCursor::interpret(string const & s)
1261 {
1262         //lyxerr << "interpret 1: '" << s << "'\n";
1263         if (s.empty())
1264                 return true;
1265
1266         //lyxerr << "char: '" << s[0] << "'  int: " << int(s[0]) << endl;
1267         //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1268         //lyxerr << "trans: '" << s[0] << "'  int: " << int(s[0]) << endl;
1269
1270         if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1271                 unsigned int n = 1;
1272                 istringstream is(s.substr(5).c_str());
1273                 is >> n;
1274                 n = max(1u, n);
1275                 niceInsert(MathAtom(new MathCasesInset(n)));
1276                 return true;
1277         }
1278
1279         if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1280                 unsigned int m = 1;
1281                 unsigned int n = 1;
1282                 string v_align;
1283                 string h_align;
1284                 istringstream is(s.substr(6).c_str());
1285                 is >> m >> n >> v_align >> h_align;
1286                 m = max(1u, m);
1287                 n = max(1u, n);
1288                 v_align += 'c';
1289                 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1290                 return true;
1291         }
1292
1293         if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1294                 ReplaceData rep;
1295                 istringstream is(s.substr(7).c_str());
1296                 string from, to;
1297                 is >> from >> to;
1298                 mathed_parse_cell(rep.from, from);
1299                 mathed_parse_cell(rep.to, to);
1300                 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1301                 par()->replace(rep);
1302                 return true;
1303         }
1304
1305         string name = s.substr(1);
1306
1307         if (name == "over" || name == "choose" || name == "atop") {
1308                 MathArray ar = array();
1309                 MathAtom t(createMathInset(name));
1310                 t->asNestInset()->cell(0).swap(array());
1311                 pos() = 0;
1312                 niceInsert(t);
1313                 popRight();
1314                 left();
1315                 return true;
1316         }
1317
1318         // prevent entering of recursive macros
1319         if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1320                 && formula()->getInsetName() == name)
1321         {
1322                 lyxerr << "can't enter recursive macro\n";
1323                 return true;
1324         }
1325
1326         niceInsert(createMathInset(name));
1327         return true;
1328 }
1329
1330
1331 bool MathCursor::script(bool up)
1332 {
1333         // Hack to get \\^ and \\_ working
1334         if (inMacroMode() && macroName() == "\\") {
1335                 if (up)
1336                         interpret("\\mathcircumflex");
1337                 else
1338                         interpret('_');
1339                 return true;
1340         }
1341
1342         macroModeClose();
1343         MathGridInset safe = grabAndEraseSelection();
1344         if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1345                 prevAtom()->asScriptInset()->ensure(up);
1346                 pushRight(prevAtom());
1347                 idx() = up;
1348                 pos() = size();
1349         } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1350                 nextAtom()->asScriptInset()->ensure(up);
1351                 pushLeft(nextAtom());
1352                 idx() = up;
1353                 pos() = 0;
1354         } else {
1355                 plainInsert(MathAtom(new MathScriptInset(up)));
1356                 prevAtom()->asScriptInset()->ensure(up);
1357                 pushRight(prevAtom());
1358                 idx() = up;
1359                 pos() = 0;
1360         }
1361         paste(safe);
1362         dump("1");
1363         return true;
1364 }
1365
1366
1367 bool MathCursor::inMathMode() const
1368 {
1369         if (par()->asBoxInset())
1370                 return false;
1371         if (par()->asFboxInset())
1372                 return false;
1373         if (par()->asParboxInset())
1374                 return false;
1375         if (par()->asParInset())
1376                 return false;
1377         return true;
1378 }
1379
1380
1381 bool MathCursor::interpret(char c)
1382 {
1383         //lyxerr << "interpret 2: '" << c << "'\n";
1384         targetx_ = -1; // "no target"
1385         if (inMacroArgMode()) {
1386                 --pos();
1387                 plainErase();
1388                 int n = c - '0';
1389                 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1390                 if (p && 1 <= n && n <= p->numargs())
1391                         insert(MathAtom(new MathMacroArgument(c - '0')));
1392                 else {
1393                         insert(createMathInset("#"));
1394                         interpret(c); // try again
1395                 }
1396                 return true;
1397         }
1398
1399         // handle macroMode
1400         if (inMacroMode()) {
1401                 string name = macroName();
1402                 //lyxerr << "interpret name: '" << name << "'\n";
1403
1404                 if (name.empty() && c == '\\') {
1405                         backspace();
1406                         interpret("\\backslash");
1407                         return true;
1408                 }
1409
1410                 if (isalpha(c)) {
1411                         inMacroMode()->name() += c;
1412                         return true;
1413                 }
1414
1415                 // handle 'special char' macros
1416                 if (name == "\\") {
1417                         // remove the '\\'
1418                         backspace();
1419                         if (c == '\\')
1420                                 interpret("\\backslash");
1421                         else
1422                                 interpret(string("\\") + c);
1423                         return true;
1424                 }
1425
1426                 // leave macro mode and try again if necessary
1427                 macroModeClose();
1428                 if (c != ' ')
1429                         interpret(c);
1430                 return true;
1431         }
1432
1433         // This is annoying as one has to press <space> far too often.
1434         // Disable it.
1435
1436         if (0) {
1437                 // leave autocorrect mode if necessary
1438                 if (autocorrect_ && c == ' ') {
1439                         autocorrect_ = false;
1440                         return true;
1441                 }
1442         }
1443
1444         // just clear selection on pressing the space bar
1445         if (selection_ && c == ' ') {
1446                 selection_ = false;
1447                 return true;
1448         }
1449
1450         selClearOrDel();
1451
1452         if (!inMathMode()) {
1453                 // suppress direct insertion of two spaces in a row
1454                 // the still allows typing  '<space>a<space>' and deleting the 'a', but
1455                 // it is better than nothing...
1456                 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1457                         return true;
1458                 insert(c);
1459                 return true;
1460         }
1461
1462         if (c == '\\') {
1463                 lyxerr << "starting with macro\n";
1464                 insert(MathAtom(new MathUnknownInset("\\", false)));
1465                 return true;
1466         }
1467
1468         if (c == ' ') {
1469                 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1470                         prevAtom()->asSpaceInset()->incSpace();
1471                         return true;
1472                 }
1473                 if (popRight())
1474                         return true;
1475                 // if are at the very end, leave the formula
1476                 return pos() != size();
1477         }
1478
1479         if (c == '#') {
1480                 insert(c); // LM_TC_TEX;
1481                 return true;
1482         }
1483
1484 /*
1485         if (c == '{' || c == '}', c)) {
1486                 insert(c); // LM_TC_TEX;
1487                 return true;
1488         }
1489 */
1490
1491         if (c == '{') {
1492                 niceInsert(MathAtom(new MathBraceInset));
1493                 return true;
1494         }
1495
1496         if (c == '}') {
1497                 return true;
1498         }
1499
1500         if (c == '$') {
1501                 insert(createMathInset("$"));
1502                 return true;
1503         }
1504
1505         if (c == '%') {
1506                 insert(createMathInset("%"));
1507                 return true;
1508         }
1509
1510 /*
1511         if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1512                 insert(c, LM_TC_VAR);
1513                 return true;
1514         }
1515
1516         if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1517                 insert(c, LM_TC_VAR);
1518                 lastcode_ = LM_TC_VAR;
1519                 return true;
1520         }
1521
1522         if (c == '\\') {
1523                 insert(c, LM_TC_TEX);
1524                 //bv->owner()->message(_("TeX mode"));
1525                 return true;
1526         }
1527 */
1528
1529         // try auto-correction
1530         //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1531         //      return true;
1532
1533         // no special circumstances, so insert the character without any fuss
1534         insert(c);
1535         autocorrect_ = true;
1536         return true;
1537 }
1538
1539
1540 void MathCursor::setSelection(cursor_type const & where, size_type n)
1541 {
1542         selection_ = true;
1543         Anchor_ = where;
1544         Cursor_ = where;
1545         cursor().pos_ += n;
1546 }
1547
1548
1549 void MathCursor::insetToggle()
1550 {
1551         if (hasNextAtom()) {
1552                 // toggle next inset ...
1553                 nextAtom()->lock(!nextAtom()->lock());
1554         } else if (popLeft() && hasNextAtom()) {
1555                 // ... or enclosing inset if we are in the last inset position
1556                 nextAtom()->lock(!nextAtom()->lock());
1557                 posRight();
1558         }
1559 }
1560
1561
1562 string MathCursor::info() const
1563 {
1564         ostringstream os;
1565         os << "Math editor mode.  ";
1566         for (int i = 0, n = depth(); i < n; ++i) {
1567                 Cursor_[i].par_->infoize(os);
1568                 os << "  ";
1569         }
1570         if (hasPrevAtom())
1571                 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1572                         prevAtom()->infoize(os);
1573         os << "                    ";
1574         return os.str().c_str(); // .c_str() needed for lyxstring
1575 }
1576
1577
1578 unsigned MathCursor::depth() const
1579 {
1580         return Cursor_.size();
1581 }
1582
1583
1584
1585
1586 namespace {
1587
1588 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1589         MathInset::row_type & r1, MathInset::row_type & r2,
1590         MathInset::col_type & c1, MathInset::col_type & c2)
1591 {
1592         MathInset * p = i1.par_;
1593         c1 = p->col(i1.idx_);
1594         c2 = p->col(i2.idx_);
1595         if (c1 > c2)
1596                 swap(c1, c2);
1597         r1 = p->row(i1.idx_);
1598         r2 = p->row(i2.idx_);
1599         if (r1 > r2)
1600                 swap(r1, r2);
1601 }
1602
1603 }
1604
1605
1606 MathGridInset MathCursor::grabSelection() const
1607 {
1608         if (!selection_)
1609                 return MathGridInset();
1610         MathCursorPos i1;
1611         MathCursorPos i2;
1612         getSelection(i1, i2);
1613         // shouldn't we assert on i1.par_ == i2.par_?
1614         if (i1.idx_ == i2.idx_) {
1615                 MathGridInset data(1, 1);
1616                 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1617                 return data;
1618         }
1619         row_type r1, r2;
1620         col_type c1, c2;
1621         region(i1, i2, r1, r2, c1, c2);
1622         MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1623         for (row_type row = 0; row < data.nrows(); ++row)
1624                 for (col_type col = 0; col < data.ncols(); ++col) {
1625                         idx_type i = i1.par_->index(row + r1, col + c1);
1626                         data.cell(data.index(row, col)) = i1.par_->cell(i);
1627                 }
1628         return data;
1629 }
1630
1631
1632 void MathCursor::eraseSelection()
1633 {
1634         MathCursorPos i1;
1635         MathCursorPos i2;
1636         getSelection(i1, i2);
1637         if (i1.idx_ == i2.idx_)
1638                 i1.cell().erase(i1.pos_, i2.pos_);
1639         else {
1640                 MathInset * p = i1.par_;
1641                 row_type r1, r2;
1642                 col_type c1, c2;
1643                 region(i1, i2, r1, r2, c1, c2);
1644                 for (row_type row = r1; row <= r2; ++row)
1645                         for (col_type col = c1; col <= c2; ++col)
1646                                 p->cell(p->index(row, col)).erase();
1647         }
1648         cursor() = i1;
1649 }
1650
1651
1652 MathGridInset MathCursor::grabAndEraseSelection()
1653 {
1654         if (!selection_)
1655                 return MathGridInset();
1656         MathGridInset res = grabSelection();
1657         eraseSelection();
1658         selection_ = false;
1659         return res;
1660 }
1661
1662
1663 MathCursorPos MathCursor::normalAnchor() const
1664 {
1665         if (Anchor_.size() < depth()) {
1666                 Anchor_ = Cursor_;
1667                 lyxerr << "unusual Anchor size\n";
1668         }
1669         //lyx::Assert(Anchor_.size() >= cursor.depth());
1670         // use Anchor on the same level as Cursor
1671         MathCursorPos normal = Anchor_[depth() - 1];
1672         if (depth() < Anchor_.size() && !(normal < cursor())) {
1673                 // anchor is behind cursor -> move anchor behind the inset
1674                 ++normal.pos_;
1675         }
1676         return normal;
1677 }
1678
1679
1680
1681 void MathCursor::handleExtern(const string & arg)
1682 {
1683         string lang;
1684         string extra;
1685         istringstream iss(arg.c_str());
1686         iss >> lang >> extra;
1687         if (extra.empty())
1688                 extra = "noextra";
1689
1690
1691         if (selection()) {
1692                 MathArray ar;
1693                 selGet(ar);
1694                 lyxerr << "use selection: " << ar << "\n";
1695                 insert(pipeThroughExtern(lang, extra, ar));
1696                 return;
1697         }
1698
1699         MathArray eq;
1700         eq.push_back(MathAtom(new MathCharInset('=')));
1701
1702         popToEnclosingHull();
1703
1704         idx_type idx = 0;
1705         MathHullInset * hull = enclosingHull(idx);
1706         lyx::Assert(hull);
1707         idxLineFirst();
1708
1709         if (hull->getType() == "simple") {
1710                 MathArray::size_type pos = cursor().cell().find_last(eq);
1711                 MathArray ar;
1712                 if (pos == size()) {
1713                         ar = array();
1714                         lyxerr << "use whole cell: " << ar << "\n";
1715                 } else {
1716                         ar = MathArray(array(), pos + 1, size());
1717                         lyxerr << "use partial cell form pos: " << pos << "\n";
1718                 }
1719                 end();
1720                 insert(eq);
1721                 insert(pipeThroughExtern(lang, extra, ar));
1722                 return;
1723         }
1724
1725         if (hull->getType() == "equation") {
1726                 lyxerr << "use equation inset\n";
1727                 hull->mutate("eqnarray");
1728                 MathArray & ar = cursor().cell();
1729                 lyxerr << "use cell: " << ar << "\n";
1730                 idxRight();
1731                 cursor().cell() = eq;
1732                 idxRight();
1733                 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1734                 idxLineLast();
1735                 return;
1736         }
1737
1738         {
1739                 lyxerr << "use eqnarray\n";
1740                 idxLineLast();
1741                 MathArray ar = cursor().cell();
1742                 lyxerr << "use cell: " << ar << "\n";
1743                 breakLine();
1744                 idxRight();
1745                 cursor().cell() = eq;
1746                 idxRight();
1747                 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1748                 idxLineLast();
1749         }
1750
1751 }
1752
1753
1754 int MathCursor::dispatch(string const & cmd)
1755 {
1756         // try to dispatch to adajcent items if they are not editable
1757         // actually, this should only happen for mouse clicks...
1758         if (hasNextAtom() && !openable(nextAtom(), false))
1759                 if (int res = nextAtom()->dispatch(cmd, 0, 0))
1760                         return res;
1761         if (hasPrevAtom() && !openable(prevAtom(), false))
1762                 if (int res = prevAtom()->dispatch(cmd, 0, 0))
1763                         return res;
1764
1765         for (int i = Cursor_.size() - 1; i >= 0; --i) {
1766                 MathCursorPos & pos = Cursor_[i];
1767                 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))
1768                         return res;
1769         }
1770         return 0;
1771 }