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