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