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