]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
5241a194a50b9c79210b8239f54f09e067ce653e
[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 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include <config.h>
23 #include <algorithm>
24 #include <cctype>
25
26 #include "support/lstrings.h"
27 #include "debug.h"
28 #include "LColor.h"
29 #include "Painter.h"
30 #include "support.h"
31 #include "formulabase.h"
32 #include "math_cursor.h"
33 #include "math_factory.h"
34 #include "math_arrayinset.h"
35 #include "math_charinset.h"
36 #include "math_deliminset.h"
37 #include "math_matrixinset.h"
38 #include "math_scriptinset.h"
39 #include "math_spaceinset.h"
40 #include "math_parser.h"
41
42 using std::endl;
43 using std::min;
44 using std::max;
45 using std::isalnum;
46
47
48 namespace {
49
50 struct Selection
51 {
52         void grab(MathCursor const & cursor)
53         {
54                 data_.clear();
55                 MathCursorPos i1;
56                 MathCursorPos i2;
57                 cursor.getSelection(i1, i2); 
58                 if (i1.idx_ == i2.idx_)
59                         data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
60                 else {
61                         std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
62                         for (unsigned i = 0; i < indices.size(); ++i)
63                                 data_.push_back(i1.cell(indices[i]));
64                 }
65         }
66
67         void erase(MathCursor & cursor)
68         {
69                 MathCursorPos i1;
70                 MathCursorPos i2;
71                 cursor.getSelection(i1, i2); 
72                 if (i1.idx_ == i2.idx_)
73                         i1.cell().erase(i1.pos_, i2.pos_);
74                 else {
75                         std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
76                         for (unsigned i = 0; i < indices.size(); ++i)
77                                 i1.cell(indices[i]).erase();
78                 }
79                 cursor.cursor() = i1;
80         }
81
82         void paste(MathCursor & cursor) const
83         {
84                 cursor.insert(glue());
85         }
86
87         // glues selection to one cell
88         MathArray glue() const
89         {
90                 MathArray ar;
91                 for (unsigned i = 0; i < data_.size(); ++i)
92                         ar.push_back(data_[i]);
93                 return ar;
94         }
95
96         void clear()
97         {
98                 data_.clear();
99         }
100
101         std::vector<MathArray> data_;
102 };
103
104
105 Selection theSelection;
106
107
108 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
109 {
110         os << "(par: " << p.par_ << " idx: " << p.idx_
111            << " pos: " << p.pos_ << ")";
112         return os;
113 }
114
115 }
116
117
118 MathCursor::MathCursor(InsetFormulaBase * formula)
119         : formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
120 {
121         first();
122 }
123
124
125 void MathCursor::pushLeft(MathInset * par)
126 {
127         MathCursorPos p;
128         p.par_ = par;
129         par->idxFirst(p.idx_, p.pos_);
130         Cursor_.push_back(p);
131 }
132
133
134 void MathCursor::pushRight(MathInset * par)
135 {
136         posLeft();
137         MathCursorPos p;
138         p.par_ = par;
139         par->idxLast(p.idx_, p.pos_);
140         Cursor_.push_back(p);
141 }
142
143
144 bool MathCursor::popLeft()
145 {
146         if (Cursor_.size() <= 1)
147                 return false;
148         Cursor_.pop_back();
149         return true;
150 }
151
152 bool MathCursor::popRight()
153 {
154         if (Cursor_.size() <= 1)
155                 return false;
156         Cursor_.pop_back();
157         posRight();
158         return true;
159 }
160
161
162 MathInset * MathCursor::parInset(int i) const
163 {
164         return Cursor_[i].par_;
165 }
166
167
168 void MathCursor::dump(char const * what) const
169 {
170         return;
171
172         lyxerr << "MC: " << what << "\n";
173         for (unsigned i = 0; i < Cursor_.size(); ++i)
174                 lyxerr << "  i: " << i 
175                         << " pos: " << Cursor_[i].pos_
176                         << " idx: " << Cursor_[i].idx_
177                         << " par: " << Cursor_[i].par_ << "\n";
178
179         //lyxerr        << " sel: " << selection_ << " data: " << array() << "\n";
180 }
181
182
183 void MathCursor::seldump(char const * str) const
184 {
185         //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
186         //dump("   Pos");
187         return;
188
189         lyxerr << "\n\n\n=================vvvvvvvvvvvvv=======================   "
190                 <<  str << "\ntheSelection: " << selection_
191                 << " '" << theSelection.glue() << "'\n";
192         for (unsigned int i = 0; i < Cursor_.size(); ++i) 
193                 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
194         lyxerr << "\n";
195         for (unsigned int i = 0; i < Anchor_.size(); ++i) 
196                 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
197         //lyxerr << "\ncursor.pos_: " << pos();
198         //lyxerr << "\nanchor.pos_: " << anchor().pos_;
199         lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
200 }
201
202
203 bool MathCursor::isInside(MathInset const * p) const
204 {
205         for (unsigned i = 0; i < Cursor_.size(); ++i) 
206                 if (parInset(i) == p) 
207                         return true;
208         return false;
209 }
210
211
212 bool MathCursor::openable(MathInset * p, bool sel) const
213 {
214         if (!p)
215                 return false;
216
217         if (!p->isActive())
218                 return false;
219
220         if (sel) {
221                 // we can't move into anything new during selection
222                 if (Cursor_.size() == Anchor_.size())
223                         return false;
224                 if (p != Anchor_[Cursor_.size()].par_)
225                         return false;
226         }
227         return true;
228 }
229
230
231 bool MathCursor::positionable(MathInset * p, bool sel) const
232 {
233         if (!p)
234                 return false;
235
236         if (!p->nargs())
237                 return false;
238
239         if (sel) {
240                 // we can't move into anything new during selection
241                 if (Cursor_.size() == Anchor_.size())
242                         return false;
243                 if (p != Anchor_[Cursor_.size()].par_)
244                         return false;
245         }
246         return true;
247 }
248
249
250 bool MathCursor::posLeft()
251 {
252         if (pos() == 0)
253                 return false;
254         --pos();
255         return true;
256 }
257
258
259 bool MathCursor::posRight()
260 {
261         if (pos() == size())
262                 return false;
263         ++pos();
264         return true;
265 }
266
267
268 bool MathCursor::left(bool sel)
269 {
270         dump("Left 1");
271         if (inMacroMode()) {
272                 macroModeClose();
273                 lastcode_ = LM_TC_VAR;
274                 return true;
275         }
276         selHandle(sel);
277         lastcode_ = LM_TC_VAR;
278
279         MathInset * p = prevInset();
280         if (openable(p, sel)) {
281                 pushRight(p);
282                 return true;
283         } 
284         
285         return posLeft() || idxLeft() || popLeft();
286 }
287
288
289 bool MathCursor::right(bool sel)
290 {
291         dump("Right 1");
292         if (inMacroMode()) {
293                 macroModeClose();
294                 lastcode_ = LM_TC_VAR;
295                 return true;
296         }
297         selHandle(sel);
298         lastcode_ = LM_TC_VAR;
299
300         MathInset * p = nextInset();
301         if (openable(p, sel)) {
302                 pushLeft(p);
303                 return true;
304         }
305
306         return posRight() || idxRight() || popRight();
307 }
308
309
310 void MathCursor::first()
311 {
312         Cursor_.clear();
313         pushLeft(outerPar());
314 }
315
316
317 void MathCursor::last()
318 {
319         first();
320         end();
321 }
322
323
324 void MathCursor::setPos(int x, int y)
325 {
326         dump("setPos 1");
327         //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
328
329         macroModeClose();
330         lastcode_ = LM_TC_VAR;
331         first();
332
333         cursor().par_  = outerPar();
334
335         while (1) {
336                 idx() = -1;
337                 cursor().pos_ = -1;
338                 //lyxerr << "found idx: " << idx_ << " cursor: " << pos()  << "\n";
339                 int distmin = 1 << 30; // large enough
340                 for (int i = 0; i < par()->nargs(); ++i) {
341                         MathXArray const & ar = par()->xcell(i);
342                         int x1 = x - ar.xo();
343                         int y1 = y - ar.yo();
344                         int c  = ar.x2pos(x1);
345                         int xx = abs(x1 - ar.pos2x(c));
346                         int yy = abs(y1);
347                         //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
348                         //      << " c: " << c  << " xo: " << ar.xo() << "\n";
349                         if (yy + xx <= distmin) {
350                                 distmin        = yy + xx;
351                                 idx()  = i;
352                                 pos()  = c;
353                         }
354                 }
355                 //lyxerr << "found idx: " << idx() << " cursor: "
356                 //      << pos()  << "\n";
357                 MathInset * n = nextInset();
358                 MathInset * p = prevInset();
359                 if (positionable(n, selection_) && n->covers(x, y))
360                         pushLeft(n);
361                 else if (positionable(p, selection_) && p->covers(x, y))
362                         pushRight(p);
363                 else 
364                         break;
365         }
366         dump("setPos 2");
367 }
368
369
370 void MathCursor::home()
371 {
372         dump("home 1");
373         macroModeClose();
374         lastcode_ = LM_TC_VAR;
375         if (!par()->idxHome(idx(), pos())) 
376                 popLeft();
377         dump("home 2");
378 }
379
380
381 void MathCursor::end()
382 {
383         dump("end 1");
384         macroModeClose();
385         lastcode_ = LM_TC_VAR;
386         if (!par()->idxEnd(idx(), pos()))
387                 popRight();
388         dump("end 2");
389 }
390
391
392 void MathCursor::plainErase()
393 {
394         array().erase(pos());
395 }
396
397
398 void MathCursor::insert(char c, MathTextCodes t)
399 {
400         //lyxerr << "inserting '" << c << "'\n";
401         array().insert(pos(), new MathCharInset(c, t));
402         posRight();
403 }
404
405
406 void MathCursor::insert(MathInset * p)
407 {
408         macroModeClose();
409
410         if (selection_) {
411                 if (p->nargs())
412                         selCut();
413                 else
414                         selDel();
415         }
416
417         array().insert(pos(), p);
418         posRight();
419 }
420
421
422 void MathCursor::niceInsert(MathInset * p) 
423 {
424         if (!p) {
425                 lyxerr << "should not happen\n";
426                 return;
427         }
428         selCut();
429         insert(p);
430         if (p->nargs()) {
431                 posLeft();
432                 right();  // do not push for e.g. MathSymbolInset
433                 selPaste();
434         }
435         p->metrics(p->size());
436 }
437
438
439 void MathCursor::insert(MathArray const & ar)
440 {
441         macroModeClose();
442         if (selection_)
443                 selCut();
444
445         array().insert(pos(), ar);
446         pos() += ar.size();
447 }
448
449
450 void MathCursor::backspace()
451 {
452         if (inMacroMode()) {
453                 left();
454                 return;
455         }
456
457         if (posLeft()) {
458                 plainErase();
459                 return;
460         }
461
462         if (size()) {
463                 pullArg(false);
464                 return;
465         }
466
467         erase();
468 }
469
470
471 void MathCursor::erase()
472 {
473         dump("erase 1");
474         if (inMacroMode())
475                 return;
476
477         if (selection_) {
478                 selDel();
479                 return;
480         }
481
482         // delete empty cells if necessary
483         if (pos() == 0 && array().empty()) {
484                 bool popit;
485                 bool removeit;
486                 par()->idxDelete(idx(), popit, removeit);
487                 if (popit && popLeft() && removeit)
488                         plainErase();
489                 return;
490         }
491
492         if (pos() < size())
493                 plainErase();
494
495         dump("erase 2");
496 }
497
498
499 void MathCursor::delLine()
500 {
501         macroModeClose();
502
503         if (selection_) {
504                 selDel();
505                 return;
506         }
507
508         if (par()->nrows() > 1)
509                 par()->delRow(row());
510 }
511
512
513 bool MathCursor::up(bool sel)
514 {
515         dump("up 1");
516         macroModeClose();
517         selHandle(sel);
518
519         if (selection_)
520                 return goUp();
521
522         // check whether we could move into an inset on the right or on the left
523         MathInset * p = nextInset();
524         if (p) {
525                 int idxx, poss;
526                 if (p->idxFirstUp(idxx, poss)) {
527                         pushLeft(p);
528                         idx() = idxx;
529                         pos() = poss;
530                         dump("up 3");
531                         return true;
532                 }
533         }
534
535         p = prevInset();
536         if (p) {
537                 int idxx, poss;
538                 if (p->idxLastUp(idxx, poss)) {
539                         pushRight(p);
540                         idx() = idxx;
541                         pos() = poss;
542                         dump("up 4");
543                         return true;
544                 }
545         }
546
547         return goUp();
548 }
549
550
551 bool MathCursor::down(bool sel)
552 {
553         dump("down 1");
554         macroModeClose();
555         selHandle(sel);
556
557         if (selection_) 
558                 return goDown();
559
560         // check whether we could move into an inset on the right or on the left
561         MathInset * p = nextInset();
562         if (p) {
563                 int idxx = 0;
564                 int poss = 0;
565                 if (p->idxFirstDown(idxx, poss)) {
566                         pushLeft(p);
567                         idx() = idxx;
568                         pos() = poss;
569                         dump("Down 3");
570                         return true;
571                 }
572         }
573
574         p = prevInset();
575         if (p) {
576                 int idxx = 0;
577                 int poss = 0;
578                 if (p->idxLastDown(idxx, poss)) {
579                         pushRight(p);
580                         idx() = idxx;
581                         pos() = poss;
582                         dump("Down 4");
583                         return true;
584                 }
585         }
586
587         return goDown();
588 }
589
590
591 bool MathCursor::toggleLimits()
592 {
593         MathScriptInset * p = prevScriptInset();
594         if (!p)
595                 return false;
596         int old = p->limits();
597         p->limits(old < 0 ? 1 : -1);
598         return old != p->limits();
599 }
600
601
602 void MathCursor::setSize(MathStyles size)
603 {
604         par()->userSetSize(size);
605 }
606
607
608 void MathCursor::macroModeClose()
609 {
610         string s = macroName();
611         if (s.size()) {
612                 pos() = pos() - s.size();
613                 for (unsigned i = 0; i < s.size(); ++i)
614                         plainErase();
615                 lastcode_ = LM_TC_VAR;
616                 interpret("\\" + s);
617         }
618 }
619
620
621 string MathCursor::macroName() const
622 {
623         string s;
624         int p = pos() - 1;
625         while (p >= 0 && array().nextInset(p)->code() == LM_TC_TEX) {
626                 s = array().nextInset(p)->getChar() + s;
627                 --p;
628         }
629         return s;
630 }
631
632
633 void MathCursor::selCopy()
634 {
635         seldump("selCopy");
636         if (selection_) {
637                 theSelection.grab(*this);
638                 selClear();
639         }
640 }
641
642
643 void MathCursor::selCut()
644 {
645         seldump("selCut");
646         if (selection_) {
647                 theSelection.grab(*this);
648                 theSelection.erase(*this);
649                 selClear();
650         } else {
651                 theSelection.clear();
652         }
653 }
654
655
656 void MathCursor::selDel()
657 {
658         seldump("selDel");
659         if (selection_) {
660                 theSelection.erase(*this);
661                 selClear();
662         }
663 }
664
665
666 void MathCursor::selPaste()
667 {
668         seldump("selPaste");
669         theSelection.paste(*this);
670         selClear();
671 }
672
673
674 void MathCursor::selHandle(bool sel)
675 {
676         if (sel == selection_)
677                 return;
678
679         theSelection.clear();
680         Anchor_    = Cursor_;
681         selection_ = sel;
682 }
683
684
685 void MathCursor::selStart()
686 {
687         seldump("selStart");
688         if (selection_)
689                 return;
690
691         theSelection.clear();
692         Anchor_ = Cursor_;
693         selection_ = true;
694 }
695
696
697 void MathCursor::selClear()
698 {
699         seldump("selClear");
700         selection_ = false;
701 }
702
703
704 void MathCursor::drawSelection(Painter & pain) const
705 {
706         if (!selection_)
707                 return;
708
709         MathCursorPos i1;
710         MathCursorPos i2;
711         getSelection(i1, i2);
712
713         //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
714
715         if (i1.idx_ == i2.idx_) {
716                 MathXArray & c = i1.xcell();
717                 int x1 = c.xo() + c.pos2x(i1.pos_);
718                 int y1 = c.yo() - c.ascent();
719                 int x2 = c.xo() + c.pos2x(i2.pos_);
720                 int y2 = c.yo() + c.descent();
721                 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
722         } else {
723                 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
724                 for (unsigned i = 0; i < indices.size(); ++i) {
725                         MathXArray & c = i1.xcell(indices[i]);
726                         int x1 = c.xo();
727                         int y1 = c.yo() - c.ascent();
728                         int x2 = c.xo() + c.width();
729                         int y2 = c.yo() + c.descent();
730                         pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
731                 }
732         }
733 }
734
735
736 MathTextCodes MathCursor::nextCode() const
737 {
738         //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
739         return LM_TC_VAR;
740 }
741
742
743 void MathCursor::handleFont(MathTextCodes t)
744 {
745         macroModeClose();
746         if (selection_) {
747                 MathCursorPos i1;
748                 MathCursorPos i2;
749                 getSelection(i1, i2); 
750                 if (i1.idx_ == i2.idx_) {
751                         MathArray & ar = i1.cell();
752                         for (int pos = i1.pos_; pos != i2.pos_; ++pos) 
753                                 ar.nextInset(pos)->handleFont(t);
754                 }
755         } else 
756                 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
757 }
758
759
760 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
761 {
762         handleNest(new MathDelimInset(l, r));
763 }
764
765
766 void MathCursor::handleNest(MathInset * p)
767 {
768         if (selection_) {
769                 selCut();
770                 p->cell(0) = theSelection.glue();
771         }
772         insert(p);
773         pushRight(p);
774 }
775
776
777 void MathCursor::getPos(int & x, int & y)
778 {
779 #ifdef WITH_WARNINGS
780 #warning This should probably take cellXOffset and cellYOffset into account
781 #endif
782         x = xarray().xo() + xarray().pos2x(pos());
783         y = xarray().yo();
784 }
785
786
787 MathInset * MathCursor::par() const
788 {
789         return cursor().par_;
790 }
791
792
793 InsetFormulaBase const * MathCursor::formula()
794 {
795         return formula_;
796 }
797
798
799 int MathCursor::idx() const
800 {
801         return cursor().idx_;
802 }
803
804
805 int & MathCursor::idx()
806 {
807         return cursor().idx_;
808 }
809
810
811 int MathCursor::pos() const
812 {
813         return cursor().pos_;
814 }
815
816
817 int & MathCursor::pos()
818 {
819         return cursor().pos_;
820 }
821
822
823 bool MathCursor::inMacroMode() const
824 {
825         return lastcode_ == LM_TC_TEX;
826 }
827
828
829 bool MathCursor::selection() const
830 {
831         return selection_;
832 }
833
834
835 MathArrayInset * MathCursor::enclosingArray(int & idx) const
836 {
837         for (int i = Cursor_.size() - 1; i >= 0; --i) {
838                 if (Cursor_[i].par_->isArray()) {
839                         idx = Cursor_[i].idx_;
840                         return static_cast<MathArrayInset *>(Cursor_[i].par_);
841                 }
842         }
843         return 0;
844 }
845
846
847 void MathCursor::pullArg(bool goright)
848 {
849         // pullArg
850         dump("pullarg");
851         MathArray a = array();
852         if (popLeft()) {
853                 plainErase();
854                 array().insert(pos(), a);
855                 if (goright) 
856                         pos() += a.size();
857         }
858 }
859
860
861 MathStyles MathCursor::style() const
862 {
863         return xarray().style();
864 }
865
866
867 void MathCursor::normalize() const
868 {
869 #ifdef WITH_WARNINGS
870 #warning This is evil!
871 #endif
872         MathCursor * it = const_cast<MathCursor *>(this);
873
874         if (idx() < 0)
875                 lyxerr << "this should not really happen - 1: " << idx() << "\n";
876         if (idx() >= par()->nargs()) {
877                 lyxerr << "this should not really happen - 2: "
878                        << idx() << " " << par()->nargs() << "\n";
879                 dump("error 2");
880         }
881         it->idx()    = max(idx(), 0);
882         it->idx()    = min(idx(), par()->nargs() - 1);
883
884         if (pos() < 0)
885                 lyxerr << "this should not really happen - 3: " << pos() << "\n";
886         if (pos() > size()) {
887                 lyxerr << "this should not really happen - 4: "
888                        << pos() << " " << size() << "\n";
889                 dump("error 4");
890         }
891         it->pos() = max(pos(), 0);
892         it->pos() = min(pos(), size());
893 }
894
895
896 int MathCursor::size() const
897 {
898         return array().size();
899 }
900
901
902 int MathCursor::col() const
903 {
904         return par()->col(idx());
905 }
906
907
908 int MathCursor::row() const
909 {
910         return par()->row(idx());
911 }
912
913
914 /*
915 char MathCursorPos::getChar() const
916 {
917         return array().getChar(pos());
918 }
919
920
921 string MathCursorPos::readString()
922 {
923         string s;
924         int code = nextCode();
925         for ( ; OK() && nextCode() == code; Next()) 
926                 s += getChar();
927
928         return s;
929 }
930 */
931
932
933 MathInset * MathCursor::prevInset() const
934 {
935         normalize();
936         if (!pos())
937                 return 0;
938         return array().nextInset(pos() - 1);
939 }
940
941
942 MathInset * MathCursor::nextInset() const
943 {
944         normalize();
945         return array().nextInset(pos());
946 }
947
948
949 MathScriptInset * MathCursor::prevScriptInset() const
950 {
951         normalize();
952         MathInset * p = prevInset();
953         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
954 }
955
956
957 MathSpaceInset * MathCursor::prevSpaceInset() const
958 {
959         normalize();
960         MathInset * p = prevInset();
961         return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
962 }
963
964
965 MathArray & MathCursor::array() const
966 {
967         static MathArray dummy;
968         if (!par()) {
969                 lyxerr << "############  par_ not valid\n";
970                 return dummy;
971         }
972
973         if (idx() < 0 || idx() >= par()->nargs()) {
974                 lyxerr << "############  idx_ " << idx() << " not valid\n";
975                 return dummy;
976         }
977
978         return cursor().cell();
979 }
980
981
982 MathXArray & MathCursor::xarray() const
983 {
984         return cursor().xcell();
985 }
986
987
988 void MathCursor::idxNext()
989 {
990         par()->idxNext(idx(), pos());
991 }
992
993
994 void MathCursor::idxPrev()
995 {
996         par()->idxPrev(idx(), pos());
997 }
998
999
1000 void MathCursor::splitCell()
1001 {
1002         if (idx() == par()->nargs() - 1) 
1003                 return;
1004         MathArray ar = array();
1005         ar.erase(0, pos());
1006         array().erase(pos(), size());
1007         ++idx();
1008         pos() = 0;
1009         array().insert(0, ar);
1010 }
1011
1012
1013 void MathCursor::breakLine()
1014 {
1015         // leave inner cells
1016         while (popRight())
1017                 ;
1018
1019         MathMatrixInset * p = outerPar();
1020         if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1021                 p->mutate(LM_OT_EQNARRAY);
1022                 idx() = 0;
1023                 pos() = size();
1024         } else {
1025                 p->addRow(row());
1026
1027                 // split line
1028                 const int r = row();
1029                 for (int c = col() + 1; c < p->ncols(); ++c) {
1030                         const int i1 = p->index(r, c);
1031                         const int i2 = p->index(r + 1, c);      
1032                         lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1033                         p->cell(i1).swap(p->cell(i2));
1034                 }
1035
1036                 // split cell
1037                 splitCell();
1038                 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1039         }
1040 }
1041
1042
1043 char MathCursor::valign() const
1044 {
1045         int idx;
1046         MathArrayInset * p = enclosingArray(idx);
1047         return p ? p->valign() : 0;
1048 }
1049
1050
1051 char MathCursor::halign() const
1052 {
1053         int idx;
1054         MathArrayInset * p = enclosingArray(idx);
1055         return p ? p->halign(idx % p->ncols()) : 0;
1056 }
1057
1058
1059 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1060 {
1061         MathCursorPos anc = normalAnchor();
1062         if (anc < cursor()) {
1063                 i1 = anc;
1064                 i2 = cursor();
1065         } else {
1066                 i1 = cursor();
1067                 i2 = anc;
1068         }
1069 }
1070
1071
1072 MathCursorPos & MathCursor::cursor()
1073 {
1074         return Cursor_.back();
1075 }
1076
1077
1078 MathCursorPos const & MathCursor::cursor() const
1079 {
1080         return Cursor_.back();
1081 }
1082
1083
1084 int MathCursor::cellXOffset() const
1085 {
1086         return par()->cellXOffset(idx());
1087 }
1088
1089
1090 int MathCursor::cellYOffset() const
1091 {
1092         return par()->cellYOffset(idx());
1093 }
1094
1095
1096 int MathCursor::xpos() const
1097 {
1098         return cellXOffset() + xarray().pos2x(pos());
1099 }
1100
1101
1102 int MathCursor::ypos() const
1103 {
1104         return cellYOffset();
1105 }
1106
1107
1108
1109 void MathCursor::gotoX(int x) 
1110 {
1111         pos() = xarray().x2pos(x - cellXOffset());
1112 }
1113
1114
1115 bool MathCursor::goUp()
1116 {
1117         // first ask the inset if it knows better then we
1118         if (par()->idxUp(idx(), pos()))
1119                 return true;
1120
1121         // if not, apply brute force.
1122         int x0;
1123         int y0;
1124         getPos(x0, y0);
1125         std::vector<MathCursorPos> save = Cursor_;
1126         y0 -= xarray().ascent();
1127         for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1128                 setPos(x0, y);
1129                 if (save != Cursor_ && xarray().yo() < y0)
1130                         return true;    
1131         }
1132         Cursor_ = save;
1133         return false;
1134 }
1135
1136
1137 bool MathCursor::goDown()
1138 {
1139         // first ask the inset if it knows better then we
1140         if (par()->idxDown(idx(), pos()))
1141                 return true;
1142
1143         // if not, apply brute force.
1144         int x0;
1145         int y0;
1146         getPos(x0, y0);
1147         std::vector<MathCursorPos> save = Cursor_;
1148         y0 += xarray().descent();
1149         for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1150                 setPos(x0, y);
1151                 if (save != Cursor_ && xarray().yo() > y0)
1152                         return true;    
1153         }
1154         Cursor_ = save;
1155         return false;
1156 }
1157
1158
1159 bool MathCursor::idxLeft()
1160 {
1161         return par()->idxLeft(idx(), pos());
1162 }
1163
1164
1165 bool MathCursor::idxRight()
1166 {
1167         return par()->idxRight(idx(), pos());
1168 }
1169
1170
1171 MathMatrixInset * MathCursor::outerPar() const
1172 {
1173         return
1174                 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1175 }
1176
1177
1178 void MathCursor::interpret(string const & s)
1179 {
1180         //lyxerr << "interpret: '" << s << "'\n";
1181         //lyxerr << "in: " << in_word_set(s) << " \n";
1182
1183         if (s.empty())
1184                 return;
1185
1186         char c = s[0];
1187
1188         //lyxerr << "char: '" << c << "'  int: " << int(c) << endl;
1189         //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);      
1190         //lyxerr << "trans: '" << c << "'  int: " << int(c) << endl;
1191
1192         if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
1193                 int m = 1;
1194                 int n = 1;
1195                 string v_align;
1196                 string h_align;
1197                 istringstream is(s.substr(7).c_str());
1198                 is >> m >> n >> v_align >> h_align;
1199                 m = std::max(1, m);
1200                 n = std::max(1, n);
1201                 v_align += 'c';
1202                 MathArrayInset * p = new MathArrayInset(m, n);
1203                 p->valign(v_align[0]);
1204                 p->halign(h_align);
1205                 niceInsert(p);
1206                 return;
1207         }
1208
1209         if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1210                 MathArray ar = array();
1211                 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1212                 p->cell(0).swap(array());
1213                 pos() = 0;
1214                 niceInsert(p);
1215                 popRight();
1216                 left();
1217                 return;
1218         }
1219
1220         if (s.size() > 1) {
1221                 niceInsert(createMathInset(s.substr(1)));
1222                 return;
1223         }
1224
1225
1226         // we got just a single char now
1227
1228         if (c == '^' || c == '_') {
1229                 bool const up = (s[0] == '^');
1230                 selCut();       
1231                 MathScriptInset * p = prevScriptInset();
1232                 if (!p) {
1233                         MathInset * b = prevInset();
1234                         if (b && b->isScriptable()) {
1235                                 p = new MathScriptInset(up, !up, b->clone());
1236                                 posLeft();
1237                                 plainErase();
1238                         } else 
1239                                 p = new MathScriptInset(up, !up);
1240                         insert(p);
1241                 }
1242                 pushRight(p);
1243                 if (up)
1244                         p->up(true);
1245                 else
1246                         p->down(true);
1247                 idx() = up ? 0 : 1;
1248                 pos() = 0;
1249                 selPaste();
1250                 return;
1251         }
1252
1253         if (selection_)
1254                 selDel();
1255
1256         if (lastcode_ == LM_TC_TEXTRM) {
1257                 insert(c, LM_TC_TEXTRM);
1258                 return;
1259         }
1260
1261         if (c == ' ') {
1262                 if (inMacroMode()) {
1263                         macroModeClose();
1264                         lastcode_ = LM_TC_VAR;
1265                         return;
1266                 }
1267
1268                 MathSpaceInset * p = prevSpaceInset();
1269                 if (p) {
1270                         p->incSpace();
1271                         return;
1272                 }
1273
1274                 if (mathcursor->popRight())
1275                         return;
1276
1277 #warning look here
1278                         // this would not work if the inset is in an table!
1279                         //bv->text->cursorRight(bv, true);
1280                         //result = FINISHED;
1281                 return;
1282         }
1283
1284         if (lastcode_ == LM_TC_TEX) {
1285                 if (macroName().empty()) {
1286                         insert(c, LM_TC_TEX);
1287                         if (!isalpha(c)) {
1288                                 macroModeClose();
1289                                 lastcode_ = LM_TC_VAR;
1290                         }
1291                 } else {
1292                         if (isalpha(c))
1293                                 insert(c, LM_TC_TEX);
1294                         else {
1295                                 macroModeClose();
1296                                 lastcode_ = LM_TC_VAR;
1297                         }
1298                 }
1299                 return;
1300         }
1301
1302         if (c == '{' || c == '}') {
1303                 niceInsert(new MathCharInset(c, LM_TC_SPECIAL));
1304                 return;
1305         }
1306
1307         if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1308                 static char const greek[26] =
1309                         {'A', 'B', 'X',  0 , 'E',  0 ,  0 , 'H', 'I',  0 ,
1310                          'K',  0 , 'M', 'N', 'O',  0 ,  0 , 'P',  0 , 'T',
1311                          0,  0,   0,   0,   0 , 'Z' };
1312                         
1313                 MathTextCodes code = LM_TC_SYMB;
1314                 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1315                         code = LM_TC_RM;
1316                         c = greek[c - 'A'];
1317                 }
1318                 insert(c, code);
1319         
1320 #warning greek insert problem? look here!
1321                 //if (lastcode_ == LM_TC_GREEK1)
1322                         lastcode_ = LM_TC_VAR;
1323                 return; 
1324         }
1325
1326         if (c == '\\') {
1327                 lastcode_ = LM_TC_TEX;
1328                 //bv->owner()->message(_("TeX mode"));
1329                 return; 
1330         }
1331
1332         // no special circumstances, so insert the character without any fuss
1333         insert(c);
1334 }
1335
1336
1337
1338 ////////////////////////////////////////////////////////////////////////
1339
1340
1341 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1342 {
1343         return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1344 }
1345
1346
1347 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1348 {
1349         if (ti.par_ != it.par_) {
1350                 lyxerr << "can't compare cursor and anchor in different insets\n";
1351                 return true;
1352         }
1353         if (ti.idx_ != it.idx_)
1354                 return ti.idx_ < it.idx_;
1355         return ti.pos_ < it.pos_;
1356 }
1357
1358
1359 MathArray & MathCursorPos::cell(int idx) const
1360 {
1361         return par_->cell(idx);
1362 }
1363
1364
1365 MathArray & MathCursorPos::cell() const
1366 {
1367         return par_->cell(idx_);
1368 }
1369
1370
1371 MathXArray & MathCursorPos::xcell(int idx) const
1372 {
1373         return par_->xcell(idx);
1374 }
1375
1376
1377 MathXArray & MathCursorPos::xcell() const
1378 {
1379         return par_->xcell(idx_);
1380 }
1381
1382
1383 MathCursorPos MathCursor::normalAnchor() const
1384 {
1385         // use Anchor on the same level as Cursor
1386         MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1387         if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1388                 // anchor is behind cursor -> move anchor behind the inset
1389                 ++normal.pos_;
1390         }
1391         return normal;
1392 }
1393