]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
remove cruft
[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, Mathed & 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 FORMS_H_LOCATION
24 #include "math_inset.h"
25 #include "math_parser.h"
26 #include "math_cursor.h"
27 #include "math_macro.h"
28 #include "math_macrotable.h"
29 #include "math_root.h"
30 #include "support/lstrings.h"
31 #include "debug.h"
32 #include "LColor.h"
33 #include "Painter.h"
34 #include "math_matrixinset.h"
35 #include "math_rowst.h"
36 #include "math_spaceinset.h"
37 #include "math_funcinset.h"
38 #include "math_bigopinset.h"
39 #include "math_fracinset.h"
40 #include "math_decorationinset.h"
41 #include "math_dotsinset.h"
42 #include "math_accentinset.h"
43 #include "mathed/support.h"
44
45 static MathedArray * selarray = 0;
46
47 using std::endl;
48
49 // This was very smaller, I'll change it later
50 static inline
51 bool IsMacro(short tok, int id)
52 {
53         return tok != LM_TK_STACK &&
54                tok != LM_TK_FRAC &&
55                tok != LM_TK_SQRT &&
56                tok != LM_TK_WIDE &&
57                tok != LM_TK_SPACE &&
58                tok != LM_TK_DOTS &&
59                tok != LM_TK_FUNCLIM &&
60                tok != LM_TK_BIGSYM &&
61                tok != LM_TK_ACCENT &&
62                !(tok == LM_TK_SYM && id < 255);
63 }
64
65 static int const MAX_STACK_ITEMS = 32;
66
67 struct MathStackXIter {
68         int i, imax;
69         MathedXIter * item;
70
71         MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
72                 item = new MathedXIter[imax];
73                 i = 0;
74         }
75
76         MathStackXIter(MathStackXIter & stk);
77
78         ~MathStackXIter() {
79                 delete[] item;
80         }
81
82         void push(MathedXIter ** a) {
83                 *a = &item[i++];
84         }
85
86         MathedXIter * pop() {
87                 --i;
88                 return &item[i - 1];
89         }
90
91         MathedXIter * Item(int idx) {
92                 return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
93         }
94
95         void Reset() {
96                 i = 0;
97         }
98
99         bool Full() {
100                 return i >= MAX_STACK_ITEMS;
101         }
102
103         bool Empty() {
104                 return i <= 1;
105         }
106
107         int Level() { return i; }
108
109 } mathstk, *selstk = 0;
110
111
112 MathStackXIter::MathStackXIter(MathStackXIter & stk)
113 {
114         imax = stk.imax;
115         item = new MathedXIter[imax];
116         i = stk.i;
117         for (int k = 0; k < i; ++k) {
118                 item[k].SetData(stk.item[k].getPar());
119                 item[k].GoBegin();
120                 item[k].goPosAbs(stk.item[k].getPos());
121         }
122 }
123
124
125 /***----------------  Mathed Cursor  ---------------------------***/
126
127 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
128 {
129         accent   = 0;
130         anchor   = 0;
131         lastcode = LM_TC_MIN;
132         SetPar(p);
133         //    selarray = 0;
134         if (!MathMacroTable::built)
135                 MathMacroTable::mathMTable.builtinMacros();
136 }
137
138
139 void MathedCursor::SetPar(MathParInset * p)
140 {
141         win        = 0;
142         is_visible = False;
143         macro_mode = false;
144         selection  = false; // not SelClear() ?
145         mathstk.Reset();
146         mathstk.push(&cursor);
147         par = p;
148         cursor->SetData(par);
149 }
150
151
152 void MathedCursor::draw(Painter & pain, int x, int y)
153 {
154         //    lyxerr << "Cursor[" << x << " " << y << "] ";
155         //win = pm;    // win = (mathedCanvas) ? mathedCanvas: pm;
156
157         par->Metrics();
158         int w = par->Width() + 2;
159         int a = par->Ascent() + 1;
160         int h = par->Height() + 1;
161
162         if (par->GetType() > LM_OT_PAR) {
163                 a += 4;
164                 h += 8;
165         }
166
167         pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
168
169         par->draw(pain, x, y);
170         cursor->Adjust();
171 }
172
173
174 void MathedCursor::Redraw(Painter & pain)
175 {
176         lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
177         par->Metrics();
178         int w = par->Width();
179         int h = par->Height();
180         int x;
181         int y;
182         par->GetXY(x, y);
183         //mathed_set_font(LM_TC_VAR, 1);
184         pain.fillRectangle(x, y - par->Ascent(),
185         x + w, y - par->Ascent() + h,
186         LColor::mathbg);
187         par->draw(pain, x, y);
188 }
189
190
191 bool MathedCursor::Left(bool sel)
192 {
193         if (macro_mode) {
194                 MacroModeBack();
195                 return true;
196         }
197
198         clearLastCode();
199
200         if (sel && !selection)
201                 SelStart();
202
203         if (!sel && selection)
204                 SelClear();
205
206         bool result = cursor->Prev();
207
208         if (!result && !mathstk.Empty()) {
209                 cursor = mathstk.pop();
210                 cursor->Adjust();
211                 result = true;
212                 if (selection)
213                         SelClear();
214         } else
215         if (result && cursor->IsActive()) {
216                 if (cursor->IsScript()) {
217                         cursor->Prev();
218                         if (!cursor->IsScript())
219                                 cursor->Next();
220                         cursor->Adjust();
221                         return true;
222                 }
223
224                 if (!selection) {
225                         MathParInset * p = cursor->GetActiveInset();
226                         if (!p)
227                                 return result;
228
229                         p->setArgumentIdx(p->getMaxArgumentIdx());
230                         mathstk.push(&cursor);
231                         cursor->SetData(p);
232                         cursor->GoLast();
233                 }
234         }
235         return result;
236 }
237
238
239 // Leave the inset
240 bool MathedCursor::Pop()
241 {
242         if (!mathstk.Empty()) {
243                 cursor = mathstk.pop();
244                 cursor->Next();
245                 return true;
246         }
247         return false;
248 }
249
250
251 // Go to the inset
252 bool MathedCursor::Push()
253 {
254         if (cursor->IsActive()) {
255                 MathParInset * p = cursor->GetActiveInset();
256                 if (!p)
257                         return false;
258                 mathstk.push(&cursor);
259                 cursor->SetData(p);
260                 return true;
261         }
262         return false;
263 }
264
265
266 bool MathedCursor::Right(bool sel)
267 {
268         if (macro_mode) {
269                 MacroModeClose();
270                 return true;
271         }
272
273         clearLastCode();
274
275         if (sel && !selection)
276                 SelStart();
277
278         if (!sel && selection)
279                 SelClear();
280
281         bool result = false;
282
283         if (cursor->IsActive()) {
284                 if (cursor->IsScript()) {
285                         cursor->Next();
286                         // A script may be followed by another script
287                         if (cursor->IsScript())
288                                 cursor->Next();
289                         return true;
290                 }
291
292                 if (!selection) {
293                         MathParInset *p = cursor->GetActiveInset();
294                         if (!p) {
295                         lyxerr << "Math error: Inset expected." << endl;
296                         return cursor->Next();
297                         }
298                         p->setArgumentIdx(0);
299                         mathstk.push(&cursor);
300                         cursor->SetData(p);
301                         result = true;
302                 } else
303                         result = cursor->Next();
304
305         } else {
306                 if (cursor->GetChar()!= LM_TC_CR)
307                 result = cursor->Next();
308                 if (!result && !mathstk.Empty()) {
309                         cursor = mathstk.pop();
310                         cursor->Next();
311                         cursor->Adjust();
312                         result = true;
313                         if (selection)
314                                 SelClear();
315                 }
316         }
317         return result;
318 }
319
320
321 void MathedCursor::SetPos(int x, int y)
322 {
323         int xp = 0;
324
325         if (macro_mode)
326                 MacroModeClose();
327
328         lastcode = LM_TC_MIN;
329         mathstk.Reset();
330         mathstk.push(&cursor);
331         cursor->SetData(par);
332         cursor->fitCoord(x, y);
333
334         while (cursor->GetX()<x && cursor->OK()) {
335                 if (cursor->IsActive()) {
336                         MathParInset * p = cursor->GetActiveInset();
337                         if (p->Inside(x, y)) {
338                                 p->SetFocus(x, y);
339                                 mathstk.push(&cursor);
340                                 cursor->SetData(p);
341                                 cursor->fitCoord(x, y);
342                                 continue;
343                         }
344                 }
345                 xp = cursor->GetX();
346                 cursor->ipush();
347                 if (!cursor->Next() && !Pop())
348                         break;
349         }
350         if (x-xp < cursor->GetX()-x) cursor->ipop();
351         cursor->Adjust();
352 }
353
354
355 void MathedCursor::Home()
356 {
357         if (macro_mode)
358                 MacroModeClose();
359         clearLastCode();
360         mathstk.Reset();
361         mathstk.push(&cursor);
362         cursor->GoBegin();
363 }
364
365
366 void MathedCursor::End()
367 {
368         if (macro_mode)
369                 MacroModeClose();
370         clearLastCode();
371         mathstk.Reset();
372         mathstk.push(&cursor);
373         cursor->GoLast();
374 }
375
376
377 MathMatrixInset * create_multiline(short int type, int cols)
378 {
379         int columns;
380         string align;
381         if (cols < 1)
382                 cols = 1;
383
384         switch (type) {
385                 case LM_OT_ALIGN:
386                 case LM_OT_ALIGNN:
387                         columns = 2*cols;
388                         for (int i = 0; i < cols; ++i)
389                         align += "Rl";
390                         break;
391
392                 case LM_OT_ALIGNAT:
393                 case LM_OT_ALIGNATN:
394                         columns = 2*cols;
395                         for (int i = 0; i < cols; ++i)
396                         align += "rl";
397                         break;
398
399                 case LM_OT_MULTLINE:
400                 case LM_OT_MULTLINEN:
401                         columns = 1;
402                         align = "C";
403                         break;
404
405                 case LM_OT_MPAR:
406                 case LM_OT_MPARN:
407                 default:
408                         columns = 3;
409                         align = "rcl";
410                         break;
411         }
412
413         MathMatrixInset * mt = new MathMatrixInset(columns, -1);
414         mt->SetAlign(' ', align);
415         return mt;
416 }
417
418
419 void MathedCursor::Insert(byte c, MathedTextCodes t)
420 {
421         if (selection)
422                 SelDel();
423
424         if (t == LM_TC_MIN)
425                 t = lastcode;
426
427         if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
428                 MacroModeClose();
429
430         if (t == LM_TC_CR) {
431                 MathParInset * p = cursor->p;
432                 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
433                         short int type = LM_OT_MPAR;
434                         int cols = 1;
435                         if (c >= '1' && c <= '9') {
436                                 type = LM_OT_ALIGN;
437                                 cols = c - '1' + 1;
438                         } else if (c >= 'A' && c <= 'I') {
439                                 type = LM_OT_ALIGNAT;
440                                 cols = c - 'A' + 1;
441                         } else if (c == 'm')
442                                 type = LM_OT_MULTLINE;
443                         else if (c == 'e')
444                                 type = LM_OT_MPAR;
445
446                         if (p->GetType() == LM_OT_PARN)
447                                 ++type;
448                         MathMatrixInset * mt = create_multiline(type, cols);
449                         mt->SetStyle(LM_ST_DISPLAY);
450                         mt->SetType(type);
451                         mt->SetData(p->GetData());
452                         p->SetData(0);                          // BUG duda
453                         delete p;
454                         par = mt;
455                         p = mt;
456                         p->Metrics();
457                         int pos = cursor->getPos();
458                         cursor->SetData(par);
459                         cursor->goPosAbs(pos);
460                 }
461                 if (p &&  p->Permit(LMPF_ALLOW_CR)) {
462                         cursor->addRow();
463                 }
464         } else if (t == LM_TC_TAB) {
465                 MathParInset * p = cursor->p;
466                 if (p &&  p->Permit(LMPF_ALLOW_TAB)) {
467                         if (c) {
468                                 cursor->Insert(c, t);
469                                 cursor->checkTabs();
470                         } else
471                                 cursor->goNextColumn();
472                 } else // Navigate between arguments
473                 if (p && p->GetType() == LM_OT_MACRO) {
474                         if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
475                                 p->setArgumentIdx(p->getArgumentIdx() + 1);
476                                 cursor->SetData(p);
477                                 return;
478                         }
479                 }
480         } else {
481                 if (macro_mode) {
482                         if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
483                                 MacroModeInsert(c);
484                                 return;
485                         }
486                 }
487
488                 if (accent) {
489                         doAccent(c, t);
490                 } else
491                         cursor->Insert(c, t);
492
493                 lastcode = t;
494                 return;
495         }
496         clearLastCode();
497 }
498
499
500 void MathedCursor::Insert(MathedInset * p, int t)
501 {
502         if (macro_mode)
503                 MacroModeClose();
504
505         if (selection) {
506                 if (MathIsActive(t)) {
507                         SelCut();
508                         static_cast<MathParInset*>(p)->SetData(selarray);
509                 } else
510                         SelDel();
511         }
512
513         if (mathstk.i < MAX_STACK_ITEMS - 1) {
514                 if (accent && !MathIsActive(t)) {       
515                         doAccent(p);
516                 } else {
517                         cursor->Insert(p, t);
518                         if (MathIsActive(t)) {
519                                 cursor->Prev();
520                                 Push();
521                         }
522                 }
523         } else
524                 lyxerr << "Math error: Full stack." << endl;
525 }
526
527
528 void MathedCursor::Delete()
529 {
530         if (macro_mode)
531                 return;
532
533         if (selection) {
534                 SelDel();
535                 return;
536         }
537
538         if (cursor->Empty() && !mathstk.Empty()) 
539                 cursor = mathstk.pop();
540
541         //   if (cursor->GetChar()!= LM_TC_TAB)
542         cursor->Delete();
543         cursor->checkTabs();
544 }
545
546
547 void MathedCursor::DelLine()
548 {
549         if (macro_mode)
550                 MacroModeClose();
551
552         if (selection) {
553                 SelDel();
554                 return;
555         }
556
557         MathParInset * p = cursor->p;
558
559         if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
560                 cursor->delRow();
561         }
562 }
563
564
565 bool MathedCursor::Up(bool sel)
566 {
567         bool result = false;
568
569         if (macro_mode)
570                 MacroModeClose();
571
572         if (sel && !selection)
573                 SelStart();
574
575         if (!sel && selection)
576                 SelClear();
577
578         if (cursor->IsScript()) {
579                 char cd = cursor->GetChar();
580                 if (MathIsUp(cd)) {
581                         Push();
582                         return true;
583                 } else {
584                         // A subscript may be followed by a superscript
585                         cursor->ipush();
586                         cursor->Next();
587                         if (MathIsUp(cursor->GetChar())) {
588                                 Push();
589                                 return true;
590                         } else  // return to the previous state
591                                 cursor->ipop();
592                 }
593         }
594
595         result = cursor->Up();
596         if (!result && cursor->p) {
597                 MathParInset * p = cursor->p;
598
599                 if (p->GetType() == LM_OT_SCRIPT) {
600                         MathedXIter * cx = mathstk.Item(1);
601                         bool is_down = (cx->GetChar() == LM_TC_DOWN);
602                         cursor = mathstk.pop();
603                         cursor->Next();
604                         result =  (is_down) ? true: Up();
605                 } else {
606                         result = (p->getArgumentIdx() > 0);
607                         if (result) {
608                                 p->setArgumentIdx(p->getArgumentIdx() - 1);
609                                 cursor->SetData(p);
610                         }
611                 }
612
613                 if (!result && !mathstk.Empty()) {
614                         cursor = mathstk.pop();
615                         return Up();
616                 }
617         }
618         return result;
619 }
620
621
622 bool MathedCursor::Down(bool sel)
623 {
624         bool result = false;
625
626         if (macro_mode)
627                 MacroModeClose();
628
629         if (sel && !selection)
630                 SelStart();
631
632         if (!sel && selection)
633                 SelClear();
634
635         if (cursor->IsScript()) {
636                 char cd = cursor->GetChar();
637                 if (MathIsDown(cd)) {
638                         Push();
639                         return true;
640                 } else {
641                 // A superscript may be followed by a subscript
642                 cursor->ipush();
643                 cursor->Next();
644                 if (MathIsDown(cursor->GetChar())) {
645                         Push();
646                         return true;
647                 } else
648                         cursor->ipop();
649                 }
650         }
651
652         result = cursor->Down();
653         if (!result && cursor->p) {
654         MathParInset * p= cursor->p;
655         if (p->GetType() == LM_OT_SCRIPT) {
656         MathedXIter * cx = mathstk.Item(1);
657         bool is_up = (cx->GetChar() == LM_TC_UP);
658         cursor = mathstk.pop();
659         cursor->Next();
660         result = (is_up) ? true: Down();
661         } else {
662         result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
663         if (result) {
664         p->setArgumentIdx(p->getArgumentIdx() + 1);
665         cursor->SetData(p);
666         }
667         }
668         if (!result && !mathstk.Empty()) {
669         cursor = mathstk.pop();
670         return Down(sel);
671         }
672         }
673         return result;
674 }
675
676
677 bool MathedCursor::Limits()
678 {
679         if (cursor->IsInset()) {
680                 MathedInset * p = cursor->GetInset();
681                 bool ol = p->GetLimits();
682                 p->SetLimits(!ol);
683                 return (ol!= p->GetLimits());
684         }
685         return false;
686 }
687
688
689 void MathedCursor::SetSize(short size)
690 {
691         MathParInset * p = cursor->p;
692         p->UserSetSize(size);
693         cursor->SetData(p);
694 }
695
696
697 void MathedCursor::setLabel(string const & label)
698 {
699         // ugly hack and possible bug
700         if (!cursor->setLabel(label))
701                 lyxerr << "MathErr: Bad place to set labels." << endl;
702 }
703
704
705 void MathedCursor::setNumbered()
706 {       
707         // another ugly hack
708         MathedRowSt * crow = cursor->crow;
709         if (!crow)
710                 return;
711         crow->setNumbered(!crow->isNumbered());
712 }
713
714
715 void MathedCursor::Interpret(string const & s)
716 {
717         MathedInset * p = 0;
718         latexkeys * l = 0;
719         MathedTextCodes tcode = LM_TC_INSET;
720
721         if (s[0] == '^' || s[0] == '_') {
722                 char c = cursor->GetChar();
723                 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
724                         Push();
725                         return;
726                 } else
727
728                 // A script may be followed by a script
729                 if (MathIsUp(c)  || MathIsDown(c)) {
730                         cursor->ipush();
731                         cursor->Next();
732                         c = cursor->GetChar();
733                         if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
734                                 Push();
735                                 return;
736                         } else
737                                 cursor->ipop();
738                 }
739                 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
740                 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
741                 return;
742         } else
743         if (s[0] == '!' || s[0] == ','  || s[0] == ':' || s[0] == ';') {
744                 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
745                 p = new MathSpaceInset(sp);
746                 Insert(p);
747                 return;
748         } else
749                 l = in_word_set(s);
750
751         if (!l) {
752                 p = MathMacroTable::mathMTable.getMacro(s);
753                 if (!p) {
754                 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
755                 if (s == "root") {
756                         p = new MathRootInset();
757                         tcode = LM_TC_ACTIVE_INSET;
758                 } else
759                         p = new MathFuncInset(s, LM_OT_UNDEF);
760                 } else {
761                         tcode = static_cast<MathMacro*>(p)->getTCode();
762                         lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
763                 }
764         } else {
765                 MathedInsetTypes fractype = LM_OT_FRAC;
766                 switch (l->token) {
767                         case LM_TK_BIGSYM: 
768                                         p = new MathBigopInset(l->name, l->id);
769                                         break;
770                                 
771                         case LM_TK_SYM: 
772                                         if (l->id<255) {
773                                                 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
774                                                 LM_TC_BOPS: LM_TC_SYMB);        
775                                         } else {
776                                                 p = new MathFuncInset(l->name);
777                                         }
778                                         break;
779                                 
780                         case LM_TK_STACK:
781                                 fractype = LM_OT_STACKREL;
782                                 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
783
784                         case LM_TK_FRAC:
785                                 p = new MathFracInset(fractype);
786                                 tcode = LM_TC_ACTIVE_INSET;
787                                 break;
788
789                         case LM_TK_SQRT:
790                                 p = new MathSqrtInset;
791                                 tcode = LM_TC_ACTIVE_INSET;
792                                 break;
793
794                         case LM_TK_WIDE:
795                                 p = new MathDecorationInset(l->id);
796                                 tcode = LM_TC_ACTIVE_INSET;
797                                 break;
798
799                         case  LM_TK_FUNCLIM:
800                                 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
801                                 break;
802
803                         case LM_TK_SPACE:
804                                 p = new MathSpaceInset(l->id);
805                                 break;
806
807                         case LM_TK_DOTS:
808                                 p = new MathDotsInset(l->name, l->id);
809                                 break;
810
811                         case LM_TK_ACCENT:
812                                 setAccent(l->id);
813                                 break;
814
815                         case LM_TK_MACRO:
816                                 p = MathMacroTable::mathMTable.getMacro(s);
817                                 tcode = static_cast<MathMacro*>(p)->getTCode();
818                                 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
819                                 break;
820
821                         default:
822                                 p = new MathFuncInset(l->name);
823                                 break;
824                 }
825         }
826
827         if (p) {
828                 Insert(p, tcode);
829                 par->Metrics();
830         }
831 }
832
833
834 bool MathedCursor::pullArg()
835 {
836         if (cursor->IsActive()) {
837                 MathParInset * p = cursor->GetActiveInset();
838                 if (!p) 
839                         return false;
840                 
841                 MathedArray * a = p->GetData();
842                 p->SetData(0);
843                 Delete();
844                 if (a) {
845                         cursor->Merge(a);
846                         cursor->Adjust();
847                 }
848
849                 return true;
850         }
851         return false;
852 }
853
854
855 void MathedCursor::MacroModeOpen()
856 {
857         if (!macro_mode) {
858                 imacro = new MathFuncInset("");
859                 Insert (imacro);
860                 macro_mode = true;
861         } else
862                 lyxerr << "Mathed Warning: Already in macro mode" << endl;
863 }
864
865
866 void MathedCursor::MacroModeClose()
867 {
868         if (macro_mode)  {
869                 macro_mode = false;
870                 latexkeys * l = in_word_set(imacro->GetName());
871                 if (!imacro->GetName().empty()
872                 && (!l || (l && IsMacro(l->token, l->id))) &&
873                 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
874                         if (!l) {
875                                 //imacro->SetName(macrobf);
876                                 // This guarantees that the string will be removed by destructor
877                                 imacro->SetType(LM_OT_UNDEF);
878                         } else
879                                 imacro->SetName(l->name);
880                 } else {
881                         Left();
882                         if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
883                                 setAccent(
884                                         static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
885                         }
886                         cursor->Delete();
887                         if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
888                                 Interpret(imacro->GetName());
889                         }
890                         imacro->SetName("");
891                 }
892                 imacro = 0;
893         }
894 }
895
896
897 void MathedCursor::MacroModeBack()
898 {
899         if (macro_mode) {
900                 if (!imacro->GetName().empty()) {
901                         imacro->SetName(
902                                         imacro->GetName().substr(0, imacro->GetName().length() - 1));
903                         imacro->Metrics();
904                 } else
905                         MacroModeClose();
906         } else
907                 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
908 }
909
910
911 void MathedCursor::MacroModeInsert(char c)
912 {
913         if (macro_mode) {
914                 imacro->SetName(imacro->GetName() + c);
915                 imacro->Metrics();
916         } else
917                 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
918 }
919
920
921 void MathedCursor::SelCopy()
922 {
923         if (selection) {
924                 int p1 = (cursor->pos < selpos) ? cursor->pos : selpos;
925                 int p2 = (cursor->pos > selpos) ? cursor->pos : selpos;
926                 selarray = cursor->Copy(p1, p2);
927                 cursor->Adjust();
928                 SelClear();
929         }
930 }
931
932
933 void MathedCursor::SelCut()
934 {
935         if (selection) {
936                 if (cursor->pos == selpos)
937                         return;
938
939                 int p1 = (cursor->pos < selpos) ? cursor->pos : selpos;
940                 int p2 = (cursor->pos > selpos) ? cursor->pos : selpos;
941                 selarray = cursor->Copy(p1, p2);
942                 cursor->Clean(selpos);
943                 cursor->Adjust();
944                 SelClear();
945         }
946 }
947
948
949 void MathedCursor::SelDel()
950 {
951         //  lyxerr << "Deleting sel "
952         if (selection) {
953                 if (cursor->pos == selpos)
954                         return;
955                 cursor->Clean(selpos);
956                 cursor->Adjust();
957                 SelClear();
958         }
959 }
960
961
962 void MathedCursor::SelPaste()
963 {
964         // lyxerr << "paste " << selarray << " " << curor->pos;
965         if (selection)
966                 SelDel();
967
968         if (selarray) {
969                 cursor->Merge(selarray);
970                 cursor->Adjust();
971         }
972 }
973
974
975 void MathedCursor::SelStart()
976 {
977         lyxerr[Debug::MATHED] << "Starting sel " << endl;
978         if (!anchor) {
979                 selpos = cursor->pos;
980                 selstk = new MathStackXIter(mathstk);
981                 anchor = selstk->Item(-1);
982                 anchor->SetData(cursor->p);
983                 anchor->GoBegin();
984                 anchor->goPosAbs(selpos);
985                 selection = true;
986         }
987 }
988
989
990 void MathedCursor::SelClear()
991 {
992         lyxerr[Debug::MATHED] << "Clearing sel " << endl;
993         selection = false;
994         delete selstk;
995         selstk = 0;
996         anchor = 0;
997 }
998
999
1000
1001 // Anchor position must be at the same level that stack.
1002 void MathedCursor::SelBalance()
1003 {
1004         int d = mathstk.Level() - selstk->Level();
1005
1006         // If unbalanced, balance them
1007         while (d != 0) {
1008                 if (d < 0) {
1009                         // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level
1010                         //  << " " << anchor->GetX() << " " << cursor->GetX() << "]";
1011                         anchor = selstk->pop();
1012                         if (anchor->GetX() >= cursor->GetX())
1013                         anchor->Next();
1014                 } else {
1015                         // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
1016                         Pop();
1017                 }
1018                 d = mathstk.Level() - selstk->Level();
1019         }
1020
1021         // Once balanced the levels, check that they are at the same paragraph
1022         selpos = anchor->pos;
1023 }
1024
1025
1026 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
1027 {
1028         static int xpoint[10];
1029         static int ypoint[10];
1030
1031         if (!selection) {
1032                 np = 0;
1033                 xpoint[0] = 0;
1034                 ypoint[0] = 0;
1035                 *xp = &xpoint[0];
1036                 *yp = &ypoint[0];
1037                 return;
1038         }
1039
1040         // single row selection
1041         int i = 0;
1042         int a;
1043         int d;
1044         int a1;
1045         int d1;
1046
1047         // Balance anchor and cursor
1048         SelBalance();
1049
1050         int xo;
1051         int yo;
1052         cursor->p->GetXY(xo, yo);
1053         int w = cursor->p->Width();
1054         int x1;
1055         int y1;
1056         cursor->GetPos(x1, y1);
1057         cursor->getAD(a1, d1);
1058         int x;
1059         int y;
1060         anchor->GetPos(x, y);
1061         anchor->getAD(a, d);
1062
1063         xpoint[i]   = x;
1064         ypoint[i++] = y + d;
1065         xpoint[i]   = x;
1066         ypoint[i++] = y - a;
1067
1068         if (y != y1) {
1069                 xpoint[i]   = xo + w;
1070                 ypoint[i++] = y - a;
1071
1072                 if (x1 < xo + w) {
1073                 xpoint[i]   = xo + w;
1074                 ypoint[i++] = y1 - a;
1075         }
1076         }
1077
1078         xpoint[i]   = x1;
1079         ypoint[i++] = y1 - a;
1080         xpoint[i]   = x1;
1081         ypoint[i++] = y1 + d;
1082
1083         if (y != y1) {
1084                 xpoint[i]   = xo;
1085                 ypoint[i++] = y1 + d;
1086                 if (x > xo) {
1087                         xpoint[i]   = xo;
1088                         ypoint[i++] = y + d;
1089                 }
1090         }
1091         xpoint[i]   = xpoint[0];
1092         ypoint[i++] = ypoint[0];
1093
1094         *xp = &xpoint[0];
1095         *yp = &ypoint[0];
1096         np = i;
1097         //    lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1098         //    lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1099         //    for (i = 0; i < np; ++i)
1100         //      lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1101 }
1102
1103
1104 void MathedCursor::setAccent(int ac)
1105 {
1106         if (ac > 0 && accent < 8) {
1107                 nestaccent[accent++] = ac;
1108         } else
1109                 accent = 0;  // consumed!
1110 }
1111
1112
1113 int MathedCursor::getAccent() const
1114 {
1115         return (accent > 0) ? nestaccent[accent - 1]: 0;
1116 }
1117
1118
1119 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1120 {
1121         MathedInset * ac = 0;
1122
1123         for (int i = accent - 1; i >= 0; --i) {
1124                 if (i == accent - 1)
1125                         ac = new MathAccentInset(c, t, nestaccent[i]);
1126                 else
1127                         ac = new MathAccentInset(ac, nestaccent[i]);
1128         }
1129         
1130         if (ac)
1131                 cursor->Insert(ac);
1132
1133         accent = 0;  // consumed!
1134 }
1135
1136
1137 void MathedCursor::doAccent(MathedInset * p)
1138 {
1139         MathedInset * ac = 0;
1140
1141         for (int i = accent - 1; i >= 0; --i) {
1142                 if (i == accent - 1)
1143                         ac = new MathAccentInset(p, nestaccent[i]);
1144                 else
1145                         ac = new MathAccentInset(ac, nestaccent[i]);
1146         }
1147
1148         if (ac)
1149                 cursor->Insert(ac);
1150
1151         accent = 0;  // consumed!
1152 }
1153
1154
1155 void MathedCursor::toggleLastCode(MathedTextCodes t)
1156 {
1157         if (lastcode == t)
1158                 lastcode = LM_TC_VAR;
1159         else
1160                 lastcode = t;
1161 }
1162
1163
1164 void MathedCursor::GetPos(int & x, int & y)
1165 {
1166         cursor->GetPos(x, y);
1167 }
1168
1169
1170 short MathedCursor::GetFCode()
1171 {
1172         return cursor->FCode();
1173 }
1174
1175
1176 MathParInset * MathedCursor::GetPar()
1177 {
1178         return par;
1179 }
1180
1181
1182 MathParInset * MathedCursor::getCurrentPar() const
1183 {
1184         return cursor->p;
1185 }
1186
1187
1188 string const & MathedCursor::getLabel() const
1189 {
1190         return cursor->getLabel();
1191 }
1192
1193
1194 bool MathedCursor::IsEnd() const
1195 {
1196         return !cursor->OK();
1197 }
1198
1199
1200 bool MathedCursor::InMacroMode()
1201 {
1202         return macro_mode;
1203 }
1204
1205
1206 bool MathedCursor::Selection()
1207 {
1208         return selection;
1209 }
1210
1211
1212 void MathedCursor::clearLastCode()
1213 {
1214         lastcode = LM_TC_MIN;
1215 }
1216
1217
1218 void MathedCursor::setLastCode(MathedTextCodes t)
1219 {
1220         lastcode = t;
1221 }
1222
1223
1224 MathedTextCodes MathedCursor::getLastCode() const
1225 {
1226         return lastcode;
1227 }