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