]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
fix broken greek keyboard input
[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(bool sel)
372 {
373         dump("home 1");
374         selHandle(sel);
375         macroModeClose();
376         lastcode_ = LM_TC_VAR;
377         if (!par()->idxHome(idx(), pos())) 
378                 popLeft();
379         dump("home 2");
380 }
381
382
383 void MathCursor::end(bool sel)
384 {
385         dump("end 1");
386         selHandle(sel);
387         macroModeClose();
388         lastcode_ = LM_TC_VAR;
389         if (!par()->idxEnd(idx(), pos()))
390                 popRight();
391         dump("end 2");
392 }
393
394
395 void MathCursor::plainErase()
396 {
397         array().erase(pos());
398 }
399
400
401 void MathCursor::insert(char c, MathTextCodes t)
402 {
403         //lyxerr << "inserting '" << c << "'\n";
404         array().insert(pos(), new MathCharInset(c, t));
405         posRight();
406 }
407
408
409 void MathCursor::insert(MathInset * p)
410 {
411         macroModeClose();
412
413         if (selection_) {
414                 if (p->nargs())
415                         selCut();
416                 else
417                         selDel();
418         }
419
420         array().insert(pos(), p);
421         posRight();
422 }
423
424
425 void MathCursor::niceInsert(MathInset * p) 
426 {
427         if (!p) {
428                 lyxerr << "should not happen\n";
429                 return;
430         }
431         selCut();
432         insert(p);
433         if (p->nargs()) {
434                 posLeft();
435                 right();  // do not push for e.g. MathSymbolInset
436                 selPaste();
437         }
438         p->metrics(p->size());
439 }
440
441
442 void MathCursor::insert(MathArray const & ar)
443 {
444         macroModeClose();
445         if (selection_)
446                 selCut();
447
448         array().insert(pos(), ar);
449         pos() += ar.size();
450 }
451
452
453 void MathCursor::backspace()
454 {
455         if (inMacroMode()) {
456                 left();
457                 return;
458         }
459
460         if (posLeft()) {
461                 plainErase();
462                 return;
463         }
464
465         if (size()) {
466                 pullArg(false);
467                 return;
468         }
469
470         erase();
471 }
472
473
474 void MathCursor::erase()
475 {
476         dump("erase 1");
477         if (inMacroMode())
478                 return;
479
480         if (selection_) {
481                 selDel();
482                 return;
483         }
484
485         // delete empty cells if necessary
486         if (pos() == 0 && array().empty()) {
487                 bool popit;
488                 bool removeit;
489                 par()->idxDelete(idx(), popit, removeit);
490                 if (popit && popLeft() && removeit)
491                         plainErase();
492                 return;
493         }
494
495         if (pos() < size())
496                 plainErase();
497
498         dump("erase 2");
499 }
500
501
502 void MathCursor::delLine()
503 {
504         macroModeClose();
505
506         if (selection_) {
507                 selDel();
508                 return;
509         }
510
511         if (par()->nrows() > 1)
512                 par()->delRow(row());
513 }
514
515
516 bool MathCursor::up(bool sel)
517 {
518         dump("up 1");
519         macroModeClose();
520         selHandle(sel);
521
522         if (selection_)
523                 return goUp();
524
525         // check whether we could move into an inset on the right or on the left
526         MathInset * p = nextInset();
527         if (p) {
528                 int idxx, poss;
529                 if (p->idxFirstUp(idxx, poss)) {
530                         pushLeft(p);
531                         idx() = idxx;
532                         pos() = poss;
533                         dump("up 3");
534                         return true;
535                 }
536         }
537
538         p = prevInset();
539         if (p) {
540                 int idxx, poss;
541                 if (p->idxLastUp(idxx, poss)) {
542                         pushRight(p);
543                         idx() = idxx;
544                         pos() = poss;
545                         dump("up 4");
546                         return true;
547                 }
548         }
549
550         return goUp();
551 }
552
553
554 bool MathCursor::down(bool sel)
555 {
556         dump("down 1");
557         macroModeClose();
558         selHandle(sel);
559
560         if (selection_) 
561                 return goDown();
562
563         // check whether we could move into an inset on the right or on the left
564         MathInset * p = nextInset();
565         if (p) {
566                 int idxx = 0;
567                 int poss = 0;
568                 if (p->idxFirstDown(idxx, poss)) {
569                         pushLeft(p);
570                         idx() = idxx;
571                         pos() = poss;
572                         dump("Down 3");
573                         return true;
574                 }
575         }
576
577         p = prevInset();
578         if (p) {
579                 int idxx = 0;
580                 int poss = 0;
581                 if (p->idxLastDown(idxx, poss)) {
582                         pushRight(p);
583                         idx() = idxx;
584                         pos() = poss;
585                         dump("Down 4");
586                         return true;
587                 }
588         }
589
590         return goDown();
591 }
592
593
594 bool MathCursor::toggleLimits()
595 {
596         MathScriptInset * p = prevScriptInset();
597         if (!p)
598                 return false;
599         int old = p->limits();
600         p->limits(old < 0 ? 1 : -1);
601         return old != p->limits();
602 }
603
604
605 void MathCursor::setSize(MathStyles size)
606 {
607         par()->userSetSize(size);
608 }
609
610
611 void MathCursor::macroModeClose()
612 {
613         string s = macroName();
614         if (s.size()) {
615                 pos() = pos() - s.size();
616                 for (unsigned i = 0; i < s.size(); ++i)
617                         plainErase();
618                 lastcode_ = LM_TC_VAR;
619                 interpret("\\" + s);
620         }
621 }
622
623
624 string MathCursor::macroName() const
625 {
626         string s;
627         int p = pos() - 1;
628         while (p >= 0 && array().nextInset(p)->code() == LM_TC_TEX) {
629                 s = array().nextInset(p)->getChar() + s;
630                 --p;
631         }
632         return s;
633 }
634
635
636 void MathCursor::selCopy()
637 {
638         seldump("selCopy");
639         if (selection_) {
640                 theSelection.grab(*this);
641                 selClear();
642         }
643 }
644
645
646 void MathCursor::selCut()
647 {
648         seldump("selCut");
649         if (selection_) {
650                 theSelection.grab(*this);
651                 theSelection.erase(*this);
652                 selClear();
653         } else {
654                 theSelection.clear();
655         }
656 }
657
658
659 void MathCursor::selDel()
660 {
661         seldump("selDel");
662         if (selection_) {
663                 theSelection.erase(*this);
664                 selClear();
665         }
666 }
667
668
669 void MathCursor::selPaste()
670 {
671         seldump("selPaste");
672         theSelection.paste(*this);
673         selClear();
674 }
675
676
677 void MathCursor::selHandle(bool sel)
678 {
679         if (sel == selection_)
680                 return;
681
682         theSelection.clear();
683         Anchor_    = Cursor_;
684         selection_ = sel;
685 }
686
687
688 void MathCursor::selStart()
689 {
690         seldump("selStart");
691         if (selection_)
692                 return;
693
694         theSelection.clear();
695         Anchor_ = Cursor_;
696         selection_ = true;
697 }
698
699
700 void MathCursor::selClear()
701 {
702         seldump("selClear");
703         selection_ = false;
704 }
705
706
707 void MathCursor::drawSelection(Painter & pain) const
708 {
709         if (!selection_)
710                 return;
711
712         MathCursorPos i1;
713         MathCursorPos i2;
714         getSelection(i1, i2);
715
716         //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
717
718         if (i1.idx_ == i2.idx_) {
719                 MathXArray & c = i1.xcell();
720                 int x1 = c.xo() + c.pos2x(i1.pos_);
721                 int y1 = c.yo() - c.ascent();
722                 int x2 = c.xo() + c.pos2x(i2.pos_);
723                 int y2 = c.yo() + c.descent();
724                 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
725         } else {
726                 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
727                 for (unsigned i = 0; i < indices.size(); ++i) {
728                         MathXArray & c = i1.xcell(indices[i]);
729                         int x1 = c.xo();
730                         int y1 = c.yo() - c.ascent();
731                         int x2 = c.xo() + c.width();
732                         int y2 = c.yo() + c.descent();
733                         pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
734                 }
735         }
736 }
737
738
739 MathTextCodes MathCursor::nextCode() const
740 {
741         //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
742         return LM_TC_VAR;
743 }
744
745
746 void MathCursor::handleFont(MathTextCodes t)
747 {
748         macroModeClose();
749         if (selection_) {
750                 MathCursorPos i1;
751                 MathCursorPos i2;
752                 getSelection(i1, i2); 
753                 if (i1.idx_ == i2.idx_) {
754                         MathArray & ar = i1.cell();
755                         for (int pos = i1.pos_; pos != i2.pos_; ++pos) 
756                                 ar.nextInset(pos)->handleFont(t);
757                 }
758         } else 
759                 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
760 }
761
762
763 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
764 {
765         handleNest(new MathDelimInset(l, r));
766 }
767
768
769 void MathCursor::handleNest(MathInset * p)
770 {
771         if (selection_) {
772                 selCut();
773                 p->cell(0) = theSelection.glue();
774         }
775         insert(p);
776         pushRight(p);
777 }
778
779
780 void MathCursor::getPos(int & x, int & y)
781 {
782 #ifdef WITH_WARNINGS
783 #warning This should probably take cellXOffset and cellYOffset into account
784 #endif
785         x = xarray().xo() + xarray().pos2x(pos());
786         y = xarray().yo();
787 }
788
789
790 MathInset * MathCursor::par() const
791 {
792         return cursor().par_;
793 }
794
795
796 InsetFormulaBase const * MathCursor::formula()
797 {
798         return formula_;
799 }
800
801
802 int MathCursor::idx() const
803 {
804         return cursor().idx_;
805 }
806
807
808 int & MathCursor::idx()
809 {
810         return cursor().idx_;
811 }
812
813
814 int MathCursor::pos() const
815 {
816         return cursor().pos_;
817 }
818
819
820 int & MathCursor::pos()
821 {
822         return cursor().pos_;
823 }
824
825
826 bool MathCursor::inMacroMode() const
827 {
828         return lastcode_ == LM_TC_TEX;
829 }
830
831
832 bool MathCursor::selection() const
833 {
834         return selection_;
835 }
836
837
838 MathArrayInset * MathCursor::enclosingArray(int & idx) const
839 {
840         for (int i = Cursor_.size() - 1; i >= 0; --i) {
841                 if (Cursor_[i].par_->isArray()) {
842                         idx = Cursor_[i].idx_;
843                         return static_cast<MathArrayInset *>(Cursor_[i].par_);
844                 }
845         }
846         return 0;
847 }
848
849
850 void MathCursor::pullArg(bool goright)
851 {
852         // pullArg
853         dump("pullarg");
854         MathArray a = array();
855         if (popLeft()) {
856                 plainErase();
857                 array().insert(pos(), a);
858                 if (goright) 
859                         pos() += a.size();
860         }
861 }
862
863
864 MathStyles MathCursor::style() const
865 {
866         return xarray().style();
867 }
868
869
870 void MathCursor::normalize() const
871 {
872 #ifdef WITH_WARNINGS
873 #warning This is evil!
874 #endif
875         MathCursor * it = const_cast<MathCursor *>(this);
876
877         if (idx() < 0)
878                 lyxerr << "this should not really happen - 1: " << idx() << "\n";
879         if (idx() >= par()->nargs()) {
880                 lyxerr << "this should not really happen - 2: "
881                        << idx() << " " << par()->nargs() << "\n";
882                 dump("error 2");
883         }
884         it->idx()    = max(idx(), 0);
885         it->idx()    = min(idx(), par()->nargs() - 1);
886
887         if (pos() < 0)
888                 lyxerr << "this should not really happen - 3: " << pos() << "\n";
889         if (pos() > size()) {
890                 lyxerr << "this should not really happen - 4: "
891                        << pos() << " " << size() << "\n";
892                 dump("error 4");
893         }
894         it->pos() = max(pos(), 0);
895         it->pos() = min(pos(), size());
896 }
897
898
899 int MathCursor::size() const
900 {
901         return array().size();
902 }
903
904
905 int MathCursor::col() const
906 {
907         return par()->col(idx());
908 }
909
910
911 int MathCursor::row() const
912 {
913         return par()->row(idx());
914 }
915
916
917 /*
918 char MathCursorPos::getChar() const
919 {
920         return array().getChar(pos());
921 }
922
923
924 string MathCursorPos::readString()
925 {
926         string s;
927         int code = nextCode();
928         for ( ; OK() && nextCode() == code; Next()) 
929                 s += getChar();
930
931         return s;
932 }
933 */
934
935
936 MathInset * MathCursor::prevInset() const
937 {
938         normalize();
939         if (!pos())
940                 return 0;
941         return array().nextInset(pos() - 1);
942 }
943
944
945 MathInset * MathCursor::nextInset() const
946 {
947         normalize();
948         return array().nextInset(pos());
949 }
950
951
952 MathScriptInset * MathCursor::prevScriptInset() const
953 {
954         normalize();
955         MathInset * p = prevInset();
956         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
957 }
958
959
960 MathSpaceInset * MathCursor::prevSpaceInset() const
961 {
962         normalize();
963         MathInset * p = prevInset();
964         return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
965 }
966
967
968 MathArray & MathCursor::array() const
969 {
970         static MathArray dummy;
971         if (!par()) {
972                 lyxerr << "############  par_ not valid\n";
973                 return dummy;
974         }
975
976         if (idx() < 0 || idx() >= par()->nargs()) {
977                 lyxerr << "############  idx_ " << idx() << " not valid\n";
978                 return dummy;
979         }
980
981         return cursor().cell();
982 }
983
984
985 MathXArray & MathCursor::xarray() const
986 {
987         return cursor().xcell();
988 }
989
990
991 void MathCursor::idxNext()
992 {
993         par()->idxNext(idx(), pos());
994 }
995
996
997 void MathCursor::idxPrev()
998 {
999         par()->idxPrev(idx(), pos());
1000 }
1001
1002
1003 void MathCursor::splitCell()
1004 {
1005         if (idx() == par()->nargs() - 1) 
1006                 return;
1007         MathArray ar = array();
1008         ar.erase(0, pos());
1009         array().erase(pos(), size());
1010         ++idx();
1011         pos() = 0;
1012         array().insert(0, ar);
1013 }
1014
1015
1016 void MathCursor::breakLine()
1017 {
1018         // leave inner cells
1019         while (popRight())
1020                 ;
1021
1022         MathMatrixInset * p = outerPar();
1023         if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1024                 p->mutate(LM_OT_EQNARRAY);
1025                 idx() = 0;
1026                 pos() = size();
1027         } else {
1028                 p->addRow(row());
1029
1030                 // split line
1031                 const int r = row();
1032                 for (int c = col() + 1; c < p->ncols(); ++c) {
1033                         const int i1 = p->index(r, c);
1034                         const int i2 = p->index(r + 1, c);      
1035                         lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1036                         p->cell(i1).swap(p->cell(i2));
1037                 }
1038
1039                 // split cell
1040                 splitCell();
1041                 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1042         }
1043 }
1044
1045
1046 char MathCursor::valign() const
1047 {
1048         int idx;
1049         MathArrayInset * p = enclosingArray(idx);
1050         return p ? p->valign() : 0;
1051 }
1052
1053
1054 char MathCursor::halign() const
1055 {
1056         int idx;
1057         MathArrayInset * p = enclosingArray(idx);
1058         return p ? p->halign(idx % p->ncols()) : 0;
1059 }
1060
1061
1062 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1063 {
1064         MathCursorPos anc = normalAnchor();
1065         if (anc < cursor()) {
1066                 i1 = anc;
1067                 i2 = cursor();
1068         } else {
1069                 i1 = cursor();
1070                 i2 = anc;
1071         }
1072 }
1073
1074
1075 MathCursorPos & MathCursor::cursor()
1076 {
1077         return Cursor_.back();
1078 }
1079
1080
1081 MathCursorPos const & MathCursor::cursor() const
1082 {
1083         return Cursor_.back();
1084 }
1085
1086
1087 int MathCursor::cellXOffset() const
1088 {
1089         return par()->cellXOffset(idx());
1090 }
1091
1092
1093 int MathCursor::cellYOffset() const
1094 {
1095         return par()->cellYOffset(idx());
1096 }
1097
1098
1099 int MathCursor::xpos() const
1100 {
1101         return cellXOffset() + xarray().pos2x(pos());
1102 }
1103
1104
1105 int MathCursor::ypos() const
1106 {
1107         return cellYOffset();
1108 }
1109
1110
1111
1112 void MathCursor::gotoX(int x) 
1113 {
1114         pos() = xarray().x2pos(x - cellXOffset());
1115 }
1116
1117
1118 bool MathCursor::goUp()
1119 {
1120         // first ask the inset if it knows better then we
1121         if (par()->idxUp(idx(), pos()))
1122                 return true;
1123
1124         // if not, apply brute force.
1125         int x0;
1126         int y0;
1127         getPos(x0, y0);
1128         std::vector<MathCursorPos> save = Cursor_;
1129         y0 -= xarray().ascent();
1130         for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1131                 setPos(x0, y);
1132                 if (save != Cursor_ && xarray().yo() < y0)
1133                         return true;    
1134         }
1135         Cursor_ = save;
1136         return false;
1137 }
1138
1139
1140 bool MathCursor::goDown()
1141 {
1142         // first ask the inset if it knows better then we
1143         if (par()->idxDown(idx(), pos()))
1144                 return true;
1145
1146         // if not, apply brute force.
1147         int x0;
1148         int y0;
1149         getPos(x0, y0);
1150         std::vector<MathCursorPos> save = Cursor_;
1151         y0 += xarray().descent();
1152         for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1153                 setPos(x0, y);
1154                 if (save != Cursor_ && xarray().yo() > y0)
1155                         return true;    
1156         }
1157         Cursor_ = save;
1158         return false;
1159 }
1160
1161
1162 bool MathCursor::idxLeft()
1163 {
1164         return par()->idxLeft(idx(), pos());
1165 }
1166
1167
1168 bool MathCursor::idxRight()
1169 {
1170         return par()->idxRight(idx(), pos());
1171 }
1172
1173
1174 MathMatrixInset * MathCursor::outerPar() const
1175 {
1176         return
1177                 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1178 }
1179
1180
1181 void MathCursor::interpret(string const & s)
1182 {
1183         //lyxerr << "interpret: '" << s << "'\n";
1184         //lyxerr << "in: " << in_word_set(s) << " \n";
1185
1186         if (s.empty())
1187                 return;
1188
1189         char c = s[0];
1190
1191         //lyxerr << "char: '" << c << "'  int: " << int(c) << endl;
1192         //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);      
1193         //lyxerr << "trans: '" << c << "'  int: " << int(c) << endl;
1194
1195         if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
1196                 int m = 1;
1197                 int n = 1;
1198                 string v_align;
1199                 string h_align;
1200                 istringstream is(s.substr(7).c_str());
1201                 is >> m >> n >> v_align >> h_align;
1202                 m = std::max(1, m);
1203                 n = std::max(1, n);
1204                 v_align += 'c';
1205                 MathArrayInset * p = new MathArrayInset(m, n);
1206                 p->valign(v_align[0]);
1207                 p->halign(h_align);
1208                 niceInsert(p);
1209                 return;
1210         }
1211
1212         if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1213                 MathArray ar = array();
1214                 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1215                 p->cell(0).swap(array());
1216                 pos() = 0;
1217                 niceInsert(p);
1218                 popRight();
1219                 left();
1220                 return;
1221         }
1222
1223         if (s.size() > 1) {
1224                 niceInsert(createMathInset(s.substr(1)));
1225                 return;
1226         }
1227
1228
1229         // we got just a single char now
1230
1231         if (c == '^' || c == '_') {
1232                 bool const up = (s[0] == '^');
1233                 selCut();       
1234                 MathScriptInset * p = prevScriptInset();
1235                 if (!p) {
1236                         MathInset * b = prevInset();
1237                         if (b && b->isScriptable()) {
1238                                 p = new MathScriptInset(up, !up, b->clone());
1239                                 posLeft();
1240                                 plainErase();
1241                         } else 
1242                                 p = new MathScriptInset(up, !up);
1243                         insert(p);
1244                 }
1245                 pushRight(p);
1246                 if (up)
1247                         p->up(true);
1248                 else
1249                         p->down(true);
1250                 idx() = up ? 0 : 1;
1251                 pos() = 0;
1252                 selPaste();
1253                 return;
1254         }
1255
1256         if (selection_)
1257                 selDel();
1258
1259         if (lastcode_ == LM_TC_TEXTRM) {
1260                 insert(c, LM_TC_TEXTRM);
1261                 return;
1262         }
1263
1264         if (c == ' ') {
1265                 if (inMacroMode()) {
1266                         macroModeClose();
1267                         lastcode_ = LM_TC_VAR;
1268                         return;
1269                 }
1270
1271                 MathSpaceInset * p = prevSpaceInset();
1272                 if (p) {
1273                         p->incSpace();
1274                         return;
1275                 }
1276
1277                 if (mathcursor->popRight())
1278                         return;
1279
1280 #warning look here
1281                         // this would not work if the inset is in an table!
1282                         //bv->text->cursorRight(bv, true);
1283                         //result = FINISHED;
1284                 return;
1285         }
1286
1287         if (lastcode_ != LM_TC_TEX && strchr("#$%{|}", c)) {
1288                 insert(new MathSpecialCharInset(c));    
1289                 return;
1290         }
1291
1292         if (lastcode_ == LM_TC_TEX) {
1293                 if (macroName().empty()) {
1294                         if (strchr("#$%{|}", c)) {
1295                                 insert(new MathCharInset(c, LM_TC_TEX));        
1296                                 lastcode_ = LM_TC_VAR;
1297                                 return;
1298                         }
1299                         insert(c, LM_TC_TEX);
1300                         if (!isalpha(c)) {
1301                                 macroModeClose();
1302                                 lastcode_ = LM_TC_VAR;
1303                         }
1304                 } else {
1305                         if (isalpha(c))
1306                                 insert(c, LM_TC_TEX);
1307                         else {
1308                                 macroModeClose();
1309                                 lastcode_ = LM_TC_VAR;
1310                         }
1311                 }
1312                 return;
1313         }
1314
1315         if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1316                 static char const greekl[][26] =
1317                         {"alpha", "beta", "chi", "delta", "epsilon", "phi", "gamma", "eta",
1318                          "iota", "", "kappa", "lambda", "mu", "nu", "omikron", "pi", "omega",
1319                          "rho", "sigma", "tau", "theta", "", "", "xi", "ypsilon", "zeta"};
1320                 static char const greeku[][26] =
1321                         {"Alpha", "Beta", "Chi", "Delta", "Epsilon", "Phi", "Gamma", "Eta",
1322                          "Iota", "", "Kappa", "Lambda", "Mu", "Nu", "Omikron", "Pi", "Omega",
1323                          "Rho", "Sigma", "Tau", "Theta", "", "", "xi", "Ypsilon", "Zeta"};
1324         
1325                 latexkeys const * l = 0;        
1326                 if ('a' <= c && c <= 'z')
1327                         l = in_word_set(greekl[c - 'a']);
1328                 if ('A' <= c && c <= 'Z')
1329                         l = in_word_set(greeku[c - 'A']);
1330         
1331                 if (l)
1332                         insert(createMathInset(l));
1333                 else
1334                         insert(c, LM_TC_VAR);
1335
1336 #warning greek insert problem? look here!
1337                 if (lastcode_ == LM_TC_GREEK1)
1338                         lastcode_ = LM_TC_VAR;
1339                 return; 
1340         }
1341
1342         if (c == '\\') {
1343                 lastcode_ = LM_TC_TEX;
1344                 //bv->owner()->message(_("TeX mode"));
1345                 return; 
1346         }
1347
1348         // no special circumstances, so insert the character without any fuss
1349         insert(c);
1350 }
1351
1352
1353
1354 ////////////////////////////////////////////////////////////////////////
1355
1356
1357 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1358 {
1359         return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1360 }
1361
1362
1363 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1364 {
1365         if (ti.par_ != it.par_) {
1366                 lyxerr << "can't compare cursor and anchor in different insets\n";
1367                 return true;
1368         }
1369         if (ti.idx_ != it.idx_)
1370                 return ti.idx_ < it.idx_;
1371         return ti.pos_ < it.pos_;
1372 }
1373
1374
1375 MathArray & MathCursorPos::cell(int idx) const
1376 {
1377         return par_->cell(idx);
1378 }
1379
1380
1381 MathArray & MathCursorPos::cell() const
1382 {
1383         return par_->cell(idx_);
1384 }
1385
1386
1387 MathXArray & MathCursorPos::xcell(int idx) const
1388 {
1389         return par_->xcell(idx);
1390 }
1391
1392
1393 MathXArray & MathCursorPos::xcell() const
1394 {
1395         return par_->xcell(idx_);
1396 }
1397
1398
1399 MathCursorPos MathCursor::normalAnchor() const
1400 {
1401         // use Anchor on the same level as Cursor
1402         MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1403         if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1404                 // anchor is behind cursor -> move anchor behind the inset
1405                 ++normal.pos_;
1406         }
1407         return normal;
1408 }
1409