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