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