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