]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
prepare re-enabling LaTeX feature validation
[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 <cctype>
24
25 #include "math_inset.h"
26 #include "math_arrayinset.h"
27 #include "math_parser.h"
28 #include "math_cursor.h"
29 #include "math_macro.h"
30 #include "math_macroarg.h"
31 #include "math_macrotable.h"
32 #include "math_root.h"
33 #include "support/lstrings.h"
34 #include "debug.h"
35 #include "LColor.h"
36 #include "Painter.h"
37 #include "math_matrixinset.h"
38 #include "math_grid.h"
39 #include "math_spaceinset.h"
40 #include "math_funcinset.h"
41 #include "math_bigopinset.h"
42 #include "math_fracinset.h"
43 #include "math_decorationinset.h"
44 #include "math_dotsinset.h"
45 #include "math_deliminset.h"
46 #include "math_macrotemplate.h"
47 #include "math_sqrtinset.h"
48 #include "math_scriptinset.h"
49 #include "mathed/support.h"
50 #include "formulabase.h"
51
52
53 using std::endl;
54 using std::min;
55 using std::max;
56 using std::isalnum;
57
58
59 namespace {
60
61 MathArray selarray;
62
63 bool IsMacro(short tok, int id)
64 {
65         return tok != LM_TK_STACK &&
66                tok != LM_TK_FRAC &&
67                tok != LM_TK_SQRT &&
68                tok != LM_TK_DECORATION &&
69                tok != LM_TK_SPACE &&
70                tok != LM_TK_DOTS &&
71                tok != LM_TK_FUNCLIM &&
72                tok != LM_TK_BIGSYM &&
73                !(tok == LM_TK_SYM && id < 255);
74 }
75
76 }
77
78 MathCursor::MathCursor(InsetFormulaBase * formula)
79         : formula_(formula)
80 {
81         lastcode   = LM_TC_MIN;
82         macro_mode = false;
83         first();
84 }
85
86
87 void MathCursor::push(MathInset * par, bool first)
88 {
89         path_.push_back(MathIter());
90         path_.back().par_    = par_;
91         path_.back().idx_    = idx_;
92         path_.back().cursor_ = cursor_;
93         dump("Pushed:");
94         par_ = par;
95         first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
96 }
97
98
99 bool MathCursor::pop()
100 {
101         if (path_.empty())
102                 return false;
103         par_    = path_.back().par_;
104         idx_    = path_.back().idx_;
105         cursor_ = path_.back().cursor_;
106         dump("Popped:");
107         path_.pop_back();
108         return true;
109 }
110
111
112 MathInset * MathCursor::parInset(int i) const
113 {
114         return path_[i].par_;
115 }
116
117 void MathCursor::dump(char const * what) const
118 {
119         return;
120
121         lyxerr << "MC: " << what
122                 << " cursor: " << cursor_
123                 << " anchor: " << anchor_
124                 << " idx: " << idx_
125                 << " par: " << par_
126                 << " sel: " << selection
127                 << " data: " << array()
128                 << "\n";
129 }
130
131 void MathCursor::seldump(char const * str) const
132 {
133         lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
134         dump("   Pos");
135         return;
136
137         lyxerr << "\n\n\\n=================vvvvvvvvvvvvv=======================   "
138                 <<  str << "\nselarray: " << selarray;
139         for (unsigned int i = 0; i < path_.size(); ++i) 
140                 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
141         lyxerr << "\ncursor: " << cursor_;
142         lyxerr << "\nanchor: " << anchor_;
143         lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
144 }
145
146
147 bool MathCursor::isInside(MathInset * p) const
148 {
149         for (unsigned i = 0; i < path_.size(); ++i) 
150                 if (parInset(i) == p) 
151                         return true;
152         return par_ == p;
153 }
154
155
156 bool MathCursor::plainLeft()
157 {
158         return array().prev(cursor_);
159 }
160
161 bool MathCursor::Left(bool sel)
162 {
163         dump("Left 1");
164         if (macro_mode) {
165                 // was MacroModeBack()
166                 if (!imacro->name().empty()) {
167                         imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
168                         imacro->Metrics(imacro->size());
169                 } else
170                         MacroModeClose();
171                 return true;
172         }
173         SelHandle(sel);
174         clearLastCode();
175
176         bool result = false;
177
178         if (selection) {
179                 result = array().prev(cursor_);
180                 if (!result && pop()) {
181                         anchor_ = cursor_;
182                         result = array().next(anchor_);
183                 }
184         } else {
185                 MathInset * p = prevInset();
186                 if (p && p->isActive()) {
187                         // We have to move deeper into the previous inset
188                         array().prev(cursor_);
189                         push(p, false);
190                         result = true;
191                 } else {
192                         // The common case, where we are not 
193                         // entering a deeper inset
194                         result = array().prev(cursor_);
195                         if (!result) {
196                                 if (par_->idxLeft(idx_, cursor_)) {
197                                         result = true;
198                                 } else if (pop()) {
199                                         result = true;
200                                 }
201                         }
202                 }
203         }
204         dump("Left 2");
205         return result;
206 }
207
208
209 bool MathCursor::plainRight()
210 {
211         return array().next(cursor_);
212 }
213
214
215 bool MathCursor::Right(bool sel)
216 {
217         dump("Right 1");
218         if (macro_mode) {
219                 MacroModeClose();
220                 return true;
221         }
222         SelHandle(sel);
223         clearLastCode();
224
225         bool result = false;
226
227         if (selection) {
228                 result = array().next(cursor_);
229                 if (!result && pop()) {
230                         anchor_ = cursor_;
231                         result = array().next(cursor_);
232                 }
233         } else {
234                 MathInset * p = nextInset();
235                 if (p && p->isActive()) {
236                         push(p, true);
237                         result = true;
238                 } else {
239                         result = array().next(cursor_);
240                         if (!result) {
241                                 if (par_->idxRight(idx_, cursor_)) {
242                                         result = true;
243                                 } else if (pop()) {
244                                         result = true;
245                                         array().next(cursor_);
246                                 }
247                         }
248                 }
249         }
250         dump("Right 2");
251         return result;
252 }
253
254
255 void MathCursor::first()
256 {
257         selection  = false;
258         par_       = formula_->par();
259         idx_       = 0;
260         cursor_    = 0;
261         anchor_    = 0;
262         par_->idxFirst(idx_, cursor_);
263 }
264
265
266 void MathCursor::last()
267 {
268         selection  = false;
269         par_       = formula_->par();
270         idx_       = 0;
271         cursor_    = 0;
272         anchor_    = 0;
273         par_->idxLast(idx_, cursor_);
274 }
275
276
277 void MathCursor::SetPos(int x, int y)
278 {
279         dump("SetPos 1");
280         //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
281
282         MacroModeClose();
283         lastcode = LM_TC_MIN;
284         path_.clear();
285
286         par_    = formula()->par();
287
288         while (1) {
289                 idx_    = -1;
290                 cursor_ = -1;
291                 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor_  << "\n";
292                 int distmin = 1 << 30; // large enough
293                 for (int i = 0; i < par_->nargs(); ++i) {
294                         MathXArray const & ar = par_->xcell(i);
295                         int x1 = x - ar.xo();
296                         int y1 = y - ar.yo();
297                         int c  = ar.x2pos(x1);
298                         int xx = abs(x1 - ar.pos2x(c));
299                         int yy = abs(y1);
300                         //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
301                         //      << " c: " << c  << " xo: " << ar.xo() << "\n";
302                         if (yy + xx <= distmin) {
303                                 distmin = yy + xx;
304                                 idx_     = i;
305                                 cursor_  = c;
306                         }
307                 }
308                 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_  << "\n";
309                 MathInset * n = nextInset();
310                 MathInset * p = prevInset();
311                 if (n && (n->isActive() || n->isUpDownInset()) && n->covers(x, y))
312                         push(n, true);
313                 else if (p && (p->isActive() || p->isUpDownInset()) && p->covers(x, y)) {
314                         array().prev(cursor_);
315                         push(p, false);
316                 } else 
317                         break;
318         }
319         dump("SetPos 2");
320 }
321
322
323 void MathCursor::Home()
324 {
325         dump("Home 1");
326         if (macro_mode)
327                 MacroModeClose();
328         clearLastCode();
329         if (!par_->idxHome(idx_, cursor_)) 
330                 pop();
331         dump("Home 2");
332 }
333
334
335 void MathCursor::End()
336 {
337         dump("End 1");
338         if (macro_mode)
339                 MacroModeClose();
340         clearLastCode();
341         if (!par_->idxEnd(idx_, cursor_)) {
342                 pop();
343                 array().next(cursor_);
344         }
345         dump("End 2");
346 }
347
348
349 void MathCursor::insert(char c, MathTextCodes t)
350 {
351         //lyxerr << "inserting '" << c << "'\n";
352         if (selection)
353                 SelDel();
354
355         if (t == LM_TC_MIN)
356                 t = lastcode;
357
358         if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
359                 MacroModeClose();
360
361         if (macro_mode) {
362                 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
363                         // was MacroModeinsert(c);
364                         imacro->SetName(imacro->name() + static_cast<char>(c));
365                         return;
366                 }
367         }
368
369         array().insert(cursor_, c, t);
370         array().next(cursor_);
371
372         lastcode = t;
373 }
374
375
376 void MathCursor::insert(MathInset * p)
377 {
378         MacroModeClose();
379
380         if (selection) {
381                 if (p->nargs())
382                         SelCut();
383                 else
384                         SelDel();
385         }
386
387         array().insert(cursor_, p);
388         array().next(cursor_);
389 }
390
391
392 void MathCursor::Delete()
393 {
394         dump("Delete 1");
395         if (macro_mode)
396                 return;
397
398         if (selection) {
399                 SelDel();
400                 return;
401         }
402
403         if (cursor_ < array().size())
404                 array().erase(cursor_);
405
406         // delete empty cells if necessary
407         if (cursor_ == 0 && array().size() == 0) {
408                 bool popit;
409                 bool removeit;
410                 par_->idxDelete(idx_, popit, removeit);
411                 if (popit && pop() && removeit)
412                         Delete();
413         }
414
415 #ifdef WITH_WARNINGS
416 #warning pullArg disabled
417 #endif
418         //if (cursor_ == 0 && !path_.empty()) {
419         //      lyxerr << "Delete: popping...\n";
420         //      pop();
421         //}
422
423         dump("Delete 2");
424 }
425
426
427 void MathCursor::DelLine()
428 {
429         MacroModeClose();
430
431         if (selection) {
432                 SelDel();
433                 return;
434         }
435
436         if (par_->nrows() > 1)
437                 par_->delRow(row());
438 }
439
440
441 bool MathCursor::Up(bool sel)
442 {
443         dump("Up 1");
444         MacroModeClose();
445         SelHandle(sel);
446         SelClear();
447
448         // check whether we could move into an inset on the right or on the left
449         MathInset * p = nextInset();
450         if (p) {
451                 int idx, cursor;
452                 if (p->idxFirstUp(idx, cursor)) {
453                         push(p, true);
454                         par_ = p;
455                         idx_    = idx;
456                         cursor_ = cursor;
457                         dump("Up 3");
458                         return true;
459                 }
460         }
461
462         p = prevInset();
463         if (p) {
464                 int idx, cursor;
465                 if (p->idxLastUp(idx, cursor)) {
466                         array().prev(cursor_);
467                         push(p, false);
468                         par_ = p;
469                         idx_    = idx;
470                         cursor_ = cursor;
471                         dump("Up 4");
472                         return true;
473                 }
474         }
475
476
477         int x = xarray().pos2x(cursor_);
478         bool result = par_->idxUp(idx_, cursor_);
479         if (!result && pop()) {
480                 result = par_->idxUp(idx_, cursor_);
481         }
482         cursor_ = xarray().x2pos(x);
483
484         dump("Up 2");
485         return result;
486 }
487
488
489 bool MathCursor::Down(bool sel)
490 {
491         dump("Down 1");
492         MacroModeClose();
493         SelHandle(sel);
494         SelClear();
495
496         // check whether we could move into an inset on the right or on the left
497         MathInset * p = nextInset();
498         if (p) {
499                 int idx, cursor;
500                 if (p->idxFirstDown(idx, cursor)) {
501                         push(p, true);
502                         idx_    = idx;
503                         cursor_ = cursor;
504                         dump("Down 3");
505                         return true;
506                 }
507         }
508
509         p = prevInset();
510         if (p) {
511                 int idx, cursor;
512                 if (p->idxLastDown(idx, cursor)) {
513                         array().prev(cursor_);
514                         push(p, false);
515                         idx_    = idx;
516                         cursor_ = cursor;
517                         dump("Down 4");
518                         return true;
519                 }
520         }
521
522         int x = xarray().pos2x(cursor_);
523         bool result = par_->idxDown(idx_, cursor_);
524         if (!result && pop()) {
525                 result = par_->idxDown(idx_, cursor_);
526         }
527         cursor_ = xarray().x2pos(x);
528
529         dump("Down 2");
530         return result;
531 }
532
533
534 bool MathCursor::toggleLimits()
535 {
536         if (!prevIsInset())
537                 return false;
538         MathInset * p = prevInset();
539         int old = p->limits();
540         p->limits(old < 0 ? 1 : -1);
541         return old != p->limits();
542 }
543
544
545 void MathCursor::SetSize(MathStyles size)
546 {
547         par_->UserSetSize(size);
548 }
549
550
551
552 void MathCursor::Interpret(string const & s)
553 {
554         lyxerr << "Interpret: '" << s << "'  ('" << s.substr(0, 7)  << "' " <<
555 in_word_set(s) << " \n";
556
557         if (s[0] == '^') {
558                 MathUpDownInset * p = nearbyUpDownInset();
559                 if (!p) {
560                         p = new MathScriptInset(true, false);
561                         insert(p);
562                         array().prev(cursor_);
563                 }
564                 push(p, true);
565                 p->up(true);
566                 idx_ = 0;
567                 return;
568         }
569
570         if (s[0] == '_') {
571                 MathUpDownInset * p = nearbyUpDownInset();
572                 if (!p) {
573                         p = new MathScriptInset(false, true);
574                         insert(p);
575                         array().prev(cursor_);
576                 }
577                 push(p, true);
578                 p->down(true);
579                 idx_ = 1;
580                 return;
581         }
582
583         if (s[0] == '!' || s[0] == ','  || s[0] == ':' || s[0] == ';') {
584                 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
585                 insert(new MathSpaceInset(sp));
586                 return;
587         }
588
589         MathInset * p = 0;
590         latexkeys const * l = in_word_set(s);
591
592         if (l == 0) {
593                 if (s == "root") 
594                         p = new MathRootInset;
595                 else if (MathMacroTable::hasTemplate(s))
596                         p = new MathMacro(MathMacroTable::provideTemplate(s));
597                 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
598                         int m = 1;
599                         int n = 1;
600                         string v_align;
601                         string h_align;
602                         istringstream is(s.substr(7).c_str());
603                         is >> m >> n >> v_align >> h_align;
604                         m = std::max(1, m);
605                         n = std::max(1, n);
606                         MathArrayInset * pp = new MathArrayInset(m, n);
607                         pp->valign(v_align[0]);
608                         pp->halign(h_align);
609                         p = pp;
610                 }
611                 else
612                         p = new MathFuncInset(s, LM_OT_UNDEF);
613         } else {
614                 switch (l->token) {
615                         case LM_TK_BIGSYM: 
616                                         p = new MathBigopInset(l->name, l->id);
617                                         break;
618                                 
619                         case LM_TK_SYM: 
620                                 if (l->id < 255)
621                                         insert(l->id, MathIsBOPS(l->id) ? LM_TC_BOPS : LM_TC_SYMB);
622                                 else
623                                         p = new MathFuncInset(l->name);
624                                 break;
625
626                         case LM_TK_STACK:
627                                 p = new MathFracInset("stackrel");
628                                 break;
629
630                         case LM_TK_FRAC:
631                                 p = new MathFracInset("frac");
632                                 break;
633
634                         case LM_TK_SQRT:
635                                 p = new MathSqrtInset;
636                                 break;
637
638                         case LM_TK_DECORATION:
639                                 p = new MathDecorationInset(l->name, l->id);
640                                 break;
641
642                         case  LM_TK_FUNCLIM:
643                                 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
644                                 break;
645
646                         case LM_TK_SPACE:
647                                 p = new MathSpaceInset(l->id);
648                                 break;
649
650                         case LM_TK_DOTS:
651                                 p = new MathDotsInset(l->name, l->id);
652                                 break;
653
654                         case LM_TK_MACRO:
655                                 p = new MathMacro(MathMacroTable::provideTemplate(s));
656                                 break;
657
658                         default:
659                                 p = new MathFuncInset(l->name);
660                                 break;
661                 }
662         }
663
664         if (p) {
665                 bool oldsel = selection;
666                 if (oldsel) 
667                         SelCut();
668                 insert(p);
669                 if (p->nargs()) {
670                         array().prev(cursor_);
671                         push(p, true);
672                         if (oldsel) 
673                                 SelPaste();
674                 }
675                 p->Metrics(p->size());
676         }
677 }
678
679
680 void MathCursor::MacroModeOpen()
681 {
682         if (!macro_mode) {
683                 imacro = new MathFuncInset("");
684                 insert(imacro);
685                 macro_mode = true;
686         } else
687                 lyxerr << "Math Warning: Already in macro mode" << endl;
688 }
689
690
691 void MathCursor::MacroModeClose()
692 {
693         if (macro_mode)  {
694                 macro_mode = false;
695                 latexkeys const * l = in_word_set(imacro->name());
696                 if (!imacro->name().empty()
697                                 && (!l || (l && IsMacro(l->token, l->id)))
698                                 && !MathMacroTable::hasTemplate(imacro->name()))
699                 {
700                         if (!l) {
701                                 //imacro->SetName(macrobf);
702                                 // This guarantees that the string will be removed by destructor
703                                 imacro->SetType(LM_OT_UNDEF);
704                         } else
705                                 imacro->SetName(l->name);
706                 } else {
707                         Left();
708                         array().erase(cursor_);
709                         if (l || MathMacroTable::hasTemplate(imacro->name())) 
710                                 Interpret(imacro->name());
711                         imacro->SetName(string());
712                 }
713                 imacro = 0;
714         }
715 }
716
717
718 void MathCursor::SelCopy()
719 {
720         seldump("SelCopy");
721         if (selection) {
722                 int const p1 = min(cursor_, anchor_);
723                 int const p2 = max(cursor_, anchor_);
724                 selarray = array();
725                 selarray.erase(p2, selarray.size());
726                 selarray.erase(0, p1);
727                 SelClear();
728         }
729 }
730
731 void MathCursor::SelCut()
732 {
733         seldump("SelCut");
734         if (selection) {
735                 int const p1 = min(cursor_, anchor_);
736                 int const p2 = max(cursor_, anchor_);
737                 cursor_ = p1;  // move cursor to a same position
738                 selarray = array();
739                 selarray.erase(p2, selarray.size());
740                 selarray.erase(0, p1);
741                 array().erase(p1, p2);
742                 SelClear();
743         }
744 }
745
746
747 void MathCursor::SelDel()
748 {
749         seldump("SelDel");
750         if (selection) {
751                 int const p1 = min(cursor_, anchor_);
752                 int const p2 = max(cursor_, anchor_);
753                 array().erase(p1, p2);
754                 SelClear();
755         }
756 }
757
758
759 void MathCursor::SelPaste()
760 {
761         seldump("SelPaste");
762         array().insert(cursor_, selarray);
763         cursor_ += selarray.size();
764         SelClear();
765 }
766
767 void MathCursor::SelHandle(bool sel)
768 {
769         if (sel && !selection)
770                 SelStart();
771         if (!sel && selection)
772                 SelClear();
773 }
774
775
776 void MathCursor::SelStart()
777 {
778         seldump("SelStart");
779         if (selection)
780                 return;
781
782         anchor_   = cursor_;
783         selection = true;
784 }
785
786
787 void MathCursor::SelClear()
788 {
789         selection = false;
790 }
791
792
793
794 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
795 {
796         if (!selection) {
797                 n = 0;
798                 xpoint[0] = 0;
799                 ypoint[0] = 0;
800                 return;
801         }
802
803         // Balance anchor and cursor
804         int xo;
805         int yo;
806         par()->GetXY(xo, yo);
807         int w = par()->width();
808         // cursor
809         int x1 = xarray().xo() + xarray().pos2x(cursor_);
810         int y1 = xarray().yo();
811         //int a1 = xarray().ascent();
812         //int d1 = xarray().descent();
813
814         // anchor
815         int x  = xarray().xo() + xarray().pos2x(anchor_);
816         int y  = xarray().yo();
817         int a  = xarray().ascent();
818         int d  = xarray().descent();
819
820         // single row selection
821         n = 0;
822         xpoint[n]   = x;
823         ypoint[n++] = y + d;
824         xpoint[n]   = x;
825         ypoint[n++] = y - a;
826
827         if (y != y1) {
828                 xpoint[n]   = xo + w;
829                 ypoint[n++] = y - a;
830
831                 if (x1 < xo + w) {
832                         xpoint[n]   = xo + w;
833                         ypoint[n++] = y1 - a;
834                 }
835         }
836
837         xpoint[n]   = x1;
838         ypoint[n++] = y1 - a;
839         xpoint[n]   = x1;
840         ypoint[n++] = y1 + d;
841
842         if (y != y1) {
843                 xpoint[n]   = xo;
844                 ypoint[n++] = y1 + d;
845                 if (x > xo) {
846                         xpoint[n]   = xo;
847                         ypoint[n++] = y + d;
848                 }
849         }
850         xpoint[n]   = xpoint[0];
851         ypoint[n++] = ypoint[0];
852
853         //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
854         //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
855         //for (i = 0; i < np; ++i)
856         //      lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
857 }
858
859
860 void MathCursor::handleFont(MathTextCodes t)
861 {
862         if (selection)  {
863                 int const p1 = std::min(cursor_, anchor_);
864                 int const p2 = std::max(cursor_, anchor_);
865                 MathArray & ar = array();
866                 for (int pos = p1; pos != p2; ar.next(pos))
867                         if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) { 
868                                 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
869                                 ar.setCode(pos, c);
870                         }
871         } else {
872                 if (lastcode == t)
873                         lastcode = LM_TC_VAR;
874                 else
875                         lastcode = t;
876         }
877 }
878
879
880 void MathCursor::handleAccent(string const & name, int code)
881 {
882         MathDecorationInset * p = new MathDecorationInset(name, code);
883         if (selection) {
884                 SelCut();
885                 p->cell(0) = selarray;
886         }
887         insert(p);
888         push(p, true);
889 }
890
891 void MathCursor::handleDelim(int l, int r)
892 {
893         MathDelimInset * p = new MathDelimInset(l, r);
894         if (selection) {
895                 SelCut();
896                 p->cell(0) = selarray;
897         }
898         insert(p);
899         push(p, true);
900 }
901
902
903 void MathCursor::GetPos(int & x, int & y)
904 {
905         x = xarray().xo() + xarray().pos2x(cursor_);
906         y = xarray().yo();
907 }
908
909
910 MathTextCodes MathCursor::nextCode() const
911 {
912         return array().GetCode(cursor_); 
913 }
914
915
916 MathTextCodes MathCursor::prevCode() const
917 {
918         return array().GetCode(cursor_ - 1); 
919 }
920
921
922 MathInset * MathCursor::par() const
923 {
924         return par_;
925 }
926
927
928 InsetFormulaBase const * MathCursor::formula()
929 {
930         return formula_;
931 }
932
933
934 int MathCursor::pos() const
935 {
936         return cursor_;
937 }
938
939
940 bool MathCursor::InMacroMode() const
941 {
942         return macro_mode;
943 }
944
945
946 bool MathCursor::Selection() const
947 {
948         return selection;
949 }
950
951
952 void MathCursor::clearLastCode()
953 {
954         lastcode = LM_TC_MIN;
955 }
956
957
958 void MathCursor::setLastCode(MathTextCodes t)
959 {
960         lastcode = t;
961 }
962
963
964 MathTextCodes MathCursor::getLastCode() const
965 {
966         return lastcode;
967 }
968
969
970 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
971 {
972         if (par_->GetType() == t) {
973                 //lyxerr << "enclosing par is current\n";
974                 idx = idx_;
975                 return par_;
976         }
977         for (int i = path_.size() - 1; i >= 0; --i) {
978                 lyxerr << "checking level " << i << "\n";
979                 if (path_[i].par_->GetType() == t) {
980                         idx = path_[i].idx_;
981                         return path_[i].par_;
982                 }
983         }
984         return 0;
985 }
986
987 void MathCursor::pullArg()
988 {
989         // pullArg
990         MathArray a = array();
991         if (!Left())
992                 return;
993         normalize();
994         array().erase(cursor_);
995         array().insert(cursor_, a);
996 }
997
998
999 MathStyles MathCursor::style() const
1000 {
1001         return xarray().style();
1002 }
1003
1004
1005 void MathCursor::normalize() const
1006 {
1007 #ifdef WITH_WARNINGS
1008 #warning This is evil!
1009 #endif
1010         MathCursor * it = const_cast<MathCursor *>(this);
1011
1012         if (idx_ < 0 || idx_ > par_->nargs())
1013                 lyxerr << "this should not really happen - 1\n";
1014         it->idx_    = max(idx_, 0);
1015         it->idx_    = min(idx_, par_->nargs());
1016
1017         if (cursor_ < 0 || cursor_ > array().size())
1018                 lyxerr << "this should not really happen - 2\n";
1019         it->cursor_ = max(cursor_, 0);
1020         it->cursor_ = min(cursor_, array().size());
1021 }
1022
1023
1024 int MathCursor::col() const
1025 {
1026         return par_->col(idx_);
1027 }
1028
1029
1030 int MathCursor::row() const
1031 {
1032         return par_->row(idx_);
1033 }
1034
1035
1036 /*
1037 char MathIter::GetChar() const
1038 {
1039         return array().GetChar(cursor_);
1040 }
1041
1042
1043 string MathIter::readString()
1044 {
1045         string s;
1046         int code = nextCode();
1047         for ( ; OK() && nextCode() == code; Next()) 
1048                 s += GetChar();
1049
1050         return s;
1051 }
1052 */
1053
1054 MathInset * MathCursor::prevInset() const
1055 {
1056         normalize();
1057         int c = cursor_;
1058         if (!array().prev(c))
1059                 return 0;
1060         return array().nextInset(c);
1061 }
1062
1063
1064 MathInset * MathCursor::nextInset() const
1065 {
1066         normalize();
1067         return array().nextInset(cursor_);
1068 }
1069
1070
1071 MathUpDownInset * MathCursor::nearbyUpDownInset() const
1072 {
1073         normalize();
1074         MathInset * p = array().prevInset(cursor_);
1075         if (p && p->isUpDownInset())
1076                 return static_cast<MathUpDownInset *>(p);
1077         p = array().nextInset(cursor_);
1078         if (p && p->isUpDownInset())
1079                 return static_cast<MathUpDownInset *>(p);
1080         return 0;
1081 }
1082
1083
1084 MathArray & MathCursor::array() const
1085 {
1086         static MathArray dummy;
1087         if (!par_) {
1088                 lyxerr << "############  par_ not valid\n";
1089                 return dummy;
1090         }
1091
1092         if (idx_ < 0 || idx_ >= par_->nargs()) {
1093                 lyxerr << "############  idx_ " << idx_ << " not valid\n";
1094                 return dummy;
1095         }
1096
1097         return par_->cell(idx_);
1098 }
1099
1100
1101 MathXArray & MathCursor::xarray() const
1102 {
1103         return par_->xcell(idx_);
1104 }
1105
1106
1107
1108 bool MathCursor::nextIsInset() const
1109 {
1110         return cursor_ < array().size() && MathIsInset(nextCode());
1111 }
1112
1113
1114 bool MathCursor::prevIsInset() const
1115 {
1116         return cursor_ > 0 && MathIsInset(prevCode());
1117 }
1118
1119
1120 int MathCursor::xpos() const 
1121 {
1122         normalize();
1123         return xarray().pos2x(cursor_);
1124 }
1125
1126 void MathCursor::gotoX(int x)
1127 {
1128         cursor_ = xarray().x2pos(x);    
1129 }
1130
1131 void MathCursor::idxNext()
1132 {
1133         par_->idxNext(idx_, cursor_);
1134 }
1135
1136 void MathCursor::idxPrev()
1137 {
1138         par_->idxPrev(idx_, cursor_);
1139 }
1140
1141 void MathCursor::splitCell()
1142 {
1143         if (idx_ == par_->nargs() - 1) 
1144                 return;
1145         MathArray ar = array();
1146         ar.erase(0, cursor_);
1147         array().erase(cursor_, array().size());
1148         ++idx_;
1149         cursor_ = 0;
1150         array().insert(0, ar);
1151 }
1152
1153 void MathCursor::breakLine()
1154 {
1155         MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1156         if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1157                 p->mutate(LM_OT_EQNARRAY);
1158                 p->addRow(row());
1159                 idx_ = p->nrows();
1160                 cursor_ = 0;
1161         } else {
1162                 p->addRow(row());
1163
1164                 // split line
1165                 const int r = row();
1166                 for (int c = col() + 1; c < p->ncols(); ++c) {
1167                         const int i1 = p->index(r, c);
1168                         const int i2 = p->index(r + 1, c);      
1169                         lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1170                         p->cell(i1).swap(p->cell(i2));
1171                 }
1172
1173                 // split cell
1174                 splitCell();
1175                 p->cell(idx_).swap(p->cell(idx_ + p->ncols() - 1));
1176         }
1177 }
1178
1179 char MathCursor::valign() const
1180 {
1181         int idx;
1182         MathGridInset * p =
1183                 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1184         return p ? p->valign() : 0;
1185 }
1186
1187 char MathCursor::halign() const
1188 {
1189         int idx;
1190         MathGridInset * p =
1191                 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1192         return p ? p->halign(idx % p->ncols()) : 0;
1193 }