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