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