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